Bug 1524619 - Part 2 - Add tests for notification permission prompts with user interaction. r=Ehsan,lina a=pascalc
authorJohann Hofmann <jhofmann@mozilla.com>
Tue, 19 Mar 2019 00:16:31 +0100
changeset 526319 9df7dbd02a5f46d9c4a149fea1b3e28dfa3f9023
parent 526318 68efdeb6246962430f930450613bb00292a9d19c
child 526320 eea064bfc1d35f1bfab2d12e2273a63cbc346d5f
push id2032
push userffxbld-merge
push dateMon, 13 May 2019 09:36:57 +0000
treeherdermozilla-release@455c1065dcbe [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersEhsan, lina, pascalc
bugs1524619
milestone67.0
Bug 1524619 - Part 2 - Add tests for notification permission prompts with user interaction. r=Ehsan,lina a=pascalc Differential Revision: https://phabricator.services.mozilla.com/D24259
browser/base/content/test/permissions/browser.ini
browser/base/content/test/permissions/browser_permissions_handling_user_input.js
browser/base/content/test/permissions/dummy.js
browser/base/content/test/permissions/permissions.html
--- a/browser/base/content/test/permissions/browser.ini
+++ b/browser/base/content/test/permissions/browser.ini
@@ -1,16 +1,19 @@
 [DEFAULT]
 support-files=
   head.js
   permissions.html
 
 [browser_canvas_fingerprinting_resistance.js]
 skip-if = debug || os == "linux" && asan # Bug 1522069
 [browser_permissions.js]
+[browser_permissions_handling_user_input.js]
+support-files=
+  dummy.js
 [browser_reservedkey.js]
 [browser_temporary_permissions.js]
 support-files =
   temporary_permissions_subframe.html
   ../webrtc/get_user_media.html
 [browser_autoplay_blocked.js]
 support-files =
   browser_autoplay_blocked.html
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/permissions/browser_permissions_handling_user_input.js
@@ -0,0 +1,81 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/ */
+
+"use strict";
+
+const ORIGIN = "https://example.com";
+const PERMISSIONS_PAGE = getRootDirectory(gTestPath).replace("chrome://mochitests/content", ORIGIN) + "permissions.html";
+
+function assertShown(task) {
+  return BrowserTestUtils.withNewTab(PERMISSIONS_PAGE, async function(browser) {
+    let popupshown = BrowserTestUtils.waitForEvent(PopupNotifications.panel, "popupshown");
+
+    await ContentTask.spawn(browser, null, task);
+
+    await popupshown;
+
+    ok(true, "Notification permission prompt was shown");
+  });
+}
+
+function assertNotShown(task) {
+  return BrowserTestUtils.withNewTab(PERMISSIONS_PAGE, async function(browser) {
+    let popupshown = BrowserTestUtils.waitForEvent(PopupNotifications.panel, "popupshown");
+
+    await ContentTask.spawn(browser, null, task);
+
+    let sawPrompt = await Promise.race([
+      popupshown.then(() => true),
+      // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
+      new Promise(c => setTimeout(() => c(false), 1000)),
+    ]);
+
+    is(sawPrompt, false, "Notification permission prompt was not shown");
+  });
+}
+
+// Tests that notification permissions are automatically denied without user interaction.
+add_task(async function testNotificationPermission() {
+  Services.prefs.setBoolPref("dom.webnotifications.requireuserinteraction", true);
+
+  // First test that when user interaction is required, requests
+  // with user interaction will show the permission prompt.
+
+  await assertShown(function() {
+    E10SUtils.wrapHandlingUserInput(content, true, function() {
+      content.document.getElementById("desktop-notification").click();
+    });
+  });
+
+  await assertShown(function() {
+    E10SUtils.wrapHandlingUserInput(content, true, function() {
+      content.document.getElementById("push").click();
+    });
+  });
+
+  // Now test that requests without user interaction will fail.
+
+  await assertNotShown(function() {
+    content.postMessage("push", "*");
+  });
+
+  await assertNotShown(async function() {
+    let response = await content.Notification.requestPermission();
+    is(response, "default", "The request was automatically denied");
+  });
+
+  Services.prefs.setBoolPref("dom.webnotifications.requireuserinteraction", false);
+
+  // Finally test that those requests will show a prompt again
+  // if the pref has been set to false.
+
+  await assertShown(function() {
+    content.postMessage("push", "*");
+  });
+
+  await assertShown(function() {
+    content.Notification.requestPermission();
+  });
+
+  Services.prefs.clearUserPref("dom.webnotifications.requireuserinteraction");
+});
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/permissions/dummy.js
@@ -0,0 +1,1 @@
+// Just a dummy file for testing.
--- a/browser/base/content/test/permissions/permissions.html
+++ b/browser/base/content/test/permissions/permissions.html
@@ -5,16 +5,34 @@
 <html dir="ltr" xml:lang="en-US" lang="en-US">
   <head>
     <meta charset="utf8">
   </head>
 <script>
 var gKeyDowns = 0;
 var gKeyPresses = 0;
 
+navigator.serviceWorker.register("dummy.js");
+
+function requestPush() {
+  return navigator.serviceWorker.ready.then(function(serviceWorkerRegistration) {
+    serviceWorkerRegistration.pushManager.subscribe();
+  });
+}
+
+window.onmessage = function(event) {
+  switch (event.data) {
+  case "push":
+      requestPush();
+      break;
+  }
+};
+
 </script>
   <body onkeydown="gKeyDowns++;" onkeypress="gKeyPresses++">
 	<!-- This page could eventually request permissions from content
 	     and make sure that chrome responds appropriately -->
   <button id="geo" onclick="navigator.geolocation.getCurrentPosition(() => {})">Geolocation</button>
+  <button id="desktop-notification" onclick="Notification.requestPermission()">Notifications</button>
+  <button id="push" onclick="requestPush()">Push Notifications</button>
   <button id="camera" onclick="navigator.mediaDevices.getUserMedia({video: true, fake: true})">Camera</button>
   </body>
 </html>