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

Fix issue #283 assertDisplayedAtPosition doesn't assert displayed lis… #354

Merged
merged 13 commits into from
Jul 5, 2021
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
package com.schibsted.spain.barista.assertion

import android.content.res.Resources
import android.graphics.Rect
import android.view.View
import android.view.ViewGroup
import android.widget.ListView
import androidx.annotation.IdRes
import androidx.annotation.StringRes
import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.Espresso
import androidx.test.espresso.NoMatchingViewException
import androidx.test.espresso.ViewAssertion
import androidx.test.espresso.assertion.ViewAssertions
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 com.schibsted.spain.barista.interaction.BaristaListInteractions.findListViewMatcher
import com.schibsted.spain.barista.interaction.BaristaListInteractions.findRecyclerMatcher
import com.schibsted.spain.barista.interaction.BaristaListInteractions.scrollListToPosition
import com.schibsted.spain.barista.internal.failurehandler.SpyFailureHandler
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.ListViewNotEmptyAssertion
import com.schibsted.spain.barista.internal.matcher.RecyclerViewItemCountAssertion
import com.schibsted.spain.barista.internal.matcher.RecyclerViewNotEmptyAssertion
import org.hamcrest.CoreMatchers
import org.hamcrest.Description
import org.hamcrest.Matcher
Expand Down Expand Up @@ -72,8 +74,6 @@ object BaristaListAssertions {

@JvmStatic
fun assertDisplayedAtPosition(@IdRes listId: Int, position: Int, @IdRes targetViewId: Int = NO_VIEW_ID, text: String) {
scrollListToPosition(listId, position)

assertCustomAssertionAtPosition(
listId = listId,
position = position,
Expand All @@ -94,8 +94,6 @@ object BaristaListAssertions {

@JvmStatic
fun assertDisplayedAtPosition(@IdRes listId: Int, position: Int, @IdRes targetViewId: Int = NO_VIEW_ID, @StringRes textId: Int) {
BaristaListInteractions.scrollListToPosition(listId, position)

assertCustomAssertionAtPosition(
listId = listId,
position = position,
Expand Down Expand Up @@ -143,13 +141,18 @@ object BaristaListAssertions {
var childView: View? = null

if (childView == null) {
val listView: ListView? = view.rootView.findViewById(listViewId) as ListView
if (listView != null && listView.id == listViewId) {
val positionOnScreen = position - listView.firstVisiblePosition
val viewAtPosition = listView.getChildAt(positionOnScreen)

viewAtPosition?.let {
childView = it
val views = getShownViewsById(view.rootView as ViewGroup, listViewId)
if (views != null && views.isNotEmpty()) {
val listView: ListView = views[0] as ListView
if (listView.id == listViewId) {
val positionOnScreen = position - listView.firstVisiblePosition
val viewAtPosition = listView.getChildAt(positionOnScreen)

viewAtPosition?.let {
childView = it
}
} else {
return false
}
} else {
return false
Expand All @@ -168,11 +171,16 @@ object BaristaListAssertions {
var childView: View? = null

if (childView == null) {
val recyclerView: RecyclerView? = view.rootView.findViewById(recyclerViewId) as RecyclerView
if (recyclerView != null && recyclerView.id == recyclerViewId) {
val viewHolder = recyclerView.findViewHolderForAdapterPosition(position)
viewHolder?.let { checkedViewHolder ->
childView = checkedViewHolder.itemView
val views = getShownViewsById(view.rootView as ViewGroup, recyclerViewId)
if (views != null && views.isNotEmpty()) {
val recyclerView: RecyclerView = views[0] as RecyclerView
if (recyclerView.id == recyclerViewId) {
val viewHolder = recyclerView.findViewHolderForAdapterPosition(position)
viewHolder?.let { checkedViewHolder ->
childView = checkedViewHolder.itemView
}
} else {
return false
}
} else {
return false
Expand All @@ -186,4 +194,31 @@ object BaristaListAssertions {
view == targetView
}
}

private fun getShownViewsById(root: ViewGroup, viewId: Int): ArrayList<View>? {
val views = ArrayList<View>()
val childCount = root.childCount
for (i in 0 until childCount) {
val child = root.getChildAt(i)
if (child is ViewGroup) {
views.addAll(getShownViewsById(child, viewId)!!)
}
val childId = child.id
if (childId == viewId && isShowOnScreen(child)) {
views.add(child)
}
}
return views
}

private fun isShowOnScreen(view: View): Boolean {
if (!view.isShown) {
return false
}
val actualPosition = Rect().also { view.getGlobalVisibleRect(it) }
val screen = Resources.getSystem().displayMetrics.run {
Rect(0, 0, widthPixels, heightPixels)
}
return actualPosition.intersect(screen)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.schibsted.spain.barista.sample;

import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static com.schibsted.spain.barista.assertion.BaristaListAssertions.assertDisplayedAtPosition;
import static com.schibsted.spain.barista.interaction.BaristaSleepInteractions.sleep;
import static com.schibsted.spain.barista.interaction.BaristaViewPagerInteractions.swipeViewPagerBack;
import static com.schibsted.spain.barista.interaction.BaristaViewPagerInteractions.swipeViewPagerForward;

@RunWith(AndroidJUnit4.class)
public class ListViewWithDifferentDataInsideViewPagerTest {

@Rule
public ActivityTestRule<ListViewsWithDifferentDataInsideViewPagerActivity> activityRule =
new ActivityTestRule<>(ListViewsWithDifferentDataInsideViewPagerActivity.class);

@Test
public void checkClickRecyclerViewItem() {
swipeViewPagerForward(R.id.pager);
sleep(500);

assertDisplayedAtPosition(R.id.listview, 1, "Marionberry");

swipeViewPagerBack(R.id.pager);
sleep(500);

assertDisplayedAtPosition(R.id.listview, 0, android.R.id.text1, "Apple");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.schibsted.spain.barista.sample;

import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static com.schibsted.spain.barista.assertion.BaristaListAssertions.assertDisplayedAtPosition;
import static com.schibsted.spain.barista.interaction.BaristaSleepInteractions.sleep;
import static com.schibsted.spain.barista.interaction.BaristaViewPagerInteractions.swipeViewPagerBack;
import static com.schibsted.spain.barista.interaction.BaristaViewPagerInteractions.swipeViewPagerForward;

@RunWith(AndroidJUnit4.class)
public class RecyclerViewWithDifferentDataInsideViewPagerTest {

@Rule
public ActivityTestRule<RecyclerViewsWithDifferentDataInsideViewPagerActivity> activityRule =
new ActivityTestRule<>(RecyclerViewsWithDifferentDataInsideViewPagerActivity.class);

@Test
public void checkClickRecyclerViewItem() {
swipeViewPagerForward(R.id.pager);
sleep(500);

assertDisplayedAtPosition(R.id.recycler, 1, "Marionberry");

swipeViewPagerBack(R.id.pager);
sleep(500);

assertDisplayedAtPosition(R.id.recycler, 0, R.id.textview, "Apple");
}
}
2 changes: 2 additions & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
android:theme="@style/AppTheme.NoActionBar"
/>
<activity android:name=".RecyclerViewsInsideViewPagerActivity"/>
<activity android:name=".RecyclerViewsWithDifferentDataInsideViewPagerActivity"/>
<activity android:name=".ListViewsWithDifferentDataInsideViewPagerActivity"/>
<activity android:name=".NestedScrollViewActivity"/>
<activity android:name=".CameraActivity">
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.schibsted.spain.barista.sample;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

public class ListViewsWithDifferentDataInsideViewPagerActivity extends AppCompatActivity {

private static final int NUM_PAGES = 2;

private static final String[] FRUIT_LIST_ONE = {
"Apple", "Apricot", "Avocado", "Banana", "Bilberry", "Blackberry", "Blackcurrant",
"Blueberry", "Boysenberry", "Currant", "Cherry", "Cherimoya", "Cloudberry", "Coconut",
"Cranberry", "Cucumber", "Custardapple", "Damson", "Date", "Dragonfruit", "Durian",
"Elderberry", "Feijoa", "Fig", "Gojiberry", "Gooseberry", "Grape", "Raisin", "Grapefruit",
"Guava", "Honeyberry", "Huckleberry", "Jabuticaba", "Jackfruit", "Jambul", "Jujube",
"Juniperberry", "Kiwifruit", "Kumquat", "Lemon", "Lime", "Loquat", "Longan", "Lychee"};

private static final String[] FRUIT_LIST_TWO = {"Mango", "Marionberry", "Melon", "Cantaloupe", "Honeydew", "Watermelon", "Miraclefruit",
"Mulberry", "Nectarine", "Nance", "Olive", "Orange", "Bloodorange", "Clementine", "Mandarine",
"Tangerine", "Papaya", "Passionfruit", "Peach", "Pear", "Persimmon", "Physalis", "Plantain",
"Plum", "Prune(driedplum)", "Pineapple", "Plumcot(orPluot)", "Pomegranate", "Pomelo",
"Purplemangosteen", "Quince", "Raspberry", "Salmonberry", "Rambutan", "Redcurrant",
"Salalberry", "Salak", "Satsuma", "Starfruit", "Solanumquitoense", "Strawberry", "Tamarillo",
"Tamarind", "Uglifruit", "Yuzu"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewpager);

ViewPager mPager = findViewById(R.id.pager);
PagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}

private static class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
return new ScreenSlidePageFragment(position);
}

@Override
public int getCount() {
return NUM_PAGES;
}
}

public static class ScreenSlidePageFragment extends Fragment {

private int position;

public ScreenSlidePageFragment(int position) {
this.position = position;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.activity_lists, container, false);

LinearLayout listsContainer = rootView.findViewById(R.id.multi_list_container);

ListView listView = new ListView(requireContext());
if (position == 0) {
listView.setAdapter(new ArrayAdapter<>(requireContext(), android.R.layout.simple_list_item_1, FRUIT_LIST_ONE));
} else {
listView.setAdapter(new ArrayAdapter<>(requireContext(), android.R.layout.simple_list_item_1, FRUIT_LIST_TWO));
}
listView.setId(R.id.listview);
listsContainer.addView(listView,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT,
1
));

return rootView;
}
}
}
Loading