Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.app; |
| 18 | |
| 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | |
| 21 | import android.app.NotificationHistory.HistoricalNotification; |
| 22 | import android.graphics.drawable.Icon; |
| 23 | import android.os.Parcel; |
Julia Reynolds | 30965c4 | 2020-02-14 10:21:28 -0500 | [diff] [blame^] | 24 | import android.util.Slog; |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 25 | |
| 26 | import androidx.test.InstrumentationRegistry; |
| 27 | import androidx.test.runner.AndroidJUnit4; |
| 28 | |
| 29 | import org.junit.Test; |
| 30 | import org.junit.runner.RunWith; |
| 31 | |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.List; |
| 34 | |
| 35 | @RunWith(AndroidJUnit4.class) |
| 36 | public 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 Reynolds | 30965c4 | 2020-02-14 10:21:28 -0500 | [diff] [blame^] | 52 | String conversationId = null; |
| 53 | if (index % 2 == 0) { |
| 54 | conversationId = "convo" + index; |
| 55 | } |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 56 | |
| 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 Reynolds | 30965c4 | 2020-02-14 10:21:28 -0500 | [diff] [blame^] | 67 | .setConversationId(conversationId) |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 68 | .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 Reynolds | 30965c4 | 2020-02-14 10:21:28 -0500 | [diff] [blame^] | 83 | String expectedConversationId = "convo"; |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 84 | |
| 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 Reynolds | 30965c4 | 2020-02-14 10:21:28 -0500 | [diff] [blame^] | 95 | .setConversationId(expectedConversationId) |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 96 | .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 Reynolds | 30965c4 | 2020-02-14 10:21:28 -0500 | [diff] [blame^] | 107 | assertThat(n.getConversationId()).isEqualTo(expectedConversationId); |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 108 | } |
| 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 Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 126 | public void testAddNotificationsToWrite() { |
| 127 | NotificationHistory history = new NotificationHistory(); |
Julia Reynolds | 1cc5f84 | 2020-01-21 11:25:46 -0500 | [diff] [blame] | 128 | HistoricalNotification n = getHistoricalNotification(3); |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 129 | HistoricalNotification n2 = getHistoricalNotification(1); |
Julia Reynolds | 1cc5f84 | 2020-01-21 11:25:46 -0500 | [diff] [blame] | 130 | HistoricalNotification n5 = getHistoricalNotification(0); |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 131 | history.addNotificationToWrite(n2); |
| 132 | history.addNotificationToWrite(n); |
Julia Reynolds | 1cc5f84 | 2020-01-21 11:25:46 -0500 | [diff] [blame] | 133 | history.addNotificationToWrite(n5); |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 134 | |
| 135 | NotificationHistory secondHistory = new NotificationHistory(); |
Julia Reynolds | 1cc5f84 | 2020-01-21 11:25:46 -0500 | [diff] [blame] | 136 | HistoricalNotification n3 = getHistoricalNotification(4); |
| 137 | HistoricalNotification n4 = getHistoricalNotification(2); |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 138 | secondHistory.addNotificationToWrite(n4); |
| 139 | secondHistory.addNotificationToWrite(n3); |
| 140 | |
| 141 | history.addNotificationsToWrite(secondHistory); |
| 142 | |
Julia Reynolds | 1cc5f84 | 2020-01-21 11:25:46 -0500 | [diff] [blame] | 143 | assertThat(history.getNotificationsToWrite().size()).isEqualTo(5); |
| 144 | assertThat(history.getNotificationsToWrite().get(0)).isSameAs(n3); |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 145 | assertThat(history.getNotificationsToWrite().get(1)).isSameAs(n); |
| 146 | assertThat(history.getNotificationsToWrite().get(2)).isSameAs(n4); |
Julia Reynolds | 1cc5f84 | 2020-01-21 11:25:46 -0500 | [diff] [blame] | 147 | assertThat(history.getNotificationsToWrite().get(3)).isSameAs(n2); |
| 148 | assertThat(history.getNotificationsToWrite().get(4)).isSameAs(n5); |
| 149 | assertThat(history.getHistoryCount()).isEqualTo(5); |
Julia Reynolds | a614c27 | 2019-11-15 16:43:29 -0500 | [diff] [blame] | 150 | |
| 151 | assertThat(history.getPooledStringsToWrite()).asList().contains(n2.getChannelName()); |
| 152 | assertThat(history.getPooledStringsToWrite()).asList().contains(n4.getPackage()); |
| 153 | } |
| 154 | |
| 155 | @Test |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 156 | 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 Reynolds | 30965c4 | 2020-02-14 10:21:28 -0500 | [diff] [blame^] | 165 | if (n.getConversationId() != null) { |
| 166 | expectedStrings.add(n.getConversationId()); |
| 167 | } |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 168 | 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 Reynolds | 30965c4 | 2020-02-14 10:21:28 -0500 | [diff] [blame^] | 195 | if (n.getConversationId() != null) { |
| 196 | expectedStrings.add(n.getConversationId()); |
| 197 | } |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 198 | 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 Reynolds | 30965c4 | 2020-02-14 10:21:28 -0500 | [diff] [blame^] | 230 | if (n.getConversationId() != null) { |
| 231 | postRemoveExpectedStrings.add(n.getConversationId()); |
| 232 | } |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 233 | postRemoveExpectedEntries.add(n); |
| 234 | } |
| 235 | |
| 236 | history.addNotificationToWrite(n); |
| 237 | } |
| 238 | |
| 239 | history.poolStringsFromNotifications(); |
| 240 | |
| 241 | assertThat(history.getNotificationsToWrite().size()).isEqualTo(10); |
Julia Reynolds | 30965c4 | 2020-02-14 10:21:28 -0500 | [diff] [blame^] | 242 | // 2 package names and 10 * 2 unique channel names and ids and 5 conversation ids |
| 243 | assertThat(history.getPooledStringsToWrite().length).isEqualTo(27); |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 244 | |
| 245 | history.removeNotificationsFromWrite("pkgOdd"); |
| 246 | |
| 247 | |
Julia Reynolds | 30965c4 | 2020-02-14 10:21:28 -0500 | [diff] [blame^] | 248 | // 1 package names and 5 * 2 unique channel names and ids and 5 conversation ids |
| 249 | assertThat(history.getPooledStringsToWrite().length).isEqualTo(16); |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 250 | assertThat(history.getNotificationsToWrite()) |
| 251 | .containsExactlyElementsIn(postRemoveExpectedEntries); |
| 252 | } |
| 253 | |
| 254 | @Test |
Julia Reynolds | b1a7718 | 2020-02-06 16:38:10 -0500 | [diff] [blame] | 255 | 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 Reynolds | 30965c4 | 2020-02-14 10:21:28 -0500 | [diff] [blame^] | 267 | if (n.getConversationId() != null) { |
| 268 | postRemoveExpectedStrings.add(n.getConversationId()); |
| 269 | } |
Julia Reynolds | b1a7718 | 2020-02-06 16:38:10 -0500 | [diff] [blame] | 270 | postRemoveExpectedEntries.add(n); |
| 271 | } |
| 272 | |
| 273 | history.addNotificationToWrite(n); |
| 274 | } |
| 275 | |
| 276 | history.poolStringsFromNotifications(); |
| 277 | |
| 278 | assertThat(history.getNotificationsToWrite().size()).isEqualTo(10); |
Julia Reynolds | 30965c4 | 2020-02-14 10:21:28 -0500 | [diff] [blame^] | 279 | // 1 package name and 20 unique channel names and ids and 5 conversation ids |
| 280 | assertThat(history.getPooledStringsToWrite().length).isEqualTo(26); |
Julia Reynolds | b1a7718 | 2020-02-06 16:38:10 -0500 | [diff] [blame] | 281 | |
| 282 | history.removeNotificationFromWrite("pkg", 987654323); |
| 283 | |
| 284 | |
Julia Reynolds | 30965c4 | 2020-02-14 10:21:28 -0500 | [diff] [blame^] | 285 | // 1 package names and 9 * 2 unique channel names and ids and 4 conversation ids |
| 286 | assertThat(history.getPooledStringsToWrite().length).isEqualTo(23); |
Julia Reynolds | b1a7718 | 2020-02-06 16:38:10 -0500 | [diff] [blame] | 287 | assertThat(history.getNotificationsToWrite()) |
| 288 | .containsExactlyElementsIn(postRemoveExpectedEntries); |
| 289 | } |
| 290 | |
| 291 | @Test |
Julia Reynolds | e261db3 | 2019-10-21 11:37:35 -0400 | [diff] [blame] | 292 | 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 | } |