Android: Espresso: 我找不到方法来检查键盘是否隐藏。
创始人
2024-10-03 15:38:06
0

要检查键盘是否隐藏,可以使用Android的InputMethodManager类的isAcceptingText()方法。

以下是一个使用Espresso进行UI测试的示例代码:

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import androidx.test.espresso.UiController;
import androidx.test.espresso.ViewAction;
import androidx.test.espresso.matcher.ViewMatchers;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;

import org.hamcrest.Matcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static org.hamcrest.Matchers.not;

@RunWith(AndroidJUnit4.class)
public class KeyboardTest {

    @Rule
    public ActivityTestRule activityRule = new ActivityTestRule<>(MainActivity.class);

    @Test
    public void testKeyboardHidden() {
        onView(ViewMatchers.withId(R.id.editText)).perform(typeText("Test"), closeSoftKeyboard());
        onView(ViewMatchers.withId(R.id.button)).perform(click());

        onView(ViewMatchers.withId(R.id.editText)).check(matches(not(isKeyboardShown())));
    }

    private static Matcher isKeyboardShown() {
        return new Matcher() {
            @Override
            public boolean matches(Object item) {
                if (item instanceof View) {
                    View view = (View) item;
                    InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    return imm.isAcceptingText();
                }
                return false;
            }

            @Override
            public void describeMismatch(Object item, Description mismatchDescription) {
                mismatchDescription.appendText("Keyboard is shown");
            }

            @Override
            public void _dont_implement_Matcher___instead_extend_BaseMatcher_() {

            }

            @Override
            public void describeTo(Description description) {
                description.appendText("Keyboard is hidden");
            }
        };
    }
}

在上面的示例中,我们首先在EditText上输入一些文本,然后关闭键盘。然后,我们点击一个按钮触发一些操作。最后,我们使用自定义的isKeyboardShown()方法来检查键盘是否隐藏,然后使用Espresso的matches()方法进行断言。如果键盘隐藏,断言将成功通过。

相关内容

热门资讯

透视教程!wepoker辅助工... 透视教程!wepoker辅助工具,hhpoker作弊码怎么用(详细辅助2025版教程)1)wepok...
WePoKe透视挂!德州pok... WePoKe透视挂!德州poker外挂(透视)原来存在有挂(详细辅助存在挂教程)1、在WePoKe透...
wepoke一定有挂!wpk微... wepoke一定有挂!wpk微扑克智能辅助(透视)原本是真的有挂(详细辅助解密教程)1、这是跨平台的...
透视脚本!智星德州菠萝透视插件... 透视脚本!智星德州菠萝透视插件工具,aapoker透视脚本(详细辅助高科技教程)1、智星德州菠萝透视...
wepoke黑科技!德州aa扑... wepoke黑科技!德州aa扑克透明挂(透视)其实真的有挂(详细辅助总结教程)1、许多玩家不知道we...
透视透视!hhpoker免费透... 透视透视!hhpoker免费透视脚本,德州hhpoker脚本(详细辅助2025新版技巧);1、不需要...
wepoke辅助有挂!微扑克软... wepoke辅助有挂!微扑克软件发牌原理(透视)好像存在有挂(详细辅助德州教程)1、wepoke辅助...
透视苹果版!aapoker辅助... 透视苹果版!aapoker辅助工具存在吗,wepoker养号规律(详细辅助攻略教程)1、下载好aap...
wepokeai代打的胜率!德... wepokeai代打的胜率!德扑之星怎么埋牌(透视)从前有挂(详细辅助可靠技巧)(1)wepokea...
透视ai代打!aapoker辅... 透视ai代打!aapoker辅助工具免费下载,fishpoker透视(详细辅助攻略教程);1、每一步...