summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/browser/browser_ext_themes_ntp_colors.js
blob: 8e2f5446c9e84123e965213bba4b9660a02725f4 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"use strict";
// This test checks whether the new tab page color properties work.

function waitForAboutNewTabReady(browser, url) {
  // Stop-gap fix for https://bugzilla.mozilla.org/show_bug.cgi?id=1697196#c24
  return SpecialPowers.spawn(browser, [url], async url => {
    let doc = content.document;
    await ContentTaskUtils.waitForCondition(
      () => doc.querySelector(".outer-wrapper"),
      `Waiting for page wrapper to be initialized at ${url}`
    );
  });
}

/**
 * Test whether the selected browser has the new tab page theme applied
 *
 * @param {object} theme that is applied
 * @param {boolean} isBrightText whether the brighttext attribute should be set
 */
async function test_ntp_theme(theme, isBrightText) {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      theme,
    },
  });

  let browser = gBrowser.selectedBrowser;

  let { originalBackground, originalCardBackground, originalColor } =
    await SpecialPowers.spawn(browser, [], function () {
      let doc = content.document;
      ok(
        !doc.documentElement.hasAttribute("lwt-newtab"),
        "New tab page should not have lwt-newtab attribute"
      );
      ok(
        !doc.documentElement.hasAttribute("lwt-newtab-brighttext"),
        `New tab page should not have lwt-newtab-brighttext attribute`
      );

      return {
        originalBackground: content.getComputedStyle(doc.body).backgroundColor,
        originalCardBackground: content.getComputedStyle(
          doc.querySelector(".top-site-outer .tile")
        ).backgroundColor,
        originalColor: content.getComputedStyle(
          doc.querySelector(".outer-wrapper")
        ).color,
        // We check the value of --newtab-link-primary-color directly because the
        // elements on which it is applied are hard to test. It is most visible in
        // the "learn more" link in the Pocket section. We cannot show the Pocket
        // section since it hits the network, and the usual workarounds to change
        // its backend only work in browser/. This variable is also used in
        // the Edit Top Site modal, but showing/hiding that is very verbose and
        // would make this test almost unreadable.
        originalLinks: content
          .getComputedStyle(doc.documentElement)
          .getPropertyValue("--newtab-link-primary-color"),
      };
    });

  await extension.startup();

  Services.ppmm.sharedData.flush();

  await SpecialPowers.spawn(
    browser,
    [
      {
        isBrightText,
        background: hexToCSS(theme.colors.ntp_background),
        card_background: hexToCSS(theme.colors.ntp_card_background),
        color: hexToCSS(theme.colors.ntp_text),
      },
    ],
    async function ({ isBrightText, background, card_background, color }) {
      let doc = content.document;
      ok(
        doc.documentElement.hasAttribute("lwt-newtab"),
        "New tab page should have lwt-newtab attribute"
      );
      is(
        doc.documentElement.hasAttribute("lwt-newtab-brighttext"),
        isBrightText,
        `New tab page should${
          !isBrightText ? " not" : ""
        } have lwt-newtab-brighttext attribute`
      );

      is(
        content.getComputedStyle(doc.body).backgroundColor,
        background,
        "New tab page background should be set."
      );
      is(
        content.getComputedStyle(doc.querySelector(".top-site-outer .tile"))
          .backgroundColor,
        card_background,
        "New tab page card background should be set."
      );
      is(
        content.getComputedStyle(doc.querySelector(".outer-wrapper")).color,
        color,
        "New tab page text color should be set."
      );
    }
  );

  await extension.unload();

  Services.ppmm.sharedData.flush();

  await SpecialPowers.spawn(
    browser,
    [
      {
        originalBackground,
        originalCardBackground,
        originalColor,
      },
    ],
    function ({ originalBackground, originalCardBackground, originalColor }) {
      let doc = content.document;
      ok(
        !doc.documentElement.hasAttribute("lwt-newtab"),
        "New tab page should not have lwt-newtab attribute"
      );
      ok(
        !doc.documentElement.hasAttribute("lwt-newtab-brighttext"),
        `New tab page should not have lwt-newtab-brighttext attribute`
      );

      is(
        content.getComputedStyle(doc.body).backgroundColor,
        originalBackground,
        "New tab page background should be reset."
      );
      is(
        content.getComputedStyle(doc.querySelector(".top-site-outer .tile"))
          .backgroundColor,
        originalCardBackground,
        "New tab page card background should be reset."
      );
      is(
        content.getComputedStyle(doc.querySelector(".outer-wrapper")).color,
        originalColor,
        "New tab page text color should be reset."
      );
    }
  );
}

add_task(async function test_support_ntp_colors() {
  await SpecialPowers.pushPrefEnv({
    set: [
      // BrowserTestUtils.withNewTab waits for about:newtab to load
      // so we disable preloading before running the test.
      ["browser.newtab.preload", false],
      // Force prefers-color-scheme to "light", as otherwise it might be
      // derived from the theme, but we hard-code the light styles on this
      // test.
      ["layout.css.prefers-color-scheme.content-override", 1],
      // Override the system color scheme to light so this test passes on
      // machines with dark system color scheme.
      ["ui.systemUsesDarkTheme", 0],
    ],
  });
  NewTabPagePreloading.removePreloadedBrowser(window);
  for (let url of ["about:newtab", "about:home"]) {
    info("Opening url: " + url);
    await BrowserTestUtils.withNewTab({ gBrowser, url }, async browser => {
      await waitForAboutNewTabReady(browser, url);
      await test_ntp_theme(
        {
          colors: {
            frame: ACCENT_COLOR,
            tab_background_text: TEXT_COLOR,
            ntp_background: "#add8e6",
            ntp_card_background: "#ffffff",
            ntp_text: "#00008b",
          },
        },
        false,
        url
      );

      await test_ntp_theme(
        {
          colors: {
            frame: ACCENT_COLOR,
            tab_background_text: TEXT_COLOR,
            ntp_background: "#00008b",
            ntp_card_background: "#000000",
            ntp_text: "#add8e6",
          },
        },
        true,
        url
      );
    });
  }
});