My test focuses on an AsyncTask completing and validating that a subsequent Activity is started.
It's known that AsyncTask#onPostExecute is not invoked unless the AsyncTask is instantiated and executed from the UI thread, so my (test visible) method to invoke the AsyncTask goes through the necessary precautions to ensure this behavior--via a Runnable that immediately runs if on the UI thread or is scheduled to run on the UI thread.
When this method is invoked from an ActivityUnitTestCase test, the Runnable that instantiates and executes this AsyncTask via Activity#runOnUiThread ends up running on a thread other than the UI thread. Is there a way to ensure that this Runnable will execute on the UI thread from within the Activity?
Addenda:
Edit: Here's a bit of (untested) code that demonstrates the essence of the problem:
// 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);
}
});
}
In LogCat we'll see:
A | Running on UI Thread? true
B | Running on UI Thread? false
Calling ActivityUnitTestCase#startActivity on the UI thread solves my problem.
public void testThatRequiresUiThread() {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
startActivity(new Intent(), null, null);
}
});
// ...
getActivity().methodRequiringUiThread();
// rest of test...
}
yields
A | Running on UI Thread? true
B | Running on UI Thread? true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With