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 unittest.runner.browser.server; | 5 library unittest.runner.browser.server; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 | 10 |
(...skipping 12 matching lines...) Expand all Loading... |
23 | 23 |
24 /// A server that serves JS-compiled tests to browsers. | 24 /// A server that serves JS-compiled tests to browsers. |
25 /// | 25 /// |
26 /// A test suite may be loaded for a given file using [loadSuite]. | 26 /// A test suite may be loaded for a given file using [loadSuite]. |
27 class BrowserServer { | 27 class BrowserServer { |
28 /// Starts the server. | 28 /// Starts the server. |
29 /// | 29 /// |
30 /// If [packageRoot] is passed, it's used for all package imports when | 30 /// If [packageRoot] is passed, it's used for all package imports when |
31 /// compiling tests to JS. Otherwise, the package root is inferred from the | 31 /// compiling tests to JS. Otherwise, the package root is inferred from the |
32 /// location of the source file. | 32 /// location of the source file. |
33 static Future<BrowserServer> start({String packageRoot}) { | 33 /// |
34 var server = new BrowserServer._(packageRoot); | 34 /// If [color] is true, console colors will be used when compiling Dart. |
| 35 static Future<BrowserServer> start({String packageRoot, bool color: false}) { |
| 36 var server = new BrowserServer._(packageRoot, color); |
35 return server._load().then((_) => server); | 37 return server._load().then((_) => server); |
36 } | 38 } |
37 | 39 |
38 /// The underlying HTTP server. | 40 /// The underlying HTTP server. |
39 HttpServer _server; | 41 HttpServer _server; |
40 | 42 |
41 /// The URL for this server. | 43 /// The URL for this server. |
42 Uri get url => baseUrlForAddress(_server.address, _server.port); | 44 Uri get url => baseUrlForAddress(_server.address, _server.port); |
43 | 45 |
44 /// a [OneOffHandler] for servicing WebSocket connections for | 46 /// a [OneOffHandler] for servicing WebSocket connections for |
45 /// [BrowserManager]s. | 47 /// [BrowserManager]s. |
46 /// | 48 /// |
47 /// This is one-off because each [BrowserManager] can only connect to a single | 49 /// This is one-off because each [BrowserManager] can only connect to a single |
48 /// WebSocket, | 50 /// WebSocket, |
49 final _webSocketHandler = new OneOffHandler(); | 51 final _webSocketHandler = new OneOffHandler(); |
50 | 52 |
51 /// The [CompilerPool] managing active instances of `dart2js`. | 53 /// The [CompilerPool] managing active instances of `dart2js`. |
52 final _compilers = new CompilerPool(); | 54 final CompilerPool _compilers; |
53 | 55 |
54 /// The temporary directory in which compiled JS is emitted. | 56 /// The temporary directory in which compiled JS is emitted. |
55 final String _compiledDir; | 57 final String _compiledDir; |
56 | 58 |
57 /// The package root which is passed to `dart2js`. | 59 /// The package root which is passed to `dart2js`. |
58 final String _packageRoot; | 60 final String _packageRoot; |
59 | 61 |
60 /// The browser in which test suites are loaded and run. | 62 /// The browser in which test suites are loaded and run. |
61 /// | 63 /// |
62 /// This is `null` until a suite is loaded. | 64 /// This is `null` until a suite is loaded. |
(...skipping 19 matching lines...) Expand all Loading... |
82 // tests complete. | 84 // tests complete. |
83 _browser.onExit.catchError((error, stackTrace) { | 85 _browser.onExit.catchError((error, stackTrace) { |
84 if (_browserManagerCompleter.isCompleted) return; | 86 if (_browserManagerCompleter.isCompleted) return; |
85 _browserManagerCompleter.completeError(error, stackTrace); | 87 _browserManagerCompleter.completeError(error, stackTrace); |
86 }); | 88 }); |
87 } | 89 } |
88 return _browserManagerCompleter.future; | 90 return _browserManagerCompleter.future; |
89 } | 91 } |
90 Completer<BrowserManager> _browserManagerCompleter; | 92 Completer<BrowserManager> _browserManagerCompleter; |
91 | 93 |
92 BrowserServer._(this._packageRoot) | 94 BrowserServer._(this._packageRoot, bool color) |
93 : _compiledDir = Directory.systemTemp.createTempSync('unittest_').path; | 95 : _compiledDir = Directory.systemTemp.createTempSync('unittest_').path, |
| 96 _compilers = new CompilerPool(color: color); |
94 | 97 |
95 /// Starts the underlying server. | 98 /// Starts the underlying server. |
96 Future _load() { | 99 Future _load() { |
97 var staticPath = p.join(libDir(packageRoot: _packageRoot), | 100 var staticPath = p.join(libDir(packageRoot: _packageRoot), |
98 'src/runner/browser/static'); | 101 'src/runner/browser/static'); |
99 var cascade = new shelf.Cascade() | 102 var cascade = new shelf.Cascade() |
100 .add(_webSocketHandler.handler) | 103 .add(_webSocketHandler.handler) |
101 .add(createStaticHandler(staticPath, defaultDocument: 'index.html')) | 104 .add(createStaticHandler(staticPath, defaultDocument: 'index.html')) |
102 .add(createStaticHandler(_compiledDir, defaultDocument: 'index.html')); | 105 .add(createStaticHandler(_compiledDir, defaultDocument: 'index.html')); |
103 | 106 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 /// Returns a [Future] that completes once the server is closed and its | 154 /// Returns a [Future] that completes once the server is closed and its |
152 /// resources have been fully released. | 155 /// resources have been fully released. |
153 Future close() { | 156 Future close() { |
154 new Directory(_compiledDir).deleteSync(recursive: true); | 157 new Directory(_compiledDir).deleteSync(recursive: true); |
155 return _server.close().then((_) { | 158 return _server.close().then((_) { |
156 if (_browserManagerCompleter == null) return null; | 159 if (_browserManagerCompleter == null) return null; |
157 return _browserManager.then((_) => _browser.close()); | 160 return _browserManager.then((_) => _browser.close()); |
158 }); | 161 }); |
159 } | 162 } |
160 } | 163 } |
OLD | NEW |