Bug 1645789 - Also fix the case when we're resetting the styles and the document has no default style. r=Gijs
authorEmilio Cobos Álvarez <emilio@crisal.io>
Mon, 15 Jun 2020 16:10:41 +0000
changeset 535711 bf23c417bc987ed71dd4e638eb052246503299d4
parent 535710 c2ee72003dc947099fd66dd2975dd9bf38e21330
child 535712 9a3a87c7c7bf250be4db8d5875e241e8f7dc9e80
push id37508
push userbtara@mozilla.com
push dateMon, 15 Jun 2020 21:48:38 +0000
treeherdermozilla-central@567a8768593e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersGijs
bugs1645789
milestone79.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 1645789 - Also fix the case when we're resetting the styles and the document has no default style. r=Gijs Differential Revision: https://phabricator.services.mozilla.com/D79701
browser/actors/PageStyleChild.jsm
browser/base/content/test/general/browser.ini
browser/base/content/test/general/browser_page_style_menu.js
browser/base/content/test/general/page_style_only_alternates.html
--- a/browser/actors/PageStyleChild.jsm
+++ b/browser/actors/PageStyleChild.jsm
@@ -52,21 +52,23 @@ class PageStyleChild extends JSWindowAct
    * Switch the stylesheet so that only the sheet with the given title is enabled.
    */
   _switchStylesheet(title) {
     let docStyleSheets = this.document.styleSheets;
 
     // Does this doc contain a stylesheet with this title?
     // If not, it's a subframe's stylesheet that's being changed,
     // so no need to disable stylesheets here.
-    let docContainsStyleSheet = false;
-    for (let docStyleSheet of docStyleSheets) {
-      if (docStyleSheet.title === title) {
-        docContainsStyleSheet = true;
-        break;
+    let docContainsStyleSheet = !title;
+    if (title) {
+      for (let docStyleSheet of docStyleSheets) {
+        if (docStyleSheet.title === title) {
+          docContainsStyleSheet = true;
+          break;
+        }
       }
     }
 
     for (let docStyleSheet of docStyleSheets) {
       if (docStyleSheet.title) {
         if (docContainsStyleSheet) {
           docStyleSheet.disabled = docStyleSheet.title !== title;
         }
--- a/browser/base/content/test/general/browser.ini
+++ b/browser/base/content/test/general/browser.ini
@@ -267,16 +267,18 @@ skip-if = toolkit == "windows" # Disable
 [browser_minimize.js]
 # DO NOT ADD MORE TESTS HERE. USE A TOPICAL DIRECTORY INSTEAD.
 fail-if = (os == 'linux' && os_version == '18.04')  # Bug 1600177
 [browser_modifiedclick_inherit_principal.js]
 # DO NOT ADD MORE TESTS HERE. USE A TOPICAL DIRECTORY INSTEAD.
 [browser_new_http_window_opened_from_file_tab.js]
 # DO NOT ADD MORE TESTS HERE. USE A TOPICAL DIRECTORY INSTEAD.
 [browser_page_style_menu.js]
+support-files =
+  page_style_only_alternates.html
 # DO NOT ADD MORE TESTS HERE. USE A TOPICAL DIRECTORY INSTEAD.
 [browser_page_style_menu_update.js]
 # DO NOT ADD MORE TESTS HERE. USE A TOPICAL DIRECTORY INSTEAD.
 [browser_plainTextLinks.js]
 # DO NOT ADD MORE TESTS HERE. USE A TOPICAL DIRECTORY INSTEAD.
 [browser_printpreview.js]
 skip-if = (os == 'win') || (os == 'linux' && bits == 64 && os_version == '18.04') # Bug 1384127
 # DO NOT ADD MORE TESTS HERE. USE A TOPICAL DIRECTORY INSTEAD.
--- a/browser/base/content/test/general/browser_page_style_menu.js
+++ b/browser/base/content/test/general/browser_page_style_menu.js
@@ -1,37 +1,46 @@
 "use strict";
 
-const PAGE =
-  "http://example.com/browser/browser/base/content/test/general/page_style_sample.html";
+function fillPopupAndGetItems() {
+  let menupopup = document.getElementById("pageStyleMenu").menupopup;
+  gPageStyleMenu.fillPopup(menupopup);
+  return Array.from(menupopup.querySelectorAll("menuseparator ~ menuitem"));
+}
+
+function getRootColor() {
+  return SpecialPowers.spawn(gBrowser.selectedBrowser, [], function() {
+    return content.document.defaultView.getComputedStyle(
+      content.document.documentElement
+    ).color;
+  });
+}
+
+const RED = "rgb(255, 0, 0)";
+const LIME = "rgb(0, 255, 0)";
+const BLUE = "rgb(0, 0, 255)";
 
 /*
  * Test that the right stylesheets do (and others don't) show up
  * in the page style menu.
  */
-add_task(async function() {
+add_task(async function test_menu() {
+  const PAGE =
+    "http://example.com/browser/browser/base/content/test/general/page_style_sample.html";
+
   let tab = await BrowserTestUtils.openNewForegroundTab(
     gBrowser,
     "about:blank",
     false
   );
   let browser = tab.linkedBrowser;
   await BrowserTestUtils.loadURI(browser, PAGE);
   await promiseStylesheetsLoaded(tab, 17);
 
-  let menupopup = document.getElementById("pageStyleMenu").menupopup;
-  gPageStyleMenu.fillPopup(menupopup);
-
-  let menuitems = [];
-  let current = menupopup.querySelector("menuseparator");
-  while (current.nextElementSibling) {
-    current = current.nextElementSibling;
-    menuitems.push(current);
-  }
-
+  let menuitems = fillPopupAndGetItems();
   let items = menuitems.map(el => ({
     label: el.getAttribute("label"),
     checked: el.getAttribute("checked") == "true",
   }));
 
   let validLinks = await SpecialPowers.spawn(
     gBrowser.selectedBrowser,
     [items],
@@ -76,45 +85,74 @@ add_task(async function() {
       }
       return contentValidLinks;
     }
   );
 
   ok(menuitems.length, "At least one item in the menu");
   is(menuitems.length, validLinks, "all valid links found");
 
-  function getRootColor() {
-    return SpecialPowers.spawn(gBrowser.selectedBrowser, [], function() {
-      return content.document.defaultView.getComputedStyle(content.document.documentElement).color;
-    });
-  }
-
-  is(await getRootColor(), "rgb(0, 255, 0)", "Root should be lime (styles should apply)");
+  is(await getRootColor(), LIME, "Root should be lime (styles should apply)");
 
   let disableStyles = document.getElementById("menu_pageStyleNoStyle");
   let defaultStyles = document.getElementById("menu_pageStylePersistentOnly");
   let otherStyles = menuitems[menuitems.length - 1];
 
   // Assert that the menu works as expected.
   disableStyles.click();
 
   await TestUtils.waitForCondition(async function() {
     let color = await getRootColor();
-    return color != "rgb(0, 255, 0)" && color != "rgb(0, 0, 255)";
+    return color != LIME && color != BLUE;
   }, "ensuring disabled styles work");
 
   otherStyles.click();
 
   await TestUtils.waitForCondition(async function() {
     let color = await getRootColor();
-    return color == "rgb(0, 0, 255)";
+    return color == BLUE;
   }, "ensuring alternate styles work. clicking on: " + otherStyles.label);
 
   defaultStyles.click();
 
   info("ensuring default styles work");
   await TestUtils.waitForCondition(async function() {
     let color = await getRootColor();
-    return color == "rgb(0, 255, 0)";
+    return color == LIME;
   }, "ensuring default styles work");
 
   BrowserTestUtils.removeTab(tab);
 });
+
+add_task(async function test_default_style_with_no_sheets() {
+  const PAGE =
+    "http://example.com/browser/browser/base/content/test/general/page_style_only_alternates.html";
+  await BrowserTestUtils.withNewTab(
+    {
+      gBrowser,
+      url: PAGE,
+      waitForLoad: true,
+    },
+    async function(browser) {
+      await promiseStylesheetsLoaded(browser, 2);
+
+      let menuitems = fillPopupAndGetItems();
+      is(menuitems.length, 2, "Should've found two style sets");
+      is(
+        await getRootColor(),
+        BLUE,
+        "First found style should become the preferred one and apply"
+      );
+
+      // Reset the styles.
+      document.getElementById("menu_pageStylePersistentOnly").click();
+      await TestUtils.waitForCondition(async function() {
+        let color = await getRootColor();
+        return color != BLUE && color != RED;
+      });
+
+      ok(
+        true,
+        "Should reset the style properly even if there are no non-alternate stylesheets"
+      );
+    }
+  );
+});
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/general/page_style_only_alternates.html
@@ -0,0 +1,5 @@
+<!doctype html>
+<title>Test for the page style menu</title>
+<!-- We only have alternates here intentionally. "Basic Page Style" should still work and remove the blue / red colors -->
+<style title="blue">:root { color: blue }</style>
+<style title="red">:root { color: red}</style>