本文整理汇总了Java中org.apache.cordova.CallbackContext.getCallbackId方法的典型用法代码示例。如果您正苦于以下问题:Java CallbackContext.getCallbackId方法的具体用法?Java CallbackContext.getCallbackId怎么用?Java CallbackContext.getCallbackId使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.cordova.CallbackContext
的用法示例。
在下文中一共展示了CallbackContext.getCallbackId方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testThatCachedCallbackIdIsValid
import org.apache.cordova.CallbackContext; //导入方法依赖的package包/类
@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);
}