summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_browserAction_popup.js
blob: 136f30eb41375c112f2ecc6159a0ece3be7d2377 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

function getBrowserAction(extension) {
  const {
    global: { browserActionFor },
  } = Management;

  let ext = WebExtensionPolicy.getByID(extension.id)?.extension;
  return browserActionFor(ext);
}

async function assertViewCount(extension, count, waitForPromise) {
  let ext = WebExtensionPolicy.getByID(extension.id).extension;

  if (waitForPromise) {
    await waitForPromise;
  }

  is(
    ext.views.size,
    count,
    "Should have the expected number of extension views"
  );
}

function promiseExtensionPageClosed(extension, viewType) {
  return new Promise(resolve => {
    const policy = WebExtensionPolicy.getByID(extension.id);

    const listener = (evtType, context) => {
      if (context.viewType === viewType) {
        policy.extension.off("extension-proxy-context-load", listener);

        context.callOnClose({
          close: resolve,
        });
      }
    };
    policy.extension.on("extension-proxy-context-load", listener);
  });
}

let scriptPage = url =>
  `<html><head><meta charset="utf-8"><script src="${url}"></script></head><body>${url}</body></html>`;

async function testInArea(area) {
  let extension = ExtensionTestUtils.loadExtension({
    background() {
      let middleClickShowPopup = false;
      browser.browserAction.onClicked.addListener((tabs, info) => {
        browser.test.sendMessage("browserAction-onClicked");
        if (info.button === 1 && middleClickShowPopup) {
          browser.browserAction.openPopup();
        }
      });

      browser.test.onMessage.addListener(async msg => {
        if (msg.type === "setBrowserActionPopup") {
          let opts = { popup: msg.popup };
          if (msg.onCurrentWindowId) {
            let { id } = await browser.windows.getCurrent();
            opts = { ...opts, windowId: id };
          } else if (msg.onActiveTabId) {
            let [{ id }] = await browser.tabs.query({
              active: true,
              currentWindow: true,
            });
            opts = { ...opts, tabId: id };
          }
          await browser.browserAction.setPopup(opts);
          browser.test.sendMessage("setBrowserActionPopup:done");
        } else if (msg.type === "setMiddleClickShowPopup") {
          middleClickShowPopup = msg.show;
          browser.test.sendMessage("setMiddleClickShowPopup:done");
        }
      });

      browser.test.sendMessage("background-page-ready");
    },
    manifest: {
      browser_action: {
        default_popup: "popup-a.html",
        browser_style: true,
      },
    },

    files: {
      "popup-a.html": scriptPage("popup-a.js"),
      "popup-a.js": function () {
        browser.test.onMessage.addListener(msg => {
          if (msg == "close-popup-using-window.close") {
            window.close();
          }
        });

        window.onload = () => {
          let color = window.getComputedStyle(document.body).color;
          browser.test.assertEq("rgb(34, 36, 38)", color);
          browser.test.sendMessage("from-popup", "popup-a");
        };
      },

      "data/popup-b.html": scriptPage("popup-b.js"),
      "data/popup-b.js": function () {
        window.onload = () => {
          browser.test.sendMessage("from-popup", "popup-b");
        };
      },

      "data/popup-c.html": scriptPage("popup-c.js"),
      "data/popup-c.js": function () {
        // Close the popup before the document is fully-loaded to make sure that
        // we handle this case sanely.
        browser.test.sendMessage("from-popup", "popup-c");
        window.close();
      },
    },
  });

  await Promise.all([
    extension.startup(),
    extension.awaitMessage("background-page-ready"),
  ]);

  let widget = getBrowserActionWidget(extension);

  // Move the browserAction widget to the area targeted by this test.
  CustomizableUI.addWidgetToArea(widget.id, area);

  async function setBrowserActionPopup(opts) {
    extension.sendMessage({ type: "setBrowserActionPopup", ...opts });
    await extension.awaitMessage("setBrowserActionPopup:done");
  }

  async function setShowPopupOnMiddleClick(show) {
    extension.sendMessage({ type: "setMiddleClickShowPopup", show });
    await extension.awaitMessage("setMiddleClickShowPopup:done");
  }

  async function runTest({
    actionType,
    waitForPopupLoaded,
    expectPopup,
    expectOnClicked,
    closePopup,
  }) {
    const oncePopupPageClosed = promiseExtensionPageClosed(extension, "popup");
    const oncePopupLoaded = waitForPopupLoaded
      ? awaitExtensionPanel(extension)
      : undefined;

    if (actionType === "click") {
      clickBrowserAction(extension);
    } else if (actionType === "trigger") {
      getBrowserAction(extension).triggerAction(window);
    } else if (actionType === "middleClick") {
      clickBrowserAction(extension, window, { button: 1 });
    }

    if (expectOnClicked) {
      await extension.awaitMessage("browserAction-onClicked");
    }

    if (expectPopup) {
      info(`Waiting for popup: ${expectPopup}`);
      is(
        await extension.awaitMessage("from-popup"),
        expectPopup,
        "expected popup opened"
      );
    }

    await oncePopupLoaded;

    if (closePopup) {
      info("Closing popup");
      await closeBrowserAction(extension);
      await assertViewCount(extension, 1, oncePopupPageClosed);
    }

    return { oncePopupPageClosed };
  }

  // Run the sequence of test cases.
  const tests = [
    async () => {
      info(`Click browser action, expect popup "a".`);

      await runTest({
        actionType: "click",
        expectPopup: "popup-a",
        closePopup: true,
      });
    },
    async () => {
      info(`Click browser action again, expect popup "a".`);

      await runTest({
        actionType: "click",
        expectPopup: "popup-a",
        waitForPopupLoaded: true,
        closePopup: true,
      });
    },
    async () => {
      info(`Call triggerAction, expect popup "a" again. Leave popup open.`);

      const { oncePopupPageClosed } = await runTest({
        actionType: "trigger",
        expectPopup: "popup-a",
        waitForPopupLoaded: true,
      });

      await assertViewCount(extension, 2);

      info(`Call triggerAction again. Expect remaining popup closed.`);
      getBrowserAction(extension).triggerAction(window);

      await assertViewCount(extension, 1, oncePopupPageClosed);
    },
    async () => {
      info(`Call triggerAction again. Expect popup "a" again.`);

      await runTest({
        actionType: "trigger",
        expectPopup: "popup-a",
        closePopup: true,
      });
    },
    async () => {
      info(`Set popup to "c" and click browser action. Expect popup "c".`);

      await setBrowserActionPopup({ popup: "data/popup-c.html" });

      const { oncePopupPageClosed } = await runTest({
        actionType: "click",
        expectPopup: "popup-c",
      });

      await assertViewCount(extension, 1, oncePopupPageClosed);
    },
    async () => {
      info(`Set popup to "b" and click browser action. Expect popup "b".`);

      await setBrowserActionPopup({ popup: "data/popup-b.html" });

      await runTest({
        actionType: "click",
        expectPopup: "popup-b",
        closePopup: true,
      });
    },
    async () => {
      info(`Click browser action again, expect popup "b".`);

      await runTest({
        actionType: "click",
        expectPopup: "popup-b",
        closePopup: true,
      });
    },
    async () => {
      info(`Middle-click browser action, expect an event only.`);

      await setShowPopupOnMiddleClick(false);

      await runTest({
        actionType: "middleClick",
        expectOnClicked: true,
      });
    },
    async () => {
      info(
        `Middle-click browser action again, expect a click event then a popup.`
      );

      await setShowPopupOnMiddleClick(true);

      await runTest({
        actionType: "middleClick",
        expectOnClicked: true,
        expectPopup: "popup-b",
        closePopup: true,
      });
    },
    async () => {
      info(`Clear popup URL. Click browser action. Expect click event.`);

      await setBrowserActionPopup({ popup: "" });

      await runTest({
        actionType: "click",
        expectOnClicked: true,
      });
    },
    async () => {
      info(`Click browser action again. Expect another click event.`);

      await runTest({
        actionType: "click",
        expectOnClicked: true,
      });
    },
    async () => {
      info(`Call triggerAction. Expect click event.`);

      await runTest({
        actionType: "trigger",
        expectOnClicked: true,
      });
    },
    async () => {
      info(
        `Set window-specific popup to "b" and click browser action. Expect popup "b".`
      );

      await setBrowserActionPopup({
        popup: "data/popup-b.html",
        onCurrentWindowId: true,
      });

      await runTest({
        actionType: "click",
        expectPopup: "popup-b",
        closePopup: true,
      });
    },
    async () => {
      info(
        `Set tab-specific popup to "a" and click browser action. Expect popup "a", and leave open.`
      );

      await setBrowserActionPopup({
        popup: "/popup-a.html",
        onActiveTabId: true,
      });

      const { oncePopupPageClosed } = await runTest({
        actionType: "click",
        expectPopup: "popup-a",
      });
      assertViewCount(extension, 2);

      info(`Tell popup "a" to call window.close(). Expect popup closed.`);
      extension.sendMessage("close-popup-using-window.close");

      await assertViewCount(extension, 1, oncePopupPageClosed);
    },
  ];

  for (let test of tests) {
    await test();
  }

  // Unload the extension and verify that the browserAction widget is gone.
  await extension.unload();

  let view = document.getElementById(widget.viewId);
  is(view, null, "browserAction view removed from document");
}

add_task(async function testBrowserActionInToolbar() {
  await testInArea(CustomizableUI.AREA_NAVBAR);
});

add_task(async function testBrowserActionInPanel() {
  await testInArea(getCustomizableUIPanelID());
});