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