1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.bluetooth.mapclient;
18 
19 import androidx.test.runner.AndroidJUnit4;
20 
21 import org.junit.Assert;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 
25 import java.util.Date;
26 import java.util.TimeZone;
27 
28 @RunWith(AndroidJUnit4.class)
29 public class ObexTimeTest {
30     private static final String TAG = ObexTimeTest.class.getSimpleName();
31 
32     private static final String VALID_TIME_STRING = "20190101T121314";
33     private static final String VALID_TIME_STRING_WITH_OFFSET_POS = "20190101T121314+0130";
34     private static final String VALID_TIME_STRING_WITH_OFFSET_NEG = "20190101T121314-0130";
35 
36     private static final String INVALID_TIME_STRING_OFFSET_EXTRA_DIGITS = "20190101T121314-99999";
37     private static final String INVALID_TIME_STRING_BAD_DELIMITER = "20190101Q121314";
38 
39     // MAP message listing times, per spec, use "local time basis" if UTC offset isn't given. The
40     // ObexTime class parses using the current default timezone (assumed to be the "local timezone")
41     // in the case that UTC isn't provided. However, the Date class assumes UTC ALWAYS when
42     // initializing off of a long value. We have to take that into account when determining our
43     // expected results for time strings that don't have an offset.
44     private static final long LOCAL_TIMEZONE_OFFSET = TimeZone.getDefault().getRawOffset();
45 
46     // If you are a positive offset from GMT then GMT is in the "past" and you need to subtract that
47     // offset from the time. If you are negative then GMT is in the future and you need to add that
48     // offset to the time.
49     private static final long VALID_TS = 1546344794000L; // Jan 01, 2019 at 12:13:14 GMT
50     private static final long TS_OFFSET = 5400000L; // 1 Hour, 30 minutes -> milliseconds
51     private static final long VALID_TS_LOCAL_TZ = VALID_TS - LOCAL_TIMEZONE_OFFSET;
52     private static final long VALID_TS_OFFSET_POS = VALID_TS - TS_OFFSET;
53     private static final long VALID_TS_OFFSET_NEG = VALID_TS + TS_OFFSET;
54 
55     private static final Date VALID_DATE_LOCAL_TZ = new Date(VALID_TS_LOCAL_TZ);
56     private static final Date VALID_DATE_WITH_OFFSET_POS = new Date(VALID_TS_OFFSET_POS);
57     private static final Date VALID_DATE_WITH_OFFSET_NEG = new Date(VALID_TS_OFFSET_NEG);
58 
59     @Test
createWithValidDateTimeString_TimestampCorrect()60     public void createWithValidDateTimeString_TimestampCorrect() {
61         ObexTime timestamp = new ObexTime(VALID_TIME_STRING);
62         Assert.assertEquals("Parsed timestamp must match expected", VALID_DATE_LOCAL_TZ,
63                 timestamp.getTime());
64     }
65 
66     @Test
createWithValidDateTimeStringWithPosOffset_TimestampCorrect()67     public void createWithValidDateTimeStringWithPosOffset_TimestampCorrect() {
68         ObexTime timestamp = new ObexTime(VALID_TIME_STRING_WITH_OFFSET_POS);
69         Assert.assertEquals("Parsed timestamp must match expected", VALID_DATE_WITH_OFFSET_POS,
70                 timestamp.getTime());
71     }
72 
73     @Test
createWithValidDateTimeStringWithNegOffset_TimestampCorrect()74     public void createWithValidDateTimeStringWithNegOffset_TimestampCorrect() {
75         ObexTime timestamp = new ObexTime(VALID_TIME_STRING_WITH_OFFSET_NEG);
76         Assert.assertEquals("Parsed timestamp must match expected", VALID_DATE_WITH_OFFSET_NEG,
77                 timestamp.getTime());
78     }
79 
80     @Test
createWithValidDate_TimestampCorrect()81     public void createWithValidDate_TimestampCorrect() {
82         ObexTime timestamp = new ObexTime(VALID_DATE_LOCAL_TZ);
83         Assert.assertEquals("ObexTime created with a date must return the same date",
84                 VALID_DATE_LOCAL_TZ, timestamp.getTime());
85     }
86 
87     @Test
printValidTime_TimestampMatchesInput()88     public void printValidTime_TimestampMatchesInput() {
89         ObexTime timestamp = new ObexTime(VALID_TIME_STRING);
90         Assert.assertEquals("Timestamp as a string must match the input string", VALID_TIME_STRING,
91                 timestamp.toString());
92     }
93 
94     @Test
createWithInvalidDelimiterString_TimestampIsNull()95     public void createWithInvalidDelimiterString_TimestampIsNull() {
96         ObexTime timestamp = new ObexTime(INVALID_TIME_STRING_BAD_DELIMITER);
97         Assert.assertEquals("Parsed timestamp was invalid and must result in a null object", null,
98                 timestamp.getTime());
99     }
100 
101     @Test
createWithInvalidOffsetString_TimestampIsNull()102     public void createWithInvalidOffsetString_TimestampIsNull() {
103         ObexTime timestamp = new ObexTime(INVALID_TIME_STRING_OFFSET_EXTRA_DIGITS);
104         Assert.assertEquals("Parsed timestamp was invalid and must result in a null object", null,
105                 timestamp.getTime());
106     }
107 
108     @Test
printInvalidTime_ReturnsNull()109     public void printInvalidTime_ReturnsNull() {
110         ObexTime timestamp = new ObexTime(INVALID_TIME_STRING_BAD_DELIMITER);
111         Assert.assertEquals("Invalid timestamps must return null for toString()", null,
112                 timestamp.toString());
113     }
114 }
115