Skip to content

Commit

Permalink
Create methods to avoid knowing the ID of the ViewPager if there's on…
Browse files Browse the repository at this point in the history
…ly one ViewPager (#58)

* Test that avoiding passing the R.id.pager also works when there are just one ViewPager

* Implement the solution to the tests

* Introduce new methods on the README.md

* Add Javadoc to clarify the automation of the added methods

* Add a custom matcher for diplayed views assignable from a given class

* Use the new matcher method and reorder methods to meet the newspaper metaphor

* Fix an infinite loop due to a bad copy paste (thanks, FindBugs!)

* Improve a parameter name to offer a better API
  • Loading branch information
rocboronat authored May 11, 2017
1 parent dc7db28 commit e379a39
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ clickDialogNegativeButton();
// Scroll on scrolls and pagers
scrollTo(R.id.button);
scrollTo("A widget with this text");
swipeViewPagerForward();
swipeViewPagerForward(R.id.pager);
swipeViewPagerBack();
swipeViewPagerBack(R.id.pager);

// Interact with the navigation drawer
Expand Down
2 changes: 2 additions & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ dependencies {
}
compile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
compile 'com.android.support:support-annotations:25.1.0'
compile 'com.android.support:support-core-ui:25.1.0'

testCompile 'junit:junit:4.12'
testCompile 'org.assertj:assertj-core:1.7.0'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,40 @@
package com.schibsted.spain.barista;

import android.support.annotation.IdRes;
import android.support.v4.view.ViewPager;

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 com.schibsted.spain.barista.custom.DisplayedMatchers.displayedAssignableFrom;
import static com.schibsted.spain.barista.custom.DisplayedMatchers.displayedWithId;

public class BaristaViewPagerActions {

/*
* Finds a ViewPager and swipes it forward.
*
* It only works if there's only one ViewPager displayed on the screen.
* If there are more than one, use swipeViewPagerForward(@IdRes int id)
*/
public static void swipeViewPagerForward() {
onView(displayedAssignableFrom(ViewPager.class)).perform(swipeLeft());
}

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

/*
* Finds a ViewPager and swipes it back.
*
* It only works if there's only one ViewPager displayed on the screen.
* If there are more than one, use swipeViewPagerBack(@IdRes int id)
*/
public static void swipeViewPagerBack() {
onView(displayedAssignableFrom(ViewPager.class)).perform(swipeRight());
}

public static void swipeViewPagerBack(@IdRes int id) {
onView(displayedWithId(id)).perform(swipeRight());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
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.isAssignableFrom;
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;
Expand All @@ -28,4 +28,9 @@ public static Matcher<View> displayedWithText(@StringRes int text) {
public static Matcher<View> displayedWithText(String text) {
return allOf(isDisplayed(), withText(text));
}

@NonNull
public static Matcher<View> displayedAssignableFrom(final Class<? extends View> viewClass) {
return allOf(isDisplayed(), isAssignableFrom(viewClass));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.schibsted.spain.barista.sample;

import android.support.test.runner.AndroidJUnit4;
import com.schibsted.spain.barista.flakyespresso.FlakyActivityTestRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static com.schibsted.spain.barista.BaristaAssertions.assertDisplayed;
import static com.schibsted.spain.barista.BaristaViewPagerActions.swipeViewPagerBack;
import static com.schibsted.spain.barista.BaristaViewPagerActions.swipeViewPagerForward;

@RunWith(AndroidJUnit4.class)
public class ViewPagerWithoutIdTest {

@Rule
public FlakyActivityTestRule<ViewPagerActivity> activityRule = new FlakyActivityTestRule<>(ViewPagerActivity.class)
.allowFlakyAttemptsByDefault(10);

@Test
public void checkSwipeForward() {
swipeViewPagerForward();

assertDisplayed("2");
}

@Test
public void checkSwipeBack() {
swipeViewPagerForward();
swipeViewPagerBack();

assertDisplayed("1");
}

@Test
public void swipingBackInTheFirstPageDoesntCrash() {
swipeViewPagerBack();

assertDisplayed("1");
}

@Test
public void swipingForwardInTheLastPageDoesntCrash() {
swipeViewPagerForward();
swipeViewPagerForward();

assertDisplayed("2");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ public void letsIntroduceBarista() {
// Scroll on scrolls and pagers
scrollTo(R.id.button);
scrollTo("A widget with this text");
swipeViewPagerForward();
swipeViewPagerForward(R.id.pager);
swipeViewPagerBack();
swipeViewPagerBack(R.id.pager);

// Interact with the navigation drawer
Expand Down

0 comments on commit e379a39

Please sign in to comment.