test_eventsourceservice_basic.html (2562B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <title>EventSource event service basic test</title> 5 <meta http-equiv='Content-Type' content='text/html; charset=utf-8'> 6 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 7 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 8 </head> 9 <body> 10 <pre id="test"> 11 <script class="testbody" type="text/javascript"> 12 13 var service = SpecialPowers.Cc["@mozilla.org/eventsourceevent/service;1"] 14 .getService(SpecialPowers.Ci.nsIEventSourceEventService); 15 ok(!!service, "We have the nsIEventSourceEventService"); 16 17 var innerId = SpecialPowers.wrap(window).windowGlobalChild.innerWindowId; 18 ok(innerId, "We have a valid innerWindowID: " + innerId); 19 20 var channelId = null; 21 var listener = { 22 QueryInterface(aIID) { 23 if ( 24 SpecialPowers.wrap(aIID).equals(SpecialPowers.Ci.nsISupports) || 25 SpecialPowers.wrap(aIID).equals(SpecialPowers.Ci.nsIEventSourceEventListener) 26 ) { 27 return this; 28 } 29 throw SpecialPowers.Components.results.NS_ERROR_NO_INTERFACE; 30 }, 31 eventSourceConnectionOpened(httpChannelId) { 32 info("eventSourceConnectionOpened"); 33 ok(httpChannelId > 0, "Channel ID received"); 34 channelId = httpChannelId; 35 }, 36 eventSourceConnectionClosed(httpChannelId) { 37 info("eventSourceConnectionClosed"); 38 ok(httpChannelId > 0, "Channel ID received"); 39 ok(httpChannelId === channelId, "Channel ID matched"); 40 service.removeListener(innerId, listener); 41 ok(true, "Listener removed"); 42 ok(!service.hasListenerFor(innerId), "hasListenerFor(innerId) should be false"); 43 SimpleTest.finish(); 44 }, 45 eventReceived(httpChannelId, eventName, lastEventId, data, retry, timeStamp) { 46 info("eventReceived"); 47 is(eventName, "message", "Event name is 'message'"); 48 is(lastEventId, "1", "Last event id is '1'"); 49 is(data, "msg 1", "Data is 'msg 1'"); 50 ok(retry > 0, "Reconnection time received"); 51 ok(httpChannelId === channelId, "Channel ID matched"); 52 ok(timeStamp > 0, "TimeStamp received"); 53 } 54 } 55 56 service.addListener(innerId, listener); 57 ok(true, "Listener added"); 58 ok(service.hasListenerFor(innerId), "hasListenerFor(innerId) should be true"); 59 addLoadEvent(function () { 60 const es = new EventSource("http://mochi.test:8888/tests/dom/base/test/eventsource_message.sjs"); 61 es.onmessage = function (e) { 62 es.close(); 63 }; 64 }); 65 SimpleTest.waitForExplicitFinish(); 66 </script> 67 </pre> 68 </body> 69 </html>