blob: 8d8acb75ae03adb69150d6be39cbaec1b6b55721 [file] [log] [blame]
Julia Reynoldse261db32019-10-21 11:37:35 -04001/*
2 * Copyright (C) 2019 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 android.app;
18
19import static com.google.common.truth.Truth.assertThat;
20
21import android.app.NotificationHistory.HistoricalNotification;
22import android.graphics.drawable.Icon;
23import android.os.Parcel;
Julia Reynolds30965c42020-02-14 10:21:28 -050024import android.util.Slog;
Julia Reynoldse261db32019-10-21 11:37:35 -040025
26import androidx.test.InstrumentationRegistry;
27import androidx.test.runner.AndroidJUnit4;
28
29import org.junit.Test;
30import org.junit.runner.RunWith;
31
32import java.util.ArrayList;
33import java.util.List;
34
35@RunWith(AndroidJUnit4.class)
36public class NotificationHistoryTest {
37
38 private HistoricalNotification getHistoricalNotification(int index) {
39 return getHistoricalNotification("package" + index, index);
40 }
41
42 private HistoricalNotification getHistoricalNotification(String packageName, int index) {
43 String expectedChannelName = "channelName" + index;
44 String expectedChannelId = "channelId" + index;
45 int expectedUid = 1123456 + index;
46 int expectedUserId = 11 + index;
47 long expectedPostTime = 987654321 + index;
48 String expectedTitle = "title" + index;
49 String expectedText = "text" + index;
50 Icon expectedIcon = Icon.createWithResource(InstrumentationRegistry.getContext(),
51 index);
Julia Reynolds30965c42020-02-14 10:21:28 -050052 String conversationId = null;
53 if (index % 2 == 0) {
54 conversationId = "convo" + index;
55 }
Julia Reynoldse261db32019-10-21 11:37:35 -040056
57 return new HistoricalNotification.Builder()
58 .setPackage(packageName)
59 .setChannelName(expectedChannelName)
60 .setChannelId(expectedChannelId)
61 .setUid(expectedUid)
62 .setUserId(expectedUserId)
63 .setPostedTimeMs(expectedPostTime)
64 .setTitle(expectedTitle)
65 .setText(expectedText)
66 .setIcon(expectedIcon)
Julia Reynolds30965c42020-02-14 10:21:28 -050067 .setConversationId(conversationId)
Julia Reynoldse261db32019-10-21 11:37:35 -040068 .build();
69 }
70
71 @Test
72 public void testHistoricalNotificationBuilder() {
73 String expectedPackage = "package";
74 String expectedChannelName = "channelName";
75 String expectedChannelId = "channelId";
76 int expectedUid = 1123456;
77 int expectedUserId = 11;
78 long expectedPostTime = 987654321;
79 String expectedTitle = "title";
80 String expectedText = "text";
81 Icon expectedIcon = Icon.createWithResource(InstrumentationRegistry.getContext(),
82 android.R.drawable.btn_star);
Julia Reynolds30965c42020-02-14 10:21:28 -050083 String expectedConversationId = "convo";
Julia Reynoldse261db32019-10-21 11:37:35 -040084
85 HistoricalNotification n = new HistoricalNotification.Builder()
86 .setPackage(expectedPackage)
87 .setChannelName(expectedChannelName)
88 .setChannelId(expectedChannelId)
89 .setUid(expectedUid)
90 .setUserId(expectedUserId)
91 .setPostedTimeMs(expectedPostTime)
92 .setTitle(expectedTitle)
93 .setText(expectedText)
94 .setIcon(expectedIcon)
Julia Reynolds30965c42020-02-14 10:21:28 -050095 .setConversationId(expectedConversationId)
Julia Reynoldse261db32019-10-21 11:37:35 -040096 .build();
97
98 assertThat(n.getPackage()).isEqualTo(expectedPackage);
99 assertThat(n.getChannelName()).isEqualTo(expectedChannelName);
100 assertThat(n.getChannelId()).isEqualTo(expectedChannelId);
101 assertThat(n.getUid()).isEqualTo(expectedUid);
102 assertThat(n.getUserId()).isEqualTo(expectedUserId);
103 assertThat(n.getPostedTimeMs()).isEqualTo(expectedPostTime);
104 assertThat(n.getTitle()).isEqualTo(expectedTitle);
105 assertThat(n.getText()).isEqualTo(expectedText);
106 assertThat(expectedIcon.sameAs(n.getIcon())).isTrue();
Julia Reynolds30965c42020-02-14 10:21:28 -0500107 assertThat(n.getConversationId()).isEqualTo(expectedConversationId);
Julia Reynoldse261db32019-10-21 11:37:35 -0400108 }
109
110 @Test
111 public void testAddNotificationToWrite() {
112 NotificationHistory history = new NotificationHistory();
113 HistoricalNotification n = getHistoricalNotification(0);
114 HistoricalNotification n2 = getHistoricalNotification(1);
115
116 history.addNotificationToWrite(n2);
117 history.addNotificationToWrite(n);
118
119 assertThat(history.getNotificationsToWrite().size()).isEqualTo(2);
120 assertThat(history.getNotificationsToWrite().get(0)).isSameAs(n2);
121 assertThat(history.getNotificationsToWrite().get(1)).isSameAs(n);
122 assertThat(history.getHistoryCount()).isEqualTo(2);
123 }
124
125 @Test
Julia Reynoldsa614c272019-11-15 16:43:29 -0500126 public void testAddNotificationsToWrite() {
127 NotificationHistory history = new NotificationHistory();
Julia Reynolds1cc5f842020-01-21 11:25:46 -0500128 HistoricalNotification n = getHistoricalNotification(3);
Julia Reynoldsa614c272019-11-15 16:43:29 -0500129 HistoricalNotification n2 = getHistoricalNotification(1);
Julia Reynolds1cc5f842020-01-21 11:25:46 -0500130 HistoricalNotification n5 = getHistoricalNotification(0);
Julia Reynoldsa614c272019-11-15 16:43:29 -0500131 history.addNotificationToWrite(n2);
132 history.addNotificationToWrite(n);
Julia Reynolds1cc5f842020-01-21 11:25:46 -0500133 history.addNotificationToWrite(n5);
Julia Reynoldsa614c272019-11-15 16:43:29 -0500134
135 NotificationHistory secondHistory = new NotificationHistory();
Julia Reynolds1cc5f842020-01-21 11:25:46 -0500136 HistoricalNotification n3 = getHistoricalNotification(4);
137 HistoricalNotification n4 = getHistoricalNotification(2);
Julia Reynoldsa614c272019-11-15 16:43:29 -0500138 secondHistory.addNotificationToWrite(n4);
139 secondHistory.addNotificationToWrite(n3);
140
141 history.addNotificationsToWrite(secondHistory);
142
Julia Reynolds1cc5f842020-01-21 11:25:46 -0500143 assertThat(history.getNotificationsToWrite().size()).isEqualTo(5);
144 assertThat(history.getNotificationsToWrite().get(0)).isSameAs(n3);
Julia Reynoldsa614c272019-11-15 16:43:29 -0500145 assertThat(history.getNotificationsToWrite().get(1)).isSameAs(n);
146 assertThat(history.getNotificationsToWrite().get(2)).isSameAs(n4);
Julia Reynolds1cc5f842020-01-21 11:25:46 -0500147 assertThat(history.getNotificationsToWrite().get(3)).isSameAs(n2);
148 assertThat(history.getNotificationsToWrite().get(4)).isSameAs(n5);
149 assertThat(history.getHistoryCount()).isEqualTo(5);
Julia Reynoldsa614c272019-11-15 16:43:29 -0500150
151 assertThat(history.getPooledStringsToWrite()).asList().contains(n2.getChannelName());
152 assertThat(history.getPooledStringsToWrite()).asList().contains(n4.getPackage());
153 }
154
155 @Test
Julia Reynoldse261db32019-10-21 11:37:35 -0400156 public void testPoolStringsFromNotifications() {
157 NotificationHistory history = new NotificationHistory();
158
159 List<String> expectedStrings = new ArrayList<>();
160 for (int i = 1; i <= 10; i++) {
161 HistoricalNotification n = getHistoricalNotification(i);
162 expectedStrings.add(n.getPackage());
163 expectedStrings.add(n.getChannelName());
164 expectedStrings.add(n.getChannelId());
Julia Reynolds30965c42020-02-14 10:21:28 -0500165 if (n.getConversationId() != null) {
166 expectedStrings.add(n.getConversationId());
167 }
Julia Reynoldse261db32019-10-21 11:37:35 -0400168 history.addNotificationToWrite(n);
169 }
170
171 history.poolStringsFromNotifications();
172
173 assertThat(history.getPooledStringsToWrite().length).isEqualTo(expectedStrings.size());
174 String previous = null;
175 for (String actual : history.getPooledStringsToWrite()) {
176 assertThat(expectedStrings).contains(actual);
177
178 if (previous != null) {
179 assertThat(actual).isGreaterThan(previous);
180 }
181 previous = actual;
182 }
183 }
184
185 @Test
186 public void testAddPooledStrings() {
187 NotificationHistory history = new NotificationHistory();
188
189 List<String> expectedStrings = new ArrayList<>();
190 for (int i = 1; i <= 10; i++) {
191 HistoricalNotification n = getHistoricalNotification(i);
192 expectedStrings.add(n.getPackage());
193 expectedStrings.add(n.getChannelName());
194 expectedStrings.add(n.getChannelId());
Julia Reynolds30965c42020-02-14 10:21:28 -0500195 if (n.getConversationId() != null) {
196 expectedStrings.add(n.getConversationId());
197 }
Julia Reynoldse261db32019-10-21 11:37:35 -0400198 history.addNotificationToWrite(n);
199 }
200
201 history.addPooledStrings(expectedStrings);
202
203 String[] actualStrings = history.getPooledStringsToWrite();
204 assertThat(actualStrings.length).isEqualTo(expectedStrings.size());
205 String previous = null;
206 for (String actual : actualStrings) {
207 assertThat(expectedStrings).contains(actual);
208
209 if (previous != null) {
210 assertThat(actual).isGreaterThan(previous);
211 }
212 previous = actual;
213 }
214 }
215
216 @Test
217 public void testRemoveNotificationsFromWrite() {
218 NotificationHistory history = new NotificationHistory();
219
220 List<HistoricalNotification> postRemoveExpectedEntries = new ArrayList<>();
221 List<String> postRemoveExpectedStrings = new ArrayList<>();
222 for (int i = 1; i <= 10; i++) {
223 HistoricalNotification n =
224 getHistoricalNotification((i % 2 == 0) ? "pkgEven" : "pkgOdd", i);
225
226 if (i % 2 == 0) {
227 postRemoveExpectedStrings.add(n.getPackage());
228 postRemoveExpectedStrings.add(n.getChannelName());
229 postRemoveExpectedStrings.add(n.getChannelId());
Julia Reynolds30965c42020-02-14 10:21:28 -0500230 if (n.getConversationId() != null) {
231 postRemoveExpectedStrings.add(n.getConversationId());
232 }
Julia Reynoldse261db32019-10-21 11:37:35 -0400233 postRemoveExpectedEntries.add(n);
234 }
235
236 history.addNotificationToWrite(n);
237 }
238
239 history.poolStringsFromNotifications();
240
241 assertThat(history.getNotificationsToWrite().size()).isEqualTo(10);
Julia Reynolds30965c42020-02-14 10:21:28 -0500242 // 2 package names and 10 * 2 unique channel names and ids and 5 conversation ids
243 assertThat(history.getPooledStringsToWrite().length).isEqualTo(27);
Julia Reynoldse261db32019-10-21 11:37:35 -0400244
245 history.removeNotificationsFromWrite("pkgOdd");
246
247
Julia Reynolds30965c42020-02-14 10:21:28 -0500248 // 1 package names and 5 * 2 unique channel names and ids and 5 conversation ids
249 assertThat(history.getPooledStringsToWrite().length).isEqualTo(16);
Julia Reynoldse261db32019-10-21 11:37:35 -0400250 assertThat(history.getNotificationsToWrite())
251 .containsExactlyElementsIn(postRemoveExpectedEntries);
252 }
253
254 @Test
Julia Reynoldsb1a77182020-02-06 16:38:10 -0500255 public void testRemoveNotificationFromWrite() {
256 NotificationHistory history = new NotificationHistory();
257
258 List<HistoricalNotification> postRemoveExpectedEntries = new ArrayList<>();
259 List<String> postRemoveExpectedStrings = new ArrayList<>();
260 for (int i = 1; i <= 10; i++) {
261 HistoricalNotification n = getHistoricalNotification("pkg", i);
262
263 if (987654323 != n.getPostedTimeMs()) {
264 postRemoveExpectedStrings.add(n.getPackage());
265 postRemoveExpectedStrings.add(n.getChannelName());
266 postRemoveExpectedStrings.add(n.getChannelId());
Julia Reynolds30965c42020-02-14 10:21:28 -0500267 if (n.getConversationId() != null) {
268 postRemoveExpectedStrings.add(n.getConversationId());
269 }
Julia Reynoldsb1a77182020-02-06 16:38:10 -0500270 postRemoveExpectedEntries.add(n);
271 }
272
273 history.addNotificationToWrite(n);
274 }
275
276 history.poolStringsFromNotifications();
277
278 assertThat(history.getNotificationsToWrite().size()).isEqualTo(10);
Julia Reynolds30965c42020-02-14 10:21:28 -0500279 // 1 package name and 20 unique channel names and ids and 5 conversation ids
280 assertThat(history.getPooledStringsToWrite().length).isEqualTo(26);
Julia Reynoldsb1a77182020-02-06 16:38:10 -0500281
282 history.removeNotificationFromWrite("pkg", 987654323);
283
284
Julia Reynolds30965c42020-02-14 10:21:28 -0500285 // 1 package names and 9 * 2 unique channel names and ids and 4 conversation ids
286 assertThat(history.getPooledStringsToWrite().length).isEqualTo(23);
Julia Reynoldsb1a77182020-02-06 16:38:10 -0500287 assertThat(history.getNotificationsToWrite())
288 .containsExactlyElementsIn(postRemoveExpectedEntries);
289 }
290
291 @Test
Julia Reynoldse261db32019-10-21 11:37:35 -0400292 public void testParceling() {
293 NotificationHistory history = new NotificationHistory();
294
295 List<HistoricalNotification> expectedEntries = new ArrayList<>();
296 for (int i = 10; i >= 1; i--) {
297 HistoricalNotification n = getHistoricalNotification(i);
298 expectedEntries.add(n);
299 history.addNotificationToWrite(n);
300 }
301 history.poolStringsFromNotifications();
302
303 Parcel parcel = Parcel.obtain();
304 history.writeToParcel(parcel, 0);
305 parcel.setDataPosition(0);
306 NotificationHistory parceledHistory = NotificationHistory.CREATOR.createFromParcel(parcel);
307
308 assertThat(parceledHistory.getHistoryCount()).isEqualTo(expectedEntries.size());
309
310 for (int i = 0; i < expectedEntries.size(); i++) {
311 assertThat(parceledHistory.hasNextNotification()).isTrue();
312
313 HistoricalNotification postParcelNotification = parceledHistory.getNextNotification();
314 assertThat(postParcelNotification).isEqualTo(expectedEntries.get(i));
315 }
316 }
317}