summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_popup_select.js
blob: 87bd945a53d0dea1d21c3b2b8e197f6317b6c33e (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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

add_task(async function testPopupSelectPopup() {
  let tab = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    url: "https://example.com",
  });

  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",
        browser_style: false,
      },

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

    files: {
      "popup.html": `<!DOCTYPE html>
        <html>
          <head><meta charset="utf-8"></head>
          <body style="width: 300px; height: 300px;">
            <div style="text-align: center">
              <select id="select">
                <option>Foo</option>
                <option>Bar</option>
                <option>Baz</option>
              </select>
            </div>
          </body>
        </html>`,
    },
  });

  await extension.startup();

  async function testPanel(browser) {
    const popupPromise = BrowserTestUtils.waitForSelectPopupShown(window);

    // Wait the select element in the popup window to be ready before sending a
    // mouse event to open the select popup.
    await SpecialPowers.spawn(browser, [], async () => {
      await ContentTaskUtils.waitForCondition(() => {
        return content.document && content.document.querySelector("#select");
      });
    });
    BrowserTestUtils.synthesizeMouseAtCenter("#select", {}, browser);

    const selectPopup = await popupPromise;

    let elemRect = await SpecialPowers.spawn(browser, [], async function () {
      let elem = content.document.getElementById("select");
      let r = elem.getBoundingClientRect();

      return { left: r.left, bottom: r.bottom };
    });

    let popupRect = selectPopup.getOuterScreenRect();
    let marginTop = parseFloat(getComputedStyle(selectPopup).marginTop);
    let marginLeft = parseFloat(getComputedStyle(selectPopup).marginLeft);

    is(
      Math.floor(browser.screenX + elemRect.left + marginLeft),
      popupRect.left,
      "Select popup has the correct x origin"
    );

    is(
      Math.floor(browser.screenY + elemRect.bottom + marginTop),
      popupRect.top,
      "Select popup has the correct y origin"
    );

    // Close the select popup before proceeding to the next test.
    const onPopupHidden = BrowserTestUtils.waitForEvent(
      selectPopup,
      "popuphidden"
    );
    selectPopup.hidePopup();
    await onPopupHidden;
  }

  {
    info("Test browserAction popup");

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

  {
    info("Test pageAction popup");

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

  BrowserTestUtils.removeTab(tab);
  await extension.unload();
});