Bug 1403577 - Associate message origin enum with Message type. r?whimboo
The MessageOrigin enum is removed as a separate type and
exposed as Message.Origin. The enum variants can be accessed as
Message.Origin.Client and Message.Origin.Server.
MozReview-Commit-ID: 1caPthSMIWx
--- a/testing/marionette/message.js
+++ b/testing/marionette/message.js
@@ -8,17 +8,16 @@ const {utils: Cu} = Components;
Cu.import("chrome://marionette/content/assert.js");
Cu.import("chrome://marionette/content/error.js");
const {truncate} = Cu.import("chrome://marionette/content/format.js", {});
this.EXPORTED_SYMBOLS = [
"Command",
"Message",
- "MessageOrigin",
"Response",
];
/** Representation of the packets transproted over the wire. */
class Message {
/**
* @param {number} messageID
* Message ID unique identifying this message.
@@ -60,23 +59,23 @@ class Message {
throw new TypeError(
"Unrecognised message type in packet: " + JSON.stringify(data));
}
}
}
/**
* Messages may originate from either the server or the client.
- * Because the remote protocol is full duplex, both endpoints may be the
- * origin of both commands and responses.
+ * Because the remote protocol is full duplex, both endpoints may be
+ * the origin of both commands and responses.
*
* @enum
* @see {@link Message}
*/
-const MessageOrigin = {
+Message.Origin = {
/** Indicates that the message originates from the client. */
Client: 0,
/** Indicates that the message originates from the server. */
Server: 1,
};
/**
* A command is a request from the client to run a series of remote end
@@ -125,17 +124,17 @@ class Command extends Message {
super(messageID);
this.name = assert.string(name);
this.parameters = assert.object(params);
this.onerror = null;
this.onresult = null;
- this.origin = MessageOrigin.Client;
+ this.origin = Message.Origin.Client;
this.sent = false;
}
/**
* Calls the error- or result handler associated with this command.
* This function can be replaced with a custom response handler.
*
* @param {Response} resp
@@ -259,17 +258,17 @@ class Response extends Message {
constructor(messageID, respHandler = () => {}) {
super(messageID);
this.respHandler_ = assert.callable(respHandler);
this.error = null;
this.body = ResponseBody();
- this.origin = MessageOrigin.Server;
+ this.origin = Message.Origin.Server;
this.sent = false;
}
/**
* Sends response conditionally, given a predicate.
*
* @param {function(Response): boolean} predicate
* A predicate taking a Response object and returning a boolean.
--- a/testing/marionette/server.js
+++ b/testing/marionette/server.js
@@ -485,17 +485,17 @@ server.TCPConnection = class {
error.report(e);
return;
}
// return immediately with any error trying to unmarshal message
let msg;
try {
msg = Message.fromPacket(data);
- msg.origin = MessageOrigin.Client;
+ msg.origin = Message.Origin.Client;
this.log_(msg);
} catch (e) {
let resp = this.createResponse(data[1]);
resp.sendError(e);
return;
}
// look up previous command we received a response for
@@ -614,17 +614,17 @@ server.TCPConnection = class {
*
* Whilst responses to commands are synchronous and must be sent in the
* correct order.
*
* @param {Message} msg
* The command or response to send.
*/
send(msg) {
- msg.origin = MessageOrigin.Server;
+ msg.origin = Message.Origin.Server;
if (msg instanceof Command) {
this.commands_.set(msg.id, msg);
this.sendToEmulator(msg);
} else if (msg instanceof Response) {
this.sendToClient(msg);
}
}
@@ -660,17 +660,16 @@ server.TCPConnection = class {
* @param {Object.<string, ?>} payload
* The payload to ship.
*/
sendRaw(payload) {
this.conn.send(payload);
}
log_(msg) {
- let a = (msg.origin == MessageOrigin.Client ? " -> " : " <- ");
- let s = JSON.stringify(msg.toMsg());
- logger.trace(this.id + a + s);
+ let dir = (msg.origin == Message.Origin.Client ? "->" : "<-");
+ logger.trace(`${this.id} ${dir} ${msg}`);
}
toString() {
return `[object server.TCPConnection ${this.id}]`;
}
};
--- a/testing/marionette/test_message.js
+++ b/testing/marionette/test_message.js
@@ -2,19 +2,19 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
const {utils: Cu} = Components;
Cu.import("chrome://marionette/content/error.js");
Cu.import("chrome://marionette/content/message.js");
-add_test(function test_MessageOrigin() {
- equal(0, MessageOrigin.Client);
- equal(1, MessageOrigin.Server);
+add_test(function test_Message_Origin() {
+ equal(0, Message.Origin.Client);
+ equal(1, Message.Origin.Server);
run_next_test();
});
add_test(function test_Message_fromPacket() {
let cmd = new Command(4, "foo");
let resp = new Response(5, () => {});
resp.error = "foo";
@@ -29,17 +29,17 @@ add_test(function test_Message_fromPacke
add_test(function test_Command() {
let cmd = new Command(42, "foo", {bar: "baz"});
equal(42, cmd.id);
equal("foo", cmd.name);
deepEqual({bar: "baz"}, cmd.parameters);
equal(null, cmd.onerror);
equal(null, cmd.onresult);
- equal(MessageOrigin.Client, cmd.origin);
+ equal(Message.Origin.Client, cmd.origin);
equal(false, cmd.sent);
run_next_test();
});
add_test(function test_Command_onresponse() {
let onerrorOk = false;
let onresultOk = false;
@@ -112,17 +112,17 @@ add_test(function test_Command_TYPE() {
add_test(function test_Response_ctor() {
let handler = () => run_next_test();
let resp = new Response(42, handler);
equal(42, resp.id);
equal(null, resp.error);
ok("origin" in resp);
- equal(MessageOrigin.Server, resp.origin);
+ equal(Message.Origin.Server, resp.origin);
equal(false, resp.sent);
equal(handler, resp.respHandler_);
run_next_test();
});
add_test(function test_Response_sendConditionally() {
let fired = false;