| 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 import 'dart:io'; | 8 import 'package:scheduled_test/scheduled_stream.dart'; |
| 9 | 9 import 'package:scheduled_test/scheduled_test.dart'; |
| 10 import 'package:path/path.dart' as p; | |
| 11 import 'package:shelf/shelf.dart' as shelf; | |
| 12 import 'package:shelf/shelf_io.dart' as shelf_io; | |
| 13 import 'package:shelf_web_socket/shelf_web_socket.dart'; | |
| 14 import 'package:test/src/runner/browser/dartium.dart'; | 10 import 'package:test/src/runner/browser/dartium.dart'; |
| 15 import 'package:test/src/util/io.dart'; | |
| 16 import 'package:test/src/utils.dart'; | |
| 17 import 'package:test/test.dart'; | |
| 18 | 11 |
| 19 import '../../io.dart'; | 12 import '../../io.dart'; |
| 20 import '../../utils.dart'; | 13 import '../../utils.dart'; |
| 21 | 14 import 'code_server.dart'; |
| 22 String _sandbox; | |
| 23 | 15 |
| 24 void main() { | 16 void main() { |
| 25 setUp(() { | 17 useSandbox(); |
| 26 _sandbox = createTempDir(); | |
| 27 }); | |
| 28 | 18 |
| 29 tearDown(() { | 19 test("starts Dartium with the given URL", () { |
| 30 new Directory(_sandbox).deleteSync(recursive: true); | 20 var server = new CodeServer(); |
| 31 }); | |
| 32 | 21 |
| 33 group("running Dart", () { | 22 schedule(() async { |
| 34 // The Dart to serve in the server. | 23 var dartium = new Dartium(await server.url); |
| 35 var dart; | 24 currentSchedule.onComplete.schedule(() async => (await dartium).close()); |
| 36 | |
| 37 var servePage = (request) { | |
| 38 var path = shelfUrl(request).path; | |
| 39 | |
| 40 if (path.isEmpty) { | |
| 41 return new shelf.Response.ok(""" | |
| 42 <!doctype html> | |
| 43 <html> | |
| 44 <head> | |
| 45 <script type="application/dart" src="index.dart"></script> | |
| 46 </head> | |
| 47 </html> | |
| 48 """, headers: {'content-type': 'text/html'}); | |
| 49 } else if (path == "index.dart") { | |
| 50 return new shelf.Response.ok(''' | |
| 51 import "dart:html"; | |
| 52 | |
| 53 main() async { | |
| 54 $dart | |
| 55 } | |
| 56 ''', headers: {'content-type': 'application/dart'}); | |
| 57 } else { | |
| 58 return new shelf.Response.notFound(null); | |
| 59 } | |
| 60 }; | |
| 61 | |
| 62 var server; | |
| 63 var webSockets; | |
| 64 setUp(() async { | |
| 65 var webSocketsController = new StreamController(); | |
| 66 webSockets = webSocketsController.stream; | |
| 67 | |
| 68 server = await shelf_io.serve( | |
| 69 new shelf.Cascade() | |
| 70 .add(webSocketHandler(webSocketsController.add)) | |
| 71 .add(servePage).handler, | |
| 72 'localhost', 0); | |
| 73 }); | 25 }); |
| 74 | 26 |
| 75 tearDown(() { | 27 server.handleDart(''' |
| 76 if (server != null) server.close(); | |
| 77 | |
| 78 dart = null; | |
| 79 server = null; | |
| 80 webSockets = null; | |
| 81 }); | |
| 82 | |
| 83 test("starts Dartium with the given URL", () async { | |
| 84 dart = ''' | |
| 85 var webSocket = new WebSocket( | 28 var webSocket = new WebSocket( |
| 86 window.location.href.replaceFirst("http://", "ws://")); | 29 window.location.href.replaceFirst("http://", "ws://")); |
| 87 await webSocket.onOpen.first; | 30 await webSocket.onOpen.first; |
| 88 webSocket.send("loaded!"); | 31 webSocket.send("loaded!"); |
| 89 '''; | 32 '''); |
| 90 var dartium = new Dartium(baseUrlForAddress(server.address, server.port)); | |
| 91 | 33 |
| 92 try { | 34 var webSocket = server.handleWebSocket(); |
| 93 var message = await (await webSockets.first).first; | |
| 94 expect(message, equals("loaded!")); | |
| 95 } finally { | |
| 96 dartium.close(); | |
| 97 } | |
| 98 }); | |
| 99 | 35 |
| 100 test("doesn't preserve state across runs", () { | 36 schedule(() async { |
| 101 dart = ''' | 37 expect(await (await webSocket).first, equals("loaded!")); |
| 102 window.localStorage["data"] = "value"; | |
| 103 | |
| 104 var webSocket = new WebSocket( | |
| 105 window.location.href.replaceFirst("http://", "ws://")); | |
| 106 await webSocket.onOpen.first; | |
| 107 webSocket.send("done"); | |
| 108 '''; | |
| 109 var dartium = new Dartium(baseUrlForAddress(server.address, server.port)); | |
| 110 | |
| 111 var first = true; | |
| 112 webSockets.listen(expectAsync((webSocket) { | |
| 113 if (first) { | |
| 114 // The first request will set local storage data. We can't kill the | |
| 115 // old Dartium and start a new one until we're sure that that has | |
| 116 // finished. | |
| 117 webSocket.first.then((_) { | |
| 118 dartium.close(); | |
| 119 | |
| 120 dart = ''' | |
| 121 var webSocket = new WebSocket( | |
| 122 window.location.href.replaceFirst("http://", "ws://")); | |
| 123 await webSocket.onOpen.first; | |
| 124 webSocket.send(window.localStorage["data"].toString()); | |
| 125 '''; | |
| 126 dartium = new Dartium( | |
| 127 baseUrlForAddress(server.address, server.port)); | |
| 128 first = false; | |
| 129 }); | |
| 130 } else { | |
| 131 // The second request will return the local storage data. This should | |
| 132 // be null, indicating that no data was saved between runs. | |
| 133 expect( | |
| 134 webSocket.first | |
| 135 .then((message) => expect(message, equals('null'))) | |
| 136 .whenComplete(dartium.close), | |
| 137 completes); | |
| 138 } | |
| 139 }, count: 2)); | |
| 140 }); | 38 }); |
| 141 }); | 39 }); |
| 142 | 40 |
| 143 test("a process can be killed synchronously after it's started", () async { | 41 test("a process can be killed synchronously after it's started", () async { |
| 144 var server = await shelf_io.serve( | 42 var server = new CodeServer(); |
| 145 expectAsync((_) {}, count: 0), 'localhost', 0); | |
| 146 | 43 |
| 147 try { | 44 schedule(() async { |
| 148 var dartium = new Dartium(baseUrlForAddress(server.address, server.port)); | 45 var dartium = new Dartium(await server.url); |
| 149 await dartium.close(); | 46 await dartium.close(); |
| 150 } finally { | 47 }); |
| 151 server.close(); | |
| 152 } | |
| 153 }); | 48 }); |
| 154 | 49 |
| 155 test("reports an error in onExit", () { | 50 test("reports an error in onExit", () { |
| 156 var dartium = new Dartium("http://dart-lang.org", | 51 var dartium = new Dartium("http://dart-lang.org", |
| 157 executable: "_does_not_exist"); | 52 executable: "_does_not_exist"); |
| 158 expect(dartium.onExit, throwsA(isApplicationException(startsWith( | 53 expect(dartium.onExit, throwsA(isApplicationException(startsWith( |
| 159 "Failed to run Dartium: $noSuchFileMessage")))); | 54 "Failed to run Dartium: $noSuchFileMessage")))); |
| 160 }); | 55 }); |
| 161 | 56 |
| 162 test("can run successful tests", () { | 57 test("can run successful tests", () { |
| 163 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(""" | 58 d.file("test.dart", """ |
| 164 import 'package:test/test.dart'; | 59 import 'package:test/test.dart'; |
| 165 | 60 |
| 166 void main() { | 61 void main() { |
| 167 test("success", () {}); | 62 test("success", () {}); |
| 168 } | 63 } |
| 169 """); | 64 """).create(); |
| 170 | 65 |
| 171 var result = _runTest(["-p", "dartium", "test.dart"]); | 66 var test = runTest(["-p", "dartium", "test.dart"]); |
| 172 expect(result.stdout, isNot(contains("Compiling"))); | 67 test.stdout.fork().expect(never(contains("Compiling"))); |
| 173 expect(result.exitCode, equals(0)); | 68 test.stdout.expect(consumeThrough(contains("+1: All tests passed!"))); |
| 69 test.shouldExit(0); |
| 174 }); | 70 }); |
| 175 | 71 |
| 176 test("can run failing tests", () { | 72 test("can run failing tests", () { |
| 177 new File(p.join(_sandbox, "test.dart")).writeAsStringSync(""" | 73 d.file("test.dart", """ |
| 178 import 'package:test/test.dart'; | 74 import 'package:test/test.dart'; |
| 179 | 75 |
| 180 void main() { | 76 void main() { |
| 181 test("failure", () => throw new TestFailure("oh no")); | 77 test("failure", () => throw new TestFailure("oh no")); |
| 182 } | 78 } |
| 183 """); | 79 """).create(); |
| 184 | 80 |
| 185 var result = _runTest(["-p", "dartium", "test.dart"]); | 81 var test = runTest(["-p", "dartium", "test.dart"]); |
| 186 expect(result.exitCode, equals(1)); | 82 test.stdout.expect(consumeThrough(contains("-1: Some tests failed."))); |
| 83 test.shouldExit(1); |
| 187 }); | 84 }); |
| 188 } | 85 } |
| 189 | |
| 190 ProcessResult _runTest(List<String> args) => | |
| 191 runTest(args, workingDirectory: _sandbox); | |
| OLD | NEW |