I followed the section named Adding a UI test of the
tutorial at
http://developer.android.com/resources/tutorials/testing/activity_test.html.
I was confused about why it wanted to interact with UI by UI-thread
and send keys via instrumentation at the same test testSpinnerUI()
method. I think it causes a race condition.
public void testSpinnerUI(){
////
Thread.currentThread().getId();
mActivity.runOnUiThread(
new Runnable() {
public void run() {
//
Thread.currentThread().getId();
mSpinner.requestFocus();
mSpinner.setSelection(INITIAL_POSITION);
} // end of run() method definition
} // end of anonymous Runnable object instantiation
);
this.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
for (int i = 1; i <= TEST_POSITION; i++) {
this.sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
} // end of for loop
this.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
//code emitted
}
I used Thread.currentThread().getId() in the run() method and the same
API in the testSpinnerUI() method. I found they were different
thread. Is it a logical bug lead to a race condition?
I also tried to modified the code like this
public void testSpinnerUI(){
this.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
for (int i = 1; i <= TEST_POSITION; i++) {
this.sendKeys(KeyEvent.KEYCODE_DPAD_DOWN);
} // end of for loop
this.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);
mActivity.runOnUiThread(
new Runnable() {
public void run() {
mSpinner.requestFocus();
mSpinner.setSelection(INITIAL_POSITION);
} // end of run() method definition
} // end of anonymous Runnable object instantiation
);
//code emitted
}
by switching the order.
Then, I got NullPointerException at mSpinner.requestFocus(). However,
the testPreConditions() method test is passed.
public void testPreConditions() {
assertTrue(mSpinner!=null);
assertTrue(mPlanetData != null);
assertEquals(mPlanetData.getCount(),ADAPTER_COUNT);
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en