Skip to content

Commit

Permalink
Only interact with visible things (#51)
Browse files Browse the repository at this point in the history
* Add an Activity with a ViewPager that contains five RecyclerViews with several fruits

* Reproduce the issue #49 with tests

* Remove all tests that are also tested on RecyclerViewTest to avoid testing same behavior multiple times

* Also reproduce the issue in the method that uses the ID, not the text

* Fix the issue reproduced by the tests

* Reformat code with SquareAndroid style to pass the checkstyle rule

* Fix a Findbugs violation making a inner class static

* Avoid taking in consideration the position of the item in the nested RecyclerView tests

* Use uppercase to showcase constants at code (thanks, Checkstyle!)

* Create Matchers that just match with displayed items

* Use the new matchers on all BaristaActions that interact only with the visible widgets

* Avoid inlining the annotations

* Reverse the calls to Espresso matchers to exactly meet the method name

* Remove uneeded imports
  • Loading branch information
rocboronat authored May 10, 2017
1 parent b8c3344 commit 9cd61d0
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import android.util.Log;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static com.schibsted.spain.barista.BaristaScrollActions.scrollTo;
import static com.schibsted.spain.barista.custom.AutocompleteViewActions.replaceAutocomplete;
import static com.schibsted.spain.barista.custom.DisplayedMatchers.displayedWithId;

public class BaristaAutoCompleteTextViewActions {

Expand All @@ -18,7 +18,7 @@ public static void writeToAutoCompleteTextView(@IdRes int id, String text) {
Log.d("Barista",
"The View's parent is not a ScrollView. Due to the power of Barista, you can ignore this error message");
} finally {
onView(withId(id)).perform(replaceAutocomplete(text));
onView(displayedWithId(id)).perform(replaceAutocomplete(text));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.Espresso.pressBack;
import static android.support.test.espresso.action.ViewActions.scrollTo;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static com.schibsted.spain.barista.custom.DisplayedMatchers.displayedWithId;
import static com.schibsted.spain.barista.custom.DisplayedMatchers.displayedWithText;

public class BaristaClickActions {

public static void click(@IdRes int id) {
try {
onView(withId(id)).perform(scrollTo(), ViewActions.click());
onView(displayedWithId(id)).perform(scrollTo(), ViewActions.click());
} catch (PerformException e) {
onView(withId(id)).perform(ViewActions.click());
onView(displayedWithId(id)).perform(ViewActions.click());
}
}

public static void click(String text) {
try {
onView(withText(text)).perform(scrollTo(), ViewActions.click());
onView(displayedWithText(text)).perform(scrollTo(), ViewActions.click());
} catch (PerformException e) {
onView(withText(text)).perform(ViewActions.click());
onView(displayedWithText(text)).perform(ViewActions.click());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static com.schibsted.spain.barista.BaristaScrollActions.scrollTo;
import static com.schibsted.spain.barista.custom.DisplayedMatchers.displayedWithId;

public class BaristaEditTextActions {

Expand All @@ -18,7 +18,7 @@ public static void writeToEditText(@IdRes int id, String text) {
Log.d("Barista",
"The View's parent is not a ScrollView. Due to the power of Barista, you can ignore this error message");
} finally {
onView(withId(id)).perform(replaceText(text));
onView(displayedWithId(id)).perform(replaceText(text));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

import android.support.test.espresso.contrib.PickerActions;
import android.widget.DatePicker;

import org.hamcrest.Matchers;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.matcher.ViewMatchers.withClassName;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static com.schibsted.spain.barista.custom.DisplayedMatchers.displayedWithId;

public class BaristaPickerActions {

public static void setDateOnPicker(int year, int month, int day) {
onView(withClassName(Matchers.equalTo(DatePicker.class.getName()))).perform(PickerActions.setDate(year, month, day));
onView(withId(android.R.id.button1)).perform(click());
onView(withClassName(Matchers.equalTo(DatePicker.class.getName()))).perform(
PickerActions.setDate(year, month, day));
onView(displayedWithId(android.R.id.button1)).perform(click());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static com.schibsted.spain.barista.custom.DisplayedMatchers.displayedWithId;
import static com.schibsted.spain.barista.custom.DisplayedMatchers.displayedWithText;
import static com.schibsted.spain.barista.custom.HelperMatchers.atPosition;
import static org.hamcrest.Matchers.allOf;

public class BaristaRadioButtonActions {

public static void clickRadioButtonItem(@IdRes int radioGroupId, @IdRes int itemToClickId) {
onView(allOf(withParent(withId(radioGroupId)), withId(itemToClickId))).perform(click());
onView(
allOf(withParent(displayedWithId(radioGroupId)), displayedWithId(itemToClickId))).perform(
click());
}

public static void clickRadioButtonItem(@IdRes int radioGroupId, String text) {
onView(allOf(withParent(withId(radioGroupId)), withText(text))).perform(click());
onView(allOf(withParent(displayedWithId(radioGroupId)), displayedWithText(text))).perform(
click());
}

public static void clickRadioButtonPosition(@IdRes int radioGroupId, int position) {
onView(atPosition(position, withParent(withId(radioGroupId)))).perform(click());
onView(atPosition(position, withParent(displayedWithId(radioGroupId)))).perform(click());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.contrib.RecyclerViewActions.scrollToPosition;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static com.schibsted.spain.barista.custom.ClickChildAction.clickChildWithId;
import static com.schibsted.spain.barista.custom.ClickChildAction.clickChildWithText;
import static com.schibsted.spain.barista.custom.DisplayedMatchers.displayedWithId;
import static com.schibsted.spain.barista.custom.PerformClickAction.clickUsingPerformClick;
import static org.hamcrest.core.AllOf.allOf;

public class BaristaRecyclerViewActions {

Expand All @@ -26,21 +24,23 @@ public static void clickRecyclerViewItem(@IdRes int recyclerViewId, int... posit
}

private static void performClick(@IdRes int recyclerViewId, int position) {
onView(allOf(withId(recyclerViewId), isDisplayed())).perform(
onView(displayedWithId(recyclerViewId)).perform(
RecyclerViewActions.actionOnItemAtPosition(position, clickUsingPerformClick()));
}

public static void scrollTo(int recyclerViewId, int position) {
onView(allOf(withId(recyclerViewId), isDisplayed())).perform(scrollToPosition(position));
onView(displayedWithId(recyclerViewId)).perform(scrollToPosition(position));
}

public static void clickRecyclerViewItemChild(@IdRes int recyclerViewId, int position, @IdRes int itemToClickId) {
onView(allOf(withId(recyclerViewId), isDisplayed())).perform(
public static void clickRecyclerViewItemChild(@IdRes int recyclerViewId, int position,
@IdRes int itemToClickId) {
onView(displayedWithId(recyclerViewId)).perform(
RecyclerViewActions.actionOnItemAtPosition(position, clickChildWithId(itemToClickId)));
}

public static void clickRecyclerViewItemChild(@IdRes int recyclerViewId, int position, String text) {
onView(allOf(withId(recyclerViewId), isDisplayed())).perform(
public static void clickRecyclerViewItemChild(@IdRes int recyclerViewId, int position,
String text) {
onView(displayedWithId(recyclerViewId)).perform(
RecyclerViewActions.actionOnItemAtPosition(position, clickChildWithText(text)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.swipeLeft;
import static android.support.test.espresso.action.ViewActions.swipeRight;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static com.schibsted.spain.barista.custom.DisplayedMatchers.displayedWithId;

public class BaristaSwipeActions {

public static void swipePagerForward(@IdRes int id) {
onView(withId(id)).perform(swipeLeft());
onView(displayedWithId(id)).perform(swipeLeft());
}

public static void swipePagerBack(@IdRes int id) {
onView(withId(id)).perform(swipeRight());
onView(displayedWithId(id)).perform(swipeRight());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.schibsted.spain.barista.custom;

import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.view.View;

import org.hamcrest.Matcher;

import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.core.AllOf.allOf;

public class DisplayedMatchers {

@NonNull
public static Matcher<View> displayedWithId(@IdRes int id) {
return allOf(isDisplayed(), withId(id));
}

@NonNull
public static Matcher<View> displayedWithText(@StringRes int text) {
return allOf(isDisplayed(), withText(text));
}

@NonNull
public static Matcher<View> displayedWithText(String text) {
return allOf(isDisplayed(), withText(text));
}
}

0 comments on commit 9cd61d0

Please sign in to comment.