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

时间:2015-02-13 12:30:43

标签: android listview android-listview robolectric

我创建了一个ListView的布局,根据按下的2个给定按钮中的哪个更新值。按下标有赔率的按钮将清除所有值,并将奇数加到20.除均数之外,按下均匀值相同。这在真实设备上模拟或尝试时工作正常。以下是活动代码。

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

}

您能告诉我需要做些什么才能通过Robolectric在ListView上触发更新。

2 个答案:

答案 0 :(得分:10)

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

Robolectric 3.0

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

Robolectric 2.0

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

答案 1 :(得分:4)

只是为了更新Robolectric 3.0的答案

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