Commit afbc26a7 authored by peter's avatar peter Committed by Commit bot

Verify replacement and activation behavior for notifications on Android.

This CL adds two more Android instrumentation tests for Web Notifications,
which verify that the "tag" property on a notification will replace any
previous notification being shown, and a test verifying that activating
the notification will invoke the Service Worker, closing the notification.

BUG=462251

Review URL: https://codereview.chromium.org/958993002

Cr-Commit-Position: refs/heads/master@{#318704}
parent a220c351
...@@ -7,6 +7,7 @@ package org.chromium.chrome.browser.notifications; ...@@ -7,6 +7,7 @@ package org.chromium.chrome.browser.notifications;
import android.app.Notification; import android.app.Notification;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.os.Build; import android.os.Build;
import android.test.suitebuilder.annotation.LargeTest;
import android.test.suitebuilder.annotation.MediumTest; import android.test.suitebuilder.annotation.MediumTest;
import android.util.SparseArray; import android.util.SparseArray;
...@@ -34,7 +35,6 @@ import java.util.concurrent.TimeoutException; ...@@ -34,7 +35,6 @@ import java.util.concurrent.TimeoutException;
public class NotificationUIManagerTest extends ChromeShellTestBase { public class NotificationUIManagerTest extends ChromeShellTestBase {
private static final String NOTIFICATION_TEST_PAGE = private static final String NOTIFICATION_TEST_PAGE =
TestHttpServerClient.getUrl("chrome/test/data/notifications/android_test.html"); TestHttpServerClient.getUrl("chrome/test/data/notifications/android_test.html");
private static final String PERMISSION_GRANTED = "\"granted\"";
private MockNotificationManagerProxy mMockNotificationManager; private MockNotificationManagerProxy mMockNotificationManager;
...@@ -48,7 +48,8 @@ public class NotificationUIManagerTest extends ChromeShellTestBase { ...@@ -48,7 +48,8 @@ public class NotificationUIManagerTest extends ChromeShellTestBase {
/** /**
* Sets the permission to use Web Notifications for the test HTTP server's origin to |setting|. * Sets the permission to use Web Notifications for the test HTTP server's origin to |setting|.
*/ */
private void setNotificationContentSettingForCurrentOrigin(final ContentSetting setting) { private void setNotificationContentSettingForCurrentOrigin(final ContentSetting setting)
throws InterruptedException, TimeoutException {
final String origin = getOrigin(); final String origin = getOrigin();
ThreadUtils.runOnUiThreadBlocking(new Runnable() { ThreadUtils.runOnUiThreadBlocking(new Runnable() {
...@@ -59,6 +60,15 @@ public class NotificationUIManagerTest extends ChromeShellTestBase { ...@@ -59,6 +60,15 @@ public class NotificationUIManagerTest extends ChromeShellTestBase {
pushNotificationInfo.setContentSetting(setting); pushNotificationInfo.setContentSetting(setting);
} }
}); });
String permission = runJavaScriptCodeInCurrentTab("Notification.permission");
if (setting == ContentSetting.ALLOW) {
assertEquals("\"granted\"", permission);
} else if (setting == ContentSetting.BLOCK) {
assertEquals("\"denied\"", permission);
} else {
assertEquals("\"default\"", permission);
}
} }
/** /**
...@@ -133,10 +143,8 @@ public class NotificationUIManagerTest extends ChromeShellTestBase { ...@@ -133,10 +143,8 @@ public class NotificationUIManagerTest extends ChromeShellTestBase {
@Feature({"Browser", "Notifications"}) @Feature({"Browser", "Notifications"})
public void testDefaultNotificationProperties() throws Exception { public void testDefaultNotificationProperties() throws Exception {
setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW); setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW);
assertEquals(PERMISSION_GRANTED, runJavaScriptCodeInCurrentTab("Notification.permission"));
Notification notification = showAndGetNotification("MyNotification", "{ body: 'Hello' }"); Notification notification = showAndGetNotification("MyNotification", "{ body: 'Hello' }");
assertNotNull(notification);
// Validate the contents of the notification. // Validate the contents of the notification.
assertEquals("MyNotification", notification.extras.getString(Notification.EXTRA_TITLE)); assertEquals("MyNotification", notification.extras.getString(Notification.EXTRA_TITLE));
...@@ -165,10 +173,8 @@ public class NotificationUIManagerTest extends ChromeShellTestBase { ...@@ -165,10 +173,8 @@ public class NotificationUIManagerTest extends ChromeShellTestBase {
@Feature({"Browser", "Notifications"}) @Feature({"Browser", "Notifications"})
public void testShowNotificationWithIcon() throws Exception { public void testShowNotificationWithIcon() throws Exception {
setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW); setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW);
assertEquals(PERMISSION_GRANTED, runJavaScriptCodeInCurrentTab("Notification.permission"));
Notification notification = showAndGetNotification("MyNotification", "{icon: 'icon.png'}"); Notification notification = showAndGetNotification("MyNotification", "{icon: 'icon.png'}");
assertNotNull(notification);
assertEquals("MyNotification", notification.extras.getString(Notification.EXTRA_TITLE)); assertEquals("MyNotification", notification.extras.getString(Notification.EXTRA_TITLE));
assertNotNull(notification.largeIcon); assertNotNull(notification.largeIcon);
...@@ -187,10 +193,8 @@ public class NotificationUIManagerTest extends ChromeShellTestBase { ...@@ -187,10 +193,8 @@ public class NotificationUIManagerTest extends ChromeShellTestBase {
@Feature({"Browser", "Notifications"}) @Feature({"Browser", "Notifications"})
public void testShowNotificationWithoutIcon() throws Exception { public void testShowNotificationWithoutIcon() throws Exception {
setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW); setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW);
assertEquals(PERMISSION_GRANTED, runJavaScriptCodeInCurrentTab("Notification.permission"));
Notification notification = showAndGetNotification("NoIconNotification", "{}"); Notification notification = showAndGetNotification("NoIconNotification", "{}");
assertNotNull(notification);
assertEquals("NoIconNotification", notification.extras.getString(Notification.EXTRA_TITLE)); assertEquals("NoIconNotification", notification.extras.getString(Notification.EXTRA_TITLE));
assertNotNull(notification.largeIcon); assertNotNull(notification.largeIcon);
...@@ -207,4 +211,51 @@ public class NotificationUIManagerTest extends ChromeShellTestBase { ...@@ -207,4 +211,51 @@ public class NotificationUIManagerTest extends ChromeShellTestBase {
assertEquals(generatedIcon.getWidth(), notification.largeIcon.getWidth()); assertEquals(generatedIcon.getWidth(), notification.largeIcon.getWidth());
assertEquals(generatedIcon.getHeight(), notification.largeIcon.getHeight()); assertEquals(generatedIcon.getHeight(), notification.largeIcon.getHeight());
} }
/*
* Verifies that starting the PendingIntent stored as the notification's content intent will
* start up the associated Service Worker, where the JavaScript code will close the notification
* by calling event.notification.close().
*/
@LargeTest
@Feature({"Browser", "Notifications"})
public void testNotificationContentIntentClosesNotification() throws Exception {
setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW);
Notification notification = showAndGetNotification("MyNotification", "{}");
// Sending the PendingIntent resembles activating the notification.
assertNotNull(notification.contentIntent);
notification.contentIntent.send();
// The Service Worker will close the notification upon receiving the notificationclick
// event. This will eventually bubble up to a call to cancel() in the NotificationManager.
assertTrue(waitForNotificationManagerMutation());
SparseArray<Notification> notifications = mMockNotificationManager.getNotifications();
assertEquals(0, notifications.size());
}
/**
* Verifies that creating a notification with an associated "tag" will cause any previous
* notification with the same tag to be dismissed prior to being shown.
*/
@MediumTest
@Feature({"Browser", "Notifications"})
public void testNotificationTagReplacement() throws Exception {
setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW);
Notification notification = showAndGetNotification("MyNotification", "{tag: 'myTag'}");
// Show the second notification with the same tag. We can't use showAndGetNotification for
// this purpose since a second notification would be shown.
runJavaScriptCodeInCurrentTab("showNotification('SecondNotification', {tag: 'myTag'});");
assertTrue(waitForNotificationManagerMutation());
// Verify that the notification was successfully replaced.
SparseArray<Notification> notifications = mMockNotificationManager.getNotifications();
assertEquals(1, notifications.size());
assertEquals("SecondNotification",
notifications.valueAt(0).extras.getString(Notification.EXTRA_TITLE));
}
} }
...@@ -8,3 +8,7 @@ addEventListener('message', function(event) { ...@@ -8,3 +8,7 @@ addEventListener('message', function(event) {
messagePort = event.data; messagePort = event.data;
messagePort.postMessage('ready'); messagePort.postMessage('ready');
}); });
addEventListener('notificationclick', function(event) {
event.notification.close();
});
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment