summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/browser/browser_ext_themes_getCurrent_differentExt.js
blob: 587c5d4efe08b0af5924db66d196632230bd9b86 (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
"use strict";

// This test checks whether browser.theme.getCurrent() works correctly when theme
// does not originate from extension querying the theme.
const THEME = {
  manifest: {
    theme: {
      images: {
        theme_frame: "image1.png",
      },
      colors: {
        frame: ACCENT_COLOR,
        tab_background_text: TEXT_COLOR,
      },
    },
  },
  files: {
    "image1.png": BACKGROUND,
  },
};

add_task(async function test_getcurrent() {
  const theme = ExtensionTestUtils.loadExtension(THEME);
  const extension = ExtensionTestUtils.loadExtension({
    background() {
      browser.theme.onUpdated.addListener(() => {
        browser.theme.getCurrent().then(theme => {
          browser.test.sendMessage("theme-updated", theme);
          if (!theme?.images) {
            return;
          }

          // Try to access the theme_frame image
          fetch(theme.images.theme_frame)
            .then(() => {
              browser.test.sendMessage("theme-image", { success: true });
            })
            .catch(e => {
              browser.test.sendMessage("theme-image", {
                success: false,
                error: e.message,
              });
            });
        });
      });
    },
  });

  await extension.startup();

  info("Testing getCurrent after static theme startup");
  let updatedPromise = extension.awaitMessage("theme-updated");
  let imageLoaded = extension.awaitMessage("theme-image");
  await theme.startup();
  let receivedTheme = await updatedPromise;
  Assert.ok(
    receivedTheme.images.theme_frame.includes("image1.png"),
    "getCurrent returns correct theme_frame image"
  );
  Assert.equal(
    receivedTheme.colors.frame,
    ACCENT_COLOR,
    "getCurrent returns correct frame color"
  );
  Assert.equal(
    receivedTheme.colors.tab_background_text,
    TEXT_COLOR,
    "getCurrent returns correct tab_background_text color"
  );
  Assert.deepEqual(await imageLoaded, { success: true }, "theme image loaded");

  info("Testing getCurrent after static theme unload");
  updatedPromise = extension.awaitMessage("theme-updated");
  await theme.unload();
  receivedTheme = await updatedPromise;
  Assert.equal(
    JSON.stringify({ colors: null, images: null, properties: null }),
    JSON.stringify(receivedTheme),
    "getCurrent returns empty theme"
  );

  await extension.unload();
});

add_task(async function test_getcurrent_privateBrowsing() {
  const theme = ExtensionTestUtils.loadExtension(THEME);

  const extension = ExtensionTestUtils.loadExtension({
    incognitoOverride: "spanning",
    manifest: {
      sidebar_action: {
        default_panel: "sidebar.html",
      },
    },
    // We don't want the sidebar to automatically open on extension startup.
    startupReason: "APP_STARTUP",
    files: {
      "sidebar.html": `<!DOCTYPE html>
        <html>
          <body>
            Test Extension Sidebar
            <script src="sidebar.js"></script>
          </body>
        </html>
      `,
      "sidebar.js": function () {
        browser.theme.getCurrent().then(theme => {
          if (!theme?.images) {
            browser.test.fail(
              `Missing expected images from theme.getCurrent result`
            );
            return;
          }

          // Try to access the theme_frame image
          fetch(theme.images.theme_frame)
            .then(() => {
              browser.test.sendMessage("theme-image", { success: true });
            })
            .catch(e => {
              browser.test.sendMessage("theme-image", {
                success: false,
                error: e.message,
              });
            });
        });
      },
    },
  });

  await extension.startup();
  await theme.startup();

  const privateWin = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
  });

  const { ExtensionCommon } = ChromeUtils.importESModule(
    "resource://gre/modules/ExtensionCommon.sys.mjs"
  );
  const { makeWidgetId } = ExtensionCommon;
  privateWin.SidebarUI.show(`${makeWidgetId(extension.id)}-sidebar-action`);

  let imageLoaded = extension.awaitMessage("theme-image");
  Assert.deepEqual(await imageLoaded, { success: true }, "theme image loaded");

  await extension.unload();
  await theme.unload();

  await BrowserTestUtils.closeWindow(privateWin);
});