summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_optionsPage_browser_style.js
blob: 9fbd4f7fd3deea5d9273203858b9117a4f160392 (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

requestLongerTimeout(2);

async function testOptionsBrowserStyle(optionsUI, assertMessage) {
  function optionsScript() {
    browser.test.onMessage.addListener((msgName, optionsUI, assertMessage) => {
      if (msgName !== "check-style") {
        browser.test.notifyFail("options-ui-browser_style");
      }

      let browserStyle =
        !("browser_style" in optionsUI) || optionsUI.browser_style;

      function verifyButton(buttonElement, expected) {
        let buttonStyle = window.getComputedStyle(buttonElement);
        let buttonBackgroundColor = buttonStyle.backgroundColor;
        if (browserStyle && expected.hasBrowserStyleClass) {
          browser.test.assertEq(
            "rgb(9, 150, 248)",
            buttonBackgroundColor,
            assertMessage
          );
        } else {
          browser.test.assertTrue(
            buttonBackgroundColor !== "rgb(9, 150, 248)",
            assertMessage
          );
        }
      }

      function verifyCheckboxOrRadio(element, expected) {
        let style = window.getComputedStyle(element);
        let styledBackground = element.checked
          ? "rgb(9, 150, 248)"
          : "rgb(255, 255, 255)";
        if (browserStyle && expected.hasBrowserStyleClass) {
          browser.test.assertEq(
            styledBackground,
            style.backgroundColor,
            assertMessage
          );
        } else {
          browser.test.assertTrue(
            style.backgroundColor != styledBackground,
            assertMessage
          );
        }
      }

      let normalButton = document.getElementById("normalButton");
      let browserStyleButton = document.getElementById("browserStyleButton");
      verifyButton(normalButton, { hasBrowserStyleClass: false });
      verifyButton(browserStyleButton, { hasBrowserStyleClass: true });

      let normalCheckbox1 = document.getElementById("normalCheckbox1");
      let normalCheckbox2 = document.getElementById("normalCheckbox2");
      let browserStyleCheckbox = document.getElementById(
        "browserStyleCheckbox"
      );
      verifyCheckboxOrRadio(normalCheckbox1, { hasBrowserStyleClass: false });
      verifyCheckboxOrRadio(normalCheckbox2, { hasBrowserStyleClass: false });
      verifyCheckboxOrRadio(browserStyleCheckbox, {
        hasBrowserStyleClass: true,
      });

      let normalRadio1 = document.getElementById("normalRadio1");
      let normalRadio2 = document.getElementById("normalRadio2");
      let browserStyleRadio = document.getElementById("browserStyleRadio");
      verifyCheckboxOrRadio(normalRadio1, { hasBrowserStyleClass: false });
      verifyCheckboxOrRadio(normalRadio2, { hasBrowserStyleClass: false });
      verifyCheckboxOrRadio(browserStyleRadio, { hasBrowserStyleClass: true });

      browser.test.notifyPass("options-ui-browser_style");
    });
    browser.test.sendMessage("options-ui-ready");
  }

  let extension = ExtensionTestUtils.loadExtension({
    useAddonManager: "temporary",

    manifest: {
      permissions: ["tabs"],
      options_ui: optionsUI,
    },
    files: {
      "options.html": `
        <!DOCTYPE html>
        <html>
          <button id="normalButton" name="button" class="default">Default</button>
          <button id="browserStyleButton" name="button" class="browser-style default">Default</button>

          <input id="normalCheckbox1" type="checkbox"/>
          <input id="normalCheckbox2" type="checkbox"/><label>Checkbox</label>
          <div class="browser-style">
            <input id="browserStyleCheckbox" type="checkbox"><label for="browserStyleCheckbox">Checkbox</label>
          </div>

          <input id="normalRadio1" type="radio"/>
          <input id="normalRadio2" type="radio"/><label>Radio</label>
          <div class="browser-style">
            <input id="browserStyleRadio" checked="" type="radio"><label for="browserStyleRadio">Radio</label>
          </div>

          <script src="options.js" type="text/javascript"></script>
        </html>`,
      "options.js": optionsScript,
    },
    background() {
      browser.runtime.openOptionsPage();
    },
  });

  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser);

  await extension.startup();
  await extension.awaitMessage("options-ui-ready");

  extension.sendMessage("check-style", optionsUI, assertMessage);
  await extension.awaitFinish("options-ui-browser_style");
  BrowserTestUtils.removeTab(tab);

  await extension.unload();
}

add_task(async function test_options_without_setting_browser_style() {
  await testOptionsBrowserStyle(
    {
      page: "options.html",
    },
    "Expected correct style when browser_style is excluded"
  );
});

add_task(async function test_options_with_browser_style_set_to_true() {
  await testOptionsBrowserStyle(
    {
      page: "options.html",
      browser_style: true,
    },
    "Expected correct style when browser_style is set to `true`"
  );
});

add_task(async function test_options_with_browser_style_set_to_false() {
  await testOptionsBrowserStyle(
    {
      page: "options.html",
      browser_style: false,
    },
    "Expected no style when browser_style is set to `false`"
  );
});