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; |
| 27 | |
| 28 | import android.app.NotificationChannel; |
| 29 | import android.content.Intent; |
| 30 | import android.os.Binder; |
| 31 | import android.os.Bundle; |
| 32 | import android.os.IBinder; |
| 33 | import android.service.notification.NotificationListenerService; |
| 34 | import android.service.notification.NotificationListenerService.Ranking; |
| 35 | import android.service.notification.NotificationRankingUpdate; |
| 36 | import android.service.notification.SnoozeCriterion; |
| 37 | import android.support.test.runner.AndroidJUnit4; |
| 38 | import android.test.suitebuilder.annotation.SmallTest; |
| 39 | |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 40 | import com.android.server.UiServiceTestCase; |
| 41 | |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 42 | import org.junit.Test; |
| 43 | import org.junit.runner.RunWith; |
| 44 | |
| 45 | import java.util.ArrayList; |
| 46 | import java.util.List; |
| 47 | |
| 48 | @SmallTest |
| 49 | @RunWith(AndroidJUnit4.class) |
Jason Monk | 74f5e36 | 2017-12-06 08:56:33 -0500 | [diff] [blame] | 50 | public class NotificationListenerServiceTest extends UiServiceTestCase { |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 51 | |
| 52 | private String[] mKeys = new String[] { "key", "key1", "key2", "key3"}; |
| 53 | |
| 54 | @Test |
| 55 | public void testRanking() throws Exception { |
| 56 | TestListenerService service = new TestListenerService(); |
| 57 | service.applyUpdateLocked(generateUpdate()); |
| 58 | for (int i = 0; i < mKeys.length; i++) { |
| 59 | String key = mKeys[i]; |
| 60 | Ranking ranking = new Ranking(); |
| 61 | service.getCurrentRanking().getRanking(key, ranking); |
| 62 | assertEquals(getVisibilityOverride(i), ranking.getVisibilityOverride()); |
| 63 | assertEquals(getOverrideGroupKey(key), ranking.getOverrideGroupKey()); |
| 64 | assertEquals(!isIntercepted(i), ranking.matchesInterruptionFilter()); |
| 65 | assertEquals(getSuppressedVisualEffects(i), ranking.getSuppressedVisualEffects()); |
| 66 | assertEquals(getImportance(i), ranking.getImportance()); |
| 67 | assertEquals(getExplanation(key), ranking.getImportanceExplanation()); |
| 68 | assertEquals(getChannel(key, i), ranking.getChannel()); |
| 69 | assertEquals(getPeople(key, i), ranking.getAdditionalPeople()); |
| 70 | assertEquals(getSnoozeCriteria(key, i), ranking.getSnoozeCriteria()); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 71 | assertEquals(getShowBadge(i), ranking.canShowBadge()); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 72 | assertEquals(getUserSentiment(i), ranking.getUserSentiment()); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 73 | assertEquals(getHidden(i), ranking.isSuspended()); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | private NotificationRankingUpdate generateUpdate() { |
| 78 | List<String> interceptedKeys = new ArrayList<>(); |
| 79 | Bundle visibilityOverrides = new Bundle(); |
| 80 | Bundle overrideGroupKeys = new Bundle(); |
| 81 | Bundle suppressedVisualEffects = new Bundle(); |
| 82 | Bundle explanation = new Bundle(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 83 | Bundle channels = new Bundle(); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 84 | Bundle overridePeople = new Bundle(); |
| 85 | Bundle snoozeCriteria = new Bundle(); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 86 | Bundle showBadge = new Bundle(); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 87 | int[] importance = new int[mKeys.length]; |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 88 | Bundle userSentiment = new Bundle(); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 89 | Bundle mHidden = new Bundle(); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 90 | |
| 91 | for (int i = 0; i < mKeys.length; i++) { |
| 92 | String key = mKeys[i]; |
| 93 | visibilityOverrides.putInt(key, getVisibilityOverride(i)); |
| 94 | overrideGroupKeys.putString(key, getOverrideGroupKey(key)); |
| 95 | if (isIntercepted(i)) { |
| 96 | interceptedKeys.add(key); |
| 97 | } |
| 98 | suppressedVisualEffects.putInt(key, getSuppressedVisualEffects(i)); |
| 99 | importance[i] = getImportance(i); |
| 100 | explanation.putString(key, getExplanation(key)); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 101 | channels.putParcelable(key, getChannel(key, i)); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 102 | overridePeople.putStringArrayList(key, getPeople(key, i)); |
| 103 | snoozeCriteria.putParcelableArrayList(key, getSnoozeCriteria(key, i)); |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 104 | showBadge.putBoolean(key, getShowBadge(i)); |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 105 | userSentiment.putInt(key, getUserSentiment(i)); |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 106 | mHidden.putBoolean(key, getHidden(i)); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 107 | } |
| 108 | NotificationRankingUpdate update = new NotificationRankingUpdate(mKeys, |
| 109 | interceptedKeys.toArray(new String[0]), visibilityOverrides, |
| 110 | suppressedVisualEffects, importance, explanation, overrideGroupKeys, |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 111 | channels, overridePeople, snoozeCriteria, showBadge, userSentiment, mHidden); |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 112 | return update; |
| 113 | } |
| 114 | |
| 115 | private int getVisibilityOverride(int index) { |
| 116 | return index * 9; |
| 117 | } |
| 118 | |
| 119 | private String getOverrideGroupKey(String key) { |
| 120 | return key + key; |
| 121 | } |
| 122 | |
| 123 | private boolean isIntercepted(int index) { |
| 124 | return index % 2 == 0; |
| 125 | } |
| 126 | |
| 127 | private int getSuppressedVisualEffects(int index) { |
| 128 | return index * 2; |
| 129 | } |
| 130 | |
| 131 | private int getImportance(int index) { |
| 132 | return index; |
| 133 | } |
| 134 | |
| 135 | private String getExplanation(String key) { |
| 136 | return key + "explain"; |
| 137 | } |
| 138 | |
| 139 | private NotificationChannel getChannel(String key, int index) { |
| 140 | return new NotificationChannel(key, key, getImportance(index)); |
| 141 | } |
| 142 | |
Julia Reynolds | 924eed1 | 2017-01-19 09:52:07 -0500 | [diff] [blame] | 143 | private boolean getShowBadge(int index) { |
| 144 | return index % 3 == 0; |
| 145 | } |
| 146 | |
Julia Reynolds | 503ed94 | 2017-10-04 16:04:56 -0400 | [diff] [blame] | 147 | private int getUserSentiment(int index) { |
| 148 | switch(index % 3) { |
| 149 | case 0: |
| 150 | return USER_SENTIMENT_NEGATIVE; |
| 151 | case 1: |
| 152 | return USER_SENTIMENT_NEUTRAL; |
| 153 | case 2: |
| 154 | return USER_SENTIMENT_POSITIVE; |
| 155 | } |
| 156 | return USER_SENTIMENT_NEUTRAL; |
| 157 | } |
| 158 | |
Beverly | 5a20a5e | 2018-03-06 15:02:44 -0500 | [diff] [blame] | 159 | private boolean getHidden(int index) { |
| 160 | return index % 2 == 0; |
| 161 | } |
| 162 | |
Julia Reynolds | 22f02b3 | 2016-12-01 15:05:13 -0500 | [diff] [blame] | 163 | private ArrayList<String> getPeople(String key, int index) { |
| 164 | ArrayList<String> people = new ArrayList<>(); |
| 165 | for (int i = 0; i < index; i++) { |
| 166 | people.add(i + key); |
| 167 | } |
| 168 | return people; |
| 169 | } |
| 170 | |
| 171 | private ArrayList<SnoozeCriterion> getSnoozeCriteria(String key, int index) { |
| 172 | ArrayList<SnoozeCriterion> snooze = new ArrayList<>(); |
| 173 | for (int i = 0; i < index; i++) { |
| 174 | snooze.add(new SnoozeCriterion(key + i, getExplanation(key), key)); |
| 175 | } |
| 176 | return snooze; |
| 177 | } |
| 178 | |
| 179 | public static class TestListenerService extends NotificationListenerService { |
| 180 | private final IBinder binder = new LocalBinder(); |
| 181 | |
| 182 | public TestListenerService() { |
| 183 | |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public IBinder onBind(Intent intent) { |
| 188 | super.onBind(intent); |
| 189 | return binder; |
| 190 | } |
| 191 | |
| 192 | public class LocalBinder extends Binder { |
| 193 | TestListenerService getService() { |
| 194 | return TestListenerService.this; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | } |