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

Unified Diff: chrome/android/javatests/src/org/chromium/chrome/browser/push_messaging/PushMessagingTest.java

Issue 2323753002: Add tests for dismissing/denying push permission prompt on Android (Closed)
Patch Set: Move loadUrl to setUp Created 4 years, 3 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/android/javatests/src/org/chromium/chrome/browser/push_messaging/PushMessagingTest.java
diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/push_messaging/PushMessagingTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/push_messaging/PushMessagingTest.java
index 7961f36b03d7f52943ff01c9a22d99e2948fda6b..b8172538e5a61e6a8796f4434b5988794e32bf61 100644
--- a/chrome/android/javatests/src/org/chromium/chrome/browser/push_messaging/PushMessagingTest.java
+++ b/chrome/android/javatests/src/org/chromium/chrome/browser/push_messaging/PushMessagingTest.java
@@ -32,7 +32,6 @@ import org.chromium.content.browser.test.util.CallbackHelper;
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 org.chromium.content_public.browser.WebContents;
import java.util.List;
import java.util.concurrent.TimeoutException;
@@ -69,6 +68,7 @@ public class PushMessagingTest
}
});
mPushTestPage = getTestServer().getURL(PUSH_TEST_PAGE);
+ loadUrl(mPushTestPage);
}
@Override
@@ -89,37 +89,98 @@ public class PushMessagingTest
}
/**
- * Verifies that PushManager.subscribe() requests permission successfully.
+ * Verifies that PushManager.subscribe() fails if Notifications permission was already denied.
*/
@MediumTest
@Feature({"Browser", "PushMessaging"})
- public void testPushPermissionInfobar() throws InterruptedException, TimeoutException {
+ public void testNotificationsPermissionDenied() throws InterruptedException, TimeoutException {
+ // Deny Notifications permission before trying to subscribe Push.
+ setNotificationContentSettingForCurrentOrigin(ContentSetting.BLOCK);
+ assertEquals("\"denied\"", runScriptBlocking("Notification.permission"));
+
+ // Reload page to ensure the block is persisted.
loadUrl(mPushTestPage);
- WebContents webContents = getActivity().getActivityTab().getWebContents();
+
+ // PushManager.subscribePush() should fail immediately without showing an infobar.
+ runScriptAndWaitForTitle("subscribePush()",
+ "subscribe fail: NotAllowedError: Registration failed - permission denied");
assertEquals(0, getInfoBars().size());
- // Notifications permission should not yet be granted.
- assertEquals("\"default\"", JavaScriptUtils.executeJavaScriptAndWaitForResult(
- webContents, "Notification.permission"));
+ // Notifications permission should still be denied.
+ assertEquals("\"denied\"", runScriptBlocking("Notification.permission"));
+ }
+
+ /**
+ * Verifies that PushManager.subscribe() fails if permission is dismissed or blocked.
+ */
+ @MediumTest
+ @Feature({"Browser", "PushMessaging"})
+ public void testPushPermissionDenied() throws InterruptedException, TimeoutException {
+ // Notifications permission should initially be prompt.
+ assertEquals("\"default\"", runScriptBlocking("Notification.permission"));
// PushManager.subscribePush() should show the notifications infobar.
- JavaScriptUtils.executeJavaScript(webContents, "subscribePush()");
- CriteriaHelper.pollUiThread(new Criteria() {
- @Override
- public boolean isSatisfied() {
- return !getInfoBars().isEmpty();
- }
- });
- List<InfoBar> infoBars = getInfoBars();
- assertEquals(1, infoBars.size());
+ assertEquals(0, getInfoBars().size());
+ runScript("subscribePush()");
+ InfoBar infoBar = getInfobarBlocking();
+
+ // Dismissing the infobar should cause subscribe() to fail.
+ assertTrue(InfoBarUtil.clickCloseButton(infoBar));
+ waitForInfobarToClose();
+ waitForTitle(getActivity().getActivityTab(),
+ "subscribe fail: NotAllowedError: Registration failed - permission denied");
+
+ // Notifications permission should still be prompt.
+ assertEquals("\"default\"", runScriptBlocking("Notification.permission"));
+
+ runScriptAndWaitForTitle("sendToTest('reset title')", "reset title");
+
+ // PushManager.subscribePush() should show the notifications infobar again.
+ runScript("subscribePush()");
+ infoBar = getInfobarBlocking();
+
+ // Denying the infobar should cause subscribe() to fail.
+ assertTrue(InfoBarUtil.clickSecondaryButton(infoBar));
+ waitForInfobarToClose();
+ waitForTitle(getActivity().getActivityTab(),
+ "subscribe fail: NotAllowedError: Registration failed - permission denied");
+
+ // This should have caused notifications permission to become denied.
+ assertEquals("\"denied\"", runScriptBlocking("Notification.permission"));
+
+ // Reload page to ensure the block is persisted.
+ loadUrl(mPushTestPage);
+
+ // PushManager.subscribePush() should now fail immediately without showing an infobar.
+ runScriptAndWaitForTitle("subscribePush()",
+ "subscribe fail: NotAllowedError: Registration failed - permission denied");
+ assertEquals(0, getInfoBars().size());
+
+ // Notifications permission should still be denied.
+ assertEquals("\"denied\"", runScriptBlocking("Notification.permission"));
+ }
+
+ /**
+ * Verifies that PushManager.subscribe() requests permission correctly.
+ */
+ @MediumTest
+ @Feature({"Browser", "PushMessaging"})
+ public void testPushPermissionGranted() throws InterruptedException, TimeoutException {
+ // Notifications permission should initially be prompt.
+ assertEquals("\"default\"", runScriptBlocking("Notification.permission"));
+
+ // PushManager.subscribePush() should show the notifications infobar.
+ assertEquals(0, getInfoBars().size());
+ runScript("subscribePush()");
+ InfoBar infoBar = getInfobarBlocking();
// Accepting the infobar should cause subscribe() to succeed.
- assertTrue(InfoBarUtil.clickPrimaryButton(infoBars.get(0)));
+ assertTrue(InfoBarUtil.clickPrimaryButton(infoBar));
+ waitForInfobarToClose();
waitForTitle(getActivity().getActivityTab(), "subscribe ok");
// This should have caused notifications permission to become granted.
- assertEquals("\"granted\"", JavaScriptUtils.executeJavaScriptAndWaitForResult(
- webContents, "Notification.permission"));
+ assertEquals("\"granted\"", runScriptBlocking("Notification.permission"));
}
/**
@@ -128,7 +189,6 @@ public class PushMessagingTest
@MediumTest
@Feature({"Browser", "PushMessaging"})
public void testPushAndShowNotification() throws InterruptedException, TimeoutException {
- loadUrl(mPushTestPage);
setNotificationContentSettingForCurrentOrigin(ContentSetting.ALLOW);
runScriptAndWaitForTitle("subscribePush()", "subscribe ok");
@@ -147,8 +207,7 @@ public class PushMessagingTest
@LargeTest
@Feature({"Browser", "PushMessaging"})
public void testDefaultNotification() throws InterruptedException, TimeoutException {
- // Load the push test page into the first tab.
- loadUrl(mPushTestPage);
+ // Start off using the tab loaded in setUp().
assertEquals(1, getActivity().getCurrentTabModel().getCount());
Tab tab = getActivity().getActivityTab();
assertEquals(mPushTestPage, tab.getUrl());
@@ -187,6 +246,21 @@ public class PushMessagingTest
}
/**
+ * Runs {@code script} in the current tab but does not wait for the result.
+ */
+ private void runScript(String script) {
+ JavaScriptUtils.executeJavaScript(getActivity().getActivityTab().getWebContents(), script);
+ }
+
+ /**
+ * Runs {@code script} in the current tab and returns its synchronous result in JSON format.
+ */
+ private String runScriptBlocking(String script) throws InterruptedException, TimeoutException {
+ return JavaScriptUtils.executeJavaScriptAndWaitForResult(
+ getActivity().getActivityTab().getWebContents(), script);
+ }
+
+ /**
* Runs {@code script} in the current tab and waits for the tab title to change to
* {@code expectedTitle}.
*/
@@ -234,4 +308,26 @@ public class PushMessagingTest
assertEquals(expectedTitle, tab.getTitle());
}
}
+
+ private InfoBar getInfobarBlocking() throws InterruptedException {
+ CriteriaHelper.pollUiThread(new Criteria() {
+ @Override
+ public boolean isSatisfied() {
+ return !getInfoBars().isEmpty();
+ }
+ });
+ List<InfoBar> infoBars = getInfoBars();
+ assertEquals(1, infoBars.size());
+ return infoBars.get(0);
+ }
+
+ private void waitForInfobarToClose() throws InterruptedException {
+ CriteriaHelper.pollUiThread(new Criteria() {
+ @Override
+ public boolean isSatisfied() {
+ return getInfoBars().isEmpty();
+ }
+ });
+ assertEquals(0, getInfoBars().size());
+ }
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698