Index: chrome/android/javatests/src/org/chromium/chrome/browser/notifications/MockNotificationManagerProxy.java |
diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/notifications/MockNotificationManagerProxy.java b/chrome/android/javatests/src/org/chromium/chrome/browser/notifications/MockNotificationManagerProxy.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..17e7b9e6437b4def9da3c169e99d560e123f2b7d |
--- /dev/null |
+++ b/chrome/android/javatests/src/org/chromium/chrome/browser/notifications/MockNotificationManagerProxy.java |
@@ -0,0 +1,105 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.chrome.browser.notifications; |
+ |
+import android.app.Notification; |
+import android.util.SparseArray; |
+ |
+import java.util.HashMap; |
+import java.util.Map; |
+ |
+/** |
+ * Mocked implementation of the NotificationManagerProxy. Imitates behavior of the Android |
+ * notification manager, and provides access to the displayed notifications for tests. |
+ */ |
+public class MockNotificationManagerProxy implements NotificationManagerProxy { |
+ private SparseArray<Notification> mNotifications; |
Michael van Ouwerkerk
2015/02/26 17:24:23
nit: final
Peter Beverloo
2015/02/26 18:22:38
Done.
|
+ private Map<String, Integer> mTags; |
Michael van Ouwerkerk
2015/02/26 17:24:23
nit: final
Peter Beverloo
2015/02/26 18:22:38
Done.
|
+ private int mMutationCount; |
+ |
+ public MockNotificationManagerProxy() { |
+ mNotifications = new SparseArray<Notification>(); |
Michael van Ouwerkerk
2015/02/26 17:24:23
nit: just new SparseArray<>();
Peter Beverloo
2015/02/26 18:22:38
Done.
|
+ mTags = new HashMap<String, Integer>(); |
Michael van Ouwerkerk
2015/02/26 17:24:23
nit: just new HashMap<>();
Peter Beverloo
2015/02/26 18:22:38
Done.
|
+ mMutationCount = 0; |
+ } |
+ |
+ /** |
+ * Returns the notifications which are currently being managed by the notification manager. |
+ */ |
+ public SparseArray<Notification> getNotifications() { |
+ return mNotifications; |
+ } |
+ |
+ /** |
+ * Returns the number of recent mutations. May be used by tests to determine if a call has |
+ * recently successfully been handled by the notification manager. If the mutation count |
+ * is higher than zero, it will be decremented by one automatically. |
+ */ |
+ public int getMutationCountAndDecrement() { |
+ int mutationCount = mMutationCount; |
+ if (mutationCount > 0) |
+ mMutationCount--; |
+ |
+ return mutationCount; |
+ } |
+ |
+ @Override |
+ public void cancel(int id) { |
+ if (mNotifications.indexOfKey(id) < 0) { |
+ throw new RuntimeException("Invalid notification id supplied."); |
+ } |
+ |
+ mNotifications.delete(id); |
+ mMutationCount++; |
+ } |
+ |
+ @Override |
+ public void cancel(String tag, int id) { |
+ if (!mTags.containsKey(tag)) { |
+ throw new RuntimeException("Invalid notification tag supplied."); |
+ } |
+ |
+ int notificationId = mTags.get(tag); |
+ if (notificationId != id) { |
+ throw new RuntimeException("Mismatch in notification ids for the given tag."); |
+ } |
+ |
+ mTags.remove(tag); |
+ |
+ assert mNotifications.indexOfKey(id) >= 0; |
+ mNotifications.delete(id); |
+ |
+ mMutationCount++; |
+ } |
+ |
+ @Override |
+ public void cancelAll() { |
+ mNotifications.clear(); |
+ mTags.clear(); |
+ |
+ mMutationCount++; |
+ } |
+ |
+ @Override |
+ public void notify(int id, Notification notification) { |
+ mNotifications.put(id, notification); |
+ mMutationCount++; |
+ } |
+ |
+ @Override |
+ public void notify(String tag, int id, Notification notification) { |
+ if (mTags.containsKey(tag)) { |
+ int notificationId = mTags.get(tag); |
+ |
+ assert mNotifications.indexOfKey(notificationId) >= 0; |
+ mNotifications.delete(notificationId); |
+ } |
+ |
+ mNotifications.put(id, notification); |
+ mTags.put(tag, id); |
+ |
+ mMutationCount++; |
+ } |
+} |