Skip to content

Commit

Permalink
Switch to repeatUnitl... implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
stoyicker committed Aug 29, 2020
1 parent 05e2757 commit f4da8aa
Show file tree
Hide file tree
Showing 18 changed files with 461 additions and 180 deletions.
20 changes: 19 additions & 1 deletion .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 19 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# espresso-onautoscrolledtoview
### An Espresso ViewInteraction for finding views that do not exist
[![CircleCI](https://circleci.com/gh/stoyicker/espresso-onautoscrolledtoview.svg?style=svg)](https://circleci.com/gh/stoyicker/espresso-onautoscrolledtoview)

## Usage
[ ![Download](https://api.bintray.com/packages/stoyicker/espresso-onautoscrolledtoview/library/images/download.svg) ](https://search.maven.org/search?q=g:com.github.stoyicker.espresso-onautoscrolledtoview)
```groovy
Expand All @@ -25,10 +25,13 @@ indexes.
To address that, this tool provides the capability of specifying the View that needs to be located
not with an index, but with a regular Hamcrest `Matcher<View>`, exactly the same way as regular
`Espresso.onView(Matcher<View>)` works. In fact, this is how this tool works too: it uses your own
matcher to evaluate your matcher against the viewport: if the evaluation fails, it simulates a
finger swipe gesture with the hopes of it resulting in a different viewport, and then repeats
(or times out); if the evaluations succeeds, it returns `Espresso.onView(Matcher<View>)` with your
matcher.
matcher to evaluate your matcher against the current view hierarchy: if the evaluation fails, it
simulates a finger swipe gesture with the hopes of it resulting in a different view state, and then
repeats (or times out); if the evaluations succeeds, it returns `Espresso.onView(Matcher<View>)`
with your matcher.

Note that if for example you have a RecyclerView and want to assert the position of your item in it,
you can still include such assertion in the matcher that you provide.

## What does it look like?
![Demo gif](demo.gif)
Expand All @@ -40,15 +43,16 @@ There is one method that is your entry point to interacting with this library:
package onautoscrolledtoview;

public final class OnAutoScrolledToView {
public static void onAutoScrolledToView(Matcher<View>, Options)
public static void onAutoScrolledToView(Matcher<View>, Matcher<View>, Options)
}
```

The first parameter is the matcher that describes a
condition to use to find your view, and the second one carries configuration that you may use to
The first parameter is the matcher that describes a condition to use to find your view, the second
one is used to find the view to perform scrolling on (must be a direct or indirect parent of the
view depicted by the first one), and the third one carries configuration that you may use to
customize the scrolling behavior.

You may use `Options.builder()` to get an instance. See [Options](library/src/main/java/onautoscrolledtoview/OnAutoScrolledToView.java#L23)
You may use `Options.builder()` to get a builder instance. See [Options](library/src/main/java/onautoscrolledtoview/OnAutoScrolledToView.java#L24)
for documentation and defaults that are used for each field if unset (or when using
`onAutoScrolledToView(Matcher<View>)`, the `Options`-less overload instead).

Expand All @@ -59,6 +63,12 @@ and it returns successfully, so you may continue to use it as such:
onAutoScrolledToView(withId(R.id.your_id)).check(matches(isDisplayed()));
```

It also works for assertions in the form of matchers, such as `doesNotExist()`:

```java
onAutoScrolledToView(withText("your text")).check(doesNotExist());
```

See [the instrumented tests in demo](demo/src/androidTest/java/onautoscrolledtoview/demo) for more
examples.

Expand Down
Binary file modified demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static onautoscrolledtoview.OnAutoScrolledToView.onAutoScrolledToView;

public final class LTRHorizontalFromEndDemoActivityTest {
@Rule
Expand Down Expand Up @@ -63,13 +65,25 @@ public void element0DoesNotExist() {

@Test
public void element0ExistsWithAutoScroll() {
OnAutoScrolledToView.onAutoScrolledToView(
onAutoScrolledToView(
withText("\n[0]\n"),
withId(R.id.my_recyclerview),
OnAutoScrolledToView.Options.builder()
.directionalPxDeltaPerScroll(
new OnAutoScrolledToView.DirectionalPxDelta(
OnAutoScrolledToView.DirectionalPxDelta.Towards.START))
.axisPxDeltaPerScroll(new OnAutoScrolledToView.AxisPxDelta(
OnAutoScrolledToView.AxisPxDelta.Axis.HORIZONTAL))
.build())
.check(matches(isDisplayed()));
}

@Test
public void elementMinus1DoesNotExistWithAutoScroll() {
onAutoScrolledToView(
withText("\n[-1]\n"),
withId(R.id.my_recyclerview),
OnAutoScrolledToView.Options.builder()
.axisPxDeltaPerScroll(new OnAutoScrolledToView.AxisPxDelta(
OnAutoScrolledToView.AxisPxDelta.Axis.HORIZONTAL))
.build())
.check(doesNotExist());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static onautoscrolledtoview.OnAutoScrolledToView.onAutoScrolledToView;

public final class LTRHorizontalFromStartDemoActivityTest {
@Rule
Expand Down Expand Up @@ -63,13 +65,25 @@ public void element99DoesNotExist() {

@Test
public void element99ExistsWithAutoScroll() {
OnAutoScrolledToView.onAutoScrolledToView(
onAutoScrolledToView(
withText("\n[99]\n"),
withId(R.id.my_recyclerview),
OnAutoScrolledToView.Options.builder()
.directionalPxDeltaPerScroll(
new OnAutoScrolledToView.DirectionalPxDelta(
OnAutoScrolledToView.DirectionalPxDelta.Towards.END))
.axisPxDeltaPerScroll(new OnAutoScrolledToView.AxisPxDelta(
OnAutoScrolledToView.AxisPxDelta.Axis.HORIZONTAL))
.build())
.check(matches(isDisplayed()));
}

@Test
public void element100DoesNotExistWithAutoScroll() {
onAutoScrolledToView(
withText("\n[100]\n"),
withId(R.id.my_recyclerview),
OnAutoScrolledToView.Options.builder()
.axisPxDeltaPerScroll(new OnAutoScrolledToView.AxisPxDelta(
OnAutoScrolledToView.AxisPxDelta.Axis.HORIZONTAL))
.build())
.check(doesNotExist());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static onautoscrolledtoview.OnAutoScrolledToView.onAutoScrolledToView;

public final class RTLHorizontalFromEndDemoActivityTest {
@Rule
Expand Down Expand Up @@ -63,13 +65,25 @@ public void element0DoesNotExist() {

@Test
public void element0ExistsWithAutoScroll() {
OnAutoScrolledToView.onAutoScrolledToView(
onAutoScrolledToView(
withText("\n[0]\n"),
withId(R.id.my_recyclerview),
OnAutoScrolledToView.Options.builder()
.directionalPxDeltaPerScroll(
new OnAutoScrolledToView.DirectionalPxDelta(
OnAutoScrolledToView.DirectionalPxDelta.Towards.START))
.axisPxDeltaPerScroll(new OnAutoScrolledToView.AxisPxDelta(
OnAutoScrolledToView.AxisPxDelta.Axis.HORIZONTAL))
.build())
.check(matches(isDisplayed()));
}

@Test
public void elementMinus1DoesNotExistWithAutoScroll() {
onAutoScrolledToView(
withText("\n[-1]\n"),
withId(R.id.my_recyclerview),
OnAutoScrolledToView.Options.builder()
.axisPxDeltaPerScroll(new OnAutoScrolledToView.AxisPxDelta(
OnAutoScrolledToView.AxisPxDelta.Axis.HORIZONTAL))
.build())
.check(doesNotExist());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static onautoscrolledtoview.OnAutoScrolledToView.onAutoScrolledToView;

public final class RTLHorizontalFromStartDemoActivityTest {
@Rule
Expand Down Expand Up @@ -63,13 +65,25 @@ public void element99DoesNotExist() {

@Test
public void element99ExistsWithAutoScroll() {
OnAutoScrolledToView.onAutoScrolledToView(
onAutoScrolledToView(
withText("\n[99]\n"),
withId(R.id.my_recyclerview),
OnAutoScrolledToView.Options.builder()
.directionalPxDeltaPerScroll(
new OnAutoScrolledToView.DirectionalPxDelta(
OnAutoScrolledToView.DirectionalPxDelta.Towards.END))
.axisPxDeltaPerScroll(new OnAutoScrolledToView.AxisPxDelta(
OnAutoScrolledToView.AxisPxDelta.Axis.HORIZONTAL))
.build())
.check(matches(isDisplayed()));
}

@Test
public void element100DoesNotExistWithAutoScroll() {
onAutoScrolledToView(
withText("\n[100]\n"),
withId(R.id.my_recyclerview),
OnAutoScrolledToView.Options.builder()
.axisPxDeltaPerScroll(new OnAutoScrolledToView.AxisPxDelta(
OnAutoScrolledToView.AxisPxDelta.Axis.HORIZONTAL))
.build())
.check(doesNotExist());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
import org.junit.Test;
import org.junit.rules.TestRule;

import onautoscrolledtoview.OnAutoScrolledToView;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static onautoscrolledtoview.OnAutoScrolledToView.onAutoScrolledToView;

public final class VerticalFromEndDemoActivityTest {
@Rule
Expand Down Expand Up @@ -49,13 +49,13 @@ public void element0DoesNotExist() {

@Test
public void element0ExistsWithAutoScroll() {
OnAutoScrolledToView.onAutoScrolledToView(
withText("\n[0]\n"),
OnAutoScrolledToView.Options.builder()
.directionalPxDeltaPerScroll(
new OnAutoScrolledToView.DirectionalPxDelta(
OnAutoScrolledToView.DirectionalPxDelta.Towards.TOP))
.build())
.check(matches(isDisplayed()));
onAutoScrolledToView(withText("\n[0]\n"), withId(R.id.my_recyclerview)).check(
matches(isDisplayed()));
}

@Test
public void elementMinus1DoesNotExistWithAutoScroll() {
onAutoScrolledToView(withText("\n[-1]\n"), withId(R.id.my_recyclerview)).check(
doesNotExist());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
import org.junit.Test;
import org.junit.rules.TestRule;

import onautoscrolledtoview.OnAutoScrolledToView;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static onautoscrolledtoview.OnAutoScrolledToView.onAutoScrolledToView;

public final class VerticalFromStartDemoActivityTest {
@Rule
Expand Down Expand Up @@ -49,7 +49,13 @@ public void element99DoesNotExist() {

@Test
public void element99ExistsWithAutoScroll() {
OnAutoScrolledToView.onAutoScrolledToView(withText("\n[99]\n")).check(
onAutoScrolledToView(withText("\n[99]\n"), withId(R.id.my_recyclerview)).check(
matches(isDisplayed()));
}

@Test
public void element100DoesNotExistWithAutoScroll() {
onAutoScrolledToView(withText("\n[100]\n"), withId(R.id.my_recyclerview)).check(
doesNotExist());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
Expand All @@ -13,6 +14,7 @@ public final class DemoActivity extends Activity {
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final RecyclerView recyclerView = new RecyclerView(this);
recyclerView.setId(R.id.my_recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(
this,
getIntent().getIntExtra(
Expand All @@ -21,7 +23,9 @@ protected void onCreate(final Bundle savedInstanceState) {
false));
recyclerView.setAdapter(new DemoRecyclerViewAdapter());
recyclerView.scrollToPosition(getIntent().getIntExtra(KEY_SCROLL_TO_POSITION, 0));
setContentView(recyclerView);
this.<ViewGroup>findViewById(android.R.id.content).addView(
recyclerView, new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}

public static Intent newCallingIntent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public void onBindViewHolder(@NonNull DemoRecyclerViewViewHolder holder, int pos

@Override
public int getItemCount() {
return Integer.MAX_VALUE;
return 100;
}
}
4 changes: 4 additions & 0 deletions demo/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" ?>
<resources>
<item name="my_recyclerview" type="id" />
</resources>
1 change: 1 addition & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ repositories {
dependencies {
compileOnly "org.projectlombok:lombok:1.18.12"
annotationProcessor "org.projectlombok:lombok:1.18.12"
compileOnly "androidx.recyclerview:recyclerview:1.1.0"
implementation "androidx.test.espresso:espresso-core:$espressoVersion"
}

Expand Down
Loading

0 comments on commit f4da8aa

Please sign in to comment.