Skip to content

Commit

Permalink
fix(geofence): handle flags properly
Browse files Browse the repository at this point in the history
SUITEDEV-36519

Co-authored-by: davidSchuppa <[email protected]>
  • Loading branch information
megamegax and davidSchuppa committed Sep 4, 2024
1 parent 89f0ae9 commit ea27f6b
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationManager
import android.os.Build
import androidx.test.filters.SdkSuppress
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import com.emarsys.core.api.MissingPermissionException
import com.emarsys.core.concurrency.ConcurrentHandlerHolderFactory
Expand Down Expand Up @@ -263,15 +264,36 @@ class DefaultGeofenceInternalTest : AnnotationSpec() {
}

@Test
fun testEnable_registersGeofenceBroadcastReceiver() {
geofenceInternalWithMockContext.enable(null)
geofenceInternalWithMockContext.enable(null)
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.TIRAMISU)
fun testEnable_registersGeofenceBroadcastReceiverAboveTiramisu() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
geofenceInternalWithMockContext.enable(null)
geofenceInternalWithMockContext.enable(null)

verify(
mockContext,
timeout(100).times(1)
).registerReceiver(any<GeofenceBroadcastReceiver>(), any())
verify(
mockContext,
timeout(100).times(1)
).registerReceiver(
any<GeofenceBroadcastReceiver>(),
any(),
eq(Context.RECEIVER_EXPORTED)
)

}
}

@Test
@SdkSuppress(maxSdkVersion = 32)
fun testEnable_registersGeofenceBroadcastReceiverBelowTiramisu() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
geofenceInternalWithMockContext.enable(null)
geofenceInternalWithMockContext.enable(null)

verify(
mockContext,
timeout(100).times(1)
).registerReceiver(any<GeofenceBroadcastReceiver>(), any())
}
}

@Test
Expand Down Expand Up @@ -309,15 +331,35 @@ class DefaultGeofenceInternalTest : AnnotationSpec() {
}

@Test
fun testDisable() {
geofenceInternalWithMockContext.enable(null)
geofenceInternalWithMockContext.disable()
geofenceInternalWithMockContext.enable(null)
@SdkSuppress(minSdkVersion = Build.VERSION_CODES.TIRAMISU)
fun testDisableAboveTiramisu() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
geofenceInternalWithMockContext.enable(null)
geofenceInternalWithMockContext.disable()
geofenceInternalWithMockContext.enable(null)

verify(
mockContext, timeout(100).times(2)
).registerReceiver(any<GeofenceBroadcastReceiver>(), any())
verify(
mockContext, timeout(100).times(2)
).registerReceiver(
any<GeofenceBroadcastReceiver>(),
any(),
eq(Context.RECEIVER_EXPORTED)
)
}
}

@Test
@SdkSuppress(maxSdkVersion = Build.VERSION_CODES.S_V2)
fun testDisableBelowTiramisu() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
geofenceInternalWithMockContext.enable(null)
geofenceInternalWithMockContext.disable()
geofenceInternalWithMockContext.enable(null)

verify(
mockContext, timeout(100).times(2)
).registerReceiver(any<GeofenceBroadcastReceiver>(), any())
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import com.emarsys.mobileengage.util.waitForTask


import com.emarsys.testUtil.AnnotationSpec
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import io.mockk.mockk
import io.mockk.verify

class FetchGeofencesActionTest : AnnotationSpec() {

Expand All @@ -20,8 +20,8 @@ class FetchGeofencesActionTest : AnnotationSpec() {

@Before
fun setUp() {
mockGeofenceInternal = mock()
mockActivity = mock()
mockGeofenceInternal = mockk(relaxed = true)
mockActivity = mockk(relaxed = true)

setupMobileEngageComponent(FakeMobileEngageDependencyContainer())

Expand All @@ -39,7 +39,7 @@ class FetchGeofencesActionTest : AnnotationSpec() {
fetchGeofencesAction.execute(mockActivity)
waitForTask()

verify(mockGeofenceInternal).fetchGeofences(null)
verify { mockGeofenceInternal.fetchGeofences(null) }
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,18 @@ class DefaultGeofenceInternal(
private fun registerBroadcastReceiver() {
if (!receiverRegistered) {
concurrentHandlerHolder.postOnMain {
context.registerReceiver(
geofenceBroadcastReceiver,
IntentFilter("com.emarsys.sdk.GEOFENCE_ACTION")
)
if (AndroidVersionUtils.isTiramisuOrAbove) {
context.registerReceiver(
geofenceBroadcastReceiver,
IntentFilter("com.emarsys.sdk.GEOFENCE_ACTION"),
Context.RECEIVER_EXPORTED
)
} else {
context.registerReceiver(
geofenceBroadcastReceiver,
IntentFilter("com.emarsys.sdk.GEOFENCE_ACTION"),
)
}
}
receiverRegistered = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ import com.emarsys.core.util.AndroidVersionUtils
class GeofencePendingIntentProvider(private val context: Context) {
fun providePendingIntent(): PendingIntent {
val intent = Intent("com.emarsys.sdk.GEOFENCE_ACTION")
if (AndroidVersionUtils.isBelowS) {
intent.setPackage(context.packageName)
if (AndroidVersionUtils.isUOrAbove) {
return PendingIntent.getBroadcast(
context,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)
} else {
return PendingIntent.getBroadcast(
context,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
PendingIntent.FLAG_UPDATE_CURRENT
)
}
}
Expand Down

0 comments on commit ea27f6b

Please sign in to comment.