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