blob: a0ee417c8b47399544717313f819f73b3c541775 [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;
20
21import android.service.notification.ZenPolicy;
Beverly174d7412018-08-22 16:34:41 -040022import android.test.suitebuilder.annotation.SmallTest;
23
Brett Chabot84151d92019-02-27 15:37:59 -080024import androidx.test.runner.AndroidJUnit4;
25
Beverly174d7412018-08-22 16:34:41 -040026import com.android.server.UiServiceTestCase;
27
28import org.junit.Test;
29import org.junit.runner.RunWith;
Beverly174d7412018-08-22 16:34:41 -040030
31@SmallTest
32@RunWith(AndroidJUnit4.class)
33public class ZenPolicyTest extends UiServiceTestCase {
34
35 @Test
36 public void testZenPolicyApplyAllowedToDisallowed() {
37 ZenPolicy.Builder builder = new ZenPolicy.Builder();
38
39 // reminders are disallowed
40 builder.allowReminders(false);
41 ZenPolicy remindersDisallowed = builder.build();
42 assertEquals(ZenPolicy.STATE_DISALLOW,
43 remindersDisallowed.getPriorityCategoryReminders());
44
45 // reminders are allowed
46 builder.allowReminders(true);
47 ZenPolicy remindersAllowed = builder.build();
48 assertEquals(ZenPolicy.STATE_ALLOW,
49 remindersAllowed.getPriorityCategoryReminders());
50 assertEquals(ZenPolicy.STATE_DISALLOW,
51 remindersDisallowed.getPriorityCategoryReminders());
52
53 // we apply reminders allowed to reminders disallowed
54 // -> reminders should remain disallowed
55 remindersDisallowed.apply(remindersAllowed);
56 assertEquals(ZenPolicy.STATE_DISALLOW,
57 remindersDisallowed.getPriorityCategoryReminders());
58 }
59
60 @Test
61 public void testZenPolicyApplyAllowedToUnset() {
62 ZenPolicy.Builder builder = new ZenPolicy.Builder();
63
64 // reminders are unset
65 ZenPolicy remindersUnset = builder.build();
66
67 // reminders are allowed
68 builder.allowReminders(true);
69 ZenPolicy remindersAllowed = builder.build();
70
71 // we apply reminders allowed to reminders unset
72 // -> reminders should be allowed
73 remindersUnset.apply(remindersAllowed);
74 assertEquals(ZenPolicy.STATE_ALLOW, remindersUnset.getPriorityCategoryReminders());
75 }
76
77 @Test
78 public void testZenPolicyApplyDisallowedToUnset() {
79 ZenPolicy.Builder builder = new ZenPolicy.Builder();
80
81 // reminders are unset
82 ZenPolicy remindersUnset = builder.build();
83
84 // reminders are disallowed
85 builder.allowReminders(false);
86 ZenPolicy remindersDisallowed = builder.build();
87
88 // we apply reminders disallowed to reminders unset
89 // -> reminders should remain disallowed
90 remindersUnset.apply(remindersDisallowed);
91 assertEquals(ZenPolicy.STATE_DISALLOW,
92 remindersUnset.getPriorityCategoryReminders());
93 }
94
95 @Test
96 public void testZenPolicyApplyDisallowedToAllowed() {
97 ZenPolicy.Builder builder = new ZenPolicy.Builder();
98
99 // reminders are allowed
100 builder.allowReminders(true);
101 ZenPolicy remindersAllowed = builder.build();
102
103 // reminders are disallowed
104 builder.allowReminders(false);
105 ZenPolicy remindersDisallowed = builder.build();
106
107 // we apply reminders allowed to reminders disallowed
108 // -> reminders should change to disallowed
109 remindersAllowed.apply(remindersDisallowed);
110 assertEquals(ZenPolicy.STATE_DISALLOW, remindersAllowed.getPriorityCategoryReminders());
111 }
112
113 @Test
114 public void testZenPolicyApplyUnsetToAllowed() {
115 ZenPolicy.Builder builder = new ZenPolicy.Builder();
116
117 // reminders are allowed
118 builder.allowReminders(true);
119 ZenPolicy remindersAllowed = builder.build();
120
121 // reminders are unset
122 ZenPolicy.Builder builder2 = new ZenPolicy.Builder();
123 ZenPolicy remindersUnset = builder2.build();
124
125 // we apply reminders allowed to reminders unset
126 // -> reminders should remain allowed
127 remindersAllowed.apply(remindersUnset);
128 assertEquals(ZenPolicy.STATE_ALLOW, remindersAllowed.getPriorityCategoryReminders());
129 }
130
131 @Test
132 public void testZenPolicyApplyMoreSevereCallSenders() {
133 ZenPolicy.Builder builder = new ZenPolicy.Builder();
134
135 // calls from contacts allowed
136 builder.allowCalls(ZenPolicy.PEOPLE_TYPE_CONTACTS);
137 ZenPolicy contactsAllowed = builder.build();
138
139 // calls from starred contacts allowed
140 builder.allowCalls(ZenPolicy.PEOPLE_TYPE_STARRED);
141 ZenPolicy starredAllowed = builder.build();
142
143 // we apply starredAllowed to contactsAllowed -> starred contacts allowed (more restrictive)
144 contactsAllowed.apply(starredAllowed);
145 assertEquals(ZenPolicy.STATE_ALLOW, contactsAllowed.getPriorityCategoryCalls());
146 assertEquals(ZenPolicy.PEOPLE_TYPE_STARRED, contactsAllowed.getPriorityCallSenders());
147 }
148
149 @Test
150 public void testZenPolicyApplyLessSevereCallSenders() {
151 ZenPolicy.Builder builder = new ZenPolicy.Builder();
152
153 // calls from contacts allowed
154 builder.allowCalls(ZenPolicy.PEOPLE_TYPE_CONTACTS);
155 ZenPolicy contactsAllowed = builder.build();
156
157 // calls from anyone allowed
158 builder.allowCalls(ZenPolicy.PEOPLE_TYPE_ANYONE);
159 ZenPolicy anyoneAllowed = builder.build();
160
161 // we apply anyoneAllowed to contactsAllowed -> contactsAllowed (more restrictive)
162 contactsAllowed.apply(anyoneAllowed);
163 assertEquals(ZenPolicy.STATE_ALLOW, contactsAllowed.getPriorityCategoryCalls());
164 assertEquals(ZenPolicy.PEOPLE_TYPE_CONTACTS, contactsAllowed.getPriorityCallSenders());
165 }
166
167 @Test
168 public void testZenPolicyApplyMoreSevereMessageSenders() {
169 ZenPolicy.Builder builder = new ZenPolicy.Builder();
170
171 // messsages from contacts allowed
172 builder.allowMessages(ZenPolicy.PEOPLE_TYPE_CONTACTS);
173 ZenPolicy contactsAllowed = builder.build();
174
175 // messsages from no one allowed
176 builder.allowMessages(ZenPolicy.PEOPLE_TYPE_NONE);
177 ZenPolicy noneAllowed = builder.build();
178
179 // noneAllowed to contactsAllowed -> no messages allowed (more restrictive)
180 contactsAllowed.apply(noneAllowed);
181 assertEquals(ZenPolicy.STATE_DISALLOW, contactsAllowed.getPriorityCategoryMessages());
182 assertEquals(ZenPolicy.PEOPLE_TYPE_NONE, contactsAllowed.getPriorityMessageSenders());
183 }
184
185 @Test
186 public void testZenPolicyMessagesInvalid() {
187 ZenPolicy.Builder builder = new ZenPolicy.Builder();
188
189 builder.allowMessages(20); // invalid #, won't change policy
190 ZenPolicy policy = builder.build();
191 assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategoryMessages());
192 assertEquals(ZenPolicy.PEOPLE_TYPE_UNSET, policy.getPriorityMessageSenders());
193 }
194
195 @Test
196 public void testZenPolicyCallsInvalid() {
197 ZenPolicy.Builder builder = new ZenPolicy.Builder();
198
199 builder.allowCalls(ZenPolicy.PEOPLE_TYPE_ANYONE);
200 builder.allowCalls(20); // invalid #, won't change policy
201 ZenPolicy policy = builder.build();
202 assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryCalls());
203 assertEquals(ZenPolicy.PEOPLE_TYPE_ANYONE, policy.getPriorityCallSenders());
204 }
205
206 @Test
207 public void testEmptyZenPolicy() {
208 ZenPolicy.Builder builder = new ZenPolicy.Builder();
209
210 ZenPolicy policy = builder.build();
211 assertAllPriorityCategoriesUnsetExcept(policy, -1);
212 assertAllVisualEffectsUnsetExcept(policy, -1);
213 }
214
215 @Test
216 public void testAllowReminders() {
217 ZenPolicy.Builder builder = new ZenPolicy.Builder();
218
219 builder.allowReminders(true);
220 ZenPolicy policy = builder.build();
221 assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_REMINDERS);
222 assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryReminders());
223 assertAllVisualEffectsUnsetExcept(policy, -1);
224
225 builder.allowReminders(false);
226 policy = builder.build();
227 assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_REMINDERS);
228 assertEquals(ZenPolicy.STATE_DISALLOW, policy.getPriorityCategoryReminders());
229 assertAllVisualEffectsUnsetExcept(policy, -1);
230 }
231
232 @Test
233 public void testAllowEvents() {
234 ZenPolicy.Builder builder = new ZenPolicy.Builder();
235
236 builder.allowEvents(true);
237 ZenPolicy policy = builder.build();
238 assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_EVENTS);
239 assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryEvents());
240 assertAllVisualEffectsUnsetExcept(policy, -1);
241
242 builder.allowEvents(false);
243 policy = builder.build();
244 assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_EVENTS);
245 assertEquals(ZenPolicy.STATE_DISALLOW, policy.getPriorityCategoryEvents());
246 assertAllVisualEffectsUnsetExcept(policy, -1);
247 }
248
249 @Test
250 public void testAllowMessages() {
251 ZenPolicy.Builder builder = new ZenPolicy.Builder();
252
253 builder.allowMessages(ZenPolicy.PEOPLE_TYPE_ANYONE);
254 ZenPolicy policy = builder.build();
255 assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_MESSAGES);
256 assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryMessages());
257 assertEquals(ZenPolicy.PEOPLE_TYPE_ANYONE, policy.getPriorityMessageSenders());
258 assertAllVisualEffectsUnsetExcept(policy, -1);
259
260 builder.allowMessages(ZenPolicy.PEOPLE_TYPE_CONTACTS);
261 policy = builder.build();
262 assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_MESSAGES);
263 assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryMessages());
264 assertEquals(ZenPolicy.PEOPLE_TYPE_CONTACTS, policy.getPriorityMessageSenders());
265 assertAllVisualEffectsUnsetExcept(policy, -1);
266
267 builder.allowMessages(ZenPolicy.PEOPLE_TYPE_STARRED);
268 policy = builder.build();
269 assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_MESSAGES);
270 assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryMessages());
271 assertEquals(ZenPolicy.PEOPLE_TYPE_STARRED, policy.getPriorityMessageSenders());
272 assertAllVisualEffectsUnsetExcept(policy, -1);
273
274 builder.allowMessages(ZenPolicy.PEOPLE_TYPE_NONE);
275 policy = builder.build();
276 assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_MESSAGES);
277 assertEquals(ZenPolicy.STATE_DISALLOW, policy.getPriorityCategoryMessages());
278 assertEquals(ZenPolicy.PEOPLE_TYPE_NONE, policy.getPriorityMessageSenders());
279 assertAllVisualEffectsUnsetExcept(policy, -1);
280
281 builder.allowMessages(ZenPolicy.PEOPLE_TYPE_UNSET);
282 policy = builder.build();
283 assertAllPriorityCategoriesUnsetExcept(policy, -1);
284 assertAllVisualEffectsUnsetExcept(policy, -1);
285 }
286
287 @Test
288 public void testAllowCalls() {
289 ZenPolicy.Builder builder = new ZenPolicy.Builder();
290
291 builder.allowCalls(ZenPolicy.PEOPLE_TYPE_ANYONE);
292 ZenPolicy policy = builder.build();
293 assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_CALLS);
294 assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryCalls());
295 assertEquals(ZenPolicy.PEOPLE_TYPE_ANYONE, policy.getPriorityCallSenders());
296 assertAllVisualEffectsUnsetExcept(policy, -1);
297
298 builder.allowCalls(ZenPolicy.PEOPLE_TYPE_CONTACTS);
299 policy = builder.build();
300 assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_CALLS);
301 assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryCalls());
302 assertEquals(ZenPolicy.PEOPLE_TYPE_CONTACTS, policy.getPriorityCallSenders());
303 assertAllVisualEffectsUnsetExcept(policy, -1);
304
305 builder.allowCalls(ZenPolicy.PEOPLE_TYPE_STARRED);
306 policy = builder.build();
307 assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_CALLS);
308 assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryCalls());
309 assertEquals(ZenPolicy.PEOPLE_TYPE_STARRED, policy.getPriorityCallSenders());
310 assertAllVisualEffectsUnsetExcept(policy, -1);
311
312 builder.allowCalls(ZenPolicy.PEOPLE_TYPE_NONE);
313 policy = builder.build();
314 assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_CALLS);
315 assertEquals(ZenPolicy.STATE_DISALLOW, policy.getPriorityCategoryCalls());
316 assertEquals(ZenPolicy.PEOPLE_TYPE_NONE, policy.getPriorityCallSenders());
317 assertAllVisualEffectsUnsetExcept(policy, -1);
318
319 builder.allowCalls(ZenPolicy.PEOPLE_TYPE_UNSET);
320 policy = builder.build();
321 assertAllPriorityCategoriesUnsetExcept(policy, -1);
322 assertAllVisualEffectsUnsetExcept(policy, -1);
323 }
324
325 @Test
326 public void testAllowRepeatCallers() {
327 ZenPolicy.Builder builder = new ZenPolicy.Builder();
328
329 builder.allowRepeatCallers(true);
330 ZenPolicy policy = builder.build();
331 assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_REPEAT_CALLERS);
332 assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryRepeatCallers());
333 assertAllVisualEffectsUnsetExcept(policy, -1);
334
335 builder.allowRepeatCallers(false);
336 policy = builder.build();
337 assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_REPEAT_CALLERS);
338 assertEquals(ZenPolicy.STATE_DISALLOW, policy.getPriorityCategoryRepeatCallers());
339 assertAllVisualEffectsUnsetExcept(policy, -1);
340 }
341
342 @Test
343 public void testAllowAlarms() {
344 ZenPolicy.Builder builder = new ZenPolicy.Builder();
345
346 builder.allowAlarms(true);
347 ZenPolicy policy = builder.build();
348 assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_ALARMS);
349 assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryAlarms());
350 assertAllVisualEffectsUnsetExcept(policy, -1);
351
352 builder.allowAlarms(false);
353 policy = builder.build();
354 assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_ALARMS);
355 assertEquals(ZenPolicy.STATE_DISALLOW, policy.getPriorityCategoryAlarms());
356 assertAllVisualEffectsUnsetExcept(policy, -1);
357 }
358
359 @Test
360 public void testAllowMedia() {
361 ZenPolicy.Builder builder = new ZenPolicy.Builder();
362
363 builder.allowMedia(true);
364 ZenPolicy policy = builder.build();
365 assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_MEDIA);
366 assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryMedia());
367 assertAllVisualEffectsUnsetExcept(policy, -1);
368
369 builder.allowMedia(false);
370 policy = builder.build();
371 assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_MEDIA);
372 assertEquals(ZenPolicy.STATE_DISALLOW, policy.getPriorityCategoryMedia());
373 assertAllVisualEffectsUnsetExcept(policy, -1);
374 }
375
376 @Test
377 public void testAllowSystem() {
378 ZenPolicy.Builder builder = new ZenPolicy.Builder();
379
380 builder.allowSystem(true);
381 ZenPolicy policy = builder.build();
382 assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_SYSTEM);
383 assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategorySystem());
384 assertAllVisualEffectsUnsetExcept(policy, -1);
385
386 builder.allowSystem(false);
387 policy = builder.build();
388 assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_SYSTEM);
389 assertEquals(ZenPolicy.STATE_DISALLOW, policy.getPriorityCategorySystem());
390 assertAllVisualEffectsUnsetExcept(policy, -1);
391 }
392
393 private void assertAllPriorityCategoriesUnsetExcept(ZenPolicy policy, int except) {
394 if (except != ZenPolicy.PRIORITY_CATEGORY_REMINDERS) {
395 assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategoryReminders());
396 }
397
398 if (except != ZenPolicy.PRIORITY_CATEGORY_EVENTS) {
399 assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategoryEvents());
400 }
401
402 if (except != ZenPolicy.PRIORITY_CATEGORY_MESSAGES) {
403 assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategoryMessages());
404 }
405
406 if (except != ZenPolicy.PRIORITY_CATEGORY_CALLS) {
407 assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategoryCalls());
408 }
409
410 if (except != ZenPolicy.PRIORITY_CATEGORY_REPEAT_CALLERS) {
411 assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategoryRepeatCallers());
412 }
413
414 if (except != ZenPolicy.PRIORITY_CATEGORY_ALARMS) {
415 assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategoryAlarms());
416 }
417
418 if (except != ZenPolicy.PRIORITY_CATEGORY_MEDIA) {
419 assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategoryMedia());
420 }
421
422 if (except != ZenPolicy.PRIORITY_CATEGORY_SYSTEM) {
423 assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategorySystem());
424 }
425 }
426
427 private void assertAllVisualEffectsUnsetExcept(ZenPolicy policy, int except) {
428 if (except != ZenPolicy.VISUAL_EFFECT_FULL_SCREEN_INTENT) {
429 assertEquals(ZenPolicy.STATE_UNSET, policy.getVisualEffectFullScreenIntent());
430 }
431
432 if (except != ZenPolicy.VISUAL_EFFECT_LIGHTS) {
433 assertEquals(ZenPolicy.STATE_UNSET, policy.getVisualEffectLights());
434 }
435
436 if (except != ZenPolicy.VISUAL_EFFECT_PEEK) {
437 assertEquals(ZenPolicy.STATE_UNSET, policy.getVisualEffectPeek());
438 }
439
440 if (except != ZenPolicy.VISUAL_EFFECT_STATUS_BAR) {
441 assertEquals(ZenPolicy.STATE_UNSET, policy.getVisualEffectStatusBar());
442 }
443
444 if (except != ZenPolicy.VISUAL_EFFECT_BADGE) {
445 assertEquals(ZenPolicy.STATE_UNSET, policy.getVisualEffectBadge());
446 }
447
448 if (except != ZenPolicy.VISUAL_EFFECT_AMBIENT) {
449 assertEquals(ZenPolicy.STATE_UNSET, policy.getVisualEffectAmbient());
450 }
451
452 if (except != ZenPolicy.VISUAL_EFFECT_NOTIFICATION_LIST) {
453 assertEquals(ZenPolicy.STATE_UNSET, policy.getVisualEffectNotificationList());
454 }
455 }
456}