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