toolkit/components/social/test/browser/worker_eventsource.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/social/test/browser/worker_eventsource.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,43 @@
     1.4 +let port, es;
     1.5 +
     1.6 +let url = "https://example.com/browser/toolkit/components/social/test/browser/eventsource.resource";
     1.7 +
     1.8 +function ok(a, msg) {
     1.9 +  port.postMessage({topic: "eventSourceTest",
    1.10 +                   result: {ok: a, msg: msg}});
    1.11 +}
    1.12 +
    1.13 +function is(a, b, msg) {
    1.14 +  port.postMessage({topic: "eventSourceTest",
    1.15 +                   result: {is: a, match: b, msg: msg}});
    1.16 +}
    1.17 +
    1.18 +function esListener(e) {
    1.19 +  esListener.msg_ok = true;
    1.20 +}
    1.21 +
    1.22 +function esOnmessage(e) {
    1.23 +  ok(true, "onmessage test");
    1.24 +  ok(esListener.msg_ok, "listener test");
    1.25 +  es.close();
    1.26 +  port.postMessage({topic: "pong"});
    1.27 +}
    1.28 +
    1.29 +function doTest() {
    1.30 +  try {
    1.31 +    es = new EventSource(url);
    1.32 +    is(es.url, url, "eventsource.resource accessed", "we can create an eventsource instance");
    1.33 +    es.addEventListener('test-message', esListener, true);
    1.34 +    es.onmessage = esOnmessage;
    1.35 +  } catch (e) {}
    1.36 +  ok(!!es, "we can create an eventsource instance");
    1.37 +}
    1.38 +
    1.39 +onconnect = function(e) {
    1.40 +  port = e.ports[0];
    1.41 +  port.onmessage = function(e) {
    1.42 +    if (e.data.topic == "ping") {
    1.43 +      doTest();
    1.44 +    }
    1.45 +  }
    1.46 +}

mercurial