Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 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 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 | |
| 17 | package android.ext.services.notification; |
| 18 | |
| 19 | import static android.app.NotificationManager.IMPORTANCE_DEFAULT; |
| 20 | import static android.app.NotificationManager.IMPORTANCE_HIGH; |
Julia Reynolds | 11e085a | 2019-02-05 15:49:56 -0500 | [diff] [blame] | 21 | import static android.app.NotificationManager.IMPORTANCE_LOW; |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 22 | import static android.app.NotificationManager.IMPORTANCE_MIN; |
| 23 | import static android.os.Process.FIRST_APPLICATION_UID; |
| 24 | |
| 25 | import static junit.framework.Assert.assertFalse; |
| 26 | import static junit.framework.Assert.assertTrue; |
| 27 | |
| 28 | import static org.junit.Assert.assertEquals; |
| 29 | import static org.mockito.Mockito.when; |
| 30 | |
| 31 | import android.app.Notification; |
| 32 | import android.app.NotificationChannel; |
| 33 | import android.os.Process; |
| 34 | import android.service.notification.StatusBarNotification; |
| 35 | import android.support.test.InstrumentationRegistry; |
| 36 | import android.support.test.runner.AndroidJUnit4; |
| 37 | import android.testing.TestableContext; |
| 38 | |
| 39 | import org.junit.Before; |
| 40 | import org.junit.Rule; |
| 41 | import org.junit.Test; |
| 42 | import org.junit.runner.RunWith; |
| 43 | import org.mockito.Mock; |
| 44 | import org.mockito.MockitoAnnotations; |
| 45 | |
| 46 | @RunWith(AndroidJUnit4.class) |
| 47 | public class NotificationCategorizerTest { |
| 48 | @Mock |
| 49 | private NotificationEntry mEntry; |
| 50 | @Mock |
| 51 | private StatusBarNotification mSbn; |
| 52 | |
| 53 | @Rule |
| 54 | public final TestableContext mContext = |
| 55 | new TestableContext(InstrumentationRegistry.getContext(), null); |
| 56 | |
| 57 | @Before |
| 58 | public void setUp() { |
| 59 | MockitoAnnotations.initMocks(this); |
| 60 | |
| 61 | when(mEntry.getSbn()).thenReturn(mSbn); |
| 62 | when(mSbn.getUid()).thenReturn(Process.myUid()); |
| 63 | when(mSbn.getPackageName()).thenReturn(mContext.getPackageName()); |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void testPeopleCategory() { |
| 68 | NotificationCategorizer nc = new NotificationCategorizer(); |
| 69 | |
| 70 | when(mEntry.getChannel()).thenReturn(new NotificationChannel("", "", IMPORTANCE_DEFAULT)); |
| 71 | when(mEntry.involvesPeople()).thenReturn(true); |
| 72 | |
| 73 | assertEquals(NotificationCategorizer.CATEGORY_PEOPLE, nc.getCategory(mEntry)); |
| 74 | assertFalse(nc.shouldSilence(NotificationCategorizer.CATEGORY_PEOPLE)); |
| 75 | } |
| 76 | |
| 77 | @Test |
| 78 | public void testMin() { |
| 79 | NotificationCategorizer nc = new NotificationCategorizer(); |
| 80 | |
| 81 | when(mEntry.getChannel()).thenReturn(new NotificationChannel("", "", IMPORTANCE_MIN)); |
| 82 | when(mEntry.involvesPeople()).thenReturn(true); |
| 83 | |
| 84 | assertEquals(NotificationCategorizer.CATEGORY_MIN, nc.getCategory(mEntry)); |
| 85 | assertTrue(nc.shouldSilence(NotificationCategorizer.CATEGORY_MIN)); |
| 86 | } |
| 87 | |
| 88 | @Test |
Julia Reynolds | 98f6e69 | 2019-01-29 16:31:56 -0500 | [diff] [blame] | 89 | public void testHigh() { |
| 90 | NotificationCategorizer nc = new NotificationCategorizer(); |
| 91 | |
| 92 | when(mEntry.getChannel()).thenReturn(new NotificationChannel("", "", IMPORTANCE_HIGH)); |
| 93 | |
| 94 | assertEquals(NotificationCategorizer.CATEGORY_HIGH, nc.getCategory(mEntry)); |
| 95 | assertFalse(nc.shouldSilence(NotificationCategorizer.CATEGORY_HIGH)); |
| 96 | } |
| 97 | |
| 98 | @Test |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 99 | public void testOngoingCategory() { |
| 100 | NotificationCategorizer nc = new NotificationCategorizer(); |
| 101 | |
| 102 | when(mEntry.getChannel()).thenReturn(new NotificationChannel("", "", IMPORTANCE_DEFAULT)); |
| 103 | when(mEntry.isOngoing()).thenReturn(true); |
| 104 | |
| 105 | assertEquals(NotificationCategorizer.CATEGORY_ONGOING, nc.getCategory(mEntry)); |
| 106 | assertTrue(nc.shouldSilence(NotificationCategorizer.CATEGORY_ONGOING)); |
| 107 | |
| 108 | when(mEntry.isOngoing()).thenReturn(false); |
| 109 | assertEquals(NotificationCategorizer.CATEGORY_EVERYTHING_ELSE, nc.getCategory(mEntry)); |
| 110 | assertTrue(nc.shouldSilence(NotificationCategorizer.CATEGORY_EVERYTHING_ELSE)); |
| 111 | } |
| 112 | |
| 113 | @Test |
| 114 | public void testAlarmCategory() { |
| 115 | NotificationCategorizer nc = new NotificationCategorizer(); |
| 116 | |
| 117 | when(mEntry.getChannel()).thenReturn(new NotificationChannel("", "", IMPORTANCE_DEFAULT)); |
| 118 | when(mEntry.isCategory(Notification.CATEGORY_ALARM)).thenReturn(true); |
| 119 | |
| 120 | assertEquals(NotificationCategorizer.CATEGORY_ALARM, nc.getCategory(mEntry)); |
| 121 | assertFalse(nc.shouldSilence(NotificationCategorizer.CATEGORY_ALARM)); |
| 122 | |
| 123 | when(mEntry.isCategory(Notification.CATEGORY_ALARM)).thenReturn(false); |
| 124 | assertEquals(NotificationCategorizer.CATEGORY_EVERYTHING_ELSE, nc.getCategory(mEntry)); |
| 125 | assertTrue(nc.shouldSilence(NotificationCategorizer.CATEGORY_EVERYTHING_ELSE)); |
| 126 | } |
| 127 | |
| 128 | @Test |
| 129 | public void testCallCategory() { |
| 130 | NotificationCategorizer nc = new NotificationCategorizer(); |
| 131 | |
| 132 | when(mEntry.getChannel()).thenReturn(new NotificationChannel("", "", IMPORTANCE_DEFAULT)); |
| 133 | when(mEntry.isCategory(Notification.CATEGORY_CALL)).thenReturn(true); |
| 134 | |
| 135 | assertEquals(NotificationCategorizer.CATEGORY_CALL, nc.getCategory(mEntry)); |
| 136 | assertFalse(nc.shouldSilence(NotificationCategorizer.CATEGORY_CALL)); |
| 137 | |
| 138 | when(mEntry.isCategory(Notification.CATEGORY_CALL)).thenReturn(false); |
| 139 | assertEquals(NotificationCategorizer.CATEGORY_EVERYTHING_ELSE, nc.getCategory(mEntry)); |
| 140 | assertTrue(nc.shouldSilence(NotificationCategorizer.CATEGORY_EVERYTHING_ELSE)); |
| 141 | } |
| 142 | |
| 143 | @Test |
| 144 | public void testReminderCategory() { |
| 145 | NotificationCategorizer nc = new NotificationCategorizer(); |
| 146 | |
| 147 | when(mEntry.getChannel()).thenReturn(new NotificationChannel("", "", IMPORTANCE_DEFAULT)); |
| 148 | when(mEntry.isCategory(Notification.CATEGORY_REMINDER)).thenReturn(true); |
| 149 | |
| 150 | assertEquals(NotificationCategorizer.CATEGORY_REMINDER, nc.getCategory(mEntry)); |
| 151 | assertFalse(nc.shouldSilence(NotificationCategorizer.CATEGORY_REMINDER)); |
| 152 | |
| 153 | when(mEntry.isCategory(Notification.CATEGORY_REMINDER)).thenReturn(false); |
| 154 | assertEquals(NotificationCategorizer.CATEGORY_EVERYTHING_ELSE, nc.getCategory(mEntry)); |
| 155 | assertTrue(nc.shouldSilence(NotificationCategorizer.CATEGORY_EVERYTHING_ELSE)); |
| 156 | } |
| 157 | |
| 158 | @Test |
| 159 | public void testEventCategory() { |
| 160 | NotificationCategorizer nc = new NotificationCategorizer(); |
| 161 | |
| 162 | when(mEntry.getChannel()).thenReturn(new NotificationChannel("", "", IMPORTANCE_DEFAULT)); |
| 163 | when(mEntry.isCategory(Notification.CATEGORY_EVENT)).thenReturn(true); |
| 164 | |
| 165 | assertEquals(NotificationCategorizer.CATEGORY_EVENT, nc.getCategory(mEntry)); |
| 166 | assertFalse(nc.shouldSilence(NotificationCategorizer.CATEGORY_EVENT)); |
| 167 | |
| 168 | when(mEntry.isCategory(Notification.CATEGORY_EVENT)).thenReturn(false); |
| 169 | assertEquals(NotificationCategorizer.CATEGORY_EVERYTHING_ELSE, nc.getCategory(mEntry)); |
| 170 | } |
| 171 | |
| 172 | @Test |
| 173 | public void testSystemCategory() { |
| 174 | NotificationCategorizer nc = new NotificationCategorizer(); |
| 175 | |
Julia Reynolds | 11e085a | 2019-02-05 15:49:56 -0500 | [diff] [blame] | 176 | when(mEntry.getChannel()).thenReturn(new NotificationChannel("", "", IMPORTANCE_DEFAULT)); |
| 177 | when(mEntry.getImportance()).thenReturn(IMPORTANCE_DEFAULT); |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 178 | when(mSbn.getUid()).thenReturn(FIRST_APPLICATION_UID - 1); |
| 179 | |
| 180 | assertEquals(NotificationCategorizer.CATEGORY_SYSTEM, nc.getCategory(mEntry)); |
| 181 | assertFalse(nc.shouldSilence(NotificationCategorizer.CATEGORY_SYSTEM)); |
| 182 | |
| 183 | when(mSbn.getUid()).thenReturn(FIRST_APPLICATION_UID); |
Julia Reynolds | 11e085a | 2019-02-05 15:49:56 -0500 | [diff] [blame] | 184 | assertEquals(NotificationCategorizer.CATEGORY_EVERYTHING_ELSE, nc.getCategory(mEntry)); |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | @Test |
| 188 | public void testSystemLowCategory() { |
| 189 | NotificationCategorizer nc = new NotificationCategorizer(); |
| 190 | |
Julia Reynolds | 11e085a | 2019-02-05 15:49:56 -0500 | [diff] [blame] | 191 | when(mEntry.getChannel()).thenReturn(new NotificationChannel("", "", IMPORTANCE_LOW)); |
| 192 | when(mEntry.getImportance()).thenReturn(IMPORTANCE_LOW); |
Julia Reynolds | 901bf28 | 2018-08-14 10:09:36 -0400 | [diff] [blame] | 193 | when(mSbn.getUid()).thenReturn(FIRST_APPLICATION_UID - 1); |
| 194 | |
| 195 | assertEquals(NotificationCategorizer.CATEGORY_SYSTEM_LOW, nc.getCategory(mEntry)); |
| 196 | assertTrue(nc.shouldSilence(NotificationCategorizer.CATEGORY_SYSTEM_LOW)); |
| 197 | |
| 198 | when(mSbn.getUid()).thenReturn(FIRST_APPLICATION_UID); |
| 199 | assertEquals(NotificationCategorizer.CATEGORY_EVERYTHING_ELSE, nc.getCategory(mEntry)); |
| 200 | } |
| 201 | } |