Skip to content

Commit

Permalink
Fix RecyclerView assert description
Browse files Browse the repository at this point in the history
  • Loading branch information
aleks-ex committed Nov 23, 2023
1 parent 5e1a4fc commit 91767b4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.BoundedMatcher
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.util.TreeIterables
import com.atiurin.ultron.core.config.UltronConfig
import com.atiurin.ultron.core.config.UltronConfig.Espresso.Companion.RECYCLER_VIEW_ITEM_SEARCH_LIMIT
import com.atiurin.ultron.core.config.UltronConfig.Espresso.Companion.RECYCLER_VIEW_LOAD_TIMEOUT
import com.atiurin.ultron.core.config.UltronConfig.Espresso.Companion.RECYCLER_VIEW_OPERATIONS_TIMEOUT
import com.atiurin.ultron.core.espresso.EspressoOperationResult
import com.atiurin.ultron.core.espresso.UltronEspresso.executeAssertion
import com.atiurin.ultron.core.espresso.UltronEspressoInteraction
import com.atiurin.ultron.core.espresso.UltronEspressoOperation
import com.atiurin.ultron.core.espresso.assertion.EspressoAssertionType
Expand Down Expand Up @@ -299,7 +297,7 @@ open class UltronRecyclerView(
fun assertEmpty() {
AssertUtils.assertTrue(
{ getSize() == 0 }, operationTimeoutMs,
"RecyclerView($recyclerViewMatcher) has no items (actual size = ${getSize()})"
{ "RecyclerView($recyclerViewMatcher) has no items (actual size = ${getSize()})" }
)
}

Expand All @@ -309,7 +307,7 @@ open class UltronRecyclerView(
fun assertNotEmpty() {
AssertUtils.assertTrue(
{ getSize() > 0 }, operationTimeoutMs,
"RecyclerView($recyclerViewMatcher) is NOT empty"
{ "RecyclerView($recyclerViewMatcher) is NOT empty" }
)
}

Expand All @@ -319,7 +317,7 @@ open class UltronRecyclerView(
open fun assertSize(expected: Int) {
AssertUtils.assertTrue(
{ getSize() == expected }, operationTimeoutMs,
"RecyclerView($recyclerViewMatcher) size is $expected (actual size = ${getSize()})"
{ "RecyclerView($recyclerViewMatcher) size is $expected (actual size = ${getSize()})" }
)
}

Expand All @@ -329,7 +327,7 @@ open class UltronRecyclerView(
open fun assertHasItemAtPosition(position: Int) {
AssertUtils.assertTrue(
{ getSize() >= position }, operationTimeoutMs,
"Wait RecyclerView($recyclerViewMatcher) size >= $position (actual size = ${getSize()})"
{ "Wait RecyclerView($recyclerViewMatcher) size >= $position (actual size = ${getSize()})" }
)
}

Expand All @@ -343,7 +341,7 @@ open class UltronRecyclerView(
AssertUtils.assertTrue(
{ !isItemExist(matcher) },
timeoutMs,
"RecyclerView($recyclerViewMatcher) has no item matched '$matcher'"
{ "RecyclerView($recyclerViewMatcher) has no item matched '$matcher'" }
)
}

Expand Down
7 changes: 5 additions & 2 deletions ultron/src/main/java/com/atiurin/ultron/utils/AssertUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import com.atiurin.ultron.exceptions.UltronAssertionException
import java.util.concurrent.atomic.AtomicReference

object AssertUtils {
fun assertTrue(block: () -> Boolean, timeoutMs: Long = 5_000, desc: String = "") {
fun assertTrue(block: () -> Boolean, timeoutMs: Long = 5_000, desc: String = "" ) =
assertTrue(block, timeoutMs) { desc }

fun assertTrue(block: () -> Boolean, timeoutMs: Long = 5_000, desc: () -> String = { "" }) {
val startTime = SystemClock.elapsedRealtime()
while (SystemClock.elapsedRealtime() < startTime + timeoutMs){
if (block()) return
}
throw UltronAssertionException("Assertion '$desc' failed during $timeoutMs ms")
throw UltronAssertionException("Assertion '${desc.invoke()}' failed during $timeoutMs ms")
}
fun <R> assertTrueAndReturn(resultContainer: R, block: (R) -> Boolean, timeoutMs: Long = 5_000, desc: String = ""): R {
val finishTime = SystemClock.elapsedRealtime() + timeoutMs
Expand Down

0 comments on commit 91767b4

Please sign in to comment.