summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/extensions/test/browser/browser_html_options_ui_dark_theme.js
blob: 9261fa0a7e9284000bd3049a0d01a8e44ca0f7df (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { AddonTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/AddonTestUtils.sys.mjs"
);

AddonTestUtils.initMochitest(this);

const LIGHT_SCHEME_BG = "rgb(255, 255, 255)";
const LIGHT_SCHEME_FG = "rgb(0, 0, 0)";

// "browser.display.background_color.dark" pref value ("#1C1B22") maps to:
const DARK_SCHEME_BG = "rgb(28, 27, 34)";
const DARK_SCHEME_FG = "rgb(251, 251, 254)";

async function getColorsForOptionsUI({ browser_style, open_in_tab }) {
  let extension = ExtensionTestUtils.loadExtension({
    useAddonManager: "temporary",
    manifest: {
      options_ui: {
        browser_style,
        page: "options.html",
        open_in_tab,
      },
    },
    background() {
      browser.test.onMessage.addListener(msg => {
        browser.test.assertEq("openOptionsPage", msg, "expect openOptionsPage");
        browser.runtime.openOptionsPage();
      });
    },
    files: {
      "options.html": `<style>:root { color-scheme: dark light; }</style>
        <script src="options.js"></script>`,
      "options.js": () => {
        window.onload = () => {
          browser.test.sendMessage("options_ui_opened");
        };
      },
    },
  });

  await extension.startup();
  extension.sendMessage("openOptionsPage");
  await extension.awaitMessage("options_ui_opened");

  const tab = gBrowser.selectedTab;
  let optionsBrowser;
  if (open_in_tab) {
    optionsBrowser = tab.linkedBrowser;
    is(
      optionsBrowser.currentURI.spec,
      `moz-extension://${extension.uuid}/options.html`,
      "With open_in_tab=true, should open options.html in tab"
    );
  } else {
    // When not opening in a new tab, the inline options page is used.
    is(
      tab.linkedBrowser.currentURI.spec,
      "about:addons",
      "Without open_in_tab, should open about:addons"
    );
    optionsBrowser = tab.linkedBrowser.contentDocument.getElementById(
      "addon-inline-options"
    );
    is(
      optionsBrowser.currentURI.spec,
      `moz-extension://${extension.uuid}/options.html`,
      "Found options.html in inline options browser"
    );
  }

  let colors = await SpecialPowers.spawn(optionsBrowser, [], () => {
    let style = content.getComputedStyle(content.document.body);
    // Note: cannot use style.backgroundColor because it defaults to
    // "transparent" (aka rgba(0, 0, 0, 0)) which is meaningless.
    // So we have to use windowUtils.canvasBackgroundColor instead.
    return {
      bgColor: content.windowUtils.canvasBackgroundColor,
      fgColor: style.color,
    };
  });

  if (colors.bgColor === "rgba(0, 0, 0, 0)") {
    // windowUtils.canvasBackgroundColor may still report a "transparent"
    // background color when the options page is rendered inline in a <browser>
    // at about:addons. In that case, the background color of the container
    // element (i.e. the <browser>) is used to render the contents.
    Assert.ok(!open_in_tab, "Background only transparent without open_in_tab");
    let style = optionsBrowser.ownerGlobal.getComputedStyle(optionsBrowser);
    colors.bgColor = style.backgroundColor;
  }

  BrowserTestUtils.removeTab(tab);

  await extension.unload();
  return colors;
}

add_setup(async () => {
  // The test calls openOptionsPage, which may end up re-using an existing blank
  // tab. Upon closing that, the browser window of the test would close and the
  // test would get stuck. To avoid that, make sure that there is a dummy tab
  // around that keeps the window open.
  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, "data:,");
  registerCleanupFunction(() => {
    BrowserTestUtils.removeTab(tab);
  });
});

add_task(async function options_ui_open_in_tab_light() {
  await SpecialPowers.pushPrefEnv({ set: [["ui.systemUsesDarkTheme", 0]] });
  // Note: browser_style:true should be no-op when open_in_tab:true.
  // Therefore the result should be equivalent to the color of a normal web
  // page, instead of options_ui_inline_light.
  Assert.deepEqual(
    await getColorsForOptionsUI({ browser_style: true, open_in_tab: true }),
    { bgColor: LIGHT_SCHEME_BG, fgColor: LIGHT_SCHEME_FG }
  );
  await SpecialPowers.popPrefEnv();
});

add_task(async function options_ui_open_in_tab_dark() {
  await SpecialPowers.pushPrefEnv({ set: [["ui.systemUsesDarkTheme", 1]] });
  // Note: browser_style:true should be no-op when open_in_tab:true.
  // Therefore the result should be equivalent to the color of a normal web
  // page, instead of options_ui_inline_dark.
  Assert.deepEqual(
    await getColorsForOptionsUI({ browser_style: true, open_in_tab: true }),
    { bgColor: DARK_SCHEME_BG, fgColor: DARK_SCHEME_FG }
  );
  await SpecialPowers.popPrefEnv();
});

add_task(async function options_ui_light() {
  await SpecialPowers.pushPrefEnv({ set: [["ui.systemUsesDarkTheme", 0]] });
  Assert.deepEqual(
    await getColorsForOptionsUI({ browser_style: false, open_in_tab: false }),
    { bgColor: LIGHT_SCHEME_BG, fgColor: LIGHT_SCHEME_FG }
  );
  await SpecialPowers.popPrefEnv();
});

add_task(async function options_ui_dark() {
  await SpecialPowers.pushPrefEnv({ set: [["ui.systemUsesDarkTheme", 1]] });
  Assert.deepEqual(
    await getColorsForOptionsUI({ browser_style: false, open_in_tab: false }),
    { bgColor: DARK_SCHEME_BG, fgColor: DARK_SCHEME_FG }
  );
  await SpecialPowers.popPrefEnv();
});

add_task(async function options_ui_browser_style_true_light() {
  await SpecialPowers.pushPrefEnv({ set: [["ui.systemUsesDarkTheme", 0]] });
  Assert.deepEqual(
    await getColorsForOptionsUI({ browser_style: true, open_in_tab: false }),
    // rgb(34, 36, 38) = color: #222426 from extension.css
    { bgColor: LIGHT_SCHEME_BG, fgColor: "rgb(34, 36, 38)" }
  );
  await SpecialPowers.popPrefEnv();
});

add_task(async function options_ui_browser_style_true_dark() {
  await SpecialPowers.pushPrefEnv({ set: [["ui.systemUsesDarkTheme", 1]] });
  Assert.deepEqual(
    await getColorsForOptionsUI({ browser_style: true, open_in_tab: false }),
    { bgColor: DARK_SCHEME_BG, fgColor: DARK_SCHEME_FG }
  );
  await SpecialPowers.popPrefEnv();
});