質問

I've just switched to using CursorLoaders and I'm having trouble writing tests that utilize them. Since using the CursorLoader methodology takes the querying off of the main thread getInstrumentation().waitForIdleSync() is returning before the adapter is being updated (or at least this is my theory). I'm trying to avoid this is all my tests

public void testUpdateList() throws InvalidRecord, InterruptedException {
    ListView listView = frag.getListView();
    // Verify list is empty
    assertEquals(0, listView.getCount());

    // Add transaction directly into database
    transTable.addOccurrences(resolver, TestUtils.createMockTrans());

    //Don't want to do this but it works.   
    synchronized (this) {
        wait(500);
        assertEquals(1, listView.getCount());
    }
}

So my question is, is there a better way to test this functionality within the Android testing framework?

役に立ちましたか?

解決

The solution I settled on was using the waitForCondition method within Robotium. Here is an example.

...
 // Waits for 500 milliseconds for the condition to be meet.  
 // If it isn't meet within this time limit the result is false.
 boolean isSatisfied =  solo.waitForCondition( new Condition() {
    public boolean isSatisfied() {
      return listView.getCount() == 1;
    }, 500);

  //Then I check if the condition has been meet.
  assertTrue(isSatisfied);
...
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top