Skip to content

Commit 0cd0f49

Browse files
committed
test: Add StrictMode check to GoogleMapsInitializerTest
Wraps the `GoogleMapsInitializer.initialize()` call with strict StrictMode policies. This ensures the initialization process does not perform any violations, such as disk reads, which would cause the test to fail.
1 parent 69984e7 commit 0cd0f49

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[versions]
22
activitycompose = "1.10.1"
3-
agp = "8.12.1"
3+
agp = "8.13.0"
44
androidCore = "1.7.0"
55
androidx-core = "1.17.0"
66
androidxtest = "1.7.0"

maps-app/src/androidTest/java/com/google/maps/android/compose/GoogleMapsInitializerTest.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.google.maps.android.compose
22

33
import android.content.Context
4+
import android.os.StrictMode
45
import androidx.test.ext.junit.runners.AndroidJUnit4
56
import androidx.test.platform.app.InstrumentationRegistry
67
import com.google.common.truth.Truth.assertThat
@@ -25,10 +26,36 @@ class GoogleMapsInitializerTest {
2526
fun testInitializationSuccess() = runTest {
2627
// In an instrumentation test environment, Google Play services are available.
2728
// Therefore, we expect the initialization to succeed.
29+
2830
val context: Context = InstrumentationRegistry.getInstrumentation().targetContext
2931

32+
// Note: we need to establish the Strict Mode settings here as there are violations outside
33+
// of our control if we try to set them in setUp
34+
val threadPolicy = StrictMode.getThreadPolicy()
35+
val vmPolicy = StrictMode.getVmPolicy()
36+
37+
StrictMode.setThreadPolicy(
38+
StrictMode.ThreadPolicy.Builder()
39+
.detectDiskReads()
40+
.detectAll()
41+
.penaltyLog()
42+
.penaltyDeath()
43+
.build()
44+
)
45+
StrictMode.setVmPolicy(
46+
StrictMode.VmPolicy.Builder()
47+
.detectAll()
48+
.detectLeakedClosableObjects()
49+
.penaltyLog()
50+
.penaltyDeath()
51+
.build()
52+
)
53+
3054
GoogleMapsInitializer.initialize(context)
3155

56+
StrictMode.setThreadPolicy(threadPolicy)
57+
StrictMode.setVmPolicy(vmPolicy)
58+
3259
assertThat(GoogleMapsInitializer.state.value).isEqualTo(InitializationState.SUCCESS)
3360
}
3461
}

0 commit comments

Comments
 (0)