test_messageEvent.html 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  10. <script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  11. <script type="application/javascript" 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. is(e.data, 'data' in test ? test.data : null, 'MessageEvent.data is ok');
  20. is(e.origin, 'origin' in test ? test.origin : '', 'MessageEvent.origin is ok');
  21. is(e.lastEventId, 'lastEventId' in test ? test.lastEventId : '', 'MessageEvent.lastEventId is ok');
  22. is(e.source, 'source' in test ? test.source : null, 'MessageEvent.source is ok');
  23. if (test.ports != undefined) {
  24. is(e.ports.length, test.ports.length, 'MessageEvent.ports is ok');
  25. is(e.ports, e.ports, 'MessageEvent.ports is ok');
  26. } else {
  27. ok(!('ports' in test) || test.ports == null, 'MessageEvent.ports is ok');
  28. }
  29. }
  30. function runTest() {
  31. var channel = new MessageChannel();
  32. var tests = [
  33. {},
  34. { data: 42 },
  35. { data: {} },
  36. { data: true, origin: 'wow' },
  37. { data: [], lastEventId: 'wow2' },
  38. { data: null, source: null },
  39. { data: window, source: window },
  40. { data: window, source: channel.port1 },
  41. { data: window, source: channel.port1, ports: [ channel.port1, channel.port2 ] },
  42. { data: null, ports: [] },
  43. ];
  44. while (tests.length) {
  45. var test = tests.shift();
  46. var e = new MessageEvent('message', test);
  47. testMessageEvent(e, test);
  48. e = new MessageEvent('message');
  49. e.initMessageEvent('message', true, true,
  50. 'data' in test ? test.data : null,
  51. 'origin' in test ? test.origin : '',
  52. 'lastEventId' in test ? test.lastEventId : '',
  53. 'source' in test ? test.source : null,
  54. 'ports' in test ? test.ports : []);
  55. testMessageEvent(e, test);
  56. }
  57. try {
  58. var e = new MessageEvent('foobar', { source: 42 });
  59. ok(false, "Source has to be a window or a port");
  60. } catch(e) {
  61. ok(true, "Source has to be a window or a port");
  62. }
  63. SimpleTest.finish();
  64. }
  65. SimpleTest.waitForExplicitFinish();
  66. runTest();
  67. </script>
  68. </body>
  69. </html>