2

我刚刚切换到使用 CursorLoaders 并且在编写使用它们的测试时遇到了麻烦。由于使用 CursorLoader 方法会在适配器更新之前从主线程getInstrumentation().waitForIdleSync()返回查询(或者至少这是我的理论)。我试图避免这是我所有的测试

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());
    }
}

所以我的问题是,有没有更好的方法在 Android 测试框架中测试这个功能?

4

1 回答 1

1

我确定的解决方案是使用waitForConditionRobotium 中的方法。这是一个例子。

...
 // 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);
...
于 2014-02-28T18:37:26.683 回答