Issue
There is an Activity and a button on it. The task is to pass white-box test for an influence of Activity consequence behaviour "Button CLICK -> Button GONE -> Activity PAUSE -> Activity RESUME" on button visability state.
Naive implementation leads to inconsistent Activity state and consequently wrong test outputs:
// worker thread
TouchUtils.clickView(this, mButton);
...
// ui thread
instrumentation.callActivityOnStop(mActivity);
instrumentation.callActivityOnResume(mActivity);
assertTrue(View.GONE == mButton.getVisibility());
Two glympse approaches don't successed:
1) Adding sleep for main thread after TouchUtils.clickView()
execution however doesn't solve problem completely but creates additional dependancy on empirical UI timeout duration.
2) Synchronizing touch events and consequent Activity lifecycle events invocation via onPostExecute()
from AsyncTask causes tests to fail unpredictably.
Is there any canonical or at least viable solution for worker-ui synchronization at general or specially designed for Android Test framework?
Solution
One has to use runOnMainSync()
from Instrumentation.class for synchronizing UI and worker threads events via Android Test Framework instrumentation:
// worker thread
TouchUtils.clickView(this, mButton);
instrumentation.runOnMainSync(new Runnable() {
@Override
public void run() {
instrumentation.callActivityOnStop(mActivity);
instrumentation.callActivityOnResume(mActivity);
assertTrue(View.GONE == mButton.getVisibility());
}
});
However if there are any other suggestions or general (not Android Test Framework related) guidelines answers are appreciated.
Note: more special tools for synchronization with main thread are described in the official best practices for testing.
Answered By - Yehor Nemov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.