在Android的视图UI元素测试 [英] Testing with UI elements in Android view

查看:142
本文介绍了在Android的视图UI元素测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图测试简单的UI与下面的测试情况下, 其主要思想是在测试一些UI文本(来模拟用户输入)的设置,然后单击积极的事件。

I am attempting to test simple UI with the following test case, The main idea is to set in the test some of the UI text (to mimic user input) and then actively click an event.

public class StackTestCase 
extends ActivityInstrumentationTestCase2<Stack> 
{
private StackDemo mActivity;

private EditText eaten;
    public StuckTestCase() {
         super("com.crocodil.software.stack", Stack.class);


    }
    public StuckTestCase(Class<Stack> activityClass) {
         super("com.crocodil.software.stack", activityClass);

    }
    protected void setUp() throws Exception {
        super.setUp();
        mActivity = this.getActivity();
        mCount = (Button) mActivity.findViewById(com.crocodil.software.stack.R.id.action);
        eaten = (EditText) mActivity.findViewById(com.crocodil.software.stack.R.id.eaten);

    }

    public void testPreconditions() {
        assertNotNull(mStatus);
      }

    public void testSimpleDefaults(){
        double status = Double.valueOf(mStatus.getText().toString());
        eaten.setText(2);
        mCount.performClick();
        assertEquals((status-2),Double.valueOf(mStatus.getText().toString()));
    }

}

运行结果是例外 -

the running result is the exception -

android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRoot.checkThread(ViewRoot.java:2802)
at android.view.ViewRoot.playSoundEffect(ViewRoot.java:2581)
at android.view.View.playSoundEffect(View.java:8516)
at android.view.View.performClick(View.java:2407)
at com.crocodil.software.stack.test.StackTestCase.testSimpleDefaults(StackTestCase.java:46)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:520)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)

这发生在每次访问的UI元素,我是无法避免它通过使用手柄或异步任务?有什么建议?

This happens on each access to the UI elements and i was unable to avoid it by using handles or async task ? any suggestions?

推荐答案

这是一个老问题,但我给你一个答案,无论如何,以防有人绊倒了。

This is an old question, but I'm giving you an answer anyway, in case someone stumbles upon it.

您不得从任何地方,但主线程(UI线程)改变UI控件的状态。您performClick必须这样做:

You are not allowed to change states of UI widgets from anywhere but the main thread (UI thread). Your performClick must be done like this:

mActivity.runOnUiThread(new Runnable() {
  @Override
  public void run() {
    mCount.performClick();
  }
});

但是,这还不是全部,你还需要同步您的仪器测试与UI,加入以下行:

But that is not all, you will also need to sync your instrumentation test with the ui, by adding the following line:

getInstrumentation().waitForIdleSync();

SYNC线通常是后runOnUiThread()code立即放置。

The sync line is usually placed immediately after the runOnUiThread() code.

这篇关于在Android的视图UI元素测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