test_protocol_children.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /* Any copyright is dedicated to the Public Domain.
  2. http://creativecommons.org/publicdomain/zero/1.0/ */
  3. /**
  4. * Test simple requests using the protocol helpers.
  5. */
  6. var protocol = require("devtools/shared/protocol");
  7. var {preEvent, types, Arg, Option, RetVal} = protocol;
  8. var events = require("sdk/event/core");
  9. function simpleHello() {
  10. return {
  11. from: "root",
  12. applicationType: "xpcshell-tests",
  13. traits: [],
  14. };
  15. }
  16. var testTypes = {};
  17. // Predeclaring the actor type so that it can be used in the
  18. // implementation of the child actor.
  19. types.addActorType("childActor");
  20. const childSpec = protocol.generateActorSpec({
  21. typeName: "childActor",
  22. events: {
  23. "event1" : {
  24. a: Arg(0),
  25. b: Arg(1),
  26. c: Arg(2)
  27. },
  28. "event2" : {
  29. a: Arg(0),
  30. b: Arg(1),
  31. c: Arg(2)
  32. },
  33. "named-event": {
  34. type: "namedEvent",
  35. a: Arg(0),
  36. b: Arg(1),
  37. c: Arg(2)
  38. },
  39. "object-event": {
  40. type: "objectEvent",
  41. detail: Arg(0, "childActor#detail1"),
  42. },
  43. "array-object-event": {
  44. type: "arrayObjectEvent",
  45. detail: Arg(0, "array:childActor#detail2"),
  46. }
  47. },
  48. methods: {
  49. echo: {
  50. request: { str: Arg(0) },
  51. response: { str: RetVal("string") },
  52. },
  53. getDetail1: {
  54. // This also exercises return-value-as-packet.
  55. response: RetVal("childActor#detail1"),
  56. },
  57. getDetail2: {
  58. // This also exercises return-value-as-packet.
  59. response: RetVal("childActor#detail2"),
  60. },
  61. getIDDetail: {
  62. response: {
  63. idDetail: RetVal("childActor#actorid")
  64. }
  65. },
  66. getIntArray: {
  67. request: { inputArray: Arg(0, "array:number") },
  68. response: RetVal("array:number")
  69. },
  70. getSibling: {
  71. request: { id: Arg(0) },
  72. response: { sibling: RetVal("childActor") }
  73. },
  74. emitEvents: {
  75. response: { value: "correct response" },
  76. },
  77. release: {
  78. release: true
  79. }
  80. }
  81. });
  82. var ChildActor = protocol.ActorClassWithSpec(childSpec, {
  83. // Actors returned by this actor should be owned by the root actor.
  84. marshallPool: function () { return this.parent(); },
  85. toString: function () { return "[ChildActor " + this.childID + "]"; },
  86. initialize: function (conn, id) {
  87. protocol.Actor.prototype.initialize.call(this, conn);
  88. this.childID = id;
  89. },
  90. destroy: function () {
  91. protocol.Actor.prototype.destroy.call(this);
  92. this.destroyed = true;
  93. },
  94. form: function (detail) {
  95. if (detail === "actorid") {
  96. return this.actorID;
  97. }
  98. return {
  99. actor: this.actorID,
  100. childID: this.childID,
  101. detail: detail
  102. };
  103. },
  104. echo: function (str) {
  105. return str;
  106. },
  107. getDetail1: function () {
  108. return this;
  109. },
  110. getDetail2: function () {
  111. return this;
  112. },
  113. getIDDetail: function () {
  114. return this;
  115. },
  116. getIntArray: function (inputArray) {
  117. // Test that protocol.js converts an iterator to an array.
  118. let f = function* () {
  119. for (let i of inputArray) {
  120. yield 2 * i;
  121. }
  122. };
  123. return f();
  124. },
  125. getSibling: function (id) {
  126. return this.parent().getChild(id);
  127. },
  128. emitEvents: function () {
  129. events.emit(this, "event1", 1, 2, 3);
  130. events.emit(this, "event2", 4, 5, 6);
  131. events.emit(this, "named-event", 1, 2, 3);
  132. events.emit(this, "object-event", this);
  133. events.emit(this, "array-object-event", [this]);
  134. },
  135. release: function () { },
  136. });
  137. var ChildFront = protocol.FrontClassWithSpec(childSpec, {
  138. initialize: function (client, form) {
  139. protocol.Front.prototype.initialize.call(this, client, form);
  140. },
  141. destroy: function () {
  142. this.destroyed = true;
  143. protocol.Front.prototype.destroy.call(this);
  144. },
  145. marshallPool: function () { return this.parent(); },
  146. toString: function () { return "[child front " + this.childID + "]"; },
  147. form: function (form, detail) {
  148. if (detail === "actorid") {
  149. return;
  150. }
  151. this.childID = form.childID;
  152. this.detail = form.detail;
  153. },
  154. onEvent1: preEvent("event1", function (a, b, c) {
  155. this.event1arg3 = c;
  156. }),
  157. onEvent2a: preEvent("event2", function (a, b, c) {
  158. return promise.resolve().then(() => this.event2arg3 = c);
  159. }),
  160. onEvent2b: preEvent("event2", function (a, b, c) {
  161. this.event2arg2 = b;
  162. }),
  163. });
  164. types.addDictType("manyChildrenDict", {
  165. child5: "childActor",
  166. more: "array:childActor",
  167. });
  168. types.addLifetime("temp", "_temporaryHolder");
  169. const rootSpec = protocol.generateActorSpec({
  170. typeName: "root",
  171. methods: {
  172. getChild: {
  173. request: { str: Arg(0) },
  174. response: { actor: RetVal("childActor") },
  175. },
  176. getChildren: {
  177. request: { ids: Arg(0, "array:string") },
  178. response: { children: RetVal("array:childActor") },
  179. },
  180. getChildren2: {
  181. request: { ids: Arg(0, "array:childActor") },
  182. response: { children: RetVal("array:childActor") },
  183. },
  184. getManyChildren: {
  185. response: RetVal("manyChildrenDict")
  186. },
  187. getTemporaryChild: {
  188. request: { id: Arg(0) },
  189. response: { child: RetVal("temp:childActor") }
  190. },
  191. clearTemporaryChildren: {}
  192. }
  193. });
  194. var rootActor = null;
  195. var RootActor = protocol.ActorClassWithSpec(rootSpec, {
  196. toString: function () { return "[root actor]"; },
  197. initialize: function (conn) {
  198. rootActor = this;
  199. this.actorID = "root";
  200. this._children = {};
  201. protocol.Actor.prototype.initialize.call(this, conn);
  202. // Root actor owns itself.
  203. this.manage(this);
  204. },
  205. sayHello: simpleHello,
  206. getChild: function (id) {
  207. if (id in this._children) {
  208. return this._children[id];
  209. }
  210. let child = new ChildActor(this.conn, id);
  211. this._children[id] = child;
  212. return child;
  213. },
  214. getChildren: function (ids) {
  215. return ids.map(id => this.getChild(id));
  216. },
  217. getChildren2: function (ids) {
  218. let f = function* () {
  219. for (let c of ids) {
  220. yield c;
  221. }
  222. };
  223. return f();
  224. },
  225. getManyChildren: function () {
  226. return {
  227. foo: "bar", // note that this isn't in the specialization array.
  228. child5: this.getChild("child5"),
  229. more: [ this.getChild("child6"), this.getChild("child7") ]
  230. };
  231. },
  232. // This should remind you of a pause actor.
  233. getTemporaryChild: function (id) {
  234. if (!this._temporaryHolder) {
  235. this._temporaryHolder = this.manage(new protocol.Actor(this.conn));
  236. }
  237. return new ChildActor(this.conn, id);
  238. },
  239. clearTemporaryChildren: function (id) {
  240. if (!this._temporaryHolder) {
  241. return;
  242. }
  243. this._temporaryHolder.destroy();
  244. delete this._temporaryHolder;
  245. }
  246. });
  247. var RootFront = protocol.FrontClassWithSpec(rootSpec, {
  248. toString: function () { return "[root front]"; },
  249. initialize: function (client) {
  250. this.actorID = "root";
  251. protocol.Front.prototype.initialize.call(this, client);
  252. // Root actor owns itself.
  253. this.manage(this);
  254. },
  255. getTemporaryChild: protocol.custom(function (id) {
  256. if (!this._temporaryHolder) {
  257. this._temporaryHolder = protocol.Front(this.conn);
  258. this._temporaryHolder.actorID = this.actorID + "_temp";
  259. this._temporaryHolder = this.manage(this._temporaryHolder);
  260. }
  261. return this._getTemporaryChild(id);
  262. }, {
  263. impl: "_getTemporaryChild"
  264. }),
  265. clearTemporaryChildren: protocol.custom(function () {
  266. if (!this._temporaryHolder) {
  267. return promise.resolve(undefined);
  268. }
  269. this._temporaryHolder.destroy();
  270. delete this._temporaryHolder;
  271. return this._clearTemporaryChildren();
  272. }, {
  273. impl: "_clearTemporaryChildren"
  274. })
  275. });
  276. function run_test()
  277. {
  278. DebuggerServer.createRootActor = (conn => {
  279. return RootActor(conn);
  280. });
  281. DebuggerServer.init();
  282. let trace = connectPipeTracing();
  283. let client = new DebuggerClient(trace);
  284. client.connect().then(([applicationType, traits]) => {
  285. trace.expectReceive({"from":"<actorid>", "applicationType":"xpcshell-tests", "traits":[]});
  286. do_check_eq(applicationType, "xpcshell-tests");
  287. let rootFront = RootFront(client);
  288. let childFront = null;
  289. let expectRootChildren = size => {
  290. do_check_eq(rootActor._poolMap.size, size + 1);
  291. do_check_eq(rootFront._poolMap.size, size + 1);
  292. if (childFront) {
  293. do_check_eq(childFront._poolMap.size, 0);
  294. }
  295. };
  296. rootFront.getChild("child1").then(ret => {
  297. trace.expectSend({"type":"getChild", "str":"child1", "to":"<actorid>"});
  298. trace.expectReceive({"actor":"<actorid>", "from":"<actorid>"});
  299. childFront = ret;
  300. do_check_true(childFront instanceof ChildFront);
  301. do_check_eq(childFront.childID, "child1");
  302. expectRootChildren(1);
  303. }).then(() => {
  304. // Request the child again, make sure the same is returned.
  305. return rootFront.getChild("child1");
  306. }).then(ret => {
  307. trace.expectSend({"type":"getChild", "str":"child1", "to":"<actorid>"});
  308. trace.expectReceive({"actor":"<actorid>", "from":"<actorid>"});
  309. expectRootChildren(1);
  310. do_check_true(ret === childFront);
  311. }).then(() => {
  312. return childFront.echo("hello");
  313. }).then(ret => {
  314. trace.expectSend({"type":"echo", "str":"hello", "to":"<actorid>"});
  315. trace.expectReceive({"str":"hello", "from":"<actorid>"});
  316. do_check_eq(ret, "hello");
  317. }).then(() => {
  318. return childFront.getDetail1();
  319. }).then(ret => {
  320. trace.expectSend({"type":"getDetail1", "to":"<actorid>"});
  321. trace.expectReceive({"actor":"<actorid>", "childID":"child1", "detail":"detail1", "from":"<actorid>"});
  322. do_check_true(ret === childFront);
  323. do_check_eq(childFront.detail, "detail1");
  324. }).then(() => {
  325. return childFront.getDetail2();
  326. }).then(ret => {
  327. trace.expectSend({"type":"getDetail2", "to":"<actorid>"});
  328. trace.expectReceive({"actor":"<actorid>", "childID":"child1", "detail":"detail2", "from":"<actorid>"});
  329. do_check_true(ret === childFront);
  330. do_check_eq(childFront.detail, "detail2");
  331. }).then(() => {
  332. return childFront.getIDDetail();
  333. }).then(ret => {
  334. trace.expectSend({"type":"getIDDetail", "to":"<actorid>"});
  335. trace.expectReceive({"idDetail": childFront.actorID, "from":"<actorid>"});
  336. do_check_true(ret === childFront);
  337. }).then(() => {
  338. return childFront.getSibling("siblingID");
  339. }).then(ret => {
  340. trace.expectSend({"type":"getSibling", "id":"siblingID", "to":"<actorid>"});
  341. trace.expectReceive({"sibling":{"actor":"<actorid>", "childID":"siblingID"}, "from":"<actorid>"});
  342. expectRootChildren(2);
  343. }).then(ret => {
  344. return rootFront.getTemporaryChild("temp1").then(temp1 => {
  345. trace.expectSend({"type":"getTemporaryChild", "id":"temp1", "to":"<actorid>"});
  346. trace.expectReceive({"child":{"actor":"<actorid>", "childID":"temp1"}, "from":"<actorid>"});
  347. // At this point we expect two direct children, plus the temporary holder
  348. // which should hold 1 itself.
  349. do_check_eq(rootActor._temporaryHolder.__poolMap.size, 1);
  350. do_check_eq(rootFront._temporaryHolder.__poolMap.size, 1);
  351. expectRootChildren(3);
  352. return rootFront.getTemporaryChild("temp2").then(temp2 => {
  353. trace.expectSend({"type":"getTemporaryChild", "id":"temp2", "to":"<actorid>"});
  354. trace.expectReceive({"child":{"actor":"<actorid>", "childID":"temp2"}, "from":"<actorid>"});
  355. // Same amount of direct children, and an extra in the temporary holder.
  356. expectRootChildren(3);
  357. do_check_eq(rootActor._temporaryHolder.__poolMap.size, 2);
  358. do_check_eq(rootFront._temporaryHolder.__poolMap.size, 2);
  359. // Get the children of the temporary holder...
  360. let checkActors = rootActor._temporaryHolder.__poolMap.values();
  361. let checkFronts = rootFront._temporaryHolder.__poolMap.values();
  362. // Now release the temporary holders and expect them to drop again.
  363. return rootFront.clearTemporaryChildren().then(() => {
  364. trace.expectSend({"type":"clearTemporaryChildren", "to":"<actorid>"});
  365. trace.expectReceive({"from":"<actorid>"});
  366. expectRootChildren(2);
  367. do_check_false(!!rootActor._temporaryHolder);
  368. do_check_false(!!rootFront._temporaryHolder);
  369. for (let checkActor of checkActors) {
  370. do_check_true(checkActor.destroyed);
  371. do_check_true(checkActor.destroyed);
  372. }
  373. });
  374. });
  375. });
  376. }).then(ret => {
  377. return rootFront.getChildren(["child1", "child2"]);
  378. }).then(ret => {
  379. trace.expectSend({"type":"getChildren", "ids":["child1", "child2"], "to":"<actorid>"});
  380. trace.expectReceive({"children":[{"actor":"<actorid>", "childID":"child1"}, {"actor":"<actorid>", "childID":"child2"}], "from":"<actorid>"});
  381. expectRootChildren(3);
  382. do_check_true(ret[0] === childFront);
  383. do_check_true(ret[1] !== childFront);
  384. do_check_true(ret[1] instanceof ChildFront);
  385. // On both children, listen to events. We're only
  386. // going to trigger events on the first child, so an event
  387. // triggered on the second should cause immediate failures.
  388. let set = new Set(["event1", "event2", "named-event", "object-event", "array-object-event"]);
  389. childFront.on("event1", (a, b, c) => {
  390. do_check_eq(a, 1);
  391. do_check_eq(b, 2);
  392. do_check_eq(c, 3);
  393. // Verify that the pre-event handler was called.
  394. do_check_eq(childFront.event1arg3, 3);
  395. set.delete("event1");
  396. });
  397. childFront.on("event2", (a, b, c) => {
  398. do_check_eq(a, 4);
  399. do_check_eq(b, 5);
  400. do_check_eq(c, 6);
  401. // Verify that the async pre-event handler was called,
  402. // setting the property before this handler was called.
  403. do_check_eq(childFront.event2arg3, 6);
  404. // And check that the sync preEvent with the same name is also
  405. // executed
  406. do_check_eq(childFront.event2arg2, 5);
  407. set.delete("event2");
  408. });
  409. childFront.on("named-event", (a, b, c) => {
  410. do_check_eq(a, 1);
  411. do_check_eq(b, 2);
  412. do_check_eq(c, 3);
  413. set.delete("named-event");
  414. });
  415. childFront.on("object-event", (obj) => {
  416. do_check_true(obj === childFront);
  417. do_check_eq(childFront.detail, "detail1");
  418. set.delete("object-event");
  419. });
  420. childFront.on("array-object-event", (array) => {
  421. do_check_true(array[0] === childFront);
  422. do_check_eq(childFront.detail, "detail2");
  423. set.delete("array-object-event");
  424. });
  425. let fail = function () {
  426. do_throw("Unexpected event");
  427. };
  428. ret[1].on("event1", fail);
  429. ret[1].on("event2", fail);
  430. ret[1].on("named-event", fail);
  431. ret[1].on("object-event", fail);
  432. ret[1].on("array-object-event", fail);
  433. return childFront.emitEvents().then(() => {
  434. trace.expectSend({"type":"emitEvents", "to":"<actorid>"});
  435. trace.expectReceive({"type":"event1", "a":1, "b":2, "c":3, "from":"<actorid>"});
  436. trace.expectReceive({"type":"event2", "a":4, "b":5, "c":6, "from":"<actorid>"});
  437. trace.expectReceive({"type":"namedEvent", "a":1, "b":2, "c":3, "from":"<actorid>"});
  438. trace.expectReceive({"type":"objectEvent", "detail":{"actor":"<actorid>", "childID":"child1", "detail":"detail1"}, "from":"<actorid>"});
  439. trace.expectReceive({"type":"arrayObjectEvent", "detail":[{"actor":"<actorid>", "childID":"child1", "detail":"detail2"}], "from":"<actorid>"});
  440. trace.expectReceive({"value":"correct response", "from":"<actorid>"});
  441. do_check_eq(set.size, 0);
  442. });
  443. }).then(ret => {
  444. return rootFront.getManyChildren();
  445. }).then(ret => {
  446. trace.expectSend({"type":"getManyChildren", "to":"<actorid>"});
  447. trace.expectReceive({"foo":"bar", "child5":{"actor":"<actorid>", "childID":"child5"}, "more":[{"actor":"<actorid>", "childID":"child6"}, {"actor":"<actorid>", "childID":"child7"}], "from":"<actorid>"});
  448. // Check all the crazy stuff we did in getManyChildren
  449. do_check_eq(ret.foo, "bar");
  450. do_check_eq(ret.child5.childID, "child5");
  451. do_check_eq(ret.more[0].childID, "child6");
  452. do_check_eq(ret.more[1].childID, "child7");
  453. }).then(() => {
  454. // Test accepting a generator.
  455. let f = function* () {
  456. for (let i of [1, 2, 3, 4, 5]) {
  457. yield i;
  458. }
  459. };
  460. return childFront.getIntArray(f());
  461. }).then((ret) => {
  462. do_check_eq(ret.length, 5);
  463. let expected = [2, 4, 6, 8, 10];
  464. for (let i = 0; i < 5; ++i) {
  465. do_check_eq(ret[i], expected[i]);
  466. }
  467. }).then(() => {
  468. return rootFront.getChildren(["child1", "child2"]);
  469. }).then(ids => {
  470. let f = function* () {
  471. for (let id of ids) {
  472. yield id;
  473. }
  474. };
  475. return rootFront.getChildren2(f());
  476. }).then(ret => {
  477. do_check_eq(ret.length, 2);
  478. do_check_true(ret[0] === childFront);
  479. do_check_true(ret[1] !== childFront);
  480. do_check_true(ret[1] instanceof ChildFront);
  481. }).then(() => {
  482. client.close().then(() => {
  483. do_test_finished();
  484. });
  485. }).then(null, err => {
  486. do_report_unexpected_exception(err, "Failure executing test");
  487. });
  488. });
  489. do_test_pending();
  490. }