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.server; | 5 library test.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 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 | 143 |
144 /// Starts the underlying server. | 144 /// Starts the underlying server. |
145 Future _load() { | 145 Future _load() { |
146 var cascade = new shelf.Cascade() | 146 var cascade = new shelf.Cascade() |
147 .add(_webSocketHandler.handler); | 147 .add(_webSocketHandler.handler); |
148 | 148 |
149 if (_pubServeUrl == null) { | 149 if (_pubServeUrl == null) { |
150 cascade = cascade | 150 cascade = cascade |
151 .add(_createPackagesHandler()) | 151 .add(_createPackagesHandler()) |
152 .add(_jsHandler.handler) | 152 .add(_jsHandler.handler) |
153 .add(_wrapperHandler) | 153 .add(createStaticHandler(_root)) |
154 .add(createStaticHandler(_root)); | 154 .add(_wrapperHandler); |
155 } | 155 } |
156 | 156 |
157 var pipeline = new shelf.Pipeline() | 157 var pipeline = new shelf.Pipeline() |
158 .addMiddleware(nestingMiddleware(_secret)) | 158 .addMiddleware(nestingMiddleware(_secret)) |
159 .addHandler(cascade.handler); | 159 .addHandler(cascade.handler); |
160 | 160 |
161 return shelf_io.serve(pipeline, 'localhost', 0).then((server) { | 161 return shelf_io.serve(pipeline, 'localhost', 0).then((server) { |
162 _server = server; | 162 _server = server; |
163 }); | 163 }); |
164 } | 164 } |
(...skipping 15 matching lines...) Expand all Loading... |
180 shelfChange(request, path: p.url.joinAll(segments.take(i + 1)))); | 180 shelfChange(request, path: p.url.joinAll(segments.take(i + 1)))); |
181 } | 181 } |
182 | 182 |
183 return new shelf.Response.notFound("Not found."); | 183 return new shelf.Response.notFound("Not found."); |
184 }; | 184 }; |
185 } | 185 } |
186 | 186 |
187 /// A handler that serves wrapper files used to bootstrap tests. | 187 /// A handler that serves wrapper files used to bootstrap tests. |
188 shelf.Response _wrapperHandler(shelf.Request request) { | 188 shelf.Response _wrapperHandler(shelf.Request request) { |
189 var path = p.fromUri(shelfUrl(request)); | 189 var path = p.fromUri(shelfUrl(request)); |
190 var withoutExtensions = p.withoutExtension(p.withoutExtension(path)); | |
191 var base = p.basename(withoutExtensions); | |
192 | 190 |
193 if (path.endsWith(".browser_test.dart")) { | 191 if (path.endsWith(".browser_test.dart")) { |
194 return new shelf.Response.ok(''' | 192 return new shelf.Response.ok(''' |
195 import "package:test/src/runner/browser/iframe_listener.dart"; | 193 import "package:test/src/runner/browser/iframe_listener.dart"; |
196 | 194 |
197 import "$base" as test; | 195 import "${p.basename(p.withoutExtension(p.withoutExtension(path)))}" as test; |
198 | 196 |
199 void main() { | 197 void main() { |
200 IframeListener.start(() => test.main); | 198 IframeListener.start(() => test.main); |
201 } | 199 } |
202 ''', headers: {'Content-Type': 'application/dart'}); | 200 ''', headers: {'Content-Type': 'application/dart'}); |
203 } | 201 } |
204 | 202 |
205 if (path.endsWith(".browser_test.html")) { | 203 if (path.endsWith(".html")) { |
206 // TODO(nweiz): support user-authored HTML files. | 204 var test = p.withoutExtension(path) + ".dart"; |
| 205 |
| 206 // Link to the Dart wrapper on Dartium and the compiled JS version |
| 207 // elsewhere. |
| 208 var script = request.headers['user-agent'].contains('(Dart)') |
| 209 ? 'type="application/dart" ' |
| 210 'src="${HTML_ESCAPE.convert(test)}.browser_test.dart"' |
| 211 : 'src="${HTML_ESCAPE.convert(test)}.browser_test.dart.js"'; |
| 212 |
207 return new shelf.Response.ok(''' | 213 return new shelf.Response.ok(''' |
208 <!DOCTYPE html> | 214 <!DOCTYPE html> |
209 <html> | 215 <html> |
210 <head> | 216 <head> |
211 <title>${HTML_ESCAPE.convert(base)}.dart Test</title> | 217 <title>${HTML_ESCAPE.convert(test)} Test</title> |
212 <script type="application/dart" | 218 <script $script></script> |
213 src="${HTML_ESCAPE.convert(base)}.browser_test.dart"> | |
214 </script> | |
215 <script src="packages/browser/dart.js"></script> | |
216 </head> | 219 </head> |
217 </html> | 220 </html> |
218 ''', headers: {'Content-Type': 'text/html'}); | 221 ''', headers: {'Content-Type': 'text/html'}); |
219 } | 222 } |
220 | 223 |
221 return new shelf.Response.notFound('Not found.'); | 224 return new shelf.Response.notFound('Not found.'); |
222 } | 225 } |
223 | 226 |
224 /// Loads the test suite at [path] on the browser [browser]. | 227 /// Loads the test suite at [path] on the browser [browser]. |
225 /// | 228 /// |
226 /// This will start a browser to load the suite if one isn't already running. | 229 /// This will start a browser to load the suite if one isn't already running. |
227 /// Throws an [ArgumentError] if [browser] isn't a browser platform. | 230 /// Throws an [ArgumentError] if [browser] isn't a browser platform. |
228 Future<Suite> loadSuite(String path, TestPlatform browser, | 231 Future<Suite> loadSuite(String path, TestPlatform browser, |
229 Metadata metadata) { | 232 Metadata metadata) { |
230 if (!browser.isBrowser) { | 233 if (!browser.isBrowser) { |
231 throw new ArgumentError("$browser is not a browser."); | 234 throw new ArgumentError("$browser is not a browser."); |
232 } | 235 } |
233 | 236 |
| 237 var htmlPath = p.withoutExtension(path) + '.html'; |
| 238 if (new File(htmlPath).existsSync() && |
| 239 !new File(htmlPath).readAsStringSync() |
| 240 .contains('packages/test/dart.js')) { |
| 241 throw new LoadException( |
| 242 path, |
| 243 '"${htmlPath}" must contain <script src="packages/test/dart.js">' |
| 244 '</script>.'); |
| 245 } |
| 246 |
234 return new Future.sync(() { | 247 return new Future.sync(() { |
235 if (_pubServeUrl != null) { | 248 if (_pubServeUrl != null) { |
236 var suitePrefix = p.relative(path, from: p.join(_root, 'test')) + | 249 var suitePrefix = p.withoutExtension( |
237 '.browser_test'; | 250 p.relative(path, from: p.join(_root, 'test'))); |
238 var jsUrl = _pubServeUrl.resolve('$suitePrefix.dart.js'); | 251 var jsUrl = _pubServeUrl.resolve( |
239 return _pubServeSuite(path, jsUrl).then((_) => | 252 '$suitePrefix.dart.browser_test.dart.js'); |
240 _pubServeUrl.resolve('$suitePrefix.html')); | 253 return _pubServeSuite(path, jsUrl) |
| 254 .then((_) => _pubServeUrl.resolveUri(p.toUri('$suitePrefix.html'))); |
241 } | 255 } |
242 | 256 |
243 return new Future.sync(() => browser.isJS ? _compileSuite(path) : null) | 257 return new Future.sync(() => browser.isJS ? _compileSuite(path) : null) |
244 .then((_) { | 258 .then((_) { |
245 if (_closed) return null; | 259 if (_closed) return null; |
246 return url.resolveUri( | 260 return url.resolveUri(p.toUri( |
247 p.toUri(p.relative(path, from: _root) + ".browser_test.html")); | 261 p.withoutExtension(p.relative(path, from: _root)) + ".html")); |
248 }); | 262 }); |
249 }).then((suiteUrl) { | 263 }).then((suiteUrl) { |
250 if (_closed) return null; | 264 if (_closed) return null; |
251 | 265 |
252 // TODO(nweiz): Don't start the browser until all the suites are compiled. | 266 // TODO(nweiz): Don't start the browser until all the suites are compiled. |
253 return _browserManagerFor(browser).then((browserManager) { | 267 return _browserManagerFor(browser).then((browserManager) { |
254 if (_closed) return null; | 268 if (_closed) return null; |
255 return browserManager.loadSuite(path, suiteUrl, metadata); | 269 return browserManager.loadSuite(path, suiteUrl, metadata); |
256 }).then((suite) { | 270 }).then((suite) { |
257 if (_closed) return null; | 271 if (_closed) return null; |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 if (_pubServeUrl == null) { | 409 if (_pubServeUrl == null) { |
396 new Directory(_compiledDir).deleteSync(recursive: true); | 410 new Directory(_compiledDir).deleteSync(recursive: true); |
397 } else { | 411 } else { |
398 _http.close(); | 412 _http.close(); |
399 } | 413 } |
400 | 414 |
401 _closeCompleter.complete(); | 415 _closeCompleter.complete(); |
402 }).catchError(_closeCompleter.completeError); | 416 }).catchError(_closeCompleter.completeError); |
403 } | 417 } |
404 } | 418 } |
OLD | NEW |