Skip to content

Commit

Permalink
Extend assertNotExist with a new implementation using a viewMatcher a…
Browse files Browse the repository at this point in the history
…s a param (#479)

* Rename .java to .kt

* Add assertNotExist with view mather implementation

* Add assertExist ,javadoc and update README

Co-authored-by: Quentin MOUTY <[email protected]>
  • Loading branch information
moutyque and Moutyquecloud authored Jan 12, 2023
1 parent bce5f90 commit 602f148
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 44 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,19 +7,16 @@ 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.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
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
import com.adevinta.android.barista.internal.matcher.TextColorMatcher
import com.adevinta.android.barista.internal.matcher.TextStyleableColorMatcher
import com.adevinta.android.barista.internal.util.resourceMatcher
import com.adevinta.android.barista.internal.matcher.withCompatText
import com.adevinta.android.barista.internal.util.resourceMatcher
import org.hamcrest.Matcher
import org.hamcrest.Matchers.allOf
import org.hamcrest.Matchers.containsString
import org.hamcrest.Matchers.not
import org.hamcrest.Matchers.*

object BaristaVisibilityAssertions {

Expand Down Expand Up @@ -58,6 +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
1 change: 1 addition & 0 deletions sample/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ dependencies {
androidTestImplementation(libs.testing.assertJ)
androidTestImplementation(libs.testing.mockito.kotlin)
androidTestImplementation(libs.testing.mockito.android)
androidTestImplementation("androidx.test.ext:junit:1.1.3")

testImplementation(libs.testing.jUnit)

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
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.ext.junit.rules.ActivityScenarioRule
import com.adevinta.android.barista.assertion.BaristaListAssertions.assertDisplayedAtPosition
import com.adevinta.android.barista.assertion.BaristaVisibilityAssertions
import com.adevinta.android.barista.interaction.BaristaSleepInteractions
import com.adevinta.android.barista.interaction.BaristaViewPagerInteractions.swipeViewPagerBack
import com.adevinta.android.barista.interaction.BaristaViewPagerInteractions.swipeViewPagerForward
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.allOf
import org.junit.Assert
import org.junit.Rule
import org.junit.Test

class RecyclerViewWithDifferentDataInsideViewPagerTest {
@Rule
var activityRule = ActivityScenarioRule(
RecyclerViewsWithDifferentDataInsideViewPagerActivity::class.java
)

@Test
fun checkClickRecyclerViewItem() {
swipeViewPagerForward(R.id.pager)
BaristaSleepInteractions.sleep(500)
assertDisplayedAtPosition(R.id.recycler, 1, "Marionberry")
swipeViewPagerBack(R.id.pager)
BaristaSleepInteractions.sleep(500)
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)
BaristaSleepInteractions.sleep(500)
try {
BaristaVisibilityAssertions.assertNotExist("Apple")
Assert.assertFalse(false)
} catch (error: AssertionFailedError) {
Assert.assertTrue(true)
}
Espresso.onView(withCompatText("Apple"))
.check(ViewAssertions.matches(withEffectiveVisibility(Visibility.VISIBLE)))

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

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

assertDisplayedAtPosition(R.id.recycler, 1, "Marionberry")
swipeViewPagerBack(R.id.pager)
BaristaSleepInteractions.sleep(500)
assertDisplayedAtPosition(R.id.recycler, 0, R.id.textview, "Apple")
}
}

0 comments on commit 602f148

Please sign in to comment.