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