summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/test/chrome_helpers.js
blob: 9aaeb95625bc31db505973190be85e7843ecefc5 (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
let swm = Cc["@mozilla.org/serviceworkers/manager;1"].getService(
  Ci.nsIServiceWorkerManager
);

let EXAMPLE_URL = "https://example.com/chrome/dom/serviceworkers/test/";

function waitForIframeLoad(iframe) {
  return new Promise(function (resolve) {
    iframe.onload = resolve;
  });
}

function waitForRegister(scope, callback) {
  return new Promise(function (resolve) {
    let listener = {
      onRegister(registration) {
        if (registration.scope !== scope) {
          return;
        }
        swm.removeListener(listener);
        resolve(callback ? callback(registration) : registration);
      },
    };
    swm.addListener(listener);
  });
}

function waitForUnregister(scope) {
  return new Promise(function (resolve) {
    let listener = {
      onUnregister(registration) {
        if (registration.scope !== scope) {
          return;
        }
        swm.removeListener(listener);
        resolve(registration);
      },
    };
    swm.addListener(listener);
  });
}

function waitForServiceWorkerRegistrationChange(registration, callback) {
  return new Promise(function (resolve) {
    let listener = {
      onChange() {
        registration.removeListener(listener);
        if (callback) {
          callback();
        }
        resolve(callback ? callback() : undefined);
      },
    };
    registration.addListener(listener);
  });
}

function waitForServiceWorkerShutdown() {
  return new Promise(function (resolve) {
    let observer = {
      observe(subject, topic, data) {
        if (topic !== "service-worker-shutdown") {
          return;
        }
        SpecialPowers.removeObserver(observer, "service-worker-shutdown");
        resolve();
      },
    };
    SpecialPowers.addObserver(observer, "service-worker-shutdown");
  });
}