Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 distriZenbuted 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 | |
Beverly | 85dfc2e | 2019-09-20 11:26:34 -0400 | [diff] [blame] | 19 | import static junit.framework.TestCase.assertEquals; |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 20 | import static junit.framework.TestCase.assertFalse; |
| 21 | import static junit.framework.TestCase.assertNull; |
Beverly | 85dfc2e | 2019-09-20 11:26:34 -0400 | [diff] [blame] | 22 | import static junit.framework.TestCase.assertTrue; |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 23 | |
| 24 | import android.app.NotificationManager.Policy; |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 25 | import android.content.ComponentName; |
Beverly | cf0fbcec | 2018-09-07 16:52:16 -0400 | [diff] [blame] | 26 | import android.net.Uri; |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 27 | import android.provider.Settings; |
| 28 | import android.service.notification.Condition; |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 29 | import android.service.notification.ZenModeConfig; |
Beverly | cf0fbcec | 2018-09-07 16:52:16 -0400 | [diff] [blame] | 30 | import android.service.notification.ZenModeConfig.EventInfo; |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 31 | import android.service.notification.ZenPolicy; |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 32 | import android.test.suitebuilder.annotation.SmallTest; |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 33 | import android.util.Xml; |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 34 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 35 | import androidx.test.runner.AndroidJUnit4; |
| 36 | |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 37 | import com.android.internal.util.FastXmlSerializer; |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 38 | import com.android.server.UiServiceTestCase; |
| 39 | |
| 40 | import org.junit.Test; |
| 41 | import org.junit.runner.RunWith; |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 42 | import org.xmlpull.v1.XmlPullParser; |
| 43 | import org.xmlpull.v1.XmlSerializer; |
| 44 | |
| 45 | import java.io.BufferedInputStream; |
| 46 | import java.io.BufferedOutputStream; |
| 47 | import java.io.ByteArrayInputStream; |
| 48 | import java.io.ByteArrayOutputStream; |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 49 | |
| 50 | @SmallTest |
| 51 | @RunWith(AndroidJUnit4.class) |
| 52 | public class ZenModeConfigTest extends UiServiceTestCase { |
| 53 | |
| 54 | @Test |
| 55 | public void testPriorityOnlyMutingAllNotifications() { |
Beverly | 85dfc2e | 2019-09-20 11:26:34 -0400 | [diff] [blame] | 56 | ZenModeConfig config = getMutedRingerConfig(); |
| 57 | assertTrue(ZenModeConfig.areAllPriorityOnlyRingerSoundsMuted(config)); |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 58 | |
| 59 | config.allowReminders = true; |
Beverly | 85dfc2e | 2019-09-20 11:26:34 -0400 | [diff] [blame] | 60 | assertFalse(ZenModeConfig.areAllPriorityOnlyRingerSoundsMuted(config)); |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 61 | config.allowReminders = false; |
| 62 | |
| 63 | config.areChannelsBypassingDnd = true; |
Beverly | 85dfc2e | 2019-09-20 11:26:34 -0400 | [diff] [blame] | 64 | assertFalse(ZenModeConfig.areAllPriorityOnlyRingerSoundsMuted(config)); |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 65 | config.areChannelsBypassingDnd = false; |
| 66 | |
Beverly | 85dfc2e | 2019-09-20 11:26:34 -0400 | [diff] [blame] | 67 | assertTrue(ZenModeConfig.areAllPriorityOnlyRingerSoundsMuted(config)); |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Test |
| 71 | public void testZenPolicyNothingSetToNotificationPolicy() { |
Beverly | 877d519 | 2019-02-01 14:02:58 -0500 | [diff] [blame] | 72 | ZenModeConfig config = getCustomConfig(); |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 73 | ZenPolicy zenPolicy = new ZenPolicy.Builder().build(); |
| 74 | assertEquals(config.toNotificationPolicy(), config.toNotificationPolicy(zenPolicy)); |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void testZenPolicyToNotificationPolicy() { |
| 79 | ZenModeConfig config = getMutedAllConfig(); |
| 80 | config.suppressedVisualEffects |= Policy.SUPPRESSED_EFFECT_BADGE; |
| 81 | |
| 82 | ZenPolicy zenPolicy = new ZenPolicy.Builder() |
| 83 | .allowAlarms(true) |
| 84 | .allowReminders(true) |
| 85 | .allowEvents(true) |
| 86 | .showLights(false) |
| 87 | .showInAmbientDisplay(false) |
| 88 | .build(); |
| 89 | |
| 90 | Policy originalPolicy = config.toNotificationPolicy(); |
| 91 | int priorityCategories = originalPolicy.priorityCategories; |
| 92 | int priorityCallSenders = originalPolicy.priorityCallSenders; |
| 93 | int priorityMessageSenders = originalPolicy.priorityMessageSenders; |
| 94 | int suppressedVisualEffects = originalPolicy.suppressedVisualEffects; |
| 95 | priorityCategories |= Policy.PRIORITY_CATEGORY_ALARMS; |
| 96 | priorityCategories |= Policy.PRIORITY_CATEGORY_REMINDERS; |
| 97 | priorityCategories |= Policy.PRIORITY_CATEGORY_EVENTS; |
| 98 | suppressedVisualEffects |= Policy.SUPPRESSED_EFFECT_LIGHTS; |
| 99 | suppressedVisualEffects |= Policy.SUPPRESSED_EFFECT_AMBIENT; |
| 100 | |
| 101 | Policy expectedPolicy = new Policy(priorityCategories, priorityCallSenders, |
Beverly | 4f8b022 | 2019-10-24 13:35:03 -0400 | [diff] [blame] | 102 | priorityMessageSenders, suppressedVisualEffects, 0); |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 103 | |
| 104 | assertEquals(expectedPolicy, config.toNotificationPolicy(zenPolicy)); |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | public void testPriorityOnlyMutingAll() { |
| 109 | ZenModeConfig config = getMutedAllConfig(); |
Beverly | 85dfc2e | 2019-09-20 11:26:34 -0400 | [diff] [blame] | 110 | assertTrue(ZenModeConfig.areAllPriorityOnlyRingerSoundsMuted(config)); |
| 111 | assertTrue(ZenModeConfig.areAllZenBehaviorSoundsMuted(config)); |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 112 | |
| 113 | config.allowReminders = true; |
Beverly | 85dfc2e | 2019-09-20 11:26:34 -0400 | [diff] [blame] | 114 | assertFalse(ZenModeConfig.areAllPriorityOnlyRingerSoundsMuted(config)); |
| 115 | assertFalse(ZenModeConfig.areAllZenBehaviorSoundsMuted(config)); |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 116 | config.allowReminders = false; |
| 117 | |
| 118 | config.areChannelsBypassingDnd = true; |
Beverly | 85dfc2e | 2019-09-20 11:26:34 -0400 | [diff] [blame] | 119 | assertFalse(ZenModeConfig.areAllPriorityOnlyRingerSoundsMuted(config)); |
| 120 | assertFalse(ZenModeConfig.areAllZenBehaviorSoundsMuted(config)); |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 121 | config.areChannelsBypassingDnd = false; |
| 122 | |
| 123 | config.allowAlarms = true; |
Beverly | 85dfc2e | 2019-09-20 11:26:34 -0400 | [diff] [blame] | 124 | assertTrue(ZenModeConfig.areAllPriorityOnlyRingerSoundsMuted(config)); |
| 125 | assertFalse(ZenModeConfig.areAllZenBehaviorSoundsMuted(config)); |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 126 | config.allowAlarms = false; |
| 127 | |
Beverly | 85dfc2e | 2019-09-20 11:26:34 -0400 | [diff] [blame] | 128 | assertTrue(ZenModeConfig.areAllPriorityOnlyRingerSoundsMuted(config)); |
| 129 | assertTrue(ZenModeConfig.areAllZenBehaviorSoundsMuted(config)); |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 130 | } |
| 131 | |
Beverly | cf0fbcec | 2018-09-07 16:52:16 -0400 | [diff] [blame] | 132 | @Test |
| 133 | public void testParseOldEvent() { |
| 134 | EventInfo oldEvent = new EventInfo(); |
| 135 | oldEvent.userId = 1; |
| 136 | oldEvent.calName = "calName"; |
| 137 | oldEvent.calendarId = null; // old events will have null ids |
| 138 | |
| 139 | Uri conditionId = ZenModeConfig.toEventConditionId(oldEvent); |
| 140 | EventInfo eventParsed = ZenModeConfig.tryParseEventConditionId(conditionId); |
| 141 | assertEquals(oldEvent, eventParsed); |
| 142 | } |
| 143 | |
| 144 | @Test |
| 145 | public void testParseNewEvent() { |
| 146 | EventInfo event = new EventInfo(); |
| 147 | event.userId = 1; |
| 148 | event.calName = "calName"; |
| 149 | event.calendarId = 12345L; |
| 150 | |
| 151 | Uri conditionId = ZenModeConfig.toEventConditionId(event); |
| 152 | EventInfo eventParsed = ZenModeConfig.tryParseEventConditionId(conditionId); |
| 153 | assertEquals(event, eventParsed); |
| 154 | } |
| 155 | |
Julia Reynolds | 6806207 | 2018-08-06 15:38:21 -0400 | [diff] [blame] | 156 | @Test |
| 157 | public void testRuleXml() throws Exception { |
| 158 | String tag = "tag"; |
| 159 | |
| 160 | ZenModeConfig.ZenRule rule = new ZenModeConfig.ZenRule(); |
| 161 | rule.configurationActivity = new ComponentName("a", "a"); |
| 162 | rule.component = new ComponentName("a", "b"); |
| 163 | rule.conditionId = new Uri.Builder().scheme("hello").build(); |
| 164 | rule.condition = new Condition(rule.conditionId, "", Condition.STATE_TRUE); |
| 165 | rule.enabled = true; |
| 166 | rule.creationTime = 123; |
| 167 | rule.id = "id"; |
| 168 | rule.zenMode = Settings.Global.ZEN_MODE_ALARMS; |
| 169 | rule.modified = true; |
| 170 | rule.name = "name"; |
| 171 | rule.snoozing = true; |
| 172 | |
| 173 | XmlSerializer out = new FastXmlSerializer(); |
| 174 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 175 | out.setOutput(new BufferedOutputStream(baos), "utf-8"); |
| 176 | out.startDocument(null, true); |
| 177 | out.startTag(null, tag); |
| 178 | ZenModeConfig.writeRuleXml(rule, out); |
| 179 | out.endTag(null, tag); |
| 180 | out.endDocument(); |
| 181 | |
| 182 | XmlPullParser parser = Xml.newPullParser(); |
| 183 | parser.setInput(new BufferedInputStream( |
| 184 | new ByteArrayInputStream(baos.toByteArray())), null); |
| 185 | parser.nextTag(); |
| 186 | ZenModeConfig.ZenRule fromXml = ZenModeConfig.readRuleXml(parser); |
| 187 | // read from backing component |
| 188 | assertEquals("a", fromXml.pkg); |
| 189 | // always resets on reboot |
| 190 | assertFalse(fromXml.snoozing); |
| 191 | //should all match original |
| 192 | assertEquals(rule.component, fromXml.component); |
| 193 | assertEquals(rule.configurationActivity, fromXml.configurationActivity); |
| 194 | assertNull(fromXml.enabler); |
| 195 | assertEquals(rule.condition, fromXml.condition); |
| 196 | assertEquals(rule.enabled, fromXml.enabled); |
| 197 | assertEquals(rule.creationTime, fromXml.creationTime); |
| 198 | assertEquals(rule.modified, fromXml.modified); |
| 199 | assertEquals(rule.conditionId, fromXml.conditionId); |
| 200 | assertEquals(rule.name, fromXml.name); |
| 201 | assertEquals(rule.zenMode, fromXml.zenMode); |
| 202 | } |
| 203 | |
Beverly | 85dfc2e | 2019-09-20 11:26:34 -0400 | [diff] [blame] | 204 | private ZenModeConfig getMutedRingerConfig() { |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 205 | ZenModeConfig config = new ZenModeConfig(); |
Beverly | 85dfc2e | 2019-09-20 11:26:34 -0400 | [diff] [blame] | 206 | // Allow alarms, media |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 207 | config.allowAlarms = true; |
| 208 | config.allowMedia = true; |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 209 | |
Beverly | 85dfc2e | 2019-09-20 11:26:34 -0400 | [diff] [blame] | 210 | // All sounds that respect the ringer are not allowed |
| 211 | config.allowSystem = false; |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 212 | config.allowCalls = false; |
| 213 | config.allowRepeatCallers = false; |
| 214 | config.allowMessages = false; |
| 215 | config.allowReminders = false; |
| 216 | config.allowEvents = false; |
| 217 | config.areChannelsBypassingDnd = false; |
| 218 | |
| 219 | config.suppressedVisualEffects = 0; |
| 220 | |
| 221 | return config; |
| 222 | } |
| 223 | |
Beverly | 877d519 | 2019-02-01 14:02:58 -0500 | [diff] [blame] | 224 | private ZenModeConfig getCustomConfig() { |
| 225 | ZenModeConfig config = new ZenModeConfig(); |
| 226 | // Some sounds allowed |
| 227 | config.allowAlarms = true; |
| 228 | config.allowMedia = false; |
| 229 | config.allowSystem = false; |
| 230 | config.allowCalls = true; |
| 231 | config.allowRepeatCallers = true; |
| 232 | config.allowMessages = false; |
| 233 | config.allowReminders = false; |
| 234 | config.allowEvents = false; |
| 235 | config.areChannelsBypassingDnd = false; |
| 236 | config.allowCallsFrom = ZenModeConfig.SOURCE_ANYONE; |
| 237 | config.allowMessagesFrom = ZenModeConfig.SOURCE_ANYONE; |
| 238 | |
| 239 | config.suppressedVisualEffects = 0; |
| 240 | return config; |
| 241 | } |
| 242 | |
Beverly | 174d741 | 2018-08-22 16:34:41 -0400 | [diff] [blame] | 243 | private ZenModeConfig getMutedAllConfig() { |
| 244 | ZenModeConfig config = new ZenModeConfig(); |
| 245 | // No sounds allowed |
| 246 | config.allowAlarms = false; |
| 247 | config.allowMedia = false; |
| 248 | config.allowSystem = false; |
| 249 | config.allowCalls = false; |
| 250 | config.allowRepeatCallers = false; |
| 251 | config.allowMessages = false; |
| 252 | config.allowReminders = false; |
| 253 | config.allowEvents = false; |
| 254 | config.areChannelsBypassingDnd = false; |
| 255 | |
| 256 | config.suppressedVisualEffects = 0; |
| 257 | return config; |
| 258 | } |
| 259 | } |