Chromium Code Reviews

Side by Side Diff: test/runner/browser/safari_test.dart

Issue 1243293002: Convert a bunch of tests to use scheduled_test's infrastructure. (Closed) Base URL: git@github.com:dart-lang/test@master
Patch Set: Code review changes Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
« no previous file with comments | « test/runner/browser/runner_test.dart ('k') | test/runner/compact_reporter_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 && mac-os") 5 @TestOn("vm && mac-os")
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/safari.dart'; 10 import 'package:test/src/runner/browser/safari.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 Safari with the given URL", () {
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 safari = new Safari(await server.url);
30 if (path.startsWith("/")) path = path.substring(1); 24 currentSchedule.onComplete.schedule(
31 25 () async => (await safari).close());
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 }); 26 });
61 27
62 tearDown(() { 28 server.handleJavaScript('''
63 if (server != null) server.close();
64
65 javaScript = null;
66 server = null;
67 webSockets = null;
68 });
69
70 test("starts Safari with the given URL", () async {
71 javaScript = '''
72 var webSocket = new WebSocket(window.location.href.replace("http://", "ws://")); 29 var webSocket = new WebSocket(window.location.href.replace("http://", "ws://"));
73 webSocket.addEventListener("open", function() { 30 webSocket.addEventListener("open", function() {
74 webSocket.send("loaded!"); 31 webSocket.send("loaded!");
75 }); 32 });
76 '''; 33 ''');
77 var safari = new Safari(baseUrlForAddress(server.address, server.port));
78 34
79 try { 35 var webSocket = server.handleWebSocket();
80 var message = await (await webSockets.first).first;
81 expect(message, equals("loaded!"));
82 } finally {
83 safari.close();
84 }
85 });
86 36
87 test("doesn't preserve state across runs", () { 37 schedule(() async {
88 javaScript = ''' 38 expect(await (await webSocket).first, equals("loaded!"));
89 localStorage.setItem("data", "value");
90
91 var webSocket = new WebSocket(window.location.href.replace("http://", "ws://"));
92 webSocket.addEventListener("open", function() {
93 webSocket.send("done");
94 });
95 ''';
96 var safari = new Safari(baseUrlForAddress(server.address, server.port));
97
98 var first = true;
99 webSockets.listen(expectAsync((webSocket) {
100 if (first) {
101 // The first request will set local storage data. We can't kill the
102 // old safari and start a new one until we're sure that that has
103 // finished.
104 webSocket.first.then((_) {
105 safari.close();
106
107 javaScript = '''
108 var webSocket = new WebSocket(window.location.href.replace("http://", "ws://"));
109 webSocket.addEventListener("open", function() {
110 webSocket.send(localStorage.getItem("data"));
111 });
112 ''';
113 safari = new Safari(baseUrlForAddress(server.address, server.port));
114 first = false;
115 });
116 } else {
117 // The second request will return the local storage data. This should
118 // be null, indicating that no data was saved between runs.
119 expect(
120 webSocket.first
121 .then((message) => expect(message, equals('null')))
122 .whenComplete(safari.close),
123 completes);
124 }
125 }, count: 2));
126 }); 39 });
127 }); 40 });
128 41
129 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 {
130 var server = await shelf_io.serve( 43 var server = new CodeServer();
131 expectAsync((_) {}, count: 0), 'localhost', 0);
132 44
133 try { 45 schedule(() async {
134 var safari = new Safari(baseUrlForAddress(server.address, server.port)); 46 var safari = new Safari(await server.url);
135 await safari.close(); 47 await safari.close();
136 } finally { 48 });
137 server.close();
138 }
139 }); 49 });
140 50
141 test("reports an error in onExit", () { 51 test("reports an error in onExit", () {
142 var safari = new Safari("http://dart-lang.org", 52 var safari = new Safari("http://dart-lang.org",
143 executable: "_does_not_exist"); 53 executable: "_does_not_exist");
144 expect(safari.onExit, throwsA(isApplicationException(startsWith( 54 expect(safari.onExit, throwsA(isApplicationException(startsWith(
145 "Failed to run Safari: $noSuchFileMessage")))); 55 "Failed to run Safari: $noSuchFileMessage"))));
146 }); 56 });
57
58 test("can run successful tests", () {
59 d.file("test.dart", """
60 import 'package:test/test.dart';
61
62 void main() {
63 test("success", () {});
147 } 64 }
65 """).create();
66
67 var test = runTest(["-p", "safari", "test.dart"]);
68 test.stdout.expect(consumeThrough(contains("+1: All tests passed!")));
69 test.shouldExit(0);
70 });
71
72 test("can run failing tests", () {
73 d.file("test.dart", """
74 import 'package:test/test.dart';
75
76 void main() {
77 test("failure", () => throw new TestFailure("oh no"));
78 }
79 """).create();
80
81 var test = runTest(["-p", "safari", "test.dart"]);
82 test.stdout.expect(consumeThrough(contains("-1: Some tests failed.")));
83 test.shouldExit(1);
84 });
85 }
OLDNEW
« no previous file with comments | « test/runner/browser/runner_test.dart ('k') | test/runner/compact_reporter_test.dart » ('j') | no next file with comments »

Powered by Google App Engine