/** * Simulates a click on the {@code Dialog} item indicated by {@code index}. Handles both multi- and single-choice dialogs, tracks which items are currently * checked and calls listeners appropriately. * * @param index the index of the item to click on */ public void clickOnItem(int index) { ShadowListView shadowListView = Shadow.extract(realAlertDialog.getListView()); shadowListView.performItemClick(index); }
@Test public void clickItemContainingText_shouldPerformItemClickOnList_arrayAdapter() throws Exception { ArrayList<String> adapterFileList = new ArrayList<>(); adapterFileList.add("Item 1"); adapterFileList.add("Item 2"); adapterFileList.add("Item 3"); final ArrayAdapter<String> adapter = new ArrayAdapter<>(application, android.R.layout.simple_list_item_1, adapterFileList); listView.setAdapter(adapter); shadowOf(listView).populateItems(); ShadowListView shadowListView = shadowOf(listView); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { transcript.add("clicked on item " + adapter.getItem(position)); } }); shadowListView.clickFirstItemContainingText("Item 3"); assertThat(transcript).containsExactly("clicked on item Item 3"); }
@Test public void findItemContainingText_shouldReturnNullIfNotFound() throws Exception { ShadowListView shadowListView = prepareListWithThreeItems(); assertThat(shadowListView.findItemContainingText("Non-existent item")).isNull(); }
private void testItemClickLaunchesActivity(int index, String expectedActivity) { ActivityController activityController = Robolectric.buildActivity(MainActivity.class).create(); Activity activity = (Activity)activityController.get(); activityController.start(); activityController.resume(); ListView list = (ListView)activity.findViewById(R.id.list); ShadowListView shadowList = shadowOf(list); shadowList.populateItems(); // First item should be the Budgets, second should be Transactions shadowList.performItemClick(index); testNextStartedActivity(activity, expectedActivity); }
private ListAdapter prepareWithListAdapter() { ListAdapter adapter = new ListAdapter("a", "b", "c"); listView.setAdapter(adapter); shadowOf(listView).populateItems(); return adapter; }
@Test public void clickItemContainingText_shouldPerformItemClickOnList() throws Exception { ShadowListView shadowListView = prepareListWithThreeItems(); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { transcript.add("clicked on item " + position); } }); shadowListView.clickFirstItemContainingText("Item 1"); assertThat(transcript).containsExactly("clicked on item 1"); }
@Test public void addFooterView_ShouldRecordFooters() throws Exception { View view0 = new View(context); View view1 = new View(context); listView.addFooterView(view0); listView.addFooterView(view1); listView.setAdapter(new ShadowCountingAdapter(3)); assertThat(shadowOf(listView).getFooterViews().get(0)).isSameAs(view0); assertThat(shadowOf(listView).getFooterViews().get(1)).isSameAs(view1); }
@Test public void addHeaderView_ShouldRecordHeaders() throws Exception { View view0 = new View(context); view0.setId(0); View view1 = new View(context); view1.setId(1); View view2 = new View(context); view2.setId(2); View view3 = new View(context); view3.setId(3); listView.addHeaderView(view0); listView.addHeaderView(view1); listView.addHeaderView(view2, null, false); listView.addHeaderView(view3, null, false); listView.setAdapter(new ShadowCountingAdapter(2)); assertThat(listView.getHeaderViewsCount()).isEqualTo(4); assertThat(shadowOf(listView).getHeaderViews().get(0)).isSameAs(view0); assertThat(shadowOf(listView).getHeaderViews().get(1)).isSameAs(view1); assertThat(shadowOf(listView).getHeaderViews().get(2)).isSameAs(view2); assertThat(shadowOf(listView).getHeaderViews().get(3)).isSameAs(view3); assertThat((View) listView.findViewById(0)).isNotNull(); assertThat((View) listView.findViewById(1)).isNotNull(); assertThat((View) listView.findViewById(2)).isNotNull(); assertThat((View) listView.findViewById(3)).isNotNull(); }
@Test public void clickOnBudget() { ActivityController activityController = Robolectric.buildActivity(BudgetActivity.class).create(); Activity activity = (Activity)activityController.get(); DBHelper db = new DBHelper(activity); db.insertBudget("name", 100); db.close(); activityController.start(); activityController.resume(); ListView list = (ListView)activity.findViewById(R.id.list); ShadowListView shadowList = shadowOf(list); shadowList.populateItems(); shadowList.performItemClick(0); ShadowActivity shadowActivity = shadowOf(activity); Intent startedIntent = shadowActivity.getNextStartedActivity(); ComponentName name = startedIntent.getComponent(); assertEquals("protect.budgetwatch/.TransactionActivity", name.flattenToShortString()); Bundle bundle = startedIntent.getExtras(); String budget = bundle.getString("budget"); assertEquals("name", budget); } }
private ShadowListView prepareListWithThreeItems() { listView.setAdapter(new ShadowCountingAdapter(3)); shadowOf(listView).populateItems(); return shadowOf(listView); }
@Test(expected = IllegalArgumentException.class) public void clickItemContainingText_shouldThrowExceptionIfNotFound() throws Exception { ShadowListView shadowListView = prepareListWithThreeItems(); shadowListView.clickFirstItemContainingText("Non-existant item"); }
/** * Simulates a click on the {@code Dialog} item indicated by {@code index}. Handles both multi- and single-choice dialogs, tracks which items are currently * checked and calls listeners appropriately. * * @param index the index of the item to click on */ public void clickOnItem(int index) { Shadows.shadowOf(realAlertDialog.getListView()).performItemClick(index); }
shadowOf(list).populateItems();
@Test public void findItemContainingText_shouldFindChildByString() throws Exception { ShadowListView shadowListView = prepareListWithThreeItems(); View item1 = shadowListView.findItemContainingText("Item 1"); assertThat(item1).isSameAs(listView.getChildAt(1)); }
/** * Simulates a click on the {@code Dialog} item indicated by {@code index}. Handles both multi- and single-choice dialogs, tracks which items are currently * checked and calls listeners appropriately. * * @param index the index of the item to click on */ public void clickOnItem(int index) { ShadowListView shadowListView = Shadow.extract(realAlertDialog.getListView()); shadowListView.performItemClick(index); }
/** * Simulates a click on the {@code Dialog} item indicated by {@code index}. Handles both multi- and single-choice dialogs, tracks which items are currently * checked and calls listeners appropriately. * * @param index the index of the item to click on */ public void clickOnItem(int index) { Shadows.shadowOf(realAlertDialog.getListView()).performItemClick(index); }
/** * Simulates a click on the {@code Dialog} item indicated by {@code index}. Handles both multi- and single-choice dialogs, tracks which items are currently * checked and calls listeners appropriately. * * @param index the index of the item to click on */ public void clickOnItem(int index) { Shadows.shadowOf(realAlertDialog.getListView()).performItemClick(index); }