@Test @Config(minSdk = Build.VERSION_CODES.N) public void getAutomaticZenRules_notificationAccessDenied_shouldThrowSecurityException() { try { notificationManager.getAutomaticZenRules(); fail("Should have thrown SecurityException"); } catch (SecurityException expected) { } }
@Test @Config(minSdk = Build.VERSION_CODES.N) public void getAutomaticZenRules_noRulesAdded_shouldReturnEmptyMap() { shadowOf(notificationManager).setNotificationPolicyAccessGranted(true); assertThat(notificationManager.getAutomaticZenRules()).isEmpty(); }
@Test @Config(minSdk = Build.VERSION_CODES.N) public void addAutomaticZenRule_oneRule_shouldAddRuleAndReturnId() { shadowOf(notificationManager).setNotificationPolicyAccessGranted(true); AutomaticZenRule rule = new AutomaticZenRule( "name", new ComponentName("pkg", "cls"), Uri.parse("condition://id"), NotificationManager.INTERRUPTION_FILTER_PRIORITY, /* enabled= */ true); String id = notificationManager.addAutomaticZenRule(rule); assertThat(id).isNotEmpty(); assertThat(notificationManager.getAutomaticZenRule(id)).isEqualTo(rule); assertThat(notificationManager.getAutomaticZenRules()).containsExactly(id, rule); }
@Test @Config(minSdk = Build.VERSION_CODES.N) public void removeAutomaticZenRule_nonexistentId_shouldAndReturnFalse() { shadowOf(notificationManager).setNotificationPolicyAccessGranted(true); AutomaticZenRule rule = new AutomaticZenRule( "name", new ComponentName("pkg", "cls"), Uri.parse("condition://id"), NotificationManager.INTERRUPTION_FILTER_PRIORITY, /* enabled= */ true); String id = notificationManager.addAutomaticZenRule(rule); String nonexistentId = "id_different_from_" + id; assertThat(notificationManager.removeAutomaticZenRule(nonexistentId)).isFalse(); // The rules stored in NotificationManager should remain unchanged. assertThat(notificationManager.getAutomaticZenRules()).containsExactly(id, rule); }
@Test @Config(minSdk = Build.VERSION_CODES.N) public void addAutomaticZenRule_twoRules_shouldAddBothRulesAndReturnDifferentIds() { shadowOf(notificationManager).setNotificationPolicyAccessGranted(true); AutomaticZenRule rule1 = new AutomaticZenRule( "name1", new ComponentName("pkg1", "cls1"), Uri.parse("condition://id1"), NotificationManager.INTERRUPTION_FILTER_PRIORITY, /* enabled= */ true); AutomaticZenRule rule2 = new AutomaticZenRule( "name2", new ComponentName("pkg2", "cls2"), Uri.parse("condition://id2"), NotificationManager.INTERRUPTION_FILTER_ALARMS, /* enabled= */ false); String id1 = notificationManager.addAutomaticZenRule(rule1); String id2 = notificationManager.addAutomaticZenRule(rule2); assertThat(id2).isNotEqualTo(id1); assertThat(notificationManager.getAutomaticZenRule(id1)).isEqualTo(rule1); assertThat(notificationManager.getAutomaticZenRule(id2)).isEqualTo(rule2); assertThat(notificationManager.getAutomaticZenRules()).containsExactly(id1, rule1, id2, rule2); }
@Test @Config(minSdk = Build.VERSION_CODES.N) public void removeAutomaticZenRule_existingId_shouldRemoveRuleAndReturnTrue() { shadowOf(notificationManager).setNotificationPolicyAccessGranted(true); AutomaticZenRule rule1 = new AutomaticZenRule( "name1", new ComponentName("pkg1", "cls1"), Uri.parse("condition://id1"), NotificationManager.INTERRUPTION_FILTER_PRIORITY, /* enabled= */ true); AutomaticZenRule rule2 = new AutomaticZenRule( "name2", new ComponentName("pkg2", "cls2"), Uri.parse("condition://id2"), NotificationManager.INTERRUPTION_FILTER_ALARMS, /* enabled= */ false); String id1 = notificationManager.addAutomaticZenRule(rule1); String id2 = notificationManager.addAutomaticZenRule(rule2); assertThat(notificationManager.removeAutomaticZenRule(id1)).isTrue(); assertThat(notificationManager.getAutomaticZenRule(id1)).isNull(); assertThat(notificationManager.getAutomaticZenRule(id2)).isEqualTo(rule2); assertThat(notificationManager.getAutomaticZenRules()).containsExactly(id2, rule2); }
@Test @Config(minSdk = Build.VERSION_CODES.N) public void updateAutomaticZenRule_nonexistentId_shouldThrowSecurityException() { shadowOf(notificationManager).setNotificationPolicyAccessGranted(true); AutomaticZenRule rule = new AutomaticZenRule( "name", new ComponentName("pkg", "cls"), Uri.parse("condition://id"), NotificationManager.INTERRUPTION_FILTER_PRIORITY, /* enabled= */ true); String id = notificationManager.addAutomaticZenRule(rule); String nonexistentId = "id_different_from_" + id; AutomaticZenRule updatedRule = new AutomaticZenRule( "updated_name", new ComponentName("updated_pkg", "updated_cls"), Uri.parse("condition://updated_id"), NotificationManager.INTERRUPTION_FILTER_ALL, /* enabled= */ false); try { assertThat(notificationManager.updateAutomaticZenRule(nonexistentId, updatedRule)); fail("Should have thrown SecurityException"); } catch (SecurityException expected) { } assertThat(notificationManager.getAutomaticZenRule(id)).isEqualTo(rule); assertThat(notificationManager.getAutomaticZenRule(nonexistentId)).isNull(); assertThat(notificationManager.getAutomaticZenRules()).containsExactly(id, rule); }
assertThat(notificationManager.getAutomaticZenRules()) .containsExactly(id1, rule1, id2, updatedRule);
@Test @Config(minSdk = Build.VERSION_CODES.N) public void setNotificationPolicyAccessGranted_temporarilyDenyAccess_shouldClearAutomaticZenRules() { shadowOf(notificationManager).setNotificationPolicyAccessGranted(true); AutomaticZenRule rule = new AutomaticZenRule( "name", new ComponentName("pkg", "cls"), Uri.parse("condition://id"), NotificationManager.INTERRUPTION_FILTER_PRIORITY, /* enabled= */ true); String id = notificationManager.addAutomaticZenRule(rule); shadowOf(notificationManager).setNotificationPolicyAccessGranted(false); shadowOf(notificationManager).setNotificationPolicyAccessGranted(true); assertThat(notificationManager.getAutomaticZenRule(id)).isNull(); assertThat(notificationManager.getAutomaticZenRules()).isEmpty(); }