summaryrefslogtreecommitdiffstats
path: root/netwerk/test/browser/browser_test_data_channel_observer.js
blob: e03bbc72e67b5fa9e2f2916259c3b3ea0e226d72 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const TEST_URI = "data:text/html;charset=utf-8,<h1>Test";

let created = false;

add_task(async function test_data_channel_observer() {
  setupObserver();
  let tab = await BrowserTestUtils.addTab(gBrowser, TEST_URI);
  await BrowserTestUtils.waitForCondition(() => created);
  ok(created, "We received observer notification");
  await BrowserTestUtils.removeTab(tab);
});

function setupObserver() {
  const observer = {
    QueryInterface: ChromeUtils.generateQI(["nsIObserver"]),

    observe: function observe(subject, topic) {
      switch (topic) {
        case "data-channel-opened":
          let channelURI = subject.QueryInterface(Ci.nsIChannel).URI.spec;
          if (channelURI === TEST_URI) {
            Services.obs.removeObserver(observer, "data-channel-opened");
            created = true;
          }
          break;
      }
    },
  };
  Services.obs.addObserver(observer, "data-channel-opened");
}