OLD | NEW |
| (Empty) |
1 // Copyright (c) 2014, 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 /// Helper library that creates an iframe sandbox that can be used to load | |
6 /// code. | |
7 library trydart.test.sandbox; | |
8 | |
9 import 'dart:html'; | |
10 import 'dart:async'; | |
11 | |
12 // TODO(ahe): Remove this import if issue 17936 is fixed. | |
13 import 'dart:js' as hack; | |
14 | |
15 import 'package:expect/expect.dart' show | |
16 Expect; | |
17 | |
18 final Listener listener = new Listener(); | |
19 | |
20 void onError(String message, String filename, int lineno, [int colno, error]) { | |
21 if (filename != null && filename != "" && lineno != 0) { | |
22 if (colno != null && colno != 0) { | |
23 message = '$filename:$lineno:$colno $message'; | |
24 } else { | |
25 message = '$filename:$lineno: $message'; | |
26 } | |
27 } | |
28 if (error != null) { | |
29 // See: | |
30 // https://mikewest.org/2013/08/debugging-runtime-errors-with-window-onerror | |
31 var stack = error['stack']; | |
32 if (stack != null) { | |
33 message += '\n$stack'; | |
34 } | |
35 } | |
36 message = "Error occurred in iframe: $message"; | |
37 | |
38 // Synchronous, easier to read when running the browser manually. | |
39 window.console.log(message); | |
40 | |
41 new Future(() { | |
42 // Browsers ignore errors throw in event listeners (or from | |
43 // window.onerror). | |
44 throw message; | |
45 }); | |
46 } | |
47 | |
48 void installErrorHandlerOn(IFrameElement iframe) { | |
49 // This method uses dart:js to install an error event handler on the content | |
50 // window of [iframe]. This is a workaround for http://dartbug.com/17936. | |
51 var iframeProxy = new hack.JsObject.fromBrowserObject(iframe); | |
52 var contentWindowProxy = iframeProxy['contentWindow']; | |
53 if (contentWindowProxy == null) { | |
54 print('No contentWindow in iframe'); | |
55 throw 'No contentWindow in iframe'; | |
56 } | |
57 | |
58 // Note: we have two options, use "iframe.contentWindow.onerror = ..." or | |
59 // "iframe.contentWindow.addEventListener('error', ...)". The former seems | |
60 // to provide more details on both Chrome and Firefox (which provides no | |
61 // information at all in error events). | |
62 contentWindowProxy['onerror'] = onError; | |
63 } | |
64 | |
65 void onIframeLoaded(Event event) { | |
66 installErrorHandlerOn(event.target); | |
67 } | |
68 | |
69 IFrameElement appendIFrame(String src, Element element) { | |
70 IFrameElement iframe = new IFrameElement() | |
71 ..src = src | |
72 ..onLoad.listen(onIframeLoaded); | |
73 element.append(iframe); | |
74 // Install an error handler both on the new iframe element, and when it has | |
75 // fired the load event. That seems to matter according to some sources on | |
76 // stackoverflow. | |
77 installErrorHandlerOn(iframe); | |
78 return iframe; | |
79 } | |
80 | |
81 class Listener { | |
82 Completer completer; | |
83 | |
84 String expectedMessage; | |
85 | |
86 Stopwatch wallclock; | |
87 | |
88 int get elapsed => wallclock.elapsedMilliseconds ~/ 1000; | |
89 | |
90 void onMessage(MessageEvent e) { | |
91 String message = e.data; | |
92 if (expectedMessage == message) { | |
93 completer.complete(); | |
94 } else { | |
95 switch (message) { | |
96 case 'dart-calling-main': | |
97 case 'dart-main-done': | |
98 case 'unittest-suite-done': | |
99 case 'unittest-suite-fail': | |
100 case 'unittest-suite-success': | |
101 case 'unittest-suite-wait-for-done': | |
102 break; | |
103 | |
104 default: | |
105 completer.completeError( | |
106 'Unexpected message: "$message" (expected "$expectedMessage").'); | |
107 } | |
108 } | |
109 } | |
110 | |
111 Future expect(data) { | |
112 if (data is String) { | |
113 Expect.isTrue(completer == null || completer.isCompleted); | |
114 expectedMessage = data; | |
115 completer = new Completer(); | |
116 return completer.future; | |
117 } else if (data is Iterable) { | |
118 return Future.forEach(data, expect); | |
119 } else { | |
120 throw 'Unexpected data type: ${data.runtimeType}.'; | |
121 } | |
122 } | |
123 | |
124 void start() { | |
125 wallclock = new Stopwatch()..start(); | |
126 window.onMessage.listen(onMessage); | |
127 } | |
128 } | |
OLD | NEW |