Skip to content

Commit

Permalink
ISSUE-648: Don't restore flaky safety interceptors in the nested flak…
Browse files Browse the repository at this point in the history
…ySafely block
  • Loading branch information
Nikitae57 committed Sep 3, 2024
1 parent 9c94128 commit b0cc107
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 27 deletions.
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ kakaoExtClicks = { module = "io.github.kakaocup:kakao-ext-clicks", version.ref =
junit = "junit:junit:4.13.2"
junitJupiter = "org.junit.jupiter:junit-jupiter:5.9.0"
truth = "com.google.truth:truth:1.3.0"
mockk = "io.mockk:mockk:1.13.12"

androidXTestCore = { module = "androidx.test:core", version.ref = "androidXTest" }
androidXTestRules = { module = "androidx.test:rules", version.ref = "androidXTest" }
Expand Down
1 change: 1 addition & 0 deletions kaspresso/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ dependencies {

testImplementation(libs.junit)
testImplementation(libs.truth)
testImplementation(libs.mockk)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,28 @@ import com.kaspersky.kaspresso.interceptors.behaviorkautomator.impl.flakysafety.
import com.kaspersky.kaspresso.interceptors.tolibrary.KakaoLibraryInjector.injectKaspressoInKakao
import com.kaspersky.kaspresso.interceptors.tolibrary.KakaoLibraryInjector.injectKaspressoInKautomator
import com.kaspersky.kaspresso.kaspresso.Kaspresso
import java.util.concurrent.atomic.AtomicInteger

/**
* The special class that removes all interceptors related to FlakySafety from Kautomator settings
* and restore them by demand
*/
internal class FlakySafeInterceptorScalpel(
private val kaspresso: Kaspresso
) {

private val kaspresso: Kaspresso,
private val scalpelSwitcher: ScalpelSwitcher = ScalpelSwitcher()
) {
private val entriesCount = AtomicInteger()

fun scalpFromLibs() {
scalpelSwitcher.attemptTakeScalp(
actionToDetermineScalp = { determineScalpExistingInKaspresso() },
actionToTakeScalp = {
scalpKakaoInterceptors()
scalpKautomatorInterceptors()
}
)
if (entriesCount.getAndIncrement() == 0) {
scalpelSwitcher.attemptTakeScalp(
actionToDetermineScalp = { determineScalpExistingInKaspresso() },
actionToTakeScalp = {
scalpKakaoInterceptors()
scalpKautomatorInterceptors()
}
)
}
}

private fun determineScalpExistingInKaspresso() =
Expand Down Expand Up @@ -86,24 +89,27 @@ internal class FlakySafeInterceptorScalpel(
}

fun restoreScalpToLibs() {
scalpelSwitcher.attemptRestoreScalp {
injectKaspressoInKakao(
kaspresso.viewBehaviorInterceptors,
kaspresso.dataBehaviorInterceptors,
kaspresso.webBehaviorInterceptors,
kaspresso.viewActionWatcherInterceptors,
kaspresso.viewAssertionWatcherInterceptors,
kaspresso.atomWatcherInterceptors,
kaspresso.webAssertionWatcherInterceptors,
kaspresso.params.clickParams
)
val nestingDepth = entriesCount.decrementAndGet()
if (nestingDepth <= 0) { // prevent restoring the interceptors in case if a "flakySafely" block is nested in an another "flakySafely"
scalpelSwitcher.attemptRestoreScalp {
injectKaspressoInKakao(
kaspresso.viewBehaviorInterceptors,
kaspresso.dataBehaviorInterceptors,
kaspresso.webBehaviorInterceptors,
kaspresso.viewActionWatcherInterceptors,
kaspresso.viewAssertionWatcherInterceptors,
kaspresso.atomWatcherInterceptors,
kaspresso.webAssertionWatcherInterceptors,
kaspresso.params.clickParams
)

injectKaspressoInKautomator(
kaspresso.objectBehaviorInterceptors,
kaspresso.deviceBehaviorInterceptors,
kaspresso.objectWatcherInterceptors,
kaspresso.deviceWatcherInterceptors
)
injectKaspressoInKautomator(
kaspresso.objectBehaviorInterceptors,
kaspresso.deviceBehaviorInterceptors,
kaspresso.objectWatcherInterceptors,
kaspresso.deviceWatcherInterceptors
)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.kaspersky.kaspresso.flakysafety.scalpel

import com.kaspersky.kaspresso.kaspresso.Kaspresso
import io.mockk.mockk
import io.mockk.verify
import org.junit.Test

class FlakySafeInterceptorScalpelTest {

private val kaspresso = mockk<Kaspresso>(relaxed = true)
private val scalpelSwitcher = mockk<ScalpelSwitcher>(relaxed = true)
private val scalpel = FlakySafeInterceptorScalpel(kaspresso, scalpelSwitcher)
@Test
fun `GIVEN nested flaky safety WHEN trying to scalp flaky safety interceptors THEN should not scalp interceptors`() {
scalpel.scalpFromLibs()
scalpel.scalpFromLibs()
scalpel.scalpFromLibs()

verify(exactly = 1) { scalpelSwitcher.attemptTakeScalp(any(), any()) }
}

@Test
fun `GIVEN nested flaky safety WHEN trying to restore flaky safety interceptors THEN should not restore interceptors`() {
scalpel.scalpFromLibs()
scalpel.scalpFromLibs()
scalpel.scalpFromLibs()

scalpel.restoreScalpToLibs()
scalpel.restoreScalpToLibs()

verify(exactly = 0) { scalpelSwitcher.attemptRestoreScalp(any()) }

scalpel.restoreScalpToLibs()
verify { scalpelSwitcher.attemptRestoreScalp(any()) }
}
}

0 comments on commit b0cc107

Please sign in to comment.