diff --git a/README.md b/README.md index c75f5e1eb..407edb28e 100644 --- a/README.md +++ b/README.md @@ -166,6 +166,7 @@ assertDisplayedAtPosition(R.id.list, 0, "text"); assertDisplayedAtPosition(R.id.list, 0, R.id.text_field, "text"); assertDisplayedAtPosition(R.id.list, 0, R.string.hello_world); assertDisplayedAtPosition(R.id.list, 0, R.id.text_field, R.string.hello_world); +assertDrawableDisplayedAtPosition(R.id.recycler, 0, R.id.imageview, R.drawable.ic_barista); // you can also pass custom matchers assertDisplayed(withTagValue(is("tagName"))) diff --git a/library/src/main/java/com/schibsted/spain/barista/assertion/BaristaListAssertions.kt b/library/src/main/java/com/schibsted/spain/barista/assertion/BaristaListAssertions.kt index 3899e01d2..003566e47 100644 --- a/library/src/main/java/com/schibsted/spain/barista/assertion/BaristaListAssertions.kt +++ b/library/src/main/java/com/schibsted/spain/barista/assertion/BaristaListAssertions.kt @@ -10,7 +10,7 @@ import androidx.test.espresso.matcher.ViewMatchers import androidx.recyclerview.widget.RecyclerView import android.view.View import android.widget.ListView -import com.schibsted.spain.barista.interaction.BaristaListInteractions +import androidx.annotation.DrawableRes import com.schibsted.spain.barista.interaction.BaristaListInteractions.findListViewMatcher import com.schibsted.spain.barista.interaction.BaristaListInteractions.findRecyclerMatcher import com.schibsted.spain.barista.interaction.BaristaListInteractions.scrollListToPosition @@ -19,6 +19,7 @@ import com.schibsted.spain.barista.internal.matcher.ListViewNotEmptyAssertion import com.schibsted.spain.barista.internal.matcher.ListViewItemCountAssertion import com.schibsted.spain.barista.internal.matcher.RecyclerViewNotEmptyAssertion import com.schibsted.spain.barista.internal.matcher.RecyclerViewItemCountAssertion +import com.schibsted.spain.barista.internal.matcher.DrawableMatcher import org.hamcrest.CoreMatchers import org.hamcrest.Description import org.hamcrest.Matcher @@ -94,7 +95,7 @@ object BaristaListAssertions { @JvmStatic fun assertDisplayedAtPosition(@IdRes listId: Int, position: Int, @IdRes targetViewId: Int = NO_VIEW_ID, @StringRes textId: Int) { - BaristaListInteractions.scrollListToPosition(listId, position) + scrollListToPosition(listId, position) assertCustomAssertionAtPosition( listId = listId, @@ -109,9 +110,26 @@ object BaristaListAssertions { ) } + @JvmStatic + fun assertDrawableDisplayedAtPosition(@IdRes listId: Int, position: Int, @IdRes targetViewId: Int = NO_VIEW_ID, @DrawableRes drawableRes: Int) { + scrollListToPosition(listId, position) + + assertCustomAssertionAtPosition( + listId = listId, + position = position, + targetViewId = targetViewId, + viewAssertion = ViewAssertions.matches( + CoreMatchers.anyOf( + ViewMatchers.hasDescendant(DrawableMatcher.withDrawable(drawableRes)), + DrawableMatcher.withDrawable(drawableRes) + ) + ) + ) + } + @JvmStatic fun assertCustomAssertionAtPosition(@IdRes listId: Int, position: Int, @IdRes targetViewId: Int = NO_VIEW_ID, viewAssertion: ViewAssertion) { - BaristaListInteractions.scrollListToPosition(listId, position) + scrollListToPosition(listId, position) Espresso.onView(atPositionOnList(listId = listId, position = position, diff --git a/sample/src/androidTest/java/com/schibsted/spain/barista/sample/ListViewAssertionTest.kt b/sample/src/androidTest/java/com/schibsted/spain/barista/sample/ListViewAssertionTest.kt index ec68a8c07..789710eab 100644 --- a/sample/src/androidTest/java/com/schibsted/spain/barista/sample/ListViewAssertionTest.kt +++ b/sample/src/androidTest/java/com/schibsted/spain/barista/sample/ListViewAssertionTest.kt @@ -7,6 +7,7 @@ import androidx.test.rule.ActivityTestRule import androidx.test.runner.AndroidJUnit4 import com.schibsted.spain.barista.assertion.BaristaListAssertions.assertCustomAssertionAtPosition import com.schibsted.spain.barista.assertion.BaristaListAssertions.assertDisplayedAtPosition +import com.schibsted.spain.barista.assertion.BaristaListAssertions.assertDrawableDisplayedAtPosition import com.schibsted.spain.barista.assertion.BaristaListAssertions.assertListItemCount import com.schibsted.spain.barista.assertion.BaristaListAssertions.assertListNotEmpty import com.schibsted.spain.barista.internal.failurehandler.BaristaException @@ -147,6 +148,38 @@ class ListViewAssertionTest { ) } + @Test + fun shouldFindItemInListViewWithSpecificDrawableWithoutIdInComplexList() { + openComplexListActivity() + assertDrawableDisplayedAtPosition( + listId = R.id.listview, + position = 0, + drawableRes = R.drawable.ic_barista + ) + } + + @Test + fun shouldFindItemInListViewWithSpecificDrawableWithIdInComplexList() { + openComplexListActivity() + assertDrawableDisplayedAtPosition( + listId = R.id.listview, + position = 1, + targetViewId = R.id.imageview, + drawableRes = R.drawable.ic_barista + ) + } + + @Test(expected = AssertionFailedError::class) + fun shouldFailWhenUnableToFindItemInListViewWithSpecificDrawable() { + openComplexListActivity() + assertDrawableDisplayedAtPosition( + listId = R.id.listview, + position = 4, + targetViewId = R.id.imageview, + drawableRes = R.drawable.ic_barista + ) + } + private fun openActivity(intentBuilder: ListsActivity.IntentBuilder) { activityTestRule.launchActivity(intentBuilder.build(ApplicationProvider.getApplicationContext())) } diff --git a/sample/src/main/java/com/schibsted/spain/barista/sample/RecyclerViewsInsideViewPagerActivity.java b/sample/src/main/java/com/schibsted/spain/barista/sample/RecyclerViewsInsideViewPagerActivity.java index e6d1874d5..7e2c481b5 100644 --- a/sample/src/main/java/com/schibsted/spain/barista/sample/RecyclerViewsInsideViewPagerActivity.java +++ b/sample/src/main/java/com/schibsted/spain/barista/sample/RecyclerViewsInsideViewPagerActivity.java @@ -92,7 +92,7 @@ static class TextAdapter extends RecyclerView.Adapter { public TextAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View root = LayoutInflater.from(parent.getContext()) - .inflate(R.layout.row_with_buttons, parent, false); + .inflate(R.layout.row_with_image_and_buttons, parent, false); TextView textView = (TextView) root.findViewById(R.id.textview); View yesButton = root.findViewById(R.id.yes); View noButton = root.findViewById(R.id.no); diff --git a/sample/src/main/java/com/schibsted/spain/barista/sample/TextListViewAdapter.java b/sample/src/main/java/com/schibsted/spain/barista/sample/TextListViewAdapter.java index 1d9184601..ec4caa938 100644 --- a/sample/src/main/java/com/schibsted/spain/barista/sample/TextListViewAdapter.java +++ b/sample/src/main/java/com/schibsted/spain/barista/sample/TextListViewAdapter.java @@ -6,6 +6,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; +import android.widget.ImageView; import android.widget.TextView; public class TextListViewAdapter extends BaseAdapter { @@ -40,7 +41,7 @@ public View getView(final int position, View convertView, ViewGroup parent) { if (convertView == null) { LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); - rowView = inflater.inflate(R.layout.row_with_buttons, parent, false); + rowView = inflater.inflate(R.layout.row_with_image_and_buttons, parent, false); } TextView textView = (TextView) rowView.findViewById(R.id.textview); @@ -48,6 +49,13 @@ public View getView(final int position, View convertView, ViewGroup parent) { View noButton = rowView.findViewById(R.id.no); textView.setText(items[position]); + ImageView imageView = rowView.findViewById(R.id.imageview); + if (position == 0 || position == 1) { + imageView.setImageResource(R.drawable.ic_barista); + } else { + imageView.setImageResource(R.drawable.ic_action_menu); + } + rowView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { diff --git a/sample/src/main/java/com/schibsted/spain/barista/sample/TextRecyclerViewAdapter.java b/sample/src/main/java/com/schibsted/spain/barista/sample/TextRecyclerViewAdapter.java index 41b37165f..27e8e3b11 100644 --- a/sample/src/main/java/com/schibsted/spain/barista/sample/TextRecyclerViewAdapter.java +++ b/sample/src/main/java/com/schibsted/spain/barista/sample/TextRecyclerViewAdapter.java @@ -18,7 +18,7 @@ public class TextRecyclerViewAdapter extends RecyclerView.Adapter - - - - - - -