| 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'; | 15 import 'dart:async'; |
| 16 | 16 |
| 17 // TODO(ahe): Remove this import if issue 17936 is fixed. |
| 18 import 'dart:js' as hack; |
| 19 |
| 17 import 'package:async_helper/async_helper.dart'; | 20 import 'package:async_helper/async_helper.dart'; |
| 18 | 21 |
| 22 void installErrorHandlerOn(IFrameElement iframe) { |
| 23 // This method uses dart:js to install an error event handler on the content |
| 24 // window of [iframe]. This is a workaround for http://dartbug.com/17936. |
| 25 var iframeProxy = new hack.JsObject.fromBrowserObject(iframe); |
| 26 var contentWindowProxy = iframeProxy['contentWindow']; |
| 27 if (contentWindowProxy == null) { |
| 28 print('No contentWindow in iframe'); |
| 29 throw 'No contentWindow in iframe'; |
| 30 } |
| 31 contentWindowProxy.callMethod('addEventListener', ['error', (eventProxy) { |
| 32 String filename = eventProxy['filename']; |
| 33 int lineno = eventProxy['lineno']; |
| 34 String message = eventProxy['message']; |
| 35 print("Error occurred in iframe: $message"); |
| 36 new Future(() { |
| 37 // Chrome seems to not call window.onerror when you throw in response to |
| 38 // an error event. So we throw the error in a future. |
| 39 throw 'Error from iframe: $filename:$lineno: $message'; |
| 40 }); |
| 41 }]); |
| 42 } |
| 43 |
| 44 void onIframeLoaded(ErrorEvent event) { |
| 45 installErrorHandlerOn(event.target); |
| 46 } |
| 47 |
| 19 void main() { | 48 void main() { |
| 20 asyncStart(); | 49 asyncStart(); |
| 21 window.onMessage.listen((MessageEvent e) { | 50 window.onMessage.listen((MessageEvent e) { |
| 22 if (e.data == 'Hello, World!\n') { | 51 if (e.data == 'Hello, World!\n') { |
| 23 // Clear the DOM to work around a bug in test.dart. | 52 // Clear the DOM to work around a bug in test.dart. |
| 24 document.body.nodes.clear(); | 53 document.body.nodes.clear(); |
| 25 | 54 |
| 26 // Clean up after ourselves. | 55 // Clean up after ourselves. |
| 27 window.localStorage.clear(); | 56 window.localStorage.clear(); |
| 28 | 57 |
| 29 asyncSuccess(null); | 58 asyncSuccess(null); |
| 30 } else { | 59 } else { |
| 31 window.console.dir(e.data); | 60 window.console.dir(e.data); |
| 32 } | 61 } |
| 33 }); | 62 }); |
| 34 | 63 |
| 35 // Clearing localStorage makes Try Dart! think it is opening for the first | 64 // Clearing localStorage makes Try Dart! think it is opening for the first |
| 36 // time. | 65 // time. |
| 37 window.localStorage.clear(); | 66 window.localStorage.clear(); |
| 38 | 67 |
| 39 document.body.append(new IFrameElement() | 68 IFrameElement iframe = new IFrameElement() |
| 40 ..src = '/root_build/try_dartlang_org/index.html' | 69 ..src = '/root_build/try_dartlang_org/index.html' |
| 41 ..style.width = '90vw' | 70 ..style.width = '90vw' |
| 42 ..style.height = '90vh'); | 71 ..style.height = '90vh' |
| 72 ..onLoad.listen(onIframeLoaded); |
| 73 document.body.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); |
| 43 } | 78 } |
| OLD | NEW |