Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 com.android.server.notification; |
| 18 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 19 | import static android.service.notification.NotificationListenerService.Ranking |
| 20 | .USER_SENTIMENT_NEGATIVE; |
| 21 | import static android.service.notification.NotificationListenerService.Ranking |
| 22 | .USER_SENTIMENT_NEUTRAL; |
| 23 | import static android.service.notification.NotificationListenerService.Ranking |
| 24 | .USER_SENTIMENT_POSITIVE; |
| 25 | |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 26 | import static org.junit.Assert.assertEquals; |
Julia Reynolds | 94a38b3 | 2018-04-20 13:33:36 -0400 | [diff] [blame] | 27 | import static org.junit.Assert.assertNotNull; |
| 28 | import static org.mockito.ArgumentMatchers.any; |
| 29 | import static org.mockito.ArgumentMatchers.anyInt; |
| 30 | import static org.mockito.Mockito.mock; |
| 31 | import static org.mockito.Mockito.when; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 32 | |
Julia Reynolds | 94a38b3 | 2018-04-20 13:33:36 -0400 | [diff] [blame] | 33 | import android.app.INotificationManager; |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 34 | import android.app.Notification; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 35 | import android.app.NotificationChannel; |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 36 | import android.app.PendingIntent; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 37 | import android.content.Intent; |
| 38 | import android.os.Binder; |
| 39 | import android.os.Bundle; |
| 40 | import android.os.IBinder; |
| 41 | import android.service.notification.NotificationListenerService; |
| 42 | import android.service.notification.NotificationListenerService.Ranking; |
| 43 | import android.service.notification.NotificationRankingUpdate; |
| 44 | import android.service.notification.SnoozeCriterion; |
| 45 | import android.support.test.runner.AndroidJUnit4; |
| 46 | import android.test.suitebuilder.annotation.SmallTest; |
| 47 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 48 | import com.android.server.UiServiceTestCase; |
| 49 | |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 50 | import org.junit.Test; |
| 51 | import org.junit.runner.RunWith; |
| 52 | |
| 53 | import java.util.ArrayList; |
| 54 | import java.util.List; |
| 55 | |
| 56 | @SmallTest |
| 57 | @RunWith(AndroidJUnit4.class) |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 58 | public class NotificationListenerServiceTest extends UiServiceTestCase { |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 59 | |
| 60 | private String[] mKeys = new String[] { "key", "key1", "key2", "key3"}; |
| 61 | |
| 62 | @Test |
Julia Reynolds | 94a38b3 | 2018-04-20 13:33:36 -0400 | [diff] [blame] | 63 | public void testGetActiveNotifications_notNull() throws Exception { |
| 64 | TestListenerService service = new TestListenerService(); |
| 65 | INotificationManager noMan = service.getNoMan(); |
| 66 | when(noMan.getActiveNotificationsFromListener(any(), any(), anyInt())).thenReturn(null); |
| 67 | |
| 68 | assertNotNull(service.getActiveNotifications()); |
| 69 | assertNotNull(service.getActiveNotifications(NotificationListenerService.TRIM_FULL)); |
| 70 | assertNotNull(service.getActiveNotifications(new String[0])); |
| 71 | assertNotNull(service.getActiveNotifications( |
| 72 | new String[0], NotificationListenerService.TRIM_LIGHT)); |
| 73 | } |
| 74 | |
| 75 | @Test |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 76 | public void testRanking() throws Exception { |
| 77 | TestListenerService service = new TestListenerService(); |
| 78 | service.applyUpdateLocked(generateUpdate()); |
| 79 | for (int i = 0; i < mKeys.length; i++) { |
| 80 | String key = mKeys[i]; |
| 81 | Ranking ranking = new Ranking(); |
| 82 | service.getCurrentRanking().getRanking(key, ranking); |
| 83 | assertEquals(getVisibilityOverride(i), ranking.getVisibilityOverride()); |
| 84 | assertEquals(getOverrideGroupKey(key), ranking.getOverrideGroupKey()); |
| 85 | assertEquals(!isIntercepted(i), ranking.matchesInterruptionFilter()); |
| 86 | assertEquals(getSuppressedVisualEffects(i), ranking.getSuppressedVisualEffects()); |
| 87 | assertEquals(getImportance(i), ranking.getImportance()); |
| 88 | assertEquals(getExplanation(key), ranking.getImportanceExplanation()); |
| 89 | assertEquals(getChannel(key, i), ranking.getChannel()); |
| 90 | assertEquals(getPeople(key, i), ranking.getAdditionalPeople()); |
| 91 | assertEquals(getSnoozeCriteria(key, i), ranking.getSnoozeCriteria()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 92 | assertEquals(getShowBadge(i), ranking.canShowBadge()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 93 | assertEquals(getUserSentiment(i), ranking.getUserSentiment()); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 94 | assertEquals(getHidden(i), ranking.isSuspended()); |
Gus Prevas | a322649 | 2018-10-23 11:10:09 -0400 | [diff] [blame] | 95 | assertEquals(audiblyAlerted(i), ranking.audiblyAlerted()); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 96 | assertActionsEqual(getSmartActions(key, i), ranking.getSmartActions()); |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 97 | assertEquals(getSmartReplies(key, i), ranking.getSmartReplies()); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
| 101 | private NotificationRankingUpdate generateUpdate() { |
| 102 | List<String> interceptedKeys = new ArrayList<>(); |
| 103 | Bundle visibilityOverrides = new Bundle(); |
| 104 | Bundle overrideGroupKeys = new Bundle(); |
| 105 | Bundle suppressedVisualEffects = new Bundle(); |
| 106 | Bundle explanation = new Bundle(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 107 | Bundle channels = new Bundle(); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 108 | Bundle overridePeople = new Bundle(); |
| 109 | Bundle snoozeCriteria = new Bundle(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 110 | Bundle showBadge = new Bundle(); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 111 | int[] importance = new int[mKeys.length]; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 112 | Bundle userSentiment = new Bundle(); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 113 | Bundle mHidden = new Bundle(); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 114 | Bundle smartActions = new Bundle(); |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 115 | Bundle smartReplies = new Bundle(); |
Gus Prevas | a322649 | 2018-10-23 11:10:09 -0400 | [diff] [blame] | 116 | Bundle audiblyAlerted = new Bundle(); |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame^] | 117 | Bundle noisy = new Bundle(); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 118 | |
| 119 | for (int i = 0; i < mKeys.length; i++) { |
| 120 | String key = mKeys[i]; |
| 121 | visibilityOverrides.putInt(key, getVisibilityOverride(i)); |
| 122 | overrideGroupKeys.putString(key, getOverrideGroupKey(key)); |
| 123 | if (isIntercepted(i)) { |
| 124 | interceptedKeys.add(key); |
| 125 | } |
| 126 | suppressedVisualEffects.putInt(key, getSuppressedVisualEffects(i)); |
| 127 | importance[i] = getImportance(i); |
| 128 | explanation.putString(key, getExplanation(key)); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 129 | channels.putParcelable(key, getChannel(key, i)); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 130 | overridePeople.putStringArrayList(key, getPeople(key, i)); |
| 131 | snoozeCriteria.putParcelableArrayList(key, getSnoozeCriteria(key, i)); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 132 | showBadge.putBoolean(key, getShowBadge(i)); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 133 | userSentiment.putInt(key, getUserSentiment(i)); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 134 | mHidden.putBoolean(key, getHidden(i)); |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 135 | smartActions.putParcelableArrayList(key, getSmartActions(key, i)); |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 136 | smartReplies.putCharSequenceArrayList(key, getSmartReplies(key, i)); |
Gus Prevas | a322649 | 2018-10-23 11:10:09 -0400 | [diff] [blame] | 137 | audiblyAlerted.putBoolean(key, audiblyAlerted(i)); |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame^] | 138 | noisy.putBoolean(key, getNoisy(i)); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 139 | } |
| 140 | NotificationRankingUpdate update = new NotificationRankingUpdate(mKeys, |
| 141 | interceptedKeys.toArray(new String[0]), visibilityOverrides, |
| 142 | suppressedVisualEffects, importance, explanation, overrideGroupKeys, |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 143 | channels, overridePeople, snoozeCriteria, showBadge, userSentiment, mHidden, |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame^] | 144 | smartActions, smartReplies, audiblyAlerted, noisy); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 145 | return update; |
| 146 | } |
| 147 | |
| 148 | private int getVisibilityOverride(int index) { |
| 149 | return index * 9; |
| 150 | } |
| 151 | |
| 152 | private String getOverrideGroupKey(String key) { |
| 153 | return key + key; |
| 154 | } |
| 155 | |
| 156 | private boolean isIntercepted(int index) { |
| 157 | return index % 2 == 0; |
| 158 | } |
| 159 | |
| 160 | private int getSuppressedVisualEffects(int index) { |
| 161 | return index * 2; |
| 162 | } |
| 163 | |
| 164 | private int getImportance(int index) { |
| 165 | return index; |
| 166 | } |
| 167 | |
| 168 | private String getExplanation(String key) { |
| 169 | return key + "explain"; |
| 170 | } |
| 171 | |
| 172 | private NotificationChannel getChannel(String key, int index) { |
| 173 | return new NotificationChannel(key, key, getImportance(index)); |
| 174 | } |
| 175 | |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 176 | private boolean getShowBadge(int index) { |
| 177 | return index % 3 == 0; |
| 178 | } |
| 179 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 180 | private int getUserSentiment(int index) { |
| 181 | switch(index % 3) { |
| 182 | case 0: |
| 183 | return USER_SENTIMENT_NEGATIVE; |
| 184 | case 1: |
| 185 | return USER_SENTIMENT_NEUTRAL; |
| 186 | case 2: |
| 187 | return USER_SENTIMENT_POSITIVE; |
| 188 | } |
| 189 | return USER_SENTIMENT_NEUTRAL; |
| 190 | } |
| 191 | |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 192 | private boolean getHidden(int index) { |
| 193 | return index % 2 == 0; |
| 194 | } |
| 195 | |
Gus Prevas | a322649 | 2018-10-23 11:10:09 -0400 | [diff] [blame] | 196 | private boolean audiblyAlerted(int index) { |
| 197 | return index < 2; |
| 198 | } |
| 199 | |
Gus Prevas | 9abc506 | 2018-10-31 16:11:04 -0400 | [diff] [blame^] | 200 | private boolean getNoisy(int index) { |
| 201 | return index < 1; |
| 202 | } |
| 203 | |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 204 | private ArrayList<String> getPeople(String key, int index) { |
| 205 | ArrayList<String> people = new ArrayList<>(); |
| 206 | for (int i = 0; i < index; i++) { |
| 207 | people.add(i + key); |
| 208 | } |
| 209 | return people; |
| 210 | } |
| 211 | |
| 212 | private ArrayList<SnoozeCriterion> getSnoozeCriteria(String key, int index) { |
| 213 | ArrayList<SnoozeCriterion> snooze = new ArrayList<>(); |
| 214 | for (int i = 0; i < index; i++) { |
| 215 | snooze.add(new SnoozeCriterion(key + i, getExplanation(key), key)); |
| 216 | } |
| 217 | return snooze; |
| 218 | } |
| 219 | |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 220 | private ArrayList<Notification.Action> getSmartActions(String key, int index) { |
| 221 | ArrayList<Notification.Action> actions = new ArrayList<>(); |
| 222 | for (int i = 0; i < index; i++) { |
| 223 | PendingIntent intent = PendingIntent.getBroadcast( |
| 224 | getContext(), |
| 225 | index /*requestCode*/, |
| 226 | new Intent("ACTION_" + key), |
| 227 | 0 /*flags*/); |
| 228 | actions.add(new Notification.Action.Builder(null /*icon*/, key, intent).build()); |
| 229 | } |
| 230 | return actions; |
| 231 | } |
| 232 | |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 233 | private ArrayList<CharSequence> getSmartReplies(String key, int index) { |
| 234 | ArrayList<CharSequence> choices = new ArrayList<>(); |
| 235 | for (int i = 0; i < index; i++) { |
| 236 | choices.add("choice_" + key + "_" + i); |
| 237 | } |
| 238 | return choices; |
| 239 | } |
| 240 | |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 241 | private void assertActionsEqual( |
| 242 | List<Notification.Action> expecteds, List<Notification.Action> actuals) { |
| 243 | assertEquals(expecteds.size(), actuals.size()); |
| 244 | for (int i = 0; i < expecteds.size(); i++) { |
| 245 | Notification.Action expected = expecteds.get(i); |
| 246 | Notification.Action actual = actuals.get(i); |
| 247 | assertEquals(expected.title, actual.title); |
| 248 | } |
| 249 | } |
| 250 | |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 251 | public static class TestListenerService extends NotificationListenerService { |
| 252 | private final IBinder binder = new LocalBinder(); |
| 253 | |
| 254 | public TestListenerService() { |
Julia Reynolds | 94a38b3 | 2018-04-20 13:33:36 -0400 | [diff] [blame] | 255 | mWrapper = mock(NotificationListenerWrapper.class); |
| 256 | mNoMan = mock(INotificationManager.class); |
| 257 | } |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 258 | |
Julia Reynolds | 94a38b3 | 2018-04-20 13:33:36 -0400 | [diff] [blame] | 259 | INotificationManager getNoMan() { |
| 260 | return mNoMan; |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public IBinder onBind(Intent intent) { |
| 265 | super.onBind(intent); |
| 266 | return binder; |
| 267 | } |
| 268 | |
| 269 | public class LocalBinder extends Binder { |
| 270 | TestListenerService getService() { |
| 271 | return TestListenerService.this; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | } |