-
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check that click() scrolls when needed (#64)
* 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
1 parent
a1d39be
commit f9f6c9b
Showing
13 changed files
with
207 additions
and
20 deletions.
There are no files selected for viewing
51 changes: 45 additions & 6 deletions
51
library/src/main/java/com/schibsted/spain/barista/BaristaClickActions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
sample/src/androidTest/java/com/schibsted/spain/barista/sample/ClickInsideViewPagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
sample/src/main/java/com/schibsted/spain/barista/sample/ViewPagerButtonFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
.../src/main/java/com/schibsted/spain/barista/sample/ViewPagerWithFiveSamePagesActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters