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