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