Bug 1324429 - Context menu items without contexts should inherit it from their parent. draft
authorLuca Greco <lgreco@mozilla.com>
Wed, 21 Dec 2016 16:44:30 -0500
changeset 452664 8431f713b925
parent 451072 c23aa18ca567
child 540265 3f11c42763ca
push id39444
push userluca.greco@alcacoop.it
push dateWed, 21 Dec 2016 23:05:35 +0000
bugs1324429
milestone53.0a1
Bug 1324429 - Context menu items without contexts should inherit it from their parent. MozReview-Commit-ID: 9BKDdFZ6Hdd
browser/components/extensions/ext-contextMenus.js
browser/components/extensions/test/browser/browser_ext_contextMenus_chrome.js
--- a/browser/components/extensions/ext-contextMenus.js
+++ b/browser/components/extensions/ext-contextMenus.js
@@ -319,16 +319,17 @@ function getContexts(contextData) {
 function MenuItem(extension, createProperties, isRoot = false) {
   this.extension = extension;
   this.children = [];
   this.parent = null;
   this.tabManager = TabManager.for(extension);
 
   this.setDefaults();
   this.setProps(createProperties);
+
   if (!this.hasOwnProperty("_id")) {
     this.id = gNextMenuItemID++;
   }
   // If the item is not the root and has no parent
   // it must be a child of the root.
   if (!isRoot && !this.parent) {
     this.root.addChild(this);
   }
@@ -346,16 +347,22 @@ MenuItem.prototype = {
 
     if (createProperties.documentUrlPatterns != null) {
       this.documentUrlMatchPattern = new MatchPattern(this.documentUrlPatterns);
     }
 
     if (createProperties.targetUrlPatterns != null) {
       this.targetUrlMatchPattern = new MatchPattern(this.targetUrlPatterns);
     }
+
+    // If a child MenuItem does not specify any contexts, then it should
+    // inherit the contexts specified from its parent.
+    if (createProperties.parentId && !createProperties.contexts) {
+      this.contexts = this.parent.contexts;
+    }
   },
 
   setDefaults() {
     this.setProps({
       type: "normal",
       checked: false,
       contexts: ["all"],
       enabled: true,
--- a/browser/components/extensions/test/browser/browser_ext_contextMenus_chrome.js
+++ b/browser/components/extensions/test/browser/browser_ext_contextMenus_chrome.js
@@ -9,18 +9,18 @@ add_task(function* test_actionContextMen
     browser_action: {},
     permissions: ["contextMenus"],
   };
 
   async function background() {
     const contexts = ["page_action", "browser_action"];
 
     const parentId = browser.contextMenus.create({contexts, title: "parent"});
-    await browser.contextMenus.create({contexts, parentId, title: "click A"});
-    await browser.contextMenus.create({contexts, parentId, title: "click B"});
+    await browser.contextMenus.create({parentId, title: "click A"});
+    await browser.contextMenus.create({parentId, title: "click B"});
 
     for (let i = 1; i < 9; i++) {
       await browser.contextMenus.create({contexts, title: `click ${i}`});
     }
 
     browser.contextMenus.onClicked.addListener((info, tab) => {
       browser.test.sendMessage("click", {info, tab});
     });
@@ -64,18 +64,21 @@ add_task(function* test_actionContextMen
 });
 
 add_task(function* test_tabContextMenu() {
   const first = ExtensionTestUtils.loadExtension({
     manifest: {
       permissions: ["contextMenus"],
     },
     async background() {
-      await browser.contextMenus.create({title: "alpha", contexts: ["tab"]});
-      await browser.contextMenus.create({title: "beta", contexts: ["tab"]});
+      await browser.contextMenus.create({
+        id: "alpha-beta-parent", title: "alpha-beta parent", contexts: ["tab"],
+      });
+      await browser.contextMenus.create({parentId: "alpha-beta-parent", title: "alpha"});
+      await browser.contextMenus.create({parentId: "alpha-beta-parent", title: "beta"});
 
       browser.contextMenus.onClicked.addListener((info, tab) => {
         browser.test.sendMessage("click", {info, tab});
       });
 
       const [tab] = await browser.tabs.query({active: true});
       browser.test.sendMessage("ready", tab.id);
     },
@@ -98,17 +101,17 @@ add_task(function* test_tabContextMenu()
   const tabId = yield first.awaitMessage("ready");
   yield second.awaitMessage("ready");
 
   const menu = yield openTabContextMenu();
   const [separator, submenu, gamma] = Array.from(menu.children).slice(-3);
   is(separator.tagName, "menuseparator", "Separator before first extension item");
 
   is(submenu.tagName, "menu", "Correct submenu type");
-  is(submenu.label, "Generated extension", "Correct submenu title");
+  is(submenu.label, "alpha-beta parent", "Correct submenu title");
 
   is(gamma.tagName, "menuitem", "Third menu item type is correct");
   is(gamma.label, "gamma", "Third menu item label is correct");
 
   const popup = yield openSubmenu(submenu);
   is(popup, submenu.firstChild, "Correct submenu opened");
   is(popup.children.length, 2, "Correct number of submenu items");