summaryrefslogtreecommitdiffstats
path: root/browser/base/content/test/pageStyle/browser_page_style_menu.js
blob: 2ae635f16b85e24aa0a6b3ebe9d5e1b2825cb6b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

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)";

const kStyleSheetsInPageStyleSample = 18;

/*
 * Test that the right stylesheets do (and others don't) show up
 * in the page style menu.
 */
add_task(async function test_menu() {
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "about:blank",
    false
  );
  let browser = tab.linkedBrowser;
  BrowserTestUtils.loadURIString(browser, WEB_ROOT + "page_style_sample.html");
  await promiseStylesheetsLoaded(browser, kStyleSheetsInPageStyleSample);

  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],
    function (contentItems) {
      let contentValidLinks = 0;
      for (let el of content.document.querySelectorAll("link, style")) {
        var title = el.getAttribute("title");
        var rel = el.getAttribute("rel");
        var media = el.getAttribute("media");
        var idstring =
          el.nodeName +
          " " +
          (title ? title : "without title and") +
          ' with rel="' +
          rel +
          '"' +
          (media ? ' and media="' + media + '"' : "");

        var item = contentItems.filter(aItem => aItem.label == title);
        var found = item.length == 1;
        var checked = found && item[0].checked;

        switch (el.getAttribute("data-state")) {
          case "0":
            ok(!found, idstring + " should not show up in page style menu");
            break;
          case "1":
            contentValidLinks++;
            ok(found, idstring + " should show up in page style menu");
            ok(!checked, idstring + " should not be selected");
            break;
          case "2":
            contentValidLinks++;
            ok(found, idstring + " should show up in page style menu");
            ok(checked, idstring + " should be selected");
            break;
          default:
            throw new Error(
              "data-state attribute is missing or has invalid value"
            );
        }
      }
      return contentValidLinks;
    }
  );

  ok(menuitems.length, "At least one item in the menu");
  is(menuitems.length, validLinks, "all valid links found");

  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[0].parentNode.querySelector("[label='28']");

  // Assert that the menu works as expected.
  disableStyles.click();

  await TestUtils.waitForCondition(async function () {
    let color = await getRootColor();
    return color != LIME && color != BLUE;
  }, "ensuring disabled styles work");

  otherStyles.click();

  await TestUtils.waitForCondition(async function () {
    let color = await getRootColor();
    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 == LIME;
  }, "ensuring default styles work");

  BrowserTestUtils.removeTab(tab);
});

add_task(async function test_default_style_with_no_sheets() {
  const PAGE = WEB_ROOT + "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"
      );
    }
  );
});

add_task(async function test_page_style_file() {
  const FILE_PAGE = Services.io.newFileURI(
    new FileUtils.File(getTestFilePath("page_style_sample.html"))
  ).spec;
  await BrowserTestUtils.withNewTab(FILE_PAGE, async function (browser) {
    await promiseStylesheetsLoaded(browser, kStyleSheetsInPageStyleSample);
    let menuitems = fillPopupAndGetItems();
    is(
      menuitems.length,
      kStyleSheetsInPageStyleSample,
      "Should have the right amount of items even for file: URI."
    );
  });
});