NewsItemViewHolder extends DataViewHolder<UgentNewsArticle> { @Override public void populate(final UgentNewsArticle newsItem) { title.setText(newsItem.getTitle()); String author = newsItem.getCreators().isEmpty() ? "" : newsItem.getCreators().iterator().next(); CharSequence dateString; if (newsItem.getCreated().toLocalDate().isEqual(newsItem.getModified().toLocalDate())) { dateString = DateUtils.relativeDateTimeString(newsItem.getCreated(), itemView.getContext()); } else { dateString = itemView.getContext().getString(R.string.article_date_changed, DateUtils.relativeDateTimeString(newsItem.getCreated(), itemView.getContext(), true), DateUtils.relativeDateTimeString(newsItem.getModified(), itemView.getContext(), true) ); } String infoText = itemView.getContext().getString(R.string.deprecated_dot_separated, dateString, author); info.setText(infoText); if (!TextUtils.isEmpty(newsItem.getDescription())) { excerpt.setText(Utils.fromHtml(newsItem.getDescription()).toString().trim()); } else { excerpt.setText(Utils.fromHtml(newsItem.getText()).toString().trim()); } itemView.setOnClickListener(v -> ArticleViewer.viewArticle(v.getContext(), newsItem, helper)); } NewsItemViewHolder(View v, ActivityHelper activityHelper); @Override void populate(final UgentNewsArticle newsItem);  }
@Test public void populate() { View view = inflate(R.layout.item_news); NewsItemViewHolder viewHolder = new NewsItemViewHolder(view, helper); UgentNewsArticle newsItem = generate(UgentNewsArticle.class); viewHolder.populate(newsItem); assertTextIs(newsItem.getTitle(), view.findViewById(R.id.name)); assertNotEmpty(view.findViewById(R.id.info)); assertNotEmpty(view.findViewById(R.id.article_excerpt)); assertTrue(view.hasOnClickListeners()); view.performClick(); verify(helper, times(1)).openCustomTab(any(Uri.class)); }
@Test public void populateVariant() { View view = inflate(R.layout.item_news); NewsItemViewHolder viewHolder = new NewsItemViewHolder(view, helper); UgentNewsArticle newsItem = generate(UgentNewsArticle.class, "description"); newsItem.setModified(OffsetDateTime.now().plusDays(10)); viewHolder.populate(newsItem); assertTextIs(newsItem.getTitle(), view.findViewById(R.id.name)); assertNotEmpty(view.findViewById(R.id.info)); assertNotEmpty(view.findViewById(R.id.article_excerpt)); assertTrue(view.hasOnClickListeners()); }