Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert drawables in list items #310

Merged
merged 14 commits into from
May 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ static class TextAdapter extends RecyclerView.Adapter<TextAdapter.ViewHolder> {

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -40,14 +41,21 @@ 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);
View yesButton = rowView.findViewById(R.id.yes);
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class TextRecyclerViewAdapter extends RecyclerView.Adapter<TextRecyclerVi
}

public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View root = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_with_buttons, parent, false);
View root = LayoutInflater.from(parent.getContext()).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);
Expand Down
40 changes: 0 additions & 40 deletions sample/src/main/res/layout/row_with_buttons.xml

This file was deleted.

41 changes: 41 additions & 0 deletions sample/src/main/res/layout/row_with_image_and_buttons.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?selectableItemBackground"
android:orientation="vertical">

<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
tools:text="Lorem ipsum banana sit amet" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:orientation="horizontal">

<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="ContentDescription" />

<Button
android:id="@+id/yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes" />

<Button
android:id="@+id/no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No" />
</LinearLayout>

</LinearLayout>