Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show/fix problems when position tested is not visible #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions RecyclerView/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile "com.android.support:recyclerview-v7:22.0.0"
compile 'com.android.support:appcompat-v7:22.2.1'
compile "com.android.support:recyclerview-v7:22.2.1"


// androidTestCompile('com.android.support.test:testing-support-lib:0.1')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public boolean matchesSafely(View view) {
RecyclerView recyclerView =
(RecyclerView) view.getRootView().findViewById(recyclerViewId);
if (recyclerView != null && recyclerView.getId() == recyclerViewId) {
// findViewHolderForAdapterPosition(position) can return null (if position is not visible/too far)
childView = recyclerView.findViewHolderForAdapterPosition(position).itemView;
}
else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dannyroa.espresso_samples.recyclerview;

import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.contrib.RecyclerViewActions;
import android.test.ActivityInstrumentationTestCase2;

import static android.support.test.espresso.Espresso.onView;
Expand Down Expand Up @@ -31,7 +32,15 @@ public void testItemClick() {
onView(withRecyclerView(R.id.recycler_view).atPosition(1)).perform(click());

onView(withId(R.id.team_name)).check(matches(isDisplayed()));
}

public void testFarItemClick() {
// Scroll is mandatory to avoid atPosition(20) fails
onView(withId(R.id.recycler_view)).perform(RecyclerViewActions.scrollToPosition(20));

onView(withRecyclerView(R.id.recycler_view).atPosition(20)).perform(click());

onView(withId(R.id.team_name)).check(matches(isDisplayed()));
}

public void testFollowButtonClick() {
Expand All @@ -46,4 +55,13 @@ public void testFollowButtonClick() {

}

public void testFarFollowButtonClick() {
final int position = 20;
onView(withId(R.id.recycler_view))
.perform(TestUtils.actionOnItemViewAtPosition(position, R.id.follow_button, click()));

String followingText = InstrumentationRegistry.getTargetContext().getString(R.string.following);
onView(withRecyclerView(R.id.recycler_view).atPositionOnView(position, R.id.follow_button))
.check(matches(withText(followingText)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public void perform(UiController uiController, View view) {
(new ScrollToPositionViewAction(this.position)).perform(uiController, view);
uiController.loopMainThreadUntilIdle();

View targetView = recyclerView.getChildAt(this.position).findViewById(this.viewId);
RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(this.position);
View targetView = viewHolder.itemView.findViewById(this.viewId);

if (targetView == null) {
throw (new PerformException.Builder()).withActionDescription(this.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ public class MainActivity extends ActionBarActivity {
teams.add(new Team("Chile"));
teams.add(new Team("Uruguay"));
teams.add(new Team("Colombia"));
teams.add(new Team("France"));
teams.add(new Team("Austria"));
teams.add(new Team("Luxembourg"));
teams.add(new Team("Portugal"));
teams.add(new Team("Vatican"));
teams.add(new Team("Latvia"));
teams.add(new Team("Lithuania"));
teams.add(new Team("Italy"));
teams.add(new Team("Ireland"));

TeamAdapter adapter = new TeamAdapter(MainActivity.this, teams);

Expand Down
4 changes: 3 additions & 1 deletion RecyclerView/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
classpath 'com.android.tools.build:gradle:4.1.1'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand All @@ -15,5 +16,6 @@ buildscript {
allprojects {
repositories {
jcenter()
google()
}
}
4 changes: 2 additions & 2 deletions RecyclerView/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Apr 10 15:27:10 PDT 2013
#Tue Jun 08 22:20:17 CEST 2021
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip