From bcaa3f87876630b76ac7dc12f110db93989190fb Mon Sep 17 00:00:00 2001 From: nenick Date: Sat, 26 Mar 2016 18:03:46 +0100 Subject: [PATCH] different checks for visible and currently display --- RELEASE_NOTES.md | 1 + .../elements/EspViewTest.java | 36 +++++++++++++++++-- .../espressomacchiato/elements/EspView.java | 14 ++++++-- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 4a019dc0..bcd7a29f 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -2,6 +2,7 @@ elements +* EspView - assertions for displayed on screen / state is visible, hidden, gone * EspEditText - assertion for hint text * EspAdapterView - click / count / assert list items * EspListView - click / count / assert list items diff --git a/library/src/androidTest/java/de/nenick/espressomacchiato/elements/EspViewTest.java b/library/src/androidTest/java/de/nenick/espressomacchiato/elements/EspViewTest.java index 22182bad..1e4a0944 100644 --- a/library/src/androidTest/java/de/nenick/espressomacchiato/elements/EspViewTest.java +++ b/library/src/androidTest/java/de/nenick/espressomacchiato/elements/EspViewTest.java @@ -30,12 +30,25 @@ public void testAssertions() { espView.assertIsVisible(); espView.assertIsEnabled(); + espView.assertIsDisplayedOnScreen(); givenViewIsDisabled(); espView.assertIsDisabled(); - givenViewIsHidden(); + givenViewIsInvisible(); espView.assertIsHidden(); + + givenViewIsGone(); + espView.assertIsHidden(); + } + + @Test + public void testAssertIsDisplayedOnScreenFailure() { + givenViewOutsideOfScreen(android.R.id.text2); + EspView.byId(android.R.id.text2).assertIsVisible(); + + exception.expect(AssertionError.class); + EspView.byId(android.R.id.text2).assertIsDisplayedOnScreen(); } @Test @@ -68,7 +81,7 @@ public void testClickSelectsOnlyVisibleView() { espTextView.assertTextIs(VIEW_WAS_CLICKED_MESSAGE); } - private void givenViewIsHidden() { + private void givenViewIsInvisible() { performOnUiThread(new Runnable() { @Override public void run() { @@ -77,6 +90,15 @@ public void run() { }); } + private void givenViewIsGone() { + performOnUiThread(new Runnable() { + @Override + public void run() { + view.setVisibility(View.GONE); + } + }); + } + private void givenViewIsDisabled() { performOnUiThread(new Runnable() { @Override @@ -104,4 +126,14 @@ private void givenClickFeedbackTextView() { messageView.setId(messageViewId); addViewToActivity(messageView, BaseActivity.rootLayout); } + + private void givenViewOutsideOfScreen(int id) { + TextView textView = new TextView(getActivity()); + textView.setHeight(5000); + addViewToActivity(textView, BaseActivity.rootLayout); + + textView = new TextView(getActivity()); + textView.setId(id); + addViewToActivity(textView, BaseActivity.rootLayout); + } } \ No newline at end of file diff --git a/library/src/main/java/de/nenick/espressomacchiato/elements/EspView.java b/library/src/main/java/de/nenick/espressomacchiato/elements/EspView.java index e2b53412..de9a7550 100644 --- a/library/src/main/java/de/nenick/espressomacchiato/elements/EspView.java +++ b/library/src/main/java/de/nenick/espressomacchiato/elements/EspView.java @@ -2,6 +2,7 @@ import android.support.test.espresso.ViewInteraction; import android.support.test.espresso.action.ViewActions; +import android.support.test.espresso.matcher.ViewMatchers; import android.view.View; import org.hamcrest.Matcher; @@ -12,10 +13,13 @@ import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.assertion.ViewAssertions.doesNotExist; import static android.support.test.espresso.assertion.ViewAssertions.matches; +import static android.support.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed; import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; import static android.support.test.espresso.matcher.ViewMatchers.isEnabled; +import static android.support.test.espresso.matcher.ViewMatchers.withEffectiveVisibility; import static android.support.test.espresso.matcher.ViewMatchers.withId; import static org.hamcrest.CoreMatchers.allOf; +import static org.hamcrest.CoreMatchers.anyOf; import static org.hamcrest.CoreMatchers.not; public class EspView { @@ -34,12 +38,18 @@ public EspView(Matcher baseMatcher) { this.baseMatcher = baseMatcher; } + public void assertIsDisplayedOnScreen() { + findView().check(matches(isCompletelyDisplayed())); + } + public void assertIsVisible() { - findView().check(matches(isDisplayed())); + findView().check(matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE))); } public void assertIsHidden() { - findView().check(matches(not(isDisplayed()))); + findView().check(matches(anyOf( + withEffectiveVisibility(ViewMatchers.Visibility.INVISIBLE), + withEffectiveVisibility(ViewMatchers.Visibility.GONE)))); } public void assertNotExist() {