Skip to content

Commit

Permalink
Added assertions for isChecked and isUnchecked (#44)
Browse files Browse the repository at this point in the history
* Added assertions for isChecked and isUnchecked

* Added tests and readme updated for check and uncheck tests
  • Loading branch information
sergiandreplace authored Apr 18, 2017
1 parent c257a59 commit 2b81a27
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 16 additions & 0 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@
android:text="I'm hidden!"
android:visibility="gone"/>

<CheckBox
android:id="@+id/checked_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/checked_checkbox"
android:checked="true"
/>

<CheckBox
android:id="@+id/unchecked_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/unchecked_checkbox"
android:checked="false"
/>

<Button
android:id="@+id/enabled_button"
android:layout_width="wrap_content"
Expand Down
2 changes: 2 additions & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
<string name="disabled_button">Disabled button</string>
<string name="centered_edittext">I\'m a centered edittext!</string>
<string name="hint">Hint</string>
<string name="checked_checkbox">Checked checkbox</string>
<string name="unchecked_checkbox">Unchecked checkbox</string>
</resources>

0 comments on commit 2b81a27

Please sign in to comment.