| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library test.runner.browser.browser_manager; | 5 library test.runner.browser.browser_manager; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 | 9 |
| 10 import 'package:http_parser/http_parser.dart'; | 10 import 'package:http_parser/http_parser.dart'; |
| 11 import 'package:pool/pool.dart'; | 11 import 'package:pool/pool.dart'; |
| 12 | 12 |
| 13 import '../../backend/metadata.dart'; | 13 import '../../backend/metadata.dart'; |
| 14 import '../../backend/test_platform.dart'; | 14 import '../../backend/test_platform.dart'; |
| 15 import '../../util/cancelable_future.dart'; |
| 15 import '../../util/multi_channel.dart'; | 16 import '../../util/multi_channel.dart'; |
| 16 import '../../util/remote_exception.dart'; | 17 import '../../util/remote_exception.dart'; |
| 17 import '../../util/stack_trace_mapper.dart'; | 18 import '../../util/stack_trace_mapper.dart'; |
| 18 import '../../utils.dart'; | 19 import '../../utils.dart'; |
| 19 import '../environment.dart'; | 20 import '../environment.dart'; |
| 20 import '../load_exception.dart'; | 21 import '../load_exception.dart'; |
| 21 import '../runner_suite.dart'; | 22 import '../runner_suite.dart'; |
| 22 import 'iframe_test.dart'; | 23 import 'iframe_test.dart'; |
| 23 | 24 |
| 24 /// A class that manages the connection to a single running browser. | 25 /// A class that manages the connection to a single running browser. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 45 | 46 |
| 46 /// The ID of the next suite to be loaded. | 47 /// The ID of the next suite to be loaded. |
| 47 /// | 48 /// |
| 48 /// This is used to ensure that the suites can be referred to consistently | 49 /// This is used to ensure that the suites can be referred to consistently |
| 49 /// across the client and server. | 50 /// across the client and server. |
| 50 int _suiteId = 0; | 51 int _suiteId = 0; |
| 51 | 52 |
| 52 /// Whether the channel to the browser has closed. | 53 /// Whether the channel to the browser has closed. |
| 53 bool _closed = false; | 54 bool _closed = false; |
| 54 | 55 |
| 56 /// The completer for [_BrowserEnvironment.displayPause]. |
| 57 /// |
| 58 /// This will be `null` as long as the browser isn't displaying a pause |
| 59 /// screen. |
| 60 CancelableCompleter _pauseCompleter; |
| 61 |
| 55 /// The environment to attach to each suite. | 62 /// The environment to attach to each suite. |
| 56 _BrowserEnvironment _environment; | 63 _BrowserEnvironment _environment; |
| 57 | 64 |
| 58 /// Creates a new BrowserManager that communicates with [browser] over | 65 /// Creates a new BrowserManager that communicates with [browser] over |
| 59 /// [webSocket]. | 66 /// [webSocket]. |
| 60 BrowserManager(this.browser, CompatibleWebSocket webSocket) | 67 BrowserManager(this.browser, CompatibleWebSocket webSocket) |
| 61 : _channel = new MultiChannel( | 68 : _channel = new MultiChannel( |
| 62 webSocket.map(JSON.decode), | 69 webSocket.map(JSON.decode), |
| 63 mapSink(webSocket, JSON.encode)) { | 70 mapSink(webSocket, JSON.encode)) { |
| 64 _environment = new _BrowserEnvironment(this); | 71 _environment = new _BrowserEnvironment(this); |
| 65 _channel.stream.listen(null, onDone: () => _closed = true); | 72 _channel.stream.listen(_onMessage, onDone: _onDone); |
| 66 } | 73 } |
| 67 | 74 |
| 68 /// Tells the browser the load a test suite from the URL [url]. | 75 /// Tells the browser the load a test suite from the URL [url]. |
| 69 /// | 76 /// |
| 70 /// [url] should be an HTML page with a reference to the JS-compiled test | 77 /// [url] should be an HTML page with a reference to the JS-compiled test |
| 71 /// suite. [path] is the path of the original test suite file, which is used | 78 /// suite. [path] is the path of the original test suite file, which is used |
| 72 /// for reporting. [metadata] is the parsed metadata for the test suite. | 79 /// for reporting. [metadata] is the parsed metadata for the test suite. |
| 73 /// | 80 /// |
| 74 /// If [mapper] is passed, it's used to map stack traces for errors coming | 81 /// If [mapper] is passed, it's used to map stack traces for errors coming |
| 75 /// from this test suite. | 82 /// from this test suite. |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 } | 153 } |
| 147 | 154 |
| 148 return new RunnerSuite(_environment, response["tests"].map((test) { | 155 return new RunnerSuite(_environment, response["tests"].map((test) { |
| 149 var testMetadata = new Metadata.deserialize(test['metadata']); | 156 var testMetadata = new Metadata.deserialize(test['metadata']); |
| 150 var testChannel = suiteChannel.virtualChannel(test['channel']); | 157 var testChannel = suiteChannel.virtualChannel(test['channel']); |
| 151 return new IframeTest(test['name'], testMetadata, testChannel, | 158 return new IframeTest(test['name'], testMetadata, testChannel, |
| 152 mapper: mapper); | 159 mapper: mapper); |
| 153 }), platform: browser, metadata: metadata, path: path, | 160 }), platform: browser, metadata: metadata, path: path, |
| 154 onClose: () => closeIframe()); | 161 onClose: () => closeIframe()); |
| 155 } | 162 } |
| 163 |
| 164 /// An implementation of [Environment.displayPause]. |
| 165 CancelableFuture _displayPause() { |
| 166 if (_pauseCompleter != null) return _pauseCompleter.future; |
| 167 |
| 168 _pauseCompleter = new CancelableCompleter(() { |
| 169 _channel.sink.add({"command": "resume"}); |
| 170 _pauseCompleter = null; |
| 171 }); |
| 172 |
| 173 _channel.sink.add({"command": "displayPause"}); |
| 174 return _pauseCompleter.future.whenComplete(() { |
| 175 _pauseCompleter = null; |
| 176 }); |
| 177 } |
| 178 |
| 179 /// The callback for handling messages received from the host page. |
| 180 void _onMessage(Map message) { |
| 181 assert(message["command"] == "resume"); |
| 182 if (_pauseCompleter == null) return; |
| 183 _pauseCompleter.complete(); |
| 184 } |
| 185 |
| 186 /// The callback called when the WebSocket is closed. |
| 187 void _onDone() { |
| 188 _closed = true; |
| 189 if (_pauseCompleter != null) _pauseCompleter.complete(); |
| 190 _pauseCompleter = null; |
| 191 } |
| 156 } | 192 } |
| 157 | 193 |
| 158 /// An implementation of [Environment] for the browser. | 194 /// An implementation of [Environment] for the browser. |
| 159 /// | 195 /// |
| 160 /// All methods forward directly to [BrowserManager]. | 196 /// All methods forward directly to [BrowserManager]. |
| 161 class _BrowserEnvironment implements Environment { | 197 class _BrowserEnvironment implements Environment { |
| 162 final BrowserManager _manager; | 198 final BrowserManager _manager; |
| 163 | 199 |
| 164 _BrowserEnvironment(this._manager); | 200 _BrowserEnvironment(this._manager); |
| 201 |
| 202 CancelableFuture displayPause() => _manager._displayPause(); |
| 165 } | 203 } |
| OLD | NEW |