blob: b654764707738debe6ed13c6cd830a8bad9e634f [file] [log] [blame]
Beverly174d7412018-08-22 16:34:41 -04001/*
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
17package com.android.server.notification;
18
Beverly85dfc2e2019-09-20 11:26:34 -040019import static junit.framework.TestCase.assertEquals;
Julia Reynolds68062072018-08-06 15:38:21 -040020import static junit.framework.TestCase.assertFalse;
21import static junit.framework.TestCase.assertNull;
Beverly85dfc2e2019-09-20 11:26:34 -040022import static junit.framework.TestCase.assertTrue;
Beverly174d7412018-08-22 16:34:41 -040023
24import android.app.NotificationManager.Policy;
Julia Reynolds68062072018-08-06 15:38:21 -040025import android.content.ComponentName;
Beverlycf0fbcec2018-09-07 16:52:16 -040026import android.net.Uri;
Julia Reynolds68062072018-08-06 15:38:21 -040027import android.provider.Settings;
28import android.service.notification.Condition;
Beverly174d7412018-08-22 16:34:41 -040029import android.service.notification.ZenModeConfig;
Beverlycf0fbcec2018-09-07 16:52:16 -040030import android.service.notification.ZenModeConfig.EventInfo;
Beverly174d7412018-08-22 16:34:41 -040031import android.service.notification.ZenPolicy;
Beverly174d7412018-08-22 16:34:41 -040032import android.test.suitebuilder.annotation.SmallTest;
Julia Reynolds68062072018-08-06 15:38:21 -040033import android.util.Xml;
Beverly174d7412018-08-22 16:34:41 -040034
Brett Chabot84151d92019-02-27 15:37:59 -080035import androidx.test.runner.AndroidJUnit4;
36
Julia Reynolds68062072018-08-06 15:38:21 -040037import com.android.internal.util.FastXmlSerializer;
Beverly174d7412018-08-22 16:34:41 -040038import com.android.server.UiServiceTestCase;
39
40import org.junit.Test;
41import org.junit.runner.RunWith;
Julia Reynolds68062072018-08-06 15:38:21 -040042import org.xmlpull.v1.XmlPullParser;
43import org.xmlpull.v1.XmlSerializer;
44
45import java.io.BufferedInputStream;
46import java.io.BufferedOutputStream;
47import java.io.ByteArrayInputStream;
48import java.io.ByteArrayOutputStream;
Beverly174d7412018-08-22 16:34:41 -040049
50@SmallTest
51@RunWith(AndroidJUnit4.class)
52public class ZenModeConfigTest extends UiServiceTestCase {
53
54 @Test
55 public void testPriorityOnlyMutingAllNotifications() {
Beverly85dfc2e2019-09-20 11:26:34 -040056 ZenModeConfig config = getMutedRingerConfig();
57 assertTrue(ZenModeConfig.areAllPriorityOnlyRingerSoundsMuted(config));
Beverly174d7412018-08-22 16:34:41 -040058
59 config.allowReminders = true;
Beverly85dfc2e2019-09-20 11:26:34 -040060 assertFalse(ZenModeConfig.areAllPriorityOnlyRingerSoundsMuted(config));
Beverly174d7412018-08-22 16:34:41 -040061 config.allowReminders = false;
62
63 config.areChannelsBypassingDnd = true;
Beverly85dfc2e2019-09-20 11:26:34 -040064 assertFalse(ZenModeConfig.areAllPriorityOnlyRingerSoundsMuted(config));
Beverly174d7412018-08-22 16:34:41 -040065 config.areChannelsBypassingDnd = false;
66
Beverly85dfc2e2019-09-20 11:26:34 -040067 assertTrue(ZenModeConfig.areAllPriorityOnlyRingerSoundsMuted(config));
Beverly174d7412018-08-22 16:34:41 -040068 }
69
70 @Test
71 public void testZenPolicyNothingSetToNotificationPolicy() {
Beverly877d5192019-02-01 14:02:58 -050072 ZenModeConfig config = getCustomConfig();
Beverly174d7412018-08-22 16:34:41 -040073 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,
Beverly4f8b0222019-10-24 13:35:03 -0400102 priorityMessageSenders, suppressedVisualEffects, 0);
Beverly174d7412018-08-22 16:34:41 -0400103
104 assertEquals(expectedPolicy, config.toNotificationPolicy(zenPolicy));
105 }
106
107 @Test
108 public void testPriorityOnlyMutingAll() {
109 ZenModeConfig config = getMutedAllConfig();
Beverly85dfc2e2019-09-20 11:26:34 -0400110 assertTrue(ZenModeConfig.areAllPriorityOnlyRingerSoundsMuted(config));
111 assertTrue(ZenModeConfig.areAllZenBehaviorSoundsMuted(config));
Beverly174d7412018-08-22 16:34:41 -0400112
113 config.allowReminders = true;
Beverly85dfc2e2019-09-20 11:26:34 -0400114 assertFalse(ZenModeConfig.areAllPriorityOnlyRingerSoundsMuted(config));
115 assertFalse(ZenModeConfig.areAllZenBehaviorSoundsMuted(config));
Beverly174d7412018-08-22 16:34:41 -0400116 config.allowReminders = false;
117
118 config.areChannelsBypassingDnd = true;
Beverly85dfc2e2019-09-20 11:26:34 -0400119 assertFalse(ZenModeConfig.areAllPriorityOnlyRingerSoundsMuted(config));
120 assertFalse(ZenModeConfig.areAllZenBehaviorSoundsMuted(config));
Beverly174d7412018-08-22 16:34:41 -0400121 config.areChannelsBypassingDnd = false;
122
123 config.allowAlarms = true;
Beverly85dfc2e2019-09-20 11:26:34 -0400124 assertTrue(ZenModeConfig.areAllPriorityOnlyRingerSoundsMuted(config));
125 assertFalse(ZenModeConfig.areAllZenBehaviorSoundsMuted(config));
Beverly174d7412018-08-22 16:34:41 -0400126 config.allowAlarms = false;
127
Beverly85dfc2e2019-09-20 11:26:34 -0400128 assertTrue(ZenModeConfig.areAllPriorityOnlyRingerSoundsMuted(config));
129 assertTrue(ZenModeConfig.areAllZenBehaviorSoundsMuted(config));
Beverly174d7412018-08-22 16:34:41 -0400130 }
131
Beverlycf0fbcec2018-09-07 16:52:16 -0400132 @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 Reynolds68062072018-08-06 15:38:21 -0400156 @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
Beverly85dfc2e2019-09-20 11:26:34 -0400204 private ZenModeConfig getMutedRingerConfig() {
Beverly174d7412018-08-22 16:34:41 -0400205 ZenModeConfig config = new ZenModeConfig();
Beverly85dfc2e2019-09-20 11:26:34 -0400206 // Allow alarms, media
Beverly174d7412018-08-22 16:34:41 -0400207 config.allowAlarms = true;
208 config.allowMedia = true;
Beverly174d7412018-08-22 16:34:41 -0400209
Beverly85dfc2e2019-09-20 11:26:34 -0400210 // All sounds that respect the ringer are not allowed
211 config.allowSystem = false;
Beverly174d7412018-08-22 16:34:41 -0400212 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
Beverly877d5192019-02-01 14:02:58 -0500224 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
Beverly174d7412018-08-22 16:34:41 -0400243 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}