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