Bug 1215376 - Test menu event order: onShown, onClicked, onHidden r?mixedpuppy draft
authorRob Wu <rob@robwu.nl>
Tue, 26 Sep 2017 19:25:22 +0200
changeset 670566 bd3e0a4f72a728bc786fb76f5e0768abb4a02b53
parent 670565 c5173f32aea2ef3a809f031c92a36107e6112bac
child 733277 50dc71f45da9708b7ab58606036265802056d1e8
push id81676
push userbmo:rob@robwu.nl
push dateTue, 26 Sep 2017 17:26:42 +0000
reviewersmixedpuppy
bugs1215376
milestone57.0a1
Bug 1215376 - Test menu event order: onShown, onClicked, onHidden r?mixedpuppy This test ensures that the menu events will always be fired in the natural event order. Two menus are tested: - A context menu in the page. - The tools menu. MozReview-Commit-ID: 4sLyvgMYMtE
browser/components/extensions/test/browser/browser-common.ini
browser/components/extensions/test/browser/browser_ext_menus_event_order.js
--- a/browser/components/extensions/test/browser/browser-common.ini
+++ b/browser/components/extensions/test/browser/browser-common.ini
@@ -81,16 +81,17 @@ skip-if = os == 'mac' # Bug 1374749 will
 [browser_ext_find.js]
 [browser_ext_geckoProfiler_symbolicate.js]
 [browser_ext_getViews.js]
 [browser_ext_identity_indication.js]
 [browser_ext_incognito_views.js]
 [browser_ext_incognito_popup.js]
 [browser_ext_lastError.js]
 [browser_ext_menus.js]
+[browser_ext_menus_event_order.js]
 [browser_ext_menus_events.js]
 [browser_ext_menus_refresh.js]
 [browser_ext_omnibox.js]
 skip-if = debug || asan # Bug 1354681
 [browser_ext_openPanel.js]
 [browser_ext_optionsPage_browser_style.js]
 [browser_ext_optionsPage_privileges.js]
 [browser_ext_pageAction_context.js]
new file mode 100644
--- /dev/null
+++ b/browser/components/extensions/test/browser/browser_ext_menus_event_order.js
@@ -0,0 +1,88 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+"use strict";
+
+const PAGE = "http://mochi.test:8888/browser/browser/components/extensions/test/browser/context.html";
+add_task(async function test_menus_click_event_sequence() {
+  async function background() {
+    let events = [];
+
+    browser.menus.onShown.addListener(() => { events.push("onShown"); });
+    browser.menus.onHidden.addListener(() => { events.push("onHidden"); });
+    // This onClicked listener is registered before the 'onclick' parameters in
+    // the browser.menus.create calls below, so it should fire first.
+    browser.menus.onClicked.addListener(() => { events.push("onClicked"); });
+
+    browser.menus.onHidden.addListener(() => {
+      // This listener is registered after the above onHidden listener, so it
+      // should be fired last.
+      browser.test.sendMessage("event_sequence", events);
+      events.length = 0;
+    });
+
+    browser.menus.create({
+      title: "item in page menu",
+      contexts: ["page"],
+      onclick() {
+        events.push("onclick parameter of page menu item");
+      },
+    });
+    browser.menus.create({
+      title: "item in tools menu",
+      contexts: ["tools_menu"],
+      onclick() {
+        events.push("onclick parameter of tools_menu menu item");
+      },
+    }, () => {
+      // The menus creation requests are expected to be handled in-order,
+      // so when the callback for the last menu creation request is called,
+      // we can assume that all menus have been registered.
+      browser.test.sendMessage("created menus");
+    });
+  }
+
+  const tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE);
+
+  let extension = ExtensionTestUtils.loadExtension({
+    background,
+    manifest: {
+      permissions: ["menus"],
+    },
+  });
+  await extension.startup();
+  info("Waiting for events and menu items to be registered");
+  await extension.awaitMessage("created menus");
+
+  async function verifyResults(menuType) {
+    info("Getting menu event info...");
+    let events = await extension.awaitMessage("event_sequence");
+    Assert.deepEqual(events, [
+      "onShown",
+      "onClicked",
+      `onclick parameter of ${menuType} menu item`,
+      "onHidden",
+    ], "Expected order of menus events");
+  }
+
+  {
+    info("Opening and closing page menu");
+    const menu = await openContextMenu("body");
+    const menuitem = menu.querySelector("menuitem[label='item in page menu']");
+    ok(menuitem, "Page menu item should exist");
+    await closeExtensionContextMenu(menuitem);
+    await verifyResults("page");
+  }
+
+  {
+    info("Opening and closing tools menu");
+    const menu = await openToolsMenu();
+    let menuitem = menu.querySelector("menuitem[label='item in tools menu']");
+    ok(menuitem, "Tools menu item should exist");
+    await closeToolsMenu(menuitem);
+    await verifyResults("tools_menu");
+  }
+
+  await extension.unload();
+  await BrowserTestUtils.removeTab(tab);
+});