Robolectric 测试 ListView 在适配器中的值更改后不更新

Robolectric test ListView not updating after value changes in adapter

我创建了一个带有 ListView 的布局,它根据按下的 2 个给定按钮中的哪一个来更新其中的值。按下标有 Odds 的按钮将清除所有值并添加奇数直到 20。按下 evens 做同样的事情,除了偶数。在真实设备上模拟或试用时,这工作正常。下面是 activity 代码。

public class ListViewActivity extends Activity {

    private ListView mainListView;
    private ArrayAdapter<String> listAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.robolectric_test_main);

        mainListView = (ListView) findViewById(R.id.mainListView);

        ArrayList<String> numbersList = new ArrayList<String>();
        for (int i = 1; i < 21; i++) {
            numbersList.add(String.valueOf(i));
        }

        listAdapter = new ArrayAdapter<String>(this, R.layout.robolectric_listview_row, numbersList);

        mainListView.setAdapter(listAdapter);
    }

    public void onOddsClicked(View view) {
        listAdapter.clear();
        for (int i = 1; i < 21; i += 2) {
            listAdapter.add(String.valueOf(i));
        }
    }

    public void onEvensClicked(View view) {
        listAdapter.clear();
        for (int i = 2; i < 21; i += 2) {
            listAdapter.add(String.valueOf(i));
        }
    }
}

然而,当尝试使用 Robolectric 对其进行测试时,即使适配器具有值修改,ListView 也永远不会更新。这是我的测试代码。

@RunWith(RobolectricTestRunner.class)
public class ListViewActivityTest {

    private ListViewActivity listViewActivity;
    private Button oddsButton;
    private Button evensButton;
    private ListView numbersList;

    @Before
    public void setUp() throws Exception {
        listViewActivity = Robolectric.buildActivity(ListViewActivity.class).create().start().resume().visible().get();
        assignFields();
    }

    private void assignFields() {
        oddsButton = (Button) listViewActivity.findViewById(R.id.oddsButton);
        evensButton = (Button) listViewActivity.findViewById(R.id.evensButton);
        numbersList = (ListView) listViewActivity.findViewById(R.id.mainListView);
    }

    @Test
    public void odds() throws Exception {
        // check all numbers are odd
        Robolectric.clickOn(oddsButton);
        Robolectric.runUiThreadTasksIncludingDelayedTasks();
        for (int i = 0; i < numbersList.getChildCount(); i++) {
            TextView number = (TextView) numbersList.getChildAt(i);
            String numberString = number.getText().toString();
            int parsedInt = Integer.parseInt(numberString);
            assertTrue((parsedInt % 2) == 1);
        }
    }

    @Test
    public void evens() throws Exception {
        // check all numbers are even
        Robolectric.clickOn(evensButton);
        Robolectric.runUiThreadTasksIncludingDelayedTasks();
        for (int i = 0; i < numbersList.getChildCount(); i++) {
            TextView number = (TextView) numbersList.getChildAt(i);
            String numberString = number.getText().toString();
            int parsedInt = Integer.parseInt(numberString);
            assertTrue((parsedInt % 2) == 0);
        }
    }

}

能否请您告诉我我需要做什么才能触发 ListView 到 Robolectric 的更新。

在 robolectric 中,您必须调用 ShadowListView.populateItems() 以获取适配器更改

Robolectric 3.0

ShadowListView shadowListView = Shadows.shadowOf(mListView);
shadowListView.populateItems();

Robolectric 2.0

ShadowListView shadowListView = Robolectric.shadowOf(listView);
shadowListView.populateItems(); 

只是更新 Robolectric 3.0

的答案
 ShadowListView shadowListView = Shadows.shadowOf(mListView);
    shadowListView.populateItems();