| 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 library test.runner.browser.suite; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 | |
| 9 import 'package:async/async.dart'; | |
| 10 | |
| 11 import '../../backend/group.dart'; | |
| 12 import '../../backend/metadata.dart'; | |
| 13 import '../../backend/test.dart'; | |
| 14 import '../../backend/test_platform.dart'; | |
| 15 import '../../util/multi_channel.dart'; | |
| 16 import '../../util/remote_exception.dart'; | |
| 17 import '../../util/stack_trace_mapper.dart'; | |
| 18 import '../../util/stream_channel.dart'; | |
| 19 import '../../utils.dart'; | |
| 20 import '../environment.dart'; | |
| 21 import '../load_exception.dart'; | |
| 22 import '../runner_suite.dart'; | |
| 23 import 'iframe_test.dart'; | |
| 24 | |
| 25 /// Loads a [RunnerSuite] for a browser. | |
| 26 /// | |
| 27 /// [channel] should connect to the iframe containing the suite, which should | |
| 28 /// eventually emit a message containing the suite's test information. | |
| 29 /// [environment], [path], [platform], and [onClose] are passed to the | |
| 30 /// [RunnerSuite]. If passed, [mapper] is used to reformat the test's stack | |
| 31 /// traces. | |
| 32 Future<RunnerSuite> loadBrowserSuite(StreamChannel channel, | |
| 33 Environment environment, String path, {StackTraceMapper mapper, | |
| 34 TestPlatform platform, AsyncFunction onClose}) async { | |
| 35 // The controller for the returned suite. This is set once we've loaded the | |
| 36 // information about the tests in the suite. | |
| 37 var controller; | |
| 38 | |
| 39 // A timer that's reset whenever we receive a message from the browser. | |
| 40 // Because the browser stops running code when the user is actively debugging, | |
| 41 // this lets us detect whether they're debugging reasonably accurately. | |
| 42 // | |
| 43 // The duration should be short enough that the debugging console is open as | |
| 44 // soon as the user is done setting breakpoints, but long enough that a test | |
| 45 // doing a lot of synchronous work doesn't trigger a false positive. | |
| 46 // | |
| 47 // Start this canceled because we don't want it to start ticking until we get | |
| 48 // some response from the iframe. | |
| 49 var timer = new RestartableTimer(new Duration(seconds: 3), () { | |
| 50 controller.setDebugging(true); | |
| 51 })..cancel(); | |
| 52 | |
| 53 // Even though [channel] is probably a [MultiChannel] already, create a | |
| 54 // nested MultiChannel because the iframe will be using a channel wrapped | |
| 55 // within the host's channel. | |
| 56 var suiteChannel = new MultiChannel(channel.stream.map((message) { | |
| 57 // Whenever we get a message, no matter which child channel it's for, we the | |
| 58 // browser is still running code which means the using isn't debugging. | |
| 59 if (controller != null) { | |
| 60 timer.reset(); | |
| 61 controller.setDebugging(false); | |
| 62 } | |
| 63 | |
| 64 return message; | |
| 65 }), channel.sink); | |
| 66 | |
| 67 var response = await _getResponse(suiteChannel.stream) | |
| 68 .timeout(new Duration(minutes: 1), onTimeout: () { | |
| 69 suiteChannel.sink.close(); | |
| 70 throw new LoadException( | |
| 71 path, | |
| 72 "Timed out waiting for the test suite to connect."); | |
| 73 }); | |
| 74 | |
| 75 try { | |
| 76 _validateResponse(path, response); | |
| 77 } catch (_) { | |
| 78 suiteChannel.sink.close(); | |
| 79 rethrow; | |
| 80 } | |
| 81 | |
| 82 controller = new RunnerSuiteController(environment, | |
| 83 _deserializeGroup(suiteChannel, response["root"], mapper), | |
| 84 platform: platform, path: path, | |
| 85 onClose: () { | |
| 86 suiteChannel.sink.close(); | |
| 87 timer.cancel(); | |
| 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 |