OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 library echo_apptests; | 5 library echo_apptests; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 | 8 |
9 import 'package:mojo_apptest/apptest.dart'; | 9 import 'package:mojo_apptest/apptest.dart'; |
10 import 'package:mojo/application.dart'; | 10 import 'package:mojo/application.dart'; |
11 import 'package:mojo/bindings.dart'; | 11 import 'package:mojo/bindings.dart'; |
12 import 'package:mojo/core.dart'; | 12 import 'package:mojo/core.dart'; |
13 import 'package:_mojo_for_test_only/test/echo_service.mojom.dart'; | 13 import 'package:_mojo_for_test_only/test/echo_service.mojom.dart'; |
14 | 14 |
15 echoApptests(Application application, String url) { | 15 echoApptests(Application application, String url) { |
16 group('Echo Service Apptests', () { | 16 group('Echo Service Apptests', () { |
17 test('String', () async { | 17 test('String', () async { |
18 var echoProxy = | 18 var echo = EchoService.connectToService(application, "mojo:dart_echo"); |
19 new EchoServiceProxy.connectToService(application, "mojo:dart_echo"); | |
20 | 19 |
21 var v = await echoProxy.echoString("foo"); | 20 var v = await echo.echoString("foo"); |
22 expect(v.value, equals("foo")); | 21 expect(v.value, equals("foo")); |
23 | 22 |
24 var q = await echoProxy.echoString("quit"); | 23 var q = await echo.echoString("quit"); |
25 expect(q.value, equals("quit")); | 24 expect(q.value, equals("quit")); |
26 | 25 |
27 await echoProxy.close(); | 26 await echo.close(); |
28 }); | 27 }); |
29 | 28 |
30 test('Empty String', () async { | 29 test('Empty String', () async { |
31 var echoProxy = | 30 var echo = EchoService.connectToService(application, "mojo:dart_echo"); |
32 new EchoServiceProxy.connectToService(application, "mojo:dart_echo"); | |
33 | 31 |
34 var v = await echoProxy.echoString(""); | 32 var v = await echo.echoString(""); |
35 expect(v.value, equals("")); | 33 expect(v.value, equals("")); |
36 | 34 |
37 var q = await echoProxy.echoString("quit"); | 35 var q = await echo.echoString("quit"); |
38 expect(q.value, equals("quit")); | 36 expect(q.value, equals("quit")); |
39 | 37 |
40 await echoProxy.close(); | 38 await echo.close(); |
41 }); | 39 }); |
42 | 40 |
43 test('Null String', () async { | 41 test('Null String', () async { |
44 var echoProxy = | 42 var echo = EchoService.connectToService(application, "mojo:dart_echo"); |
45 new EchoServiceProxy.connectToService(application, "mojo:dart_echo"); | |
46 | 43 |
47 var v = await echoProxy.echoString(null); | 44 var v = await echo.echoString(null); |
48 expect(v.value, equals(null)); | 45 expect(v.value, equals(null)); |
49 | 46 |
50 var q = await echoProxy.echoString("quit"); | 47 var q = await echo.echoString("quit"); |
51 expect(q.value, equals("quit")); | 48 expect(q.value, equals("quit")); |
52 | 49 |
53 await echoProxy.close(); | 50 await echo.close(); |
54 }); | 51 }); |
55 | 52 |
56 test('Delayed Success', () async { | 53 test('Delayed Success', () async { |
57 var echoProxy = | 54 var echo = EchoService.connectToService(application, "mojo:dart_echo"); |
58 new EchoServiceProxy.connectToService(application, "mojo:dart_echo"); | |
59 | 55 |
60 var milliseconds = 100; | 56 var milliseconds = 100; |
61 var watch = new Stopwatch()..start(); | 57 var watch = new Stopwatch()..start(); |
62 var v = await echoProxy.delayedEchoString("foo", milliseconds); | 58 var v = await echo.delayedEchoString("foo", milliseconds); |
63 var elapsed = watch.elapsedMilliseconds; | 59 var elapsed = watch.elapsedMilliseconds; |
64 expect(v.value, equals("foo")); | 60 expect(v.value, equals("foo")); |
65 expect(elapsed, greaterThanOrEqualTo(milliseconds)); | 61 expect(elapsed, greaterThanOrEqualTo(milliseconds)); |
66 | 62 |
67 var q = await echoProxy.echoString("quit"); | 63 var q = await echo.echoString("quit"); |
68 expect(q.value, equals("quit")); | 64 expect(q.value, equals("quit")); |
69 | 65 |
70 await echoProxy.close(); | 66 await echo.close(); |
71 }); | 67 }); |
72 | 68 |
73 test('Delayed Close', () { | 69 test('Delayed Close', () { |
74 var echoProxy = | 70 var echo = EchoService.connectToService(application, "mojo:dart_echo"); |
75 new EchoServiceProxy.connectToService(application, "mojo:dart_echo"); | |
76 | 71 |
77 var milliseconds = 100; | 72 var milliseconds = 100; |
78 echoProxy.responseOrError(echoProxy.delayedEchoString( | 73 echo.responseOrError(echo.delayedEchoString( |
79 "quit", milliseconds)).then((result) { | 74 "quit", milliseconds)).then((result) { |
80 fail('This future should not complete.'); | 75 fail('This future should not complete.'); |
81 }, onError: (e) { | 76 }, onError: (e) { |
82 expect(e is ProxyError, isTrue); | 77 expect(e is ProxyError, isTrue); |
83 }); | 78 }); |
84 | 79 |
85 return new Future.delayed( | 80 return new Future.delayed( |
86 new Duration(milliseconds: 10), () => echoProxy.close()); | 81 new Duration(milliseconds: 10), () => echo.close()); |
87 }); | 82 }); |
88 | 83 |
89 test('Swap', () async { | 84 test('Swap', () async { |
90 var echoProxy = | 85 var echo = EchoService.connectToService(application, "mojo:dart_echo"); |
91 new EchoServiceProxy.connectToService(application, "mojo:dart_echo"); | |
92 | 86 |
93 for (int i = 0; i < 10; i++) { | 87 for (int i = 0; i < 10; i++) { |
94 var v = | 88 var v = await echo.responseOrError(echo.echoString("foo")); |
95 await echoProxy.responseOrError(echoProxy.echoString("foo")); | |
96 expect(v.value, equals("foo")); | 89 expect(v.value, equals("foo")); |
97 } | 90 } |
98 | 91 |
99 echoProxy.ctrl.errorFuture.then((e) { | 92 echo.ctrl.errorFuture.then((e) { |
100 fail("echoProxy: $e"); | 93 fail("echo: $e"); |
101 }); | 94 }); |
102 | 95 |
103 // Trigger an implementation swap in the echo server. | 96 // Trigger an implementation swap in the echo server. |
104 echoProxy.swap(); | 97 echo.swap(); |
105 | 98 |
106 expect(echoProxy.ctrl.isBound, isTrue); | 99 expect(echo.ctrl.isBound, isTrue); |
107 | 100 |
108 for (int i = 0; i < 10; i++) { | 101 for (int i = 0; i < 10; i++) { |
109 var v = | 102 var v = await echo.responseOrError(echo.echoString("foo")); |
110 await echoProxy.responseOrError(echoProxy.echoString("foo")); | |
111 expect(v.value, equals("foo")); | 103 expect(v.value, equals("foo")); |
112 } | 104 } |
113 | 105 |
114 var q = await echoProxy.responseOrError(echoProxy.echoString("quit")); | 106 var q = await echo.responseOrError(echo.echoString("quit")); |
115 expect(q.value, equals("quit")); | 107 expect(q.value, equals("quit")); |
116 | 108 |
117 await echoProxy.close(); | 109 await echo.close(); |
118 }); | 110 }); |
119 | 111 |
120 test('Multiple Error Checks Success', () { | 112 test('Multiple Error Checks Success', () { |
121 var echoProxy = | 113 var echo = EchoService.connectToService(application, "mojo:dart_echo"); |
122 new EchoServiceProxy.connectToService(application, "mojo:dart_echo"); | |
123 | 114 |
124 List<Future> futures = []; | 115 List<Future> futures = []; |
125 for (int i = 0; i < 100; i++) { | 116 for (int i = 0; i < 100; i++) { |
126 var f = echoProxy.responseOrError(echoProxy.echoString("foo")) | 117 var f = echo.responseOrError(echo.echoString("foo")).then((r) { |
127 .then((r) { | |
128 expect(r.value, equals("foo")); | 118 expect(r.value, equals("foo")); |
129 }, onError: (e) { | 119 }, onError: (e) { |
130 fail('There should be no errors'); | 120 fail('There should be no errors'); |
131 }); | 121 }); |
132 futures.add(f); | 122 futures.add(f); |
133 } | 123 } |
134 return Future.wait(futures).whenComplete(() => echoProxy.close()); | 124 return Future.wait(futures).whenComplete(() => echo.close()); |
135 }); | 125 }); |
136 | 126 |
137 test('Multiple Error Checks Fail', () { | 127 test('Multiple Error Checks Fail', () { |
138 var echoProxy = | 128 var echo = EchoService.connectToService(application, "mojo:dart_echo"); |
139 new EchoServiceProxy.connectToService(application, "mojo:dart_echo"); | |
140 | 129 |
141 List<Future> futures = []; | 130 List<Future> futures = []; |
142 var milliseconds = 100; | 131 var milliseconds = 100; |
143 for (int i = 0; i < 100; i++) { | 132 for (int i = 0; i < 100; i++) { |
144 var f = echoProxy.responseOrError( | 133 var f = echo.responseOrError( |
145 echoProxy.delayedEchoString("foo", milliseconds)).then((_) { | 134 echo.delayedEchoString("foo", milliseconds)).then((_) { |
146 fail('This call should fail'); | 135 fail('This call should fail'); |
147 }, onError: (e) { | 136 }, onError: (e) { |
148 expect(e is ProxyError, isTrue); | 137 expect(e is ProxyError, isTrue); |
149 }); | 138 }); |
150 futures.add(f); | 139 futures.add(f); |
151 } | 140 } |
152 return echoProxy.close().then((_) => Future.wait(futures)); | 141 return echo.close().then((_) => Future.wait(futures)); |
153 }); | 142 }); |
154 | 143 |
155 test('Uncaught Call Closed', () async { | 144 test('Uncaught Call Closed', () async { |
156 var echoProxy = | 145 var echo = EchoService.connectToService(application, "mojo:dart_echo"); |
157 new EchoServiceProxy.connectToService(application, "mojo:dart_echo"); | |
158 | 146 |
159 // Do a normal call. | 147 // Do a normal call. |
160 var v = await echoProxy.echoString("foo"); | 148 var v = await echo.echoString("foo"); |
161 expect(v.value, equals("foo")); | 149 expect(v.value, equals("foo")); |
162 | 150 |
163 // Close the proxy. | 151 // Close the proxy. |
164 await echoProxy.close(); | 152 await echo.close(); |
165 | 153 |
166 // Try to do another call, which should not return. | 154 // Try to do another call, which should not return. |
167 echoProxy.echoString("foo").then((_) { | 155 echo.echoString("foo").then((_) { |
168 fail('This should be unreachable'); | 156 fail('This should be unreachable'); |
169 }); | 157 }); |
170 }); | 158 }); |
171 | 159 |
172 test('Catch Call Closed', () async { | 160 test('Catch Call Closed', () async { |
173 var echoProxy = | 161 var echo = EchoService.connectToService(application, "mojo:dart_echo"); |
174 new EchoServiceProxy.connectToService(application, "mojo:dart_echo"); | |
175 | 162 |
176 // Do a normal call. | 163 // Do a normal call. |
177 var v = await echoProxy.echoString("foo"); | 164 var v = await echo.echoString("foo"); |
178 expect(v.value, equals("foo")); | 165 expect(v.value, equals("foo")); |
179 | 166 |
180 // Close the proxy. | 167 // Close the proxy. |
181 await echoProxy.close(); | 168 await echo.close(); |
182 | 169 |
183 // Try to do another call, which should fail. | 170 // Try to do another call, which should fail. |
184 bool caughtException = false; | 171 bool caughtException = false; |
185 try { | 172 try { |
186 v = await echoProxy.responseOrError(echoProxy.echoString("foo")); | 173 v = await echo.responseOrError(echo.echoString("foo")); |
187 fail('This should be unreachable'); | 174 fail('This should be unreachable'); |
188 } on ProxyError catch (e) { | 175 } on ProxyError catch (e) { |
189 caughtException = true; | 176 caughtException = true; |
190 } | 177 } |
191 expect(caughtException, isTrue); | 178 expect(caughtException, isTrue); |
192 }); | 179 }); |
193 | 180 |
194 test('Catch Call Sequence Closed Twice', () async { | 181 test('Catch Call Sequence Closed Twice', () async { |
195 var echoProxy = | 182 var echo = EchoService.connectToService(application, "mojo:dart_echo"); |
196 new EchoServiceProxy.connectToService(application, "mojo:dart_echo"); | |
197 | 183 |
198 // Do a normal call. | 184 // Do a normal call. |
199 var v = await echoProxy.echoString("foo"); | 185 var v = await echo.echoString("foo"); |
200 expect(v.value, equals("foo")); | 186 expect(v.value, equals("foo")); |
201 | 187 |
202 // Close the proxy. | 188 // Close the proxy. |
203 await echoProxy.close(); | 189 await echo.close(); |
204 | 190 |
205 // Try to do another call, which should fail. | 191 // Try to do another call, which should fail. |
206 bool caughtException = false; | 192 bool caughtException = false; |
207 try { | 193 try { |
208 v = await echoProxy.responseOrError(echoProxy.echoString("foo")); | 194 v = await echo.responseOrError(echo.echoString("foo")); |
209 fail('This should be unreachable'); | 195 fail('This should be unreachable'); |
210 } on ProxyError catch (e) { | 196 } on ProxyError catch (e) { |
211 caughtException = true; | 197 caughtException = true; |
212 } | 198 } |
213 expect(caughtException, isTrue); | 199 expect(caughtException, isTrue); |
214 | 200 |
215 // Make sure we can catch an error more than once. | 201 // Make sure we can catch an error more than once. |
216 caughtException = false; | 202 caughtException = false; |
217 try { | 203 try { |
218 v = await echoProxy.responseOrError(echoProxy.echoString("foo")); | 204 v = await echo.responseOrError(echo.echoString("foo")); |
219 fail('This should be unreachable'); | 205 fail('This should be unreachable'); |
220 } on ProxyError catch (e) { | 206 } on ProxyError catch (e) { |
221 caughtException = true; | 207 caughtException = true; |
222 } | 208 } |
223 expect(caughtException, isTrue); | 209 expect(caughtException, isTrue); |
224 }); | 210 }); |
225 | 211 |
226 test('Catch Call Parallel Closed Twice', () async { | 212 test('Catch Call Parallel Closed Twice', () async { |
227 var echoProxy = | 213 var echo = EchoService.connectToService(application, "mojo:dart_echo"); |
228 new EchoServiceProxy.connectToService(application, "mojo:dart_echo"); | |
229 | 214 |
230 // Do a normal call. | 215 // Do a normal call. |
231 var v = await echoProxy.echoString("foo"); | 216 var v = await echo.echoString("foo"); |
232 expect(v.value, equals("foo")); | 217 expect(v.value, equals("foo")); |
233 | 218 |
234 // Close the proxy. | 219 // Close the proxy. |
235 await echoProxy.close(); | 220 await echo.close(); |
236 | 221 |
237 // Queue up two calls after the close, and make sure they both fail. | 222 // Queue up two calls after the close, and make sure they both fail. |
238 var f1 = echoProxy.responseOrError(echoProxy.echoString("foo")) | 223 var f1 = echo.responseOrError(echo.echoString("foo")).then((_) { |
239 .then((_) { | |
240 fail('This should be unreachable'); | 224 fail('This should be unreachable'); |
241 }, onError: (e) { | 225 }, onError: (e) { |
242 expect(e is ProxyError, isTrue); | 226 expect(e is ProxyError, isTrue); |
243 }); | 227 }); |
244 | 228 |
245 var f2 = echoProxy.responseOrError(echoProxy.echoString("foo")) | 229 var f2 = echo.responseOrError(echo.echoString("foo")).then((_) { |
246 .then((_) { | |
247 fail('This should be unreachable'); | 230 fail('This should be unreachable'); |
248 }, onError: (e) { | 231 }, onError: (e) { |
249 expect(e is ProxyError, isTrue); | 232 expect(e is ProxyError, isTrue); |
250 }); | 233 }); |
251 | 234 |
252 return Future.wait([f1, f2]); | 235 return Future.wait([f1, f2]); |
253 }); | 236 }); |
254 | 237 |
255 test('Unbind, close', () async { | 238 test('Unbind, close', () async { |
256 var echoProxy = | 239 var echo = EchoService.connectToService(application, "mojo:dart_echo"); |
257 new EchoServiceProxy.connectToService(application, "mojo:dart_echo"); | |
258 | 240 |
259 var r = await echoProxy.responseOrError(echoProxy.echoString("foo")); | 241 var r = await echo.responseOrError(echo.echoString("foo")); |
260 expect(r.value, equals("foo")); | 242 expect(r.value, equals("foo")); |
261 | 243 |
262 var endpoint = echoProxy.ctrl.unbind(); | 244 var endpoint = echo.ctrl.unbind(); |
263 await echoProxy.close(); | 245 await echo.close(); |
264 endpoint.close(); | 246 endpoint.close(); |
265 }); | 247 }); |
266 | 248 |
267 test('Unbind, rebind to same', () async { | 249 test('Unbind, rebind to same', () async { |
268 var echoProxy = | 250 var echo = EchoService.connectToService(application, "mojo:dart_echo"); |
269 new EchoServiceProxy.connectToService(application, "mojo:dart_echo"); | |
270 | 251 |
271 var r = await echoProxy.responseOrError(echoProxy.echoString("foo")); | 252 var r = await echo.responseOrError(echo.echoString("foo")); |
272 expect(r.value, equals("foo")); | 253 expect(r.value, equals("foo")); |
273 | 254 |
274 var endpoint = echoProxy.ctrl.unbind(); | 255 var endpoint = echo.ctrl.unbind(); |
275 echoProxy.ctrl.bind(endpoint); | 256 echo.ctrl.bind(endpoint); |
276 | 257 |
277 r = await echoProxy.responseOrError(echoProxy.echoString("foo")); | 258 r = await echo.responseOrError(echo.echoString("foo")); |
278 expect(r.value, equals("foo")); | 259 expect(r.value, equals("foo")); |
279 | 260 |
280 await echoProxy.close(); | 261 await echo.close(); |
281 }); | 262 }); |
282 | 263 |
283 test('Unbind, rebind to different', () async { | 264 test('Unbind, rebind to different', () async { |
284 var echoProxy = | 265 var echo = EchoService.connectToService(application, "mojo:dart_echo"); |
285 new EchoServiceProxy.connectToService(application, "mojo:dart_echo"); | |
286 | 266 |
287 var r = await echoProxy.responseOrError(echoProxy.echoString("foo")); | 267 var r = await echo.responseOrError(echo.echoString("foo")); |
288 expect(r.value, equals("foo")); | 268 expect(r.value, equals("foo")); |
289 | 269 |
290 var endpoint = echoProxy.ctrl.unbind(); | 270 var endpoint = echo.ctrl.unbind(); |
291 var differentEchoProxy = new EchoServiceProxy.fromEndpoint(endpoint); | 271 var differentEchoProxy = new EchoServiceProxy.fromEndpoint(endpoint); |
292 | 272 |
293 r = await differentEchoProxy.responseOrError( | 273 r = await differentEchoProxy.responseOrError( |
294 differentEchoProxy.echoString("foo")); | 274 differentEchoProxy.echoString("foo")); |
295 expect(r.value, equals("foo")); | 275 expect(r.value, equals("foo")); |
296 | 276 |
297 await differentEchoProxy.close(); | 277 await differentEchoProxy.close(); |
298 }); | 278 }); |
299 | 279 |
300 test('Unbind, rebind to different, close original', () async { | 280 test('Unbind, rebind to different, close original', () async { |
301 var echoProxy = | 281 var echo = EchoService.connectToService(application, "mojo:dart_echo"); |
302 new EchoServiceProxy.connectToService(application, "mojo:dart_echo"); | |
303 | 282 |
304 var r = await echoProxy.responseOrError(echoProxy.echoString("foo")); | 283 var r = await echo.responseOrError(echo.echoString("foo")); |
305 expect(r.value, equals("foo")); | 284 expect(r.value, equals("foo")); |
306 | 285 |
307 var endpoint = echoProxy.ctrl.unbind(); | 286 var endpoint = echo.ctrl.unbind(); |
308 var differentEchoProxy = new EchoServiceProxy.fromEndpoint(endpoint); | 287 var differentEchoProxy = new EchoServiceProxy.fromEndpoint(endpoint); |
309 await echoProxy.close(); | 288 await echo.close(); |
310 | 289 |
311 r = await differentEchoProxy.responseOrError( | 290 r = await differentEchoProxy.responseOrError( |
312 differentEchoProxy.echoString("foo")); | 291 differentEchoProxy.echoString("foo")); |
313 expect(r.value, equals("foo")); | 292 expect(r.value, equals("foo")); |
314 | 293 |
315 await differentEchoProxy.close(); | 294 await differentEchoProxy.close(); |
316 }); | 295 }); |
317 }); | 296 }); |
318 } | 297 } |
OLD | NEW |