bug 1373850 add browser.theme.reset which allows unloading changes caused by browser.theme.update and resets to the default theme r?mattw draft
authorAndy McKay <amckay@mozilla.com>
Tue, 27 Jun 2017 12:57:19 -0700
changeset 600718 f981bd1124c31fb3a4f52653eebe3d8e2c2f2866
parent 591816 2d20b365eee19434657f6b365d310e8b70904d2b
child 635083 be47a3e1bb28aa6df7e36d3cfd5e5db34129635b
push id65859
push userbmo:amckay@mozilla.com
push dateTue, 27 Jun 2017 19:58:51 +0000
reviewersmattw
bugs1373850
milestone55.0a1
bug 1373850 add browser.theme.reset which allows unloading changes caused by browser.theme.update and resets to the default theme r?mattw MozReview-Commit-ID: 4wZQNsM2bFw
toolkit/components/extensions/ext-theme.js
toolkit/components/extensions/schemas/theme.json
toolkit/components/extensions/test/browser/browser_ext_themes_dynamic_updates.js
--- a/toolkit/components/extensions/ext-theme.js
+++ b/toolkit/components/extensions/ext-theme.js
@@ -276,12 +276,25 @@ this.theme = class extends ExtensionAPI 
             // WebExtensions using the Theme API will not have a theme defined
             // in the manifest. Therefore, we need to initialize the theme the
             // first time browser.theme.update is called.
             this.theme = new Theme(extension.baseURI, extension.logger);
           }
 
           this.theme.load(details);
         },
+        reset: () => {
+          if (!gThemesEnabled) {
+            // Return early if themes are disabled.
+            return;
+          }
+
+          if (!this.theme) {
+            // If no theme has been initialized, nothing to do.
+            return;
+          }
+
+          this.theme.unload();
+        },
       },
     };
   }
 };
--- a/toolkit/components/extensions/schemas/theme.json
+++ b/toolkit/components/extensions/schemas/theme.json
@@ -453,12 +453,19 @@
         "description": "Make complete or partial updates to the theme. Resolves when the update has completed.",
         "parameters": [
           {
             "name": "details",
             "$ref": "manifest.ThemeType",
             "description": "The properties of the theme to update."
           }
         ]
+      },
+      {
+        "name": "reset",
+        "type": "function",
+        "async": true,
+        "description": "Removes the updates made to the theme.",
+        "parameters": []
       }
     ]
   }
 ]
--- a/toolkit/components/extensions/test/browser/browser_ext_themes_dynamic_updates.js
+++ b/toolkit/components/extensions/test/browser/browser_ext_themes_dynamic_updates.js
@@ -7,32 +7,38 @@ const TEXT_COLOR_1 = "#fac96e";
 
 // PNG image data for the Mozilla dino head.
 const BACKGROUND_2 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAHWSURBVHjaYvz//z8DJQAggJiQOe/fv2fv7Oz8rays/N+VkfG/iYnJfyD/1+rVq7ffu3dPFpsBAAHEAHIBCJ85c8bN2Nj4vwsDw/8zQLwKiO8CcRoQu0DxqlWrdsHUwzBAAIGJmTNnPgYa9j8UqhFElwPxf2MIDeIrKSn9FwSJoRkAEEAM0DD4DzMAyPi/G+QKY4hh5WAXGf8PDQ0FGwJ22d27CjADAAIIrLmjo+MXA9R2kAHvGBA2wwx6B8W7od6CeQcggKCmCEL8bgwxYCbUIGTDVkHDBia+CuotgACCueD3TDQN75D4xmAvCoK9ARMHBzAw0AECiBHkAlC0Mdy7x9ABNA3obAZXIAa6iKEcGlMVQHwWyjYuL2d4v2cPg8vZswx7gHyAAAK7AOif7SAbOqCmn4Ha3AHFsIDtgPq/vLz8P4MSkJ2W9h8ggBjevXvHDo4FQUQg/kdypqCg4H8lUIACnQ/SOBMYI8bAsAJFPcj1AAEEjwVQqLpAbXmH5BJjqI0gi9DTAAgDBBCcAVLkgmQ7yKCZxpCQxqUZhAECCJ4XgMl493ug21ZD+aDAXH0WLM4A9MZPXJkJIIAwTAR5pQMalaCABQUULttBGCCAGCnNzgABBgAMJ5THwGvJLAAAAABJRU5ErkJggg==";
 const ACCENT_COLOR_2 = "#03fe03";
 const TEXT_COLOR_2 = "#0ef325";
 
 function hexToRGB(hex) {
   hex = parseInt((hex.indexOf("#") > -1 ? hex.substring(1) : hex), 16);
-  return [hex >> 16, (hex & 0x00FF00) >> 8, (hex & 0x0000FF)];
+  return "rgb(" + [hex >> 16, (hex & 0x00FF00) >> 8, (hex & 0x0000FF)].join(", ") + ")";
 }
 
