diff --git a/mobile-engage/src/androidTest/java/com/emarsys/mobileengage/geofence/DefaultGeofenceInternalTest.kt b/mobile-engage/src/androidTest/java/com/emarsys/mobileengage/geofence/DefaultGeofenceInternalTest.kt index 6d1ca27e..aae9fcc5 100644 --- a/mobile-engage/src/androidTest/java/com/emarsys/mobileengage/geofence/DefaultGeofenceInternalTest.kt +++ b/mobile-engage/src/androidTest/java/com/emarsys/mobileengage/geofence/DefaultGeofenceInternalTest.kt @@ -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() } diff --git a/mobile-engage/src/main/java/com/emarsys/mobileengage/geofence/DefaultGeofenceInternal.kt b/mobile-engage/src/main/java/com/emarsys/mobileengage/geofence/DefaultGeofenceInternal.kt index 05d8d830..c7a4aa03 100644 --- a/mobile-engage/src/main/java/com/emarsys/mobileengage/geofence/DefaultGeofenceInternal.kt +++ b/mobile-engage/src/main/java/com/emarsys/mobileengage/geofence/DefaultGeofenceInternal.kt @@ -193,11 +193,13 @@ class DefaultGeofenceInternal(private val requestModelFactory: MobileEngageReque override fun onGeofenceTriggered(events: List) { 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() @@ -221,8 +223,10 @@ class DefaultGeofenceInternal(private val requestModelFactory: MobileEngageReque this.geofenceEventHandlerProvider.eventHandler = eventHandler } - private fun createActionsFromTriggers(it: MEGeofence): List { - return it.triggers.mapNotNull { actionCommandFactory.createActionCommand(it.action) } + private fun createActionsFromTriggers(geofence: MEGeofence, triggeringGeofence: TriggeringGeofence): List { + return geofence.triggers.filter { trigger -> + trigger.type == triggeringGeofence.triggerType + }.mapNotNull { actionCommandFactory.createActionCommand(it.action) } } override fun onLocationChanged(location: Location) {}