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