I have extended the InputMethodService
class to create my a custom IME. However, I am struggling to write valid Instrumentation test cases to verify the behavior. Previously the Service
, could be tested using ServiceTestCase<YourServiceClass>
. But it seems to have been deprecated, and the new format looks like this. Now in the given guidelines, I am struggling with this snippet:
CustomKeyboardService service =
((CustomKeyboardService.LocalBinder) binder).getService();
Since I am extending InputMethodService
, it has already abstracted the IBinder
, how can I obtain LocalBinder
to get this snippet running? Currently, this snippet throws following exception:
java.lang.ClassCastException: android.inputmethodservice.IInputMethodWrapper cannot be cast to com.osrc.zdar.customkeyboard.CustomKeyboardService$LocalBinder
The extended class looks like:
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
}
How can I extend my custom class such that CustomKeyboardService service
= ((CustomKeyboardService.LocalBinder) binder).getService();
doesn't returns error?
Here is my test case code:
@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();
}
}
You can also check out OimeKeyboard on Github for full source code and submit a PR with a valid instrumentation test case.