blob: 181fcebbb5369254dcb0acab6796231d417c7d47 [file] [log] [blame]
Julia Reynolds22f02b32016-12-01 15:05:13 -05001/*
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
17package com.android.server.notification;
18
Julia Reynolds503ed942017-10-04 16:04:56 -040019import static android.service.notification.NotificationListenerService.Ranking
20 .USER_SENTIMENT_NEGATIVE;
21import static android.service.notification.NotificationListenerService.Ranking
22 .USER_SENTIMENT_NEUTRAL;
23import static android.service.notification.NotificationListenerService.Ranking
24 .USER_SENTIMENT_POSITIVE;
25
Julia Reynolds22f02b32016-12-01 15:05:13 -050026import static org.junit.Assert.assertEquals;
27
28import android.app.NotificationChannel;
29import android.content.Intent;
30import android.os.Binder;
31import android.os.Bundle;
32import android.os.IBinder;
33import android.service.notification.NotificationListenerService;
34import android.service.notification.NotificationListenerService.Ranking;
35import android.service.notification.NotificationRankingUpdate;
36import android.service.notification.SnoozeCriterion;
37import android.support.test.runner.AndroidJUnit4;
38import android.test.suitebuilder.annotation.SmallTest;
39
Jason Monk74f5e362017-12-06 08:56:33 -050040import com.android.server.UiServiceTestCase;
41
Julia Reynolds22f02b32016-12-01 15:05:13 -050042import org.junit.Test;
43import org.junit.runner.RunWith;
44
45import java.util.ArrayList;
46import java.util.List;
47
48@SmallTest
49@RunWith(AndroidJUnit4.class)
Jason Monk74f5e362017-12-06 08:56:33 -050050public class NotificationListenerServiceTest extends UiServiceTestCase {
Julia Reynolds22f02b32016-12-01 15:05:13 -050051
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 Reynolds924eed12017-01-19 09:52:07 -050071 assertEquals(getShowBadge(i), ranking.canShowBadge());
Julia Reynolds503ed942017-10-04 16:04:56 -040072 assertEquals(getUserSentiment(i), ranking.getUserSentiment());
Beverly5a20a5e2018-03-06 15:02:44 -050073 assertEquals(getHidden(i), ranking.isSuspended());
Julia Reynolds22f02b32016-12-01 15:05:13 -050074 }
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 Reynolds924eed12017-01-19 09:52:07 -050083 Bundle channels = new Bundle();
Julia Reynolds22f02b32016-12-01 15:05:13 -050084 Bundle overridePeople = new Bundle();
85 Bundle snoozeCriteria = new Bundle();
Julia Reynolds924eed12017-01-19 09:52:07 -050086 Bundle showBadge = new Bundle();
Julia Reynolds22f02b32016-12-01 15:05:13 -050087 int[] importance = new int[mKeys.length];
Julia Reynolds503ed942017-10-04 16:04:56 -040088 Bundle userSentiment = new Bundle();
Beverly5a20a5e2018-03-06 15:02:44 -050089 Bundle mHidden = new Bundle();
Julia Reynolds22f02b32016-12-01 15:05:13 -050090
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 Reynolds924eed12017-01-19 09:52:07 -0500101 channels.putParcelable(key, getChannel(key, i));
Julia Reynolds22f02b32016-12-01 15:05:13 -0500102 overridePeople.putStringArrayList(key, getPeople(key, i));
103 snoozeCriteria.putParcelableArrayList(key, getSnoozeCriteria(key, i));
Julia Reynolds924eed12017-01-19 09:52:07 -0500104 showBadge.putBoolean(key, getShowBadge(i));
Julia Reynolds503ed942017-10-04 16:04:56 -0400105 userSentiment.putInt(key, getUserSentiment(i));
Beverly5a20a5e2018-03-06 15:02:44 -0500106 mHidden.putBoolean(key, getHidden(i));
Julia Reynolds22f02b32016-12-01 15:05:13 -0500107 }
108 NotificationRankingUpdate update = new NotificationRankingUpdate(mKeys,
109 interceptedKeys.toArray(new String[0]), visibilityOverrides,
110 suppressedVisualEffects, importance, explanation, overrideGroupKeys,
Beverly5a20a5e2018-03-06 15:02:44 -0500111 channels, overridePeople, snoozeCriteria, showBadge, userSentiment, mHidden);
Julia Reynolds22f02b32016-12-01 15:05:13 -0500112 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 Reynolds924eed12017-01-19 09:52:07 -0500143 private boolean getShowBadge(int index) {
144 return index % 3 == 0;
145 }
146
Julia Reynolds503ed942017-10-04 16:04:56 -0400147 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
Beverly5a20a5e2018-03-06 15:02:44 -0500159 private boolean getHidden(int index) {
160 return index % 2 == 0;
161 }
162
Julia Reynolds22f02b32016-12-01 15:05:13 -0500163 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}