問題描述
我正在嘗試使用以下測(cè)試用例測(cè)試簡(jiǎn)單的 UI,主要思想是在測(cè)試中設(shè)置一些 UI 文本(模仿用戶輸入),然后主動(dòng)點(diǎn)擊一個(gè)事件.
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()));
}
}
運(yùn)行結(jié)果是異常——
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)
這發(fā)生在每次訪問 UI 元素時(shí),我無法通過使用句柄或異步任務(wù)來避免它?有什么建議嗎?
This happens on each access to the UI elements and i was unable to avoid it by using handles or async task ? any suggestions?
推薦答案
這是一個(gè)老問題,但我還是給你一個(gè)答案,以防有人偶然發(fā)現(xiàn).
This is an old question, but I'm giving you an answer anyway, in case someone stumbles upon it.
除了主線程(UI 線程)之外,您不能從任何地方更改 UI 小部件的狀態(tài).您的 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();
}
});
但這還不是全部,您還需要通過添加以下行來將您的儀器測(cè)試與 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();
同步行通常緊跟在 runOnUiThread() 代碼之后.
The sync line is usually placed immediately after the runOnUiThread() code.
這篇關(guān)于在 Android 視圖中使用 UI 元素進(jìn)行測(cè)試的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!