Skip to content

Commit

Permalink
Check that click() scrolls when needed (#64)
Browse files Browse the repository at this point in the history
* Use strings.xml to reuse button labels, which will be great to reuse them on instrumentation tests

* Reproduce the issue #62 and improve test method names

* Fix the issue described by the new tests

* Reproduce the issue of clicking a Button repeated in multiple ViewPager pages

* Fix the issue described at last test

* Use String.valueOf() instead of ""+int to avoid a warning

* Name the exceptions to explain what they catch

* Extract methods that call Espresso to avoid having two level of abstaction levels at click() methods

* Avoid unused imports

* Avoid lines larger than 140 chars
  • Loading branch information
rocboronat authored May 17, 2017
1 parent a1d39be commit f9f6c9b
Show file tree
Hide file tree
Showing 13 changed files with 207 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,34 +1,73 @@
package com.schibsted.spain.barista;

import android.support.annotation.IdRes;
import android.support.test.espresso.AmbiguousViewMatcherException;
import android.support.test.espresso.PerformException;
import android.support.test.espresso.action.ViewActions;

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(displayedWithId(id)).perform(scrollTo(), ViewActions.click());
} catch (PerformException e) {
onView(displayedWithId(id)).perform(ViewActions.click());
scrollAndClickView(id);
} catch (AmbiguousViewMatcherException multipleViewsMatched) {
try {
scrollAndClickDisplayedView(id);
} catch (PerformException parentIsNotAnScrollView) {
clickDisplayedView(id);
}
} catch (PerformException parentIsNotAnScrollView) {
clickDisplayedView(id);
}
}

public static void click(String text) {
try {
onView(displayedWithText(text)).perform(scrollTo(), ViewActions.click());
} catch (PerformException e) {
onView(displayedWithText(text)).perform(ViewActions.click());
scrollAndClickView(text);
} catch (AmbiguousViewMatcherException multipleViewsMatched) {
try {
scrollAndClickDisplayedView(text);
} catch (PerformException parentIsNotAnScrollView) {
clickDisplayedView(text);
}
} catch (PerformException parentIsNotAnScrollView) {
clickDisplayedView(text);
}
}

public static void clickBack() {
pressBack();
}

private static void scrollAndClickView(@IdRes int id) {
onView(withId(id)).perform(scrollTo(), ViewActions.click());
}

private static void scrollAndClickView(String text) {
onView(withText(text)).perform(scrollTo(), ViewActions.click());
}

private static void scrollAndClickDisplayedView(@IdRes int id) {
onView(displayedWithId(id)).perform(scrollTo(), ViewActions.click());
}

private static void scrollAndClickDisplayedView(String text) {
onView(displayedWithText(text)).perform(scrollTo(), ViewActions.click());
}

private static void clickDisplayedView(@IdRes int id) {
onView(displayedWithId(id)).perform(ViewActions.click());
}

