Skip to content

Commit

Permalink
fix(geofence): fix, that only invoke the triggers action, not all of …
Browse files Browse the repository at this point in the history
…the triggering geofence's

SUITEDEV-27479

Co-authored-by: davidSchuppa <[email protected]>
Co-authored-by: LasOri <[email protected]>
Co-authored-by: megamegax <[email protected]>
  • Loading branch information
4 people committed May 6, 2021
1 parent 98c6124 commit 5cfba09
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -406,36 +406,42 @@ class DefaultGeofenceInternalTest {
fun testOnGeofenceTriggered() {
val latch = CountDownLatch(1)
val mockAction: Runnable = mock()
val appEventAction = JSONObject("""
{
"type": "MEAppEvent",
"name": "nameValue",
"payload": {
"someKey": "someValue"
}
val appEventAction = JSONObject(
"""
{
"type": "MEAppEvent",
"name": "nameValue",
"payload": {
"someKey": "someValue"
}
""".trimIndent())

val testTrigger = Trigger(id = "appEventActionId", type = TriggerType.ENTER, action = appEventAction)
}
""".trimIndent()
)
val testTrigger =
Trigger(id = "appEventActionId", type = TriggerType.ENTER, action = appEventAction)
val testExitTrigger =
Trigger(id = "appEventActionId", type = TriggerType.EXIT, action = appEventAction)
val trigger = Trigger(id = "triggerId", type = TriggerType.ENTER, action = JSONObject())
val allGeofences = listOf(
MEGeofence("geofenceId1", 47.493160, 19.058355, 10.0, null, listOf(trigger)),
MEGeofence("geofenceId2", 47.493812, 19.058537, 10.0, null, listOf(trigger)),
MEGeofence("testId", 47.493827, 19.060715, 10.0, null, listOf(testTrigger)),
MEGeofence("testId", 47.493827, 19.060715, 10.0, null, listOf(testTrigger, testExitTrigger)),
MEGeofence("geofenceId4", 47.489680, 19.061230, 350.0, null, listOf(trigger)),
MEGeofence("geofenceId5", 47.492292, 19.056440, 10.0, null, listOf(trigger))
)

ReflectionTestUtils.setInstanceField(geofenceInternal, "nearestGeofences", allGeofences)
whenever(mockAction.run()).thenAnswer { latch.countDown() }
whenever(mockActionCommandFactory.createActionCommand(appEventAction)).thenReturn(mockAction)

geofenceInternal.onGeofenceTriggered(listOf(TriggeringGeofence("testId", TriggerType.ENTER)))

verify(mockActionCommandFactory).createActionCommand(appEventAction)

geofenceInternal.onGeofenceTriggered(
listOf(
TriggeringGeofence(
"testId",
TriggerType.ENTER
)
)
)
verify(mockActionCommandFactory, times(1)).createActionCommand(appEventAction)
latch.await()

verify(mockAction).run()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,13 @@ class DefaultGeofenceInternal(private val requestModelFactory: MobileEngageReque

override fun onGeofenceTriggered(events: List<TriggeringGeofence>) {
events.flatMap { triggeringGeofence ->
nearestGeofences.filter {
it.id == triggeringGeofence.geofenceId && it.triggers.any { trigger -> trigger.type == triggeringGeofence.triggerType }
}
}.flatMap { nearestTriggeredGeofences ->
createActionsFromTriggers(nearestTriggeredGeofences)
nearestGeofences
.filter {
it.id == triggeringGeofence.geofenceId && it.triggers.any { trigger -> trigger.type == triggeringGeofence.triggerType }

}.map { geofence -> Pair(geofence, triggeringGeofence) }
}.flatMap { nearestTriggeredGeofencesWithTrigger ->
createActionsFromTriggers(nearestTriggeredGeofencesWithTrigger.first, nearestTriggeredGeofencesWithTrigger.second)
}.forEach { action ->
Handler(Looper.getMainLooper()).post {
action?.run()
Expand All @@ -221,8 +223,10 @@ class DefaultGeofenceInternal(private val requestModelFactory: MobileEngageReque
this.geofenceEventHandlerProvider.eventHandler = eventHandler
}

private fun createActionsFromTriggers(it: MEGeofence): List<Runnable?> {
return it.triggers.mapNotNull { actionCommandFactory.createActionCommand(it.action) }
private fun createActionsFromTriggers(geofence: MEGeofence, triggeringGeofence: TriggeringGeofence): List<Runnable?> {
return geofence.triggers.filter { trigger ->
trigger.type == triggeringGeofence.triggerType
}.mapNotNull { actionCommandFactory.createActionCommand(it.action) }
}

override fun onLocationChanged(location: Location) {}
Expand Down

0 comments on commit 5cfba09

Please sign in to comment.