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 /// Whitebox integration/end-to-end test of Try Dart! site. |
6 /// | 6 /// |
7 /// This test opens Try Dart! in an iframe. When opened the first time, Try | 7 /// This test opens Try Dart! in an iframe. When opened the first time, Try |
8 /// Dart! will display a simple hello-world example, color tokens, compile the | 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 | 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 | 10 /// window.parent.postMessage when the running program prints anything. So this |
11 /// test just waits for a "Hello, World!" message. | 11 /// test just waits for a "Hello, World!" message. |
12 library trydart.end_to_end_test; | 12 library trydart.end_to_end_test; |
13 | 13 |
14 import 'dart:html'; | 14 import 'dart:html'; |
15 import 'dart:async'; | |
16 | 15 |
17 // TODO(ahe): Remove this import if issue 17936 is fixed. | 16 import 'package:async_helper/async_helper.dart' show |
18 import 'dart:js' as hack; | 17 asyncTest; |
19 | 18 |
20 import 'package:async_helper/async_helper.dart'; | 19 import 'sandbox.dart' show |
| 20 appendIFrame, |
| 21 listener; |
21 | 22 |
22 void onError(String message, String filename, int lineno, [int colno, error]) { | 23 void main() => asyncTest(() { |
23 if (filename != null && filename != "" && lineno != 0) { | 24 listener.start(); |
24 if (colno != null && colno != 0) { | |
25 message = '$filename:$lineno:$colno $message'; | |
26 } else { | |
27 message = '$filename:$lineno: $message'; | |
28 } | |
29 } | |
30 if (error != null) { | |
31 // See: | |
32 // https://mikewest.org/2013/08/debugging-runtime-errors-with-window-onerror | |
33 var stack = error['stack']; | |
34 if (stack != null) { | |
35 message += '\n$stack'; | |
36 } | |
37 } | |
38 message = "Error occurred in Try Dart iframe: $message"; | |
39 | 25 |
40 // Synchronous, easier to read when running the browser manually. | 26 // Disable analytics for testing. |
41 window.console.log(message); | |
42 | |
43 new Future(() { | |
44 // Browsers ignore errors throw in event listeners (or from | |
45 // window.onerror). | |
46 throw message; | |
47 }); | |
48 } | |
49 | |
50 void installErrorHandlerOn(IFrameElement iframe) { | |
51 // This method uses dart:js to install an error event handler on the content | |
52 // window of [iframe]. This is a workaround for http://dartbug.com/17936. | |
53 var iframeProxy = new hack.JsObject.fromBrowserObject(iframe); | |
54 var contentWindowProxy = iframeProxy['contentWindow']; | |
55 if (contentWindowProxy == null) { | |
56 print('No contentWindow in iframe'); | |
57 throw 'No contentWindow in iframe'; | |
58 } | |
59 | |
60 // Note: we have two options, use "iframe.contentWindow.onerror = ..." or | |
61 // "iframe.contentWindow.addEventListener('error', ...)". The former seems | |
62 // to provide more details on both Chrome and Firefox (which provides no | |
63 // information at all in error events). | |
64 contentWindowProxy['onerror'] = onError; | |
65 } | |
66 | |
67 void onIframeLoaded(Event event) { | |
68 installErrorHandlerOn(event.target); | |
69 } | |
70 | |
71 void main() { | |
72 document.cookie = 'org-trydart-AutomatedTest=true;path=/'; | 27 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 | 28 |
99 // Clearing localStorage makes Try Dart! think it is opening for the first | 29 // Clearing localStorage makes Try Dart! think it is opening for the first |
100 // time. | 30 // time. |
101 window.localStorage.clear(); | 31 window.localStorage.clear(); |
102 | 32 |
103 IFrameElement iframe = new IFrameElement() | 33 IFrameElement iframe = |
104 ..src = '/root_build/try_dartlang_org/index.html' | 34 appendIFrame('/root_build/try_dartlang_org/index.html', document.body) |
105 ..style.width = '90vw' | 35 ..style.width = '90vw' |
106 ..style.height = '90vh' | 36 ..style.height = '90vh'; |
107 ..onLoad.listen(onIframeLoaded); | 37 |
108 document.body.append(iframe); | 38 return listener.expect('Hello, World!\n').then((_) { |
109 // Install an error handler both on the new iframe element, and when it has | 39 // Remove the iframe to work around a bug in test.dart. |
110 // fired the load event. That seems to matter according to some sources on | 40 iframe.remove(); |
111 // stackoverflow. | 41 |
112 installErrorHandlerOn(iframe); | 42 // Clean up after ourselves. |
113 } | 43 window.localStorage.clear(); |
| 44 }); |
| 45 }); |
OLD | NEW |