blob: 110e3fe5eb24f160d78b44ee929ee73b54b240fc [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
Jason Monk74f5e362017-12-06 08:56:33 -050030import com.android.server.UiServiceTestCase;
31
Julia Reynolds4b318432017-11-22 10:56:18 -050032import org.junit.Before;
Julia Reynolds27291c22018-04-04 09:45:15 -040033import org.junit.Ignore;
Julia Reynolds4b318432017-11-22 10:56:18 -050034import org.junit.Test;
35import org.junit.runner.RunWith;
36
37import java.util.Calendar;
38import java.util.GregorianCalendar;
39
40@SmallTest
41@RunWith(AndroidJUnit4.class)
Jason Monk74f5e362017-12-06 08:56:33 -050042public class ScheduleCalendarTest extends UiServiceTestCase {
Julia Reynolds4b318432017-11-22 10:56:18 -050043
44 private ScheduleCalendar mScheduleCalendar;
45 private ZenModeConfig.ScheduleInfo mScheduleInfo;
46
47 @Before
48 public void setUp() throws Exception {
49 mScheduleCalendar = new ScheduleCalendar();
50 mScheduleInfo = new ZenModeConfig.ScheduleInfo();
51 mScheduleInfo.days = new int[] {1, 2, 3, 4, 5};
Julia Reynolds4b318432017-11-22 10:56:18 -050052 }
53
54 @Test
55 public void testNullScheduleInfo() throws Exception {
56 mScheduleCalendar.setSchedule(null);
57
58 mScheduleCalendar.maybeSetNextAlarm(1000, 1999);
59 assertEquals(0, mScheduleCalendar.getNextChangeTime(1000));
60 assertFalse(mScheduleCalendar.isInSchedule(100));
61 assertFalse(mScheduleCalendar.shouldExitForAlarm(100));
62 }
63
64 @Test
65 public void testGetNextChangeTime_startToday() throws Exception {
66 Calendar cal = new GregorianCalendar();
67 cal.set(Calendar.HOUR_OF_DAY, 1);
68 cal.set(Calendar.MINUTE, 15);
69 cal.set(Calendar.SECOND, 0);
70 cal.set(Calendar.MILLISECOND, 0);
71 mScheduleInfo.days = new int[] {getTodayDay()};
72 mScheduleInfo.startHour = cal.get(Calendar.HOUR_OF_DAY) + 1;
73 mScheduleInfo.endHour = cal.get(Calendar.HOUR_OF_DAY) + 3;
74 mScheduleInfo.startMinute = 15;
75 mScheduleInfo.endMinute = 15;
76 mScheduleInfo.exitAtAlarm = false;
77 mScheduleCalendar.setSchedule(mScheduleInfo);
78
79 Calendar expected = new GregorianCalendar();
80 expected.setTimeInMillis(cal.getTimeInMillis());
81 expected.set(Calendar.HOUR_OF_DAY, mScheduleInfo.startHour);
82
83 long actualMs = mScheduleCalendar.getNextChangeTime(cal.getTimeInMillis());
84 GregorianCalendar actual = new GregorianCalendar();
85 actual.setTimeInMillis(actualMs);
86 assertEquals("Expected " + expected + " was " + actual, expected.getTimeInMillis(),
87 actualMs);
88 }
89
90 @Test
91 public void testGetNextChangeTime_endToday() throws Exception {
92 Calendar cal = new GregorianCalendar();
93 cal.set(Calendar.HOUR_OF_DAY, 2);
94 cal.set(Calendar.MINUTE, 15);
95 cal.set(Calendar.SECOND, 0);
96 cal.set(Calendar.MILLISECOND, 0);
97 mScheduleInfo.days = new int[] {getTodayDay()};
98 mScheduleInfo.startHour = cal.get(Calendar.HOUR_OF_DAY) - 1;
99 mScheduleInfo.endHour = cal.get(Calendar.HOUR_OF_DAY) + 3;
100 mScheduleInfo.startMinute = 15;
101 mScheduleInfo.endMinute = 15;
102 mScheduleInfo.exitAtAlarm = false;
Julia Reynolds7edcd782018-01-12 16:50:49 -0500103 mScheduleCalendar.setSchedule(mScheduleInfo);
Julia Reynolds4b318432017-11-22 10:56:18 -0500104
105 Calendar expected = new GregorianCalendar();
106 expected.setTimeInMillis(cal.getTimeInMillis());
107 expected.set(Calendar.HOUR_OF_DAY, mScheduleInfo.endHour);
108 expected.set(Calendar.MINUTE, mScheduleInfo.endMinute);
109
110 long actualMs = mScheduleCalendar.getNextChangeTime(cal.getTimeInMillis());
111 GregorianCalendar actual = new GregorianCalendar();
112 actual.setTimeInMillis(actualMs);
113 assertEquals("Expected " + expected + " was " + actual, expected.getTimeInMillis(),
114 actualMs);
115 }
116
117 @Test
118 public void testGetNextChangeTime_startTomorrow() throws Exception {
119 Calendar cal = new GregorianCalendar();
120 cal.set(Calendar.HOUR_OF_DAY, 23);
121 cal.set(Calendar.MINUTE, 15);
122 cal.set(Calendar.SECOND, 0);
123 cal.set(Calendar.MILLISECOND, 0);
Beverlyfe099b52018-06-18 16:42:14 -0400124 mScheduleInfo.days = new int[] {getTodayDay(), getTodayDay(1)};
Julia Reynolds4b318432017-11-22 10:56:18 -0500125 mScheduleInfo.startHour = 1;
126 mScheduleInfo.endHour = 3;
127 mScheduleInfo.startMinute = 15;
128 mScheduleInfo.endMinute = 15;
129 mScheduleInfo.exitAtAlarm = false;
Julia Reynolds7edcd782018-01-12 16:50:49 -0500130 mScheduleCalendar.setSchedule(mScheduleInfo);
Julia Reynolds4b318432017-11-22 10:56:18 -0500131
132 Calendar expected = new GregorianCalendar();
133 expected.setTimeInMillis(cal.getTimeInMillis());
134 expected.add(Calendar.DATE, 1);
135 expected.set(Calendar.HOUR_OF_DAY, mScheduleInfo.startHour);
136 expected.set(Calendar.MINUTE, mScheduleInfo.startMinute);
137
138 long actualMs = mScheduleCalendar.getNextChangeTime(cal.getTimeInMillis());
139 GregorianCalendar actual = new GregorianCalendar();
140 actual.setTimeInMillis(actualMs);
141 assertEquals("Expected " + expected + " was " + actual, expected.getTimeInMillis(),
142 actualMs);
143 }
144
145 @Test
146 public void testGetNextChangeTime_endTomorrow() throws Exception {
147 Calendar cal = new GregorianCalendar();
148 cal.set(Calendar.HOUR_OF_DAY, 23);
149 cal.set(Calendar.MINUTE, 15);
150 cal.set(Calendar.SECOND, 0);
151 cal.set(Calendar.MILLISECOND, 0);
Beverlyfe099b52018-06-18 16:42:14 -0400152 mScheduleInfo.days = new int[] {getTodayDay(), getTodayDay(1)};
Julia Reynolds4b318432017-11-22 10:56:18 -0500153 mScheduleInfo.startHour = 22;
154 mScheduleInfo.endHour = 3;
155 mScheduleInfo.startMinute = 15;
156 mScheduleInfo.endMinute = 15;
157 mScheduleInfo.exitAtAlarm = false;
Julia Reynolds7edcd782018-01-12 16:50:49 -0500158 mScheduleCalendar.setSchedule(mScheduleInfo);
Julia Reynolds4b318432017-11-22 10:56:18 -0500159
160 Calendar expected = new GregorianCalendar();
161 expected.setTimeInMillis(cal.getTimeInMillis());
162 expected.add(Calendar.DATE, 1);
163 expected.set(Calendar.HOUR_OF_DAY, mScheduleInfo.endHour);
164 expected.set(Calendar.MINUTE, mScheduleInfo.endMinute);
165
166 long actualMs = mScheduleCalendar.getNextChangeTime(cal.getTimeInMillis());
167 GregorianCalendar actual = new GregorianCalendar();
168 actual.setTimeInMillis(actualMs);
169 assertEquals("Expected " + expected + " was " + actual, expected.getTimeInMillis(),
170 actualMs);
171 }
172
173 @Test
174 public void testShouldExitForAlarm_settingOff() {
175 mScheduleInfo.exitAtAlarm = false;
176 mScheduleInfo.nextAlarm = 1000;
Julia Reynolds7edcd782018-01-12 16:50:49 -0500177 mScheduleCalendar.setSchedule(mScheduleInfo);
Julia Reynolds4b318432017-11-22 10:56:18 -0500178
179 assertFalse(mScheduleCalendar.shouldExitForAlarm(1000));
180 }
181
182 @Test
183 public void testShouldExitForAlarm_beforeAlarm() {
184 mScheduleInfo.exitAtAlarm = true;
185 mScheduleInfo.nextAlarm = 1000;
Julia Reynolds7edcd782018-01-12 16:50:49 -0500186 mScheduleCalendar.setSchedule(mScheduleInfo);
Julia Reynolds4b318432017-11-22 10:56:18 -0500187
188 assertFalse(mScheduleCalendar.shouldExitForAlarm(999));
189 }
190
191 @Test
192 public void testShouldExitForAlarm_noAlarm() {
193 mScheduleInfo.exitAtAlarm = true;
194 mScheduleInfo.nextAlarm = 0;
Julia Reynolds7edcd782018-01-12 16:50:49 -0500195 mScheduleCalendar.setSchedule(mScheduleInfo);
Julia Reynolds4b318432017-11-22 10:56:18 -0500196
197 assertFalse(mScheduleCalendar.shouldExitForAlarm(999));
198 }
199
200 @Test
201 public void testShouldExitForAlarm() {
202 mScheduleInfo.exitAtAlarm = true;
203 mScheduleInfo.nextAlarm = 1000;
Julia Reynolds7edcd782018-01-12 16:50:49 -0500204 mScheduleCalendar.setSchedule(mScheduleInfo);
Julia Reynolds4b318432017-11-22 10:56:18 -0500205
206 assertTrue(mScheduleCalendar.shouldExitForAlarm(1000));
207 }
208
209 @Test
Beverly369dd422018-03-29 18:14:04 -0400210 public void testShouldExitForAlarm_oldAlarm() {
211 // Cal: today 2:15pm
Beverly4b7853e2018-05-04 11:21:20 -0400212 Calendar now = new GregorianCalendar();
213 now.set(Calendar.HOUR_OF_DAY, 14);
214 now.set(Calendar.MINUTE, 15);
215 now.set(Calendar.SECOND, 0);
216 now.set(Calendar.MILLISECOND, 0);
Beverly369dd422018-03-29 18:14:04 -0400217
218 // ScheduleInfo: today 12:16pm - today 3:15pm
219 mScheduleInfo.days = new int[] {getTodayDay()};
220 mScheduleInfo.startHour = 12;
221 mScheduleInfo.endHour = 3;
222 mScheduleInfo.startMinute = 16;
223 mScheduleInfo.endMinute = 15;
224 mScheduleInfo.exitAtAlarm = true;
225 mScheduleInfo.nextAlarm = 1000; // very old alarm
226
227 mScheduleCalendar.setSchedule(mScheduleInfo);
Beverly4b7853e2018-05-04 11:21:20 -0400228 assertTrue(mScheduleCalendar.isInSchedule(now.getTimeInMillis()));
Beverly369dd422018-03-29 18:14:04 -0400229
230 // don't exit for an alarm if it's an old alarm
Beverly4b7853e2018-05-04 11:21:20 -0400231 assertFalse(mScheduleCalendar.shouldExitForAlarm(now.getTimeInMillis()));
232 }
233
234 @Test
235 public void testShouldExitForAlarm_oldAlarmInSchedule() {
236 // calNow: day 2 at 9pm
237 Calendar calNow = new GregorianCalendar();
238 calNow.set(Calendar.HOUR_OF_DAY, 21);
239 calNow.set(Calendar.MINUTE, 0);
240 calNow.set(Calendar.SECOND, 0);
241 calNow.set(Calendar.MILLISECOND, 0);
242 calNow.add(Calendar.DATE, 1); // add a day
243
244 // calAlarm: day 2 at 5am
245 Calendar calAlarm = new GregorianCalendar();
246 calAlarm.set(Calendar.HOUR_OF_DAY, 5);
247 calAlarm.set(Calendar.MINUTE, 0);
248 calAlarm.set(Calendar.SECOND, 0);
249 calAlarm.set(Calendar.MILLISECOND, 0);
250 calAlarm.add(Calendar.DATE, 1); // add a day
251
252 // ScheduleInfo: day 1, day 2: 9pm-7am
Beverlyfe099b52018-06-18 16:42:14 -0400253 mScheduleInfo.days = new int[] {getTodayDay(), getTodayDay(1)};
Beverly4b7853e2018-05-04 11:21:20 -0400254 mScheduleInfo.startHour = 21;
255 mScheduleInfo.endHour = 7;
256 mScheduleInfo.startMinute = 0;
257 mScheduleInfo.endMinute = 0;
258 mScheduleInfo.exitAtAlarm = true;
259 mScheduleInfo.nextAlarm = calAlarm.getTimeInMillis(); // old alarm (5am day 2)
260
261 mScheduleCalendar.setSchedule(mScheduleInfo);
262 assertTrue(mScheduleCalendar.isInSchedule(calNow.getTimeInMillis()));
263 assertTrue(mScheduleCalendar.isInSchedule(calAlarm.getTimeInMillis()));
264
265 // don't exit for an alarm if it's an old alarm
266 assertFalse(mScheduleCalendar.shouldExitForAlarm(calNow.getTimeInMillis()));
Beverly369dd422018-03-29 18:14:04 -0400267 }
268
269 @Test
Julia Reynolds4b318432017-11-22 10:56:18 -0500270 public void testMaybeSetNextAlarm_settingOff() {
271 mScheduleInfo.exitAtAlarm = false;
272 mScheduleInfo.nextAlarm = 0;
Julia Reynolds7edcd782018-01-12 16:50:49 -0500273 mScheduleCalendar.setSchedule(mScheduleInfo);
Julia Reynolds4b318432017-11-22 10:56:18 -0500274
275 mScheduleCalendar.maybeSetNextAlarm(1000, 2000);
276
277 assertEquals(0, mScheduleInfo.nextAlarm);
278 }
279
280 @Test
281 public void testMaybeSetNextAlarm_settingOn() {
282 mScheduleInfo.exitAtAlarm = true;
283 mScheduleInfo.nextAlarm = 0;
Julia Reynolds7edcd782018-01-12 16:50:49 -0500284 mScheduleCalendar.setSchedule(mScheduleInfo);
Julia Reynolds4b318432017-11-22 10:56:18 -0500285
286 mScheduleCalendar.maybeSetNextAlarm(1000, 2000);
287
288 assertEquals(2000, mScheduleInfo.nextAlarm);
289 }
290
291 @Test
292 public void testMaybeSetNextAlarm_alarmCanceled() {
293 mScheduleInfo.exitAtAlarm = true;
294 mScheduleInfo.nextAlarm = 10000;
Julia Reynolds7edcd782018-01-12 16:50:49 -0500295 mScheduleCalendar.setSchedule(mScheduleInfo);
Julia Reynolds4b318432017-11-22 10:56:18 -0500296
297 mScheduleCalendar.maybeSetNextAlarm(1000, 0);
298
299 assertEquals(0, mScheduleInfo.nextAlarm);
300 }
301
302 @Test
303 public void testMaybeSetNextAlarm_earlierAlarm() {
304 mScheduleInfo.exitAtAlarm = true;
305 mScheduleInfo.nextAlarm = 2000;
Julia Reynolds7edcd782018-01-12 16:50:49 -0500306 mScheduleCalendar.setSchedule(mScheduleInfo);
Julia Reynolds4b318432017-11-22 10:56:18 -0500307
308 mScheduleCalendar.maybeSetNextAlarm(1000, 1500);
309
310 assertEquals(1500, mScheduleInfo.nextAlarm);
311 }
312
313 @Test
314 public void testMaybeSetNextAlarm_laterAlarm() {
315 mScheduleInfo.exitAtAlarm = true;
Julia Reynolds7edcd782018-01-12 16:50:49 -0500316 mScheduleCalendar.setSchedule(mScheduleInfo);
Julia Reynolds4b318432017-11-22 10:56:18 -0500317 mScheduleInfo.nextAlarm = 2000;
318
319 mScheduleCalendar.maybeSetNextAlarm(1000, 3000);
320
321 assertEquals(2000, mScheduleInfo.nextAlarm);
322 }
323
324 @Test
325 public void testMaybeSetNextAlarm_expiredAlarm() {
326 mScheduleInfo.exitAtAlarm = true;
327 mScheduleInfo.nextAlarm = 998;
Julia Reynolds7edcd782018-01-12 16:50:49 -0500328 mScheduleCalendar.setSchedule(mScheduleInfo);
Julia Reynolds4b318432017-11-22 10:56:18 -0500329
330 mScheduleCalendar.maybeSetNextAlarm(1000, 999);
331
332 assertEquals(0, mScheduleInfo.nextAlarm);
333 }
334
335 @Test
Beverly4e761912018-08-14 14:58:18 -0400336 public void testMaybeSetNextAlarm_expiredOldAlarm() {
337 mScheduleInfo.exitAtAlarm = true;
338 mScheduleInfo.nextAlarm = 998;
339 mScheduleCalendar.setSchedule(mScheduleInfo);
340
341 mScheduleCalendar.maybeSetNextAlarm(1000, 1001);
342
343 assertEquals(1001, mScheduleInfo.nextAlarm);
344 }
345
346 @Test
Geoffrey Pitsch69048f42017-12-08 09:53:02 -0500347 @FlakyTest
Julia Reynolds4b318432017-11-22 10:56:18 -0500348 public void testIsInSchedule_inScheduleOvernight() {
349 Calendar cal = new GregorianCalendar();
350 cal.set(Calendar.HOUR_OF_DAY, 23);
351 cal.set(Calendar.MINUTE, 15);
352 cal.set(Calendar.SECOND, 0);
353 cal.set(Calendar.MILLISECOND, 0);
354 mScheduleInfo.days = new int[] {getTodayDay()};
355 mScheduleInfo.startHour = 22;
356 mScheduleInfo.endHour = 3;
357 mScheduleInfo.startMinute = 15;
358 mScheduleInfo.endMinute = 15;
Julia Reynolds7edcd782018-01-12 16:50:49 -0500359 mScheduleCalendar.setSchedule(mScheduleInfo);
Julia Reynolds4b318432017-11-22 10:56:18 -0500360
361 assertTrue(mScheduleCalendar.isInSchedule(cal.getTimeInMillis()));
362 }
363
364 @Test
Geoffrey Pitsch69048f42017-12-08 09:53:02 -0500365 @FlakyTest
Julia Reynolds4b318432017-11-22 10:56:18 -0500366 public void testIsInSchedule_inScheduleSingleDay() {
367 Calendar cal = new GregorianCalendar();
368 cal.set(Calendar.HOUR_OF_DAY, 14);
369 cal.set(Calendar.MINUTE, 15);
370 cal.set(Calendar.SECOND, 0);
371 cal.set(Calendar.MILLISECOND, 0);
372 mScheduleInfo.days = new int[] {getTodayDay()};
373 mScheduleInfo.startHour = 12;
374 mScheduleInfo.endHour = 3;
375 mScheduleInfo.startMinute = 16;
376 mScheduleInfo.endMinute = 15;
Julia Reynolds7edcd782018-01-12 16:50:49 -0500377 mScheduleCalendar.setSchedule(mScheduleInfo);
Julia Reynolds4b318432017-11-22 10:56:18 -0500378
379 assertTrue(mScheduleCalendar.isInSchedule(cal.getTimeInMillis()));
380 }
381
382 @Test
383 public void testIsInSchedule_notToday() {
384 Calendar cal = new GregorianCalendar();
385 cal.set(Calendar.HOUR_OF_DAY, 14);
386 cal.set(Calendar.MINUTE, 15);
387 cal.set(Calendar.SECOND, 0);
388 cal.set(Calendar.MILLISECOND, 0);
389 cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
390 mScheduleInfo.days = new int[] {Calendar.FRIDAY, Calendar.SUNDAY};
391 mScheduleInfo.startHour = 12;
392 mScheduleInfo.startMinute = 16;
393 mScheduleInfo.endHour = 15;
394 mScheduleInfo.endMinute = 15;
Julia Reynolds7edcd782018-01-12 16:50:49 -0500395 mScheduleCalendar.setSchedule(mScheduleInfo);
Julia Reynolds4b318432017-11-22 10:56:18 -0500396
397 assertFalse(mScheduleCalendar.isInSchedule(cal.getTimeInMillis()));
398 }
399
400 @Test
401 public void testIsInSchedule_startingSoon() {
402 Calendar cal = new GregorianCalendar();
403 cal.set(Calendar.HOUR_OF_DAY, 14);
404 cal.set(Calendar.MINUTE, 15);
405 cal.set(Calendar.SECOND, 59);
406 cal.set(Calendar.MILLISECOND, 0);
407 mScheduleInfo.days = new int[] {getTodayDay()};
408 mScheduleInfo.startHour = 14;
409 mScheduleInfo.endHour = 3;
410 mScheduleInfo.startMinute = 16;
411 mScheduleInfo.endMinute = 15;
Julia Reynolds7edcd782018-01-12 16:50:49 -0500412 mScheduleCalendar.setSchedule(mScheduleInfo);
Julia Reynolds4b318432017-11-22 10:56:18 -0500413
414 assertFalse(mScheduleCalendar.isInSchedule(cal.getTimeInMillis()));
415 }
416
Beverly4b7853e2018-05-04 11:21:20 -0400417 @Test
418 public void testIsAlarmInSchedule_alarmAndNowInSchedule_sameScheduleTrigger() {
419 Calendar alarm = new GregorianCalendar();
420 alarm.set(Calendar.HOUR_OF_DAY, 23);
421 alarm.set(Calendar.MINUTE, 15);
422 alarm.set(Calendar.SECOND, 0);
423 alarm.set(Calendar.MILLISECOND, 0);
424
425 Calendar now = new GregorianCalendar();
426 now.set(Calendar.HOUR_OF_DAY, 2);
427 now.set(Calendar.MINUTE, 15);
428 now.set(Calendar.SECOND, 0);
429 now.set(Calendar.MILLISECOND, 0);
430 now.add(Calendar.DATE, 1); // add a day
431
Beverlyfe099b52018-06-18 16:42:14 -0400432 mScheduleInfo.days = new int[] {getTodayDay(), getTodayDay(1)};
Beverly4b7853e2018-05-04 11:21:20 -0400433 mScheduleInfo.startHour = 22;
434 mScheduleInfo.startMinute = 15;
435 mScheduleInfo.endHour = 3;
436 mScheduleInfo.endMinute = 15;
437 mScheduleCalendar.setSchedule(mScheduleInfo);
438
439 assertTrue(mScheduleCalendar.isInSchedule(alarm.getTimeInMillis()));
440 assertTrue(mScheduleCalendar.isInSchedule(now.getTimeInMillis()));
441 assertTrue(mScheduleCalendar.isAlarmInSchedule(alarm.getTimeInMillis(),
442 now.getTimeInMillis()));
443 }
444
445 @Test
446 public void testIsAlarmInSchedule_alarmAndNowInSchedule_differentScheduleTrigger() {
447 Calendar alarm = new GregorianCalendar();
448 alarm.set(Calendar.HOUR_OF_DAY, 23);
449 alarm.set(Calendar.MINUTE, 15);
450 alarm.set(Calendar.SECOND, 0);
451 alarm.set(Calendar.MILLISECOND, 0);
452
453 Calendar now = new GregorianCalendar();
454 now.set(Calendar.HOUR_OF_DAY, 23);
455 now.set(Calendar.MINUTE, 15);
456 now.set(Calendar.SECOND, 0);
457 now.set(Calendar.MILLISECOND, 0);
458 now.add(Calendar.DATE, 1); // add a day
459
Beverlyfe099b52018-06-18 16:42:14 -0400460 mScheduleInfo.days = new int[] {getTodayDay(), getTodayDay(1)};
Beverly4b7853e2018-05-04 11:21:20 -0400461 mScheduleInfo.startHour = 22;
462 mScheduleInfo.startMinute = 15;
463 mScheduleInfo.endHour = 3;
464 mScheduleInfo.endMinute = 15;
465 mScheduleCalendar.setSchedule(mScheduleInfo);
466
467 // even though both alarm and now are in schedule, they are not in the same part of
468 // the schedule (alarm is in schedule for the previous day's schedule compared to now)
469 assertTrue(mScheduleCalendar.isInSchedule(alarm.getTimeInMillis()));
470 assertTrue(mScheduleCalendar.isInSchedule(now.getTimeInMillis()));
471 assertFalse(mScheduleCalendar.isAlarmInSchedule(alarm.getTimeInMillis(),
472 now.getTimeInMillis()));
473 }
474
Julia Reynolds4b318432017-11-22 10:56:18 -0500475 private int getTodayDay() {
476 return new GregorianCalendar().get(Calendar.DAY_OF_WEEK);
477 }
Beverlyfe099b52018-06-18 16:42:14 -0400478
479 private int getTodayDay(int offset) {
480 Calendar cal = new GregorianCalendar();
481 cal.add(Calendar.DATE, offset);
482 return cal.get(Calendar.DAY_OF_WEEK);
483 }
Julia Reynolds4b318432017-11-22 10:56:18 -0500484}