tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_eventsourceservice_reconnect_error.html (2328B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4    <title>EventSource event service reconnect error 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;
     21 var count = {
     22  open: 0,
     23  msg: 0,
     24  close: 0
     25 };
     26 var listener = {
     27    QueryInterface(aIID) {
     28      if (
     29        SpecialPowers.wrap(aIID).equals(SpecialPowers.Ci.nsISupports) ||
     30        SpecialPowers.wrap(aIID).equals(SpecialPowers.Ci.nsIEventSourceEventListener)
     31      ) {
     32        return this;
     33      }
     34      throw SpecialPowers.Components.results.NS_ERROR_NO_INTERFACE;
     35    },
     36    eventSourceConnectionOpened(httpChannelId) {
     37      info("eventSourceConnectionOpened");
     38      ok(httpChannelId > 0, "Channel ID received");
     39      channelId = httpChannelId;
     40      count.open++;
     41    },
     42    eventSourceConnectionClosed(httpChannelId) {
     43      info("eventSourceConnectionClosed");
     44      ok (httpChannelId === channelId, "Channel was closed on failure");
     45      count.close++;
     46      SimpleTest.requestFlakyTimeout("Test for open/close");
     47      setTimeout(checkCallsCount, 2000);
     48    },
     49    eventReceived(httpChannelId, eventName, lastEventId, data, retry, timeStamp) {
     50      info("eventReceived=", retry);
     51      is(eventName, "message", "Event name is 'message'");
     52      count.msg++;
     53    }
     54 }
     55 
     56 service.addListener(innerId, listener);
     57 ok(true, "Listener added");
     58 addLoadEvent(function () {
     59    const es = new EventSource("http://mochi.test:8888/tests/dom/base/test/eventsource_reconnect.sjs?id=" + Date.now());
     60 });
     61 SimpleTest.waitForExplicitFinish();
     62 
     63 function checkCallsCount() {
     64  ok(count.open == 1, "No new open event");
     65  ok(count.close == 1, "No new close event");
     66  ok(count.msg == 1, "No new message event");
     67  SimpleTest.finish();
     68 }
     69 </script>
     70 </pre>
     71 </body>
     72 </html>