Chromium Code Reviews| 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. | 17 // TODO(ahe): Remove this import if issue 17936 is fixed. |
| 18 import 'dart:js' as hack; | 18 import 'dart:js' as hack; |
| 19 | 19 |
| 20 import 'package:async_helper/async_helper.dart'; | 20 import 'package:async_helper/async_helper.dart'; |
| 21 | 21 |
| 22 void onError(String message, String filename, int lineno) { | 22 void onError(String message, String filename, int lineno, [int colno, error]) { |
| 23 if (filename != null) { | 23 if (filename != null && filename != "" && lineno != 0) { |
| 24 message = '$filename:$lineno: $message'; | 24 if (colno != null && colno != 0) { |
| 25 message = '$filename:$lineno:$colno $message'; | |
| 26 } else { | |
| 27 message = '$filename:$lineno: $message'; | |
| 28 } | |
| 25 } | 29 } |
| 26 print("Error occurred in Try Dart iframe: $message"); | 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 | |
| 40 // Synchronous, easier to read when running the browser manually. | |
| 41 window.console.log(message); | |
| 42 | |
| 27 new Future(() { | 43 new Future(() { |
| 28 // Chrome seems to not call window.onerror when you throw in response to an | 44 // Browsers ignore errors throw in event listeners (or from |
| 29 // error event. So we throw the error in a future. | 45 // window.onerror). |
| 30 throw 'Error from iframe: $message'; | 46 throw message; |
| 31 }); | 47 }); |
| 32 } | 48 } |
| 33 | 49 |
| 34 void installErrorHandlerOn(IFrameElement iframe) { | 50 void installErrorHandlerOn(IFrameElement iframe) { |
| 35 // This method uses dart:js to install an error event handler on the content | 51 // This method uses dart:js to install an error event handler on the content |
| 36 // window of [iframe]. This is a workaround for http://dartbug.com/17936. | 52 // window of [iframe]. This is a workaround for http://dartbug.com/17936. |
| 37 var iframeProxy = new hack.JsObject.fromBrowserObject(iframe); | 53 var iframeProxy = new hack.JsObject.fromBrowserObject(iframe); |
| 38 var contentWindowProxy = iframeProxy['contentWindow']; | 54 var contentWindowProxy = iframeProxy['contentWindow']; |
| 39 if (contentWindowProxy == null) { | 55 if (contentWindowProxy == null) { |
| 40 print('No contentWindow in iframe'); | 56 print('No contentWindow in iframe'); |
| 41 throw 'No contentWindow in iframe'; | 57 throw 'No contentWindow in iframe'; |
| 42 } | 58 } |
| 43 contentWindowProxy.callMethod('addEventListener', ['error', (eventProxy) { | |
| 44 onError( | |
| 45 eventProxy['message'], eventProxy['filename'], eventProxy['lineno']); | |
| 46 }]); | |
| 47 contentWindowProxy['onerror'] = onError; | 59 contentWindowProxy['onerror'] = onError; |
| 60 | |
| 61 // Note: we have two options, use "iframe.contentWindow.onerror = ..." or | |
|
ricow1
2014/05/08 05:33:10
move this comment above the code line doing it?
| |
| 62 // "iframe.contentWindow.addEventListener('error', ...)". The former seems | |
| 63 // to provide more details on both Chrome and Firefox (which provides no | |
| 64 // information at all in error events). | |
| 48 } | 65 } |
| 49 | 66 |
| 50 void onIframeLoaded(Event event) { | 67 void onIframeLoaded(Event event) { |
| 51 installErrorHandlerOn(event.target); | 68 installErrorHandlerOn(event.target); |
| 52 } | 69 } |
| 53 | 70 |
| 54 void main() { | 71 void main() { |
| 55 asyncStart(); | 72 asyncStart(); |
| 56 window.onMessage.listen((MessageEvent e) { | 73 window.onMessage.listen((MessageEvent e) { |
| 57 if (e.data == 'Hello, World!\n') { | 74 if (e.data == 'Hello, World!\n') { |
| 58 // Clear the DOM to work around a bug in test.dart. | 75 // Clear the DOM to work around a bug in test.dart. |
| 59 document.body.nodes.clear(); | 76 document.body.nodes.clear(); |
| 60 | 77 |
| 61 // Clean up after ourselves. | 78 // Clean up after ourselves. |
| 62 window.localStorage.clear(); | 79 window.localStorage.clear(); |
| 63 | 80 |
| 64 asyncSuccess(null); | 81 asyncEnd(); |
| 65 } else { | |
| 66 window.console.dir(e.data); | |
| 67 } | 82 } |
| 68 }); | 83 }); |
| 69 | 84 |
| 70 // Clearing localStorage makes Try Dart! think it is opening for the first | 85 // Clearing localStorage makes Try Dart! think it is opening for the first |
| 71 // time. | 86 // time. |
| 72 window.localStorage.clear(); | 87 window.localStorage.clear(); |
| 73 | 88 |
| 74 IFrameElement iframe = new IFrameElement() | 89 IFrameElement iframe = new IFrameElement() |
| 90 ..src = '/root_build/try_dartlang_org/index.html' | |
| 75 ..style.width = '90vw' | 91 ..style.width = '90vw' |
| 76 ..style.height = '90vh' | 92 ..style.height = '90vh' |
| 77 ..onLoad.listen(onIframeLoaded); | 93 ..onLoad.listen(onIframeLoaded); |
| 78 document.body.append(iframe); | 94 document.body.append(iframe); |
| 79 // Install an error handler both on the new iframe element, and when it has | 95 // Install an error handler both on the new iframe element, and when it has |
| 80 // fired the load event. That seems to matter according to some sources on | 96 // fired the load event. That seems to matter according to some sources on |
| 81 // stackoverflow. | 97 // stackoverflow. |
| 82 installErrorHandlerOn(iframe); | 98 installErrorHandlerOn(iframe); |
| 83 | |
| 84 iframe.src = '/root_build/try_dartlang_org/index.html'; | |
| 85 } | 99 } |
| OLD | NEW |