Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 1 | package com.android.server.notification; |
| 2 | |
Jason Monk | ec4b677 | 2017-12-06 10:31:09 -0500 | [diff] [blame] | 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertFalse; |
| 5 | import static org.junit.Assert.assertTrue; |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 6 | import static org.mockito.Mockito.spy; |
| 7 | |
| 8 | import android.content.Intent; |
| 9 | import android.net.Uri; |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 10 | import android.service.notification.Condition; |
Beverly | be6d352 | 2017-11-20 11:01:59 -0500 | [diff] [blame] | 11 | import android.service.notification.ScheduleCalendar; |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 12 | import android.service.notification.ZenModeConfig; |
Jason Monk | ec4b677 | 2017-12-06 10:31:09 -0500 | [diff] [blame] | 13 | import android.support.test.filters.SmallTest; |
| 14 | import android.support.test.runner.AndroidJUnit4; |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 15 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 16 | import com.android.server.UiServiceTestCase; |
| 17 | |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 18 | import org.junit.Before; |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 19 | import org.junit.Test; |
Jason Monk | ec4b677 | 2017-12-06 10:31:09 -0500 | [diff] [blame] | 20 | import org.junit.runner.RunWith; |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 21 | import org.mockito.MockitoAnnotations; |
| 22 | |
| 23 | import java.util.Calendar; |
| 24 | import java.util.GregorianCalendar; |
| 25 | |
Jason Monk | ec4b677 | 2017-12-06 10:31:09 -0500 | [diff] [blame] | 26 | @RunWith(AndroidJUnit4.class) |
| 27 | @SmallTest |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 28 | public class ScheduleConditionProviderTest extends UiServiceTestCase { |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 29 | |
| 30 | ScheduleConditionProvider mService; |
| 31 | |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 32 | @Before |
| 33 | public void setUp() throws Exception { |
| 34 | MockitoAnnotations.initMocks(this); |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 35 | |
| 36 | Intent startIntent = |
| 37 | new Intent("com.android.server.notification.ScheduleConditionProvider"); |
| 38 | startIntent.setPackage("android"); |
Jason Monk | ec4b677 | 2017-12-06 10:31:09 -0500 | [diff] [blame] | 39 | ScheduleConditionProvider service = new ScheduleConditionProvider(); |
| 40 | service.attach( |
| 41 | getContext(), |
| 42 | null, // ActivityThread not actually used in Service |
| 43 | ScheduleConditionProvider.class.getName(), |
| 44 | null, // token not needed when not talking with the activity manager |
| 45 | null, |
| 46 | null // mocked services don't talk with the activity manager |
| 47 | ); |
| 48 | service.onCreate(); |
| 49 | service.onBind(startIntent); |
| 50 | mService = spy(service); |
Julia Reynolds | 4b31843 | 2017-11-22 10:56:18 -0500 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | @Test |
| 54 | public void testIsValidConditionId_incomplete() throws Exception { |
| 55 | Uri badConditionId = Uri.EMPTY; |
| 56 | assertFalse(mService.isValidConditionId(badConditionId)); |
| 57 | assertEquals(Condition.STATE_ERROR, |
| 58 | mService.evaluateSubscriptionLocked(badConditionId, null, 0, 1000).state); |
| 59 | } |
| 60 | |
| 61 | @Test |
| 62 | public void testIsValidConditionId() throws Exception { |
| 63 | ZenModeConfig.ScheduleInfo info = new ZenModeConfig.ScheduleInfo(); |
| 64 | info.days = new int[] {1, 2, 4}; |
| 65 | info.startHour = 8; |
| 66 | info.startMinute = 56; |
| 67 | info.nextAlarm = 1000; |
| 68 | info.exitAtAlarm = true; |
| 69 | info.endHour = 12; |
| 70 | info.endMinute = 9; |
| 71 | Uri conditionId = ZenModeConfig.toScheduleConditionId(info); |
| 72 | assertTrue(mService.isValidConditionId(conditionId)); |
| 73 | } |
| 74 | |
| 75 | @Test |
| 76 | public void testEvaluateSubscription_noAlarmExit_InSchedule() { |
| 77 | Calendar now = getNow(); |
| 78 | |
| 79 | // Schedule - 1 hour long; starts now |
| 80 | ZenModeConfig.ScheduleInfo info = new ZenModeConfig.ScheduleInfo(); |
| 81 | info.days = new int[] {Calendar.FRIDAY}; |
| 82 | info.startHour = now.get(Calendar.HOUR_OF_DAY); |
| 83 | info.startMinute = now.get(Calendar.MINUTE); |
| 84 | info.nextAlarm = 0; |
| 85 | info.exitAtAlarm = false; |
| 86 | info.endHour = now.get(Calendar.HOUR_OF_DAY) + 1; |
| 87 | info.endMinute = info.startMinute; |
| 88 | Uri conditionId = ZenModeConfig.toScheduleConditionId(info); |
| 89 | ScheduleCalendar cal = new ScheduleCalendar(); |
| 90 | cal.setSchedule(info); |
| 91 | assertTrue(cal.isInSchedule(now.getTimeInMillis())); |
| 92 | |
| 93 | Condition condition = mService.evaluateSubscriptionLocked( |
| 94 | conditionId, cal, now.getTimeInMillis(), now.getTimeInMillis() + 1000); |
| 95 | |
| 96 | assertEquals(Condition.STATE_TRUE, condition.state); |
| 97 | } |
| 98 | |
| 99 | @Test |
| 100 | public void testEvaluateSubscription_noAlarmExit_InScheduleSnoozed() { |
| 101 | Calendar now = getNow(); |
| 102 | |
| 103 | // Schedule - 1 hour long; starts now |
| 104 | ZenModeConfig.ScheduleInfo info = new ZenModeConfig.ScheduleInfo(); |
| 105 | info.days = new int[] {Calendar.FRIDAY}; |
| 106 | info.startHour = now.get(Calendar.HOUR_OF_DAY); |
| 107 | info.startMinute = now.get(Calendar.MINUTE); |
| 108 | info.nextAlarm = 0; |
| 109 | info.exitAtAlarm = false; |
| 110 | info.endHour = now.get(Calendar.HOUR_OF_DAY) + 1; |
| 111 | info.endMinute = info.startMinute; |
| 112 | Uri conditionId = ZenModeConfig.toScheduleConditionId(info); |
| 113 | ScheduleCalendar cal = new ScheduleCalendar(); |
| 114 | cal.setSchedule(info); |
| 115 | assertTrue(cal.isInSchedule(now.getTimeInMillis())); |
| 116 | |
| 117 | mService.addSnoozed(conditionId); |
| 118 | |
| 119 | Condition condition = mService.evaluateSubscriptionLocked( |
| 120 | conditionId, cal, now.getTimeInMillis(), now.getTimeInMillis() + 1000); |
| 121 | |
| 122 | assertEquals(Condition.STATE_FALSE, condition.state); |
| 123 | } |
| 124 | |
| 125 | @Test |
| 126 | public void testEvaluateSubscription_noAlarmExit_beforeSchedule() { |
| 127 | Calendar now = new GregorianCalendar(); |
| 128 | now.set(Calendar.HOUR_OF_DAY, 14); |
| 129 | now.set(Calendar.MINUTE, 15); |
| 130 | now.set(Calendar.SECOND, 59); |
| 131 | now.set(Calendar.MILLISECOND, 0); |
| 132 | now.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY); |
| 133 | |
| 134 | // Schedule - 1 hour long; starts in 1 second |
| 135 | ZenModeConfig.ScheduleInfo info = new ZenModeConfig.ScheduleInfo(); |
| 136 | info.days = new int[] {Calendar.FRIDAY}; |
| 137 | info.startHour = now.get(Calendar.HOUR_OF_DAY); |
| 138 | info.startMinute = now.get(Calendar.MINUTE) + 1; |
| 139 | info.nextAlarm = 0; |
| 140 | info.exitAtAlarm = false; |
| 141 | info.endHour = now.get(Calendar.HOUR_OF_DAY) + 1; |
| 142 | info.endMinute = info.startMinute; |
| 143 | Uri conditionId = ZenModeConfig.toScheduleConditionId(info); |
| 144 | ScheduleCalendar cal = new ScheduleCalendar(); |
| 145 | cal.setSchedule(info); |
| 146 | |
| 147 | Condition condition = mService.evaluateSubscriptionLocked( |
| 148 | conditionId, cal, now.getTimeInMillis(), now.getTimeInMillis() + 1000); |
| 149 | |
| 150 | assertEquals(Condition.STATE_FALSE, condition.state); |
| 151 | } |
| 152 | |
| 153 | @Test |
| 154 | public void testEvaluateSubscription_noAlarmExit_endSchedule() { |
| 155 | Calendar now = getNow(); |
| 156 | |
| 157 | // Schedule - 1 hour long; ends now |
| 158 | ZenModeConfig.ScheduleInfo info = new ZenModeConfig.ScheduleInfo(); |
| 159 | info.days = new int[] {Calendar.FRIDAY}; |
| 160 | info.startHour = now.get(Calendar.HOUR_OF_DAY) - 1; |
| 161 | info.startMinute = now.get(Calendar.MINUTE); |
| 162 | info.nextAlarm = 0; |
| 163 | info.exitAtAlarm = false; |
| 164 | info.endHour = now.get(Calendar.HOUR_OF_DAY); |
| 165 | info.endMinute = now.get(Calendar.MINUTE); |
| 166 | Uri conditionId = ZenModeConfig.toScheduleConditionId(info); |
| 167 | ScheduleCalendar cal = new ScheduleCalendar(); |
| 168 | cal.setSchedule(info); |
| 169 | |
| 170 | Condition condition = mService.evaluateSubscriptionLocked( |
| 171 | conditionId, cal, now.getTimeInMillis(), now.getTimeInMillis() + 1000); |
| 172 | |
| 173 | assertEquals(Condition.STATE_FALSE, condition.state); |
| 174 | } |
| 175 | |
| 176 | @Test |
| 177 | public void testEvaluateSubscription_alarmSetBeforeInSchedule() { |
| 178 | Calendar now = getNow(); |
| 179 | |
| 180 | // Schedule - 1 hour long; starts now, ends with alarm |
| 181 | ZenModeConfig.ScheduleInfo info = getScheduleEndsInHour(now); |
| 182 | Uri conditionId = ZenModeConfig.toScheduleConditionId(info); |
| 183 | ScheduleCalendar cal = new ScheduleCalendar(); |
| 184 | cal.setSchedule(info); |
| 185 | |
| 186 | // an hour before start, update with an alarm that will fire during the schedule |
| 187 | mService.evaluateSubscriptionLocked( |
| 188 | conditionId, cal, now.getTimeInMillis() - 1000, now.getTimeInMillis() + 1000); |
| 189 | |
| 190 | // at start, should be in dnd |
| 191 | Condition condition = mService.evaluateSubscriptionLocked( |
| 192 | conditionId, cal, now.getTimeInMillis(), now.getTimeInMillis() + 1000); |
| 193 | assertEquals(Condition.STATE_TRUE, condition.state); |
| 194 | |
| 195 | // at alarm fire time, should exit dnd |
| 196 | assertTrue(cal.isInSchedule(now.getTimeInMillis() + 1000)); |
| 197 | assertTrue("" + info.nextAlarm + " " + now.getTimeInMillis(), |
| 198 | cal.shouldExitForAlarm(now.getTimeInMillis() + 1000)); |
| 199 | condition = mService.evaluateSubscriptionLocked( |
| 200 | conditionId, cal, now.getTimeInMillis() + 1000, 0); |
| 201 | assertEquals(Condition.STATE_FALSE, condition.state); |
| 202 | } |
| 203 | |
| 204 | @Test |
| 205 | public void testEvaluateSubscription_alarmSetInSchedule() { |
| 206 | Calendar now = getNow(); |
| 207 | |
| 208 | // Schedule - 1 hour long; starts now, ends with alarm |
| 209 | ZenModeConfig.ScheduleInfo info = getScheduleEndsInHour(now); |
| 210 | Uri conditionId = ZenModeConfig.toScheduleConditionId(info); |
| 211 | ScheduleCalendar cal = new ScheduleCalendar(); |
| 212 | cal.setSchedule(info); |
| 213 | |
| 214 | // at start, should be in dnd |
| 215 | Condition condition = mService.evaluateSubscriptionLocked( |
| 216 | conditionId, cal, now.getTimeInMillis(), 0); |
| 217 | assertEquals(Condition.STATE_TRUE, condition.state); |
| 218 | |
| 219 | // in schedule, update with alarm time, should be in dnd |
| 220 | condition = mService.evaluateSubscriptionLocked( |
| 221 | conditionId, cal, now.getTimeInMillis() + 500, now.getTimeInMillis() + 1000); |
| 222 | assertEquals(Condition.STATE_TRUE, condition.state); |
| 223 | |
| 224 | // at alarm fire time, should exit dnd |
| 225 | assertTrue(cal.isInSchedule(now.getTimeInMillis() + 1000)); |
| 226 | assertTrue("" + info.nextAlarm + " " + now.getTimeInMillis(), |
| 227 | cal.shouldExitForAlarm(now.getTimeInMillis() + 1000)); |
| 228 | condition = mService.evaluateSubscriptionLocked( |
| 229 | conditionId, cal, now.getTimeInMillis() + 1000, 0); |
| 230 | assertEquals(Condition.STATE_FALSE, condition.state); |
| 231 | } |
| 232 | |
| 233 | @Test |
| 234 | public void testEvaluateSubscription_earlierAlarmSet() { |
| 235 | Calendar now = getNow(); |
| 236 | |
| 237 | // Schedule - 1 hour long; starts now, ends with alarm |
| 238 | ZenModeConfig.ScheduleInfo info = getScheduleEndsInHour(now); |
| 239 | Uri conditionId = ZenModeConfig.toScheduleConditionId(info); |
| 240 | ScheduleCalendar cal = new ScheduleCalendar(); |
| 241 | cal.setSchedule(info); |
| 242 | |
| 243 | // at start, should be in dnd, alarm in 2000 ms |
| 244 | Condition condition = mService.evaluateSubscriptionLocked( |
| 245 | conditionId, cal, now.getTimeInMillis(), now.getTimeInMillis() + 2000); |
| 246 | assertEquals(Condition.STATE_TRUE, condition.state); |
| 247 | |
| 248 | // in schedule, update with earlier alarm time, should be in dnd |
| 249 | condition = mService.evaluateSubscriptionLocked( |
| 250 | conditionId, cal, now.getTimeInMillis() + 500, now.getTimeInMillis() + 1000); |
| 251 | assertEquals(Condition.STATE_TRUE, condition.state); |
| 252 | |
| 253 | // at earliest alarm fire time, should exit dnd |
| 254 | assertTrue(cal.isInSchedule(now.getTimeInMillis() + 1000)); |
| 255 | assertTrue("" + info.nextAlarm + " " + now.getTimeInMillis(), |
| 256 | cal.shouldExitForAlarm(now.getTimeInMillis() + 1000)); |
| 257 | condition = mService.evaluateSubscriptionLocked( |
| 258 | conditionId, cal, now.getTimeInMillis() + 1000, 0); |
| 259 | assertEquals(Condition.STATE_FALSE, condition.state); |
| 260 | } |
| 261 | |
| 262 | @Test |
| 263 | public void testEvaluateSubscription_laterAlarmSet() { |
| 264 | Calendar now = getNow(); |
| 265 | |
| 266 | // Schedule - 1 hour long; starts now, ends with alarm |
| 267 | ZenModeConfig.ScheduleInfo info = getScheduleEndsInHour(now); |
| 268 | Uri conditionId = ZenModeConfig.toScheduleConditionId(info); |
| 269 | ScheduleCalendar cal = new ScheduleCalendar(); |
| 270 | cal.setSchedule(info); |
| 271 | |
| 272 | // at start, should be in dnd, alarm in 500 ms |
| 273 | Condition condition = mService.evaluateSubscriptionLocked( |
| 274 | conditionId, cal, now.getTimeInMillis(), now.getTimeInMillis() + 500); |
| 275 | assertEquals(Condition.STATE_TRUE, condition.state); |
| 276 | |
| 277 | // in schedule, update with later alarm time, should be in dnd |
| 278 | condition = mService.evaluateSubscriptionLocked( |
| 279 | conditionId, cal, now.getTimeInMillis() + 250, now.getTimeInMillis() + 1000); |
| 280 | assertEquals(Condition.STATE_TRUE, condition.state); |
| 281 | |
| 282 | // at earliest alarm fire time, should exit dnd |
| 283 | assertTrue(cal.isInSchedule(now.getTimeInMillis() + 500)); |
| 284 | assertTrue("" + info.nextAlarm + " " + now.getTimeInMillis(), |
| 285 | cal.shouldExitForAlarm(now.getTimeInMillis() + 500)); |
| 286 | condition = mService.evaluateSubscriptionLocked( |
| 287 | conditionId, cal, now.getTimeInMillis() + 500, 0); |
| 288 | assertEquals(Condition.STATE_FALSE, condition.state); |
| 289 | } |
| 290 | |
| 291 | @Test |
| 292 | public void testEvaluateSubscription_alarmCanceled() { |
| 293 | Calendar now = getNow(); |
| 294 | |
| 295 | // Schedule - 1 hour long; starts now, ends with alarm |
| 296 | ZenModeConfig.ScheduleInfo info = getScheduleEndsInHour(now); |
| 297 | Uri conditionId = ZenModeConfig.toScheduleConditionId(info); |
| 298 | ScheduleCalendar cal = new ScheduleCalendar(); |
| 299 | cal.setSchedule(info); |
| 300 | |
| 301 | // at start, should be in dnd, alarm in 500 ms |
| 302 | Condition condition = mService.evaluateSubscriptionLocked( |
| 303 | conditionId, cal, now.getTimeInMillis(), now.getTimeInMillis() + 500); |
| 304 | assertEquals(Condition.STATE_TRUE, condition.state); |
| 305 | |
| 306 | // in schedule, cancel alarm |
| 307 | condition = mService.evaluateSubscriptionLocked( |
| 308 | conditionId, cal, now.getTimeInMillis() + 250, 0); |
| 309 | assertEquals(Condition.STATE_TRUE, condition.state); |
| 310 | |
| 311 | // at previous alarm time, should not exit DND |
| 312 | assertTrue(cal.isInSchedule(now.getTimeInMillis() + 500)); |
| 313 | assertFalse(cal.shouldExitForAlarm(now.getTimeInMillis() + 500)); |
| 314 | condition = mService.evaluateSubscriptionLocked( |
| 315 | conditionId, cal, now.getTimeInMillis() + 500, 0); |
| 316 | assertEquals(Condition.STATE_TRUE, condition.state); |
| 317 | |
| 318 | // end of schedule, exit DND |
| 319 | now.add(Calendar.HOUR_OF_DAY, 1); |
| 320 | condition = mService.evaluateSubscriptionLocked(conditionId, cal, now.getTimeInMillis(), 0); |
| 321 | assertEquals(Condition.STATE_FALSE, condition.state); |
| 322 | } |
| 323 | |
| 324 | private Calendar getNow() { |
| 325 | Calendar now = new GregorianCalendar(); |
| 326 | now.set(Calendar.HOUR_OF_DAY, 14); |
| 327 | now.set(Calendar.MINUTE, 16); |
| 328 | now.set(Calendar.SECOND, 0); |
| 329 | now.set(Calendar.MILLISECOND, 0); |
| 330 | now.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY); |
| 331 | return now; |
| 332 | } |
| 333 | |
| 334 | private ZenModeConfig.ScheduleInfo getScheduleEndsInHour(Calendar now) { |
| 335 | ZenModeConfig.ScheduleInfo info = new ZenModeConfig.ScheduleInfo(); |
| 336 | info.days = new int[] {Calendar.FRIDAY}; |
| 337 | info.startHour = now.get(Calendar.HOUR_OF_DAY); |
| 338 | info.startMinute = now.get(Calendar.MINUTE); |
| 339 | info.exitAtAlarm = true; |
| 340 | info.endHour = now.get(Calendar.HOUR_OF_DAY) + 1; |
| 341 | info.endMinute = now.get(Calendar.MINUTE); |
| 342 | return info; |
| 343 | } |
| 344 | } |