Skip to content
This repository has been archived by the owner on Oct 11, 2022. It is now read-only.

Commit

Permalink
Add BaristaClickableAssertions (AdevintaSpain#277)
Browse files Browse the repository at this point in the history
* Add new methods for asserting whether or not views are clickable

* Write tests for assertClickable and assertNotClickable

* Add clickable assertions

* Remove IdRes annotation from resId parameters

The resId parameter value can be either a view ID or a string resource ID, therefore it does not make sense for it to be annotated with IdRes.
  • Loading branch information
Phixyn authored and Sloy committed Mar 1, 2019
1 parent aed99b6 commit 08ce731
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 1 deletion.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,21 @@ assertUnchecked("Unchecked checkbox");
assertUnchecked(R.string.unchecked_checkbox);
assertUnchecked(R.id.unchecked_checkbox);

// Is this view clickable?
assertClickable("Hello world")
assertClickable(R.string.hello_world)
assertClickable(R.id.button)

// ...or not?
assertNotClickable("Hello world")
assertNotClickable(R.string.hello_world)
assertNotClickable(R.id.button)

// Does this view have the focus?
assertFocused(R.id.focused_view)
assertFocused("Button")

// ... or not?
// ...or not?
assertNotFocused(R.id.focused_view)
assertNotFocused("Button")

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.schibsted.spain.barista.assertion

import android.support.test.espresso.matcher.ViewMatchers.isClickable
import android.support.test.espresso.matcher.ViewMatchers.withText
import com.schibsted.spain.barista.internal.assertAny
import com.schibsted.spain.barista.internal.util.resourceMatcher
import org.hamcrest.Matchers.not

object BaristaClickableAssertions {

@JvmStatic
fun assertClickable(resId: Int) {
resId.resourceMatcher().assertAny(isClickable())
}

@JvmStatic
fun assertClickable(text: String) {
withText(text).assertAny(isClickable())
}

@JvmStatic
fun assertNotClickable(resId: Int) {
resId.resourceMatcher().assertAny(not(isClickable()))
}

@JvmStatic
fun assertNotClickable(text: String) {
withText(text).assertAny(not(isClickable()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import static com.schibsted.spain.barista.assertion.BaristaBackgroundAssertions.assertHasNoBackground;
import static com.schibsted.spain.barista.assertion.BaristaCheckedAssertions.assertChecked;
import static com.schibsted.spain.barista.assertion.BaristaCheckedAssertions.assertUnchecked;
import static com.schibsted.spain.barista.assertion.BaristaClickableAssertions.assertClickable;
import static com.schibsted.spain.barista.assertion.BaristaClickableAssertions.assertNotClickable;
import static com.schibsted.spain.barista.assertion.BaristaEnabledAssertions.assertDisabled;
import static com.schibsted.spain.barista.assertion.BaristaEnabledAssertions.assertEnabled;
import static com.schibsted.spain.barista.assertion.BaristaFocusedAssertions.assertFocused;
Expand Down Expand Up @@ -156,6 +158,58 @@ public void checkNonCheckableView_breaksWhenCheckStateAsserted() {
}
}

@Test
public void checkClickableView() {
assertClickable(R.id.enabled_button);
assertClickable(R.string.enabled_button);
assertClickable("Enabled button");
}

@Test
public void checkClickableView_breaksWhenNeeded() {
try {
assertClickable(R.id.disabled_button);
fail();
} catch (Throwable expected) {
}
try {
assertClickable(R.string.disabled_button);
fail();
} catch (Throwable expected) {
}
try {
assertClickable("Disabled button");
fail();
} catch (Throwable expected) {
}
}

@Test
public void checkNonClickableView() {
assertNotClickable(R.id.disabled_button);
assertNotClickable(R.string.disabled_button);
assertNotClickable("Disabled button");
}

@Test
public void checkNonClickableView_breaksWhenNeeded() {
try {
assertNotClickable(R.id.enabled_button);
fail();
} catch (Throwable expected) {
}
try {
assertNotClickable(R.string.enabled_button);
fail();
} catch (Throwable expected) {
}
try {
assertNotClickable("Enabled button");
fail();
} catch (Throwable expected) {
}
}

@Test
public void methodsByIdLaunchExceptionWhenThePassedIdIsNotAResIdOrAStringId() {
try {
Expand Down
3 changes: 3 additions & 0 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,16 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/enabled_button"
android:clickable="true"
android:focusable="true"
/>

<Button
android:id="@+id/disabled_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:clickable="false"
android:text="@string/disabled_button"
/>

Expand Down

0 comments on commit 08ce731

Please sign in to comment.