Skip to content

Commit

Permalink
Add isAbsent tests case to ScenarioProcessing
Browse files Browse the repository at this point in the history
  • Loading branch information
Nain57 committed Nov 23, 2024
1 parent 4f81795 commit 5828dd3
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ package com.buzbuz.smartautoclicker.core.processing.tests.processor
import android.graphics.Bitmap
import android.os.Build
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.buzbuz.smartautoclicker.core.base.AndroidExecutor
import com.buzbuz.smartautoclicker.core.detection.ImageDetector
import com.buzbuz.smartautoclicker.core.domain.model.SmartActionExecutor
import com.buzbuz.smartautoclicker.core.domain.model.action.Action
import com.buzbuz.smartautoclicker.core.domain.model.action.ChangeCounter.OperationType
import com.buzbuz.smartautoclicker.core.domain.model.action.ToggleEvent
import com.buzbuz.smartautoclicker.core.domain.model.condition.ImageCondition
Expand Down Expand Up @@ -328,4 +326,69 @@ class ProcessingTests {
mockProcessingListener.verifyTriggerEventProcessed(testEvent2, false)
mockProcessingListener.verifyTriggerEventProcessed(testEvent3, true)
}

/**
* Use case: 2 image events, all enabled. Both should not be detected. First one is present on the frame, second is not
* Expected behaviour: First one should not be fulfilled, second one should
*
* Unit test for [#551](https://github.com/Nain57/Smart-AutoClicker/issues/551)
*/
@Test
fun `isAbsent ImageConditions should be triggered when not detected`() = runTest {
// Given
val scenarioId = testsData.newScenarioId()
val eventId1 = testsData.newEventId()
val eventId2 = testsData.newEventId()
val testConditionEvt1 = testsData.newTestImageCondition(eventId1, shouldBeDetected = false)
val testConditionEvt2 = testsData.newTestImageCondition(eventId2, shouldBeDetected = false)
val testScenario = testsData.newTestScenario(
scenarioId = scenarioId,
imageEvents = listOf(
testsData.newTestImageEvent(
eventId = eventId1,
scenarioId = scenarioId,
enabledOnStart = true,
keepDetecting = true,
conditions = listOf(testConditionEvt1),
actions = listOf(testsData.newPauseAction(eventId1)),
),
testsData.newTestImageEvent(
eventId = eventId2,
scenarioId = scenarioId,
enabledOnStart = true,
keepDetecting = true,
conditions = listOf(testConditionEvt2),
actions = listOf(testsData.newPauseAction(eventId2)),
),
),
)

// Mock the bitmaps for each image conditions
mockBitmapSupplier.apply {
mockBitmapProviding(testConditionEvt1)
mockBitmapProviding(testConditionEvt2)
}
// Mock detection results for each condition.
mockImageDetector.apply {
mockDetectionResult(testConditionEvt1, true)
mockDetectionResult(testConditionEvt2, false)
}
// Keep track of event fulfillment results
val eventsFulfilled = mockProcessingListener.monitorImageEventProcessing(testScenario.imageEvents)

// When: Only verify on one frame here
scenarioProcessor = createScenarioProcessor(testScenario).apply {
process(testsData.newMockedScreenBitmap())
}

// Then
// Event1 should be detected, Event2 should not
inOrder(mockProcessingListener).apply {
mockProcessingListener.verifyImageConditionProcessed(testConditionEvt1, true)
mockProcessingListener.verifyImageConditionProcessed(testConditionEvt2, false)
}
// Event1 not be triggered, Event2 should
assertFalse(eventsFulfilled[0])
assertTrue(eventsFulfilled[1])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import android.graphics.Bitmap
import android.graphics.Point

import com.buzbuz.smartautoclicker.core.base.identifier.Identifier
import com.buzbuz.smartautoclicker.core.domain.model.action.Action
import com.buzbuz.smartautoclicker.core.domain.model.action.ToggleEvent
import com.buzbuz.smartautoclicker.core.domain.model.condition.ImageCondition
import com.buzbuz.smartautoclicker.core.domain.model.event.ImageEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@ import com.buzbuz.smartautoclicker.core.detection.ImageDetector
import com.buzbuz.smartautoclicker.core.domain.model.EXACT
import com.buzbuz.smartautoclicker.core.domain.model.IN_AREA
import com.buzbuz.smartautoclicker.core.domain.model.WHOLE_SCREEN
import com.buzbuz.smartautoclicker.core.domain.model.event.ImageEvent
import com.buzbuz.smartautoclicker.core.domain.model.event.TriggerEvent
import com.buzbuz.smartautoclicker.core.processing.data.processor.ConditionsResult
import com.buzbuz.smartautoclicker.core.processing.domain.ScenarioProcessingListener
import com.buzbuz.smartautoclicker.core.processing.tests.processor.ProcessingTests.BitmapSupplier
import com.buzbuz.smartautoclicker.core.processing.utils.anyNotNull
import org.mockito.Mockito.times
import org.mockito.Mockito.`when`
import org.mockito.kotlin.doAnswer
import org.mockito.kotlin.eq
import org.mockito.kotlin.never
import org.mockito.kotlin.verify

Expand Down Expand Up @@ -69,10 +74,25 @@ internal fun ImageDetector.mockDetectionResult(testCondition: TestImageCondition

internal suspend fun ScenarioProcessingListener.verifyImageConditionProcessed(
condition: TestImageCondition,
expectedResult: Boolean,
detected: Boolean,
processedCount: Int = 1,
): Unit = verify(this, times(processedCount))
.onImageConditionProcessingCompleted(condition.expectedResult(expectedResult))
.onImageConditionProcessingCompleted(condition.expectedResult(detected))

internal suspend fun ScenarioProcessingListener.monitorImageEventProcessing(
events: List<ImageEvent>,
): List<Boolean> {
val results = mutableListOf<Boolean>()

events.forEach { event ->
`when`(onImageEventProcessingCompleted(eq(event), anyNotNull())).doAnswer { invocationOnMock ->
results.add((invocationOnMock.arguments[1] as ConditionsResult).fulfilled == true)
Unit
}
}

return results
}

internal fun ImageDetector.verifyConditionNeverProcessed(testCondition: TestImageCondition) {
when (testCondition.imageCondition.detectionType) {
Expand Down

0 comments on commit 5828dd3

Please sign in to comment.