Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.notification; |
| 18 | |
| 19 | import static junit.framework.Assert.assertFalse; |
| 20 | |
| 21 | import static org.junit.Assert.assertEquals; |
| 22 | import static org.junit.Assert.assertTrue; |
| 23 | |
Beverly | be6d352 | 2017-11-20 11:01:59 -0500 | [diff] [blame] | 24 | import android.service.notification.ScheduleCalendar; |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 25 | import android.service.notification.ZenModeConfig; |
Geoffrey Pitsch | 69048f4 | 2017-12-08 09:53:02 -0500 | [diff] [blame] | 26 | import android.support.test.filters.FlakyTest; |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 27 | import android.support.test.runner.AndroidJUnit4; |
| 28 | import android.test.suitebuilder.annotation.SmallTest; |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 29 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 30 | import com.android.server.UiServiceTestCase; |
| 31 | |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 32 | import org.junit.Before; |
Julia Reynolds | 27291c2 | 2018-04-04 09:45:15 -0400 | [diff] [blame] | 33 | import org.junit.Ignore; |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 34 | import org.junit.Test; |
| 35 | import org.junit.runner.RunWith; |
| 36 | |
| 37 | import java.util.Calendar; |
| 38 | import java.util.GregorianCalendar; |
| 39 | |
| 40 | @SmallTest |
| 41 | @RunWith(AndroidJUnit4.class) |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 42 | public class ScheduleCalendarTest extends UiServiceTestCase { |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 43 | |
| 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 Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 52 | } |
| 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 Reynolds | 7edcd78 | 2018-01-12 16:50:49 -0500 | [diff] [blame] | 103 | mScheduleCalendar.setSchedule(mScheduleInfo); |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 104 | |
| 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); |
Beverly | fe099b5 | 2018-06-18 16:42:14 -0400 | [diff] [blame] | 124 | mScheduleInfo.days = new int[] {getTodayDay(), getTodayDay(1)}; |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 125 | mScheduleInfo.startHour = 1; |
| 126 | mScheduleInfo.endHour = 3; |
| 127 | mScheduleInfo.startMinute = 15; |
| 128 | mScheduleInfo.endMinute = 15; |
| 129 | mScheduleInfo.exitAtAlarm = false; |
Julia Reynolds | 7edcd78 | 2018-01-12 16:50:49 -0500 | [diff] [blame] | 130 | mScheduleCalendar.setSchedule(mScheduleInfo); |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 131 | |
| 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); |
Beverly | fe099b5 | 2018-06-18 16:42:14 -0400 | [diff] [blame] | 152 | mScheduleInfo.days = new int[] {getTodayDay(), getTodayDay(1)}; |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 153 | mScheduleInfo.startHour = 22; |
| 154 | mScheduleInfo.endHour = 3; |
| 155 | mScheduleInfo.startMinute = 15; |
| 156 | mScheduleInfo.endMinute = 15; |
| 157 | mScheduleInfo.exitAtAlarm = false; |
Julia Reynolds | 7edcd78 | 2018-01-12 16:50:49 -0500 | [diff] [blame] | 158 | mScheduleCalendar.setSchedule(mScheduleInfo); |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 159 | |
| 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 Reynolds | 7edcd78 | 2018-01-12 16:50:49 -0500 | [diff] [blame] | 177 | mScheduleCalendar.setSchedule(mScheduleInfo); |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 178 | |
| 179 | assertFalse(mScheduleCalendar.shouldExitForAlarm(1000)); |
| 180 | } |
| 181 | |
| 182 | @Test |
| 183 | public void testShouldExitForAlarm_beforeAlarm() { |
| 184 | mScheduleInfo.exitAtAlarm = true; |
| 185 | mScheduleInfo.nextAlarm = 1000; |
Julia Reynolds | 7edcd78 | 2018-01-12 16:50:49 -0500 | [diff] [blame] | 186 | mScheduleCalendar.setSchedule(mScheduleInfo); |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 187 | |
| 188 | assertFalse(mScheduleCalendar.shouldExitForAlarm(999)); |
| 189 | } |
| 190 | |
| 191 | @Test |
| 192 | public void testShouldExitForAlarm_noAlarm() { |
| 193 | mScheduleInfo.exitAtAlarm = true; |
| 194 | mScheduleInfo.nextAlarm = 0; |
Julia Reynolds | 7edcd78 | 2018-01-12 16:50:49 -0500 | [diff] [blame] | 195 | mScheduleCalendar.setSchedule(mScheduleInfo); |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 196 | |
| 197 | assertFalse(mScheduleCalendar.shouldExitForAlarm(999)); |
| 198 | } |
| 199 | |
| 200 | @Test |
| 201 | public void testShouldExitForAlarm() { |
| 202 | mScheduleInfo.exitAtAlarm = true; |
| 203 | mScheduleInfo.nextAlarm = 1000; |
Julia Reynolds | 7edcd78 | 2018-01-12 16:50:49 -0500 | [diff] [blame] | 204 | mScheduleCalendar.setSchedule(mScheduleInfo); |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 205 | |
| 206 | assertTrue(mScheduleCalendar.shouldExitForAlarm(1000)); |
| 207 | } |
| 208 | |
| 209 | @Test |
Beverly | 369dd42 | 2018-03-29 18:14:04 -0400 | [diff] [blame] | 210 | public void testShouldExitForAlarm_oldAlarm() { |
| 211 | // Cal: today 2:15pm |
Beverly | 4b7853e | 2018-05-04 11:21:20 -0400 | [diff] [blame] | 212 | 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); |
Beverly | 369dd42 | 2018-03-29 18:14:04 -0400 | [diff] [blame] | 217 | |
| 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); |
Beverly | 4b7853e | 2018-05-04 11:21:20 -0400 | [diff] [blame] | 228 | assertTrue(mScheduleCalendar.isInSchedule(now.getTimeInMillis())); |
Beverly | 369dd42 | 2018-03-29 18:14:04 -0400 | [diff] [blame] | 229 | |
| 230 | // don't exit for an alarm if it's an old alarm |
Beverly | 4b7853e | 2018-05-04 11:21:20 -0400 | [diff] [blame] | 231 | 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 |
Beverly | fe099b5 | 2018-06-18 16:42:14 -0400 | [diff] [blame] | 253 | mScheduleInfo.days = new int[] {getTodayDay(), getTodayDay(1)}; |
Beverly | 4b7853e | 2018-05-04 11:21:20 -0400 | [diff] [blame] | 254 | 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())); |
Beverly | 369dd42 | 2018-03-29 18:14:04 -0400 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | @Test |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 270 | public void testMaybeSetNextAlarm_settingOff() { |
| 271 | mScheduleInfo.exitAtAlarm = false; |
| 272 | mScheduleInfo.nextAlarm = 0; |
Julia Reynolds | 7edcd78 | 2018-01-12 16:50:49 -0500 | [diff] [blame] | 273 | mScheduleCalendar.setSchedule(mScheduleInfo); |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 274 | |
| 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 Reynolds | 7edcd78 | 2018-01-12 16:50:49 -0500 | [diff] [blame] | 284 | mScheduleCalendar.setSchedule(mScheduleInfo); |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 285 | |
| 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 Reynolds | 7edcd78 | 2018-01-12 16:50:49 -0500 | [diff] [blame] | 295 | mScheduleCalendar.setSchedule(mScheduleInfo); |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 296 | |
| 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 Reynolds | 7edcd78 | 2018-01-12 16:50:49 -0500 | [diff] [blame] | 306 | mScheduleCalendar.setSchedule(mScheduleInfo); |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 307 | |
| 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 Reynolds | 7edcd78 | 2018-01-12 16:50:49 -0500 | [diff] [blame] | 316 | mScheduleCalendar.setSchedule(mScheduleInfo); |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 317 | 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 Reynolds | 7edcd78 | 2018-01-12 16:50:49 -0500 | [diff] [blame] | 328 | mScheduleCalendar.setSchedule(mScheduleInfo); |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 329 | |
| 330 | mScheduleCalendar.maybeSetNextAlarm(1000, 999); |
| 331 | |
| 332 | assertEquals(0, mScheduleInfo.nextAlarm); |
| 333 | } |
| 334 | |
| 335 | @Test |
Beverly | 4e76191 | 2018-08-14 14:58:18 -0400 | [diff] [blame] | 336 | 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 Pitsch | 69048f4 | 2017-12-08 09:53:02 -0500 | [diff] [blame] | 347 | @FlakyTest |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 348 | 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 Reynolds | 7edcd78 | 2018-01-12 16:50:49 -0500 | [diff] [blame] | 359 | mScheduleCalendar.setSchedule(mScheduleInfo); |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 360 | |
| 361 | assertTrue(mScheduleCalendar.isInSchedule(cal.getTimeInMillis())); |
| 362 | } |
| 363 | |
| 364 | @Test |
Geoffrey Pitsch | 69048f4 | 2017-12-08 09:53:02 -0500 | [diff] [blame] | 365 | @FlakyTest |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 366 | 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 Reynolds | 7edcd78 | 2018-01-12 16:50:49 -0500 | [diff] [blame] | 377 | mScheduleCalendar.setSchedule(mScheduleInfo); |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 378 | |
| 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 Reynolds | 7edcd78 | 2018-01-12 16:50:49 -0500 | [diff] [blame] | 395 | mScheduleCalendar.setSchedule(mScheduleInfo); |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 396 | |
| 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 Reynolds | 7edcd78 | 2018-01-12 16:50:49 -0500 | [diff] [blame] | 412 | mScheduleCalendar.setSchedule(mScheduleInfo); |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 413 | |
| 414 | assertFalse(mScheduleCalendar.isInSchedule(cal.getTimeInMillis())); |
| 415 | } |
| 416 | |
Beverly | 4b7853e | 2018-05-04 11:21:20 -0400 | [diff] [blame] | 417 | @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 | |
Beverly | fe099b5 | 2018-06-18 16:42:14 -0400 | [diff] [blame] | 432 | mScheduleInfo.days = new int[] {getTodayDay(), getTodayDay(1)}; |
Beverly | 4b7853e | 2018-05-04 11:21:20 -0400 | [diff] [blame] | 433 | 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 | |
Beverly | fe099b5 | 2018-06-18 16:42:14 -0400 | [diff] [blame] | 460 | mScheduleInfo.days = new int[] {getTodayDay(), getTodayDay(1)}; |
Beverly | 4b7853e | 2018-05-04 11:21:20 -0400 | [diff] [blame] | 461 | 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 Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 475 | private int getTodayDay() { |
| 476 | return new GregorianCalendar().get(Calendar.DAY_OF_WEEK); |
| 477 | } |
Beverly | fe099b5 | 2018-06-18 16:42:14 -0400 | [diff] [blame] | 478 | |
| 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 Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 484 | } |