blob: d767ba2e84878175d7ed064807a17a8fa322d1e2 [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
40import org.junit.Test;
41import org.junit.runner.RunWith;
42
43import java.util.ArrayList;
44import java.util.List;
45
46@SmallTest
47@RunWith(AndroidJUnit4.class)
Chris Wren89aa2262017-05-05 18:05:56 -040048public class NotificationListenerServiceTest extends NotificationTestCase {
Julia Reynolds22f02b32016-12-01 15:05:13 -050049
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 Reynolds924eed12017-01-19 09:52:07 -050069 assertEquals(getShowBadge(i), ranking.canShowBadge());
Julia Reynolds503ed942017-10-04 16:04:56 -040070 assertEquals(getUserSentiment(i), ranking.getUserSentiment());
Julia Reynolds22f02b32016-12-01 15:05:13 -050071 }
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 Reynolds924eed12017-01-19 09:52:07 -050080 Bundle channels = new Bundle();
Julia Reynolds22f02b32016-12-01 15:05:13 -050081 Bundle overridePeople = new Bundle();
82 Bundle snoozeCriteria = new Bundle();
Julia Reynolds924eed12017-01-19 09:52:07 -050083 Bundle showBadge = new Bundle();
Julia Reynolds22f02b32016-12-01 15:05:13 -050084 int[] importance = new int[mKeys.length];
Julia Reynolds503ed942017-10-04 16:04:56 -040085 Bundle userSentiment = new Bundle();
Julia Reynolds22f02b32016-12-01 15:05:13 -050086
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 Reynolds924eed12017-01-19 09:52:07 -050097 channels.putParcelable(key, getChannel(key, i));
Julia Reynolds22f02b32016-12-01 15:05:13 -050098 overridePeople.putStringArrayList(key, getPeople(key, i));
99 snoozeCriteria.putParcelableArrayList(key, getSnoozeCriteria(key, i));
Julia Reynolds924eed12017-01-19 09:52:07 -0500100 showBadge.putBoolean(key, getShowBadge(i));
Julia Reynolds503ed942017-10-04 16:04:56 -0400101 userSentiment.putInt(key, getUserSentiment(i));
Julia Reynolds22f02b32016-12-01 15:05:13 -0500102 }
103 NotificationRankingUpdate update = new NotificationRankingUpdate(mKeys,
104 interceptedKeys.toArray(new String[0]), visibilityOverrides,
105 suppressedVisualEffects, importance, explanation, overrideGroupKeys,
Julia Reynolds503ed942017-10-04 16:04:56 -0400106 channels, overridePeople, snoozeCriteria, showBadge, userSentiment);
Julia Reynolds22f02b32016-12-01 15:05:13 -0500107 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 Reynolds924eed12017-01-19 09:52:07 -0500138 private boolean getShowBadge(int index) {
139 return index % 3 == 0;
140 }
141
Julia Reynolds503ed942017-10-04 16:04:56 -0400142 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 Reynolds22f02b32016-12-01 15:05:13 -0500154 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}