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

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/notifications/NotificationTestBase.java

Issue 2877253002: Convert Notification Tests to JUnit4 (Closed)
Patch Set: address comments Created 3 years, 7 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.chrome.browser.notifications;
6
7 import static org.chromium.base.test.util.ScalableTimeout.scaleTimeout;
8
9 import android.app.Notification;
10
11 import org.chromium.base.ThreadUtils;
12 import org.chromium.chrome.browser.preferences.website.ContentSetting;
13 import org.chromium.chrome.browser.preferences.website.NotificationInfo;
14 import org.chromium.chrome.test.ChromeTabbedActivityTestBase;
15 import org.chromium.chrome.test.util.browser.notifications.MockNotificationManag erProxy;
16 import org.chromium.chrome.test.util.browser.notifications.MockNotificationManag erProxy.NotificationEntry;
17 import org.chromium.content.browser.test.util.Criteria;
18 import org.chromium.content.browser.test.util.CriteriaHelper;
19 import org.chromium.net.test.EmbeddedTestServer;
20
21 import java.util.List;
22 import java.util.concurrent.TimeoutException;
23
24 /**
25 * Base class for instrumentation tests using Web Notifications on Android.
26 *
27 * Web Notifications are only supported on Android JellyBean and beyond.
28 */
29 public class NotificationTestBase extends ChromeTabbedActivityTestBase {
30 /** The maximum time to wait for a criteria to become valid. */
31 private static final long MAX_TIME_TO_POLL_MS = scaleTimeout(6000);
32
33 /** The polling interval to wait between checking for a satisfied criteria. */
34 private static final long POLLING_INTERVAL_MS = 50;
35
36 private MockNotificationManagerProxy mMockNotificationManager;
37 private EmbeddedTestServer mTestServer;
38
39 @Override
40 protected void setUp() throws Exception {
41 super.setUp();
42 mTestServer = EmbeddedTestServer.createAndStartServer(getInstrumentation ().getContext());
43 }
44
45 /** Returns the test server. */
46 protected EmbeddedTestServer getTestServer() {
47 return mTestServer;
48 }
49
50 /**
51 * Returns the origin of the HTTP server the test is being ran on.
52 */
53 protected String getOrigin() {
54 return mTestServer.getURL("/");
55 }
56
57 /**
58 * Sets the permission to use Web Notifications for the test HTTP server's o rigin to |setting|.
59 */
60 protected void setNotificationContentSettingForCurrentOrigin(final ContentSe tting setting)
61 throws InterruptedException, TimeoutException {
62 final String origin = getOrigin();
63
64 ThreadUtils.runOnUiThreadBlocking(new Runnable() {
65 @Override
66 public void run() {
67 // The notification content setting does not consider the embedd er origin.
68 NotificationInfo notificationInfo = new NotificationInfo(origin, "", false);
69 notificationInfo.setContentSetting(setting);
70 }
71 });
72
73 String permission = runJavaScriptCodeInCurrentTab("Notification.permissi on");
74 if (setting == ContentSetting.ALLOW) {
75 assertEquals("\"granted\"", permission);
76 } else if (setting == ContentSetting.BLOCK) {
77 assertEquals("\"denied\"", permission);
78 } else {
79 assertEquals("\"default\"", permission);
80 }
81 }
82
83 /**
84 * Shows a notification with |title| and |options|, waits until it has been displayed and then
85 * returns the Notification object to the caller. Requires that only a singl e notification is
86 * being displayed in the notification manager.
87 *
88 * @param title Title of the Web Notification to show.
89 * @param options Optional map of options to include when showing the notifi cation.
90 * @return The Android Notification object, as shown in the framework.
91 */
92 protected Notification showAndGetNotification(String title, String options)
93 throws InterruptedException, TimeoutException {
94 runJavaScriptCodeInCurrentTab("showNotification(\"" + title + "\", " + o ptions + ");");
95 return waitForNotification().notification;
96 }
97
98 /**
99 * Waits until a notification has been displayed and then returns a Notifica tionEntry object to
100 * the caller. Requires that only a single notification is displayed.
101 *
102 * @return The NotificationEntry object tracked by the MockNotificationManag erProxy.
103 */
104 protected NotificationEntry waitForNotification() throws InterruptedExceptio n {
105 waitForNotificationManagerMutation();
106 List<NotificationEntry> notifications = getNotificationEntries();
107 assertEquals(1, notifications.size());
108 return notifications.get(0);
109 }
110
111 protected List<NotificationEntry> getNotificationEntries() {
112 return mMockNotificationManager.getNotifications();
113 }
114
115 /**
116 * Waits for a mutation to occur in the mocked notification manager. This in dicates that Chrome
117 * called into Android to notify or cancel a notification.
118 */
119 protected void waitForNotificationManagerMutation() {
120 CriteriaHelper.pollUiThread(new Criteria() {
121 @Override
122 public boolean isSatisfied() {
123 return mMockNotificationManager.getMutationCountAndDecrement() > 0;
124 }
125 }, MAX_TIME_TO_POLL_MS, POLLING_INTERVAL_MS);
126 }
127
128 @Override
129 public void startMainActivity() throws InterruptedException {
130 // The NotificationPlatformBridge must be overriden prior to the browser process starting.
131 mMockNotificationManager = new MockNotificationManagerProxy();
132 NotificationPlatformBridge.overrideNotificationManagerForTesting(mMockNo tificationManager);
133
134 startMainActivityFromLauncher();
135 }
136
137 @Override
138 protected void tearDown() throws Exception {
139 NotificationPlatformBridge.overrideNotificationManagerForTesting(null);
140 mTestServer.stopAndDestroyServer();
141 super.tearDown();
142 }
143 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698