private static void clickDisplayedView(String text) {
onView(displayedWithText(text)).perform(ViewActions.click());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.schibsted.spain.barista.sample;

import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
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.BaristaClickActions.click;

@RunWith(AndroidJUnit4.class)
public class ClickInsideViewPagerTest {

@Rule
public ActivityTestRule<ViewPagerWithFiveSamePagesActivity> activityRule =
new ActivityTestRule<>(ViewPagerWithFiveSamePagesActivity.class);

@Test
public void clickWorksAlsoWhenButtonIsRepeatedInMultipleViewPagerViews_byId() {
click(R.id.button);
assertDisplayed(R.string.click);
}

@Test
public void clickWorksAlsoWhenButtonIsRepeatedInMultipleViewPagerViews_byText() {
click("Centered button");
assertDisplayed(R.string.click);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,43 @@
import static com.schibsted.spain.barista.BaristaClickActions.clickBack;

@RunWith(AndroidJUnit4.class)
public class ButtonsTest {
public class ClickTest {

@Rule
public ActivityTestRule<FlowFirstScreen> activityRule = new ActivityTestRule<>(FlowFirstScreen.class);

@Test
public void checkClickById() {
public void checkClick_byId() {
click(R.id.next);
assertDisplayed("Hi! I'm the second screen!");
}

@Test
public void checkClickByText() {
public void checkClick_byText() {
click("Next");
assertDisplayed("Hi! I'm the second screen!");
}

@Test
public void checkClickWhenParentIsNotAnScrollView() {
public void checkClickScrollsIfNeeded_byId() {
click(R.id.really_far_away_button);
assertDisplayed("Hi! I'm the second screen!");
}

@Test
public void checkClickScrollsIfNeeded_byText() {
click("Really far away button");
assertDisplayed("Hi! I'm the second screen!");
}

@Test
public void checkClickWhenParentIsNotAnScrollView_byId() {
click(R.id.centered_button);
assertDisplayed("Hi! I'm the second screen!");
}

@Test
public void checkClickWhenParentIsNotAnScrollView_byText() {
click("Centered button");
assertDisplayed("Hi! I'm the second screen!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
public class ViewPagerTest {

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

@Test
public void checkSwipeForward() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
public class ViewPagerWithoutIdTest {

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

@Test
public void checkSwipeForward() {
Expand Down
3 changes: 2 additions & 1 deletion sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
<activity android:name=".RecyclerViewActivity"/>
<activity android:name=".ListViewActivity"/>
<activity android:name=".LabelActivity"/>
<activity android:name=".ViewPagerActivity"/>
<activity android:name=".ViewPagerWithTwoDifferentPagesActivity"/>
<activity android:name=".ViewPagerWithFiveSamePagesActivity"/>
<activity android:name=".DialogActivity"/>
<activity android:name=".PreferencesActivity"/>
<activity android:name=".DatabaseActivity"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private class PutClickedIdOnTextView implements RadioGroup.OnCheckedChangeListen
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
TextView textView = (TextView) findViewById(R.id.selected_item);
textView.setText("" + checkedId);
textView.setText(String.valueOf(checkedId));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.schibsted.spain.barista.sample;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class ViewPagerButtonFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View root = inflater.inflate(R.layout.activity_centered_button, container, false);
root.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View view) {
TextView tv = (TextView) root.findViewById(R.id.textview);
tv.setText(R.string.click);
}
});
return root;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.schibsted.spain.barista.sample;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;

public class ViewPagerWithFiveSamePagesActivity extends FragmentActivity {

private static final int NUM_PAGES = 5;

private ViewPager pager;

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

PagerAdapter adapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(adapter);
}

@Override
public void onBackPressed() {
if (pager.getCurrentItem() == 0) {
super.onBackPressed();
} else {
pager.setCurrentItem(pager.getCurrentItem() - 1);
}
}

private static class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
return new ViewPagerButtonFragment();
}

@Override
public int getCount() {
return NUM_PAGES;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;

public class ViewPagerActivity extends FragmentActivity {
public class ViewPagerWithTwoDifferentPagesActivity extends FragmentActivity {

private static final int NUM_PAGES = 2;

Expand Down
19 changes: 19 additions & 0 deletions sample/src/main/res/layout/activity_centered_button.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/centered_button"/>

<TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"/>
</LinearLayout>
6 changes: 3 additions & 3 deletions sample/src/main/res/layout/activity_flow_1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"/>
android:text="@string/next"/>

<Button
android:id="@+id/really_far_away_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="1000dp"
android:text="Really far away button"/>
android:text="@string/really_far_away_button"/>
</LinearLayout>
</ScrollView>

Expand All @@ -37,5 +37,5 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Centered button"/>
android:text="@string/centered_button"/>
</FrameLayout>
4 changes: 4 additions & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<resources>
<string name="app_name">Barista</string>
<string name="next">Next</string>
<string name="really_far_away_button">Really far away button</string>
<string name="centered_button">Centered button</string>
<string name="hello_world">Hello world!</string>
<string name="repeated">Repeated</string>
<string name="im_invisible">I\'m invisible!</string>
Expand All @@ -10,4 +13,5 @@
<string name="hint">Hint</string>
<string name="checked_checkbox">Checked checkbox</string>
<string name="unchecked_checkbox">Unchecked checkbox</string>
<string name="click">Click</string>
</resources>

0 comments on commit f9f6c9b

Please sign in to comment.