Skip to content

Commit

Permalink
Added support for GridView interactions (#188)
Browse files Browse the repository at this point in the history
* Added support for GridView interactions

* Added tests for GridView interactions
  • Loading branch information
jamesbluecrow authored and Sloy committed Dec 12, 2017
1 parent 8d94c74 commit 88f1ae1
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@ package com.schibsted.spain.barista.interaction
import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.Espresso.pressBack
import android.support.test.espresso.ViewAction
import android.support.test.espresso.action.ViewActions.*
import android.support.test.espresso.matcher.ViewMatchers.*
import android.support.test.espresso.action.ViewActions.click
import android.support.test.espresso.action.ViewActions.longClick
import android.support.test.espresso.action.ViewActions.scrollTo
import android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom
import android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA
import android.support.test.espresso.matcher.ViewMatchers.isDisplayed
import android.support.test.espresso.matcher.ViewMatchers.withText
import android.support.v4.widget.NestedScrollView
import android.view.View
import android.widget.AbsListView
import android.widget.HorizontalScrollView
import android.widget.ListView
import android.widget.ScrollView
import com.schibsted.spain.barista.internal.matcher.DisplayedMatchers.displayedAnd
import com.schibsted.spain.barista.internal.viewaction.NestedEnabledScrollToAction.nestedScrollToAction
import com.schibsted.spain.barista.internal.failurehandler.SpyFailureHandler
import com.schibsted.spain.barista.internal.failurehandler.description
import com.schibsted.spain.barista.internal.matcher.DisplayedMatchers.displayedAnd
import com.schibsted.spain.barista.internal.util.resourceMatcher
import com.schibsted.spain.barista.internal.viewaction.NestedEnabledScrollToAction.nestedScrollToAction
import org.hamcrest.Matcher
import org.hamcrest.Matchers.allOf
import org.hamcrest.Matchers.anyOf
Expand Down Expand Up @@ -75,7 +80,7 @@ object BaristaClickInteractions {
anyOf(
isAssignableFrom(ScrollView::class.java),
isAssignableFrom(HorizontalScrollView::class.java),
isAssignableFrom(ListView::class.java),
isAssignableFrom(AbsListView::class.java),
isAssignableFrom(NestedScrollView::class.java)
)
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ import android.support.test.espresso.action.ViewActions.scrollTo
import android.support.test.espresso.assertion.ViewAssertions.matches
import android.support.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition
import android.support.test.espresso.contrib.RecyclerViewActions.scrollToPosition
import android.support.test.espresso.matcher.ViewMatchers.*
import android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom
import android.support.test.espresso.matcher.ViewMatchers.isDisplayed
import android.support.test.espresso.matcher.ViewMatchers.withId
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.RecyclerView.ViewHolder
import android.widget.ListView
import android.widget.AbsListView
import com.schibsted.spain.barista.internal.failurehandler.SpyFailureHandler
import com.schibsted.spain.barista.internal.failurehandler.withFailureHandler
import com.schibsted.spain.barista.internal.viewaction.ClickChildAction.clickChildWithId
import com.schibsted.spain.barista.internal.viewaction.PerformClickAction.clickUsingPerformClick
import org.hamcrest.Matchers.*
import org.hamcrest.Matchers.allOf
import org.hamcrest.Matchers.anyOf
import org.hamcrest.Matchers.anything

object BaristaListInteractions {

Expand Down Expand Up @@ -101,8 +105,8 @@ object BaristaListInteractions {
}

private fun findListViewMatcher(@IdRes id: Int?) = when (id) {
null -> allOf(isDisplayed(), isAssignableFrom(ListView::class.java))
else -> allOf(isDisplayed(), isAssignableFrom(ListView::class.java), withId(id))
null -> allOf(isDisplayed(), isAssignableFrom(AbsListView::class.java))
else -> allOf(isDisplayed(), isAssignableFrom(AbsListView::class.java), withId(id))
}

private fun resourceName(resId: Int) = InstrumentationRegistry.getTargetContext().resources.getResourceName(resId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ public void clickListViewItemChild() throws Exception {
assertResult("yes");
}


@Test
public void clickGridViewItemChild() throws Exception {
openActivity(ListsActivity.buildIntent()
.withComplexGrids(R.id.gridview)
);

clickListItemChild(20, R.id.yes);

assertResult("yes");
}
@Test
public void clickMultipleRecyclerItemChild_byId() throws Exception {
openActivity(ListsActivity.buildIntent()
Expand All @@ -69,6 +80,17 @@ public void clickMultipleListViewItemChild_byId() throws Exception {
assertResult("yes");
}

@Test
public void clickMultipleGridViewItemChild_byId() throws Exception {
openActivity(ListsActivity.buildIntent()
.withComplexGrids(R.id.gridview, R.id.gridview2)
);

clickListItemChild(R.id.gridview, 20, R.id.yes);

assertResult("yes");
}

@Test(expected = PerformException.class)
public void fails_whenListViewChildNotExist() throws Exception {
openActivity(ListsActivity.buildIntent()
Expand All @@ -80,6 +102,17 @@ public void fails_whenListViewChildNotExist() throws Exception {
assertResult("");
}

@Test(expected = PerformException.class)
public void fails_whenGridViewChildNotExist() throws Exception {
openActivity(ListsActivity.buildIntent()
.withComplexGrids(R.id.gridview)
);

clickListItemChild(20, R.id.unknown);

assertResult("");
}

private void openActivity(IntentBuilder intentBuilder) {
activity.launchActivity(intentBuilder.build(InstrumentationRegistry.getTargetContext()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,39 @@ public void clickMultipleListView_byId() throws Exception {
assertResult(getSimpleListViewTextAt(20));
}

@Test
public void clickSimpleGridView() throws Exception {
launchTestActivity(ListsActivity.buildIntent()
.withSimpleGrids(R.id.gridview)
);

clickListItem(20);

assertResult(getSimpleListViewTextAt(20));
}

@Test
public void clickComplexGridView() throws Exception {
launchTestActivity(ListsActivity.buildIntent()
.withComplexGrids(R.id.gridview)
);

clickListItem(20);

assertResult(getComplexListViewTextAt(20));
}

@Test
public void clickMultipleGridView_byId() throws Exception {
launchTestActivity(ListsActivity.buildIntent()
.withSimpleLists(R.id.gridview, R.id.gridview2)
);
clickListItem(R.id.gridview, 20);

assertResult(getSimpleListViewTextAt(20));
}


@Test(expected = BaristaException.class)
public void fail_whenNoViewFound() throws Exception {
launchTestActivity(ListsActivity.buildIntent());
Expand All @@ -106,11 +139,21 @@ public void fail_whenMultipleListsViews_withoutId() throws Exception {
clickListItem(20);
}

@Test(expected = BaristaException.class)
public void fail_whenMultipleGridViews_withoutId() throws Exception {
launchTestActivity(ListsActivity.buildIntent()
.withSimpleGrids(R.id.gridview, R.id.gridview2)
);

clickListItem(20);
}

@Test(expected = BaristaException.class)
public void fail_whenRecyclerAndListView_withoutId() throws Exception {
launchTestActivity(ListsActivity.buildIntent()
.withRecyclers(R.id.recycler)
.withSimpleLists(R.id.listview)
.withSimpleGrids(R.id.gridview)
);

clickListItem(20);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
Expand All @@ -23,6 +24,8 @@ public class ListsActivity extends AppCompatActivity {
private static final String EXTRA_SIMPLE_LISTS = "simpleLists";
private static final String EXTRA_COMPLEX_LISTS = "complexLists";
private static final String EXTRA_RECYCLERS = "recyclers";
private static final String EXTRA_SIMPLE_GRIDS = "simpleGrids";
private static final String EXTRA_COMPLEX_GRIDS = "complexGrids";

static final String[] FRUITS = new String[] {
"Apple", "Apricot", "Avocado", "Banana", "Bilberry", "Blackberry", "Blackcurrant",
Expand Down Expand Up @@ -75,6 +78,14 @@ protected void onCreate(Bundle savedInstanceState) {
for (int id : getIntent().getIntArrayExtra(EXTRA_RECYCLERS)) {
addRecyclerView(id);
}

for (int id : getIntent().getIntArrayExtra(EXTRA_SIMPLE_GRIDS)) {
addSimpleGridView(id);
}

for (int id : getIntent().getIntArrayExtra(EXTRA_COMPLEX_GRIDS)) {
addComplexGridView(id);
}
}

private void addSimpleListView(int id) {
Expand Down Expand Up @@ -108,6 +119,26 @@ private void addRecyclerView(int id) {
addList(recyclerView);
}

private void addSimpleGridView(int id) {
GridView gridView = new GridView(this);
gridView.setId(id);
gridView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, FRUITS));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
clickedResult.setText(getSimpleListViewTextAt(position));
}
});
addList(gridView);
}

private void addComplexGridView(int id) {
GridView gridView = new GridView(this);
gridView.setId(id);
gridView.setAdapter(new TextListViewAdapter(this, FRUITS, clickedResult));
addList(gridView);
}

private void addList(View listView) {
listsContainer.addView(listView,
new LinearLayout.LayoutParams(
Expand All @@ -121,6 +152,8 @@ public static class IntentBuilder {
private int[] simpleListViewIds = {};
private int[] complexListViewIds = {};
private int[] recyclerViewIds = {};
private int[] simpleGridViewIds = {};
private int[] complexGridViewIds = {};

public IntentBuilder withSimpleLists(int... ids) {
simpleListViewIds = ids;
Expand All @@ -137,11 +170,23 @@ public IntentBuilder withRecyclers(int... ids) {
return this;
}

public IntentBuilder withSimpleGrids(int... ids) {
simpleGridViewIds = ids;
return this;
}

public IntentBuilder withComplexGrids(int... ids) {
complexGridViewIds = ids;
return this;
}

public Intent build(Context context) {
Intent intent = new Intent(context, ListsActivity.class);
intent.putExtra(EXTRA_SIMPLE_LISTS, simpleListViewIds);
intent.putExtra(EXTRA_COMPLEX_LISTS, complexListViewIds);
intent.putExtra(EXTRA_RECYCLERS, recyclerViewIds);
intent.putExtra(EXTRA_SIMPLE_GRIDS, simpleGridViewIds);
intent.putExtra(EXTRA_COMPLEX_GRIDS, complexGridViewIds);
return intent;
}
}
Expand Down
2 changes: 2 additions & 0 deletions sample/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
<item name="listview2" type="id" />
<item name="recycler" type="id" />
<item name="recycler2" type="id" />
<item name="gridview" type="id" />
<item name="gridview2" type="id" />
<item name="unknown" type="id" />
</resources>

0 comments on commit 88f1ae1

Please sign in to comment.