summaryrefslogtreecommitdiffstats
path: root/tests/unit/src/com/android/bluetooth/mapclient/ObexTimeTest.java
blob: 5ef2d455b7e42a02c81e36292bfe2e743517ee60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.bluetooth.mapclient;

import androidx.test.runner.AndroidJUnit4;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Date;
import java.util.TimeZone;

@RunWith(AndroidJUnit4.class)
public class ObexTimeTest {
    private static final String TAG = ObexTimeTest.class.getSimpleName();

    private static final String VALID_TIME_STRING = "20190101T121314";
    private static final String VALID_TIME_STRING_WITH_OFFSET_POS = "20190101T121314+0130";
    private static final String VALID_TIME_STRING_WITH_OFFSET_NEG = "20190101T121314-0130";

    private static final String INVALID_TIME_STRING_OFFSET_EXTRA_DIGITS = "20190101T121314-99999";
    private static final String INVALID_TIME_STRING_BAD_DELIMITER = "20190101Q121314";

    // MAP message listing times, per spec, use "local time basis" if UTC offset isn't given. The
    // ObexTime class parses using the current default timezone (assumed to be the "local timezone")
    // in the case that UTC isn't provided. However, the Date class assumes UTC ALWAYS when
    // initializing off of a long value. We have to take that into account when determining our
    // expected results for time strings that don't have an offset.
    private static final long LOCAL_TIMEZONE_OFFSET = TimeZone.getDefault().getRawOffset();

    // If you are a positive offset from GMT then GMT is in the "past" and you need to subtract that
    // offset from the time. If you are negative then GMT is in the future and you need to add that
    // offset to the time.
    private static final long VALID_TS = 1546344794000L; // Jan 01, 2019 at 12:13:14 GMT
    private static final long TS_OFFSET = 5400000L; // 1 Hour, 30 minutes -> milliseconds
    private static final long VALID_TS_LOCAL_TZ = VALID_TS - LOCAL_TIMEZONE_OFFSET;
    private static final long VALID_TS_OFFSET_POS = VALID_TS - TS_OFFSET;
    private static final long VALID_TS_OFFSET_NEG = VALID_TS + TS_OFFSET;

    private static final Date VALID_DATE_LOCAL_TZ = new Date(VALID_TS_LOCAL_TZ);
    private static final Date VALID_DATE_WITH_OFFSET_POS = new Date(VALID_TS_OFFSET_POS);
    private static final Date VALID_DATE_WITH_OFFSET_NEG = new Date(VALID_TS_OFFSET_NEG);

    @Test
    public void createWithValidDateTimeString_TimestampCorrect() {
        ObexTime timestamp = new ObexTime(VALID_TIME_STRING);
        Assert.assertEquals("Parsed timestamp must match expected", VALID_DATE_LOCAL_TZ,
                timestamp.getTime());
    }

    @Test
    public void createWithValidDateTimeStringWithPosOffset_TimestampCorrect() {
        ObexTime timestamp = new ObexTime(VALID_TIME_STRING_WITH_OFFSET_POS);
        Assert.assertEquals("Parsed timestamp must match expected", VALID_DATE_WITH_OFFSET_POS,
                timestamp.getTime());
    }

    @Test
    public void createWithValidDateTimeStringWithNegOffset_TimestampCorrect() {
        ObexTime timestamp = new ObexTime(VALID_TIME_STRING_WITH_OFFSET_NEG);
        Assert.assertEquals("Parsed timestamp must match expected", VALID_DATE_WITH_OFFSET_NEG,
                timestamp.getTime());
    }

    @Test
    public void createWithValidDate_TimestampCorrect() {
        ObexTime timestamp = new ObexTime(VALID_DATE_LOCAL_TZ);
        Assert.assertEquals("ObexTime created with a date must return the same date",
                VALID_DATE_LOCAL_TZ, timestamp.getTime());
    }

    @Test
    public void printValidTime_TimestampMatchesInput() {
        ObexTime timestamp = new ObexTime(VALID_TIME_STRING);
        Assert.assertEquals("Timestamp as a string must match the input string", VALID_TIME_STRING,
                timestamp.toString());
    }

    @Test
    public void createWithInvalidDelimiterString_TimestampIsNull() {
        ObexTime timestamp = new ObexTime(INVALID_TIME_STRING_BAD_DELIMITER);
        Assert.assertEquals("Parsed timestamp was invalid and must result in a null object", null,
                timestamp.getTime());
    }

    @Test
    public void createWithInvalidOffsetString_TimestampIsNull() {
        ObexTime timestamp = new ObexTime(INVALID_TIME_STRING_OFFSET_EXTRA_DIGITS);
        Assert.assertEquals("Parsed timestamp was invalid and must result in a null object", null,
                timestamp.getTime());
    }

    @Test
    public void printInvalidTime_ReturnsNull() {
        ObexTime timestamp = new ObexTime(INVALID_TIME_STRING_BAD_DELIMITER);
        Assert.assertEquals("Invalid timestamps must return null for toString()", null,
                timestamp.toString());
    }
}