tor-browser

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

test_messageChannel_any.html (2312B)


      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>MessagePort/Channel any content</title>
      9  <script 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">
     18 
     19 var tests = [
     20 'hello world',
     21 123,
     22 null,
     23 true,
     24 new Date(),
     25 [ 1, 'test', true, new Date() ],
     26 { a: true, b:  null, c: new Date(), d: [ true, false, {} ] },
     27 new Blob([123], { type: 'plain/text' })
     28 ];
     29 
     30 var currentTest = null;
     31 
     32 function getType(a) {
     33  if (a === null || a === undefined)
     34    return 'null';
     35 
     36  if (Array.isArray(a))
     37    return 'array';
     38 
     39  if (typeof a == 'object')
     40    return 'object';
     41 
     42  return 'primitive';
     43 }
     44 
     45 function compare(a, b) {
     46  is (getType(a), getType(b), 'Type matches');
     47 
     48  var type = getType(a);
     49  if (type == 'array') {
     50    is (a.length, b.length, 'Array.length matches');
     51    for (var i = 0; i < a.length; ++i) {
     52      compare(a[i], b[i]);
     53    }
     54 
     55    return;
     56  }
     57 
     58  if (type == 'object') {
     59    ok (a !== b, 'They should not match');
     60 
     61    var aProps = [];
     62    for (let p in a) aProps.push(p);
     63 
     64    var bProps = [];
     65    for (let p in b) bProps.push(p);
     66 
     67    is (aProps.length, bProps.length, 'Props match');
     68    is (aProps.sort().toString(), bProps.sort().toString(), 'Prop names match');
     69 
     70    for (let p in a) {
     71      compare(a[p], b[p]);
     72    }
     73 
     74    return;
     75  }
     76 
     77  if (type != 'null') {
     78    is (a, b, 'Same value');
     79  }
     80 }
     81 
     82 function runTest() {
     83  var mc = new MessageChannel('foobar');
     84  ok(mc, "MessageChannel can be created");
     85 
     86  mc.port1.onmessage = function(event) {
     87    compare(event.data, currentTest);
     88    next();
     89  }
     90 
     91  function next() {
     92    if (!tests.length) {
     93      SimpleTest.finish();
     94      return;
     95    }
     96 
     97    currentTest = tests.shift();
     98    mc.port1.postMessage(currentTest);
     99  }
    100 
    101  var worker = new Worker("worker_messageChannel_any.js");
    102  worker.onmessage = function(event) {
    103    if (event.data == "READY") {
    104      next();
    105    }
    106  };
    107 
    108  worker.postMessage(mc.port2, [mc.port2]);
    109 }
    110 
    111 SimpleTest.waitForExplicitFinish();
    112 runTest();
    113  </script>
    114 </body>
    115 </html>