aboutsummaryrefslogtreecommitdiff
path: root/tests/unit/src/com/android/tv/util/UtilsTest_IsInGivenDay.java
blob: 2b43abc1413b543c58fbab42362174fc0e4ed739 (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
/*
 * Copyright (C) 2015 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.tv.util;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import android.support.test.filters.SmallTest;

import org.junit.Test;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.TimeZone;

/**
 * Tests for {@link com.android.tv.util.Utils#isInGivenDay}.
 */
@SmallTest
public class UtilsTest_IsInGivenDay {
    @Test
    public void testIsInGivenDay() {
        assertTrue(Utils.isInGivenDay(
                new GregorianCalendar(2015, Calendar.JANUARY, 1).getTimeInMillis(),
                new GregorianCalendar(2015, Calendar.JANUARY, 1, 0, 30).getTimeInMillis()));
    }

    @Test
    public void testIsNotInGivenDay() {
        assertFalse(Utils.isInGivenDay(
                new GregorianCalendar(2015, Calendar.JANUARY, 1).getTimeInMillis(),
                new GregorianCalendar(2015, Calendar.JANUARY, 2).getTimeInMillis()));
    }

    @Test
    public void testIfTimeZoneApplied() {
        TimeZone timeZone = TimeZone.getDefault();

        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Seoul"));

        // 2015.01.01 00:00 in KST = 2014.12.31 15:00 in UTC
        long date2015StartMs =
                new GregorianCalendar(2015, Calendar.JANUARY, 1).getTimeInMillis();

        // 2015.01.01 10:00 in KST = 2015.01.01 01:00 in UTC
        long date2015Start10AMMs =
                new GregorianCalendar(2015, Calendar.JANUARY, 1, 10, 0).getTimeInMillis();

        // Those two times aren't in the same day in UTC, but they are in KST.
        assertTrue(Utils.isInGivenDay(date2015StartMs, date2015Start10AMMs));

        TimeZone.setDefault(timeZone);
    }
}