| 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 @TestOn("vm") | 5 @TestOn("vm") |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'package:scheduled_test/descriptor.dart' as d; |
| 8 | 8 import 'package:scheduled_test/scheduled_stream.dart'; |
| 9 import 'package:test/test.dart'; | 9 import 'package:scheduled_test/scheduled_test.dart'; |
| 10 import 'package:test/src/runner/browser/chrome.dart'; | 10 import 'package:test/src/runner/browser/chrome.dart'; |
| 11 import 'package:test/src/util/io.dart'; | |
| 12 import 'package:shelf/shelf.dart' as shelf; | |
| 13 import 'package:shelf/shelf_io.dart' as shelf_io; | |
| 14 import 'package:shelf_web_socket/shelf_web_socket.dart'; | |
| 15 | 11 |
| 16 import '../../io.dart'; | 12 import '../../io.dart'; |
| 17 import '../../utils.dart'; | 13 import '../../utils.dart'; |
| 14 import 'code_server.dart'; |
| 18 | 15 |
| 19 void main() { | 16 void main() { |
| 20 group("running JavaScript", () { | 17 useSandbox(); |
| 21 // The JavaScript to serve in the server. We use actual JavaScript here to | |
| 22 // avoid the pain of compiling to JS in a test | |
| 23 var javaScript; | |
| 24 | 18 |
| 25 var servePage = (request) { | 19 test("starts Chrome with the given URL", () { |
| 26 var path = request.url.path; | 20 var server = new CodeServer(); |
| 27 | 21 |
| 28 // We support both shelf 0.5.x and 0.6.x. The former has a leading "/" | 22 schedule(() async { |
| 29 // here, the latter does not. | 23 var chrome = new Chrome(await server.url); |
| 30 if (path.startsWith("/")) path = path.substring(1); | 24 currentSchedule.onComplete.schedule(() async => (await chrome).close()); |
| 31 | |
| 32 if (path.isEmpty) { | |
| 33 return new shelf.Response.ok(""" | |
| 34 <!doctype html> | |
| 35 <html> | |
| 36 <head> | |
| 37 <script src="index.js"></script> | |
| 38 </head> | |
| 39 </html> | |
| 40 """, headers: {'content-type': 'text/html'}); | |
| 41 } else if (path == "index.js") { | |
| 42 return new shelf.Response.ok(javaScript, | |
| 43 headers: {'content-type': 'application/javascript'}); | |
| 44 } else { | |
| 45 return new shelf.Response.notFound(null); | |
| 46 } | |
| 47 }; | |
| 48 | |
| 49 var server; | |
| 50 var webSockets; | |
| 51 setUp(() async { | |
| 52 var webSocketsController = new StreamController(); | |
| 53 webSockets = webSocketsController.stream; | |
| 54 | |
| 55 server = await shelf_io.serve( | |
| 56 new shelf.Cascade() | |
| 57 .add(webSocketHandler(webSocketsController.add)) | |
| 58 .add(servePage).handler, | |
| 59 'localhost', 0); | |
| 60 }); | 25 }); |
| 61 | 26 |
| 62 tearDown(() { | 27 server.handleJavaScript(''' |
| 63 if (server != null) server.close(); | |
| 64 | |
| 65 javaScript = null; | |
| 66 server = null; | |
| 67 webSockets = null; | |
| 68 }); | |
| 69 | |
| 70 test("starts Chrome with the given URL", () async { | |
| 71 javaScript = ''' | |
| 72 var webSocket = new WebSocket(window.location.href.replace("http://", "ws://")); | 28 var webSocket = new WebSocket(window.location.href.replace("http://", "ws://")); |
| 73 webSocket.addEventListener("open", function() { | 29 webSocket.addEventListener("open", function() { |
| 74 webSocket.send("loaded!"); | 30 webSocket.send("loaded!"); |
| 75 }); | 31 }); |
| 76 '''; | 32 '''); |
| 77 var chrome = new Chrome(baseUrlForAddress(server.address, server.port)); | |
| 78 | 33 |
| 79 try { | 34 var webSocket = server.handleWebSocket(); |
| 80 var message = await (await webSockets.first).first; | |
| 81 expect(message, equals("loaded!")); | |
| 82 } finally { | |
| 83 chrome.close(); | |
| 84 } | |
| 85 }, | |
| 86 // It's not clear why, but this test in particular seems to time out | |
| 87 // when run in parallel with many other tests. | |
| 88 timeout: new Timeout.factor(2)); | |
| 89 | 35 |
| 90 test("doesn't preserve state across runs", () { | 36 schedule(() async { |
| 91 javaScript = ''' | 37 expect(await (await webSocket).first, equals("loaded!")); |
| 92 localStorage.setItem("data", "value"); | 38 }); |
| 39 }, |
| 40 // It's not clear why, but this test in particular seems to time out |
| 41 // when run in parallel with many other tests. |
| 42 timeout: new Timeout.factor(2)); |
| 93 | 43 |
| 94 var webSocket = new WebSocket(window.location.href.replace("http://", "ws://")); | 44 test("a process can be killed synchronously after it's started", () async { |
| 95 webSocket.addEventListener("open", function() { | 45 var server = new CodeServer(); |
| 96 webSocket.send("done"); | |
| 97 }); | |
| 98 '''; | |
| 99 var chrome = new Chrome(baseUrlForAddress(server.address, server.port)); | |
| 100 | 46 |
| 101 var first = true; | 47 schedule(() async { |
| 102 webSockets.listen(expectAsync((webSocket) { | 48 var chrome = new Chrome(await server.url); |
| 103 if (first) { | 49 await chrome.close(); |
| 104 // The first request will set local storage data. We can't kill the | |
| 105 // old chrome and start a new one until we're sure that that has | |
| 106 // finished. | |
| 107 webSocket.first.then((_) { | |
| 108 chrome.close(); | |
| 109 | |
| 110 javaScript = ''' | |
| 111 var webSocket = new WebSocket(window.location.href.replace("http://", "ws://")); | |
| 112 webSocket.addEventListener("open", function() { | |
| 113 webSocket.send(localStorage.getItem("data")); | |
| 114 }); | |
| 115 '''; | |
| 116 chrome = new Chrome(baseUrlForAddress(server.address, server.port)); | |
| 117 first = false; | |
| 118 }); | |
| 119 } else { | |
| 120 // The second request will return the local storage data. This should | |
| 121 // be null, indicating that no data was saved between runs. | |
| 122 expect( | |
| 123 webSocket.first | |
| 124 .then((message) => expect(message, equals('null'))) | |
| 125 .whenComplete(chrome.close), | |
| 126 completes); | |
| 127 } | |
| 128 }, count: 2)); | |
| 129 }); | 50 }); |
| 130 }); | 51 }); |
| 131 | 52 |
| 132 test("a process can be killed synchronously after it's started", () async { | |
| 133 var server = await shelf_io.serve( | |
| 134 expectAsync((_) {}, count: 0), 'localhost', 0); | |
| 135 | |
| 136 try { | |
| 137 var chrome = new Chrome(baseUrlForAddress(server.address, server.port)); | |
| 138 await chrome.close(); | |
| 139 } finally { | |
| 140 server.close(); | |
| 141 } | |
| 142 }); | |
| 143 | |
| 144 test("reports an error in onExit", () { | 53 test("reports an error in onExit", () { |
| 145 var chrome = new Chrome("http://dart-lang.org", | 54 var chrome = new Chrome("http://dart-lang.org", |
| 146 executable: "_does_not_exist"); | 55 executable: "_does_not_exist"); |
| 147 expect(chrome.onExit, throwsA(isApplicationException(startsWith( | 56 expect(chrome.onExit, throwsA(isApplicationException(startsWith( |
| 148 "Failed to run Chrome: $noSuchFileMessage")))); | 57 "Failed to run Chrome: $noSuchFileMessage")))); |
| 149 }); | 58 }); |
| 59 |
| 60 test("can run successful tests", () { |
| 61 d.file("test.dart", """ |
| 62 import 'package:test/test.dart'; |
| 63 |
| 64 void main() { |
| 65 test("success", () {}); |
| 150 } | 66 } |
| 67 """).create(); |
| 68 |
| 69 var test = runTest(["-p", "chrome", "test.dart"]); |
| 70 test.stdout.expect(consumeThrough(contains("+1: All tests passed!"))); |
| 71 test.shouldExit(0); |
| 72 }); |
| 73 |
| 74 test("can run failing tests", () { |
| 75 d.file("test.dart", """ |
| 76 import 'package:test/test.dart'; |
| 77 |
| 78 void main() { |
| 79 test("failure", () => throw new TestFailure("oh no")); |
| 80 } |
| 81 """).create(); |
| 82 |
| 83 var test = runTest(["-p", "chrome", "test.dart"]); |
| 84 test.stdout.expect(consumeThrough(contains("-1: Some tests failed."))); |
| 85 test.shouldExit(1); |
| 86 }); |
| 87 } |
| OLD | NEW |