browser_test_data_channel_observer.js (1192B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const TEST_URI = "data:text/html;charset=utf-8,<h1>Test"; 7 8 let created = false; 9 10 add_task(async function test_data_channel_observer() { 11 setupObserver(); 12 let tab = await BrowserTestUtils.addTab(gBrowser, TEST_URI); 13 await BrowserTestUtils.waitForCondition(() => created); 14 ok(created, "We received observer notification"); 15 await BrowserTestUtils.removeTab(tab); 16 }); 17 18 function setupObserver() { 19 const observer = { 20 QueryInterface: ChromeUtils.generateQI(["nsIObserver"]), 21 22 observe: function observe(subject, topic) { 23 switch (topic) { 24 case "data-channel-opened": { 25 ok( 26 subject instanceof Ci.nsIDataChannel, 27 "Channel should be a nsIDataChannel instance" 28 ); 29 let channelURI = subject.QueryInterface(Ci.nsIChannel).URI.spec; 30 if (channelURI === TEST_URI) { 31 Services.obs.removeObserver(observer, "data-channel-opened"); 32 created = true; 33 } 34 break; 35 } 36 } 37 }, 38 }; 39 Services.obs.addObserver(observer, "data-channel-opened"); 40 }