toolkit/devtools/server/tests/unit/test_protocol_longstring.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 let protocol = devtools.require("devtools/server/protocol");
michael@0 8 let {method, RetVal, Arg, Option} = protocol;
michael@0 9 let events = devtools.require("sdk/event/core");
michael@0 10 let {LongStringActor} = devtools.require("devtools/server/actors/string");
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 DebuggerServer.LONG_STRING_LENGTH = DebuggerServer.LONG_STRING_INITIAL_LENGTH = DebuggerServer.LONG_STRING_READ_LENGTH = 5;
michael@0 21
michael@0 22 let SHORT_STR = "abc";
michael@0 23 let LONG_STR = "abcdefghijklmnop";
michael@0 24
michael@0 25 let rootActor = null;
michael@0 26
michael@0 27 let RootActor = protocol.ActorClass({
michael@0 28 typeName: "root",
michael@0 29
michael@0 30 initialize: function(conn) {
michael@0 31 rootActor = this;
michael@0 32 protocol.Actor.prototype.initialize.call(this, conn);
michael@0 33 // Root actor owns itself.
michael@0 34 this.manage(this);
michael@0 35 this.actorID = "root";
michael@0 36 },
michael@0 37
michael@0 38 sayHello: simpleHello,
michael@0 39
michael@0 40 shortString: method(function() {
michael@0 41 return new LongStringActor(this.conn, SHORT_STR);
michael@0 42 }, {
michael@0 43 response: { value: RetVal("longstring") },
michael@0 44 }),
michael@0 45
michael@0 46 longString: method(function() {
michael@0 47 return new LongStringActor(this.conn, LONG_STR);
michael@0 48 }, {
michael@0 49 response: { value: RetVal("longstring") },
michael@0 50 }),
michael@0 51
michael@0 52 emitShortString: method(function() {
michael@0 53 events.emit(this, "string-event", new LongStringActor(this.conn, SHORT_STR));
michael@0 54 }, {
michael@0 55 oneway: true,
michael@0 56 }),
michael@0 57
michael@0 58 emitLongString: method(function() {
michael@0 59 events.emit(this, "string-event", new LongStringActor(this.conn, LONG_STR));
michael@0 60 }, {
michael@0 61 oneway: true,
michael@0 62 }),
michael@0 63
michael@0 64 events: {
michael@0 65 "string-event": {
michael@0 66 str: Arg(0, "longstring")
michael@0 67 }
michael@0 68 }
michael@0 69 });
michael@0 70
michael@0 71 let RootFront = protocol.FrontClass(RootActor, {
michael@0 72 initialize: function(client) {
michael@0 73 this.actorID = "root";
michael@0 74 protocol.Front.prototype.initialize.call(this, client);
michael@0 75 // Root owns itself.
michael@0 76 this.manage(this);
michael@0 77 }
michael@0 78 });
michael@0 79
michael@0 80 function run_test()
michael@0 81 {
michael@0 82 DebuggerServer.createRootActor = (conn => {
michael@0 83 return RootActor(conn);
michael@0 84 });
michael@0 85
michael@0 86 DebuggerServer.init(() => true);
michael@0 87 let trace = connectPipeTracing();
michael@0 88 let client = new DebuggerClient(trace);
michael@0 89 let rootClient;
michael@0 90
michael@0 91 let strfront = null;
michael@0 92
michael@0 93 let expectRootChildren = function(size) {
michael@0 94 do_check_eq(rootActor.__poolMap.size, size + 1);
michael@0 95 do_check_eq(rootClient.__poolMap.size, size + 1);
michael@0 96 }
michael@0 97
michael@0 98
michael@0 99 client.connect((applicationType, traits) => {
michael@0 100 rootClient = RootFront(client);
michael@0 101
michael@0 102 // Root actor has no children yet.
michael@0 103 expectRootChildren(0);
michael@0 104
michael@0 105 trace.expectReceive({"from":"<actorid>","applicationType":"xpcshell-tests","traits":[]});
michael@0 106 do_check_eq(applicationType, "xpcshell-tests");
michael@0 107 rootClient.shortString().then(ret => {
michael@0 108 trace.expectSend({"type":"shortString","to":"<actorid>"});
michael@0 109 trace.expectReceive({"value":"abc","from":"<actorid>"});
michael@0 110
michael@0 111 // Should only own the one reference (itself) at this point.
michael@0 112 expectRootChildren(0);
michael@0 113 strfront = ret;
michael@0 114 }).then(() => {
michael@0 115 return strfront.string();
michael@0 116 }).then(ret => {
michael@0 117 do_check_eq(ret, SHORT_STR);
michael@0 118 }).then(() => {
michael@0 119 return rootClient.longString();
michael@0 120 }).then(ret => {
michael@0 121 trace.expectSend({"type":"longString","to":"<actorid>"});
michael@0 122 trace.expectReceive({"value":{"type":"longString","actor":"<actorid>","length":16,"initial":"abcde"},"from":"<actorid>"});
michael@0 123
michael@0 124 strfront = ret;
michael@0 125 // Should own a reference to itself and an extra string now.
michael@0 126 expectRootChildren(1);
michael@0 127 }).then(() => {
michael@0 128 return strfront.string();
michael@0 129 }).then(ret => {
michael@0 130 trace.expectSend({"type":"substring","start":5,"end":10,"to":"<actorid>"});
michael@0 131 trace.expectReceive({"substring":"fghij","from":"<actorid>"});
michael@0 132 trace.expectSend({"type":"substring","start":10,"end":15,"to":"<actorid>"});
michael@0 133 trace.expectReceive({"substring":"klmno","from":"<actorid>"});
michael@0 134 trace.expectSend({"type":"substring","start":15,"end":20,"to":"<actorid>"});
michael@0 135 trace.expectReceive({"substring":"p","from":"<actorid>"});
michael@0 136
michael@0 137 do_check_eq(ret, LONG_STR);
michael@0 138 }).then(() => {
michael@0 139 return strfront.release();
michael@0 140 }).then(() => {
michael@0 141 trace.expectSend({"type":"release","to":"<actorid>"});
michael@0 142 trace.expectReceive({"from":"<actorid>"});
michael@0 143
michael@0 144 // That reference should be removed now.
michael@0 145 expectRootChildren(0);
michael@0 146 }).then(() => {
michael@0 147 let deferred = promise.defer();
michael@0 148 rootClient.once("string-event", (str) => {
michael@0 149 trace.expectSend({"type":"emitShortString","to":"<actorid>"});
michael@0 150 trace.expectReceive({"type":"string-event","str":"abc","from":"<actorid>"});
michael@0 151
michael@0 152 do_check_true(!!str);
michael@0 153 strfront = str;
michael@0 154 // Shouldn't generate any new references
michael@0 155 expectRootChildren(0);
michael@0 156 // will generate no packets.
michael@0 157 strfront.string().then((value) => { deferred.resolve(value) });
michael@0 158 });
michael@0 159 rootClient.emitShortString();
michael@0 160 return deferred.promise;
michael@0 161 }).then(value => {
michael@0 162 do_check_eq(value, SHORT_STR);
michael@0 163 }).then(() => {
michael@0 164 // Will generate no packets
michael@0 165 return strfront.release();
michael@0 166 }).then(() => {
michael@0 167 let deferred = promise.defer();
michael@0 168 rootClient.once("string-event", (str) => {
michael@0 169 trace.expectSend({"type":"emitLongString","to":"<actorid>"});
michael@0 170 trace.expectReceive({"type":"string-event","str":{"type":"longString","actor":"<actorid>","length":16,"initial":"abcde"},"from":"<actorid>"});
michael@0 171
michael@0 172 do_check_true(!!str);
michael@0 173 // Should generate one new reference
michael@0 174 expectRootChildren(1);
michael@0 175 strfront = str;
michael@0 176 strfront.string().then((value) => {
michael@0 177 trace.expectSend({"type":"substring","start":5,"end":10,"to":"<actorid>"});
michael@0 178 trace.expectReceive({"substring":"fghij","from":"<actorid>"});
michael@0 179 trace.expectSend({"type":"substring","start":10,"end":15,"to":"<actorid>"});
michael@0 180 trace.expectReceive({"substring":"klmno","from":"<actorid>"});
michael@0 181 trace.expectSend({"type":"substring","start":15,"end":20,"to":"<actorid>"});
michael@0 182 trace.expectReceive({"substring":"p","from":"<actorid>"});
michael@0 183
michael@0 184 deferred.resolve(value);
michael@0 185 });
michael@0 186 });
michael@0 187 rootClient.emitLongString();
michael@0 188 return deferred.promise;
michael@0 189 }).then(value => {
michael@0 190 do_check_eq(value, LONG_STR);
michael@0 191 }).then(() => {
michael@0 192 return strfront.release();
michael@0 193 }).then(() => {
michael@0 194 trace.expectSend({"type":"release","to":"<actorid>"});
michael@0 195 trace.expectReceive({"from":"<actorid>"});
michael@0 196 expectRootChildren(0);
michael@0 197 }).then(() => {
michael@0 198 client.close(() => {
michael@0 199 do_test_finished();
michael@0 200 });
michael@0 201 }).then(null, err => {
michael@0 202 do_report_unexpected_exception(err, "Failure executing test");
michael@0 203 });
michael@0 204 });
michael@0 205 do_test_pending();
michael@0 206 }

mercurial