summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_browserAction_popup_preload_smoketest.js
blob: 4069dbe89255f9d1fc3315e8fe3c6fd6a3a2f0a1 (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
/* -*- 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";

// This test does create and cancel the preloaded popup
// multiple times and in some cases it takes longer than
// the default timeouts allows.
requestLongerTimeout(4);

const { AddonTestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/AddonTestUtils.sys.mjs"
);

async function installTestAddon(addonId, unpacked = false) {
  let xpi = AddonTestUtils.createTempWebExtensionFile({
    manifest: {
      browser_specific_settings: { gecko: { id: addonId } },
      browser_action: {
        default_popup: "popup.html",
        default_area: "navbar",
      },
    },
    files: {
      "popup.html": `<!DOCTYPE html>
      <html>
        <head>
          <meta charset="utf-8">
            <link rel="stylesheet" href="popup.css">
            </head>
            <body>
            </body>
          </html>
      `,
      "popup.css": `@import "imported.css";`,
      "imported.css": `
        /* Increasing the imported.css file size to increase the
         * chances to trigger the stylesheet preload issue that
         * has been fixed by Bug 1735899 consistently and with
         * a smaller number of preloaded popup cancelled.
         *
         * ${new Array(600000).fill("x").join("\n")} 
         */
        body { width: 300px; height: 300px; background: red; }
      `,
    },
  });

  if (unpacked) {
    // This temporary directory is going to be removed from the
    // cleanup function, but also make it unique as we do for the
    // other temporary files (e.g. like getTemporaryFile as defined
    // in XPInstall.jsm).
    const random = Math.round(Math.random() * 36 ** 3).toString(36);
    const tmpDirName = `mochitest_unpacked_addons_${random}`;
    let tmpExtPath = FileUtils.getDir("TmpD", [tmpDirName], true);
    registerCleanupFunction(() => {
      tmpExtPath.remove(true);
    });

    // Unpacking the xpi file into the tempoary directory.
    const extDir = await AddonTestUtils.manuallyInstall(
      xpi,
      tmpExtPath,
      null,
      /* unpacked */ true
    );

    // Install temporarily as unpacked.
    return AddonManager.installTemporaryAddon(extDir);
  }

  // Install temporarily as packed.
  return AddonManager.installTemporaryAddon(xpi);
}

async function waitForExtensionAndBrowserAction(addonId) {
  const {
    Management: {
      global: { browserActionFor },
    },
  } = ChromeUtils.importESModule("resource://gre/modules/Extension.sys.mjs");

  // trigger a number of preloads
  let extension;
  let browserAction;
  await TestUtils.waitForCondition(() => {
    extension = WebExtensionPolicy.getByID(addonId)?.extension;
    browserAction = extension && browserActionFor(extension);
    return browserAction;
  }, "got the extension and browserAction");

  let widget = getBrowserActionWidget(extension).forWindow(window);

  return { extension, browserAction, widget };
}

async function testCancelPreloadedPopup({ browserAction, widget }) {
  // Trigger the preloaded popup.
  EventUtils.synthesizeMouseAtCenter(
    widget.node,
    { type: "mouseover", button: 0 },
    window
  );
  await TestUtils.waitForCondition(
    () => browserAction.pendingPopup,
    "Wait for the preloaded popup"
  );
  // Cancel the preloaded popup.
  EventUtils.synthesizeMouseAtCenter(
    widget.node,
    { type: "mouseout", button: 0 },
    window
  );
  EventUtils.synthesizeMouseAtCenter(
    window.gURLBar.textbox,
    { type: "mouseover" },
    window
  );
  await TestUtils.waitForCondition(
    () => !browserAction.pendingPopup,
    "Wait for the preloaded popup to be cancelled"
  );
}

async function testPopupLoadCompleted({ extension, browserAction, widget }) {
  const promiseViewShowing = BrowserTestUtils.waitForEvent(
    document,
    "ViewShowing",
    false,
    ev => ev.target.id === browserAction.viewId
  );
  EventUtils.synthesizeMouseAtCenter(
    widget.node,
    { type: "mousedown", button: 0 },
    window
  );
  EventUtils.synthesizeMouseAtCenter(
    widget.node,
    { type: "mouseup", button: 0 },
    window
  );

  info("Await the browserAction popup to be shown");
  await promiseViewShowing;

  info("Await the browserAction popup to be fully loaded");
  const browser = await awaitExtensionPanel(
    extension,
    window,
    /* awaitLoad */ true
  );

  await TestUtils.waitForCondition(async () => {
    const docReadyState = await SpecialPowers.spawn(browser, [], () => {
      return this.content.document.readyState;
    });

    return docReadyState === "complete";
  }, "Wait the popup document to get to readyState complete");

  ok(true, "Popup document was fully loaded");
}

// This test is covering a scenario similar to the one fixed in Bug 1735899,
// and possibly some other similar ones that may slip through unnoticed.
add_task(async function testCancelPopupPreloadRaceOnUnpackedAddon() {
  const ID = "preloaded-popup@test";
  const addon = await installTestAddon(ID, /* unpacked */ true);
  const { extension, browserAction, widget } =
    await waitForExtensionAndBrowserAction(ID);
  info("Preload popup and cancel it multiple times");
  for (let i = 0; i < 200; i++) {
    await testCancelPreloadedPopup({ browserAction, widget });
  }
  await testPopupLoadCompleted({ extension, browserAction, widget });
  await addon.uninstall();
});

// This test is covering a scenario similar to the one fixed in Bug 1735899,
// and possibly some other similar ones that may slip through unnoticed.
add_task(async function testCancelPopupPreloadRaceOnPackedAddon() {
  const ID = "preloaded-popup@test";
  const addon = await installTestAddon(ID, /* unpacked */ false);
  const { extension, browserAction, widget } =
    await waitForExtensionAndBrowserAction(ID);
  info("Preload popup and cancel it multiple times");
  for (let i = 0; i < 200; i++) {
    await testCancelPreloadedPopup({ browserAction, widget });
  }
  await testPopupLoadCompleted({ extension, browserAction, widget });
  await addon.uninstall();
});