dom/base/test/test_messageChannel_start.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 <!DOCTYPE HTML>
     2 <html>
     3 <!--
     4 https://bugzilla.mozilla.org/show_bug.cgi?id=677638
     5 -->
     6 <head>
     7   <meta charset="utf-8">
     8   <title>Test for Bug 677638 - start/close</title>
     9   <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    10   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
    11 </head>
    12 <body>
    13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=677638">Mozilla Bug 677638</a>
    14 <div id="content"></div>
    15 <pre id="test">
    16 </pre>
    17   <script type="application/javascript">
    19   function runTests() {
    20     if (!tests.length) {
    21       SimpleTest.finish();
    22       return;
    23     }
    25     var test = tests.shift();
    26     test();
    27   }
    29   function testOnMessage() {
    30     var a = new MessageChannel();
    31     ok(a, "MessageChannel created");
    33     a.port1.postMessage(42);
    34     a.port2.postMessage(43);
    35     ok(true, "MessagePort{1,2}.postmessage() invoked");
    37     var events = 2;
    39     a.port1.onmessage = function(evt) {
    40       ok(true, "This method should be called");
    41       if (!--events) runTests();
    42     }
    44     a.port2.onmessage = function(evt) {
    45       ok(true, "This method should be called");
    46       if (!--events) runTests();
    47     }
    48   }
    50   function testAddEventListener() {
    51     var a = new MessageChannel();
    52     ok(a, "MessageChannel created");
    54     a.port1.postMessage(42);
    55     a.port2.postMessage(43);
    56     ok(true, "MessagePort{1,2}.postmessage() invoked");
    58     a.port1.addEventListener('message', function(evt) {
    59       ok(false, "This method should not be called");
    60     }, false);
    62     a.port2.addEventListener('message', function(evt) {
    63       ok(false, "This method should not be called");
    64     }, false);
    66     setTimeout(runTests, 0);
    67   }
    69   function testAddEventListenerAndStart() {
    70     var a = new MessageChannel();
    71     ok(a, "MessageChannel created");
    73     a.port1.postMessage(42);
    74     a.port2.postMessage(43);
    75     ok(true, "MessagePort{1,2}.postmessage() invoked");
    77     var events = 2;
    79     a.port1.addEventListener('message', function(evt) {
    80       ok(true, "This method should be called");
    81       if (!--events) runTests();
    82     }, false);
    84     a.port2.addEventListener('message', function(evt) {
    85       ok(true, "This method should be called");
    86       if (!--events) runTests();
    87     }, false);
    89     a.port1.start();
    90     a.port2.start();
    91   }
    93   function testAddEventListener1AndStart() {
    94     var a = new MessageChannel();
    95     ok(a, "MessageChannel created");
    97     a.port1.postMessage(42);
    98     a.port2.postMessage(43);
    99     ok(true, "MessagePort{1,2}.postmessage() invoked");
   101     var events = 1;
   103     a.port1.addEventListener('message', function(evt) {
   104       ok(true, "This method should be called");
   105       if (!--events) runTests();
   106     }, false);
   108     a.port2.addEventListener('message', function(evt) {
   109       ok(false, "This method should not be called");
   110     }, false);
   112     a.port1.start();
   113   }
   115   function testAddEventListener2AndStart() {
   116     var a = new MessageChannel();
   117     ok(a, "MessageChannel created");
   119     a.port1.postMessage(42);
   120     a.port2.postMessage(43);
   121     ok(true, "MessagePort{1,2}.postmessage() invoked");
   123     var events = 1;
   125     a.port1.addEventListener('message', function(evt) {
   126       ok(false, "This method should not be called");
   127     }, false);
   129     a.port2.addEventListener('message', function(evt) {
   130       ok(true, "This method should be called");
   131       if (!--events) runTests();
   132     }, false);
   134     a.port2.start();
   135   }
   137   function testTimer() {
   138     var a = new MessageChannel();
   139     ok(a, "MessageChannel created");
   141     a.port1.postMessage(42);
   142     a.port2.postMessage(43);
   143     ok(true, "MessagePort{1,2}.postmessage() invoked");
   145     setTimeout(function() {
   146       var events = 2;
   147       a.port1.onmessage = function(evt) {
   148         ok(true, "This method should be called");
   149         if (!--events) runTests();
   150       }
   152       a.port2.onmessage = function(evt) {
   153         ok(true, "This method should be called");
   154         if (!--events) runTests();
   155       }
   156     }, 200);
   157   }
   159   function testAddEventListenerAndStartWrongOrder() {
   160     var a = new MessageChannel();
   161     ok(a, "MessageChannel created");
   163     a.port1.postMessage(42);
   164     a.port2.postMessage(43);
   165     ok(true, "MessagePort{1,2}.postmessage() invoked");
   167     var events = 2;
   169     a.port1.start();
   170     a.port1.addEventListener('message', function(evt) {
   171       ok(true, "This method should be called");
   172       if (!--events) runTests();
   173     }, false);
   175     a.port2.start();
   176     a.port2.addEventListener('message', function(evt) {
   177       ok(true, "This method should be called");
   178       if (!--events) runTests();
   179     }, false);
   180   }
   182   function testOnMessageClone() {
   183     var a = new MessageChannel();
   184     ok(a, "MessageChannel created");
   186     a.port1.postMessage(42);
   187     a.port2.postMessage(43);
   188     ok(true, "MessagePort{1,2}.postmessage() invoked");
   190     var events = 2;
   192     addEventListener('message', testOnMessageCloneCb, false);
   193     function testOnMessageCloneCb(evt) {
   194       a.port1.onmessage = function(evt) {
   195         ok(true, "This method should be called");
   196         testOnMessageCloneFinish();
   197       }
   199       evt.data.onmessage = function(evt) {
   200         ok(true, "This method should be called");
   201         testOnMessageCloneFinish();
   202       }
   204       a.port2.onmessage = function(evt) {
   205         ok(false, "This method should not be called");
   206       }
   207     }
   209     function testOnMessageCloneFinish() {
   210       if (!--events) {
   211         removeEventListener('message', testOnMessageCloneCb);
   212         runTests();
   213       }
   214     }
   216     postMessage(a.port2, '*', [a.port2]);
   217   }
   219   var tests = [
   220     testOnMessage,
   221     testAddEventListener,
   222     testAddEventListenerAndStart,
   223     testAddEventListener1AndStart,
   224     testAddEventListener2AndStart,
   225     testTimer,
   226     testAddEventListenerAndStartWrongOrder,
   227     testOnMessageClone,
   228   ];
   230   SimpleTest.waitForExplicitFinish();
   231   SpecialPowers.pushPrefEnv({"set": [["dom.messageChannel.enabled", true]]}, runTests);
   232   </script>
   233 </body>
   234 </html>

mercurial