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 junit.framework.Assert.assertEquals;
20 import static junit.framework.Assert.fail;
21 
22 import android.os.Parcel;
23 import android.service.notification.ZenPolicy;
24 import android.service.notification.nano.DNDPolicyProto;
25 import android.test.suitebuilder.annotation.SmallTest;
26 
27 import androidx.test.runner.AndroidJUnit4;
28 
29 import com.android.server.UiServiceTestCase;
30 
31 import com.google.protobuf.nano.InvalidProtocolBufferNanoException;
32 
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 
36 import java.lang.reflect.Field;
37 import java.util.ArrayList;
38 
39 @SmallTest
40 @RunWith(AndroidJUnit4.class)
41 public class ZenPolicyTest extends UiServiceTestCase {
42     private static final String CLASS = "android.service.notification.ZenPolicy";
43 
44     @Test
testZenPolicyApplyAllowedToDisallowed()45     public void testZenPolicyApplyAllowedToDisallowed() {
46         ZenPolicy.Builder builder = new ZenPolicy.Builder();
47 
48         // reminders are disallowed
49         builder.allowReminders(false);
50         ZenPolicy remindersDisallowed = builder.build();
51         assertEquals(ZenPolicy.STATE_DISALLOW,
52                 remindersDisallowed.getPriorityCategoryReminders());
53 
54         // reminders are allowed
55         builder.allowReminders(true);
56         ZenPolicy remindersAllowed = builder.build();
57         assertEquals(ZenPolicy.STATE_ALLOW,
58                 remindersAllowed.getPriorityCategoryReminders());
59         assertEquals(ZenPolicy.STATE_DISALLOW,
60                 remindersDisallowed.getPriorityCategoryReminders());
61 
62         // we apply reminders allowed to reminders disallowed
63         // -> reminders should remain disallowed
64         remindersDisallowed.apply(remindersAllowed);
65         assertEquals(ZenPolicy.STATE_DISALLOW,
66                 remindersDisallowed.getPriorityCategoryReminders());
67     }
68 
69     @Test
testZenPolicyApplyAllowedToUnset()70     public void testZenPolicyApplyAllowedToUnset() {
71         ZenPolicy.Builder builder = new ZenPolicy.Builder();
72 
73         // reminders are unset
74         ZenPolicy remindersUnset = builder.build();
75 
76         // reminders are allowed
77         builder.allowReminders(true);
78         ZenPolicy remindersAllowed = builder.build();
79 
80         // we apply reminders allowed to reminders unset
81         // -> reminders should be allowed
82         remindersUnset.apply(remindersAllowed);
83         assertEquals(ZenPolicy.STATE_ALLOW, remindersUnset.getPriorityCategoryReminders());
84     }
85 
86     @Test
testZenPolicyApplyDisallowedToUnset()87     public void testZenPolicyApplyDisallowedToUnset() {
88         ZenPolicy.Builder builder = new ZenPolicy.Builder();
89 
90         // reminders are unset
91         ZenPolicy remindersUnset = builder.build();
92 
93         // reminders are disallowed
94         builder.allowReminders(false);
95         ZenPolicy remindersDisallowed = builder.build();
96 
97         // we apply reminders disallowed to reminders unset
98         // -> reminders should remain disallowed
99         remindersUnset.apply(remindersDisallowed);
100         assertEquals(ZenPolicy.STATE_DISALLOW,
101                 remindersUnset.getPriorityCategoryReminders());
102     }
103 
104     @Test
testZenPolicyApplyDisallowedToAllowed()105     public void testZenPolicyApplyDisallowedToAllowed() {
106         ZenPolicy.Builder builder = new ZenPolicy.Builder();
107 
108         // reminders are allowed
109         builder.allowReminders(true);
110         ZenPolicy remindersAllowed = builder.build();
111 
112         // reminders are disallowed
113         builder.allowReminders(false);
114         ZenPolicy remindersDisallowed = builder.build();
115 
116         // we apply reminders allowed to reminders disallowed
117         // -> reminders should change to disallowed
118         remindersAllowed.apply(remindersDisallowed);
119         assertEquals(ZenPolicy.STATE_DISALLOW, remindersAllowed.getPriorityCategoryReminders());
120     }
121 
122     @Test
testZenPolicyApplyUnsetToAllowed()123     public void testZenPolicyApplyUnsetToAllowed() {
124         ZenPolicy.Builder builder = new ZenPolicy.Builder();
125 
126         // reminders are allowed
127         builder.allowReminders(true);
128         ZenPolicy remindersAllowed = builder.build();
129 
130         // reminders are unset
131         ZenPolicy.Builder builder2 = new ZenPolicy.Builder();
132         ZenPolicy remindersUnset = builder2.build();
133 
134         // we apply reminders allowed to reminders unset
135         // -> reminders should remain allowed
136         remindersAllowed.apply(remindersUnset);
137         assertEquals(ZenPolicy.STATE_ALLOW, remindersAllowed.getPriorityCategoryReminders());
138     }
139 
140     @Test
testZenPolicyApplyMoreSevereCallSenders()141     public void testZenPolicyApplyMoreSevereCallSenders() {
142         ZenPolicy.Builder builder = new ZenPolicy.Builder();
143 
144         // calls from contacts allowed
145         builder.allowCalls(ZenPolicy.PEOPLE_TYPE_CONTACTS);
146         ZenPolicy contactsAllowed = builder.build();
147 
148         // calls from starred contacts allowed
149         builder.allowCalls(ZenPolicy.PEOPLE_TYPE_STARRED);
150         ZenPolicy starredAllowed = builder.build();
151 
152         // we apply starredAllowed to contactsAllowed -> starred contacts allowed (more restrictive)
153         contactsAllowed.apply(starredAllowed);
154         assertEquals(ZenPolicy.STATE_ALLOW, contactsAllowed.getPriorityCategoryCalls());
155         assertEquals(ZenPolicy.PEOPLE_TYPE_STARRED, contactsAllowed.getPriorityCallSenders());
156     }
157 
158     @Test
testZenPolicyApplyLessSevereCallSenders()159     public void testZenPolicyApplyLessSevereCallSenders() {
160         ZenPolicy.Builder builder = new ZenPolicy.Builder();
161 
162         // calls from contacts allowed
163         builder.allowCalls(ZenPolicy.PEOPLE_TYPE_CONTACTS);
164         ZenPolicy contactsAllowed = builder.build();
165 
166         // calls from anyone allowed
167         builder.allowCalls(ZenPolicy.PEOPLE_TYPE_ANYONE);
168         ZenPolicy anyoneAllowed = builder.build();
169 
170         // we apply anyoneAllowed to contactsAllowed -> contactsAllowed (more restrictive)
171         contactsAllowed.apply(anyoneAllowed);
172         assertEquals(ZenPolicy.STATE_ALLOW, contactsAllowed.getPriorityCategoryCalls());
173         assertEquals(ZenPolicy.PEOPLE_TYPE_CONTACTS, contactsAllowed.getPriorityCallSenders());
174     }
175 
176     @Test
testZenPolicyApplyMoreSevereMessageSenders()177     public void testZenPolicyApplyMoreSevereMessageSenders() {
178         ZenPolicy.Builder builder = new ZenPolicy.Builder();
179 
180         // messsages from contacts allowed
181         builder.allowMessages(ZenPolicy.PEOPLE_TYPE_CONTACTS);
182         ZenPolicy contactsAllowed = builder.build();
183 
184         // messsages from no one allowed
185         builder.allowMessages(ZenPolicy.PEOPLE_TYPE_NONE);
186         ZenPolicy noneAllowed = builder.build();
187 
188         // noneAllowed to contactsAllowed -> no messages allowed (more restrictive)
189         contactsAllowed.apply(noneAllowed);
190         assertEquals(ZenPolicy.STATE_DISALLOW, contactsAllowed.getPriorityCategoryMessages());
191         assertEquals(ZenPolicy.PEOPLE_TYPE_NONE, contactsAllowed.getPriorityMessageSenders());
192     }
193 
194     @Test
testZenPolicyMessagesInvalid()195     public void testZenPolicyMessagesInvalid() {
196         ZenPolicy.Builder builder = new ZenPolicy.Builder();
197 
198         builder.allowMessages(20); // invalid #, won't change policy
199         ZenPolicy policy = builder.build();
200         assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategoryMessages());
201         assertEquals(ZenPolicy.PEOPLE_TYPE_UNSET, policy.getPriorityMessageSenders());
202         assertProtoMatches(policy, policy.toProto());
203     }
204 
205     @Test
testZenPolicyCallsInvalid()206     public void testZenPolicyCallsInvalid() {
207         ZenPolicy.Builder builder = new ZenPolicy.Builder();
208 
209         builder.allowCalls(ZenPolicy.PEOPLE_TYPE_ANYONE);
210         builder.allowCalls(20); // invalid #, won't change policy
211         ZenPolicy policy = builder.build();
212         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryCalls());
213         assertEquals(ZenPolicy.PEOPLE_TYPE_ANYONE, policy.getPriorityCallSenders());
214         assertProtoMatches(policy, policy.toProto());
215     }
216 
217     @Test
testEmptyZenPolicy()218     public void testEmptyZenPolicy() {
219         ZenPolicy.Builder builder = new ZenPolicy.Builder();
220 
221         ZenPolicy policy = builder.build();
222         assertAllPriorityCategoriesUnsetExcept(policy, -1);
223         assertAllVisualEffectsUnsetExcept(policy, -1);
224         assertProtoMatches(policy, policy.toProto());
225     }
226 
227     @Test
testAllowReminders()228     public void testAllowReminders() {
229         ZenPolicy.Builder builder = new ZenPolicy.Builder();
230 
231         builder.allowReminders(true);
232         ZenPolicy policy = builder.build();
233         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_REMINDERS);
234         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryReminders());
235         assertAllVisualEffectsUnsetExcept(policy, -1);
236         assertProtoMatches(policy, policy.toProto());
237 
238         builder.allowReminders(false);
239         policy = builder.build();
240         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_REMINDERS);
241         assertEquals(ZenPolicy.STATE_DISALLOW, policy.getPriorityCategoryReminders());
242         assertAllVisualEffectsUnsetExcept(policy, -1);
243         assertProtoMatches(policy, policy.toProto());
244     }
245 
246     @Test
testAllowEvents()247     public void testAllowEvents() {
248         ZenPolicy.Builder builder = new ZenPolicy.Builder();
249 
250         builder.allowEvents(true);
251         ZenPolicy policy = builder.build();
252         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_EVENTS);
253         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryEvents());
254         assertAllVisualEffectsUnsetExcept(policy, -1);
255         assertProtoMatches(policy, policy.toProto());
256 
257         builder.allowEvents(false);
258         policy = builder.build();
259         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_EVENTS);
260         assertEquals(ZenPolicy.STATE_DISALLOW, policy.getPriorityCategoryEvents());
261         assertAllVisualEffectsUnsetExcept(policy, -1);
262         assertProtoMatches(policy, policy.toProto());
263     }
264 
265     @Test
testAllowMessages()266     public void testAllowMessages() {
267         ZenPolicy.Builder builder = new ZenPolicy.Builder();
268 
269         builder.allowMessages(ZenPolicy.PEOPLE_TYPE_ANYONE);
270         ZenPolicy policy = builder.build();
271         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_MESSAGES);
272         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryMessages());
273         assertEquals(ZenPolicy.PEOPLE_TYPE_ANYONE, policy.getPriorityMessageSenders());
274         assertAllVisualEffectsUnsetExcept(policy, -1);
275         assertProtoMatches(policy, policy.toProto());
276 
277         builder.allowMessages(ZenPolicy.PEOPLE_TYPE_CONTACTS);
278         policy = builder.build();
279         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_MESSAGES);
280         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryMessages());
281         assertEquals(ZenPolicy.PEOPLE_TYPE_CONTACTS, policy.getPriorityMessageSenders());
282         assertAllVisualEffectsUnsetExcept(policy, -1);
283         assertProtoMatches(policy, policy.toProto());
284 
285         builder.allowMessages(ZenPolicy.PEOPLE_TYPE_STARRED);
286         policy = builder.build();
287         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_MESSAGES);
288         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryMessages());
289         assertEquals(ZenPolicy.PEOPLE_TYPE_STARRED, policy.getPriorityMessageSenders());
290         assertAllVisualEffectsUnsetExcept(policy, -1);
291         assertProtoMatches(policy, policy.toProto());
292 
293         builder.allowMessages(ZenPolicy.PEOPLE_TYPE_NONE);
294         policy = builder.build();
295         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_MESSAGES);
296         assertEquals(ZenPolicy.STATE_DISALLOW, policy.getPriorityCategoryMessages());
297         assertEquals(ZenPolicy.PEOPLE_TYPE_NONE, policy.getPriorityMessageSenders());
298         assertAllVisualEffectsUnsetExcept(policy, -1);
299         assertProtoMatches(policy, policy.toProto());
300 
301         builder.allowMessages(ZenPolicy.PEOPLE_TYPE_UNSET);
302         policy = builder.build();
303         assertAllPriorityCategoriesUnsetExcept(policy, -1);
304         assertAllVisualEffectsUnsetExcept(policy, -1);
305         assertProtoMatches(policy, policy.toProto());
306     }
307 
308     @Test
testAllowCalls()309     public void testAllowCalls() {
310         ZenPolicy.Builder builder = new ZenPolicy.Builder();
311 
312         builder.allowCalls(ZenPolicy.PEOPLE_TYPE_ANYONE);
313         ZenPolicy policy = builder.build();
314         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_CALLS);
315         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryCalls());
316         assertEquals(ZenPolicy.PEOPLE_TYPE_ANYONE, policy.getPriorityCallSenders());
317         assertAllVisualEffectsUnsetExcept(policy, -1);
318         assertProtoMatches(policy, policy.toProto());
319 
320         builder.allowCalls(ZenPolicy.PEOPLE_TYPE_CONTACTS);
321         policy = builder.build();
322         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_CALLS);
323         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryCalls());
324         assertEquals(ZenPolicy.PEOPLE_TYPE_CONTACTS, policy.getPriorityCallSenders());
325         assertAllVisualEffectsUnsetExcept(policy, -1);
326         assertProtoMatches(policy, policy.toProto());
327 
328         builder.allowCalls(ZenPolicy.PEOPLE_TYPE_STARRED);
329         policy = builder.build();
330         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_CALLS);
331         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryCalls());
332         assertEquals(ZenPolicy.PEOPLE_TYPE_STARRED, policy.getPriorityCallSenders());
333         assertAllVisualEffectsUnsetExcept(policy, -1);
334         assertProtoMatches(policy, policy.toProto());
335 
336         builder.allowCalls(ZenPolicy.PEOPLE_TYPE_NONE);
337         policy = builder.build();
338         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_CALLS);
339         assertEquals(ZenPolicy.STATE_DISALLOW, policy.getPriorityCategoryCalls());
340         assertEquals(ZenPolicy.PEOPLE_TYPE_NONE, policy.getPriorityCallSenders());
341         assertAllVisualEffectsUnsetExcept(policy, -1);
342         assertProtoMatches(policy, policy.toProto());
343 
344         builder.allowCalls(ZenPolicy.PEOPLE_TYPE_UNSET);
345         policy = builder.build();
346         assertAllPriorityCategoriesUnsetExcept(policy, -1);
347         assertAllVisualEffectsUnsetExcept(policy, -1);
348         assertProtoMatches(policy, policy.toProto());
349     }
350 
351     @Test
testAllowRepeatCallers()352     public void testAllowRepeatCallers() {
353         ZenPolicy.Builder builder = new ZenPolicy.Builder();
354 
355         builder.allowRepeatCallers(true);
356         ZenPolicy policy = builder.build();
357         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_REPEAT_CALLERS);
358         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryRepeatCallers());
359         assertAllVisualEffectsUnsetExcept(policy, -1);
360         assertProtoMatches(policy, policy.toProto());
361 
362         builder.allowRepeatCallers(false);
363         policy = builder.build();
364         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_REPEAT_CALLERS);
365         assertEquals(ZenPolicy.STATE_DISALLOW, policy.getPriorityCategoryRepeatCallers());
366         assertAllVisualEffectsUnsetExcept(policy, -1);
367         assertProtoMatches(policy, policy.toProto());
368     }
369 
370     @Test
testAllowAlarms()371     public void testAllowAlarms() {
372         ZenPolicy.Builder builder = new ZenPolicy.Builder();
373 
374         builder.allowAlarms(true);
375         ZenPolicy policy = builder.build();
376         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_ALARMS);
377         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryAlarms());
378         assertAllVisualEffectsUnsetExcept(policy, -1);
379         assertProtoMatches(policy, policy.toProto());
380 
381         builder.allowAlarms(false);
382         policy = builder.build();
383         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_ALARMS);
384         assertEquals(ZenPolicy.STATE_DISALLOW, policy.getPriorityCategoryAlarms());
385         assertAllVisualEffectsUnsetExcept(policy, -1);
386         assertProtoMatches(policy, policy.toProto());
387     }
388 
389     @Test
testAllowMedia()390     public void testAllowMedia() {
391         ZenPolicy.Builder builder = new ZenPolicy.Builder();
392 
393         builder.allowMedia(true);
394         ZenPolicy policy = builder.build();
395         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_MEDIA);
396         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategoryMedia());
397         assertAllVisualEffectsUnsetExcept(policy, -1);
398         assertProtoMatches(policy, policy.toProto());
399 
400         builder.allowMedia(false);
401         policy = builder.build();
402         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_MEDIA);
403         assertEquals(ZenPolicy.STATE_DISALLOW, policy.getPriorityCategoryMedia());
404         assertAllVisualEffectsUnsetExcept(policy, -1);
405         assertProtoMatches(policy, policy.toProto());
406     }
407 
408     @Test
testAllowSystem()409     public void testAllowSystem() {
410         ZenPolicy.Builder builder = new ZenPolicy.Builder();
411 
412         builder.allowSystem(true);
413         ZenPolicy policy = builder.build();
414         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_SYSTEM);
415         assertEquals(ZenPolicy.STATE_ALLOW, policy.getPriorityCategorySystem());
416         assertAllVisualEffectsUnsetExcept(policy, -1);
417         assertProtoMatches(policy, policy.toProto());
418 
419         builder.allowSystem(false);
420         policy = builder.build();
421         assertAllPriorityCategoriesUnsetExcept(policy, ZenPolicy.PRIORITY_CATEGORY_SYSTEM);
422         assertEquals(ZenPolicy.STATE_DISALLOW, policy.getPriorityCategorySystem());
423         assertAllVisualEffectsUnsetExcept(policy, -1);
424         assertProtoMatches(policy, policy.toProto());
425     }
426 
427     @Test
tesShowFullScreenIntent()428     public void tesShowFullScreenIntent() {
429         ZenPolicy.Builder builder = new ZenPolicy.Builder();
430 
431         builder.showFullScreenIntent(true);
432         ZenPolicy policy = builder.build();
433         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_FULL_SCREEN_INTENT);
434         assertProtoMatches(policy, policy.toProto());
435 
436         builder.showFullScreenIntent(false);
437         policy = builder.build();
438         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_FULL_SCREEN_INTENT);
439         assertProtoMatches(policy, policy.toProto());
440     }
441 
442     @Test
tesShowLights()443     public void tesShowLights() {
444         ZenPolicy.Builder builder = new ZenPolicy.Builder();
445 
446         builder.showLights(true);
447         ZenPolicy policy = builder.build();
448         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_LIGHTS);
449         assertProtoMatches(policy, policy.toProto());
450 
451         builder.showLights(false);
452         policy = builder.build();
453         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_LIGHTS);
454         assertProtoMatches(policy, policy.toProto());
455     }
456 
457     @Test
tesShowPeeking()458     public void tesShowPeeking() {
459         ZenPolicy.Builder builder = new ZenPolicy.Builder();
460 
461         builder.showPeeking(true);
462         ZenPolicy policy = builder.build();
463         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_PEEK);
464         assertProtoMatches(policy, policy.toProto());
465 
466         builder.showPeeking(false);
467         policy = builder.build();
468         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_PEEK);
469         assertProtoMatches(policy, policy.toProto());
470     }
471 
472     @Test
tesShowStatusBarIcons()473     public void tesShowStatusBarIcons() {
474         ZenPolicy.Builder builder = new ZenPolicy.Builder();
475 
476         builder.showStatusBarIcons(true);
477         ZenPolicy policy = builder.build();
478         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_STATUS_BAR);
479         assertProtoMatches(policy, policy.toProto());
480 
481         builder.showStatusBarIcons(false);
482         policy = builder.build();
483         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_STATUS_BAR);
484         assertProtoMatches(policy, policy.toProto());
485     }
486 
487     @Test
tesShowBadges()488     public void tesShowBadges() {
489         ZenPolicy.Builder builder = new ZenPolicy.Builder();
490 
491         builder.showBadges(true);
492         ZenPolicy policy = builder.build();
493         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_BADGE);
494         assertProtoMatches(policy, policy.toProto());
495 
496         builder.showBadges(false);
497         policy = builder.build();
498         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_BADGE);
499         assertProtoMatches(policy, policy.toProto());
500     }
501 
502     @Test
tesShowInAmbientDisplay()503     public void tesShowInAmbientDisplay() {
504         ZenPolicy.Builder builder = new ZenPolicy.Builder();
505 
506         builder.showInAmbientDisplay(true);
507         ZenPolicy policy = builder.build();
508         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_AMBIENT);
509         assertProtoMatches(policy, policy.toProto());
510 
511         builder.showInAmbientDisplay(false);
512         policy = builder.build();
513         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_AMBIENT);
514         assertProtoMatches(policy, policy.toProto());
515     }
516 
517     @Test
tesShowInNotificationList()518     public void tesShowInNotificationList() {
519         ZenPolicy.Builder builder = new ZenPolicy.Builder();
520 
521         builder.showInNotificationList(true);
522         ZenPolicy policy = builder.build();
523         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_NOTIFICATION_LIST);
524         assertProtoMatches(policy, policy.toProto());
525 
526         builder.showInNotificationList(false);
527         policy = builder.build();
528         assertAllVisualEffectsUnsetExcept(policy, ZenPolicy.VISUAL_EFFECT_NOTIFICATION_LIST);
529         assertProtoMatches(policy, policy.toProto());
530     }
531 
532     @Test
testTooLongLists_fromParcel()533     public void testTooLongLists_fromParcel() {
534         ArrayList<Integer> longList = new ArrayList<Integer>(50);
535         for (int i = 0; i < 50; i++) {
536             longList.add(ZenPolicy.STATE_UNSET);
537         }
538 
539         ZenPolicy.Builder builder = new ZenPolicy.Builder();
540         ZenPolicy policy = builder.build();
541 
542         try {
543             Field priorityCategories = Class.forName(CLASS).getDeclaredField(
544                     "mPriorityCategories");
545             priorityCategories.setAccessible(true);
546             priorityCategories.set(policy, longList);
547 
548             Field visualEffects = Class.forName(CLASS).getDeclaredField("mVisualEffects");
549             visualEffects.setAccessible(true);
550             visualEffects.set(policy, longList);
551         } catch (NoSuchFieldException e) {
552             fail(e.toString());
553         } catch (ClassNotFoundException e) {
554             fail(e.toString());
555         } catch (IllegalAccessException e) {
556             fail(e.toString());
557         }
558 
559         Parcel parcel = Parcel.obtain();
560         policy.writeToParcel(parcel, 0);
561         parcel.setDataPosition(0);
562 
563         ZenPolicy fromParcel = ZenPolicy.CREATOR.createFromParcel(parcel);
564 
565         // Confirm that all the fields are accessible and UNSET
566         assertAllPriorityCategoriesUnsetExcept(fromParcel, -1);
567         assertAllVisualEffectsUnsetExcept(fromParcel, -1);
568 
569         // Because we don't access the lists directly, we also need to use reflection to make sure
570         // the lists are the right length.
571         try {
572             Field priorityCategories = Class.forName(CLASS).getDeclaredField(
573                     "mPriorityCategories");
574             priorityCategories.setAccessible(true);
575             ArrayList<Integer> pcList = (ArrayList<Integer>) priorityCategories.get(fromParcel);
576             assertEquals(ZenPolicy.NUM_PRIORITY_CATEGORIES, pcList.size());
577 
578 
579             Field visualEffects = Class.forName(CLASS).getDeclaredField("mVisualEffects");
580             visualEffects.setAccessible(true);
581             ArrayList<Integer> veList = (ArrayList<Integer>) visualEffects.get(fromParcel);
582             assertEquals(ZenPolicy.NUM_VISUAL_EFFECTS, veList.size());
583         } catch (NoSuchFieldException e) {
584             fail(e.toString());
585         } catch (ClassNotFoundException e) {
586             fail(e.toString());
587         } catch (IllegalAccessException e) {
588             fail(e.toString());
589         }
590     }
591 
assertAllPriorityCategoriesUnsetExcept(ZenPolicy policy, int except)592     private void assertAllPriorityCategoriesUnsetExcept(ZenPolicy policy, int except) {
593         if (except != ZenPolicy.PRIORITY_CATEGORY_REMINDERS) {
594             assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategoryReminders());
595         }
596 
597         if (except != ZenPolicy.PRIORITY_CATEGORY_EVENTS) {
598             assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategoryEvents());
599         }
600 
601         if (except != ZenPolicy.PRIORITY_CATEGORY_MESSAGES) {
602             assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategoryMessages());
603         }
604 
605         if (except != ZenPolicy.PRIORITY_CATEGORY_CALLS) {
606             assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategoryCalls());
607         }
608 
609         if (except != ZenPolicy.PRIORITY_CATEGORY_REPEAT_CALLERS) {
610             assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategoryRepeatCallers());
611         }
612 
613         if (except != ZenPolicy.PRIORITY_CATEGORY_ALARMS) {
614             assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategoryAlarms());
615         }
616 
617         if (except != ZenPolicy.PRIORITY_CATEGORY_MEDIA) {
618             assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategoryMedia());
619         }
620 
621         if (except != ZenPolicy.PRIORITY_CATEGORY_SYSTEM) {
622             assertEquals(ZenPolicy.STATE_UNSET, policy.getPriorityCategorySystem());
623         }
624     }
625 
assertAllVisualEffectsUnsetExcept(ZenPolicy policy, int except)626     private void assertAllVisualEffectsUnsetExcept(ZenPolicy policy, int except) {
627         if (except != ZenPolicy.VISUAL_EFFECT_FULL_SCREEN_INTENT) {
628             assertEquals(ZenPolicy.STATE_UNSET, policy.getVisualEffectFullScreenIntent());
629         }
630 
631         if (except != ZenPolicy.VISUAL_EFFECT_LIGHTS) {
632             assertEquals(ZenPolicy.STATE_UNSET, policy.getVisualEffectLights());
633         }
634 
635         if (except != ZenPolicy.VISUAL_EFFECT_PEEK) {
636             assertEquals(ZenPolicy.STATE_UNSET, policy.getVisualEffectPeek());
637         }
638 
639         if (except != ZenPolicy.VISUAL_EFFECT_STATUS_BAR) {
640             assertEquals(ZenPolicy.STATE_UNSET, policy.getVisualEffectStatusBar());
641         }
642 
643         if (except != ZenPolicy.VISUAL_EFFECT_BADGE) {
644             assertEquals(ZenPolicy.STATE_UNSET, policy.getVisualEffectBadge());
645         }
646 
647         if (except != ZenPolicy.VISUAL_EFFECT_AMBIENT) {
648             assertEquals(ZenPolicy.STATE_UNSET, policy.getVisualEffectAmbient());
649         }
650 
651         if (except != ZenPolicy.VISUAL_EFFECT_NOTIFICATION_LIST) {
652             assertEquals(ZenPolicy.STATE_UNSET, policy.getVisualEffectNotificationList());
653         }
654     }
655 
assertProtoMatches(ZenPolicy policy, byte[] bytes)656     private void assertProtoMatches(ZenPolicy policy, byte[] bytes) {
657         try {
658             DNDPolicyProto proto = DNDPolicyProto.parseFrom(bytes);
659 
660             assertEquals(policy.getPriorityCategoryCalls(), proto.calls);
661             assertEquals(policy.getPriorityCategoryRepeatCallers(), proto.repeatCallers);
662             assertEquals(policy.getPriorityCategoryMessages(), proto.messages);
663             assertEquals(policy.getPriorityCategoryConversations(), proto.conversations);
664             assertEquals(policy.getPriorityCategoryReminders(), proto.reminders);
665             assertEquals(policy.getPriorityCategoryEvents(), proto.events);
666             assertEquals(policy.getPriorityCategoryAlarms(), proto.alarms);
667             assertEquals(policy.getPriorityCategoryMedia(), proto.media);
668             assertEquals(policy.getPriorityCategorySystem(), proto.system);
669 
670             assertEquals(policy.getVisualEffectFullScreenIntent(), proto.fullscreen);
671             assertEquals(policy.getVisualEffectLights(), proto.lights);
672             assertEquals(policy.getVisualEffectPeek(), proto.peek);
673             assertEquals(policy.getVisualEffectStatusBar(), proto.statusBar);
674             assertEquals(policy.getVisualEffectBadge(), proto.badge);
675             assertEquals(policy.getVisualEffectAmbient(), proto.ambient);
676             assertEquals(policy.getVisualEffectNotificationList(), proto.notificationList);
677 
678             assertEquals(policy.getPriorityCallSenders(), proto.allowCallsFrom);
679             assertEquals(policy.getPriorityMessageSenders(), proto.allowMessagesFrom);
680             assertEquals(policy.getPriorityConversationSenders(), proto.allowConversationsFrom);
681         } catch (InvalidProtocolBufferNanoException e) {
682             fail("could not parse proto bytes");
683         }
684 
685     }
686 }
687