PageRenderTime 55ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/cordova-android/test/androidTest/src/org/apache/cordova/test/MessageChannelMultiPageTest.java

https://gitlab.com/blocknotary/IonicInterviews
Java | 110 lines | 54 code | 16 blank | 40 comment | 2 complexity | 871eb9cca0a17e42496b43b36752b1c0 MD5 | raw file
  1. package org.apache.cordova.test;
  2. /*
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. *
  21. */
  22. import android.view.KeyEvent;
  23. import android.view.inputmethod.BaseInputConnection;
  24. import org.apache.cordova.CallbackContext;
  25. import org.apache.cordova.CordovaWebViewImpl;
  26. import org.apache.cordova.PluginManager;
  27. import java.lang.reflect.Field;
  28. import java.lang.reflect.Method;
  29. public class MessageChannelMultiPageTest extends BaseCordovaIntegrationTest {
  30. private static final String START_URL = "file:///android_asset/www/backbuttonmultipage/index.html";
  31. @Override
  32. public void setUp() throws Exception {
  33. super.setUp();
  34. setUpWithStartUrl(START_URL);
  35. }
  36. //test that after a page load the cached callback id and the live callback id match
  37. //this is to prevent a regression
  38. //the issue was that CordovaWebViewImpl's cached instance of CoreAndroid would become stale on page load
  39. //this is because the cached instance was not being cleared when the pluginManager was reset on page load
  40. //the plugin manager would get a new instance which would be updated with a new callback id
  41. //the cached instance's message channel callback id would become stale
  42. //effectively this caused message channel events to not be delivered
  43. public void testThatCachedCallbackIdIsValid() throws Throwable {
  44. Class cordovaWebViewImpl = CordovaWebViewImpl.class;
  45. //send a test event - this initializes cordovaWebViewImpl.appPlugin (the cached instance of CoreAndroid)
  46. Method method = cordovaWebViewImpl.getDeclaredMethod("sendJavascriptEvent", String.class);
  47. method.setAccessible(true);
  48. method.invoke(cordovaWebView, "testEvent");
  49. sleep(1000);
  50. //load a page - this resets the plugin manager and nulls cordovaWebViewImpl.appPlugin
  51. //(previously this resets plugin manager but did not null cordovaWebViewImpl.appPlugin, leading to the issue)
  52. runTestOnUiThread(new Runnable() {
  53. public void run() {
  54. cordovaWebView.loadUrl(START_URL);
  55. }
  56. });
  57. assertEquals(START_URL, testActivity.onPageFinishedUrl.take());
  58. //send a test event - this initializes cordovaWebViewImpl.appPlugin (the cached instance of CoreAndroid)
  59. method.invoke(cordovaWebView, "testEvent");
  60. sleep(1000);
  61. //get reference to package protected class CoreAndroid
  62. Class coreAndroid = Class.forName("org.apache.cordova.CoreAndroid");
  63. //get cached CoreAndroid
  64. Field appPluginField = cordovaWebViewImpl.getDeclaredField("appPlugin");
  65. appPluginField.setAccessible(true);
  66. Object cachedAppPlugin = appPluginField.get(cordovaWebView);
  67. //get cached CallbackContext
  68. Field messageChannelField = coreAndroid.getDeclaredField("messageChannel");
  69. messageChannelField.setAccessible(true);
  70. CallbackContext cachedCallbackContext = (CallbackContext) messageChannelField.get(cachedAppPlugin);
  71. //get live CoreAndroid
  72. PluginManager pluginManager = MessageChannelMultiPageTest.this.cordovaWebView.getPluginManager();
  73. Field coreAndroidPluginNameField = coreAndroid.getField("PLUGIN_NAME");
  74. String coreAndroidPluginName = (String) coreAndroidPluginNameField.get(null);
  75. Object liveAppPlugin = pluginManager.getPlugin(coreAndroidPluginName);
  76. //get live CallbackContext
  77. CallbackContext liveCallbackContext = (CallbackContext) messageChannelField.get(liveAppPlugin);
  78. //get callback id from live callbackcontext
  79. String liveCallbackId = (liveCallbackContext != null) ? liveCallbackContext.getCallbackId() : null;
  80. //get callback id from cached callbackcontext
  81. String cachedCallbackId = (cachedCallbackContext != null) ? cachedCallbackContext.getCallbackId() : null;
  82. //verify that the live message channel has been initialized
  83. assertNotNull(liveCallbackId);
  84. //verify that the cached message channel and the live message channel have the same id
  85. assertEquals(liveCallbackId, cachedCallbackId);
  86. }
  87. private void sleep(int timeout) {
  88. try {
  89. Thread.sleep(timeout);
  90. } catch (InterruptedException e) {
  91. fail("Unexpected Timeout");
  92. }
  93. }
  94. }