How to test InputMethodService

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?

+6
source share
1 answer

If you are doing unit testing with an input method, I would test it low enough not to have an InputConnection. If you are testing the integration / acceptance level, I will simply write an application with an edit text box and enter the input / output there.

But really, having done this for 2 years, tests at this level will be almost useless. Your problems will not be related to the default implementation of a text editor - out of thousands of applications, you will use the EditText subclass or create your own EditText classes that will behave differently. You cannot automate the test for this, and you will spend years fixing errors on them.

+2
source

Source: https://habr.com/ru/post/947995/


All Articles