Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TECH: Consider a RootViewWithoutFocusException in a flaky safety #686

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.kaspersky.kaspresso.internal.exceptions

import java.lang.RuntimeException

/**
* A wrapper for a RootViewWithoutFocusException. It's needed because RootViewWithoutFocusException is a private class,
* so we have to use reflection to catch it and provide a user a sane way to control it.
*
* @see com.kaspersky.kaspresso.params.FlakySafetyParams.Companion.getDefaultAllowedExceptions
* @see com.kaspersky.kaspresso.internal.extensions.other.ThrowableExtKt.isAllowed
*/
class RootViewWithoutFocusWrapperException : RuntimeException()
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kaspersky.kaspresso.internal.extensions.other

import com.kaspersky.kaspresso.internal.exceptions.KaspressoError
import com.kaspersky.kaspresso.internal.exceptions.RootViewWithoutFocusWrapperException
import io.reactivex.exceptions.ExtCompositeException

internal inline fun <reified T : Throwable> invokeSafely(
Expand Down Expand Up @@ -44,15 +45,16 @@ internal fun <T : Throwable> List<T>.throwAll() {
* @return true if the given throwable is contained by [allowed] set, false otherwise.
*/
internal fun <T : Throwable> T.isAllowed(allowed: Set<Class<out Throwable>>): Boolean {
return when (this) {
is ExtCompositeException -> {
exceptions.find { e: Throwable ->
allowed.find { it.isAssignableFrom(e.javaClass) } != null
} != null
}
else -> {
allowed.find { it.isAssignableFrom(javaClass) } != null
}
return when {
javaClass.simpleName == "RootViewWithoutFocusException" -> allowed.find {
it.isAssignableFrom(RootViewWithoutFocusWrapperException::class.java) // RootViewWithoutFocusException class is private, so we cannot access it directly
} != null

this is ExtCompositeException -> exceptions.find {
e: Throwable -> allowed.find { it.isAssignableFrom(e.javaClass) } != null
} != null

else -> allowed.find { it.isAssignableFrom(javaClass) } != null
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.test.espresso.NoMatchingViewException
import androidx.test.espresso.PerformException
import androidx.test.uiautomator.StaleObjectException
import com.kaspersky.components.kautomator.intercept.exception.UnfoundedUiObjectException
import com.kaspersky.kaspresso.internal.exceptions.RootViewWithoutFocusWrapperException
import junit.framework.AssertionFailedError

/**
Expand All @@ -29,7 +30,8 @@ class FlakySafetyParams(
AssertionFailedError::class.java,
UnfoundedUiObjectException::class.java,
StaleObjectException::class.java,
IllegalStateException::class.java
IllegalStateException::class.java,
RootViewWithoutFocusWrapperException::class.java,
)

fun default() = FlakySafetyParams(
Expand Down
Loading