summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_popup_background.js
blob: bf0f78b732a6df6f68d09bc2f928138375efa6de (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
/* eslint-disable mozilla/no-arbitrary-setTimeout */
"use strict";

async function testPanel(browser, standAlone, background_check) {
  let panel = getPanelForNode(browser);

  let checkBackground = (background = null) => {
    if (!standAlone) {
      return;
    }

    is(
      getComputedStyle(panel.panelContent).backgroundColor,
      background,
      "Content should have correct background"
    );
  };

  function getBackground(browser) {
    return SpecialPowers.spawn(browser, [], async function () {
      return content.windowUtils.canvasBackgroundColor;
    });
  }

  let setBackground = color => {
    content.document.body.style.backgroundColor = color;
  };

  await new Promise(resolve => setTimeout(resolve, 100));

  info("Test that initial background color is applied");
  let initialBackground = await getBackground(browser);
  checkBackground(initialBackground);
  background_check(initialBackground);

  info("Test that dynamically-changed background color is applied");
  await alterContent(browser, setBackground, "black");
  checkBackground(await getBackground(browser));

  info("Test that non-opaque background color results in default styling");
  await alterContent(browser, setBackground, "rgba(1, 2, 3, .9)");
}

add_task(async function testPopupBackground() {
  let testCases = [
    {
      browser_style: false,
      popup: `
        <!doctype html>
        <body style="width: 100px; height: 100px; background-color: green">
        </body>
      `,
      background_check: function (bg) {
        is(bg, "rgb(0, 128, 0)", "Initial background should be green");
      },
    },
    {
      browser_style: false,
      popup: `
        <!doctype html>
        <body style="width: 100px; height: 100px"">
        </body>
      `,
      background_check: function (bg) {
        is(bg, "rgb(255, 255, 255)", "Initial background should be white");
      },
    },
    {
      browser_style: false,
      popup: `
        <!doctype html>
        <meta name=color-scheme content=light>
        <body style="width: 100px; height: 100px;">
        </body>
      `,
      background_check: function (bg) {
        is(bg, "rgb(255, 255, 255)", "Initial background should be white");
      },
    },
    {
      browser_style: false,
      popup: `
        <!doctype html>
        <meta name=color-scheme content=dark>
        <body style="width: 100px; height: 100px;">
        </body>
      `,
      background_check: function (bg) {
        isnot(
          bg,
          "rgb(255, 255, 255)",
          "Initial background should not be white"
        );
      },
    },
  ];
  for (let { browser_style, popup, background_check } of testCases) {
    info(`Testing browser_style: ${browser_style} popup: ${popup}`);
    let extension = ExtensionTestUtils.loadExtension({
      background() {
        browser.tabs.query({ active: true, currentWindow: true }, tabs => {
          browser.pageAction.show(tabs[0].id);
        });
      },

      manifest: {
        browser_action: {
          default_popup: "popup.html",
          default_area: "navbar",
          browser_style,
        },

        page_action: {
          default_popup: "popup.html",
          browser_style,
        },
      },

      files: {
        "popup.html": popup,
      },
    });

    await extension.startup();

    {
      info("Test stand-alone browserAction popup");

      clickBrowserAction(extension);
      let browser = await awaitExtensionPanel(extension);
      await testPanel(browser, true, background_check);
      await closeBrowserAction(extension);
    }

    {
      info("Test menu panel browserAction popup");

      let widget = getBrowserActionWidget(extension);
      CustomizableUI.addWidgetToArea(widget.id, getCustomizableUIPanelID());

      clickBrowserAction(extension);
      let browser = await awaitExtensionPanel(extension);
      await testPanel(browser, false, background_check);
      await closeBrowserAction(extension);
    }

    {
      info("Test pageAction popup");

      clickPageAction(extension);
      let browser = await awaitExtensionPanel(extension);
      await testPanel(browser, true, background_check);
      await closePageAction(extension);
    }

    await extension.unload();
  }
});