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