Nenhuma descrição

WebView.integration.js 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. 'use strict';
  2. const React = require('react');
  3. const ReactNative = require('react-native');
  4. const { WebView } = ReactNative;
  5. const { TestModule } = ReactNative.NativeModules;
  6. class WebViewTest extends React.Component {
  7. render() {
  8. let firstMessageReceived = false;
  9. let secondMessageReceived = false;
  10. function processMessage(e) {
  11. const message = e.nativeEvent.data;
  12. if (message === 'First') {
  13. firstMessageReceived = true;
  14. }
  15. if (message === 'Second') {
  16. secondMessageReceived = true;
  17. }
  18. // got both messages
  19. if (firstMessageReceived && secondMessageReceived) {
  20. TestModule.markTestPassed(true);
  21. }
  22. // wait for next message
  23. else if (firstMessageReceived && !secondMessageReceived) {
  24. return;
  25. }
  26. // first message got lost
  27. else if (!firstMessageReceived && secondMessageReceived) {
  28. throw new Error('First message got lost');
  29. }
  30. }
  31. const html =
  32. 'Hello world' +
  33. '<script>' +
  34. "window.setTimeout(function(){window.postMessage('First'); window.postMessage('Second')}, 0)" +
  35. '</script>';
  36. // fail if messages didn't get through;
  37. window.setTimeout(function () {
  38. throw new Error(
  39. firstMessageReceived
  40. ? 'Both messages got lost'
  41. : 'Second message got lost',
  42. );
  43. }, 10000);
  44. const source = {
  45. html: html,
  46. };
  47. return (
  48. <WebView
  49. source={source}
  50. onMessage={processMessage}
  51. originWhitelist={['about:blank']}
  52. />
  53. );
  54. }
  55. }
  56. WebViewTest.displayName = 'WebViewTest';
  57. module.exports = WebViewTest;