blob: 95bfa44b5f268592aee07a413096b38075e81a3e [file] [log] [blame]
Julia Reynoldsc861a3d2018-02-15 10:34:49 -05001/*
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 distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.notification;
18
19import static android.app.NotificationManager.IMPORTANCE_LOW;
Julia Reynoldsccc6ae62018-03-01 16:24:49 -050020import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST;
21import static android.app.NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK;
Julia Reynoldsc861a3d2018-02-15 10:34:49 -050022
23import static junit.framework.Assert.assertEquals;
24import static junit.framework.Assert.assertFalse;
25import static junit.framework.Assert.assertTrue;
26
27import static org.mockito.ArgumentMatchers.any;
28import static org.mockito.Mockito.when;
29
30import android.app.Notification;
31import android.app.NotificationChannel;
32import android.app.NotificationManager;
33import android.os.UserHandle;
34import android.service.notification.StatusBarNotification;
35
36import com.android.server.UiServiceTestCase;
37
38import org.junit.Before;
39import org.junit.Test;
40import org.mockito.Mock;
41import org.mockito.MockitoAnnotations;
42
43public class ZenModeExtractorTest extends UiServiceTestCase {
44
45 @Mock
46 ZenModeHelper mZenModeHelper;
47
48 @Before
49 public void setUp() {
50 MockitoAnnotations.initMocks(this);
51 }
52
53 @Test
54 public void testExtractIntercepted() {
55 ZenModeExtractor extractor = new ZenModeExtractor();
56 extractor.setZenHelper(mZenModeHelper);
57 NotificationRecord r = generateRecord();
58
59 assertFalse(r.isIntercepted());
60
61 when(mZenModeHelper.shouldIntercept(any())).thenReturn(true);
Beverlyff2df9b2018-10-10 16:54:10 -040062 when(mZenModeHelper.getConsolidatedNotificationPolicy()).thenReturn(
Julia Reynoldsccc6ae62018-03-01 16:24:49 -050063 new NotificationManager.Policy(0,0,0));
Julia Reynoldsc861a3d2018-02-15 10:34:49 -050064
65 extractor.process(r);
66
67 assertTrue(r.isIntercepted());
68 }
69
70 @Test
71 public void testExtractVisualDisturbancesNotIntercepted() {
72 ZenModeExtractor extractor = new ZenModeExtractor();
73 extractor.setZenHelper(mZenModeHelper);
74 NotificationRecord r = generateRecord();
75
76 when(mZenModeHelper.shouldIntercept(any())).thenReturn(false);
Beverlyff2df9b2018-10-10 16:54:10 -040077 when(mZenModeHelper.getConsolidatedNotificationPolicy()).thenReturn(
Julia Reynoldsccc6ae62018-03-01 16:24:49 -050078 new NotificationManager.Policy(0,0,0));
Julia Reynoldsc861a3d2018-02-15 10:34:49 -050079
80 extractor.process(r);
81
82 assertEquals(0, r.getSuppressedVisualEffects());
83 }
84
85 @Test
86 public void testExtractVisualDisturbancesIntercepted() {
87 ZenModeExtractor extractor = new ZenModeExtractor();
88 extractor.setZenHelper(mZenModeHelper);
89 NotificationRecord r = generateRecord();
90
91 when(mZenModeHelper.shouldIntercept(any())).thenReturn(true);
Beverlyff2df9b2018-10-10 16:54:10 -040092 when(mZenModeHelper.getConsolidatedNotificationPolicy()).thenReturn(
Julia Reynoldsccc6ae62018-03-01 16:24:49 -050093 new NotificationManager.Policy(0,0,0, SUPPRESSED_EFFECT_PEEK
94 | SUPPRESSED_EFFECT_NOTIFICATION_LIST));
Julia Reynoldsc861a3d2018-02-15 10:34:49 -050095
96 extractor.process(r);
97
Julia Reynoldsccc6ae62018-03-01 16:24:49 -050098 assertEquals(NotificationManager.Policy.SUPPRESSED_EFFECT_PEEK
99 | NotificationManager.Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST,
Julia Reynoldsc861a3d2018-02-15 10:34:49 -0500100 r.getSuppressedVisualEffects());
101 }
102
103 private NotificationRecord generateRecord() {
104 NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_LOW);
105 final Notification.Builder builder = new Notification.Builder(getContext())
106 .setContentTitle("foo")
107 .setSmallIcon(android.R.drawable.sym_def_app_icon);
108 Notification n = builder.build();
109 StatusBarNotification sbn = new StatusBarNotification("", "", 0, "", 0,
110 0, n, UserHandle.ALL, null, System.currentTimeMillis());
111 return new NotificationRecord(getContext(), sbn, channel);
112 }
113}