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