diff --git a/README.md b/README.md index eca051f17..27819226b 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,16 @@ assertNotExist("Hello world"); assertNotExist(R.string.hello_world); assertNotExist(R.id.button); +// Is the expected checkbox checked? +assertChecked("Checked checkbox"); +assertChecked(R.string.checked_checkbox); +assertChecked(R.id.checked_checkbox); + +// ...And the other checkbox unchecked? +assertUnchecked("Unchecked checkbox"); +assertUnchecked(R.string.unchecked_checkbox); +assertUnchecked(R.id.unchecked_checkbox); + // What's the state of the Drawer? assertDrawerIsOpen(R.id.drawer); assertDrawerIsClosed(R.id.drawer); diff --git a/library/src/main/java/com/schibsted/spain/barista/BaristaAssertions.java b/library/src/main/java/com/schibsted/spain/barista/BaristaAssertions.java index 67b6c813a..73d15d8e2 100644 --- a/library/src/main/java/com/schibsted/spain/barista/BaristaAssertions.java +++ b/library/src/main/java/com/schibsted/spain/barista/BaristaAssertions.java @@ -13,8 +13,10 @@ import static android.support.test.espresso.assertion.ViewAssertions.matches; import static android.support.test.espresso.contrib.DrawerMatchers.isClosed; import static android.support.test.espresso.contrib.DrawerMatchers.isOpen; +import static android.support.test.espresso.matcher.ViewMatchers.isChecked; 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.isNotChecked; import static android.support.test.espresso.matcher.ViewMatchers.withHint; import static android.support.test.espresso.matcher.ViewMatchers.withId; import static android.support.test.espresso.matcher.ViewMatchers.withText; @@ -97,6 +99,34 @@ public static void assertDisabled(String text) { onView(withText(text)).check(matches(not(isEnabled()))); } + public static void assertChecked(int id) { + if (isIdResource(id)) { + onView(withId(id)).check(matches(isChecked())); + } else if (isStringResource(id)) { + onView(withText(id)).check(matches(isChecked())); + } else { + throw new BaristaArgumentTypeException(); + } + } + + public static void assertChecked(String text) { + onView(withText(text)).check(matches(isChecked())); + } + + public static void assertUnchecked(int id) { + if (isIdResource(id)) { + onView(withId(id)).check(matches(isNotChecked())); + } else if (isStringResource(id)) { + onView(withText(id)).check(matches(isNotChecked())); + } else { + throw new BaristaArgumentTypeException(); + } + } + + public static void assertUnchecked(String text) { + onView(withText(text)).check(matches(isNotChecked())); + } + public static void assertThatBackButtonClosesTheApp() { try { pressBack(); // Will launch an Exception if it closes the app diff --git a/sample/src/androidTest/java/com/schibsted/spain/barista/sample/AssertionsTest.java b/sample/src/androidTest/java/com/schibsted/spain/barista/sample/AssertionsTest.java index 7207a7581..8a98931a0 100644 --- a/sample/src/androidTest/java/com/schibsted/spain/barista/sample/AssertionsTest.java +++ b/sample/src/androidTest/java/com/schibsted/spain/barista/sample/AssertionsTest.java @@ -7,12 +7,14 @@ import org.junit.Test; import org.junit.runner.RunWith; +import static com.schibsted.spain.barista.BaristaAssertions.assertChecked; import static com.schibsted.spain.barista.BaristaAssertions.assertDisabled; import static com.schibsted.spain.barista.BaristaAssertions.assertDisplayed; import static com.schibsted.spain.barista.BaristaAssertions.assertEnabled; import static com.schibsted.spain.barista.BaristaAssertions.assertNotDisplayed; import static com.schibsted.spain.barista.BaristaAssertions.assertNotExist; import static com.schibsted.spain.barista.BaristaAssertions.assertThatBackButtonClosesTheApp; +import static com.schibsted.spain.barista.BaristaAssertions.assertUnchecked; import static junit.framework.Assert.fail; @RunWith(AndroidJUnit4.class) @@ -166,6 +168,72 @@ public void checkDisabledView_breaksWhenNeeded() { } } + @Test + public void checkCheckedView() { + assertChecked(R.id.checked_checkbox); + assertChecked(R.string.checked_checkbox); + assertChecked("Checked checkbox"); + } + + @Test + public void checkCheckedView_breaksWhenNeeded() { + try { + assertChecked(R.id.unchecked_checkbox); + fail(); + } catch (Throwable expected) { + } + try { + assertChecked(R.string.unchecked_checkbox); + fail(); + } catch (Throwable expected) { + } + try { + assertChecked("Unchecked checkbox"); + fail(); + } catch (Throwable expected) { + } + } + + @Test + public void checkUncheckedView() { + assertUnchecked(R.id.unchecked_checkbox); + assertUnchecked(R.string.unchecked_checkbox); + assertUnchecked("Unchecked checkbox"); + } + + @Test + public void checkUncheckedView_breaksWhenNeeded() { + try { + assertChecked(R.id.checked_checkbox); + fail(); + } catch (Throwable expected) { + } + try { + assertChecked(R.string.checked_checkbox); + fail(); + } catch (Throwable expected) { + } + try { + assertChecked("Checked checkbox"); + fail(); + } catch (Throwable expected) { + } + } + + @Test + public void checkNonCheckableView_breaksWhenCheckStateAsserted() { + try { + assertChecked(R.id.button); + fail(); + } catch (Throwable expected) { + } + try { + assertUnchecked(R.id.button); + fail(); + } catch (Throwable expected) { + } + } + @Test public void methodsByIdLaunchExceptionWhenThePassedIdIsNotAResIdOrAStringId() { try { diff --git a/sample/src/androidTest/java/com/schibsted/spain/barista/sample/introduction/IntroducingBaristaAssertions.java b/sample/src/androidTest/java/com/schibsted/spain/barista/sample/introduction/IntroducingBaristaAssertions.java index ac160ed5c..bf3ed70b8 100644 --- a/sample/src/androidTest/java/com/schibsted/spain/barista/sample/introduction/IntroducingBaristaAssertions.java +++ b/sample/src/androidTest/java/com/schibsted/spain/barista/sample/introduction/IntroducingBaristaAssertions.java @@ -6,6 +6,7 @@ import org.junit.Test; import org.junit.runner.RunWith; +import static com.schibsted.spain.barista.BaristaAssertions.assertChecked; import static com.schibsted.spain.barista.BaristaAssertions.assertDisabled; import static com.schibsted.spain.barista.BaristaAssertions.assertDisplayed; import static com.schibsted.spain.barista.BaristaAssertions.assertDrawerIsClosed; @@ -15,6 +16,7 @@ import static com.schibsted.spain.barista.BaristaAssertions.assertNotDisplayed; import static com.schibsted.spain.barista.BaristaAssertions.assertNotExist; import static com.schibsted.spain.barista.BaristaAssertions.assertThatBackButtonClosesTheApp; +import static com.schibsted.spain.barista.BaristaAssertions.assertUnchecked; @RunWith(AndroidJUnit4.class) public class IntroducingBaristaAssertions { @@ -47,6 +49,16 @@ public void letsIntroduceBarista() { assertNotExist(R.string.hello_world); assertNotExist(R.id.button); + // Is the expected checkbox checked? + assertChecked("Checked checkbox"); + assertChecked(R.string.checked_checkbox); + assertChecked(R.id.checked_checkbox); + + // ...And the other checkbox unchecked? + assertUnchecked("Unchecked checkbox"); + assertUnchecked(R.string.unchecked_checkbox); + assertUnchecked(R.id.unchecked_checkbox); + // What's the state of the Drawer assertDrawerIsOpen(R.id.drawer); assertDrawerIsClosed(R.id.drawer); diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml index 900224660..748eb7a89 100644 --- a/sample/src/main/res/layout/activity_main.xml +++ b/sample/src/main/res/layout/activity_main.xml @@ -43,6 +43,22 @@ android:text="I'm hidden!" android:visibility="gone"/> + + + +