blob: 273a9e66e55ba1a3b59a3c53a50a8db4bd7149f2 [file] [log] [blame]
Julia Reynolds4509ce72019-01-31 13:12:43 -05001/*
2 * Copyright (C) 2019 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 */
16package com.android.server.notification;
17
18import static android.app.NotificationManager.IMPORTANCE_HIGH;
19import static android.app.NotificationManager.IMPORTANCE_UNSPECIFIED;
Julia Reynolds4509ce72019-01-31 13:12:43 -050020
21import static junit.framework.Assert.assertFalse;
22import static junit.framework.Assert.assertTrue;
23
24import static org.mockito.Mockito.when;
25
26import android.app.ActivityManager;
27import android.app.Notification;
28import android.app.Notification.Builder;
29import android.app.NotificationChannel;
30import android.os.UserHandle;
31import android.service.notification.StatusBarNotification;
Julia Reynolds4509ce72019-01-31 13:12:43 -050032import android.test.suitebuilder.annotation.SmallTest;
33
Brett Chabot84151d92019-02-27 15:37:59 -080034import androidx.test.runner.AndroidJUnit4;
35
Julia Reynolds4509ce72019-01-31 13:12:43 -050036import com.android.server.UiServiceTestCase;
37
38import org.junit.Before;
39import org.junit.Test;
40import org.junit.runner.RunWith;
41import org.mockito.Mock;
42import org.mockito.MockitoAnnotations;
43
44@SmallTest
45@RunWith(AndroidJUnit4.class)
46public class BubbleExtractorTest extends UiServiceTestCase {
47
48 @Mock RankingConfig mConfig;
49
50 private String mPkg = "com.android.server.notification";
51 private int mId = 1001;
52 private String mTag = null;
53 private int mUid = 1000;
54 private int mPid = 2000;
55 private UserHandle mUser = UserHandle.of(ActivityManager.getCurrentUser());
56
57 @Before
58 public void setUp() {
59 MockitoAnnotations.initMocks(this);
60 }
61
62 private NotificationRecord getNotificationRecord(boolean allow, int importanceHigh) {
63 NotificationChannel channel = new NotificationChannel("a", "a", importanceHigh);
64 channel.setAllowBubbles(allow);
65 when(mConfig.getNotificationChannel(mPkg, mUid, "a", false)).thenReturn(channel);
66
67 final Builder builder = new Builder(getContext())
68 .setContentTitle("foo")
69 .setSmallIcon(android.R.drawable.sym_def_app_icon)
70 .setPriority(Notification.PRIORITY_HIGH)
71 .setDefaults(Notification.DEFAULT_SOUND);
72
73 Notification n = builder.build();
74 StatusBarNotification sbn = new StatusBarNotification(mPkg, mPkg, mId, mTag, mUid,
75 mPid, n, mUser, null, System.currentTimeMillis());
76 NotificationRecord r = new NotificationRecord(getContext(), sbn, channel);
77 return r;
78 }
79
80 //
81 // Tests
82 //
83
84 @Test
85 public void testAppYesChannelNo() {
86 BubbleExtractor extractor = new BubbleExtractor();
87 extractor.setConfig(mConfig);
88
89 when(mConfig.bubblesEnabled(mUser)).thenReturn(true);
90 when(mConfig.areBubblesAllowed(mPkg, mUid)).thenReturn(true);
91 NotificationRecord r = getNotificationRecord(false, IMPORTANCE_UNSPECIFIED);
92
93 extractor.process(r);
94
95 assertFalse(r.canBubble());
96 }
97
98 @Test
99 public void testAppNoChannelYes() throws Exception {
100 BubbleExtractor extractor = new BubbleExtractor();
101 extractor.setConfig(mConfig);
102
103 when(mConfig.bubblesEnabled(mUser)).thenReturn(true);
104 when(mConfig.areBubblesAllowed(mPkg, mUid)).thenReturn(false);
105 NotificationRecord r = getNotificationRecord(true, IMPORTANCE_HIGH);
106
107 extractor.process(r);
108
109 assertFalse(r.canBubble());
110 }
111
112 @Test
113 public void testAppYesChannelYes() {
114 BubbleExtractor extractor = new BubbleExtractor();
115 extractor.setConfig(mConfig);
116
117 when(mConfig.bubblesEnabled(mUser)).thenReturn(true);
118 when(mConfig.areBubblesAllowed(mPkg, mUid)).thenReturn(true);
119 NotificationRecord r = getNotificationRecord(true, IMPORTANCE_UNSPECIFIED);
120
121 extractor.process(r);
122
123 assertTrue(r.canBubble());
124 }
125
126 @Test
127 public void testAppNoChannelNo() {
128 BubbleExtractor extractor = new BubbleExtractor();
129 extractor.setConfig(mConfig);
130
131 when(mConfig.bubblesEnabled(mUser)).thenReturn(true);
132 when(mConfig.areBubblesAllowed(mPkg, mUid)).thenReturn(false);
133 NotificationRecord r = getNotificationRecord(false, IMPORTANCE_UNSPECIFIED);
134
135 extractor.process(r);
136
137 assertFalse(r.canBubble());
138 }
139
140 @Test
141 public void testAppYesChannelYesUserNo() {
142 BubbleExtractor extractor = new BubbleExtractor();
143 extractor.setConfig(mConfig);
144
145 when(mConfig.bubblesEnabled(mUser)).thenReturn(false);
146 when(mConfig.areBubblesAllowed(mPkg, mUid)).thenReturn(true);
147 NotificationRecord r = getNotificationRecord(true, IMPORTANCE_HIGH);
148
149 extractor.process(r);
150
151 assertFalse(r.canBubble());
152 }
153}