summaryrefslogtreecommitdiffstats
path: root/devtools/client/aboutdebugging/test/browser/browser_aboutdebugging_serviceworker_pushservice_url.js
blob: 9581a493d947298d8c3a9c4f07f4a5f289c76643 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

/* import-globals-from helper-serviceworker.js */
Services.scriptloader.loadSubScript(
  CHROME_URL_ROOT + "helper-serviceworker.js",
  this
);

const SERVICE_WORKER = URL_ROOT_SSL + "resources/service-workers/push-sw.js";
const TAB_URL = URL_ROOT_SSL + "resources/service-workers/push-sw.html";

const FAKE_ENDPOINT = "https://fake/endpoint";

// Test that the push service url is displayed for service workers subscribed to a push
// service.
add_task(async function () {
  await enableServiceWorkerDebugging();

  info("Mock the push service");
  mockPushService(FAKE_ENDPOINT);

  const { document, tab, window } = await openAboutDebugging({
    enableWorkerUpdates: true,
  });
  await selectThisFirefoxPage(document, window.AboutDebugging.store);

  // Open a tab that registers a push service worker.
  const swTab = await addTab(TAB_URL);

  info(
    "Wait for the service worker to claim the test window before proceeding."
  );
  await SpecialPowers.spawn(
    swTab.linkedBrowser,
    [],
    () => content.wrappedJSObject.onSwClaimed
  );

  info("Wait until the service worker appears and is running");
  const targetElement = await waitForServiceWorkerRunning(
    SERVICE_WORKER,
    document
  );

  info("Subscribe from the push service");
  SpecialPowers.spawn(swTab.linkedBrowser, [], () => {
    content.wrappedJSObject.subscribeToPush();
  });

  info("Wait until the push service appears");
  await waitUntil(() =>
    targetElement.querySelector(".qa-worker-push-service-value")
  );
  const pushUrl = targetElement.querySelector(".qa-worker-push-service-value");

  ok(!!pushUrl, "Push URL is displayed for the serviceworker");
  is(pushUrl.textContent, FAKE_ENDPOINT, "Push URL shows the expected content");

  info("Unsubscribe from the push service");
  SpecialPowers.spawn(swTab.linkedBrowser, [], () => {
    content.wrappedJSObject.unsubscribeToPush();
  });

  info("Wait until the push service disappears");
  await waitUntil(
    () => !targetElement.querySelector(".qa-worker-push-service-value")
  );

  info("Unregister the service worker");
  await unregisterServiceWorker(swTab);

  info("Wait until the service worker disappears from about:debugging");
  await waitUntil(() => !findDebugTargetByText(SERVICE_WORKER, document));

  info("Remove the service worker tab");
  await removeTab(swTab);

  await removeTab(tab);
});

function mockPushService(endpoint) {
  const PushService = Cc["@mozilla.org/push/Service;1"].getService(
    Ci.nsIPushService
  ).wrappedJSObject;

  PushService.service = {
    _registrations: new Map(),
    _notify(scope) {
      Services.obs.notifyObservers(
        null,
        PushService.subscriptionModifiedTopic,
        scope
      );
    },
    init() {},
    register(pageRecord) {
      const registration = {
        endpoint,
      };
      this._registrations.set(pageRecord.scope, registration);
      this._notify(pageRecord.scope);
      return Promise.resolve(registration);
    },
    registration(pageRecord) {
      return Promise.resolve(this._registrations.get(pageRecord.scope));
    },
    unregister(pageRecord) {
      const deleted = this._registrations.delete(pageRecord.scope);
      if (deleted) {
        this._notify(pageRecord.scope);
      }
      return Promise.resolve(deleted);
    },
  };
}