I have a basic version of Android InputMethodService that I am trying to write unit tests for. There are no Activites in my application, just an implementation of InputMethodService.
So far, I have a basic ServiceTestCase implementation that works fine:
SoftKeyboardTest.java
public class SoftKeyboardTest extends ServiceTestCase<SoftKeyboard> { @Override protected void setUp() throws Exception { super.setUp(); bindService(new Intent(this.getContext(), SoftKeyboard.class)); } public void testShowKeyboard() { this.getService().ShowKeyboard(); assertTrue(this.getService().GetKeyboardIsVisible()); } public void testInsertText() { String text = "Hello, world"; this.getService().InsertText(text); assertEquals(this.getService().ReadText(text.length()), text); } }
However, I would like to test some functions that insert text into the currently edited EditText using getCurrentInputConnection () :
SoftKeyboard.java
public void InsertText(String sentence) { getCurrentInputConnection().commitText(sentence, 1); } public void ReadText(int chars) { getCurrentInputConnection().getTextBeforeCursor(chars, 0); }
Obviously, in this case, I get a NullPointerException due to the fact that there is actually no focused EditText.
How can I get my test application to start my service, somehow focus on EditText, and then run my test cases so that I can validate my maintenance methods correctly?
source share