Chromium Code Reviews| Index: chrome/android/javatests/src/org/chromium/chrome/browser/notifications/NotificationUIManagerTest.java |
| diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/notifications/NotificationUIManagerTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/notifications/NotificationUIManagerTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..37437d3f9d44dee58bd9ed0463ecf653478f03ff |
| --- /dev/null |
| +++ b/chrome/android/javatests/src/org/chromium/chrome/browser/notifications/NotificationUIManagerTest.java |
| @@ -0,0 +1,145 @@ |
| +// 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.test.suitebuilder.annotation.MediumTest; |
| +import android.util.SparseArray; |
| + |
| +import org.chromium.base.ThreadUtils; |
| +import org.chromium.base.test.util.Feature; |
| +import org.chromium.chrome.browser.preferences.website.ContentSetting; |
| +import org.chromium.chrome.browser.preferences.website.PushNotificationInfo; |
| +import org.chromium.chrome.shell.ChromeShellTestBase; |
| +import org.chromium.chrome.test.util.TestHttpServerClient; |
| +import org.chromium.content.browser.test.util.Criteria; |
| +import org.chromium.content.browser.test.util.CriteriaHelper; |
| +import org.chromium.content.browser.test.util.JavaScriptUtils; |
| + |
| +import java.util.concurrent.TimeoutException; |
| + |
| +/** |
| + * Instrumentation tests for the Notification UI Manager implementation on Android. |
| + */ |
| +public class NotificationUIManagerTest extends ChromeShellTestBase { |
| + private static final String NOTIFICATION_TEST_PAGE = |
| + TestHttpServerClient.getUrl("chrome/test/data/notifications/android_test.html"); |
| + private static final String PERMISSION_GRANTED = "\"granted\""; |
| + |
| + private MockNotificationManagerProxy mMockNotificationManager; |
| + |
| + /** |
| + * Sets the permission to use Web Notifications for the test HTTP server's origin to |setting|. |
| + */ |
| + private void setNotificationContentSettingForCurrentOrigin(final ContentSetting setting) { |
|
Michael van Ouwerkerk
2015/02/26 17:24:23
nit: this looks like it can be static
Peter Beverloo
2015/02/26 18:22:38
Making this static requires me to wrap it, which d
Michael van Ouwerkerk
2015/02/26 19:12:50
Since this is a test, I don't feel strongly. In ot
|
| + final String origin = TestHttpServerClient.getUrl(""); |
| + |
| + ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| + @Override |
| + public void run() { |
| + PushNotificationInfo pushNotificationInfo = new PushNotificationInfo(origin, ""); |
|
Michael van Ouwerkerk
2015/02/26 17:24:23
Hm, does "" mean "*" for embedder here? Maybe extr
Peter Beverloo
2015/02/26 18:22:38
Added a comment.
|
| + pushNotificationInfo.setContentSetting(setting); |
| + } |
| + }); |
| + } |
| + |
| + /** |
| + * Runs the Javascript |code| in the current tab, and waits for the result to be available. |
| + */ |
| + private String runJavaScriptCodeInCurrentTab(String code) throws InterruptedException, |
| + TimeoutException { |
| + return JavaScriptUtils.executeJavaScriptAndWaitForResult( |
| + getActivity().getActiveContentViewCore().getWebContents(), code); |
| + } |
| + |
| + /** |
| + * Shows a notification with |title| and |options|, waits until it has been displayed and then |
| + * returns the Notification object to the caller. Requires that only a single notification is |
| + * being displayed in the notification manager. |
| + */ |
| + private Notification showAndGetNotification(String title, String options) throws Exception { |
| + runJavaScriptCodeInCurrentTab("showNotification(\"" + title + "\", " + options + ");"); |
| + assertTrue(waitForNotificationManagerMutation()); |
| + |
| + SparseArray<Notification> notifications = mMockNotificationManager.getNotifications(); |
| + assertEquals(1, notifications.size()); |
| + |
| + return notifications.valueAt(0); |
| + } |
| + |
| + /** |
| + * Waits for a mutation to occur in the mocked notification manager. This indicates that Chrome |
| + * called into Android to notify or cancel a notification. |
| + */ |
| + private boolean waitForNotificationManagerMutation() throws Exception { |
| + return CriteriaHelper.pollForUIThreadCriteria(new Criteria() { |
| + @Override |
| + public boolean isSatisfied() { |
| + return mMockNotificationManager.getMutationCountAndDecrement() > 0; |
| + } |
| + }); |
| + } |
| + |
| + @Override |
| + protected void setUp() throws Exception { |
| + super.setUp(); |
| + |
| + mMockNotificationManager = new MockNotificationManagerProxy(); |
| + NotificationUIManager.overrideNotificationManager(mMockNotificationManager); |
| + |
| + launchChromeShellWithUrl(NOTIFICATION_TEST_PAGE); |
| + assertTrue("Page failed to load", waitForActiveShellToBeDoneLoading()); |
| + } |
| + |
| + /** |
| + * Verifies that the intended default properties of a notification will indeed be set on the |
| + * Notification object that will be send to Android. |
| + */ |
| + @MediumTest |
| + @Feature({"Browser", "Notifications"}) |
| + public void testDefaultNotificationProperties() throws Exception { |
| + setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW); |
| + assertEquals(PERMISSION_GRANTED, runJavaScriptCodeInCurrentTab("Notification.permission")); |
| + |
| + String origin = TestHttpServerClient.getUrl(""); |
| + |
| + Notification notification = showAndGetNotification("MyNotification", "{ body: 'Hello' }"); |
| + assertNotNull(notification); |
| + |
| + // Validate the contents of the notification. |
| + assertEquals("MyNotification", notification.extras.getString(Notification.EXTRA_TITLE)); |
| + assertEquals("Hello", notification.extras.getString(Notification.EXTRA_TEXT)); |
| + assertEquals(origin, notification.extras.getString(Notification.EXTRA_SUB_TEXT)); |
| + |
| + // Validate the appearance style of the notification. |
| + assertEquals("android.app.Notification$BigTextStyle", |
| + notification.extras.getString(Notification.EXTRA_TEMPLATE)); |
| + |
| + // Validate the notification's behavior. |
| + assertEquals(Notification.DEFAULT_ALL, notification.defaults); |
| + assertEquals(Notification.PRIORITY_DEFAULT, notification.priority); |
| + } |
| + |
| + /** |
| + * Verifies that notifications which specify an icon will have that icon fetched, converted into |
| + * a Bitmap and included as the large icon in the notification. |
| + */ |
| + @MediumTest |
| + @Feature({"Browser", "Notifications"}) |
| + public void testShowNotificationWithIcon() throws Exception { |
| + setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW); |
| + assertEquals(PERMISSION_GRANTED, runJavaScriptCodeInCurrentTab("Notification.permission")); |
| + |
| + Notification notification = showAndGetNotification("MyNotification", "{icon: 'icon.png'}"); |
| + assertNotNull(notification); |
| + |
| + assertEquals("MyNotification", notification.extras.getString(Notification.EXTRA_TITLE)); |
| + assertNotNull(notification.largeIcon); |
| + |
| + // These are the dimensions of //chrome/test/data/notifications/icon.png at 1x scale. |
| + assertEquals(100, notification.largeIcon.getWidth()); |
| + assertEquals(100, notification.largeIcon.getHeight()); |
| + } |
| +} |