android - 为什么我使用 sendKeys() 的测试在 Android 模拟器中没有动画?

标签 android unit-testing android-emulator junit

当我在 Android 模拟器中运行以下测试时,输入值未显示在 EditText 中。为什么不呢?我需要更改什么才能在模拟器中看到输入? (测试通过,所以最后,这可能并不重要。我只是希望能够在模拟器中看到它实际发生。)

public void testOkButtonOnClickWithUserInputNumber() throws Throwable {
    this.sendKeys(Integer.toString(this.testNumber)); // 123

    this.runTestOnUiThread(new Runnable() {
        @Override
        public void run() {
            Assert.assertTrue(NumberFilterTest.this.okButton.performClick());
        }
    });

    this.getInstrumentation().waitForIdle(new Runnable() {
        @Override
        public void run() {
            Assert.assertTrue(NumberFilterTest.this.activity.isFinishing());
        }
    });
}

最佳答案

查看官方开发指南 - Activity Testing .

1 确保触摸模式已关闭:

To control the emulator or a device with key events you send from your tests, you must turn off touch mode. If you do not do this, the key events are ignored.

 ActivityInstrumentationTestCase2.setActivityTouchMode(false);

2 确保屏幕已解锁:

You may find that UI tests don't work if the emulator's or device's home screen is disabled with the keyguard pattern. This is because the application under test can't receive key events sent by sendKeys().

 mKeyGuardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
 mLock = mKeyGuardManager.newKeyguardLock("activity_classname");
 mLock.disableKeyguard();

关于android - 为什么我使用 sendKeys() 的测试在 Android 模拟器中没有动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13169260/

相关文章:

java - 我如何模拟服务以抛出异常返回列表的方法?

spring - 使用 Mockk 重载解决歧义的 kotlin 单元测试

java - 如何设置创建 Android 设备选项

python - 有没有办法在python中将点击事件发送到后台的窗口?

javascript - 当移动设备从 sleep 中唤醒时重新加载网页

java - 如何使用github上的eclipse项目进行Android开发

java - 当我使用双数据类型时,我想打印类似 2523525252.025 的金额,它在 2.523525252025E9 中已更改,如何修复?

android - 使用 DropDown open() 方法打开自定义 DropDown 不起作用

python - 当您的应用程序具有测试目录时,在 Django 中运行特定的测试用例

android - 通过 ssh ubuntu 64 位启动 Android 模拟器时出错