【问题标题】:Instrumentation test cases for InputMethodServiceInputMethodService 的仪器测试用例
【发布时间】:2018-10-23 16:44:45
【问题描述】:

我扩展了 InputMethodService 类来创建我的自定义 IME。但是,我正在努力编写有效的 Instrumentation 测试用例来验证行为。以前Service 可以使用ServiceTestCase<YourServiceClass> 进行测试。但它似乎已被弃用,新格式看起来像this。现在在给定的指导方针中,我正在努力解决这个问题:

CustomKeyboardService service =
            ((CustomKeyboardService.LocalBinder) binder).getService();

由于我在扩展InputMethodService,它已经抽象了IBinder,我怎样才能获得LocalBinder来让这个sn-p运行呢?目前,这个 sn-p 抛出以下异常:

java.lang.ClassCastException: android.inputmethodservice.IInputMethodWrapper 不能被强制转换为 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 service = ((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 上查看 OimeKeyboard 以获取完整源代码,并提交带有有效仪器测试用例的 PR。

【问题讨论】:

    标签: android android-input-method android-service-binding android-instrumentation servicetestcase


    【解决方案1】:

    同样的问题发生在我身上,请查看以下链接的解决方案。
    从链接更新代码sn-ps:

    @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);
    
        ...
    }
    

    发件人:https://github.com/sealor/prototype-Android-Espresso-Keyboard-Testing

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 2014-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多