gpt4 book ai didi

android - runOnUiThread() 方法和@UiThreadTest 注解的区别

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:52:37 24 4
gpt4 key购买 nike

如题,谁能解释一下runOnUiThread()方法和@UiThreadTest注解的区别?我一直在阅读使用这两种方法的 Android 测试教程 ( http://developer.android.com/tools/testing/activity_test.html )。它指出:

Code in a test application that interacts with a View of the application under test must run in the main application's thread, also known as the UI thread. To do this, you use the Activity.runOnUiThread() method

和:

The @UiThreadTest annotation tells Android to build this method so that it runs on the UI thread. This allows the method to change the state of the spinner widget in the application under test.

对于runOnUi()方法,有问题的代码是

public void testASpinnerUI()
{
mActivity.runOnUiThread(
new Runnable()
{
@Override
public void run()
{
mSpinner.requestFocus();
mSpinner.setSelection(INITIAL_POSITION);
}// end of run
} // end of runnable
); //end of runOnUiThread

this.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
for (int i = 0; i < TEST_POSITION; i++)
{
this.sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
}
this.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);

mPos = mSpinner.getSelectedItemPosition();
mSelection = (String) mSpinner.getItemAtPosition(mPos);

TextView resultView = (TextView) mActivity.findViewById(com.android.example.spinner.R.id.SpinnerResult);

String resultText = (String) resultView.getText();
assertEquals(resultText, mSelection);
}

对于@UiThreadTest 注解:

@UiThreadTest
public void testStatePause()
{
Instrumentation mInstr = this.getInstrumentation();
mActivity.setSpinnerPosition(TEST_STATE_PAUSE_POSITION);
mActivity.setSpinnerSelection(TEST_STATE_PAUSE_SELECTION);

mInstr.callActivityOnPause(mActivity);

mActivity.setSpinnerPosition(0);
mActivity.setSpinnerSelection("");

mInstr.callActivityOnResume(mActivity);

int currentPosition = mActivity.getSpinnerPosition();
String currentSelection = mActivity.getSpinnerSelection();

assertEquals(TEST_STATE_PAUSE_POSITION, currentPosition);
assertEquals(TEST_STATE_PAUSE_SELECTION, currentSelection);
}

它们似乎可以互换,因为我可以从带注释的测试中删除注释并将其内容包含在 runOnUiThread() 方法中并且它通过。类似地,我可以从另一个测试中删除 runOnUiThread() 方法并添加 @UiThreadTest 注释并通过。

那么有什么区别呢?

此外,本教程还包括另一个测试:

public void testStateDestroy()
{
mActivity.setSpinnerPosition(TEST_STATE_DESTROY_POSITION);
mActivity.setSpinnerSelection(TEST_STATE_DESTROY_SELECTION);

mActivity.finish();
mActivity = getActivity();

int currentPosition = mActivity.getSpinnerPosition();
String currentSelection = mActivity.getSpinnerSelection();

assertEquals(TEST_STATE_DESTROY_POSITION, currentPosition);
assertEquals(TEST_STATE_DESTROY_SELECTION, currentSelection);
}

此测试也与 Activity 交互,但既不需要 @UiThreadTest 注释也不需要 runOnUiThread() 方法。这是为什么?

最佳答案

区别在于语义和副作用。

首先,@UiThreadTest 的存在会导致创建 Activity (如果尚未创建)by calling getActivity() .

然后,在InstrumentatinTestCase ,它使用 getInstrumentation().runOnMainSync() 运行完整测试。

getInstrumentation().runOnMainSync()之间的区别和 Activity.runOnUiThread()是前者等待调用完成(运行完整测试时需要,或者,你知道,在测试中调用东西)而后者则没有。

除此之外,它们发布到不同的 Handler(runOnMainSync 使用来自 ActivityThread 的那个,而 Activity 实例有自己的),但这无关紧要,因为它们被安排在同一个 MessageQueue 上。

关于android - runOnUiThread() 方法和@UiThreadTest 注解的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19427387/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com