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 && windows") | 5 @TestOn("vm && windows") |
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/internet_explorer.dart'; | 10 import 'package:test/src/runner/browser/internet_explorer.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 IE with the given URL", () async { |
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 ie = new InternetExplorer(await server.url); |
30 if (path.startsWith("/")) path = path.substring(1); | 24 currentSchedule.onComplete.schedule(() async => (await ie).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 IE 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 ie = new InternetExplorer( | |
78 baseUrlForAddress(server.address, server.port)); | |
79 | 33 |
80 try { | 34 var webSocket = server.handleWebSocket(); |
81 var message = await (await webSockets.first).first; | 35 |
82 expect(message, equals("loaded!")); | 36 schedule(() async { |
83 } finally { | 37 expect(await (await webSocket).first, equals("loaded!")); |
84 ie.close(); | |
85 } | |
86 }); | 38 }); |
87 }); | 39 }); |
88 | 40 |
89 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 { |
90 var server = await shelf_io.serve( | 42 var server = new CodeServer(); |
91 expectAsync((_) {}, count: 0), 'localhost', 0); | |
92 | 43 |
93 try { | 44 schedule(() async { |
94 var ie = new InternetExplorer( | 45 var ie = new InternetExplorer(await server.url); |
95 baseUrlForAddress(server.address, server.port)); | |
96 await ie.close(); | 46 await ie.close(); |
97 } finally { | 47 }); |
98 server.close(); | |
99 } | |
100 }); | 48 }); |
101 | 49 |
102 test("reports an error in onExit", () { | 50 test("reports an error in onExit", () { |
103 var ie = new InternetExplorer("http://dart-lang.org", | 51 var ie = new InternetExplorer("http://dart-lang.org", |
104 executable: "_does_not_exist"); | 52 executable: "_does_not_exist"); |
105 expect(ie.onExit, throwsA(isApplicationException(startsWith( | 53 expect(ie.onExit, throwsA(isApplicationException(startsWith( |
106 "Failed to run Internet Explorer: $noSuchFileMessage")))); | 54 "Failed to run Internet Explorer: $noSuchFileMessage")))); |
107 }); | 55 }); |
| 56 |
| 57 test("can run successful tests", () { |
| 58 d.file("test.dart", """ |
| 59 import 'package:test/test.dart'; |
| 60 |
| 61 void main() { |
| 62 test("success", () {}); |
108 } | 63 } |
| 64 """).create(); |
| 65 |
| 66 var test = runTest(["-p", "ie", "test.dart"]); |
| 67 test.stdout.expect(consumeThrough(contains("+1: All tests passed!"))); |
| 68 test.shouldExit(0); |
| 69 }); |
| 70 |
| 71 test("can run failing tests", () { |
| 72 d.file("test.dart", """ |
| 73 import 'package:test/test.dart'; |
| 74 |
| 75 void main() { |
| 76 test("failure", () => throw new TestFailure("oh no")); |
| 77 } |
| 78 """).create(); |
| 79 |
| 80 var test = runTest(["-p", "ie", "test.dart"]); |
| 81 test.stdout.expect(consumeThrough(contains("-1: Some tests failed."))); |
| 82 test.shouldExit(1); |
| 83 }); |
| 84 } |
OLD | NEW |