Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to test InputMethodService
    text
    copied!<p>I have a basic implementation of the Android <a href="http://developer.android.com/reference/android/inputmethodservice/InputMethodService.html">InputMethodService</a> which I am trying to write unit tests for. My application does not have any Activites, just the implementation of InputMethodService.</p> <p>So far I have a basic implementation of a ServiceTestCase which is working great:</p> <p><strong>SoftKeyboardTest.java</strong></p> <pre><code> public class SoftKeyboardTest extends ServiceTestCase&lt;SoftKeyboard&gt; { @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); } } </code></pre> <p>However, I would like to test some of the functionality which inserts text into the currently focused EditText using <a href="http://developer.android.com/reference/android/inputmethodservice/InputMethodService.html#getCurrentInputConnection%28%29">getCurrentInputConnection()</a>:</p> <p><strong>SoftKeyboard.java</strong></p> <pre><code>public void InsertText(String sentence) { getCurrentInputConnection().commitText(sentence, 1); } public void ReadText(int chars) { getCurrentInputConnection().getTextBeforeCursor(chars, 0); } </code></pre> <p>Obviously in this case I get a NullPointerException due to there not actually being any focused EditText.</p> <p>How can I get my test application to launch my service, somehow focus on an EditText, then launch my test cases so that I can properly test my service methods?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload