private void addItems(Activity activity, Map<String, Integer> items) { ListView list = (ListView)activity.findViewById(R.id.list); assertNotNull(list); ShadowListView shadowList = shadowOf(list); shadowList.populateItems(); int initialCount = list.getCount(); for(Map.Entry<String, Integer> item : items.entrySet()) { addItem(activity, item.getKey(), item.getValue()); } assertEquals(initialCount + items.size(), list.getCount()); }
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); }
public static void openASavedForm(int expectedFormCount, int formIndexToSelect) { Intent savedFormsIntent = new Intent(RuntimeEnvironment.application, FormRecordListActivity.class); ShadowActivity homeActivityShadow = prepSavedFormsActivity(savedFormsIntent); FormRecordListActivity savedFormsActivity = Robolectric.buildActivity(FormRecordListActivity.class) .withIntent(savedFormsIntent).create().start() .resume().get(); // wait for saved forms to load Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); ShadowListView shadowEntityList = assertSavedFormEntries(expectedFormCount, savedFormsActivity); shadowEntityList.performItemClick(formIndexToSelect); launchFormEntryForSavedForm(homeActivityShadow, savedFormsIntent, savedFormsActivity); }
/** * test interface redirect {@link #showTransactionList()} * @param mTransactionList */ @Override public void showTransactionList(List mTransactionList) { // prepare List<TransactionFields> list = MockTransaction.fakeTransactionList(); // test DashboardView dashboardView = getTestObj(); dashboardView.showTransactionList(list); ListView listView = getField(dashboardView, ReflectionId.listView); ListAdapter listAdapter = listView.getAdapter(); ShadowListView shadowListView = shadowOf(listView); shadowListView.populateItems(); // verify int i = 0; for (TransactionFields transactionFields : list) { ViewTransactionFields viewTransactionFields = (ViewTransactionFields) listAdapter.getItem(i); compareObj(transactionFields, viewTransactionFields.getTransactionFields()); i++; } }
@Test public void clickPropertyLaunchesActivity() { db.insertProperty(new Property()); ActivityController controller = Robolectric.buildActivity(PropertiesListActivity.class).create(); Activity activity = (Activity)controller.get(); controller.start(); controller.visible(); controller.resume(); ListView list = (ListView)activity.findViewById(R.id.list); ShadowListView shadowList = shadowOf(list); shadowList.populateItems(); assertEquals(1, list.getCount()); shadowList.performItemClick(0); assertTrue(activity.isFinishing() == false); Intent next = shadowOf(activity).getNextStartedActivity(); ComponentName componentName = next.getComponent(); String name = componentName.flattenToShortString(); assertEquals("protect.rentalcalc/.PropertyOverviewActivity", name); Bundle extras = next.getExtras(); assertNotNull(extras); assertTrue(extras.containsKey("id")); assertEquals(DatabaseTestHelper.FIRST_ID, extras.getLong("id")); }
@Test public void doneBlankItem() { HashMap<String, Integer> emptyMap = new HashMap<>(); ActivityController controller = startWithMap(emptyMap); Activity activity = (Activity)controller.get(); ShadowActivity shadowActivity = shadowOf(activity); ListView list = (ListView)activity.findViewById(R.id.list); assertNotNull(list); ShadowListView shadowList = shadowOf(list); shadowList.populateItems(); assertEquals(0, list.getCount()); shadowActivity.clickMenuItem(R.id.action_add); assertEquals(1, list.getCount()); assertTrue(shadowActivity.isFinishing() == false); shadowActivity.clickMenuItem(R.id.action_done); assertTrue(shadowActivity.isFinishing()); // Check that can finish out the lifecycle controller.pause(); controller.stop(); controller.destroy(); checkReturnedMap(activity, emptyMap); }
@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); }
@Test public void shouldShowContentInListView() { ShadowListView shadowListView = shadowOf(topicFragment.getListView()); int index = shadowListView.findIndexOfItemContainingText("Topic 1 content"); assertThat(index).isEqualTo(0); }
@Test public void shouldInvokeCallbackWhenItemIsClicked() { RootActivity spyActivity = spy(Robolectric.buildActivity(RootActivity.class).create().start().resume().get()); fragment.onAttach(spyActivity); int position = 2; ShadowListView shadowListView = shadowOf(fragment.mDrawerListView); shadowListView.performItemClick(position); verify(spyActivity).onNavigationDrawerItemSelected(eq(position)); }
@Test public void shouldShowCircleNameInListItem() { ShadowListView shadowListView = shadowOf(circleFragment.getListView()); int position = shadowListView.findIndexOfItemContainingText("Circle Tang"); assertThat(position).isEqualTo(1); }
private static ShadowListView assertSavedFormEntries(int expectedFormCount, FormRecordListActivity savedFormActivity) { ListView entityList = (ListView)savedFormActivity.findViewById(R.id.screen_entity_select_list); IncompleteFormListAdapter adapter = (IncompleteFormListAdapter)entityList.getAdapter(); adapter.setFormFilter(FormRecordListActivity.FormRecordFilter.Submitted); adapter.resetRecords(); assertEquals(expectedFormCount, adapter.getCount()); return Shadows.shadowOf(entityList); }
public static EntityListAdapter loadList(EntitySelectActivity entitySelectActivity) { // wait for entities to load Robolectric.flushBackgroundThreadScheduler(); Robolectric.flushForegroundThreadScheduler(); ListView entityList = (ListView)entitySelectActivity.findViewById(R.id.screen_entity_select_list); EntityListAdapter adapter = (EntityListAdapter)entityList.getAdapter(); ShadowListView shadowListView = Shadows.shadowOf(entityList); shadowListView.populateItems(); return adapter; }
public static void testListViewClick(ListViewFragment fragment, List<ListItem> items, Class expectedActivity) { Activity activity = FragmentTestDriver.bindData(fragment, items); ListView listView = FragmentTestDriver.getListView(fragment); ShadowListView shadowList = Shadows.shadowOf(listView); ShadowActivity shadowActivity = Shadows.shadowOf(activity); shadowList.performItemClick(0); assertNotNull(shadowActivity.peekNextStartedActivity()); assertEquals(shadowActivity.peekNextStartedActivity().getComponent(), new ComponentName(activity, expectedActivity)); activity.finish(); }
private void testMenuClick(final int index, final Class<?> expected) { ShadowListView list = shadowOf_(foul.getListView()); assertThat(list.performItemClick(index), is(true)); ShadowActivity activity = shadowOf_(foul); ShadowIntent intent = shadowOf_(activity.getNextStartedActivity()); assertThat(intent.getIntentClass().getName(), equalTo(expected.getName())); }
public static ShadowListView shadowOf(ListView instance) { return (ShadowListView) shadowOf_(instance); }