Extract NoManSimulator to top level
So it can be used by other tests (see following CLs)
Also adds some more methods to SbnBuilder to modify the underling
Notification.
Test: atest
Change-Id: I44c0b13ceabd30d633848a68074c2a9b31427d56
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/SbnBuilder.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/SbnBuilder.java
index 94b3ac4..2605402 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/SbnBuilder.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/SbnBuilder.java
@@ -116,6 +116,27 @@
public SbnBuilder setNotification(Notification notification) {
mNotification = notification;
+ mNotificationBuilder = null;
+ return this;
+ }
+
+ public SbnBuilder setContentTitle(Context context, String contentTitle) {
+ modifyNotification(context).setContentTitle(contentTitle);
+ return this;
+ }
+
+ public SbnBuilder setContentText(Context context, String contentText) {
+ modifyNotification(context).setContentText(contentText);
+ return this;
+ }
+
+ public SbnBuilder setGroup(Context context, String groupKey) {
+ modifyNotification(context).setGroup(groupKey);
+ return this;
+ }
+
+ public SbnBuilder setGroupSummary(Context context, boolean isGroupSummary) {
+ modifyNotification(context).setGroupSummary(isGroupSummary);
return this;
}
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/NoManSimulator.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/NoManSimulator.java
new file mode 100644
index 0000000..fcfe43f
--- /dev/null
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/NoManSimulator.java
@@ -0,0 +1,99 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.statusbar.notification.collection;
+
+import static org.junit.Assert.assertNotNull;
+
+import android.service.notification.NotificationListenerService.Ranking;
+import android.service.notification.NotificationListenerService.RankingMap;
+import android.service.notification.StatusBarNotification;
+import android.util.ArrayMap;
+
+import com.android.systemui.statusbar.NotificationListener.NotifServiceListener;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Simulates a NotificationManager
+ *
+ * You can post and retract notifications, each with an accompanying Ranking. The simulator will
+ * keep its RankingMap up to date and call appropriate event listeners.
+ */
+public class NoManSimulator {
+ private final List<NotifServiceListener> mListeners = new ArrayList<>();
+ private final Map<String, Ranking> mRankings = new ArrayMap<>();
+
+ public NoManSimulator() {
+ }
+
+ public void addListener(NotifServiceListener listener) {
+ mListeners.add(listener);
+ }
+
+ public NotifEvent postNotif(NotificationEntryBuilder builder) {
+ final NotificationEntry entry = builder.build();
+ mRankings.put(entry.getKey(), entry.getRanking());
+ final RankingMap rankingMap = buildRankingMap();
+ for (NotifServiceListener listener : mListeners) {
+ listener.onNotificationPosted(entry.getSbn(), rankingMap);
+ }
+ return new NotifEvent(entry.getSbn(), entry.getRanking(), rankingMap);
+ }
+
+ public NotifEvent retractNotif(StatusBarNotification sbn, int reason) {
+ assertNotNull(mRankings.remove(sbn.getKey()));
+ final RankingMap rankingMap = buildRankingMap();
+ for (NotifServiceListener listener : mListeners) {
+ listener.onNotificationRemoved(sbn, rankingMap, reason);
+ }
+ return new NotifEvent(sbn, null, rankingMap);
+ }
+
+ public void issueRankingUpdate() {
+ final RankingMap rankingMap = buildRankingMap();
+ for (NotifServiceListener listener : mListeners) {
+ listener.onNotificationRankingUpdate(rankingMap);
+ }
+ }
+
+ public void setRanking(String key, Ranking ranking) {
+ mRankings.put(key, ranking);
+ }
+
+ private RankingMap buildRankingMap() {
+ return new RankingMap(mRankings.values().toArray(new Ranking[0]));
+ }
+
+ public static class NotifEvent {
+ public final String key;
+ public final StatusBarNotification sbn;
+ public final Ranking ranking;
+ public final RankingMap rankingMap;
+
+ private NotifEvent(
+ StatusBarNotification sbn,
+ Ranking ranking,
+ RankingMap rankingMap) {
+ this.key = sbn.getKey();
+ this.sbn = sbn;
+ this.ranking = ranking;
+ this.rankingMap = rankingMap;
+ }
+ }
+}
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/NotifCollectionTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/NotifCollectionTest.java
index e1e7220..f6a7abd 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/NotifCollectionTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/NotifCollectionTest.java
@@ -24,7 +24,6 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
-import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
@@ -35,9 +34,7 @@
import android.annotation.Nullable;
import android.os.RemoteException;
import android.service.notification.NotificationListenerService.Ranking;
-import android.service.notification.NotificationListenerService.RankingMap;
import android.service.notification.NotificationStats;
-import android.service.notification.StatusBarNotification;
import android.testing.AndroidTestingRunner;
import android.testing.TestableLooper;
import android.util.ArrayMap;
@@ -50,6 +47,7 @@
import com.android.systemui.statusbar.NotificationListener;
import com.android.systemui.statusbar.NotificationListener.NotifServiceListener;
import com.android.systemui.statusbar.RankingBuilder;
+import com.android.systemui.statusbar.notification.collection.NoManSimulator.NotifEvent;
import com.android.systemui.statusbar.notification.collection.NotifCollection.CancellationReason;
import com.android.systemui.util.Assert;
@@ -101,13 +99,14 @@
verify(mListenerService).addNotificationListener(mListenerCaptor.capture());
mServiceListener = Objects.requireNonNull(mListenerCaptor.getValue());
- mNoMan = new NoManSimulator(mServiceListener);
+ mNoMan = new NoManSimulator();
+ mNoMan.addListener(mServiceListener);
}
@Test
public void testEventDispatchedWhenNotifPosted() {
// WHEN a notification is posted
- PostedNotif notif1 = mNoMan.postNotif(
+ NotifEvent notif1 = mNoMan.postNotif(
buildNotif(TEST_PACKAGE, 3)
.setRank(4747));
@@ -127,7 +126,7 @@
.setRank(4747));
// WHEN the notif is reposted
- PostedNotif notif2 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 3)
+ NotifEvent notif2 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 3)
.setRank(89));
// THEN the listener is notified
@@ -145,7 +144,7 @@
mNoMan.postNotif(buildNotif(TEST_PACKAGE, 3));
clearInvocations(mCollectionListener);
- PostedNotif notif = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 47));
+ NotifEvent notif = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 47));
NotificationEntry entry = mCollectionListener.getEntry(notif.key);
clearInvocations(mCollectionListener);
@@ -161,7 +160,7 @@
@Test
public void testRankingsAreUpdatedForOtherNotifs() {
// GIVEN a collection with one notif
- PostedNotif notif1 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 3)
+ NotifEvent notif1 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 3)
.setRank(47));
NotificationEntry entry1 = mCollectionListener.getEntry(notif1.key);
@@ -178,11 +177,11 @@
@Test
public void testRankingUpdateIsProperlyIssuedToEveryone() {
// GIVEN a collection with a couple notifs
- PostedNotif notif1 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 3)
+ NotifEvent notif1 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 3)
.setRank(3));
- PostedNotif notif2 = mNoMan.postNotif(buildNotif(TEST_PACKAGE2, 8)
+ NotifEvent notif2 = mNoMan.postNotif(buildNotif(TEST_PACKAGE2, 8)
.setRank(2));
- PostedNotif notif3 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 77)
+ NotifEvent notif3 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 77)
.setRank(1));
NotificationEntry entry1 = mCollectionListener.getEntry(notif1.key);
@@ -217,7 +216,7 @@
@Test
public void testNotifEntriesAreNotPersistedAcrossRemovalAndReposting() {
// GIVEN a notification that has been posted
- PostedNotif notif1 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 3));
+ NotifEvent notif1 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 3));
NotificationEntry entry1 = mCollectionListener.getEntry(notif1.key);
// WHEN the notification is retracted and then reposted
@@ -234,8 +233,8 @@
// GIVEN a collection with a couple notifications and a lifetime extender
mCollection.addNotificationLifetimeExtender(mExtender1);
- PostedNotif notif1 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 47, "myTag"));
- PostedNotif notif2 = mNoMan.postNotif(buildNotif(TEST_PACKAGE2, 88, "barTag"));
+ NotifEvent notif1 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 47, "myTag"));
+ NotifEvent notif2 = mNoMan.postNotif(buildNotif(TEST_PACKAGE2, 88, "barTag"));
NotificationEntry entry2 = mCollectionListener.getEntry(notif2.key);
// WHEN a notification is manually dismissed
@@ -267,9 +266,9 @@
@Test(expected = IllegalStateException.class)
public void testDismissingNonExistentNotificationThrows() {
// GIVEN a collection that originally had three notifs, but where one was dismissed
- PostedNotif notif1 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 47));
- PostedNotif notif2 = mNoMan.postNotif(buildNotif(TEST_PACKAGE2, 88));
- PostedNotif notif3 = mNoMan.postNotif(buildNotif(TEST_PACKAGE2, 99));
+ NotifEvent notif1 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 47));
+ NotifEvent notif2 = mNoMan.postNotif(buildNotif(TEST_PACKAGE2, 88));
+ NotifEvent notif3 = mNoMan.postNotif(buildNotif(TEST_PACKAGE2, 99));
NotificationEntry entry2 = mCollectionListener.getEntry(notif2.key);
mNoMan.retractNotif(notif2.sbn, REASON_UNKNOWN);
@@ -292,8 +291,8 @@
mCollection.addNotificationLifetimeExtender(mExtender2);
mCollection.addNotificationLifetimeExtender(mExtender3);
- PostedNotif notif1 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 47));
- PostedNotif notif2 = mNoMan.postNotif(buildNotif(TEST_PACKAGE2, 88));
+ NotifEvent notif1 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 47));
+ NotifEvent notif2 = mNoMan.postNotif(buildNotif(TEST_PACKAGE2, 88));
NotificationEntry entry2 = mCollectionListener.getEntry(notif2.key);
// WHEN a notification is removed
@@ -320,8 +319,8 @@
mCollection.addNotificationLifetimeExtender(mExtender2);
mCollection.addNotificationLifetimeExtender(mExtender3);
- PostedNotif notif1 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 47));
- PostedNotif notif2 = mNoMan.postNotif(buildNotif(TEST_PACKAGE2, 88));
+ NotifEvent notif1 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 47));
+ NotifEvent notif2 = mNoMan.postNotif(buildNotif(TEST_PACKAGE2, 88));
NotificationEntry entry2 = mCollectionListener.getEntry(notif2.key);
// GIVEN a notification gets lifetime-extended by one of them
@@ -357,8 +356,8 @@
mCollection.addNotificationLifetimeExtender(mExtender2);
mCollection.addNotificationLifetimeExtender(mExtender3);
- PostedNotif notif1 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 47));
- PostedNotif notif2 = mNoMan.postNotif(buildNotif(TEST_PACKAGE2, 88));
+ NotifEvent notif1 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 47));
+ NotifEvent notif2 = mNoMan.postNotif(buildNotif(TEST_PACKAGE2, 88));
NotificationEntry entry2 = mCollectionListener.getEntry(notif2.key);
// GIVEN a notification gets lifetime-extended by a couple of them
@@ -392,8 +391,8 @@
mCollection.addNotificationLifetimeExtender(mExtender2);
mCollection.addNotificationLifetimeExtender(mExtender3);
- PostedNotif notif1 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 47));
- PostedNotif notif2 = mNoMan.postNotif(buildNotif(TEST_PACKAGE2, 88));
+ NotifEvent notif1 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 47));
+ NotifEvent notif2 = mNoMan.postNotif(buildNotif(TEST_PACKAGE2, 88));
NotificationEntry entry2 = mCollectionListener.getEntry(notif2.key);
// GIVEN a notification gets lifetime-extended by a couple of them
@@ -422,8 +421,8 @@
mExtender1.shouldExtendLifetime = true;
mExtender2.shouldExtendLifetime = true;
- PostedNotif notif1 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 47));
- PostedNotif notif2 = mNoMan.postNotif(buildNotif(TEST_PACKAGE2, 88));
+ NotifEvent notif1 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 47));
+ NotifEvent notif2 = mNoMan.postNotif(buildNotif(TEST_PACKAGE2, 88));
NotificationEntry entry2 = mCollectionListener.getEntry(notif2.key);
// GIVEN a notification gets lifetime-extended by a couple of them
@@ -452,8 +451,8 @@
mExtender1.shouldExtendLifetime = true;
mExtender2.shouldExtendLifetime = true;
- PostedNotif notif1 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 47));
- PostedNotif notif2 = mNoMan.postNotif(buildNotif(TEST_PACKAGE2, 88));
+ NotifEvent notif1 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 47));
+ NotifEvent notif2 = mNoMan.postNotif(buildNotif(TEST_PACKAGE2, 88));
NotificationEntry entry2 = mCollectionListener.getEntry(notif2.key);
// GIVEN a notification gets lifetime-extended by a couple of them
@@ -481,8 +480,8 @@
mExtender1.shouldExtendLifetime = true;
mExtender2.shouldExtendLifetime = true;
- PostedNotif notif1 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 47));
- PostedNotif notif2 = mNoMan.postNotif(buildNotif(TEST_PACKAGE2, 88));
+ NotifEvent notif1 = mNoMan.postNotif(buildNotif(TEST_PACKAGE, 47));
+ NotifEvent notif2 = mNoMan.postNotif(buildNotif(TEST_PACKAGE2, 88));
NotificationEntry entry2 = mCollectionListener.getEntry(notif2.key);
// GIVEN a notification gets lifetime-extended by a couple of them
@@ -491,7 +490,7 @@
clearInvocations(mExtender1, mExtender2, mExtender3);
// WHEN the notification is reposted
- PostedNotif notif2a = mNoMan.postNotif(buildNotif(TEST_PACKAGE2, 88)
+ NotifEvent notif2a = mNoMan.postNotif(buildNotif(TEST_PACKAGE2, 88)
.setRank(4747)
.setExplanation("Some new explanation"));
@@ -512,53 +511,6 @@
.setId(id);
}
- private static class NoManSimulator {
- private final NotifServiceListener mListener;
- private final Map<String, Ranking> mRankings = new ArrayMap<>();
-
- private NoManSimulator(
- NotifServiceListener listener) {
- mListener = listener;
- }
-
- PostedNotif postNotif(NotificationEntryBuilder builder) {
- NotificationEntry entry = builder.build();
- mRankings.put(entry.getKey(), entry.getRanking());
- mListener.onNotificationPosted(entry.getSbn(), buildRankingMap());
- return new PostedNotif(entry.getSbn(), entry.getRanking());
- }
-
- void retractNotif(StatusBarNotification sbn, int reason) {
- assertNotNull(mRankings.remove(sbn.getKey()));
- mListener.onNotificationRemoved(sbn, buildRankingMap(), reason);
- }
-
- void issueRankingUpdate() {
- mListener.onNotificationRankingUpdate(buildRankingMap());
- }
-
- void setRanking(String key, Ranking ranking) {
- mRankings.put(key, ranking);
- }
-
- private RankingMap buildRankingMap() {
- return new RankingMap(mRankings.values().toArray(new Ranking[0]));
- }
- }
-
- private static class PostedNotif {
- public final String key;
- public final StatusBarNotification sbn;
- public final Ranking ranking;
-
- private PostedNotif(StatusBarNotification sbn,
- Ranking ranking) {
- this.key = sbn.getKey();
- this.sbn = sbn;
- this.ranking = ranking;
- }
- }
-
private static class RecordingCollectionListener implements NotifCollectionListener {
private final Map<String, NotificationEntry> mLastSeenEntries = new ArrayMap<>();
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/NotificationEntryBuilder.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/NotificationEntryBuilder.java
index e6a61d6..300ec18 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/NotificationEntryBuilder.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/notification/collection/NotificationEntryBuilder.java
@@ -140,6 +140,28 @@
return this;
}
+ /* Delegated to Notification.Builder (via SbnBuilder) */
+
+ public NotificationEntryBuilder setContentTitle(Context context, String contentTitle) {
+ mSbnBuilder.setContentTitle(context, contentTitle);
+ return this;
+ }
+
+ public NotificationEntryBuilder setContentText(Context context, String contentText) {
+ mSbnBuilder.setContentText(context, contentText);
+ return this;
+ }
+
+ public NotificationEntryBuilder setGroup(Context context, String groupKey) {
+ mSbnBuilder.setGroup(context, groupKey);
+ return this;
+ }
+
+ public NotificationEntryBuilder setGroupSummary(Context context, boolean isGroupSummary) {
+ mSbnBuilder.setGroupSummary(context, isGroupSummary);
+ return this;
+ }
+
/* Delegated to RankingBuilder */
public NotificationEntryBuilder setRank(int rank) {