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 com.google.common.truth.Truth.assertThat;
20 
21 import static junit.framework.Assert.assertEquals;
22 import static junit.framework.Assert.fail;
23 
24 import android.app.Flags;
25 import android.os.Parcel;
26 import android.platform.test.flag.junit.SetFlagsRule;
27 import android.service.notification.ZenPolicy;
28 import android.service.notification.nano.DNDPolicyProto;
29 
30 import androidx.test.filters.SmallTest;
31 import androidx.test.runner.AndroidJUnit4;
32 
33 import com.android.server.UiServiceTestCase;
34 
35 import com.google.protobuf.nano.InvalidProtocolBufferNanoException;
36 
37 import org.junit.Before;
38 import org.junit.Rule;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 
42 import java.lang.reflect.Field;
43 import java.util.ArrayList;
44 
45 @SmallTest
46 @RunWith(AndroidJUnit4.class)
47 public class ZenPolicyTest extends UiServiceTestCase {
48     private static final String CLASS = "android.service.notification.ZenPolicy";
49 
50     @Rule
51     public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();
52 
53     @Before
setUp()54     public final void setUp() {
55         mSetFlagsRule.enableFlags(Flags.FLAG_MODES_API);
56     }
57 
58     @Test
testZenPolicyApplyAllowedToDisallowed()59     public void testZenPolicyApplyAllowedToDisallowed() {
60         ZenPolicy.Builder builder = new ZenPolicy.Builder();
61 
62         // reminders are disallowed
63         builder.allowReminders(false);
64         ZenPolicy remindersDisallowed = builder.build();
65         assertEquals(ZenPolicy.STATE_DISALLOW,
66                 remindersDisallowed.getPriorityCategoryReminders());
67 
68         // reminders are allowed
69         builder.allowReminders(true);
70         ZenPolicy remindersAllowed = builder.build();
71         assertEquals(ZenPolicy.STATE_ALLOW,
72                 remindersAllowed.getPriorityCategoryReminders());
73         assertEquals(ZenPolicy.STATE_DISALLOW,
74                 remindersDisallowed.getPriorityCategoryReminders());
75 
76         // we apply reminders allowed to reminders disallowed
77         // -> reminders should remain disallowed
78         remindersDisallowed.apply(remindersAllowed);
79         assertEquals(ZenPolicy.STATE_DISALLOW,
80                 remindersDisallowed.getPriorityCategoryReminders());
81     }
82 
83     @Test
testZenPolicyApplyAllowedToUnset()84     public void testZenPolicyApplyAllowedToUnset() {
85         ZenPolicy.Builder builder = new ZenPolicy.Builder();
86 
87         // reminders are unset
88         ZenPolicy remindersUnset = builder.build();
89 
90         // reminders are allowed
91         builder.allowReminders(true);
92         ZenPolicy remindersAllowed = builder.build();
93 
94         // we apply reminders allowed to reminders unset
95         // -> reminders should be allowed
96         remindersUnset.apply(remindersAllowed);
97         assertEquals(ZenPolicy.STATE_ALLOW, remindersUnset.getPriorityCategoryReminders());
98     }
99 
100     @Test
testZenPolicyApplyDisallowedToUnset()101     public void testZenPolicyApplyDisallowedToUnset() {
102         ZenPolicy.Builder builder = new ZenPolicy.Builder();
103 
104         // reminders are unset
105         ZenPolicy remindersUnset = builder.build();
106 
107         // reminders are disallowed
108         builder.allowReminders(false);
109         ZenPolicy remindersDisallowed = builder.build();
110 
111         // we apply reminders disallowed to reminders unset
112         // -> reminders should remain disallowed
113         remindersUnset.apply(remindersDisallowed);
114         assertEquals(ZenPolicy.STATE_DISALLOW,
115                 remindersUnset.getPriorityCategoryReminders());
116     }
117 
118     @Test
testZenPolicyApplyDisallowedToAllowed()119     public void testZenPolicyApplyDisallowedToAllowed() {
120         ZenPolicy.Builder builder = new ZenPolicy.Builder();
121 
122         // reminders are allowed
123         builder.allowReminders(true);
124         ZenPolicy remindersAllowed = builder.build();
125 
126         // reminders are disallowed
127         builder.allowReminders(false);
128         ZenPolicy remindersDisallowed = builder.build();
129 
130         // we apply reminders allowed to reminders disallowed
131         // -> reminders should change to disallowed
132         remindersAllowed.apply(remindersDisallowed);
133         assertEquals(ZenPolicy.STATE_DISALLOW, remindersAllowed.getPriorityCategoryReminders());
134     }
135 
136     @Test
testZenPolicyApplyUnsetToAllowed()137     public void testZenPolicyApplyUnsetToAllowed() {
138         ZenPolicy.Builder builder = new ZenPolicy.Builder();
139 
140         // reminders are allowed
141         builder.allowReminders(true);
142         ZenPolicy remindersAllowed = builder.build();
143 
144         // reminders are unset
145         ZenPolicy.Builder builder2 = new ZenPolicy.Builder();
146         ZenPolicy remindersUnset = builder2.build();
147 
148         // we apply reminders allowed to reminders unset
149         // -> reminders should remain allowed
150         remindersAllowed.apply(remindersUnset);
151         assertEquals(ZenPolicy.STATE_ALLOW, remindersAllowed.getPriorityCategoryReminders());
152     }
153 
154     @Test
testZenPolicyApplyMoreSevereCallSenders()155     public void testZenPolicyApplyMoreSevereCallSenders() {
156         ZenPolicy.Builder builder = new ZenPolicy.Builder();
157 
158         // calls from contacts allowed
159         builder.allowCalls(ZenPolicy.PEOPLE_TYPE_CONTACTS);
160         ZenPolicy contactsAllowed = builder.build();
161 
162         // calls from starred contacts allowed
163         builder.allowCalls(ZenPolicy.PEOPLE_TYPE_STARRED);
164         ZenPolicy starredAllowed = builder.build();
165 
166         // we apply starredAllowed to contactsAllowed -> starred contacts allowed (more restrictive)
167         contactsAllowed.apply(starredAllowed);
168         assertEquals(ZenPolicy.STATE_ALLOW, contactsAllowed.getPriorityCategoryCalls());
169         assertEquals(ZenPolicy.PEOPLE_TYPE_STARRED, contactsAllowed.getPriorityCallSenders());
170     }
171 
172     @Test
testZenPolicyApplyLessSevereCallSenders()173     public void testZenPolicyApplyLessSevereCallSenders() {
174         ZenPolicy.Builder builder = new ZenPolicy.Builder();
175 
176         // calls from contacts allowed
177         builder.allowCalls(ZenPolicy.PEOPLE_TYPE_CONTACTS);
178         ZenPolicy contactsAllowed = builder.build();
179 
180         // calls from anyone allowed
181         builder.allowCalls(ZenPolicy.PEOPLE_TYPE_ANYONE);
182         ZenPolicy anyoneAllowed = builder.build();
183 
184         // we apply anyoneAllowed to contactsAllowed -> contactsAllowed (more restrictive)
185         contactsAllowed.apply(anyoneAllowed);
186         assertEquals(ZenPolicy.STATE_ALLOW, contactsAllowed.getPriorityCategoryCalls());
187         assertEquals(ZenPolicy.PEOPLE_TYPE_CONTACTS, contactsAllowed.getPriorityCallSenders());
188     }
189 
190     @Test
testZenPolicyApplyMoreSevereMessageSenders()191     public void testZenPolicyApplyMoreSevereMessageSenders() {
192         ZenPolicy.Builder builder = new ZenPolicy.Builder();
193 
194         // messsages from contacts allowed
195         builder.allowMessages(ZenPolicy.PEOPLE_TYPE_CONTACTS);
196         ZenPolicy contactsAllowed = builder.build();
197 
198         // messsages from no one allowed
199         builder.allowMessages(ZenPolicy.PEOPLE_TYPE_NONE);
200         ZenPolicy noneAllowed = builder.build();
201 
202         // noneAllowed to contactsAllowed -> no messages allowed (more restrictive)
203         contactsAllowed.apply(noneAllowed);
204         assertEquals(ZenPolicy.STATE_DISALLOW, contactsAllowed.getPriorityCategoryMessages());
205         assertEquals(ZenPolicy.PEOPLE_TYPE_NONE, contactsAllowed.getPriorityMessageSenders());
206     }
207 
208     @Test
testZenPolicyApplyChannels_applyUnset()209     public void testZenPolicyApplyChannels_applyUnset() {
210         mSetFlagsRule.enableFlags(Flags.FLAG_MODES_API);
211 
212         ZenPolicy.Builder builder = new ZenPolicy.Builder();
213         ZenPolicy unset = builder.build();
214 
215         // priority channels allowed
216         builder.allowPriorityChannels(true);
217         ZenPolicy channelsPriority = builder.build();
218 
219         // unset applied, channels setting keeps its state
220         channelsPriority.apply(unset);
221         assertThat(channelsPriority.getPriorityChannelsAllowed()).isEqualTo(ZenPolicy.STATE_ALLOW);
222     }
223 
224     @Test
testZenPolicyApplyChannels_applyStricter()225     public void testZenPolicyApplyChannels_applyStricter() {
226         mSetFlagsRule.enableFlags(Flags.FLAG_MODES_API);
227 
228         ZenPolicy.Builder builder = new ZenPolicy.Builder();
229         builder.allowPriorityChannels(false);
230         ZenPolicy none = builder.build();
231 
232         builder.allowPriorityChannels(true);
233         ZenPolicy priority = builder.build();
234 
235         // priority channels (less strict state) cannot override a setting that sets it to none
236         none.apply(priority);
237         assertThat(none.getPriorityChannelsAllowed()).isEqualTo(ZenPolicy.STATE_DISALLOW);
238     }
239 
240     @Test
testZenPolicyApplyChannels_applyLooser()241     public void testZenPolicyApplyChannels_applyLooser() {
242         mSetFlagsRule.enableFlags(Flags.FLAG_MODES_API);
243 
244         ZenPolicy.Builder builder = new ZenPolicy.Builder();
245         builder.allowPriorityChannels(false);
246         ZenPolicy none = builder.build();
247 
248         builder.allowPriorityChannels(true);
249         ZenPolicy priority = builder.build();
250 
251         // applying a policy with channelType=none overrides priority setting
252         priority.apply(none);
253         assertThat(priority.getPriorityChannelsAllowed()).isEqualTo(ZenPolicy.STATE_DISALLOW);
254     }
255 
256     @Test
testZenPolicyApplyChannels_applySet()257     public void testZenPolicyApplyChannels_applySet() {
258         mSetFlagsRule.enableFlags(Flags.FLAG_MODES_API);
259 
260         ZenPolicy.Builder builder = new ZenPolicy.Builder();
261         ZenPolicy unset = builder.build();
262 
263         builder.allowPriorityChannels(true);
264         ZenPolicy priority = builder.build();
265 
266         // applying a policy with a set channel type actually goes through
267         unset.apply(priority);
268         assertThat(unset.getPriorityChannelsAllowed()).isEqualTo(ZenPolicy.STATE_ALLOW);
269     }
270 
271     @Test
testZenPolicyOverwrite_allUnsetPolicies()272     public void testZenPolicyOverwrite_allUnsetPolicies() {
273         mSetFlagsRule.enableFlags(Flags.FLAG_MODES_API);
274 
275         ZenPolicy.Builder builder = new ZenPolicy.Builder();
276         ZenPolicy unset = builder.build();
277 
278         builder.allowCalls(ZenPolicy.PEOPLE_TYPE_CONTACTS);
279         builder.allowMedia(false);
280         builder.allowEvents(true);
281         builder.showFullScreenIntent(false);
282         builder.showInNotificationList(false);
283         ZenPolicy set = builder.build();
284 
285         ZenPolicy overwritten = set.overwrittenWith(unset);
286         assertThat(overwritten).isEqualTo(set);
287 
288         // should actually work the other way too.
289         ZenPolicy overwrittenWithSet = unset.overwrittenWith(set);
290         assertThat(overwrittenWithSet).isEqualTo(set);
291     }
292 
293     @Test
testZenPolicyOverwrite_someOverlappingFields_takeNewPolicy()294     public void testZenPolicyOverwrite_someOverlappingFields_takeNewPolicy() {
295         mSetFlagsRule.enableFlags(Flags.FLAG_MODES_API);
296 
297         ZenPolicy p1 = new ZenPolicy.Builder()
298                 .allowCalls(ZenPolicy.PEOPLE_TYPE_CONTACTS)
299                 .allowMessages(ZenPolicy.PEOPLE_TYPE_STARRED)
300                 .allowMedia(false)
301                 .showBadges(true)
302                 .build();
303 
304         ZenPolicy p2 = new ZenPolicy.Builder()
305                 .allowRepeatCallers(false)
306                 .allowConversations(ZenPolicy.CONVERSATION_SENDERS_IMPORTANT)
307                 .allowMessages(ZenPolicy.PEOPLE_TYPE_NONE)
308                 .showBadges(false)
309                 .showPeeking(true)
310                 .build();
311 
312         // when p1 is overwritten with p2, all values from p2 win regardless of strictness, and
313         // remaining fields take values from p1.
314         ZenPolicy p1OverwrittenWithP2 = p1.overwrittenWith(p2);
315         assertThat(p1OverwrittenWithP2.getPriorityCallSenders())
316                 .isEqualTo(ZenPolicy.PEOPLE_TYPE_CONTACTS);  // from p1
317         assertThat(p1OverwrittenWithP2.getPriorityMessageSenders())
318                 .isEqualTo(ZenPolicy.PEOPLE_TYPE_NONE);  // from p2
319         assertThat(p1OverwrittenWithP2.getPriorityCategoryRepeatCallers())
320                 .isEqualTo(ZenPolicy.STATE_DISALLOW);  // from p2
321         assertThat(p1OverwrittenWithP2.getPriorityCategoryMedia())
322                 .isEqualTo(ZenPolicy.STATE_DISALLOW);  // from p1
323         assertThat(p1OverwrittenWithP2.getVisualEffectBadge())
324                 .isEqualTo(ZenPolicy.STATE_DISALLOW);  // from p2
325         assertThat(p1OverwrittenWithP2.getVisualEffectPeek())
326                 .isEqualTo(ZenPolicy.STATE_ALLOW); // from p2
327 
328         ZenPolicy p2OverwrittenWithP1 = p2.overwrittenWith(p1);
329         assertThat(p2OverwrittenWithP1.getPriorityCallSenders())
330                 .isEqualTo(ZenPolicy.PEOPLE_TYPE_CONTACTS);  // from p1
331         assertThat(p2OverwrittenWithP1.getPriorityMessageSenders())
332                 .isEqualTo(ZenPolicy.PEOPLE_TYPE_STARRED);  // from p1
333         assertThat(p2OverwrittenWithP1.getPriorityCategoryRepeatCallers())
334                 .isEqualTo(ZenPolicy.STATE_DISALLOW);  // from p2
335         assertThat(p2OverwrittenWithP1.getPriorityCategoryMedia())
336                 .isEqualTo(ZenPolicy.STATE_DISALLOW);  // from p1
337         assertThat(p2OverwrittenWithP1.getVisualEffectBadge())
338                 .isEqualTo(ZenPolicy.STATE_ALLOW);  // from p1
339         assertThat(p2OverwrittenWithP1.getVisualEffectPeek())
340                 .isEqualTo(ZenPolicy.STATE_ALLOW); // from p2
341     }
342 
343     @Test
testZenPolicyMessagesInvalid()344     public void testZenPolicyMessagesInvalid() {
345         ZenPolicy.Builder builder = new ZenPolicy.Builder();
346 
347         builder.allowMessages(20); // invalid #, won't change policy
348         ZenPolicy policy = builder.build();
349         assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategoryMessages());
350         assertEquals(ZenPolicy.PEOPLE_TYPE_UNSET, policy.getPriorityMessageSenders());
351         assertProtoMatches(policy, policy.toProto());
352     }
353 
354     @Test
testZenPolicyCallsInvalid()355     public void testZenPolicyCallsInvalid() {
356         ZenPolicy.Builder builder = new ZenPolicy.Builder();
357 
358         builder.allowCalls(ZenPolicy.PEOPLE_TYPE_ANYONE);
359         builder.allowCalls(20); // invalid #, won't change policy
360         ZenPolicy policy = builder.build();
361         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryCalls());
362         assertEquals(ZenPolicy.PEOPLE_TYPE_ANYONE, policy.getPriorityCallSenders());
363         assertProtoMatches(policy, policy.toProto());
364     }
365 
366     @Test
testEmptyZenPolicy()367     public void testEmptyZenPolicy() {
368         ZenPolicy.Builder builder = new ZenPolicy.Builder();
369 
370         ZenPolicy policy = builder.build();
371         assertAllPriorityCategoriesUnsetExcept(policy, -1);
372         assertAllVisualEffectsUnsetExcept(policy, -1);
373         assertProtoMatches(policy, policy.toProto());
374     }
375 
376     @Test
testEmptyZenPolicy_emptyChannels()377     public void testEmptyZenPolicy_emptyChannels() {
378         mSetFlagsRule.enableFlags(Flags.FLAG_MODES_API);
379         ZenPolicy.Builder builder = new ZenPolicy.Builder();
380 
381         ZenPolicy policy = builder.build();
382         assertThat(policy.getPriorityChannelsAllowed()).isEqualTo(ZenPolicy.STATE_UNSET);
383     }
384 
385     @Test
testAllowReminders()386     public void testAllowReminders() {
387         ZenPolicy.Builder builder = new ZenPolicy.Builder();
388 
389         builder.allowReminders(true);
390         ZenPolicy policy = builder.build();
391         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_REMINDERS);
392         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryReminders());
393         assertAllVisualEffectsUnsetExcept(policy, -1);
394         assertProtoMatches(policy, policy.toProto());
395 
396         builder.allowReminders(false);
397         policy = builder.build();
398         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_REMINDERS);
399         assertEquals(ZenPolicy.STATE_DISALLOW, policy.getPriorityCategoryReminders());
400         assertAllVisualEffectsUnsetExcept(policy, -1);
401         assertProtoMatches(policy, policy.toProto());
402     }
403 
404     @Test
testAllowEvents()405     public void testAllowEvents() {
406         ZenPolicy.Builder builder = new ZenPolicy.Builder();
407 
408         builder.allowEvents(true);
409         ZenPolicy policy = builder.build();
410         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_EVENTS);
411         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryEvents());
412         assertAllVisualEffectsUnsetExcept(policy, -1);
413         assertProtoMatches(policy, policy.toProto());
414 
415         builder.allowEvents(false);
416         policy = builder.build();
417         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_EVENTS);
418         assertEquals(ZenPolicy.STATE_DISALLOW, policy.getPriorityCategoryEvents());
419         assertAllVisualEffectsUnsetExcept(policy, -1);
420         assertProtoMatches(policy, policy.toProto());
421     }
422 
423     @Test
testAllowMessages()424     public void testAllowMessages() {
425         ZenPolicy.Builder builder = new ZenPolicy.Builder();
426 
427         builder.allowMessages(ZenPolicy.PEOPLE_TYPE_ANYONE);
428         ZenPolicy policy = builder.build();
429         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_MESSAGES);
430         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryMessages());
431         assertEquals(ZenPolicy.PEOPLE_TYPE_ANYONE, policy.getPriorityMessageSenders());
432         assertAllVisualEffectsUnsetExcept(policy, -1);
433         assertProtoMatches(policy, policy.toProto());
434 
435         builder.allowMessages(ZenPolicy.PEOPLE_TYPE_CONTACTS);
436         policy = builder.build();
437         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_MESSAGES);
438         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryMessages());
439         assertEquals(ZenPolicy.PEOPLE_TYPE_CONTACTS, policy.getPriorityMessageSenders());
440         assertAllVisualEffectsUnsetExcept(policy, -1);
441         assertProtoMatches(policy, policy.toProto());
442 
443         builder.allowMessages(ZenPolicy.PEOPLE_TYPE_STARRED);
444         policy = builder.build();
445         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_MESSAGES);
446         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryMessages());
447         assertEquals(ZenPolicy.PEOPLE_TYPE_STARRED, policy.getPriorityMessageSenders());
448         assertAllVisualEffectsUnsetExcept(policy, -1);
449         assertProtoMatches(policy, policy.toProto());
450 
451         builder.allowMessages(ZenPolicy.PEOPLE_TYPE_NONE);
452         policy = builder.build();
453         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_MESSAGES);
454         assertEquals(ZenPolicy.STATE_DISALLOW, policy.getPriorityCategoryMessages());
455         assertEquals(ZenPolicy.PEOPLE_TYPE_NONE, policy.getPriorityMessageSenders());
456         assertAllVisualEffectsUnsetExcept(policy, -1);
457         assertProtoMatches(policy, policy.toProto());
458 
459         builder.allowMessages(ZenPolicy.PEOPLE_TYPE_UNSET);
460         policy = builder.build();
461         assertAllPriorityCategoriesUnsetExcept(policy, -1);
462         assertAllVisualEffectsUnsetExcept(policy, -1);
463         assertProtoMatches(policy, policy.toProto());
464     }
465 
466     @Test
testAllowCalls()467     public void testAllowCalls() {
468         ZenPolicy.Builder builder = new ZenPolicy.Builder();
469 
470         builder.allowCalls(ZenPolicy.PEOPLE_TYPE_ANYONE);
471         ZenPolicy policy = builder.build();
472         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_CALLS);
473         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryCalls());
474         assertEquals(ZenPolicy.PEOPLE_TYPE_ANYONE, policy.getPriorityCallSenders());
475         assertAllVisualEffectsUnsetExcept(policy, -1);
476         assertProtoMatches(policy, policy.toProto());
477 
478         builder.allowCalls(ZenPolicy.PEOPLE_TYPE_CONTACTS);
479         policy = builder.build();
480         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_CALLS);
481         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryCalls());
482         assertEquals(ZenPolicy.PEOPLE_TYPE_CONTACTS, policy.getPriorityCallSenders());
483         assertAllVisualEffectsUnsetExcept(policy, -1);
484         assertProtoMatches(policy, policy.toProto());
485 
486         builder.allowCalls(ZenPolicy.PEOPLE_TYPE_STARRED);
487         policy = builder.build();
488         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_CALLS);
489         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryCalls());
490         assertEquals(ZenPolicy.PEOPLE_TYPE_STARRED, policy.getPriorityCallSenders());
491         assertAllVisualEffectsUnsetExcept(policy, -1);
492         assertProtoMatches(policy, policy.toProto());
493 
494         builder.allowCalls(ZenPolicy.PEOPLE_TYPE_NONE);
495         policy = builder.build();
496         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_CALLS);
497         assertEquals(ZenPolicy.STATE_DISALLOW, policy.getPriorityCategoryCalls());
498         assertEquals(ZenPolicy.PEOPLE_TYPE_NONE, policy.getPriorityCallSenders());
499         assertAllVisualEffectsUnsetExcept(policy, -1);
500         assertProtoMatches(policy, policy.toProto());
501 
502         builder.allowCalls(ZenPolicy.PEOPLE_TYPE_UNSET);
503         policy = builder.build();
504         assertAllPriorityCategoriesUnsetExcept(policy, -1);
505         assertAllVisualEffectsUnsetExcept(policy, -1);
506         assertProtoMatches(policy, policy.toProto());
507     }
508 
509     @Test
testAllowRepeatCallers()510     public void testAllowRepeatCallers() {
511         ZenPolicy.Builder builder = new ZenPolicy.Builder();
512 
513         builder.allowRepeatCallers(true);
514         ZenPolicy policy = builder.build();
515         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_REPEAT_CALLERS);
516         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryRepeatCallers());
517         assertAllVisualEffectsUnsetExcept(policy, -1);
518         assertProtoMatches(policy, policy.toProto());
519 
520         builder.allowRepeatCallers(false);
521         policy = builder.build();
522         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_REPEAT_CALLERS);
523         assertEquals(ZenPolicy.STATE_DISALLOW, policy.getPriorityCategoryRepeatCallers());
524         assertAllVisualEffectsUnsetExcept(policy, -1);
525         assertProtoMatches(policy, policy.toProto());
526     }
527 
528     @Test
testAllowAlarms()529     public void testAllowAlarms() {
530         ZenPolicy.Builder builder = new ZenPolicy.Builder();
531 
532         builder.allowAlarms(true);
533         ZenPolicy policy = builder.build();
534         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_ALARMS);
535         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryAlarms());
536         assertAllVisualEffectsUnsetExcept(policy, -1);
537         assertProtoMatches(policy, policy.toProto());
538 
539         builder.allowAlarms(false);
540         policy = builder.build();
541         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_ALARMS);
542         assertEquals(ZenPolicy.STATE_DISALLOW, policy.getPriorityCategoryAlarms());
543         assertAllVisualEffectsUnsetExcept(policy, -1);
544         assertProtoMatches(policy, policy.toProto());
545     }
546 
547     @Test
testAllowMedia()548     public void testAllowMedia() {
549         ZenPolicy.Builder builder = new ZenPolicy.Builder();
550 
551         builder.allowMedia(true);
552         ZenPolicy policy = builder.build();
553         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_MEDIA);
554         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryMedia());
555         assertAllVisualEffectsUnsetExcept(policy, -1);
556         assertProtoMatches(policy, policy.toProto());
557 
558         builder.allowMedia(false);
559         policy = builder.build();
560         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_MEDIA);
561         assertEquals(ZenPolicy.STATE_DISALLOW, policy.getPriorityCategoryMedia());
562         assertAllVisualEffectsUnsetExcept(policy, -1);
563         assertProtoMatches(policy, policy.toProto());
564     }
565 
566     @Test
testAllowSystem()567     public void testAllowSystem() {
568         ZenPolicy.Builder builder = new ZenPolicy.Builder();
569 
570         builder.allowSystem(true);
571         ZenPolicy policy = builder.build();
572         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_SYSTEM);
573         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategorySystem());
574         assertAllVisualEffectsUnsetExcept(policy, -1);
575         assertProtoMatches(policy, policy.toProto());
576 
577         builder.allowSystem(false);
578         policy = builder.build();
579         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_SYSTEM);
580         assertEquals(ZenPolicy.STATE_DISALLOW, policy.getPriorityCategorySystem());
581         assertAllVisualEffectsUnsetExcept(policy, -1);
582         assertProtoMatches(policy, policy.toProto());
583     }
584 
585     @Test
tesShowFullScreenIntent()586     public void tesShowFullScreenIntent() {
587         ZenPolicy.Builder builder = new ZenPolicy.Builder();
588 
589         builder.showFullScreenIntent(true);
590         ZenPolicy policy = builder.build();
591         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_FULL_SCREEN_INTENT);
592         assertProtoMatches(policy, policy.toProto());
593 
594         builder.showFullScreenIntent(false);
595         policy = builder.build();
596         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_FULL_SCREEN_INTENT);
597         assertProtoMatches(policy, policy.toProto());
598     }
599 
600     @Test
tesShowLights()601     public void tesShowLights() {
602         ZenPolicy.Builder builder = new ZenPolicy.Builder();
603 
604         builder.showLights(true);
605         ZenPolicy policy = builder.build();
606         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_LIGHTS);
607         assertProtoMatches(policy, policy.toProto());
608 
609         builder.showLights(false);
610         policy = builder.build();
611         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_LIGHTS);
612         assertProtoMatches(policy, policy.toProto());
613     }
614 
615     @Test
tesShowPeeking()616     public void tesShowPeeking() {
617         ZenPolicy.Builder builder = new ZenPolicy.Builder();
618 
619         builder.showPeeking(true);
620         ZenPolicy policy = builder.build();
621         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_PEEK);
622         assertProtoMatches(policy, policy.toProto());
623 
624         builder.showPeeking(false);
625         policy = builder.build();
626         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_PEEK);
627         assertProtoMatches(policy, policy.toProto());
628     }
629 
630     @Test
tesShowStatusBarIcons()631     public void tesShowStatusBarIcons() {
632         ZenPolicy.Builder builder = new ZenPolicy.Builder();
633 
634         builder.showStatusBarIcons(true);
635         ZenPolicy policy = builder.build();
636         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_STATUS_BAR);
637         assertProtoMatches(policy, policy.toProto());
638 
639         builder.showStatusBarIcons(false);
640         policy = builder.build();
641         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_STATUS_BAR);
642         assertProtoMatches(policy, policy.toProto());
643     }
644 
645     @Test
tesShowBadges()646     public void tesShowBadges() {
647         ZenPolicy.Builder builder = new ZenPolicy.Builder();
648 
649         builder.showBadges(true);
650         ZenPolicy policy = builder.build();
651         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_BADGE);
652         assertProtoMatches(policy, policy.toProto());
653 
654         builder.showBadges(false);
655         policy = builder.build();
656         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_BADGE);
657         assertProtoMatches(policy, policy.toProto());
658     }
659 
660     @Test
tesShowInAmbientDisplay()661     public void tesShowInAmbientDisplay() {
662         ZenPolicy.Builder builder = new ZenPolicy.Builder();
663 
664         builder.showInAmbientDisplay(true);
665         ZenPolicy policy = builder.build();
666         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_AMBIENT);
667         assertProtoMatches(policy, policy.toProto());
668 
669         builder.showInAmbientDisplay(false);
670         policy = builder.build();
671         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_AMBIENT);
672         assertProtoMatches(policy, policy.toProto());
673     }
674 
675     @Test
tesShowInNotificationList()676     public void tesShowInNotificationList() {
677         ZenPolicy.Builder builder = new ZenPolicy.Builder();
678 
679         builder.showInNotificationList(true);
680         ZenPolicy policy = builder.build();
681         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_NOTIFICATION_LIST);
682         assertProtoMatches(policy, policy.toProto());
683 
684         builder.showInNotificationList(false);
685         policy = builder.build();
686         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_NOTIFICATION_LIST);
687         assertProtoMatches(policy, policy.toProto());
688     }
689 
690     @Test
testAllowChannels_noFlag()691     public void testAllowChannels_noFlag() {
692         mSetFlagsRule.disableFlags(Flags.FLAG_MODES_API);
693 
694         // allowChannels should be unset, not be modifiable, and not show up in any output
695         ZenPolicy.Builder builder = new ZenPolicy.Builder();
696         builder.allowPriorityChannels(true);
697         ZenPolicy policy = builder.build();
698 
699         assertThat(policy.getPriorityChannelsAllowed()).isEqualTo(ZenPolicy.STATE_UNSET);
700         assertThat(policy.toString().contains("allowChannels")).isFalse();
701     }
702 
703     @Test
testAllowChannels()704     public void testAllowChannels() {
705         mSetFlagsRule.enableFlags(Flags.FLAG_MODES_API);
706 
707         // allow priority channels
708         ZenPolicy.Builder builder = new ZenPolicy.Builder();
709         builder.allowPriorityChannels(true);
710         ZenPolicy policy = builder.build();
711         assertThat(policy.getPriorityChannelsAllowed()).isEqualTo(ZenPolicy.STATE_ALLOW);
712 
713         // disallow priority channels
714         builder.allowPriorityChannels(false);
715         policy = builder.build();
716         assertThat(policy.getPriorityChannelsAllowed()).isEqualTo(ZenPolicy.STATE_DISALLOW);
717     }
718 
719     @Test
testPolicyBuilder_constructFromPolicy()720     public void testPolicyBuilder_constructFromPolicy() {
721         ZenPolicy.Builder builder = new ZenPolicy.Builder();
722         ZenPolicy policy = builder.allowRepeatCallers(true).allowAlarms(false)
723                 .showLights(true).showBadges(false)
724                 .allowPriorityChannels(true)
725                 .build();
726 
727         ZenPolicy newPolicy = new ZenPolicy.Builder(policy).build();
728 
729         assertThat(newPolicy.getPriorityCategoryAlarms()).isEqualTo(ZenPolicy.STATE_DISALLOW);
730         assertThat(newPolicy.getPriorityCategoryCalls()).isEqualTo(ZenPolicy.STATE_UNSET);
731         assertThat(newPolicy.getPriorityCategoryRepeatCallers()).isEqualTo(ZenPolicy.STATE_ALLOW);
732 
733         assertThat(newPolicy.getVisualEffectLights()).isEqualTo(ZenPolicy.STATE_ALLOW);
734         assertThat(newPolicy.getVisualEffectBadge()).isEqualTo(ZenPolicy.STATE_DISALLOW);
735         assertThat(newPolicy.getVisualEffectPeek()).isEqualTo(ZenPolicy.STATE_UNSET);
736 
737         assertThat(newPolicy.getPriorityChannelsAllowed()).isEqualTo(ZenPolicy.STATE_ALLOW);
738     }
739 
740     @Test
testTooLongLists_fromParcel()741     public void testTooLongLists_fromParcel() {
742         ArrayList<Integer> longList = new ArrayList<Integer>(50);
743         for (int i = 0; i < 50; i++) {
744             longList.add(ZenPolicy.STATE_UNSET);
745         }
746 
747         ZenPolicy.Builder builder = new ZenPolicy.Builder();
748         ZenPolicy policy = builder.build();
749 
750         try {
751             Field priorityCategories = Class.forName(CLASS).getDeclaredField(
752                     "mPriorityCategories");
753             priorityCategories.setAccessible(true);
754             priorityCategories.set(policy, longList);
755 
756             Field visualEffects = Class.forName(CLASS).getDeclaredField("mVisualEffects");
757             visualEffects.setAccessible(true);
758             visualEffects.set(policy, longList);
759         } catch (NoSuchFieldException e) {
760             fail(e.toString());
761         } catch (ClassNotFoundException e) {
762             fail(e.toString());
763         } catch (IllegalAccessException e) {
764             fail(e.toString());
765         }
766 
767         Parcel parcel = Parcel.obtain();
768         policy.writeToParcel(parcel, 0);
769         parcel.setDataPosition(0);
770 
771         ZenPolicy fromParcel = ZenPolicy.CREATOR.createFromParcel(parcel);
772 
773         // Confirm that all the fields are accessible and UNSET
774         assertAllPriorityCategoriesUnsetExcept(fromParcel, -1);
775         assertAllVisualEffectsUnsetExcept(fromParcel, -1);
776 
777         // Because we don't access the lists directly, we also need to use reflection to make sure
778         // the lists are the right length.
779         try {
780             Field priorityCategories = Class.forName(CLASS).getDeclaredField(
781                     "mPriorityCategories");
782             priorityCategories.setAccessible(true);
783             ArrayList<Integer> pcList = (ArrayList<Integer>) priorityCategories.get(fromParcel);
784             assertEquals(ZenPolicy.NUM_PRIORITY_CATEGORIES, pcList.size());
785 
786 
787             Field visualEffects = Class.forName(CLASS).getDeclaredField("mVisualEffects");
788             visualEffects.setAccessible(true);
789             ArrayList<Integer> veList = (ArrayList<Integer>) visualEffects.get(fromParcel);
790             assertEquals(ZenPolicy.NUM_VISUAL_EFFECTS, veList.size());
791         } catch (NoSuchFieldException e) {
792             fail(e.toString());
793         } catch (ClassNotFoundException e) {
794             fail(e.toString());
795         } catch (IllegalAccessException e) {
796             fail(e.toString());
797         }
798     }
799 
assertAllPriorityCategoriesUnsetExcept(ZenPolicy policy, int except)800     private void assertAllPriorityCategoriesUnsetExcept(ZenPolicy policy, int except) {
801         if (except != ZenPolicy.PRIORITY_CATEGORY_REMINDERS) {
802             assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategoryReminders());
803         }
804 
805         if (except != ZenPolicy.PRIORITY_CATEGORY_EVENTS) {
806             assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategoryEvents());
807         }
808 
809         if (except != ZenPolicy.PRIORITY_CATEGORY_MESSAGES) {
810             assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategoryMessages());
811         }
812 
813         if (except != ZenPolicy.PRIORITY_CATEGORY_CALLS) {
814             assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategoryCalls());
815         }
816 
817         if (except != ZenPolicy.PRIORITY_CATEGORY_REPEAT_CALLERS) {
818             assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategoryRepeatCallers());
819         }
820 
821         if (except != ZenPolicy.PRIORITY_CATEGORY_ALARMS) {
822             assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategoryAlarms());
823         }
824 
825         if (except != ZenPolicy.PRIORITY_CATEGORY_MEDIA) {
826             assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategoryMedia());
827         }
828 
829         if (except != ZenPolicy.PRIORITY_CATEGORY_SYSTEM) {
830             assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategorySystem());
831         }
832     }
833 
assertAllVisualEffectsUnsetExcept(ZenPolicy policy, int except)834     private void assertAllVisualEffectsUnsetExcept(ZenPolicy policy, int except) {
835         if (except != ZenPolicy.VISUAL_EFFECT_FULL_SCREEN_INTENT) {
836             assertEquals(ZenPolicy.STATE_UNSET, policy.getVisualEffectFullScreenIntent());
837         }
838 
839         if (except != ZenPolicy.VISUAL_EFFECT_LIGHTS) {
840             assertEquals(ZenPolicy.STATE_UNSET, policy.getVisualEffectLights());
841         }
842 
843         if (except != ZenPolicy.VISUAL_EFFECT_PEEK) {
844             assertEquals(ZenPolicy.STATE_UNSET, policy.getVisualEffectPeek());
845         }
846 
847         if (except != ZenPolicy.VISUAL_EFFECT_STATUS_BAR) {
848             assertEquals(ZenPolicy.STATE_UNSET, policy.getVisualEffectStatusBar());
849         }
850 
851         if (except != ZenPolicy.VISUAL_EFFECT_BADGE) {
852             assertEquals(ZenPolicy.STATE_UNSET, policy.getVisualEffectBadge());
853         }
854 
855         if (except != ZenPolicy.VISUAL_EFFECT_AMBIENT) {
856             assertEquals(ZenPolicy.STATE_UNSET, policy.getVisualEffectAmbient());
857         }
858 
859         if (except != ZenPolicy.VISUAL_EFFECT_NOTIFICATION_LIST) {
860             assertEquals(ZenPolicy.STATE_UNSET, policy.getVisualEffectNotificationList());
861         }
862     }
863 
assertProtoMatches(ZenPolicy policy, byte[] bytes)864     private void assertProtoMatches(ZenPolicy policy, byte[] bytes) {
865         try {
866             DNDPolicyProto proto = DNDPolicyProto.parseFrom(bytes);
867 
868             assertEquals(policy.getPriorityCategoryCalls(), proto.calls);
869             assertEquals(policy.getPriorityCategoryRepeatCallers(), proto.repeatCallers);
870             assertEquals(policy.getPriorityCategoryMessages(), proto.messages);
871             assertEquals(policy.getPriorityCategoryConversations(), proto.conversations);
872             assertEquals(policy.getPriorityCategoryReminders(), proto.reminders);
873             assertEquals(policy.getPriorityCategoryEvents(), proto.events);
874             assertEquals(policy.getPriorityCategoryAlarms(), proto.alarms);
875             assertEquals(policy.getPriorityCategoryMedia(), proto.media);
876             assertEquals(policy.getPriorityCategorySystem(), proto.system);
877 
878             assertEquals(policy.getVisualEffectFullScreenIntent(), proto.fullscreen);
879             assertEquals(policy.getVisualEffectLights(), proto.lights);
880             assertEquals(policy.getVisualEffectPeek(), proto.peek);
881             assertEquals(policy.getVisualEffectStatusBar(), proto.statusBar);
882             assertEquals(policy.getVisualEffectBadge(), proto.badge);
883             assertEquals(policy.getVisualEffectAmbient(), proto.ambient);
884             assertEquals(policy.getVisualEffectNotificationList(), proto.notificationList);
885 
886             assertEquals(policy.getPriorityCallSenders(), proto.allowCallsFrom);
887             assertEquals(policy.getPriorityMessageSenders(), proto.allowMessagesFrom);
888             assertEquals(policy.getPriorityConversationSenders(), proto.allowConversationsFrom);
889         } catch (InvalidProtocolBufferNanoException e) {
890             fail("could not parse proto bytes");
891         }
892 
893     }
894 }
895