1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 { error } = ChromeUtils.importESModule(
"chrome://remote/content/shared/webdriver/Errors.sys.mjs"
);
const { Command, Message, Response } = ChromeUtils.importESModule(
"chrome://remote/content/marionette/message.sys.mjs"
);
add_task(function test_Message_Origin() {
equal(0, Message.Origin.Client);
equal(1, Message.Origin.Server);
});
add_task(function test_Message_fromPacket() {
let cmd = new Command(4, "foo");
let resp = new Response(5, () => {});
resp.error = "foo";
ok(Message.fromPacket(cmd.toPacket()) instanceof Command);
ok(Message.fromPacket(resp.toPacket()) instanceof Response);
Assert.throws(
() => Message.fromPacket([3, 4, 5, 6]),
/Unrecognised message type in packet/
);
});
add_task(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(Message.Origin.Client, cmd.origin);
equal(false, cmd.sent);
});
add_task(function test_Command_onresponse() {
let onerrorOk = false;
let onresultOk = false;
let cmd = new Command(7, "foo");
cmd.onerror = () => (onerrorOk = true);
cmd.onresult = () => (onresultOk = true);
let errorResp = new Response(8, () => {});
errorResp.error = new error.WebDriverError("foo");
let bodyResp = new Response(9, () => {});
bodyResp.body = "bar";
cmd.onresponse(errorResp);
equal(true, onerrorOk);
equal(false, onresultOk);
cmd.onresponse(bodyResp);
equal(true, onresultOk);
});
add_task(function test_Command_ctor() {
let cmd = new Command(42, "bar", { bar: "baz" });
let msg = cmd.toPacket();
equal(Command.Type, msg[0]);
equal(cmd.id, msg[1]);
equal(cmd.name, msg[2]);
equal(cmd.parameters, msg[3]);
});
add_task(function test_Command_toString() {
let cmd = new Command(42, "foo", { bar: "baz" });
equal(JSON.stringify(cmd.toPacket()), cmd.toString());
});
add_task(function test_Command_fromPacket() {
let c1 = new Command(42, "foo", { bar: "baz" });
let msg = c1.toPacket();
let c2 = Command.fromPacket(msg);
equal(c1.id, c2.id);
equal(c1.name, c2.name);
equal(c1.parameters, c2.parameters);
Assert.throws(
() => Command.fromPacket([null, 2, "foo", {}]),
/InvalidArgumentError/
);
Assert.throws(
() => Command.fromPacket([1, 2, "foo", {}]),
/InvalidArgumentError/
);
Assert.throws(
() => Command.fromPacket([0, null, "foo", {}]),
/InvalidArgumentError/
);
Assert.throws(
() => Command.fromPacket([0, 2, null, {}]),
/InvalidArgumentError/
);
Assert.throws(
() => Command.fromPacket([0, 2, "foo", false]),
/InvalidArgumentError/
);
let nullParams = Command.fromPacket([0, 2, "foo", null]);
equal(
"[object Object]",
Object.prototype.toString.call(nullParams.parameters)
);
});
add_task(function test_Command_Type() {
equal(0, Command.Type);
});
add_task(function test_Response_ctor() {
let handler = () => {
throw new Error("foo");
};
let resp = new Response(42, handler);
equal(42, resp.id);
equal(null, resp.error);
ok("origin" in resp);
equal(Message.Origin.Server, resp.origin);
equal(false, resp.sent);
equal(handler, resp.respHandler_);
});
add_task(function test_Response_sendConditionally() {
let fired = false;
let resp = new Response(42, () => (fired = true));
resp.sendConditionally(() => false);
equal(false, resp.sent);
equal(false, fired);
resp.sendConditionally(() => true);
equal(true, resp.sent);
equal(true, fired);
});
add_task(function test_Response_send() {
let fired = false;
let resp = new Response(42, () => (fired = true));
resp.send();
equal(true, resp.sent);
equal(true, fired);
});
add_task(function test_Response_sendError_sent() {
let resp = new Response(42, r => equal(false, r.sent));
resp.sendError(new error.WebDriverError());
ok(resp.sent);
Assert.throws(() => resp.send(), /already been sent/);
});
add_task(function test_Response_sendError_body() {
let resp = new Response(42, r => equal(null, r.body));
resp.sendError(new error.WebDriverError());
});
add_task(function test_Response_sendError_errorSerialisation() {
let err1 = new error.WebDriverError();
let resp1 = new Response(42);
resp1.sendError(err1);
equal(err1.status, resp1.error.error);
deepEqual(err1.toJSON(), resp1.error);
let err2 = new error.InvalidArgumentError();
let resp2 = new Response(43);
resp2.sendError(err2);
equal(err2.status, resp2.error.error);
deepEqual(err2.toJSON(), resp2.error);
});
add_task(function test_Response_sendError_wrapInternalError() {
let err = new ReferenceError("foo");
// errors that originate from JavaScript (i.e. Marionette implementation
// issues) should be converted to UnknownError for transport
let resp = new Response(42, r => {
equal("unknown error", r.error.error);
equal(false, resp.sent);
});
// they should also throw after being sent
Assert.throws(() => resp.sendError(err), /foo/);
equal(true, resp.sent);
});
add_task(function test_Response_toPacket() {
let resp = new Response(42, () => {});
let msg = resp.toPacket();
equal(Response.Type, msg[0]);
equal(resp.id, msg[1]);
equal(resp.error, msg[2]);
equal(resp.body, msg[3]);
});
add_task(function test_Response_toString() {
let resp = new Response(42, () => {});
resp.error = "foo";
resp.body = "bar";
equal(JSON.stringify(resp.toPacket()), resp.toString());
});
add_task(function test_Response_fromPacket() {
let r1 = new Response(42, () => {});
r1.error = "foo";
r1.body = "bar";
let msg = r1.toPacket();
let r2 = Response.fromPacket(msg);
equal(r1.id, r2.id);
equal(r1.error, r2.error);
equal(r1.body, r2.body);
Assert.throws(
() => Response.fromPacket([null, 2, "foo", {}]),
/InvalidArgumentError/
);
Assert.throws(
() => Response.fromPacket([0, 2, "foo", {}]),
/InvalidArgumentError/
);
Assert.throws(
() => Response.fromPacket([1, null, "foo", {}]),
/InvalidArgumentError/
);
Assert.throws(
() => Response.fromPacket([1, 2, null, {}]),
/InvalidArgumentError/
);
Response.fromPacket([1, 2, "foo", null]);
});
add_task(function test_Response_Type() {
equal(1, Response.Type);
});
|