Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4689)

Unified Diff: chrome/android/javatests/src/org/chromium/chrome/browser/notifications/MockNotificationManagerProxy.java

Issue 960923004: Add two instrumentation tests for Web Notifications on Android. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..568bd97748474447de6fc54c1c6e2821353ac003
--- /dev/null
+++ b/chrome/android/javatests/src/org/chromium/chrome/browser/notifications/MockNotificationManagerProxy.java
@@ -0,0 +1,113 @@
+// 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 final SparseArray<Notification> mNotifications;
+
+ // Mapping between a textual tag and the index of the associated notification in
+ // the |mNotifications| sparse array.
+ private final Map<String, Integer> mTags;
+
+ private int mMutationCount;
+
+ public MockNotificationManagerProxy() {
+ mNotifications = new SparseArray<>();
+ mTags = new HashMap<>();
+ mMutationCount = 0;
+ }
+
+ /**
+ * Returns the notifications currently managed by the mocked notification manager.
+ *
+ * @return Sparse array of the managed notifications.
+ */
+ public SparseArray<Notification> getNotifications() {
+ return mNotifications;
+ }
+
+ /**
+ * 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.
+ *
+ * @return The number of recent mutations.
+ */
+ 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++;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698