Skip to content

Commit

Permalink
Add assertExist and javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
Moutyquecloud committed Jan 11, 2023
1 parent 4455190 commit 4228933
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
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 @@ -4,6 +4,7 @@ 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.withId
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 +15,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 +36,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 Down

0 comments on commit 4228933

Please sign in to comment.