test_message.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  3. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. const {utils: Cu} = Components;
  5. Cu.import("chrome://marionette/content/error.js");
  6. Cu.import("chrome://marionette/content/message.js");
  7. add_test(function test_MessageOrigin() {
  8. equal(0, MessageOrigin.Client);
  9. equal(1, MessageOrigin.Server);
  10. run_next_test();
  11. });
  12. add_test(function test_Message_fromMsg() {
  13. let cmd = new Command(4, "foo");
  14. let resp = new Response(5, () => {});
  15. ok(Message.fromMsg(cmd.toMsg()) instanceof Command);
  16. ok(Message.fromMsg(resp.toMsg()) instanceof Response);
  17. Assert.throws(() => Message.fromMsg([3, 4, 5, 6]),
  18. /Unrecognised message type in packet/);
  19. run_next_test();
  20. });
  21. add_test(function test_Command() {
  22. let cmd = new Command(42, "foo", {bar: "baz"});
  23. equal(42, cmd.id);
  24. equal("foo", cmd.name);
  25. deepEqual({bar: "baz"}, cmd.parameters);
  26. equal(null, cmd.onerror);
  27. equal(null, cmd.onresult);
  28. equal(MessageOrigin.Client, cmd.origin);
  29. equal(false, cmd.sent);
  30. run_next_test();
  31. });
  32. add_test(function test_Command_onresponse() {
  33. let onerrorOk = false;
  34. let onresultOk = false;
  35. let cmd = new Command();
  36. cmd.onerror = () => onerrorOk = true;
  37. cmd.onresult = () => onresultOk = true;
  38. let errorResp = new Response();
  39. errorResp.error = new WebDriverError("foo");
  40. let bodyResp = new Response();
  41. bodyResp.body = "bar";
  42. cmd.onresponse(errorResp);
  43. equal(true, onerrorOk);
  44. equal(false, onresultOk);
  45. cmd.onresponse(bodyResp);
  46. equal(true, onresultOk);
  47. run_next_test();
  48. });
  49. add_test(function test_Command_fromMsg() {
  50. let cmd = new Command(42, "bar", {bar: "baz"});
  51. let msg = cmd.toMsg();
  52. equal(Command.TYPE, msg[0]);
  53. equal(cmd.id, msg[1]);
  54. equal(cmd.name, msg[2]);
  55. equal(cmd.parameters, msg[3]);
  56. run_next_test();
  57. });
  58. add_test(function test_Command_toString() {
  59. let cmd = new Command(42, "foo", {bar: "baz"});
  60. equal(`Command {id: ${cmd.id}, ` +
  61. `name: ${JSON.stringify(cmd.name)}, ` +
  62. `parameters: ${JSON.stringify(cmd.parameters)}}`,
  63. cmd.toString());
  64. run_next_test();
  65. });
  66. add_test(function test_Command_fromMsg() {
  67. let c1 = new Command(42, "foo", {bar: "baz"});
  68. let msg = c1.toMsg();
  69. let c2 = Command.fromMsg(msg);
  70. equal(c1.id, c2.id);
  71. equal(c1.name, c2.name);
  72. equal(c1.parameters, c2.parameters);
  73. run_next_test();
  74. });
  75. add_test(function test_Command_TYPE() {
  76. equal(0, Command.TYPE);
  77. run_next_test();
  78. });
  79. add_test(function test_Response() {
  80. let handler = () => run_next_test();
  81. let resp = new Response(42, handler);
  82. equal(42, resp.id);
  83. equal(null, resp.error);
  84. ok("origin" in resp);
  85. equal(MessageOrigin.Server, resp.origin);
  86. equal(false, resp.sent);
  87. equal(handler, resp.respHandler_);
  88. run_next_test();
  89. });
  90. add_test(function test_Response_sendConditionally() {
  91. let fired = false;
  92. let resp = new Response(42, () => fired = true);
  93. resp.sendConditionally(r => false);
  94. equal(false, resp.sent);
  95. equal(false, fired);
  96. resp.sendConditionally(r => true);
  97. equal(true, resp.sent);
  98. equal(true, fired);
  99. run_next_test();
  100. });
  101. add_test(function test_Response_send() {
  102. let fired = false;
  103. let resp = new Response(42, () => fired = true);
  104. resp.send();
  105. equal(true, resp.sent);
  106. equal(true, fired);
  107. run_next_test();
  108. });
  109. add_test(function test_Response_sendError() {
  110. let err = new WebDriverError();
  111. let resp = new Response(42, r => {
  112. equal(err.toJSON().error, r.error.error);
  113. equal(null, r.body);
  114. equal(false, r.sent);
  115. });
  116. resp.sendError(err);
  117. equal(true, resp.sent);
  118. Assert.throws(() => resp.send(), /already been sent/);
  119. resp.sent = false;
  120. Assert.throws(() => resp.sendError(new Error()));
  121. run_next_test();
  122. });
  123. add_test(function test_Response_toMsg() {
  124. let resp = new Response(42);
  125. let msg = resp.toMsg();
  126. equal(Response.TYPE, msg[0]);
  127. equal(resp.id, msg[1]);
  128. equal(resp.error, msg[2]);
  129. equal(resp.body, msg[3]);
  130. run_next_test();
  131. });
  132. add_test(function test_Response_toString() {
  133. let resp = new Response(42);
  134. resp.error = "foo";
  135. resp.body = "bar";
  136. equal(`Response {id: ${resp.id}, ` +
  137. `error: ${JSON.stringify(resp.error)}, ` +
  138. `body: ${JSON.stringify(resp.body)}}`,
  139. resp.toString());
  140. run_next_test();
  141. });
  142. add_test(function test_Response_fromMsg() {
  143. let r1 = new Response(42);
  144. r1.error = "foo";
  145. r1.body = "bar";
  146. let msg = r1.toMsg();
  147. let r2 = Response.fromMsg(msg);
  148. equal(r1.id, r2.id);
  149. equal(r1.error, r2.error);
  150. equal(r1.body, r2.body);
  151. run_next_test();
  152. });
  153. add_test(function test_Response_TYPE() {
  154. equal(1, Response.TYPE);
  155. run_next_test();
  156. });