Skip to content

Commit

Permalink
Fixes click on RecyclerView when ItemView is wrapped (#48)
Browse files Browse the repository at this point in the history
* Reproduce the performClick issue with a test (espresso version)

This test proves that click() and clickUsingPerformClick() have different behaviors when clicking the parent (wrapper) of a view.

* Make recyclerview tests less flaky

Tests are passing even though the feature is broken. \o/

* Fixed recyclerview code

The click listener should be on the row (itemView), not in the label (textView).
Otherwise clicking the row won't open the next activity.

* Only use performClick() when the view is clickable.

Otherwise use espresso's click action (which simulates a tap)

* Fix test naming style

* Extract method for helping readability
  • Loading branch information
Sloy authored May 3, 2017
1 parent d4b413f commit 87147e3
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.hamcrest.Matcher;

import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;

public class PerformClickAction {
Expand All @@ -32,7 +33,15 @@ public String getDescription() {

@Override
public void perform(UiController uiController, View view) {
view.performClick();
if (view.isClickable()) {
view.performClick();
} else {
propagateClickToChildren(uiController, view);
}
}

private void propagateClickToChildren(UiController uiController, View view) {
click().perform(uiController, view);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,32 @@ public class RecyclerViewTest {
@Test
public void checkClickRecyclerViewItem_byPosition_atTwo() {
clickRecyclerViewItem(R.id.recycler, 2);
assertDisplayed("Avocado");
assertDisplayed("Avocado has been clicked");
}

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

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

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.schibsted.spain.barista.sample;

import android.support.test.rule.ActivityTestRule;
import org.junit.Rule;
import org.junit.Test;

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 com.schibsted.spain.barista.BaristaAssertions.assertDisplayed;
import static com.schibsted.spain.barista.custom.PerformClickAction.clickUsingPerformClick;

public class WrappedViewClickTest {

@Rule
public ActivityTestRule<WrappedViewActivity> activityTestRule =
new ActivityTestRule<>(WrappedViewActivity.class);

@Test
public void clickOnButton() throws Exception {
onView(withId(R.id.button)).perform(click());

assertDisplayed("Clicked");
}

@Test
public void clickOnButtonWithCustomAction() throws Exception {
onView(withId(R.id.button)).perform(clickUsingPerformClick());

assertDisplayed("Clicked");
}

@Test
public void clickOnButtonWrapper() throws Exception {
onView(withId(R.id.button_wrapper)).perform(click());

assertDisplayed("Clicked");
}

@Test
public void clickOnButtonWrapperWithCustomAction() throws Exception {
onView(withId(R.id.button_wrapper)).perform(clickUsingPerformClick());

assertDisplayed("Clicked");
}
}
1 change: 1 addition & 0 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<activity android:name=".PreferencesActivity"/>
<activity android:name=".DatabaseActivity"/>
<activity android:name=".HelloWorldActivity"/>
<activity android:name=".WrappedViewActivity" />
<activity android:name=".NavigationDrawerActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ public TextAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
return new ViewHolder(root, textView, yesButton, noButton);
}

public void onBindViewHolder(ViewHolder holder, int position) {
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.textView.setText(items[position]);
holder.textView.setOnClickListener(new View.OnClickListener() {
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(activity, LabelActivity.class);
i.putExtra(LabelActivity.EXTRA_TEXT, ((TextView) view).getText().toString());
i.putExtra(LabelActivity.EXTRA_TEXT, holder.textView.getText().toString() + " has been clicked");
activity.startActivity(i);
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.schibsted.spain.barista.sample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

public class WrappedViewActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wrapped_view);

findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
findViewById(R.id.text).setVisibility(View.VISIBLE);
}
});
}
}
33 changes: 33 additions & 0 deletions sample/src/main/res/layout/activity_wrapped_view.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>


<FrameLayout
android:id="@+id/button_wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wrapped button"
/>
</FrameLayout>

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clicked"
android:visibility="gone"
tools:visibility="visible"
/>


</FrameLayout>

0 comments on commit 87147e3

Please sign in to comment.