Skip to content

Commit

Permalink
Add a recycler view test method scrolls to a position (#31)
Browse files Browse the repository at this point in the history
* Add a recycler view test method scrolls to a position

* Add more fruits to have a really long RecyclerView

* Make the rows larger to disturb Espresso

* Add tests to check clicks and scrolls in the stard of the list, the middle and the end

* Introduce Recycler.scrollTo in the README.md

* Add Recycler.scrollTo to the test that checks the content that we add to the README

* Remove unneeded imports
  • Loading branch information
sergiocastilloscmspain authored Mar 16, 2017
1 parent 3fbd0f3 commit bd7630c
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ clickRecyclerViewItem(R.id.recycler, 2);
clickRecyclerViewItem(R.id.recycler, 2, 3, 4);
clickSpinnerItem(R.id.spinner, 1);

// Scroll on AdapterViews
scrollTo(R.id.recycler, 42);

// Select items on RadioButtons and CheckBoxes
clickCheckBoxItem(R.id.first_item);
clickCheckBoxItem("The checkbox text");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.contrib.RecyclerViewActions.scrollToPosition;
import static android.support.test.espresso.matcher.ViewMatchers.withId;

public class BaristaRecyclerViewActions {
Expand All @@ -23,4 +24,8 @@ private static void performClick(@IdRes int recyclerViewId, int position) {
onView(withId(recyclerViewId))
.perform(RecyclerViewActions.actionOnItemAtPosition(position, click()));
}

public static void scrollTo(int recyclerViewId, int position) {
onView(withId(recyclerViewId)).perform(scrollToPosition(position));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import static com.schibsted.spain.barista.BaristaAssertions.assertDisplayed;
import static com.schibsted.spain.barista.BaristaRecyclerViewActions.clickRecyclerViewItem;
import static com.schibsted.spain.barista.BaristaRecyclerViewActions.scrollTo;

@RunWith(AndroidJUnit4.class)
public class RecyclerViewTest {
Expand All @@ -18,12 +19,58 @@ public class RecyclerViewTest {
@Test
public void checkClickRecyclerViewItem_byPosition_atTwo() {
clickRecyclerViewItem(R.id.recycler, 2);
assertDisplayed("Orange");
assertDisplayed("Avocado");
}

//region Clicks
@Test
public void checkClickRecyclerViewItem_byPosition_atThree() {
clickRecyclerViewItem(R.id.recycler, 3);
assertDisplayed("Raspberry");
assertDisplayed("Banana");
}

@Test
public void checkClickRecyclerViewItem_byPosition_atTwenty() {
clickRecyclerViewItem(R.id.recycler, 20);
assertDisplayed("Durian");
}

@Test
public void checkClickRecyclerViewItem_byPosition_atFourty() {
clickRecyclerViewItem(R.id.recycler, 40);
assertDisplayed("Lime");
}

@Test
public void checkClickRecyclerViewItem_byPosition_atSixty() {
clickRecyclerViewItem(R.id.recycler, 60);
assertDisplayed("Papaya");
}
//endregion

//region Scrolls
@Test
public void checkScrollToRecyclerViewItem_byPosition_atZero() {
scrollTo(R.id.recycler, 0);
assertDisplayed("Apple");
}

@Test
public void checkScrollToRecyclerViewItem_byPosition_atTwenty() {
scrollTo(R.id.recycler, 20);
assertDisplayed("Durian");
}

@Test
public void checkScrollToRecyclerViewItem_byPosition_atFourty() {
scrollTo(R.id.recycler, 40);
assertDisplayed("Lime");
}

@Test
public void checkScrollToRecyclerViewItem_byPosition_atSixty() {
scrollTo(R.id.recycler, 60);
assertDisplayed("Papaya");
}
//endregion
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.schibsted.spain.barista.sample.introduction;

import android.support.test.runner.AndroidJUnit4;
import com.schibsted.spain.barista.BaristaRecyclerViewActions;
import com.schibsted.spain.barista.sample.R;
import org.junit.Ignore;
import org.junit.Test;
Expand Down Expand Up @@ -49,6 +50,9 @@ public void letsIntroduceBarista() {
clickRecyclerViewItem(R.id.recycler, 2, 3, 4);
clickSpinnerItem(R.id.spinner, 1);

// Scroll on AdapterViews
BaristaRecyclerViewActions.scrollTo(R.id.recycler, 42);

// Select items on RadioButtons and CheckBoxes
clickCheckBoxItem(R.id.first_item);
clickCheckBoxItem("The checkbox text");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,21 @@

public class RecyclerViewActivity extends AppCompatActivity {

private static final String[] FRUITS = { "Banana", "Apple", "Orange", "Raspberry" };
private static final String[] FRUITS = {
"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", "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) {
Expand Down
6 changes: 4 additions & 2 deletions sample/src/main/res/layout/row_textview.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
android:layout_height="wrap_content"
android:padding="8dp"/>

0 comments on commit bd7630c

Please sign in to comment.