Bug 1226423: [wbext] Add tests for panel popup API injection. r=billm, a=al
☠☠ backed out by c7a2630ceab0 ☠ ☠
authorKris Maglione <maglione.k@gmail.com>
Fri, 20 Nov 2015 15:16:31 -0800
changeset 305624 e3b938c0cde25153e263e91c2d1a6d1ca5f1f0f7
parent 305623 08c70292a9a80666d51572676e1175100468d318
child 305625 337f0ed43821944b9ba77dd4b98083192413a765
push id1001
push userraliiev@mozilla.com
push dateMon, 18 Jan 2016 19:06:03 +0000
treeherdermozilla-release@8b89261f3ac4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbillm, al
bugs1226423
milestone44.0a2
Bug 1226423: [wbext] Add tests for panel popup API injection. r=billm, a=al
browser/components/extensions/test/browser/browser.ini
browser/components/extensions/test/browser/browser_ext_popup_api_injection.js
browser/components/extensions/test/browser/file_popup_api_injection_a.html
browser/components/extensions/test/browser/file_popup_api_injection_b.html
--- a/browser/components/extensions/test/browser/browser.ini
+++ b/browser/components/extensions/test/browser/browser.ini
@@ -1,23 +1,27 @@
 [DEFAULT]
 support-files =
   head.js
   context.html
   ctxmenu-image.png
   context_tabs_onUpdated_page.html
   context_tabs_onUpdated_iframe.html
+  file_popup_api_injection_a.html
+  file_popup_api_injection_b.html
 
 [browser_ext_simple.js]
 [browser_ext_currentWindow.js]
 [browser_ext_browserAction_simple.js]
 [browser_ext_browserAction_pageAction_icon.js]
 [browser_ext_browserAction_context.js]
 [browser_ext_pageAction_context.js]
 [browser_ext_pageAction_popup.js]
+[browser_ext_browserAction_popup.js]
+[browser_ext_popup_api_injection.js]
 [browser_ext_contextMenus.js]
 [browser_ext_getViews.js]
 [browser_ext_tabs_executeScript.js]
 [browser_ext_tabs_query.js]
 [browser_ext_tabs_update.js]
 [browser_ext_tabs_onUpdated.js]
 [browser_ext_tabs_sendMessage.js]
 [browser_ext_windows_update.js]
new file mode 100644
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_popup_api_injection.js
@@ -0,0 +1,106 @@
+/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
+/* vim: set sts=2 sw=2 et tw=80: */
+"use strict";
+
+add_task(function* testPageActionPopup() {
+  const BASE = "http://example.com/browser/browser/components/extensions/test/browser";
+
+  let extension = ExtensionTestUtils.loadExtension({
+    manifest: {
+      "browser_action": {
+        "default_popup": `${BASE}/file_popup_api_injection_a.html`,
+      },
+      "page_action": {
+        "default_popup": `${BASE}/file_popup_api_injection_b.html`,
+      },
+    },
+
+    files: {
+      "popup-a.html": String.raw`<html><head><meta charset="utf-8"><script type="application/javascript">
+        browser.test.sendMessage("from-popup-a");
+      </script></head></html>`,
+
+      "popup-b.html": String.raw`<html><head><meta charset="utf-8"><script type="application/javascript">
+        browser.test.sendMessage("from-popup-b");
+      </script></head></html>`,
+    },
+
+    background: function() {
+      let tabId
+      browser.tabs.query({ active: true, currentWindow: true }, tabs => {
+        tabId = tabs[0].id;
+        browser.pageAction.show(tabId);
+        browser.test.sendMessage("ready");
+      });
+
+      browser.test.onMessage.addListener(() => {
+        browser.browserAction.setPopup({ popup: "/popup-a.html" });
+        browser.pageAction.setPopup({ tabId, popup: "popup-b.html" });
+
+        browser.test.sendMessage("ok");
+      });
+    },
+  });
+
+  let browserActionId = makeWidgetId(extension.id) + "-browser-action";
+  let pageActionId = makeWidgetId(extension.id) + "-page-action";
+
+  function openPopup(buttonId) {
+    let button = document.getElementById(buttonId);
+    if (buttonId == pageActionId) {
+      // TODO: I don't know why a proper synthesized event doesn't work here.
+      button.dispatchEvent(new MouseEvent("click", {}));
+    } else {
+      EventUtils.synthesizeMouseAtCenter(button, {}, window);
+    }
+  };
+
+  let promiseConsoleMessage = pattern => new Promise(resolve => {
+    Services.console.registerListener(function listener(msg) {
+      if (pattern.test(msg.message)) {
+        resolve(msg.message);
+        Services.console.unregisterListener(listener);
+      }
+    });
+  });
+
+  yield extension.startup();
+  yield extension.awaitMessage("ready");
+
+
+  // Check that unprivileged documents don't get the API.
+  // BrowserAction:
+  let awaitMessage = promiseConsoleMessage(/WebExt Privilege Escalation: BrowserAction/);
+  SimpleTest.expectUncaughtException();
+  openPopup(browserActionId);
+
+  let message = yield awaitMessage;
+  ok(message.includes("WebExt Privilege Escalation: BrowserAction: typeof(browser) = undefined"),
+     `No BrowserAction API injection`);
+
+  // PageAction
+  awaitMessage = promiseConsoleMessage(/WebExt Privilege Escalation: PageAction/);
+  SimpleTest.expectUncaughtException();
+  openPopup(pageActionId);
+
+  message = yield awaitMessage;
+  ok(message.includes("WebExt Privilege Escalation: PageAction: typeof(browser) = undefined"),
+     `No PageAction API injection: ${message}`);
+
+  SimpleTest.expectUncaughtException(false);
+
+
+  // Check that privileged documents *do* get the API.
+  extension.sendMessage("next");
+  yield extension.awaitMessage("ok");
+
+
+  // Check that unprivileged documents don't get the API.
+  openPopup(browserActionId);
+  yield extension.awaitMessage("from-popup-a");
+
+  openPopup(pageActionId);
+  yield extension.awaitMessage("from-popup-b");
+
+  yield extension.unload();
+});
new file mode 100644
--- /dev/null
+++ b/browser/components/extensions/test/browser/file_popup_api_injection_a.html
@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <meta charset="utf-8">
+  <script type="application/javascript">
+    throw new Error(`WebExt Privilege Escalation: BrowserAction: typeof(browser) = ${typeof(browser)}`);
+  </script>
+</head>
+</html>
new file mode 100644
--- /dev/null
+++ b/browser/components/extensions/test/browser/file_popup_api_injection_b.html
@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <meta charset="utf-8">
+  <script type="application/javascript">
+    throw new Error(`WebExt Privilege Escalation: PageAction: typeof(browser) = ${typeof(browser)}`);
+  </script>
+</head>
+</html>