1 /*
2  * Copyright (C) 2020 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.deskclock.uidata;
18 
19 import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;
20 
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 
24 import java.util.Calendar;
25 
26 import static com.android.deskclock.uidata.PeriodicCallbackModel.Period.HOUR;
27 import static com.android.deskclock.uidata.PeriodicCallbackModel.Period.MIDNIGHT;
28 import static com.android.deskclock.uidata.PeriodicCallbackModel.Period.MINUTE;
29 import static com.android.deskclock.uidata.PeriodicCallbackModel.Period.QUARTER_HOUR;
30 
31 import static java.util.Calendar.MILLISECOND;
32 import static org.junit.Assert.assertEquals;
33 
34 @RunWith(AndroidJUnit4ClassRunner.class)
35 public class PeriodicCallbackModelTest {
36 
37     @Test
getMinuteDelay()38     public void getMinuteDelay() {
39         assertEquals(1, PeriodicCallbackModel.getDelay(56999, MINUTE, -3000));
40         assertEquals(60000, PeriodicCallbackModel.getDelay(57000, MINUTE, -3000));
41         assertEquals(59999, PeriodicCallbackModel.getDelay(57001, MINUTE, -3000));
42 
43         assertEquals(1, PeriodicCallbackModel.getDelay(59999, MINUTE, 0));
44         assertEquals(60000, PeriodicCallbackModel.getDelay(60000, MINUTE, 0));
45         assertEquals(59999, PeriodicCallbackModel.getDelay(60001, MINUTE, 0));
46 
47         assertEquals(3001, PeriodicCallbackModel.getDelay(59999, MINUTE, 3000));
48         assertEquals(3000, PeriodicCallbackModel.getDelay(60000, MINUTE, 3000));
49         assertEquals(1, PeriodicCallbackModel.getDelay(62999, MINUTE, 3000));
50         assertEquals(60000, PeriodicCallbackModel.getDelay(63000, MINUTE, 3000));
51         assertEquals(59999, PeriodicCallbackModel.getDelay(63001, MINUTE, 3000));
52     }
53 
54     @Test
getQuarterHourDelay()55     public void getQuarterHourDelay() {
56         assertEquals(1, PeriodicCallbackModel.getDelay(896999, QUARTER_HOUR, -3000));
57         assertEquals(900000, PeriodicCallbackModel.getDelay(897000, QUARTER_HOUR, -3000));
58         assertEquals(899999, PeriodicCallbackModel.getDelay(897001, QUARTER_HOUR, -3000));
59 
60         assertEquals(1, PeriodicCallbackModel.getDelay(899999, QUARTER_HOUR, 0));
61         assertEquals(900000, PeriodicCallbackModel.getDelay(900000, QUARTER_HOUR, 0));
62         assertEquals(899999, PeriodicCallbackModel.getDelay(900001, QUARTER_HOUR, 0));
63 
64         assertEquals(3001, PeriodicCallbackModel.getDelay(899999, QUARTER_HOUR, 3000));
65         assertEquals(3000, PeriodicCallbackModel.getDelay(900000, QUARTER_HOUR, 3000));
66         assertEquals(1, PeriodicCallbackModel.getDelay(902999, QUARTER_HOUR, 3000));
67         assertEquals(900000, PeriodicCallbackModel.getDelay(903000, QUARTER_HOUR, 3000));
68         assertEquals(899999, PeriodicCallbackModel.getDelay(903001, QUARTER_HOUR, 3000));
69     }
70 
71     @Test
getHourDelay()72     public void getHourDelay() {
73         assertEquals(1, PeriodicCallbackModel.getDelay(3596999, HOUR, -3000));
74         assertEquals(3600000, PeriodicCallbackModel.getDelay(3597000, HOUR, -3000));
75         assertEquals(3599999, PeriodicCallbackModel.getDelay(3597001, HOUR, -3000));
76 
77         assertEquals(1, PeriodicCallbackModel.getDelay(3599999, HOUR, 0));
78         assertEquals(3600000, PeriodicCallbackModel.getDelay(3600000, HOUR, 0));
79         assertEquals(3599999, PeriodicCallbackModel.getDelay(3600001, HOUR, 0));
80 
81         assertEquals(3001, PeriodicCallbackModel.getDelay(3599999, HOUR, 3000));
82         assertEquals(3000, PeriodicCallbackModel.getDelay(3600000, HOUR, 3000));
83         assertEquals(1, PeriodicCallbackModel.getDelay(3602999, HOUR, 3000));
84         assertEquals(3600000, PeriodicCallbackModel.getDelay(3603000, HOUR, 3000));
85         assertEquals(3599999, PeriodicCallbackModel.getDelay(3603001, HOUR, 3000));
86     }
87 
88     @Test
getMidnightDelay()89     public void getMidnightDelay() {
90         final Calendar c = Calendar.getInstance();
91         c.set(2016, 0, 20, 0, 0, 0);
92         c.set(MILLISECOND, 0);
93         final long now = c.getTimeInMillis();
94 
95         assertEquals(1, PeriodicCallbackModel.getDelay(now - 3001, MIDNIGHT, -3000));
96         assertEquals(86400000, PeriodicCallbackModel.getDelay(now - 3000, MIDNIGHT, -3000));
97         assertEquals(86399999, PeriodicCallbackModel.getDelay(now - 2999, MIDNIGHT, -3000));
98 
99         assertEquals(1, PeriodicCallbackModel.getDelay(now - 1, MIDNIGHT, 0));
100         assertEquals(86400000, PeriodicCallbackModel.getDelay(now, MIDNIGHT, 0));
101         assertEquals(86399999, PeriodicCallbackModel.getDelay(now + 1, MIDNIGHT, 0));
102 
103         assertEquals(3001, PeriodicCallbackModel.getDelay(now - 1, MIDNIGHT, 3000));
104         assertEquals(3000, PeriodicCallbackModel.getDelay(now, MIDNIGHT, 3000));
105         assertEquals(1, PeriodicCallbackModel.getDelay(now + 2999, MIDNIGHT, 3000));
106         assertEquals(86400000, PeriodicCallbackModel.getDelay(now + 3000, MIDNIGHT, 3000));
107         assertEquals(86399999, PeriodicCallbackModel.getDelay(now + 3001, MIDNIGHT, 3000));
108     }
109 }
110