|
1 | 1 | package com.mobilerun.portal.triggers |
2 | 2 |
|
| 3 | +import com.mobilerun.portal.events.model.EventType |
3 | 4 | import com.mobilerun.portal.taskprompt.PortalTaskSettings |
4 | 5 | import org.junit.Assert.assertEquals |
5 | 6 | import org.junit.Assert.assertFalse |
6 | 7 | import org.junit.Assert.assertNotNull |
| 8 | +import org.junit.Assert.assertNull |
7 | 9 | import org.junit.Assert.assertTrue |
8 | 10 | import org.junit.Test |
9 | 11 | import org.json.JSONObject |
@@ -251,6 +253,155 @@ class TriggerCoreTest { |
251 | 253 | } |
252 | 254 | } |
253 | 255 |
|
| 256 | + @Test |
| 257 | + fun `every trigger source has an intended visibility and cooldown decision`() { |
| 258 | + val defaults = TriggerEditorSupport.Visibility() |
| 259 | + val expected = mapOf( |
| 260 | + TriggerSource.TIME_DELAY to TriggerEditorSupport.Visibility( |
| 261 | + showDelay = true, |
| 262 | + showCooldown = false, |
| 263 | + showRunLimit = false, |
| 264 | + ), |
| 265 | + TriggerSource.TIME_ABSOLUTE to TriggerEditorSupport.Visibility( |
| 266 | + showAbsoluteTime = true, |
| 267 | + showCooldown = false, |
| 268 | + showRunLimit = false, |
| 269 | + ), |
| 270 | + TriggerSource.TIME_DAILY to TriggerEditorSupport.Visibility( |
| 271 | + showRecurringTime = true, |
| 272 | + showCooldown = false, |
| 273 | + ), |
| 274 | + TriggerSource.TIME_WEEKLY to TriggerEditorSupport.Visibility( |
| 275 | + showRecurringTime = true, |
| 276 | + showCooldown = false, |
| 277 | + ), |
| 278 | + TriggerSource.NOTIFICATION_POSTED to TriggerEditorSupport.Visibility( |
| 279 | + showMatchMode = true, |
| 280 | + showPackageName = true, |
| 281 | + showTitleFilter = true, |
| 282 | + showTextFilter = true, |
| 283 | + ), |
| 284 | + TriggerSource.NOTIFICATION_REMOVED to TriggerEditorSupport.Visibility( |
| 285 | + showMatchMode = true, |
| 286 | + showPackageName = true, |
| 287 | + showTitleFilter = true, |
| 288 | + showTextFilter = true, |
| 289 | + ), |
| 290 | + TriggerSource.APP_ENTERED to TriggerEditorSupport.Visibility( |
| 291 | + showMatchMode = true, |
| 292 | + showPackageName = true, |
| 293 | + ), |
| 294 | + TriggerSource.APP_EXITED to TriggerEditorSupport.Visibility( |
| 295 | + showMatchMode = true, |
| 296 | + showPackageName = true, |
| 297 | + ), |
| 298 | + TriggerSource.BATTERY_LEVEL_CHANGED to TriggerEditorSupport.Visibility( |
| 299 | + showThreshold = true, |
| 300 | + ), |
| 301 | + TriggerSource.NETWORK_CONNECTED to TriggerEditorSupport.Visibility( |
| 302 | + showNetworkType = true, |
| 303 | + ), |
| 304 | + TriggerSource.NETWORK_TYPE_CHANGED to TriggerEditorSupport.Visibility( |
| 305 | + showNetworkType = true, |
| 306 | + ), |
| 307 | + TriggerSource.SMS_RECEIVED to TriggerEditorSupport.Visibility( |
| 308 | + showMatchMode = true, |
| 309 | + showPhoneNumber = true, |
| 310 | + showMessageFilter = true, |
| 311 | + ), |
| 312 | + // Intended-defaults allow-list: these sources deliberately use |
| 313 | + // the plain editor defaults. |
| 314 | + TriggerSource.BATTERY_LOW to defaults, |
| 315 | + TriggerSource.BATTERY_OKAY to defaults, |
| 316 | + TriggerSource.POWER_CONNECTED to defaults, |
| 317 | + TriggerSource.POWER_DISCONNECTED to defaults, |
| 318 | + TriggerSource.USER_PRESENT to defaults, |
| 319 | + ) |
| 320 | + |
| 321 | + assertEquals(expected.keys, TriggerSource.entries.toSet()) |
| 322 | + expected.forEach { (source, visibility) -> |
| 323 | + assertEquals(source.name, visibility, TriggerEditorSupport.visibilityFor(source)) |
| 324 | + val capabilities = TriggerEditorSupport.capabilitiesFor(source) |
| 325 | + assertEquals(source.name, visibility.showCooldown, capabilities.supportsCooldown) |
| 326 | + assertEquals(source.name, visibility.showRunLimit, capabilities.supportsRunLimit) |
| 327 | + assertEquals( |
| 328 | + source.name, |
| 329 | + if (visibility.showCooldown) 60L else 0L, |
| 330 | + TriggerEditorSupport.defaultCooldownSecondsFor(source).toLong(), |
| 331 | + ) |
| 332 | + } |
| 333 | + } |
| 334 | + |
| 335 | + @Test |
| 336 | + fun `portal event mapping decides every event type`() { |
| 337 | + val expected = mapOf( |
| 338 | + EventType.NOTIFICATION_POSTED to TriggerSource.NOTIFICATION_POSTED, |
| 339 | + EventType.NOTIFICATION_REMOVED to TriggerSource.NOTIFICATION_REMOVED, |
| 340 | + EventType.APP_ENTERED to TriggerSource.APP_ENTERED, |
| 341 | + EventType.APP_EXITED to TriggerSource.APP_EXITED, |
| 342 | + EventType.BATTERY_LOW to TriggerSource.BATTERY_LOW, |
| 343 | + EventType.BATTERY_OKAY to TriggerSource.BATTERY_OKAY, |
| 344 | + EventType.BATTERY_LEVEL_CHANGED to TriggerSource.BATTERY_LEVEL_CHANGED, |
| 345 | + EventType.POWER_CONNECTED to TriggerSource.POWER_CONNECTED, |
| 346 | + EventType.POWER_DISCONNECTED to TriggerSource.POWER_DISCONNECTED, |
| 347 | + EventType.USER_PRESENT to TriggerSource.USER_PRESENT, |
| 348 | + EventType.NETWORK_CONNECTED to TriggerSource.NETWORK_CONNECTED, |
| 349 | + EventType.NETWORK_TYPE_CHANGED to TriggerSource.NETWORK_TYPE_CHANGED, |
| 350 | + EventType.SMS_RECEIVED to TriggerSource.SMS_RECEIVED, |
| 351 | + ) |
| 352 | + val intentionallyUnmapped = setOf( |
| 353 | + EventType.NOTIFICATION, |
| 354 | + EventType.PING, |
| 355 | + EventType.PONG, |
| 356 | + EventType.UNKNOWN, |
| 357 | + ) |
| 358 | + |
| 359 | + assertEquals(expected.keys, EventType.entries.toSet() - intentionallyUnmapped) |
| 360 | + expected.forEach { (type, source) -> |
| 361 | + assertEquals(type.name, source, TriggerRuntime.triggerSourceFor(type)) |
| 362 | + } |
| 363 | + intentionallyUnmapped.forEach { type -> |
| 364 | + assertNull(type.name, TriggerRuntime.triggerSourceFor(type)) |
| 365 | + } |
| 366 | + } |
| 367 | + |
| 368 | + @Test |
| 369 | + fun `every trigger source has a notification access and debounce decision`() { |
| 370 | + // Pair(requiresNotificationAccess, shouldDebounce) per source. |
| 371 | + val expected = mapOf( |
| 372 | + TriggerSource.TIME_DELAY to Pair(false, false), |
| 373 | + TriggerSource.TIME_ABSOLUTE to Pair(false, false), |
| 374 | + TriggerSource.TIME_DAILY to Pair(false, false), |
| 375 | + TriggerSource.TIME_WEEKLY to Pair(false, false), |
| 376 | + TriggerSource.NOTIFICATION_POSTED to Pair(true, true), |
| 377 | + // Removed notifications need listener access but carry no |
| 378 | + // message text, so they are never debounced. |
| 379 | + TriggerSource.NOTIFICATION_REMOVED to Pair(true, false), |
| 380 | + TriggerSource.APP_ENTERED to Pair(false, false), |
| 381 | + TriggerSource.APP_EXITED to Pair(false, false), |
| 382 | + TriggerSource.BATTERY_LOW to Pair(false, false), |
| 383 | + TriggerSource.BATTERY_OKAY to Pair(false, false), |
| 384 | + TriggerSource.BATTERY_LEVEL_CHANGED to Pair(false, false), |
| 385 | + TriggerSource.POWER_CONNECTED to Pair(false, false), |
| 386 | + TriggerSource.POWER_DISCONNECTED to Pair(false, false), |
| 387 | + TriggerSource.USER_PRESENT to Pair(false, false), |
| 388 | + TriggerSource.NETWORK_CONNECTED to Pair(false, false), |
| 389 | + TriggerSource.NETWORK_TYPE_CHANGED to Pair(false, false), |
| 390 | + TriggerSource.SMS_RECEIVED to Pair(false, false), |
| 391 | + ) |
| 392 | + |
| 393 | + assertEquals(expected.keys, TriggerSource.entries.toSet()) |
| 394 | + expected.forEach { (source, decision) -> |
| 395 | + val (needsAccess, debounced) = decision |
| 396 | + assertEquals( |
| 397 | + source.name, |
| 398 | + needsAccess, |
| 399 | + TriggerEditorSupport.requiresNotificationAccess(source), |
| 400 | + ) |
| 401 | + assertEquals(source.name, debounced, TriggerRuntime.shouldDebounce(source)) |
| 402 | + } |
| 403 | + } |
| 404 | + |
254 | 405 | @Test |
255 | 406 | fun `editor support clears stale fields for battery triggers`() { |
256 | 407 | val sanitized = TriggerEditorSupport.sanitize( |
|
0 commit comments