Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
screenshot = "0.0.1-alpha11"
constraintlayout = "2.2.1"
material = "1.12.0"
robolectric = "4.12.1"
truth = "1.1.3"

[libraries]
android-gradle-plugin = { module = "com.android.tools.build:gradle", version.ref = "agp" }
Expand Down Expand Up @@ -53,6 +55,8 @@
androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
screenshot-validation-api = { group = "com.android.tools.screenshot", name = "screenshot-validation-api", version.ref = "screenshot" }
robolectric = { module = "org.robolectric:robolectric", version.ref = "robolectric" }
truth = { module = "com.google.truth:truth", version.ref = "truth" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
Expand Down
7 changes: 7 additions & 0 deletions maps-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ dependencies {
androidTestImplementation(libs.test.junit)
androidTestImplementation(libs.androidx.test.compose.ui)
androidTestImplementation(libs.kotlinx.coroutines.test)
androidTestImplementation(libs.truth)

testImplementation(libs.test.junit)
testImplementation(libs.robolectric)
testImplementation(libs.androidx.test.core)
testImplementation(libs.truth)
testImplementation(libs.kotlinx.coroutines.test)

screenshotTestImplementation(libs.androidx.compose.ui.tooling)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class GoogleMapViewTests {
}

@Test
fun testRighColorSchemeAfterChangingIt() {
fun testRightColorSchemeAfterChangingIt() {
mapColorScheme = ComposeMapColorScheme.DARK
initMap()
mapColorScheme.assertEquals(ComposeMapColorScheme.DARK)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.google.maps.android.compose

import android.content.Context
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.google.common.truth.Truth.assertThat
import com.google.maps.android.compose.internal.GoogleMapsInitializer
import com.google.maps.android.compose.internal.InitializationState
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Test
import org.junit.runner.RunWith

@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(AndroidJUnit4::class)
class GoogleMapsInitializerTest {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a random idea: since one of the motivations to do some work on the Maps Initializer was the positive we got on the StrictMode, wondering if it could make sense to test this with the StrictMode activated:

    @Before
    fun setUp() {
        StrictMode.setThreadPolicy(
            StrictMode.ThreadPolicy.Builder()
                .detectDiskReads()
                .detectAll()
                .penaltyLog()
                .build()
        )
        StrictMode.setVmPolicy(
            StrictMode.VmPolicy.Builder()
                .detectAll()
                .penaltyLog()
                .build()
        )
    }

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good idea. Thanks for the suggestion!

@After
fun tearDown() = runTest {
GoogleMapsInitializer.reset()
}

@Test
fun testInitializationSuccess() = runTest {
// In an instrumentation test environment, Google Play services are available.
// Therefore, we expect the initialization to succeed.
val context: Context = InstrumentationRegistry.getInstrumentation().targetContext

GoogleMapsInitializer.initialize(context)

assertThat(GoogleMapsInitializer.state.value).isEqualTo(InitializationState.SUCCESS)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.google.maps.android.compose

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import com.google.common.truth.Truth.assertThat
import com.google.maps.android.compose.internal.GoogleMapsInitializer
import com.google.maps.android.compose.internal.InitializationState
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner

@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(RobolectricTestRunner::class)
class GoogleMapsInitializerTest {

@After
fun tearDown() = runTest {
GoogleMapsInitializer.reset()
}

@Test
fun testInitializationFailure() = runTest {
// In a unit test environment, Google Play services are not available.
// Therefore, we expect the initialization to fail.
val context: Context = ApplicationProvider.getApplicationContext()

GoogleMapsInitializer.initialize(context)

// The initialization is now synchronous within the test scope, so we don't need to wait.
assertThat(GoogleMapsInitializer.state.value).isEqualTo(InitializationState.FAILURE)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ import com.google.android.gms.maps.MapView
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.MapColorScheme
import com.google.android.gms.maps.model.PointOfInterest
import com.google.maps.android.compose.internal.MapsApiAttribution
import com.google.maps.android.compose.internal.GoogleMapsInitializer
import com.google.maps.android.compose.internal.InitializationState
import com.google.maps.android.ktx.awaitMap
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
Expand Down Expand Up @@ -113,16 +114,16 @@ public fun GoogleMap(
return
}

val isInitialized by MapsApiAttribution.isInitialized
val initializationState by GoogleMapsInitializer.state

if (!isInitialized) {
if (initializationState != InitializationState.SUCCESS) {
val context = LocalContext.current
LaunchedEffect(Unit) {
MapsApiAttribution.addAttributionId(context)
GoogleMapsInitializer.initialize(context)
}
}

if (isInitialized) {
if (initializationState == InitializationState.SUCCESS) {
// rememberUpdatedState and friends are used here to make these values observable to
// the subcomposition without providing a new content function each recomposition
val mapClickListeners = remember { MapClickListeners() }.also {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.maps.android.compose.internal

import android.content.Context
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.maps.MapsInitializer
import com.google.android.gms.maps.MapsApiSettings
import com.google.maps.android.compose.meta.AttributionId
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext

/**
* Enum representing the initialization state of the Google Maps SDK.
*/
public enum class InitializationState {
/**
* The SDK has not been initialized.
*/
UNINITIALIZED,

/**
* The SDK is currently being initialized.
*/
INITIALIZING,

/**
* The SDK has been successfully initialized.
*/
SUCCESS,

/**
* The SDK initialization failed.
*/
FAILURE
}

/**
* A singleton object to manage the initialization of the Google Maps SDK.
*
* This object provides a state machine to track the initialization process and ensures that
* the initialization is performed only once. It also provides a mechanism to reset the
* initialization state, which can be useful in test environments.
*
* The initialization process consists of two main steps:
* 1. Calling `MapsInitializer.initialize(context)` to initialize the Google Maps SDK.
* 2. Calling `MapsApiSettings.addInternalUsageAttributionId(context, attributionId)` to add
* the library's attribution ID to the Maps API settings.
*
* The state of the initialization is exposed via the `state` property, which is a [State] object
* that can be observed for changes.
*/
public object GoogleMapsInitializer {
private val _state = mutableStateOf(InitializationState.UNINITIALIZED)
public val state: State<InitializationState> = _state

private val mutex = Mutex()

/**
* The value of the attribution ID. Set this to the empty string to opt out of attribution.
*
* This must be set before calling the `initialize` function.
*/
public var attributionId: String = AttributionId.VALUE

/**
* Initializes the Google Maps SDK.
*
* This function starts the initialization process on a background thread. The process is
* performed only once. If the initialization is already in progress or has completed,
* this function does nothing.
*
* The initialization state can be observed via the `state` property.
*
* @param context The context to use for initialization.
*/
public suspend fun initialize(context: Context) {
// 1. Quick exit if already initialized or in progress.
if (_state.value != InitializationState.UNINITIALIZED) {
return
}

// 2. Acquire the mutex, perform a double-check (in case another
// coroutine was also waiting), and set the state.
// This block is synchronous and ensures only one coroutine
// proceeds to the IO operation.
mutex.withLock {
if (_state.value != InitializationState.UNINITIALIZED) {
return // Another coroutine won the race while this one was suspended on the lock.
}
_state.value = InitializationState.INITIALIZING
}

// The lock is now released.

// 3. Run the blocking initialization code on the IO dispatcher.
// This function will SUSPEND until the withContext(Dispatchers.IO) block completes.
// If the calling scope is cancelled while waiting, withContext will throw
// a CancellationException, and the state will remain INITIALIZING
// (which the catch block will update to FAILURE).
try {
withContext(Dispatchers.IO) {
// This is the blocking call. The thread will be blocked here.
// If cancellation happens, the thread STILL finishes this call,
// but the coroutine will immediately throw CancellationException
// *after* this call returns, skipping the state assignments below.
if (MapsInitializer.initialize(context) == ConnectionResult.SUCCESS) {
MapsApiSettings.addInternalUsageAttributionId(context, attributionId)
_state.value = InitializationState.SUCCESS
} else {
// Handle cases where initialize() returns a non-SUCCESS code
_state.value = InitializationState.FAILURE
}
}
} catch (_: Exception) {
// This will catch any exceptions from the init process (like from mocks in tests)
// Note: By default, this does NOT catch CancellationException.
_state.value = InitializationState.FAILURE
}
}

/**
* Resets the initialization state.
*
* This function cancels any ongoing initialization and resets the state to `UNINITIALIZED`.
* This is useful in test environments where you might need to re-initialize the SDK
* multiple times.
*/
public suspend fun reset() {
mutex.withLock {
_state.value = InitializationState.UNINITIALIZED
}
}
}

This file was deleted.

Loading