summaryrefslogtreecommitdiffstats
path: root/dom/manifest/test/browser_fire_appinstalled_event.js
blob: 517b120d381a5ca5c2a9432f6038126c77bc7b99 (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
//Used by JSHint:
/*global Cu, BrowserTestUtils, ok, add_task, gBrowser */
"use strict";
const { PromiseMessage } = Cu.import("resource://gre/modules/PromiseMessage.jsm", {});
const testPath = "/browser/dom/manifest/test/file_reg_appinstalled_event.html";
const defaultURL = new URL("http://example.org/browser/dom/manifest/test/file_testserver.sjs");
const testURL = new URL(defaultURL);
testURL.searchParams.append("file", testPath);

// Enable window.onappinstalled, so we can fire events at it.
function enableOnAppInstalledPref() {
  const ops = {
    "set": [
      ["dom.manifest.onappinstalled", true],
    ],
  };
  return SpecialPowers.pushPrefEnv(ops);
}

// Send a message for the even to be fired.
// This cause file_reg_install_event.html to be dynamically change.
function* theTest(aBrowser) {
  aBrowser.allowEvents = true;
  let waitForInstall = ContentTask.spawn(aBrowser, null, function*() {
    yield ContentTaskUtils.waitForEvent(content.window, "appinstalled");
  });
  const { data: { success } } = yield PromiseMessage
    .send(aBrowser.messageManager, "DOM:Manifest:FireAppInstalledEvent");
  ok(success, "message sent and received successfully.");
  try {
    yield waitForInstall;
    ok(true, "AppInstalled event fired");
  } catch (err) {
    ok(false, "AppInstalled event didn't fire: " + err.message);
  }
}

// Open a tab and run the test
add_task(function*() {
  yield enableOnAppInstalledPref();
  let tabOptions = {
    gBrowser: gBrowser,
    url: testURL.href,
  };
  yield BrowserTestUtils.withNewTab(
    tabOptions,
    theTest
  );
});