Search in sources :

Example 1 with CordovaWebViewImpl

use of org.apache.cordova.CordovaWebViewImpl in project cordova-android by apache.

the class MessageChannelMultipageTest method testThatCachedCallbackIdIsValid.

//test that after a page load the cached callback id and the live callback id match
//this is to prevent a regression
//the issue was that CordovaWebViewImpl's cached instance of CoreAndroid would become stale on page load
//this is because the cached instance was not being cleared when the pluginManager was reset on page load
//the plugin manager would get a new instance which would be updated with a new callback id
//the cached instance's message channel callback id would become stale
//effectively this caused message channel events to not be delivered
@Test
public void testThatCachedCallbackIdIsValid() throws Throwable {
    final CordovaWebView cordovaWebView = testActivity.getWebInterface();
    Class cordovaWebViewImpl = CordovaWebViewImpl.class;
    //send a test event - this initializes cordovaWebViewImpl.appPlugin (the cached instance of CoreAndroid)
    Method method = cordovaWebViewImpl.getDeclaredMethod("sendJavascriptEvent", String.class);
    method.setAccessible(true);
    method.invoke(cordovaWebView, "testEvent");
    sleep(1000);
    //load a page - this resets the plugin manager and nulls cordovaWebViewImpl.appPlugin
    //(previously this resets plugin manager but did not null cordovaWebViewImpl.appPlugin, leading to the issue)
    mActivityRule.runOnUiThread(new Runnable() {

        public void run() {
            cordovaWebView.loadUrl(START_URL);
        }
    });
    assertEquals(START_URL, testActivity.onPageFinishedUrl.take());
    //send a test event - this initializes cordovaWebViewImpl.appPlugin (the cached instance of CoreAndroid)
    method.invoke(cordovaWebView, "testEvent");
    sleep(1000);
    //get reference to package protected class CoreAndroid
    Class coreAndroid = Class.forName("org.apache.cordova.CoreAndroid");
    //get cached CoreAndroid
    Field appPluginField = cordovaWebViewImpl.getDeclaredField("appPlugin");
    appPluginField.setAccessible(true);
    Object cachedAppPlugin = appPluginField.get(cordovaWebView);
    //get cached CallbackContext
    Field messageChannelField = coreAndroid.getDeclaredField("messageChannel");
    messageChannelField.setAccessible(true);
    CallbackContext cachedCallbackContext = (CallbackContext) messageChannelField.get(cachedAppPlugin);
    //get live CoreAndroid
    PluginManager pluginManager = cordovaWebView.getPluginManager();
    Field coreAndroidPluginNameField = coreAndroid.getField("PLUGIN_NAME");
    String coreAndroidPluginName = (String) coreAndroidPluginNameField.get(null);
    Object liveAppPlugin = pluginManager.getPlugin(coreAndroidPluginName);
    //get live CallbackContext
    CallbackContext liveCallbackContext = (CallbackContext) messageChannelField.get(liveAppPlugin);
    //get callback id from live callbackcontext
    String liveCallbackId = (liveCallbackContext != null) ? liveCallbackContext.getCallbackId() : null;
    //get callback id from cached callbackcontext
    String cachedCallbackId = (cachedCallbackContext != null) ? cachedCallbackContext.getCallbackId() : null;
    //verify that the live message channel has been initialized
    assertNotNull(liveCallbackId);
    //verify that the cached message channel and the live message channel have the same id
    assertEquals(liveCallbackId, cachedCallbackId);
}
Also used : PluginManager(org.apache.cordova.PluginManager) Field(java.lang.reflect.Field) CallbackContext(org.apache.cordova.CallbackContext) CordovaWebView(org.apache.cordova.CordovaWebView) Method(java.lang.reflect.Method) CordovaWebViewImpl(org.apache.cordova.CordovaWebViewImpl) Test(org.junit.Test)

Example 2 with CordovaWebViewImpl

use of org.apache.cordova.CordovaWebViewImpl in project cordova-android by apache.

the class EmbeddedWebViewActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Set up the webview
    ConfigXmlParser parser = new ConfigXmlParser();
    parser.parse(this);
    SystemWebView webView = (SystemWebView) findViewById(R.id.cordovaWebView);
    webInterface = new CordovaWebViewImpl(new SystemWebViewEngine(webView));
    webInterface.init(cordovaInterface, parser.getPluginEntries(), parser.getPreferences());
    webView.loadUrl(parser.getLaunchUrl());
}
Also used : SystemWebView(org.apache.cordova.engine.SystemWebView) SystemWebViewEngine(org.apache.cordova.engine.SystemWebViewEngine) ConfigXmlParser(org.apache.cordova.ConfigXmlParser) CordovaWebViewImpl(org.apache.cordova.CordovaWebViewImpl)

Aggregations

CordovaWebViewImpl (org.apache.cordova.CordovaWebViewImpl)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 CallbackContext (org.apache.cordova.CallbackContext)1 ConfigXmlParser (org.apache.cordova.ConfigXmlParser)1 CordovaWebView (org.apache.cordova.CordovaWebView)1 PluginManager (org.apache.cordova.PluginManager)1 SystemWebView (org.apache.cordova.engine.SystemWebView)1 SystemWebViewEngine (org.apache.cordova.engine.SystemWebViewEngine)1 Test (org.junit.Test)1