blob: 5ebfd488eb97cf5856c266aa114f2ed3efb1ddea [file] [log] [blame]
Julia Reynolds4b318432017-11-22 10:56:18 -05001/*
2 * Copyright (C) 2017 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
17package com.android.server.notification;
18
19import static junit.framework.Assert.assertFalse;
20
21import static org.junit.Assert.assertEquals;
22import static org.junit.Assert.assertTrue;
23
Beverlybe6d3522017-11-20 11:01:59 -050024import android.service.notification.ScheduleCalendar;
Julia Reynolds4b318432017-11-22 10:56:18 -050025import android.service.notification.ZenModeConfig;
Geoffrey Pitsch69048f42017-12-08 09:53:02 -050026import android.support.test.filters.FlakyTest;
Julia Reynolds4b318432017-11-22 10:56:18 -050027import android.support.test.runner.AndroidJUnit4;
28import android.test.suitebuilder.annotation.SmallTest;
Julia Reynolds4b318432017-11-22 10:56:18 -050029
30import org.junit.Before;
31import org.junit.Test;
32import org.junit.runner.RunWith;
33
34import java.util.Calendar;
35import java.util.GregorianCalendar;
36
37@SmallTest
38@RunWith(AndroidJUnit4.class)
39public class ScheduleCalendarTest extends NotificationTestCase {
40
41 private ScheduleCalendar mScheduleCalendar;
42 private ZenModeConfig.ScheduleInfo mScheduleInfo;
43
44 @Before
45 public void setUp() throws Exception {
46 mScheduleCalendar = new ScheduleCalendar();
47 mScheduleInfo = new ZenModeConfig.ScheduleInfo();
48 mScheduleInfo.days = new int[] {1, 2, 3, 4, 5};
49 mScheduleCalendar.setSchedule(mScheduleInfo);
50 }
51
52 @Test
53 public void testNullScheduleInfo() throws Exception {
54 mScheduleCalendar.setSchedule(null);
55
56 mScheduleCalendar.maybeSetNextAlarm(1000, 1999);
57 assertEquals(0, mScheduleCalendar.getNextChangeTime(1000));
58 assertFalse(mScheduleCalendar.isInSchedule(100));
59 assertFalse(mScheduleCalendar.shouldExitForAlarm(100));
60 }
61
62 @Test
63 public void testGetNextChangeTime_startToday() throws Exception {
64 Calendar cal = new GregorianCalendar();
65 cal.set(Calendar.HOUR_OF_DAY, 1);
66 cal.set(Calendar.MINUTE, 15);
67 cal.set(Calendar.SECOND, 0);
68 cal.set(Calendar.MILLISECOND, 0);
69 mScheduleInfo.days = new int[] {getTodayDay()};
70 mScheduleInfo.startHour = cal.get(Calendar.HOUR_OF_DAY) + 1;
71 mScheduleInfo.endHour = cal.get(Calendar.HOUR_OF_DAY) + 3;
72 mScheduleInfo.startMinute = 15;
73 mScheduleInfo.endMinute = 15;
74 mScheduleInfo.exitAtAlarm = false;
75 mScheduleCalendar.setSchedule(mScheduleInfo);
76
77 Calendar expected = new GregorianCalendar();
78 expected.setTimeInMillis(cal.getTimeInMillis());
79 expected.set(Calendar.HOUR_OF_DAY, mScheduleInfo.startHour);
80
81 long actualMs = mScheduleCalendar.getNextChangeTime(cal.getTimeInMillis());
82 GregorianCalendar actual = new GregorianCalendar();
83 actual.setTimeInMillis(actualMs);
84 assertEquals("Expected " + expected + " was " + actual, expected.getTimeInMillis(),
85 actualMs);
86 }
87
88 @Test
89 public void testGetNextChangeTime_endToday() throws Exception {
90 Calendar cal = new GregorianCalendar();
91 cal.set(Calendar.HOUR_OF_DAY, 2);
92 cal.set(Calendar.MINUTE, 15);
93 cal.set(Calendar.SECOND, 0);
94 cal.set(Calendar.MILLISECOND, 0);
95 mScheduleInfo.days = new int[] {getTodayDay()};
96 mScheduleInfo.startHour = cal.get(Calendar.HOUR_OF_DAY) - 1;
97 mScheduleInfo.endHour = cal.get(Calendar.HOUR_OF_DAY) + 3;
98 mScheduleInfo.startMinute = 15;
99 mScheduleInfo.endMinute = 15;
100 mScheduleInfo.exitAtAlarm = false;
101
102 Calendar expected = new GregorianCalendar();
103 expected.setTimeInMillis(cal.getTimeInMillis());
104 expected.set(Calendar.HOUR_OF_DAY, mScheduleInfo.endHour);
105 expected.set(Calendar.MINUTE, mScheduleInfo.endMinute);
106
107 long actualMs = mScheduleCalendar.getNextChangeTime(cal.getTimeInMillis());
108 GregorianCalendar actual = new GregorianCalendar();
109 actual.setTimeInMillis(actualMs);
110 assertEquals("Expected " + expected + " was " + actual, expected.getTimeInMillis(),
111 actualMs);
112 }
113
114 @Test
115 public void testGetNextChangeTime_startTomorrow() throws Exception {
116 Calendar cal = new GregorianCalendar();
117 cal.set(Calendar.HOUR_OF_DAY, 23);
118 cal.set(Calendar.MINUTE, 15);
119 cal.set(Calendar.SECOND, 0);
120 cal.set(Calendar.MILLISECOND, 0);
121 mScheduleInfo.days = new int[] {getTodayDay(), getTodayDay() + 1};
122 mScheduleInfo.startHour = 1;
123 mScheduleInfo.endHour = 3;
124 mScheduleInfo.startMinute = 15;
125 mScheduleInfo.endMinute = 15;
126 mScheduleInfo.exitAtAlarm = false;
127
128 Calendar expected = new GregorianCalendar();
129 expected.setTimeInMillis(cal.getTimeInMillis());
130 expected.add(Calendar.DATE, 1);
131 expected.set(Calendar.HOUR_OF_DAY, mScheduleInfo.startHour);
132 expected.set(Calendar.MINUTE, mScheduleInfo.startMinute);
133
134 long actualMs = mScheduleCalendar.getNextChangeTime(cal.getTimeInMillis());
135 GregorianCalendar actual = new GregorianCalendar();
136 actual.setTimeInMillis(actualMs);
137 assertEquals("Expected " + expected + " was " + actual, expected.getTimeInMillis(),
138 actualMs);
139 }
140
141 @Test
142 public void testGetNextChangeTime_endTomorrow() throws Exception {
143 Calendar cal = new GregorianCalendar();
144 cal.set(Calendar.HOUR_OF_DAY, 23);
145 cal.set(Calendar.MINUTE, 15);
146 cal.set(Calendar.SECOND, 0);
147 cal.set(Calendar.MILLISECOND, 0);
148 mScheduleInfo.days = new int[] {getTodayDay(), getTodayDay() + 1};
149 mScheduleInfo.startHour = 22;
150 mScheduleInfo.endHour = 3;
151 mScheduleInfo.startMinute = 15;
152 mScheduleInfo.endMinute = 15;
153 mScheduleInfo.exitAtAlarm = false;
154
155 Calendar expected = new GregorianCalendar();
156 expected.setTimeInMillis(cal.getTimeInMillis());
157 expected.add(Calendar.DATE, 1);
158 expected.set(Calendar.HOUR_OF_DAY, mScheduleInfo.endHour);
159 expected.set(Calendar.MINUTE, mScheduleInfo.endMinute);
160
161 long actualMs = mScheduleCalendar.getNextChangeTime(cal.getTimeInMillis());
162 GregorianCalendar actual = new GregorianCalendar();
163 actual.setTimeInMillis(actualMs);
164 assertEquals("Expected " + expected + " was " + actual, expected.getTimeInMillis(),
165 actualMs);
166 }
167
168 @Test
169 public void testShouldExitForAlarm_settingOff() {
170 mScheduleInfo.exitAtAlarm = false;
171 mScheduleInfo.nextAlarm = 1000;
172
173 assertFalse(mScheduleCalendar.shouldExitForAlarm(1000));
174 }
175
176 @Test
177 public void testShouldExitForAlarm_beforeAlarm() {
178 mScheduleInfo.exitAtAlarm = true;
179 mScheduleInfo.nextAlarm = 1000;
180
181 assertFalse(mScheduleCalendar.shouldExitForAlarm(999));
182 }
183
184 @Test
185 public void testShouldExitForAlarm_noAlarm() {
186 mScheduleInfo.exitAtAlarm = true;
187 mScheduleInfo.nextAlarm = 0;
188
189 assertFalse(mScheduleCalendar.shouldExitForAlarm(999));
190 }
191
192 @Test
193 public void testShouldExitForAlarm() {
194 mScheduleInfo.exitAtAlarm = true;
195 mScheduleInfo.nextAlarm = 1000;
196
197 assertTrue(mScheduleCalendar.shouldExitForAlarm(1000));
198 }
199
200 @Test
201 public void testMaybeSetNextAlarm_settingOff() {
202 mScheduleInfo.exitAtAlarm = false;
203 mScheduleInfo.nextAlarm = 0;
204
205 mScheduleCalendar.maybeSetNextAlarm(1000, 2000);
206
207 assertEquals(0, mScheduleInfo.nextAlarm);
208 }
209
210 @Test
211 public void testMaybeSetNextAlarm_settingOn() {
212 mScheduleInfo.exitAtAlarm = true;
213 mScheduleInfo.nextAlarm = 0;
214
215 mScheduleCalendar.maybeSetNextAlarm(1000, 2000);
216
217 assertEquals(2000, mScheduleInfo.nextAlarm);
218 }
219
220 @Test
221 public void testMaybeSetNextAlarm_alarmCanceled() {
222 mScheduleInfo.exitAtAlarm = true;
223 mScheduleInfo.nextAlarm = 10000;
224
225 mScheduleCalendar.maybeSetNextAlarm(1000, 0);
226
227 assertEquals(0, mScheduleInfo.nextAlarm);
228 }
229
230 @Test
231 public void testMaybeSetNextAlarm_earlierAlarm() {
232 mScheduleInfo.exitAtAlarm = true;
233 mScheduleInfo.nextAlarm = 2000;
234
235 mScheduleCalendar.maybeSetNextAlarm(1000, 1500);
236
237 assertEquals(1500, mScheduleInfo.nextAlarm);
238 }
239
240 @Test
241 public void testMaybeSetNextAlarm_laterAlarm() {
242 mScheduleInfo.exitAtAlarm = true;
243 mScheduleInfo.nextAlarm = 2000;
244
245 mScheduleCalendar.maybeSetNextAlarm(1000, 3000);
246
247 assertEquals(2000, mScheduleInfo.nextAlarm);
248 }
249
250 @Test
251 public void testMaybeSetNextAlarm_expiredAlarm() {
252 mScheduleInfo.exitAtAlarm = true;
253 mScheduleInfo.nextAlarm = 998;
254
255 mScheduleCalendar.maybeSetNextAlarm(1000, 999);
256
257 assertEquals(0, mScheduleInfo.nextAlarm);
258 }
259
260 @Test
Geoffrey Pitsch69048f42017-12-08 09:53:02 -0500261 @FlakyTest
Julia Reynolds4b318432017-11-22 10:56:18 -0500262 public void testIsInSchedule_inScheduleOvernight() {
263 Calendar cal = new GregorianCalendar();
264 cal.set(Calendar.HOUR_OF_DAY, 23);
265 cal.set(Calendar.MINUTE, 15);
266 cal.set(Calendar.SECOND, 0);
267 cal.set(Calendar.MILLISECOND, 0);
268 mScheduleInfo.days = new int[] {getTodayDay()};
269 mScheduleInfo.startHour = 22;
270 mScheduleInfo.endHour = 3;
271 mScheduleInfo.startMinute = 15;
272 mScheduleInfo.endMinute = 15;
273
274 assertTrue(mScheduleCalendar.isInSchedule(cal.getTimeInMillis()));
275 }
276
277 @Test
Geoffrey Pitsch69048f42017-12-08 09:53:02 -0500278 @FlakyTest
Julia Reynolds4b318432017-11-22 10:56:18 -0500279 public void testIsInSchedule_inScheduleSingleDay() {
280 Calendar cal = new GregorianCalendar();
281 cal.set(Calendar.HOUR_OF_DAY, 14);
282 cal.set(Calendar.MINUTE, 15);
283 cal.set(Calendar.SECOND, 0);
284 cal.set(Calendar.MILLISECOND, 0);
285 mScheduleInfo.days = new int[] {getTodayDay()};
286 mScheduleInfo.startHour = 12;
287 mScheduleInfo.endHour = 3;
288 mScheduleInfo.startMinute = 16;
289 mScheduleInfo.endMinute = 15;
290
291 assertTrue(mScheduleCalendar.isInSchedule(cal.getTimeInMillis()));
292 }
293
294 @Test
295 public void testIsInSchedule_notToday() {
296 Calendar cal = new GregorianCalendar();
297 cal.set(Calendar.HOUR_OF_DAY, 14);
298 cal.set(Calendar.MINUTE, 15);
299 cal.set(Calendar.SECOND, 0);
300 cal.set(Calendar.MILLISECOND, 0);
301 cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
302 mScheduleInfo.days = new int[] {Calendar.FRIDAY, Calendar.SUNDAY};
303 mScheduleInfo.startHour = 12;
304 mScheduleInfo.startMinute = 16;
305 mScheduleInfo.endHour = 15;
306 mScheduleInfo.endMinute = 15;
307
308 assertFalse(mScheduleCalendar.isInSchedule(cal.getTimeInMillis()));
309 }
310
311 @Test
312 public void testIsInSchedule_startingSoon() {
313 Calendar cal = new GregorianCalendar();
314 cal.set(Calendar.HOUR_OF_DAY, 14);
315 cal.set(Calendar.MINUTE, 15);
316 cal.set(Calendar.SECOND, 59);
317 cal.set(Calendar.MILLISECOND, 0);
318 mScheduleInfo.days = new int[] {getTodayDay()};
319 mScheduleInfo.startHour = 14;
320 mScheduleInfo.endHour = 3;
321 mScheduleInfo.startMinute = 16;
322 mScheduleInfo.endMinute = 15;
323
324 assertFalse(mScheduleCalendar.isInSchedule(cal.getTimeInMillis()));
325 }
326
327 private int getTodayDay() {
328 return new GregorianCalendar().get(Calendar.DAY_OF_WEEK);
329 }
330}