CardListAdapter extends ArrayAdapter<Translation> { @Override public View getView(int position, View translationItemView, ViewGroup parent) { Translation item = getItem(position); TranslationCardItem cardItem; if (translationItemView == null) { cardItem = new TranslationCardItem(context); cardItem.setShowDeleteAndEditOptions(true); cardItem.setGreyOutIfNoAudio(true); } else { cardItem = (TranslationCardItem) translationItemView; } String currentDictionaryLabel = LanguageService.getTitleCaseName(dictionaryService.currentDictionary().getLanguage()); cardItem.setTranslation(item, currentDictionaryLabel, position); if (!deckService.currentDeck().isLocked()) { cardItem.setEditClickListener(getOnEditListener(item)); cardItem.setDeleteClickListener(getOnDeleteListener(item)); } return cardItem; } CardListAdapter(Context context, int resource, int textViewResourceId,
                           List<Translation> objects, TranslationService translationService,
                           DictionaryService dictionaryService, DeckService deckService); @Override View getView(int position, View translationItemView, ViewGroup parent); void update();  }
@Test public void shouldCreateTranslationCardItemIfNoExistingView() { View resultingView = cardListAdapter.getView(0, null, null); assertEquals(TranslationCardItem.class, resultingView.getClass()); }
@Test public void shouldRecycleTranslationCardItemView() { TranslationCardItem translationCardItem = new TranslationCardItem(activity.getApplicationContext()); TranslationCardItem resultingView = (TranslationCardItem) cardListAdapter.getView(0, translationCardItem, null); assertEquals(translations.get(0), resultingView.getTranslation()); }
@Test public void shouldSetMessageOfAlertDialogOnDeleteClick() throws Exception { View resultingView = cardListAdapter.getView(0, null, null); resultingView.findViewById(R.id.translation_card_delete).performClick(); AlertDialog dialog = ShadowAlertDialog.getLatestAlertDialog(); CharSequence actual = ((TextView) dialog.findViewById(android.R.id.message)).getText(); String expected = RuntimeEnvironment.application.getString(R.string.delete_dialog_message); assertEquals(expected, actual); }
@Test public void shouldShowAlertDialogWhenDeleteClicked() throws Exception { View resultingView = cardListAdapter.getView(0, null, null); resultingView.findViewById(R.id.translation_card_delete).performClick(); AlertDialog dialog = ShadowAlertDialog.getLatestAlertDialog(); assertTrue(dialog.isShowing()); }
@Test public void shouldStartNextActivityWhenEditTriggered() { View resultingView = cardListAdapter.getView(0, null, null); resultingView.findViewById(R.id.translation_card_edit).performClick(); String activityClass = shadowOf(activity).getNextStartedActivity().getComponent().getClassName(); assertEquals(EnterSourcePhraseActivity.class.getName(), activityClass); }
@Test public void shouldUseCorrectKeyWhenPassingContextToIntentWhenEditTriggered() { View resultingView = cardListAdapter.getView(0, null, null); resultingView.findViewById(R.id.translation_card_edit).performClick(); boolean hasExtra = shadowOf(shadowOf(activity).getNextStartedActivity()).hasExtra(AddTranslationActivity.CONTEXT_INTENT_KEY); assertTrue(hasExtra); }
@Test public void shouldUseCorrectKeyWhenPassingDeckToIntentWhenEditTriggered() { View resultingView = cardListAdapter.getView(0, null, null); resultingView.findViewById(R.id.translation_card_edit).performClick(); boolean hasExtra = shadowOf(shadowOf(activity).getNextStartedActivity()).hasExtra(DeckService.INTENT_KEY_DECK); assertTrue(hasExtra); }
@Test public void shouldPassCorrectDeckWhenEditTriggered() { View resultingView = cardListAdapter.getView(0, null, null); resultingView.findViewById(R.id.translation_card_edit).performClick(); Deck retrievedDeck = (Deck)shadowOf(shadowOf(activity).getNextStartedActivity()).getExtras().get(DeckService.INTENT_KEY_DECK); assertEquals(basicDeck, retrievedDeck); }