Bug 1481179 - Stop extensions from changing the icon in the top-level menu r=mixedpuppy
authorRob Wu <rob@robwu.nl>
Mon, 13 Aug 2018 13:25:50 +0000
changeset 486326 6d03373ced3a298e6fdff35f9c9ae8638fded93f
parent 486325 bc15796e6fa77d268144f43427a9e388b6cbca91
child 486327 6b4359d502be4882df3a9f6f38da2796901e5280
push id9719
push userffxbld-merge
push dateFri, 24 Aug 2018 17:49:46 +0000
treeherdermozilla-beta@719ec98fba77 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy
bugs1481179
milestone63.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1481179 - Stop extensions from changing the icon in the top-level menu r=mixedpuppy Differential Revision: https://phabricator.services.mozilla.com/D3198
browser/components/extensions/parent/ext-menus.js
browser/components/extensions/test/browser/browser_ext_contextMenus_icons.js
--- a/browser/components/extensions/parent/ext-menus.js
+++ b/browser/components/extensions/parent/ext-menus.js
@@ -124,16 +124,20 @@ var gMenuBuilder = {
       return null;
     }
     rootElement.setAttribute("ext-type", "top-level-menu");
     rootElement = this.removeTopLevelMenuIfNeeded(rootElement);
 
     // Display the extension icon on the root element.
     if (root.extension.manifest.icons) {
       this.setMenuItemIcon(rootElement, root.extension, contextData, root.extension.manifest.icons);
+    } else {
+      // Undo changes from setMenuItemIcon:
+      rootElement.removeAttribute("class");
+      rootElement.removeAttribute("image");
     }
     return rootElement;
   },
 
   appendTopLevelElement(rootElement) {
     if (this.itemsToCleanUp.size === 0) {
       const separator = this.xulMenu.ownerDocument.createElement("menuseparator");
       this.itemsToCleanUp.add(separator);
--- a/browser/components/extensions/test/browser/browser_ext_contextMenus_icons.js
+++ b/browser/components/extensions/test/browser/browser_ext_contextMenus_icons.js
@@ -169,8 +169,79 @@ add_task(async function test_child_icon(
   let contextMenuChild3 = contextMenu.getElementsByAttribute("label", "child3")[0];
   confirmContextMenuIcon(contextMenuChild3, "green_icon.png");
 
   await closeContextMenu();
 
   await extension.unload();
   BrowserTestUtils.removeTab(tab);
 });
+
+add_task(async function test_manifest_without_icons() {
+  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, PAGE);
+
+  let redIconData = "iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAIAAADZrBkAAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4QYGEgw1XkM0ygAAABl0RVh0Q29tbWVudABDcmVhdGVkIHdpdGggR0lNUFeBDhcAAAAYSURBVCjPY/zPQA5gYhjVNqptVNsg1wYAItkBI/GNR3YAAAAASUVORK5CYII=";
+  const IMAGE_ARRAYBUFFER_RED = imageBufferFromDataURI(redIconData);
+
+  let greenIconData = "iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAIAAADZrBkAAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4QYGEg0rvVc46AAAABl0RVh0Q29tbWVudABDcmVhdGVkIHdpdGggR0lNUFeBDhcAAAAaSURBVCjPY+Q8xkAGYGJgGNU2qm1U2+DWBgBolADz1beTnwAAAABJRU5ErkJggg==";
+  const IMAGE_ARRAYBUFFER_GREEN = imageBufferFromDataURI(greenIconData);
+
+  let extension = ExtensionTestUtils.loadExtension({
+    manifest: {
+      "name": "contextMenus icons",
+      "permissions": ["contextMenus"],
+    },
+    files: {
+      "red.png": IMAGE_ARRAYBUFFER_RED,
+      "green.png": IMAGE_ARRAYBUFFER_GREEN,
+    },
+
+    background() {
+      browser.contextMenus.create({
+        title: "first item",
+        icons: {
+          18: "red.png",
+        },
+        onclick() {
+          browser.contextMenus.create({
+            title: "second item",
+            icons: {
+              18: "green.png",
+            },
+          }, () => {
+            browser.test.sendMessage("added-second-item");
+          });
+        },
+      }, () => {
+        browser.test.sendMessage("contextmenus-icons");
+      });
+    },
+  });
+
+  await extension.startup();
+  await extension.awaitMessage("contextmenus-icons");
+
+  let menu = await openContextMenu();
+  let items = menu.getElementsByAttribute("label", "first item");
+  is(items.length, 1, "Found first item");
+  // manifest.json does not declare icons, so the root menu item shouldn't have an icon either.
+  is(items[0].getAttribute("image"), "", "Root menu must not have an icon");
+
+  await closeExtensionContextMenu(items[0]);
+  await extension.awaitMessage("added-second-item");
+
+  menu = await openExtensionContextMenu();
+  items = document.querySelectorAll("#contentAreaContextMenu [ext-type='top-level-menu']");
+  is(items.length, 1, "Auto-generated root item exists");
+  is(items[0].getAttribute("image"), "", "Auto-generated menu root must not have an icon");
+
+  items = menu.getElementsByAttribute("label", "first item");
+  is(items.length, 1, "First child item should exist");
+  is(items[0].getAttribute("image").split("/").pop(), "red.png", "First item should have an icon");
+
+  items = menu.getElementsByAttribute("label", "second item");
+  is(items.length, 1, "Secobnd child item should exist");
+  is(items[0].getAttribute("image").split("/").pop(), "green.png", "Second item should have an icon");
+
+  await closeExtensionContextMenu();
+  await extension.unload();
+  BrowserTestUtils.removeTab(tab);
+});