-function validateTheme(backgroundImage, accentColor, textColor) {
+function validateTheme(backgroundImage, accentColor, textColor, isLWT) {
   let docEl = window.document.documentElement;
   let style = window.getComputedStyle(docEl);
 
-  Assert.ok(docEl.hasAttribute("lwtheme"), "LWT attribute should be set");
-  Assert.equal(docEl.getAttribute("lwthemetextcolor"), "bright",
-    "LWT text color attribute should be set");
+  if (isLWT) {
+    Assert.ok(docEl.hasAttribute("lwtheme"), "LWT attribute should be set");
+    Assert.equal(docEl.getAttribute("lwthemetextcolor"), "bright",
+      "LWT text color attribute should be set");
+  }
 
   Assert.ok(style.backgroundImage.includes(backgroundImage), "Expected correct background image");
-  Assert.equal(style.backgroundColor, "rgb(" + hexToRGB(accentColor).join(", ") + ")",
-    "Expected correct accent color");
-  Assert.equal(style.color, "rgb(" + hexToRGB(textColor).join(", ") + ")",
-    "Expected correct text color");
+  if (accentColor.startsWith("#")) {
+    accentColor = hexToRGB(accentColor);
+  }
+  if (textColor.startsWith("#")) {
+    textColor = hexToRGB(textColor);
+  }
+  Assert.equal(style.backgroundColor, accentColor, "Expected correct accent color");
+  Assert.equal(style.color, textColor, "Expected correct text color");
 }
 
 add_task(async function setup() {
   await SpecialPowers.pushPrefEnv({
     set: [["extensions.webextensions.themes.enabled", true]],
   });
 });
 
@@ -42,53 +48,64 @@ add_task(async function test_dynamic_the
       permissions: ["theme"],
     },
     files: {
       "image1.png": BACKGROUND_1,
       "image2.png": BACKGROUND_2,
     },
     background() {
       browser.test.onMessage.addListener((msg, details) => {
-        if (msg != "update-theme") {
-          browser.test.fail("expected 'update-theme' message");
+        if (msg === "update-theme") {
+          browser.theme.update(details).then(() => {
+            browser.test.sendMessage("theme-updated");
+          });
+        } else {
+          browser.theme.reset().then(() => {
+            browser.test.sendMessage("theme-reset");
+          });
         }
-
-        browser.theme.update(details);
-        browser.test.sendMessage("theme-updated");
       });
     },
   });
 
+  let defaultStyle = window.getComputedStyle(window.document.documentElement);
   await extension.startup();
 
   extension.sendMessage("update-theme", {
     "images": {
       "headerURL": "image1.png",
     },
     "colors": {
       "accentcolor": ACCENT_COLOR_1,
       "textcolor": TEXT_COLOR_1,
     },
   });
 
   await extension.awaitMessage("theme-updated");
 
-  validateTheme("image1.png", ACCENT_COLOR_1, TEXT_COLOR_1);
+  validateTheme("image1.png", ACCENT_COLOR_1, TEXT_COLOR_1, true);
 
   extension.sendMessage("update-theme", {
     "images": {
       "headerURL": "image2.png",
     },
     "colors": {
       "accentcolor": ACCENT_COLOR_2,
       "textcolor": TEXT_COLOR_2,
     },
   });
 
   await extension.awaitMessage("theme-updated");
 
-  validateTheme("image2.png", ACCENT_COLOR_2, TEXT_COLOR_2);
+  validateTheme("image2.png", ACCENT_COLOR_2, TEXT_COLOR_2, true);
+
+  extension.sendMessage("reset-theme");
+
+  await extension.awaitMessage("theme-reset");
+
+  let {backgroundImage, backgroundColor, color} = defaultStyle;
+  validateTheme(backgroundImage, backgroundColor, color, false);
 
   await extension.unload();
 
   let docEl = window.document.documentElement;
   Assert.ok(!docEl.hasAttribute("lwtheme"), "LWT attribute should not be set");
 });