Bez popisu

WebView.integration.js 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * Copyright (c) 2018-present, Infinite Red, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @format
  8. */
  9. 'use strict';
  10. const React = require('react');
  11. const ReactNative = require('react-native');
  12. const { WebView } = ReactNative;
  13. const { TestModule } = ReactNative.NativeModules;
  14. class WebViewTest extends React.Component {
  15. render() {
  16. let firstMessageReceived = false;
  17. let secondMessageReceived = false;
  18. function processMessage(e) {
  19. const message = e.nativeEvent.data;
  20. if (message === 'First') {
  21. firstMessageReceived = true;
  22. }
  23. if (message === 'Second') {
  24. secondMessageReceived = true;
  25. }
  26. // got both messages
  27. if (firstMessageReceived && secondMessageReceived) {
  28. TestModule.markTestPassed(true);
  29. }
  30. // wait for next message
  31. else if (firstMessageReceived && !secondMessageReceived) {
  32. return;
  33. }
  34. // first message got lost
  35. else if (!firstMessageReceived && secondMessageReceived) {
  36. throw new Error('First message got lost');
  37. }
  38. }
  39. const html =
  40. 'Hello world' +
  41. '<script>' +
  42. "window.setTimeout(function(){window.postMessage('First'); window.postMessage('Second')}, 0)" +
  43. '</script>';
  44. // fail if messages didn't get through;
  45. window.setTimeout(function () {
  46. throw new Error(
  47. firstMessageReceived
  48. ? 'Both messages got lost'
  49. : 'Second message got lost',
  50. );
  51. }, 10000);
  52. const source = {
  53. html: html,
  54. };
  55. return (
  56. <WebView
  57. source={source}
  58. onMessage={processMessage}
  59. originWhitelist={['about:blank']}
  60. />
  61. );
  62. }
  63. }
  64. WebViewTest.displayName = 'WebViewTest';
  65. module.exports = WebViewTest;