OLD | NEW |
---|---|
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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 /// Whitebox integration/end-to-end test of Try Dart! site. | 5 /// Helper library that creates an iframe sandbox for that can be used to load |
Johnni Winther
2014/10/10 10:28:56
'for that' -> 'that'.
ahe
2014/10/13 11:47:35
Done.
| |
6 /// | 6 /// code. |
7 /// This test opens Try Dart! in an iframe. When opened the first time, Try | 7 library trydart.test.sandbox; |
8 /// Dart! will display a simple hello-world example, color tokens, compile the | |
9 /// example, and run the result. We've instrumented Try Dart! to use | |
10 /// window.parent.postMessage when the running program prints anything. So this | |
11 /// test just waits for a "Hello, World!" message. | |
12 library trydart.end_to_end_test; | |
13 | 8 |
14 import 'dart:html'; | 9 import 'dart:html'; |
15 import 'dart:async'; | 10 import 'dart:async'; |
16 | 11 |
17 // TODO(ahe): Remove this import if issue 17936 is fixed. | 12 // TODO(ahe): Remove this import if issue 17936 is fixed. |
18 import 'dart:js' as hack; | 13 import 'dart:js' as hack; |
19 | 14 |
20 import 'package:async_helper/async_helper.dart'; | 15 import 'package:expect/expect.dart' show |
16 Expect; | |
17 | |
18 final Listener listener = new Listener(); | |
21 | 19 |
22 void onError(String message, String filename, int lineno, [int colno, error]) { | 20 void onError(String message, String filename, int lineno, [int colno, error]) { |
23 if (filename != null && filename != "" && lineno != 0) { | 21 if (filename != null && filename != "" && lineno != 0) { |
24 if (colno != null && colno != 0) { | 22 if (colno != null && colno != 0) { |
25 message = '$filename:$lineno:$colno $message'; | 23 message = '$filename:$lineno:$colno $message'; |
26 } else { | 24 } else { |
27 message = '$filename:$lineno: $message'; | 25 message = '$filename:$lineno: $message'; |
28 } | 26 } |
29 } | 27 } |
30 if (error != null) { | 28 if (error != null) { |
31 // See: | 29 // See: |
32 // https://mikewest.org/2013/08/debugging-runtime-errors-with-window-onerror | 30 // https://mikewest.org/2013/08/debugging-runtime-errors-with-window-onerror |
33 var stack = error['stack']; | 31 var stack = error['stack']; |
34 if (stack != null) { | 32 if (stack != null) { |
35 message += '\n$stack'; | 33 message += '\n$stack'; |
36 } | 34 } |
37 } | 35 } |
38 message = "Error occurred in Try Dart iframe: $message"; | 36 message = "Error occurred in iframe: $message"; |
39 | 37 |
40 // Synchronous, easier to read when running the browser manually. | 38 // Synchronous, easier to read when running the browser manually. |
41 window.console.log(message); | 39 window.console.log(message); |
42 | 40 |
43 new Future(() { | 41 new Future(() { |
44 // Browsers ignore errors throw in event listeners (or from | 42 // Browsers ignore errors throw in event listeners (or from |
45 // window.onerror). | 43 // window.onerror). |
46 throw message; | 44 throw message; |
47 }); | 45 }); |
48 } | 46 } |
(...skipping 12 matching lines...) Expand all Loading... | |
61 // "iframe.contentWindow.addEventListener('error', ...)". The former seems | 59 // "iframe.contentWindow.addEventListener('error', ...)". The former seems |
62 // to provide more details on both Chrome and Firefox (which provides no | 60 // to provide more details on both Chrome and Firefox (which provides no |
63 // information at all in error events). | 61 // information at all in error events). |
64 contentWindowProxy['onerror'] = onError; | 62 contentWindowProxy['onerror'] = onError; |
65 } | 63 } |
66 | 64 |
67 void onIframeLoaded(Event event) { | 65 void onIframeLoaded(Event event) { |
68 installErrorHandlerOn(event.target); | 66 installErrorHandlerOn(event.target); |
69 } | 67 } |
70 | 68 |
71 void main() { | 69 IFrameElement appendIFrame(String src, Element element) { |
72 document.cookie = 'org-trydart-AutomatedTest=true;path=/'; | |
73 asyncStart(); | |
74 window.onMessage.listen((MessageEvent e) { | |
75 switch (e.data) { | |
76 case 'Hello, World!\n': | |
77 // Clear the DOM to work around a bug in test.dart. | |
78 document.body.nodes.clear(); | |
79 | |
80 // Clean up after ourselves. | |
81 window.localStorage.clear(); | |
82 | |
83 asyncEnd(); | |
84 break; | |
85 | |
86 case 'dart-calling-main': | |
87 case 'dart-main-done': | |
88 case 'unittest-suite-done': | |
89 case 'unittest-suite-fail': | |
90 case 'unittest-suite-success': | |
91 case 'unittest-suite-wait-for-done': | |
92 break; | |
93 | |
94 default: | |
95 throw 'Unexpected message: ${e.data}'; | |
96 } | |
97 }); | |
98 | |
99 // Clearing localStorage makes Try Dart! think it is opening for the first | |
100 // time. | |
101 window.localStorage.clear(); | |
102 | |
103 IFrameElement iframe = new IFrameElement() | 70 IFrameElement iframe = new IFrameElement() |
104 ..src = '/root_build/try_dartlang_org/index.html' | 71 ..src = src |
105 ..style.width = '90vw' | |
106 ..style.height = '90vh' | |
107 ..onLoad.listen(onIframeLoaded); | 72 ..onLoad.listen(onIframeLoaded); |
108 document.body.append(iframe); | 73 element.append(iframe); |
109 // Install an error handler both on the new iframe element, and when it has | 74 // Install an error handler both on the new iframe element, and when it has |
110 // fired the load event. That seems to matter according to some sources on | 75 // fired the load event. That seems to matter according to some sources on |
111 // stackoverflow. | 76 // stackoverflow. |
112 installErrorHandlerOn(iframe); | 77 installErrorHandlerOn(iframe); |
78 return iframe; | |
113 } | 79 } |
80 | |
81 class Listener { | |
82 Completer completer; | |
83 | |
84 String expectedMessage; | |
85 | |
86 void onMessage(MessageEvent e) { | |
87 String message = e.data; | |
88 if (expectedMessage == message) { | |
89 completer.complete(); | |
90 } else { | |
91 switch (message) { | |
92 case 'dart-calling-main': | |
93 case 'dart-main-done': | |
94 case 'unittest-suite-done': | |
95 case 'unittest-suite-fail': | |
96 case 'unittest-suite-success': | |
97 case 'unittest-suite-wait-for-done': | |
98 break; | |
99 | |
100 default: | |
101 completer.completeError('Unexpected message: "$message".'); | |
102 } | |
103 } | |
104 } | |
105 | |
106 Future expect(data) { | |
107 if (data is String) { | |
108 Expect.isTrue(completer == null || completer.isCompleted); | |
109 expectedMessage = message; | |
110 completer = new Completer(); | |
111 return completer.future; | |
112 } else if (data is Iterable) { | |
113 return Future.forEach(data, expect); | |
114 } else { | |
115 throw 'Unexpected data type: ${data.runtimeType}.'; | |
116 } | |
117 } | |
118 | |
119 void start() { | |
120 window.onMessage.listen(onMessage); | |
121 } | |
122 } | |
OLD | NEW |