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 library unittest.runner.browser.host; | 5 library test.runner.browser.host; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:html'; | 9 import 'dart:html'; |
10 | 10 |
11 import 'package:stack_trace/stack_trace.dart'; | 11 import 'package:stack_trace/stack_trace.dart'; |
12 import 'package:unittest/src/util/multi_channel.dart'; | 12 import 'package:test/src/util/multi_channel.dart'; |
13 import 'package:unittest/src/util/stream_channel.dart'; | 13 import 'package:test/src/util/stream_channel.dart'; |
14 | 14 |
15 // TODO(nweiz): test this once we can run browser tests. | 15 // TODO(nweiz): test this once we can run browser tests. |
16 /// Code that runs in the browser and loads test suites at the server's behest. | 16 /// Code that runs in the browser and loads test suites at the server's behest. |
17 /// | 17 /// |
18 /// One instance of this runs for each browser. When the server tells it to load | 18 /// One instance of this runs for each browser. When the server tells it to load |
19 /// a test, it starts an iframe pointing at that test's code; from then on, it | 19 /// a test, it starts an iframe pointing at that test's code; from then on, it |
20 /// just relays messages between the two. | 20 /// just relays messages between the two. |
21 /// | 21 /// |
22 /// The browser uses two layers of [MultiChannel]s when communicating with the | 22 /// The browser uses two layers of [MultiChannel]s when communicating with the |
23 /// server: | 23 /// server: |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 // (http://caniuse.com/#search=MessageChannel). | 111 // (http://caniuse.com/#search=MessageChannel). |
112 | 112 |
113 // Send an initial command to give the iframe something to reply to. | 113 // Send an initial command to give the iframe something to reply to. |
114 iframe.contentWindow.postMessage( | 114 iframe.contentWindow.postMessage( |
115 {"command": "connect"}, | 115 {"command": "connect"}, |
116 window.location.origin); | 116 window.location.origin); |
117 | 117 |
118 window.onMessage.listen((message) { | 118 window.onMessage.listen((message) { |
119 // A message on the Window can theoretically come from any website. It's | 119 // A message on the Window can theoretically come from any website. It's |
120 // very unlikely that a malicious site would care about hacking someone's | 120 // very unlikely that a malicious site would care about hacking someone's |
121 // unit tests, let alone be able to find the unittest server while it's | 121 // unit tests, let alone be able to find the test server while it's |
122 // running, but it's good practice to check the origin anyway. | 122 // running, but it's good practice to check the origin anyway. |
123 if (message.origin != window.location.origin) return; | 123 if (message.origin != window.location.origin) return; |
124 | 124 |
125 // TODO(nweiz): Stop manually checking href here once issue 22554 is | 125 // TODO(nweiz): Stop manually checking href here once issue 22554 is |
126 // fixed. | 126 // fixed. |
127 if (message.data["href"] != iframe.src) return; | 127 if (message.data["href"] != iframe.src) return; |
128 | 128 |
129 message.stopPropagation(); | 129 message.stopPropagation(); |
130 inputController.add(message.data["data"]); | 130 inputController.add(message.data["data"]); |
131 }); | 131 }); |
132 | 132 |
133 outputController.stream.listen((message) => | 133 outputController.stream.listen((message) => |
134 iframe.contentWindow.postMessage(message, window.location.origin)); | 134 iframe.contentWindow.postMessage(message, window.location.origin)); |
135 }); | 135 }); |
136 | 136 |
137 return new StreamChannel(inputController.stream, outputController.sink); | 137 return new StreamChannel(inputController.stream, outputController.sink); |
138 } | 138 } |
OLD | NEW |