Skip to content

Commit

Permalink
Add assertExist ,javadoc and update README
Browse files Browse the repository at this point in the history
  • Loading branch information
Moutyquecloud committed Jan 11, 2023
1 parent 4455190 commit 8eba3ae
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 13 deletions.
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,22 +197,34 @@ assertDisabled(R.id.button);
```

#### Hope this view doesn't exist!

```java
assertNotExist("Hello world");
assertNotExist(R.string.hello_world);
assertNotExist(R.id.button);
assertNotExist(R.string.hello_world);
assertNotExist(R.id.button);
assertNotExist(allOf(withId(R.id.recycler),isDisplayed()));
```

#### Hope this view does exist!

```java
assertExist("Hello world");
assertExist(R.string.hello_world);
assertExist(R.id.button);
assertExist(allOf(withId(R.id.recycler),isDisplayed()));
```

#### Is the expected checkbox checked?

```java
assertChecked("Checked checkbox");
assertChecked(R.string.checked_checkbox);
assertChecked(R.id.checkbox);
assertChecked(R.string.checked_checkbox);
assertChecked(R.id.checkbox);

// ...or not?
assertUnchecked("Unchecked checkbox");
assertUnchecked(R.string.unchecked_checkbox);
assertUnchecked(R.id.checkbox);
assertUnchecked("Unchecked checkbox");
assertUnchecked(R.string.unchecked_checkbox);
assertUnchecked(R.id.checkbox);
```

#### Is this view clickable?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.annotation.StyleRes
import androidx.annotation.StyleableRes
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.doesNotExist
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.platform.app.InstrumentationRegistry
import com.adevinta.android.barista.internal.assertAny
Expand Down Expand Up @@ -54,11 +55,40 @@ object BaristaVisibilityAssertions {
onView(withCompatText(text)).check(doesNotExist())
}

/**
* Assert that no view match the viewMatcher in the hierarchy
*/
@JvmStatic
fun assertNotExist(viewMatcher: Matcher<View>) {
onView(viewMatcher).check(doesNotExist())
}

/**
* Assert that a view match the viewMatcher is in the hierarchy
*/
@JvmStatic
fun assertExist(viewMatcher: Matcher<View>) {
onView(viewMatcher).check(matches(not(doesNotExist())))
}

/**
* Assert that a view with this text is in the hierarchy
*/
@JvmStatic
fun assertExist(text: String) {
onView(withCompatText(text)).check(matches(not(doesNotExist())))
}

/**
* Assert that a view with this id is in the hierarchy
*
* Id must be unique
*/
@JvmStatic
fun assertExist(viewId: Int) {
onView(withId(viewId)).check(matches(not(doesNotExist())))
}

@JvmStatic
fun assertNotDisplayed(viewId: Int) {
viewId.resourceMatcher().assertAny(not(isDisplayed()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.adevinta.android.barista.sample
import android.view.View
import androidx.test.espresso.Espresso
import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.rules.ActivityScenarioRule
import com.adevinta.android.barista.assertion.BaristaListAssertions.assertDisplayedAtPosition
import com.adevinta.android.barista.assertion.BaristaVisibilityAssertions
Expand All @@ -14,7 +14,7 @@ import com.adevinta.android.barista.internal.matcher.withCompatText
import junit.framework.AssertionFailedError
import org.hamcrest.CoreMatchers
import org.hamcrest.Matcher
import org.hamcrest.core.AllOf
import org.hamcrest.core.AllOf.allOf
import org.junit.Assert
import org.junit.Rule
import org.junit.Test
Expand All @@ -35,6 +35,31 @@ class RecyclerViewWithDifferentDataInsideViewPagerTest {
assertDisplayedAtPosition(R.id.recycler, 0, R.id.textview, "Apple")
}

@Test
fun assertExistingView() {
BaristaVisibilityAssertions.assertDisplayed("Apple")
BaristaVisibilityAssertions.assertExist(
allOf(
withCompatText("Apple"),
withId(R.id.textview)
)
)
BaristaVisibilityAssertions.assertExist("Apple")
BaristaVisibilityAssertions.assertExist(R.id.pager)

swipeViewPagerForward(R.id.pager)
BaristaSleepInteractions.sleep(500)
BaristaVisibilityAssertions.assertNotDisplayed("Apple")
BaristaVisibilityAssertions.assertExist(
allOf(
withCompatText("Apple"),
withId(R.id.textview)
)
)
BaristaVisibilityAssertions.assertExist(R.id.pager)

}

@Test
fun assertNotExistingView() {
swipeViewPagerForward(R.id.pager)
Expand All @@ -46,15 +71,15 @@ class RecyclerViewWithDifferentDataInsideViewPagerTest {
Assert.assertTrue(true)
}
Espresso.onView(withCompatText("Apple"))
.check(ViewAssertions.matches(ViewMatchers.withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
.check(ViewAssertions.matches(withEffectiveVisibility(Visibility.VISIBLE)))

val displayedRecyclerView: Matcher<View> =
AllOf.allOf(ViewMatchers.withId(R.id.recycler), ViewMatchers.isDisplayed())
allOf(withId(R.id.recycler), isDisplayed())

BaristaVisibilityAssertions.assertNotExist(
CoreMatchers.allOf(
ViewMatchers.withText("Apple"),
ViewMatchers.isDescendantOfA(displayedRecyclerView)
withText("Apple"),
isDescendantOfA(displayedRecyclerView)
)
)

Expand Down

0 comments on commit 8eba3ae

Please sign in to comment.