summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_messaging_startup.js
blob: 120bebb43156dfeba4936d2115de59405d39743f (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
"use strict";

const server = createHttpServer({ hosts: ["example.com"] });
server.registerDirectory("/data/", do_get_file("data"));

AddonTestUtils.init(this);
AddonTestUtils.overrideCertDB();
AddonTestUtils.createAppInfo(
  "xpcshell@tests.mozilla.org",
  "XPCShell",
  "1",
  "43"
);

let { promiseRestartManager, promiseShutdownManager, promiseStartupManager } =
  AddonTestUtils;

const PAGE_HTML = `<!DOCTYPE html><meta charset="utf-8"><script src="script.js"></script>`;

function trackEvents(wrapper) {
  let events = new Map();
  for (let event of ["background-script-event", "start-background-script"]) {
    events.set(event, false);
    wrapper.extension.once(event, () => events.set(event, true));
  }
  return events;
}

async function test(what, background, script) {
  await promiseStartupManager();

  let extension = ExtensionTestUtils.loadExtension({
    useAddonManager: "permanent",

    manifest: {
      content_scripts: [
        {
          matches: ["http://example.com/*"],
          js: ["script.js"],
        },
      ],
    },

    files: {
      "page.html": PAGE_HTML,
      "script.js": script,
    },

    background,
  });

  info(`Set up ${what} listener`);
  await extension.startup();
  await extension.awaitMessage("bg-ran");

  info(`Test wakeup for ${what} from an extension page`);
  await promiseRestartManager({ earlyStartup: false });
  await extension.awaitStartup();

  function awaitBgEvent() {
    return new Promise(resolve =>
      extension.extension.once("background-script-event", resolve)
    );
  }

  let events = trackEvents(extension);

  let url = extension.extension.baseURI.resolve("page.html");

  let [, page] = await Promise.all([
    awaitBgEvent(),
    ExtensionTestUtils.loadContentPage(url, { extension }),
  ]);

  equal(
    events.get("background-script-event"),
    true,
    "Should have gotten a background page event"
  );
  equal(
    events.get("start-background-script"),
    false,
    "Background page should not be started"
  );

  equal(extension.messageQueue.size, 0, "Have not yet received bg-ran message");

  let promise = extension.awaitMessage("bg-ran");
  AddonTestUtils.notifyEarlyStartup();
  await promise;

  equal(
    events.get("start-background-script"),
    true,
    "Should have gotten start-background-script event"
  );

  await extension.awaitFinish("messaging-test");
  ok(true, "Background page loaded and received message from extension page");

  await page.close();

  info(`Test wakeup for ${what} from a content script`);
  await promiseRestartManager({ earlyStartup: false });
  await extension.awaitStartup();

  events = trackEvents(extension);

  [, page] = await Promise.all([
    awaitBgEvent(),
    ExtensionTestUtils.loadContentPage(
      "http://example.com/data/file_sample.html"
    ),
  ]);

  equal(
    events.get("background-script-event"),
    true,
    "Should have gotten a background script event"
  );
  equal(
    events.get("start-background-script"),
    false,
    "Background script should not be started"
  );

  equal(extension.messageQueue.size, 0, "Have not yet received bg-ran message");

  promise = extension.awaitMessage("bg-ran");
  AddonTestUtils.notifyEarlyStartup();
  await promise;

  equal(
    events.get("start-background-script"),
    true,
    "Should have gotten start-background-script event"
  );

  await extension.awaitFinish("messaging-test");
  ok(true, "Background page loaded and received message from content script");

  await page.close();
  await extension.unload();

  await promiseShutdownManager();
}

add_task(function test_onMessage() {
  function script() {
    browser.runtime.sendMessage("ping").then(reply => {
      browser.test.assertEq(
        reply,
        "pong",
        "Extension page received pong reply"
      );
      browser.test.notifyPass("messaging-test");
    });
  }

  async function background() {
    browser.runtime.onMessage.addListener((msg, sender) => {
      browser.test.assertEq(
        msg,
        "ping",
        "Background page received ping message"
      );
      return Promise.resolve("pong");
    });

    // addListener() returns right away but make a round trip to the
    // main process to ensure the persistent onMessage listener is recorded.
    await browser.runtime.getBrowserInfo();
    browser.test.sendMessage("bg-ran");
  }

  return test("onMessage", background, script);
});

add_task(function test_onConnect() {
  function script() {
    let port = browser.runtime.connect();
    port.onMessage.addListener(msg => {
      browser.test.assertEq(msg, "pong", "Extension page received pong reply");
      browser.test.notifyPass("messaging-test");
    });
    port.postMessage("ping");
  }

  async function background() {
    browser.runtime.onConnect.addListener(port => {
      port.onMessage.addListener(msg => {
        browser.test.assertEq(
          msg,
          "ping",
          "Background page received ping message"
        );
        port.postMessage("pong");
      });
    });

    // addListener() returns right away but make a round trip to the
    // main process to ensure the persistent onMessage listener is recorded.
    await browser.runtime.getBrowserInfo();
    browser.test.sendMessage("bg-ran");
  }

  return test("onConnect", background, script);
});

// Test that messaging works if the background page is started before
// any messages are exchanged.  (See bug 1467136 for an example of how
// this broke at one point).
add_task(async function test_other_startup() {
  await promiseStartupManager();

  let extension = ExtensionTestUtils.loadExtension({
    useAddonManager: "permanent",

    async background() {
      browser.runtime.onMessage.addListener(msg => {
        browser.test.notifyPass("startup");
      });

      // addListener() returns right away but make a round trip to the
      // main process to ensure the persistent onMessage listener is recorded.
      await browser.runtime.getBrowserInfo();
      browser.test.sendMessage("bg-ran");
    },

    files: {
      "page.html": PAGE_HTML,
      "script.js"() {
        browser.runtime.sendMessage("ping");
      },
    },
  });

  await extension.startup();
  await extension.awaitMessage("bg-ran");

  await promiseRestartManager({ lateStartup: false });
  await extension.awaitStartup();
  let events = trackEvents(extension);

  equal(
    events.get("background-script-event"),
    false,
    "Should not have gotten a background page event"
  );
  equal(
    events.get("start-background-script"),
    false,
    "Background page should not be started"
  );

  // Start the background page.  No message have been sent at this point.
  await AddonTestUtils.notifyLateStartup();
  equal(
    events.get("start-background-script"),
    true,
    "Background page should be started"
  );

  await extension.awaitMessage("bg-ran");

  // Now that the background page is fully started, load a new page that
  // sends a message to the background page.
  let url = extension.extension.baseURI.resolve("page.html");
  let page = await ExtensionTestUtils.loadContentPage(url, { extension });

  await extension.awaitFinish("startup");

  await page.close();
  await extension.unload();

  await promiseShutdownManager();
});