Simulating a click on a Next/Done button in Robolectric

Jit*_*yay 1 android robolectric

In a given screen i have few controls where the first one is EditText and than RadioGroup and than a Button, at EditText i have used android:imeOptions="actionNext", now for this writing a straightforward Robolectric test case is not a big deal as we can write

Button someButton = (Button) findViewById(R.id.some_button);
someButton.performClick();
Run Code Online (Sandbox Code Playgroud)

but the question is how to automate and write a test case on clicking Next/Done button of the given softinput KeyBoard. How we can write and perform next/done Button of softinput keyboard click ?

Please guide!!

小智 7

你有可能想要测试的东西:

getView(R.id.etPassword).setOnEditorActionListener(
            new OnEditorActionListener() {

                @Override
                public boolean onEditorAction(TextView v, int actionId,
                        KeyEvent event) {
                    if (actionId == EditorInfo.IME_ACTION_NEXT) {
                        checkCredentialsAndStartLoginTask();
                    }
                    return false;
                }
            });
Run Code Online (Sandbox Code Playgroud)

当您单击软键盘上的"下一步"时,将调用此侦听器.如果你想测试它,只需在EditText上使用足够的actionId作为参数调用onEditorAction来模拟这个软键盘按钮点击.请参阅此示例.

@Test
public void emptyUsernameShouldTriggerToast() throws Exception {
    activity = controller.create().start().resume().visible().get();

    // enter username and password
    ui.etAccount.setText(""); 
    ui.etPassword.setText("Battery");

    // test initial state
    ui.etPassword.onEditorAction(EditorInfo.IME_ACTION_NEXT);

    // test Toast
    ShadowHandler.idleMainLooper(); 
    assertThat( ShadowToast.getTextOfLatestToast() ).isEqualTo(("Please enter username and password.") ); 
}
Run Code Online (Sandbox Code Playgroud)