我知道cordova插件测试框架将有助于测试任何cordova项目。我正在为Android平台开发一个自定义的摄像头cordova插件。我想为我的自定义相机安卓插件代码写一些Junit/Instrumentation测试用例。我面临的问题是,我无法从我的测试类创建CordovaInterface
、CordovaWebView
对象。
我是否可以从我的测试类中创建CordovaInterface
、CordovaWebView
对象,并将它们作为我的自定义相机Android插件excute
方法的参数传递。
我想避免使用cordova插件测试框架的js级别的单元测试用例,并编写一些Junit测试用例(因为我可以访问我的相机cordova插入使用的大多数内部类)。如果这种做法不对,请纠正我。
首先,将CordovaLib作为库复制到您的插件项目中。在你的测试类中添加这样的内容:
首先,一个活动,不要忘记将其添加到AndroidMainfest.xml中进行测试。
public class TestActivity extends CordovaActivity {
CordovaWebView getWebView(){
return this.appView;
}
CordovaInterface getInterface(){
return this.cordovaInterface;
}
public void init(){
super.init();
}
}
在你的测试课上:
@Rule
public ActivityTestRule<CordovaActivity> mActivityRule =
new ActivityTestRule<CordovaActivity>(CordovaActivity.class);
private CordovaWebView wv;
private CordovaInterface cordovaInterface;
private YourCordovaPlugin km;
private TestActivity activity;
@Before
public void Init() throws InterruptedException {
km = new KM1930CordovaPlugin();
activity = mActivityRule.getActivity();
Runnable action = new Runnable() {
@Override
public void run() {
activity.init();
wv = mActivityRule.getActivity().getWebView();
cordovaInterface = mActivityRule.getActivity().getInterface();
km = new YourCordovaPlugin();
km.initialize(cordovaInterface,wv);
wv.getPluginManager().addService(new PluginEntry("YourServiceName",km));
synchronized (this) {
this.notify();
}
}
};
synchronized( action ) {
activity.runOnUiThread(action);
action.wait() ;
}
}
然后您可以通过调用execute
方法来添加您的测试方法和调用方法。
@Test
public void TestBluetooth() throws JSONException {
this.km.execute("echo","[]",new CallbackContext("0",wv));
}
在我的情况下,我不得不修改一下这种方法:
首先,我的TestActivity
必须与使用<action android:name="android.intent.action.MAIN" />
intent-filter
的活动处于同一级别。
然后,在src/androidTest/java.com/your/package/name中创建测试文件(在我的例子中是MyTestActivityTest.java
)
然后,在测试本身:
import android.support.test.rule.ActivityTestRule;
import android.support.test.rule.UiThreadTestRule;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Before;
import org.junit.Rule;
import org.junit.runner.RunWith;
import java.util.concurrent.CountDownLatch;
@RunWith(AndroidJUnit4.class)
public class MyTestActivityTest {
@Rule
public ActivityTestRule<TestActivity> mActivityRule = new ActivityTestRule<>(TestActivity.class);
@Rule
public UiThreadTestRule uiThreadTestRule = new UiThreadTestRule();
private YourPlugin mockYourPlugin;
private CordovaWebView cordovaWebView;
private CordovaInterface cordovaInterface;
private TestActivity activity;
@Before
public void setUp() throws Throwable {
activity = mActivityRule.getActivity();
assertNotNull(activity);
final CountDownLatch signal = new CountDownLatch(1); --> this one is the important! the sync part got stucked!
Runnable action = () -> {
activity.init();
cordovaWebView = mActivityRule.getActivity().getWebView();
cordovaInterface = mActivityRule.getActivity().getInterface();
mockYourPlugin = new YourPlugin();
mockYourPlugin.initialize(cordovaInterface, cordovaWebView);
cordovaWebView.getPluginManager().addService(new PluginEntry("YourServiceName", mockBankId));
signal.countDown();// notify the count down latch, i.e release it
};
uiThreadTestRule.runOnUiThread(action);
signal.await();// wait for callback
}
@Test
public void TestBluetooth() throws JSONException {
this.mockYourPlugin.execute("echo","[]",new CallbackContext("0", cordovaWebView));
}
}
和我的build.gradle
文件,我有这个依赖项:
// JUnit 4 framework
implementation 'junit:junit:4.12'
// required if you want to use Mockito for unit tests
implementation 'org.mockito:mockito-core:2.13.0'
implementation 'org.mockito:mockito-android:2.8.47'
implementation 'com.android.support.test:rules:1.0.2'
别忘了把它也放在build.gradle
文件中:
defaultConfig {
...
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
...
}
我真的希望这能帮助