我的测试侧重于AsyncTask完成并验证后续活动是否已启动。
众所周知,除非AsyncTask是从UI线程实例化并执行的,否则不调用AsyncTask#onPostExecute,因此调用AsyncTask的我的(测试可见)方法会通过必要的预防措施来确保这种行为 - 通过Runnable如果在UI线程上或者计划在UI线程上运行,则立即运行。
当从ActivityUnitTestCase测试调用此方法时,通过Activity#runOnUiThread实例化并执行此AsyncTask的Runnable最终会在UI线程以外的线程上运行。 有没有办法确保此Runnable将在Activity中的UI线程上执行?
附录:
编辑:这是一些(未经测试的)代码,用于演示问题的本质:
// ExampleActivityTests.java
class ExampleActivityTests : public ActivityUnitTestCase <ExampleActivity> {
public void testThatRequiresUiThread() {
startActivity (new Intent(), null, null);
// ...call instrumentation onStart, onResume...
runTestOnUiThread(new Runnable() {
public void run() {
boolean isUiThread = Thread.currentThread() == Looper.getMainLooper().getThread();
Log.d ("A", "Running on UI Thread? " + isUiThread);
}
});
getActivity().methodRequiringUiThread();
// assertions here...
}
}
// ExampleActivity.java -- just the relevant method
public void methodRequiringUiThread() {
runOnUiThread(new Runnable() {
public void run() {
boolean isUiThread = Thread.currentThread() == Looper.getMainLooper().getThread();
Log.d ("B", "Running on UI Thread? " + isUiThread);
}
});
}
在LogCat中我们会看到:
A | Running on UI Thread? true
B | Running on UI Thread? false
答案 0 :(得分:4)
在UI线程上调用ActivityUnitTestCase#startActivity解决了我的问题。
public void testThatRequiresUiThread() {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
startActivity(new Intent(), null, null);
}
});
// ...
getActivity().methodRequiringUiThread();
// rest of test...
}
产量
A | Running on UI Thread? true
B | Running on UI Thread? true