blob: 9c7e0289248bf6daa302b58f86b279a851e3edd7 [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
19import static junit.framework.Assert.assertEquals;
Julia Reynolds68062072018-08-06 15:38:21 -040020import static junit.framework.TestCase.assertFalse;
21import static junit.framework.TestCase.assertNull;
Beverly174d7412018-08-22 16:34:41 -040022
23import android.app.NotificationManager.Policy;
Julia Reynolds68062072018-08-06 15:38:21 -040024import android.content.ComponentName;
Beverlycf0fbcec2018-09-07 16:52:16 -040025import android.net.Uri;
Julia Reynolds68062072018-08-06 15:38:21 -040026import android.provider.Settings;
27import android.service.notification.Condition;
Beverly174d7412018-08-22 16:34:41 -040028import android.service.notification.ZenModeConfig;
Beverlycf0fbcec2018-09-07 16:52:16 -040029import android.service.notification.ZenModeConfig.EventInfo;
Beverly174d7412018-08-22 16:34:41 -040030import android.service.notification.ZenPolicy;
31import android.support.test.runner.AndroidJUnit4;
32import android.test.suitebuilder.annotation.SmallTest;
Julia Reynolds68062072018-08-06 15:38:21 -040033import android.util.Xml;
Beverly174d7412018-08-22 16:34:41 -040034
Julia Reynolds68062072018-08-06 15:38:21 -040035import com.android.internal.util.FastXmlSerializer;
Beverly174d7412018-08-22 16:34:41 -040036import com.android.server.UiServiceTestCase;
37
38import org.junit.Test;
39import org.junit.runner.RunWith;
Julia Reynolds68062072018-08-06 15:38:21 -040040import org.xmlpull.v1.XmlPullParser;
41import org.xmlpull.v1.XmlSerializer;
42
43import java.io.BufferedInputStream;
44import java.io.BufferedOutputStream;
45import java.io.ByteArrayInputStream;
46import java.io.ByteArrayOutputStream;
Beverly174d7412018-08-22 16:34:41 -040047
48@SmallTest
49@RunWith(AndroidJUnit4.class)
50public 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() {
Beverly877d5192019-02-01 14:02:58 -050070 ZenModeConfig config = getCustomConfig();
Beverly174d7412018-08-22 16:34:41 -040071 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
Beverlycf0fbcec2018-09-07 16:52:16 -0400130 @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 Reynolds68062072018-08-06 15:38:21 -0400154 @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
Beverly174d7412018-08-22 16:34:41 -0400202 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
Beverly877d5192019-02-01 14:02:58 -0500222 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
Beverly174d7412018-08-22 16:34:41 -0400241 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}