tor-browser

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

test_messageEvent.html (2582B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 https://bugzilla.mozilla.org/show_bug.cgi?id=848294
      5 -->
      6 <head>
      7  <meta charset="utf-8">
      8  <title>Test for Bug 848294</title>
      9  <script src="/tests/SimpleTest/SimpleTest.js"></script>
     10  <script src="/tests/SimpleTest/EventUtils.js"></script>
     11  <script src="/tests/SimpleTest/WindowSnapshot.js"></script>
     12  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
     13 </head>
     14 <body>
     15  <script type="application/javascript">
     16  function testMessageEvent(e, test) {
     17    ok(e, "MessageEvent created");
     18    is(e.type, 'message', 'MessageEvent.type is right');
     19 
     20    is(e.data, 'data' in test ? test.data : null, 'MessageEvent.data is ok');
     21    is(e.origin, 'origin' in test ? test.origin : '', 'MessageEvent.origin is ok');
     22    is(e.lastEventId, 'lastEventId' in test ? test.lastEventId : '', 'MessageEvent.lastEventId is ok');
     23    is(e.source, 'source' in test ? test.source : null, 'MessageEvent.source is ok');
     24 
     25    if (test.ports != undefined) {
     26      is(e.ports.length, test.ports.length, 'MessageEvent.ports is ok');
     27      is(e.ports, e.ports, 'MessageEvent.ports is ok');
     28    } else {
     29      ok(!('ports' in test) || test.ports == null, 'MessageEvent.ports is ok');
     30    }
     31  }
     32 
     33  function runTest() {
     34    var channel = new MessageChannel();
     35 
     36    var tests = [
     37      {},
     38      { data: 42 },
     39      { data: {} },
     40      { data: true, origin: 'wow' },
     41      { data: [], lastEventId: 'wow2'  },
     42      { data: null, source: null },
     43      { data: window, source: window },
     44      { data: window, source: channel.port1 },
     45      { data: window, source: channel.port1, ports: [ channel.port1, channel.port2 ] },
     46      { data: null, ports: [] },
     47    ];
     48 
     49    while (tests.length) {
     50      var test = tests.shift();
     51 
     52      var e = new MessageEvent('message', test);
     53      testMessageEvent(e, test);
     54 
     55      e = new MessageEvent('message');
     56      e.initMessageEvent('message', true, true,
     57                         'data' in test ? test.data : null,
     58                         'origin' in test ? test.origin : '',
     59                         'lastEventId' in test ? test.lastEventId : '',
     60                         'source' in test ? test.source : null,
     61                         'ports' in test ? test.ports : []);
     62      testMessageEvent(e, test);
     63    }
     64 
     65    try {
     66      var e = new MessageEvent('foobar', { source: 42 });
     67      ok(false, "Source has to be a window or a port");
     68    } catch(ex) {
     69      ok(true, "Source has to be a window or a port");
     70    }
     71 
     72    SimpleTest.finish();
     73  }
     74 
     75  SimpleTest.waitForExplicitFinish();
     76  runTest();
     77  </script>
     78 </body>
     79 </html>