test_websocket-transport.html 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Test the WebSocket debugger transport</title>
  6. <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  7. <script src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script>
  8. <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
  9. </head>
  10. <body>
  11. <script>
  12. window.onload = function() {
  13. const {require} = Components.utils.import("resource://devtools/shared/Loader.jsm", {});
  14. const Services = require("Services");
  15. const {DebuggerClient} = require("devtools/shared/client/main");
  16. const {DebuggerServer} = require("devtools/server/main");
  17. Services.prefs.setBoolPref("devtools.debugger.remote-enabled", true);
  18. Services.prefs.setBoolPref("devtools.debugger.prompt-connection", false);
  19. SimpleTest.registerCleanupFunction(() => {
  20. Services.prefs.clearUserPref("devtools.debugger.remote-enabled");
  21. Services.prefs.clearUserPref("devtools.debugger.prompt-connection");
  22. });
  23. add_task(function* () {
  24. if (!DebuggerServer.initialized) {
  25. DebuggerServer.init();
  26. DebuggerServer.addBrowserActors();
  27. }
  28. is(DebuggerServer.listeningSockets, 0, "0 listening sockets");
  29. let listener = DebuggerServer.createListener();
  30. ok(listener, "Socket listener created");
  31. listener.portOrPath = -1;
  32. listener.webSocket = true;
  33. yield listener.open();
  34. is(DebuggerServer.listeningSockets, 1, "1 listening socket");
  35. let transport = yield DebuggerClient.socketConnect({
  36. host: "127.0.0.1",
  37. port: listener.port,
  38. webSocket: true
  39. });
  40. ok(transport, "Client transport created");
  41. let client = new DebuggerClient(transport);
  42. let onUnexpectedClose = () => {
  43. do_throw("Closed unexpectedly");
  44. };
  45. client.addListener("closed", onUnexpectedClose);
  46. yield client.connect();
  47. yield client.listTabs();
  48. // Send a message the server that will echo back
  49. let message = "message";
  50. let reply = yield client.request({
  51. to: "root",
  52. type: "echo",
  53. message
  54. });
  55. is(reply.message, message, "Echo message matches");
  56. client.removeListener("closed", onUnexpectedClose);
  57. transport.close();
  58. listener.close();
  59. is(DebuggerServer.listeningSockets, 0, "0 listening sockets");
  60. DebuggerServer.destroy();
  61. });
  62. }
  63. </script>
  64. </body>
  65. </html>