toolkit/devtools/server/tests/unit/test_protocol_simple.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 /**
michael@0 5 * Test simple requests using the protocol helpers.
michael@0 6 */
michael@0 7
michael@0 8 let protocol = devtools.require("devtools/server/protocol");
michael@0 9 let {method, Arg, Option, RetVal} = protocol;
michael@0 10 let events = devtools.require("sdk/event/core");
michael@0 11
michael@0 12 function simpleHello() {
michael@0 13 return {
michael@0 14 from: "root",
michael@0 15 applicationType: "xpcshell-tests",
michael@0 16 traits: [],
michael@0 17 }
michael@0 18 }
michael@0 19
michael@0 20 let RootActor = protocol.ActorClass({
michael@0 21 typeName: "root",
michael@0 22 initialize: function(conn) {
michael@0 23 protocol.Actor.prototype.initialize.call(this, conn);
michael@0 24 // Root actor owns itself.
michael@0 25 this.manage(this);
michael@0 26 this.actorID = "root";
michael@0 27 },
michael@0 28
michael@0 29 sayHello: simpleHello,
michael@0 30
michael@0 31 simpleReturn: method(function() {
michael@0 32 return 1;
michael@0 33 }, {
michael@0 34 response: { value: RetVal() },
michael@0 35 }),
michael@0 36
michael@0 37 promiseReturn: method(function() {
michael@0 38 return promise.resolve(1);
michael@0 39 }, {
michael@0 40 response: { value: RetVal("number") },
michael@0 41 }),
michael@0 42
michael@0 43 simpleArgs: method(function(a, b) {
michael@0 44 return { firstResponse: a + 1, secondResponse: b + 1 };
michael@0 45 }, {
michael@0 46 request: {
michael@0 47 firstArg: Arg(0),
michael@0 48 secondArg: Arg(1),
michael@0 49 },
michael@0 50 response: RetVal()
michael@0 51 }),
michael@0 52
michael@0 53 nestedArgs: method(function(a, b, c) {
michael@0 54 return { a: a, b: b, c: c };
michael@0 55 }, {
michael@0 56 request: {
michael@0 57 firstArg: Arg(0),
michael@0 58 nest: {
michael@0 59 secondArg: Arg(1),
michael@0 60 nest: {
michael@0 61 thirdArg: Arg(2)
michael@0 62 }
michael@0 63 }
michael@0 64 },
michael@0 65 response: RetVal()
michael@0 66 }),
michael@0 67
michael@0 68 optionArgs: method(function(options) {
michael@0 69 return { option1: options.option1, option2: options.option2 };
michael@0 70 }, {
michael@0 71 request: {
michael@0 72 option1: Option(0),
michael@0 73 option2: Option(0)
michael@0 74 },
michael@0 75 response: RetVal()
michael@0 76 }),
michael@0 77
michael@0 78 optionalArgs: method(function(a, b=200) {
michael@0 79 return b;
michael@0 80 }, {
michael@0 81 request: {
michael@0 82 a: Arg(0),
michael@0 83 b: Arg(1, "nullable:number")
michael@0 84 },
michael@0 85 response: {
michael@0 86 value: RetVal("number")
michael@0 87 },
michael@0 88 }),
michael@0 89
michael@0 90 arrayArgs: method(function(a) {
michael@0 91 return a;
michael@0 92 }, {
michael@0 93 request: {
michael@0 94 a: Arg(0, "array:number")
michael@0 95 },
michael@0 96 response: {
michael@0 97 arrayReturn: RetVal("array:number")
michael@0 98 },
michael@0 99 }),
michael@0 100
michael@0 101 nestedArrayArgs: method(function(a) {
michael@0 102 return a;
michael@0 103 }, {
michael@0 104 request: { a: Arg(0, "array:array:number") },
michael@0 105 response: { value: RetVal("array:array:number") },
michael@0 106 }),
michael@0 107
michael@0 108 /**
michael@0 109 * Test that the 'type' part of the request packet works
michael@0 110 * correctly when the type isn't the same as the method name
michael@0 111 */
michael@0 112 renamedEcho: method(function(a) {
michael@0 113 if (this.conn.currentPacket.type != "echo") {
michael@0 114 return "goodbye";
michael@0 115 }
michael@0 116 return a;
michael@0 117 }, {
michael@0 118 request: {
michael@0 119 type: "echo",
michael@0 120 a: Arg(0),
michael@0 121 },
michael@0 122 response: {
michael@0 123 value: RetVal("string")
michael@0 124 },
michael@0 125 }),
michael@0 126
michael@0 127 testOneWay: method(function(a) {
michael@0 128 // Emit to show that we got this message, because there won't be a response.
michael@0 129 events.emit(this, "oneway", a);
michael@0 130 }, {
michael@0 131 request: { a: Arg(0) },
michael@0 132 oneway: true
michael@0 133 }),
michael@0 134
michael@0 135 events: {
michael@0 136 "oneway": { a: Arg(0) }
michael@0 137 }
michael@0 138 });
michael@0 139
michael@0 140 let RootFront = protocol.FrontClass(RootActor, {
michael@0 141 initialize: function(client) {
michael@0 142 this.actorID = "root";
michael@0 143 protocol.Front.prototype.initialize.call(this, client);
michael@0 144 // Root owns itself.
michael@0 145 this.manage(this);
michael@0 146 }
michael@0 147 });
michael@0 148
michael@0 149 function run_test()
michael@0 150 {
michael@0 151 DebuggerServer.createRootActor = (conn => {
michael@0 152 return RootActor(conn);
michael@0 153 });
michael@0 154 DebuggerServer.init(() => true);
michael@0 155
michael@0 156 check_except(() => {
michael@0 157 let badActor = ActorClass({
michael@0 158 missing: preEvent("missing-event", function() {
michael@0 159 })
michael@0 160 })
michael@0 161 });
michael@0 162
michael@0 163 protocol.types.getType("array:array:array:number");
michael@0 164 protocol.types.getType("array:array:array:number");
michael@0 165
michael@0 166 check_except(() => protocol.types.getType("unknown"));
michael@0 167 check_except(() => protocol.types.getType("array:unknown"));
michael@0 168 check_except(() => protocol.types.getType("unknown:number"));
michael@0 169 let trace = connectPipeTracing();
michael@0 170 let client = new DebuggerClient(trace);
michael@0 171 let rootClient;
michael@0 172
michael@0 173 client.connect((applicationType, traits) => {
michael@0 174 trace.expectReceive({"from":"<actorid>","applicationType":"xpcshell-tests","traits":[]});
michael@0 175 do_check_eq(applicationType, "xpcshell-tests");
michael@0 176
michael@0 177 rootClient = RootFront(client);
michael@0 178
michael@0 179 rootClient.simpleReturn().then(ret => {
michael@0 180 trace.expectSend({"type":"simpleReturn","to":"<actorid>"});
michael@0 181 trace.expectReceive({"value":1,"from":"<actorid>"});
michael@0 182 do_check_eq(ret, 1);
michael@0 183 }).then(() => {
michael@0 184 return rootClient.promiseReturn();
michael@0 185 }).then(ret => {
michael@0 186 trace.expectSend({"type":"promiseReturn","to":"<actorid>"});
michael@0 187 trace.expectReceive({"value":1,"from":"<actorid>"});
michael@0 188 do_check_eq(ret, 1);
michael@0 189 }).then(() => {
michael@0 190 // Missing argument should throw an exception
michael@0 191 check_except(() => {
michael@0 192 rootClient.simpleArgs(5);
michael@0 193 });
michael@0 194
michael@0 195 return rootClient.simpleArgs(5, 10)
michael@0 196 }).then(ret => {
michael@0 197 trace.expectSend({"type":"simpleArgs","firstArg":5,"secondArg":10,"to":"<actorid>"});
michael@0 198 trace.expectReceive({"firstResponse":6,"secondResponse":11,"from":"<actorid>"});
michael@0 199 do_check_eq(ret.firstResponse, 6);
michael@0 200 do_check_eq(ret.secondResponse, 11);
michael@0 201 }).then(() => {
michael@0 202 return rootClient.nestedArgs(1, 2, 3);
michael@0 203 }).then(ret => {
michael@0 204 trace.expectSend({"type":"nestedArgs","firstArg":1,"nest":{"secondArg":2,"nest":{"thirdArg":3}},"to":"<actorid>"});
michael@0 205 trace.expectReceive({"a":1,"b":2,"c":3,"from":"<actorid>"});
michael@0 206 do_check_eq(ret.a, 1);
michael@0 207 do_check_eq(ret.b, 2);
michael@0 208 do_check_eq(ret.c, 3);
michael@0 209 }).then(() => {
michael@0 210 return rootClient.optionArgs({
michael@0 211 "option1": 5,
michael@0 212 "option2": 10
michael@0 213 });
michael@0 214 }).then(ret => {
michael@0 215 trace.expectSend({"type":"optionArgs","option1":5,"option2":10,"to":"<actorid>"});
michael@0 216 trace.expectReceive({"option1":5,"option2":10,"from":"<actorid>"});
michael@0 217 do_check_eq(ret.option1, 5);
michael@0 218 do_check_eq(ret.option2, 10);
michael@0 219 }).then(() => {
michael@0 220 return rootClient.optionArgs({});
michael@0 221 }).then(ret => {
michael@0 222 trace.expectSend({"type":"optionArgs","to":"<actorid>"});
michael@0 223 trace.expectReceive({"from":"<actorid>"});
michael@0 224 do_check_true(typeof(ret.option1) === "undefined");
michael@0 225 do_check_true(typeof(ret.option2) === "undefined");
michael@0 226 }).then(ret => {
michael@0 227 // Explicitly call an optional argument...
michael@0 228 return rootClient.optionalArgs(5, 10);
michael@0 229 }).then(ret => {
michael@0 230 trace.expectSend({"type":"optionalArgs","a":5,"b":10,"to":"<actorid>"});
michael@0 231 trace.expectReceive({"value":10,"from":"<actorid>"});
michael@0 232 do_check_eq(ret, 10);
michael@0 233 }).then(() => {
michael@0 234 // Now don't pass the optional argument, expect the default.
michael@0 235 return rootClient.optionalArgs(5);
michael@0 236 }).then(ret => {
michael@0 237 trace.expectSend({"type":"optionalArgs","a":5,"to":"<actorid>"});
michael@0 238 trace.expectReceive({"value":200,"from":"<actorid>"});
michael@0 239 do_check_eq(ret, 200);
michael@0 240 }).then(ret => {
michael@0 241 return rootClient.arrayArgs([0, 1, 2, 3, 4, 5]);
michael@0 242 }).then(ret => {
michael@0 243 trace.expectSend({"type":"arrayArgs","a":[0,1,2,3,4,5],"to":"<actorid>"});
michael@0 244 trace.expectReceive({"arrayReturn":[0,1,2,3,4,5],"from":"<actorid>"});
michael@0 245 do_check_eq(ret[0], 0);
michael@0 246 do_check_eq(ret[5], 5);
michael@0 247 }).then(() => {
michael@0 248 return rootClient.arrayArgs([[5]]);
michael@0 249 }).then(ret => {
michael@0 250 trace.expectSend({"type":"arrayArgs","a":[[5]],"to":"<actorid>"});
michael@0 251 trace.expectReceive({"arrayReturn":[[5]],"from":"<actorid>"});
michael@0 252 do_check_eq(ret[0][0], 5);
michael@0 253 }).then(() => {
michael@0 254 return rootClient.renamedEcho("hello");
michael@0 255 }).then(str => {
michael@0 256 trace.expectSend({"type":"echo","a":"hello","to":"<actorid>"});
michael@0 257 trace.expectReceive({"value":"hello","from":"<actorid>"});
michael@0 258
michael@0 259 do_check_eq(str, "hello");
michael@0 260
michael@0 261 let deferred = promise.defer();
michael@0 262 rootClient.on("oneway", (response) => {
michael@0 263 trace.expectSend({"type":"testOneWay","a":"hello","to":"<actorid>"});
michael@0 264 trace.expectReceive({"type":"oneway","a":"hello","from":"<actorid>"});
michael@0 265
michael@0 266 do_check_eq(response, "hello");
michael@0 267 deferred.resolve();
michael@0 268 });
michael@0 269 do_check_true(typeof(rootClient.testOneWay("hello")) === "undefined");
michael@0 270 return deferred.promise;
michael@0 271 }).then(() => {
michael@0 272 client.close(() => {
michael@0 273 do_test_finished();
michael@0 274 });
michael@0 275 }).then(null, err => {
michael@0 276 do_report_unexpected_exception(err, "Failure executing test");
michael@0 277 });
michael@0 278 });
michael@0 279 do_test_pending();
michael@0 280 }

mercurial