test_WorkerDebugger_console.xul 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?xml version="1.0"?>
  2. <!--
  3. Any copyright is dedicated to the Public Domain.
  4. http://creativecommons.org/publicdomain/zero/1.0/
  5. -->
  6. <window title="Test for WorkerDebuggerGlobalScope.console methods"
  7. xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
  8. onload="test();">
  9. <script type="application/javascript"
  10. src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
  11. <script type="application/javascript"
  12. src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
  13. <script type="application/javascript" src="dom_worker_helper.js"/>
  14. <script type="application/javascript">
  15. <![CDATA[
  16. const WORKER_URL = "WorkerDebugger.console_worker.js";
  17. const CHILD_WORKER_URL = "WorkerDebugger.console_childWorker.js";
  18. const DEBUGGER_URL = BASE_URL + "WorkerDebugger.console_debugger.js";
  19. consoleMessagesReceived = 0;
  20. function test() {
  21. function consoleListener() {
  22. SpecialPowers.addObserver(this, "console-api-log-event", false);
  23. }
  24. consoleListener.prototype = {
  25. observe: function(aSubject, aTopic, aData) {
  26. if (aTopic == "console-api-log-event") {
  27. var obj = aSubject.wrappedJSObject;
  28. if (obj.arguments[0] == "Hello from the debugger script!" &&
  29. !consoleMessagesReceived) {
  30. consoleMessagesReceived++;
  31. ok(true, "Something has been received");
  32. SpecialPowers.removeObserver(this, "console-api-log-event");
  33. }
  34. }
  35. }
  36. }
  37. var cl = new consoleListener();
  38. Task.spawn(function* () {
  39. SimpleTest.waitForExplicitFinish();
  40. info("Create a worker that creates a child worker, wait for their " +
  41. "debuggers to be registered, and initialize them.");
  42. let promise = waitForMultiple([
  43. waitForRegister(WORKER_URL, DEBUGGER_URL),
  44. waitForRegister(CHILD_WORKER_URL, DEBUGGER_URL)
  45. ]);
  46. let worker = new Worker(WORKER_URL);
  47. let [dbg, childDbg] = yield promise;
  48. info("Send a request to the worker debugger. This should cause the " +
  49. "the worker debugger to send a response.");
  50. dbg.addListener({
  51. onMessage: function(msg) {
  52. try {
  53. msg = JSON.parse(msg);
  54. } catch(e) {
  55. ok(false, "Something went wrong");
  56. return;
  57. }
  58. if (msg.type == 'finish') {
  59. ok(consoleMessagesReceived, "We received something via debugger console!");
  60. dbg.removeListener(this);
  61. SimpleTest.finish();
  62. return;
  63. }
  64. if (msg.type == 'status') {
  65. ok(msg.what, msg.msg);
  66. return;
  67. }
  68. ok(false, "Something went wrong");
  69. }
  70. });
  71. dbg.postMessage("do magic");
  72. });
  73. }
  74. ]]>
  75. </script>
  76. <body xmlns="http://www.w3.org/1999/xhtml">
  77. <p id="display"></p>
  78. <div id="content" style="display:none;"></div>
  79. <pre id="test"></pre>
  80. </body>
  81. <label id="test-result"/>
  82. </window>