Android InputMethodService的仪表测试用例
我已经扩展了Android InputMethodService的仪表测试用例,android,android-input-method,android-service-binding,android-instrumentation,servicetestcase,Android,Android Input Method,Android Service Binding,Android Instrumentation,Servicetestcase,我已经扩展了InputMethodService类来创建自定义IME。然而,我正在努力编写有效的测试用例来验证行为。以前,可以使用ServiceTestCase对服务进行测试。但是它似乎已经被弃用了,新的格式看起来像。现在,在给定的指导原则中,我正在努力解决以下问题: CustomKeyboardService service = ((CustomKeyboardService.LocalBinder) binder).getService(); 由于我正在扩展Inpu
InputMethodService
类来创建自定义IME。然而,我正在努力编写有效的测试用例来验证行为。以前,可以使用ServiceTestCase
对服务进行测试。但是它似乎已经被弃用了,新的格式看起来像。现在,在给定的指导原则中,我正在努力解决以下问题:
CustomKeyboardService service =
((CustomKeyboardService.LocalBinder) binder).getService();
由于我正在扩展InputMethodService
,它已经抽象了IBinder
,如何获取LocalBinder
以运行此代码段?当前,此代码段引发以下异常:
java.lang.ClassCastException:
无法将android.inputmethodservice.IIInputMethodWrapper强制转换为
com.osrc.zdar.customkeyboard.CustomKeyboardService$LocalBinder
扩展类如下所示:
public class CustomKeyboardService extends InputMethodService {
// Some keyboard related stuff
public class LocalBinder extends Binder {
public CustomKeyboardService getService() {
// Return this instance of LocalService so clients can call public methods.
return CustomKeyboardService.this;
}
}
// Some keyboard related stuff
}
如何扩展我的自定义类,以便CustomKeyboardService服务
=((CustomKeyboardService.LocalBinder)binder).getService()代码>不返回错误
以下是我的测试用例代码:
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest2 {
@Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();
@Test
public void testWithBoundService() throws TimeoutException {
// Create the service Intent.
Intent serviceIntent =
new Intent(InstrumentationRegistry.getTargetContext(), CustomKeyboardService.class);
// Bind the service and grab a reference to the binder.
IBinder binder = mServiceRule.bindService(serviceIntent);
// Get the reference to the service, or you can call public methods on the binder directly.
// This Line throws the error
CustomKeyboardService service =
((CustomKeyboardService.LocalBinder) binder).getService();
}
}
您还可以在Github上查看完整的源代码,并提交一份带有有效检测测试用例的PR。同样的问题也发生在我身上,请查看下面链接的解决方案。
已更新链接中的代码段:
@Rule
public final ServiceTestRule mServiceRule = new ServiceTestRule();
private MyKeyboard retrieveMyKeyboardInstance(IBinder binder) {
try {
Class wrapperClass = Class.forName("android.inputmethodservice.IInputMethodWrapper");
Field mTargetField = wrapperClass.getDeclaredField("mTarget");
mTargetField.setAccessible(true);
WeakReference<MyKeyboard> weakReference = (WeakReference<MyKeyboard>) mTargetField.get(binder);
return weakReference.get();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void validateEditTextWithKeyboardInput() throws TimeoutException {
...
Intent serviceIntent = new Intent(InstrumentationRegistry.getTargetContext(), MyKeyboard.class);
IBinder binder = mServiceRule.bindService(serviceIntent);
MyKeyboard keyboard = retrieveMyKeyboardInstance(binder);
...
}
@规则
public final ServiceTestRule mServiceRule=新ServiceTestRule();
私有MyKeyboard检索MyKeyboardInstance(IBinder活页夹){
试一试{
Class wrapperClass=Class.forName(“android.inputmethodservice.IIInputMethodWrapper”);
Field mTargetField=wrapperClass.getDeclaredField(“mTarget”);
mTargetField.setAccessible(true);
WeakReference WeakReference=(WeakReference)mTargetField.get(binder);
返回weakReference.get();
}捕获(例外e){
抛出新的运行时异常(e);
}
}
public void validateEditttextWithKeyboardInput()引发TimeoutException{
...
Intent serviceIntent=new Intent(InstrumentationRegistry.getTargetContext(),MyKeyboard.class);
IBinder binder=mServiceRule.bindService(serviceIntent);
MyKeyboard=retrieveMyKeyboardInstance(活页夹);
...
}
发件人: