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 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import 'package:test/test.dart'; | |
11 import 'package:test/src/runner/browser/firefox.dart'; | |
12 import 'package:test/src/util/io.dart'; | |
13 import 'package:shelf/shelf.dart' as shelf; | 10 import 'package:shelf/shelf.dart' as shelf; |
14 import 'package:shelf/shelf_io.dart' as shelf_io; | 11 import 'package:shelf/shelf_io.dart' as shelf_io; |
15 import 'package:shelf_web_socket/shelf_web_socket.dart'; | 12 import 'package:shelf_web_socket/shelf_web_socket.dart'; |
| 13 import 'package:test/src/runner/browser/dartium.dart'; |
| 14 import 'package:test/src/util/io.dart'; |
| 15 import 'package:test/src/utils.dart'; |
| 16 import 'package:test/test.dart'; |
16 | 17 |
17 void main() { | 18 void main() { |
18 group("running JavaScript", () { | 19 group("running Dart", () { |
19 // The JavaScript to serve in the server. We use actual JavaScript here to | 20 // The Dart to serve in the server. |
20 // avoid the pain of compiling to JS in a test | 21 var dart; |
21 var javaScript; | |
22 | 22 |
23 var servePage = (request) { | 23 var servePage = (request) { |
24 var path = request.url.path; | 24 var path = shelfUrl(request).path; |
25 | |
26 // We support both shelf 0.5.x and 0.6.x. The former has a leading "/" | |
27 // here, the latter does not. | |
28 if (path.startsWith("/")) path = path.substring(1); | |
29 | 25 |
30 if (path.isEmpty) { | 26 if (path.isEmpty) { |
31 return new shelf.Response.ok(""" | 27 return new shelf.Response.ok(""" |
32 <!doctype html> | 28 <!doctype html> |
33 <html> | 29 <html> |
34 <head> | 30 <head> |
35 <script src="index.js"></script> | 31 <script type="application/dart" src="index.dart"></script> |
36 </head> | 32 </head> |
37 </html> | 33 </html> |
38 """, headers: {'content-type': 'text/html'}); | 34 """, headers: {'content-type': 'text/html'}); |
39 } else if (path == "index.js") { | 35 } else if (path == "index.dart") { |
40 return new shelf.Response.ok(javaScript, | 36 return new shelf.Response.ok(''' |
41 headers: {'content-type': 'application/javascript'}); | 37 import "dart:html"; |
| 38 |
| 39 void main() { |
| 40 $dart |
| 41 } |
| 42 ''', headers: {'content-type': 'application/dart'}); |
42 } else { | 43 } else { |
43 return new shelf.Response.notFound(null); | 44 return new shelf.Response.notFound(null); |
44 } | 45 } |
45 }; | 46 }; |
46 | 47 |
47 var server; | 48 var server; |
48 var webSockets; | 49 var webSockets; |
49 setUp(() { | 50 setUp(() { |
50 var webSocketsController = new StreamController(); | 51 var webSocketsController = new StreamController(); |
51 webSockets = webSocketsController.stream; | 52 webSockets = webSocketsController.stream; |
52 | 53 |
53 return shelf_io.serve( | 54 return shelf_io.serve( |
54 new shelf.Cascade() | 55 new shelf.Cascade() |
55 .add(webSocketHandler(webSocketsController.add)) | 56 .add(webSocketHandler(webSocketsController.add)) |
56 .add(servePage).handler, | 57 .add(servePage).handler, |
57 'localhost', 0).then((server_) { | 58 'localhost', 0).then((server_) { |
58 server = server_; | 59 server = server_; |
59 }); | 60 }); |
60 }); | 61 }); |
61 | 62 |
62 tearDown(() { | 63 tearDown(() { |
63 if (server != null) server.close(); | 64 if (server != null) server.close(); |
64 | 65 |
65 javaScript = null; | 66 dart = null; |
66 server = null; | 67 server = null; |
67 webSockets = null; | 68 webSockets = null; |
68 }); | 69 }); |
69 | 70 |
70 test("starts Firefox with the given URL", () { | 71 test("starts Dartium with the given URL", () { |
71 javaScript = ''' | 72 dart = ''' |
72 var webSocket = new WebSocket(window.location.href.replace("http://", "ws://")); | 73 var webSocket = new WebSocket( |
73 webSocket.addEventListener("open", function() { | 74 window.location.href.replaceFirst("http://", "ws://")); |
74 webSocket.send("loaded!"); | 75 webSocket.onOpen.first.then((_) => webSocket.send("loaded!")); |
75 }); | |
76 '''; | 76 '''; |
77 var firefox = new Firefox(baseUrlForAddress(server.address, server.port)); | 77 var dartium = new Dartium(baseUrlForAddress(server.address, server.port)); |
78 | 78 |
79 return webSockets.first.then((webSocket) { | 79 return webSockets.first.then((webSocket) { |
80 return webSocket.first.then( | 80 return webSocket.first.then( |
81 (message) => expect(message, equals("loaded!"))); | 81 (message) => expect(message, equals("loaded!"))); |
82 }).whenComplete(firefox.close); | 82 }).whenComplete(dartium.close); |
83 }); | 83 }); |
84 | 84 |
85 test("doesn't preserve state across runs", () { | 85 test("doesn't preserve state across runs", () { |
86 javaScript = ''' | 86 dart = ''' |
87 localStorage.setItem("data", "value"); | 87 window.localStorage["data"] = "value"; |
88 | 88 |
89 var webSocket = new WebSocket(window.location.href.replace("http://", "ws://")); | 89 var webSocket = new WebSocket( |
90 webSocket.addEventListener("open", function() { | 90 window.location.href.replaceFirst("http://", "ws://")); |
91 webSocket.send("done"); | 91 webSocket.onOpen.first.then((_) => webSocket.send("done")); |
92 }); | |
93 '''; | 92 '''; |
94 var firefox = new Firefox(baseUrlForAddress(server.address, server.port)); | 93 var dartium = new Dartium(baseUrlForAddress(server.address, server.port)); |
95 | 94 |
96 var first = true; | 95 var first = true; |
97 webSockets.listen(expectAsync((webSocket) { | 96 webSockets.listen(expectAsync((webSocket) { |
98 if (first) { | 97 if (first) { |
99 // The first request will set local storage data. We can't kill the | 98 // The first request will set local storage data. We can't kill the |
100 // old firefox and start a new one until we're sure that that has | 99 // old Dartium and start a new one until we're sure that that has |
101 // finished. | 100 // finished. |
102 webSocket.first.then((_) { | 101 webSocket.first.then((_) { |
103 firefox.close(); | 102 dartium.close(); |
104 | 103 |
105 javaScript = ''' | 104 dart = ''' |
106 var webSocket = new WebSocket(window.location.href.replace("http://", "ws://")); | 105 var webSocket = new WebSocket( |
107 webSocket.addEventListener("open", function() { | 106 window.location.href.replaceFirst("http://", "ws://")); |
108 webSocket.send(localStorage.getItem("data")); | 107 webSocket.onOpen.first.then((_) => |
109 }); | 108 webSocket.send(window.localStorage["data"].toString())); |
110 '''; | 109 '''; |
111 firefox = new Firefox(baseUrlForAddress(server.address, server.port)
); | 110 dartium = new Dartium( |
| 111 baseUrlForAddress(server.address, server.port)); |
112 first = false; | 112 first = false; |
113 }); | 113 }); |
114 } else { | 114 } else { |
115 // The second request will return the local storage data. This should | 115 // The second request will return the local storage data. This should |
116 // be null, indicating that no data was saved between runs. | 116 // be null, indicating that no data was saved between runs. |
117 expect( | 117 expect( |
118 webSocket.first | 118 webSocket.first |
119 .then((message) => expect(message, equals('null'))) | 119 .then((message) => expect(message, equals('null'))) |
120 .whenComplete(firefox.close), | 120 .whenComplete(dartium.close), |
121 completes); | 121 completes); |
122 } | 122 } |
123 }, count: 2)); | 123 }, count: 2)); |
124 }); | 124 }); |
125 }); | 125 }); |
126 | 126 |
127 test("a process can be killed synchronously after it's started", () { | 127 test("a process can be killed synchronously after it's started", () { |
128 return shelf_io.serve(expectAsync((_) {}, count: 0), 'localhost', 0) | 128 return shelf_io.serve(expectAsync((_) {}, count: 0), 'localhost', 0) |
129 .then((server) { | 129 .then((server) { |
130 var firefox = new Firefox(baseUrlForAddress(server.address, server.port)); | 130 var dartium = new Dartium(baseUrlForAddress(server.address, server.port)); |
131 return firefox.close().whenComplete(server.close); | 131 return dartium.close().whenComplete(server.close); |
132 }); | 132 }); |
133 }); | 133 }); |
134 | 134 |
135 test("reports an error in onExit", () { | 135 test("reports an error in onExit", () { |
136 var firefox = new Firefox("http://dart-lang.org", | 136 var dartium = new Dartium("http://dart-lang.org", |
137 executable: "_does_not_exist"); | 137 executable: "_does_not_exist"); |
138 expect(firefox.onExit, throwsA(new isInstanceOf<ProcessException>())); | 138 expect(dartium.onExit, throwsA(new isInstanceOf<ProcessException>())); |
139 }); | 139 }); |
140 } | 140 } |
OLD | NEW |