OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import 'dart:async'; | |
6 | |
7 import 'package:async/async.dart'; | |
8 import 'package:stream_channel/stream_channel.dart'; | |
9 | |
10 import '../../backend/group.dart'; | |
11 import '../../backend/metadata.dart'; | |
12 import '../../backend/test.dart'; | |
13 import '../../backend/test_platform.dart'; | |
14 import '../../util/remote_exception.dart'; | |
15 import '../../util/stack_trace_mapper.dart'; | |
16 import '../../utils.dart'; | |
17 import '../environment.dart'; | |
18 import '../load_exception.dart'; | |
19 import '../runner_suite.dart'; | |
20 import 'iframe_test.dart'; | |
21 | |
22 /// Loads a [RunnerSuite] for a browser. | |
23 /// | |
24 /// [channel] should connect to the iframe containing the suite, which should | |
25 /// eventually emit a message containing the suite's test information. | |
26 /// [environment], [path], [platform], and [onClose] are passed to the | |
27 /// [RunnerSuite]. If passed, [mapper] is used to reformat the test's stack | |
28 /// traces. | |
29 Future<RunnerSuite> loadBrowserSuite(StreamChannel channel, | |
30 Environment environment, String path, {StackTraceMapper mapper, | |
31 TestPlatform platform, AsyncFunction onClose}) async { | |
32 // The controller for the returned suite. This is set once we've loaded the | |
33 // information about the tests in the suite. | |
34 var controller; | |
35 | |
36 // A timer that's reset whenever we receive a message from the browser. | |
37 // Because the browser stops running code when the user is actively debugging, | |
38 // this lets us detect whether they're debugging reasonably accurately. | |
39 // | |
40 // The duration should be short enough that the debugging console is open as | |
41 // soon as the user is done setting breakpoints, but long enough that a test | |
42 // doing a lot of synchronous work doesn't trigger a false positive. | |
43 // | |
44 // Start this canceled because we don't want it to start ticking until we get | |
45 // some response from the iframe. | |
46 var timer = new RestartableTimer(new Duration(seconds: 3), () { | |
47 controller.setDebugging(true); | |
48 })..cancel(); | |
49 | |
50 // Even though [channel] is probably a [MultiChannel] already, create a | |
51 // nested MultiChannel because the iframe will be using a channel wrapped | |
52 // within the host's channel. | |
53 var suiteChannel = new MultiChannel(channel.changeStream((stream) { | |
54 return stream.map((message) { | |
55 // Whenever we get a message, no matter which child channel it's for, we t
he | |
56 // browser is still running code which means the using isn't debugging. | |
57 if (controller != null) { | |
58 timer.reset(); | |
59 controller.setDebugging(false); | |
60 } | |
61 | |
62 return message; | |
63 }); | |
64 })); | |
65 | |
66 var response = await _getResponse(suiteChannel.stream) | |
67 .timeout(new Duration(minutes: 1), onTimeout: () { | |
68 suiteChannel.sink.close(); | |
69 throw new LoadException( | |
70 path, | |
71 "Timed out waiting for the test suite to connect."); | |
72 }); | |
73 | |
74 try { | |
75 _validateResponse(path, response); | |
76 } catch (_) { | |
77 suiteChannel.sink.close(); | |
78 rethrow; | |
79 } | |
80 | |
81 controller = new RunnerSuiteController(environment, | |
82 _deserializeGroup(suiteChannel, response["root"], mapper), | |
83 platform: platform, path: path, | |
84 onClose: () { | |
85 suiteChannel.sink.close(); | |
86 timer.cancel(); | |
87 controller = null; | |
88 return onClose == null ? null : onClose(); | |
89 }); | |
90 | |
91 // Start the debugging timer counting down. | |
92 timer.reset(); | |
93 return controller.suite; | |
94 } | |
95 | |
96 /// Listens for responses from the iframe on [stream]. | |
97 /// | |
98 /// Returns the serialized representation of the the root group for the suite, | |
99 /// or a response indicating that an error occurred. | |
100 Future<Map> _getResponse(Stream stream) { | |
101 var completer = new Completer(); | |
102 stream.listen((response) { | |
103 if (response["type"] == "print") { | |
104 print(response["line"]); | |
105 } else if (response["type"] != "ping") { | |
106 completer.complete(response); | |
107 } | |
108 }, onDone: () { | |
109 if (!completer.isCompleted) completer.complete(); | |
110 }); | |
111 | |
112 return completer.future; | |
113 } | |
114 | |
115 /// Throws an error encoded in [response], if there is one. | |
116 /// | |
117 /// [path] is used for the error's metadata. | |
118 Future _validateResponse(String path, Map response) { | |
119 if (response == null) { | |
120 throw new LoadException( | |
121 path, "Connection closed before test suite loaded."); | |
122 } | |
123 | |
124 if (response["type"] == "loadException") { | |
125 throw new LoadException(path, response["message"]); | |
126 } | |
127 | |
128 if (response["type"] == "error") { | |
129 var asyncError = RemoteException.deserialize(response["error"]); | |
130 return new Future.error( | |
131 new LoadException(path, asyncError.error), | |
132 asyncError.stackTrace); | |
133 } | |
134 | |
135 return new Future.value(); | |
136 } | |
137 | |
138 /// Deserializes [group] into a concrete [Group] class. | |
139 Group _deserializeGroup(MultiChannel suiteChannel, Map group, | |
140 [StackTraceMapper mapper]) { | |
141 var metadata = new Metadata.deserialize(group['metadata']); | |
142 return new Group(group['name'], group['entries'].map((entry) { | |
143 if (entry['type'] == 'group') { | |
144 return _deserializeGroup(suiteChannel, entry, mapper); | |
145 } | |
146 | |
147 return _deserializeTest(suiteChannel, entry, mapper); | |
148 }), | |
149 metadata: metadata, | |
150 setUpAll: _deserializeTest(suiteChannel, group['setUpAll'], mapper), | |
151 tearDownAll: | |
152 _deserializeTest(suiteChannel, group['tearDownAll'], mapper)); | |
153 } | |
154 | |
155 /// Deserializes [test] into a concrete [Test] class. | |
156 /// | |
157 /// Returns `null` if [test] is `null`. | |
158 Test _deserializeTest(MultiChannel suiteChannel, Map test, | |
159 [StackTraceMapper mapper]) { | |
160 if (test == null) return null; | |
161 | |
162 var metadata = new Metadata.deserialize(test['metadata']); | |
163 var testChannel = suiteChannel.virtualChannel(test['channel']); | |
164 return new IframeTest(test['name'], metadata, testChannel, | |
165 mapper: mapper); | |
166 } | |
OLD | NEW |