1111// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212// See the License for the specific language governing permissions and
1313// limitations under the License.
14- package com.google.maps.android.compose
14+ package com.google.maps.android.compose.internal
1515
1616import android.content.Context
1717import android.os.StrictMode
1818import androidx.test.ext.junit.runners.AndroidJUnit4
1919import androidx.test.platform.app.InstrumentationRegistry
2020import com.google.common.truth.Truth.assertThat
21- import com.google.maps.android.compose.internal.GoogleMapsInitializer
22- import com.google.maps.android.compose.internal.InitializationState
2321import kotlinx.coroutines.ExperimentalCoroutinesApi
22+ import kotlinx.coroutines.delay
23+ import kotlinx.coroutines.launch
2424import kotlinx.coroutines.test.runTest
2525import org.junit.After
2626import org.junit.Test
2727import org.junit.runner.RunWith
28+ import kotlin.time.Duration.Companion.milliseconds
2829
2930@OptIn(ExperimentalCoroutinesApi ::class )
3031@RunWith(AndroidJUnit4 ::class )
3132class GoogleMapsInitializerTest {
3233
34+ private val googleMapsInitializer = DefaultGoogleMapsInitializer ()
35+
3336 @After
3437 fun tearDown () = runTest {
35- GoogleMapsInitializer .reset()
38+ googleMapsInitializer .reset()
3639 }
3740
3841 @Test
@@ -64,11 +67,56 @@ class GoogleMapsInitializerTest {
6467 .build()
6568 )
6669
67- GoogleMapsInitializer .initialize(context)
70+ googleMapsInitializer.initialize(context)
71+
72+ StrictMode .setThreadPolicy(threadPolicy)
73+ StrictMode .setVmPolicy(vmPolicy)
74+
75+ assertThat(googleMapsInitializer.state.value).isEqualTo(InitializationState .SUCCESS )
76+ }
77+
78+ @Test
79+ fun testInitializationCancellationLeavesStateUninitialized () = runTest {
80+ // In an instrumentation test environment, Google Play services are available.
81+ // Therefore, we expect the initialization to succeed.
82+
83+ val context: Context = InstrumentationRegistry .getInstrumentation().targetContext
84+
85+ // Note: we need to establish the Strict Mode settings here as there are violations outside
86+ // of our control if we try to set them in setUp
87+ val threadPolicy = StrictMode .getThreadPolicy()
88+ val vmPolicy = StrictMode .getVmPolicy()
89+
90+ StrictMode .setThreadPolicy(
91+ StrictMode .ThreadPolicy .Builder ()
92+ .detectDiskReads()
93+ .detectAll()
94+ .penaltyLog()
95+ .penaltyDeath()
96+ .build()
97+ )
98+ StrictMode .setVmPolicy(
99+ StrictMode .VmPolicy .Builder ()
100+ .detectAll()
101+ .detectLeakedClosableObjects()
102+ .penaltyLog()
103+ .penaltyDeath()
104+ .build()
105+ )
106+
107+ val job = launch {
108+ googleMapsInitializer.reset()
109+ googleMapsInitializer.initialize(context)
110+ }
111+
112+ // Allow the initialization coroutine to start before we cancel it.
113+ delay(1 .milliseconds)
114+ job.cancel()
115+ job.join()
68116
69117 StrictMode .setThreadPolicy(threadPolicy)
70118 StrictMode .setVmPolicy(vmPolicy)
71119
72- assertThat(GoogleMapsInitializer .state.value).isEqualTo(InitializationState .SUCCESS )
120+ assertThat(googleMapsInitializer .state.value).isEqualTo(InitializationState .UNINITIALIZED )
73121 }
74122}
0 commit comments