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