Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions maps-compose-utils/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ android {
enableAndroidTestCoverage = true
}
}

testOptions {
unitTests {
isIncludeAndroidResources = true
}
}
}

kotlin {
Expand Down Expand Up @@ -100,4 +106,10 @@ dependencies {
testImplementation(libs.mockk)
testImplementation(libs.robolectric)
testImplementation(libs.test.junit)
testImplementation(libs.truth)
testImplementation(libs.androidx.test.compose.ui)
testImplementation(libs.androidx.test.compose.ui.manifest)
testImplementation(libs.androidx.activity.compose)
testImplementation(libs.androidx.test.core)
testImplementation(libs.maps.testing.robolectric)
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import java.util.concurrent.Executor

@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [34])
public class ClusterRendererTest {

private data class TestItem(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
/*
* Copyright 2026 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.clustering

import android.os.Looper
import androidx.activity.ComponentActivity
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.test.junit4.AndroidComposeTestRule
import androidx.compose.ui.test.junit4.v2.createAndroidComposeRule
import androidx.test.ext.junit.rules.ActivityScenarioRule
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.LatLng
import com.google.common.truth.Truth.assertThat
import com.google.maps.android.clustering.Cluster
import com.google.maps.android.clustering.ClusterItem
import com.google.maps.android.clustering.ClusterManager
import com.google.maps.android.clustering.view.ClusterRenderer
import com.google.maps.android.compose.GoogleMap
import com.google.maps.android.compose.MapEffect
import com.google.maps.android.compose.MapsComposeExperimentalApi
import com.google.maps.android.compose.rememberCameraPositionState
import com.google.maps.android.robolectric.annotation.EnableMapsShadows
import com.google.maps.android.robolectric.ktx.markers
import com.google.maps.android.robolectric.truth.GoogleMapSubject.Companion.assertThat
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.Shadows.shadowOf
import org.robolectric.annotation.Config

// [START maps_compose_utils_clustering_test]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Just to understand it: are these [START ...]/[END ...] region tags meant to be pulled into some documentation? If not, I would remove them from the tests.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yes — these [START ...] / [END ...] region tags are used for single-source-of-truth snippet extraction into documentation and guides, consistent with GoogleMapTest, CameraPositionStateTest, and ScaleBarTest.

@RunWith(RobolectricTestRunner::class)
@Config(sdk = [34])
@EnableMapsShadows
@OptIn(MapsComposeExperimentalApi::class)
internal class ClusteringTest {

@get:Rule
internal val composeTestRule: AndroidComposeTestRule<ActivityScenarioRule<ComponentActivity>, ComponentActivity> =
createAndroidComposeRule<ComponentActivity>()

private data class SampleClusterItem(
override val position: LatLng,
override val title: String? = null,
override val snippet: String? = null,
override val zIndex: Float? = null,
) : ClusterItem

@Test
internal fun testClusteringMarkerProperties_updatesCompositionLocal() {
val properties = ClusteringMarkerProperties()
assertThat(properties.anchor).isNull()
assertThat(properties.zIndex).isNull()
assertThat(properties.rotation).isNull()

composeTestRule.setContent {
CompositionLocalProvider(LocalClusteringMarkerProperties provides properties) {
ClusteringMarkerProperties(
anchor = Offset(0.5f, 0.5f),
zIndex = 3.0f,
rotation = 45f,
)
}
}
composeTestRule.waitForIdle()

assertThat(properties.anchor).isEqualTo(Offset(0.5f, 0.5f))
assertThat(properties.zIndex).isEqualTo(3.0f)
assertThat(properties.rotation).isEqualTo(45f)
}

@Test
internal fun testClustering_defaultRenderer_rendersItemsAndClusters() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This one only checks manager.algorithm.items, so we are not testing really that something is rendered. Could we assert on the markers or the clusters as well?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good catch — updated in 865eefc5 to place 4 nearby SF items + 1 distant NYC item at zoom 4, idle the main looper while waiting for rendering, and assert directly on GoogleMap.markers (hasMarkerCount(2) and hasMarkerWithTitle("NYC")).

var underlyingMap: GoogleMap? = null
var managerFromCallback: ClusterManager<SampleClusterItem>? = null
val sfClusterItems = listOf(
SampleClusterItem(LatLng(37.7749, -122.4194), "SF 1"),
SampleClusterItem(LatLng(37.7750, -122.4195), "SF 2"),
SampleClusterItem(LatLng(37.7751, -122.4196), "SF 3"),
SampleClusterItem(LatLng(37.7752, -122.4197), "SF 4"),
)
val nycItem = SampleClusterItem(LatLng(40.7128, -74.0060), "NYC")
val items = sfClusterItems + nycItem

composeTestRule.setContent {
val cameraState = rememberCameraPositionState {
position = CameraPosition.fromLatLngZoom(LatLng(39.0, -98.0), 4f)
}
GoogleMap(
cameraPositionState = cameraState,
) {
MapEffect(Unit) { map ->
underlyingMap = map
}
Clustering(
items = items,
onClusterManager = { manager ->
managerFromCallback = manager
},
)
}
}

composeTestRule.waitForIdle()

// ClusterManager clusters on a background dispatcher and DefaultClusterRenderer delivers
// markers onto GoogleMap via the main looper. Idle the looper until both the 4-item SF
// cluster marker and the unclustered NYC marker are rendered on the map.
composeTestRule.waitUntil(timeoutMillis = 5000) {
shadowOf(Looper.getMainLooper()).idle()
managerFromCallback?.algorithm?.items?.size == items.size &&
(underlyingMap?.markers?.size ?: 0) >= 2
}

val map = checkNotNull(underlyingMap)
val manager = checkNotNull(managerFromCallback)
assertThat(manager.algorithm.items).containsExactlyElementsIn(items)
// 4 nearby SF items collapse into 1 cluster marker + 1 unclustered NYC item marker.
assertThat(map).hasMarkerCount(2)
assertThat(map).hasMarkerWithTitle("NYC")
}

@Test
internal fun testClustering_withCustomComposeContent() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: we set clusterContent and clusterItemContent here but we never check them, only the decoration. Should we assert on the custom content too, or rename the test?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Updated in 865eefc5 to assert that clusterContent (4-item cluster), clusterItemContent, and clusterItemDecoration are all invoked and that the rendered markers on GoogleMap receive the 10f and 20f rotations from ClusteringMarkerProperties.

var underlyingMap: GoogleMap? = null
val renderedClusters = mutableListOf<Cluster<SampleClusterItem>>()
val renderedItems = mutableListOf<SampleClusterItem>()
val decoratedItems = mutableListOf<SampleClusterItem>()
val sfClusterItems = listOf(
SampleClusterItem(LatLng(37.7749, -122.4194), "SF 1"),
SampleClusterItem(LatLng(37.7750, -122.4195), "SF 2"),
SampleClusterItem(LatLng(37.7751, -122.4196), "SF 3"),
SampleClusterItem(LatLng(37.7752, -122.4197), "SF 4"),
)
val nycItem = SampleClusterItem(LatLng(40.7128, -74.0060), "NYC")
val items = sfClusterItems + nycItem

composeTestRule.setContent {
val cameraState = rememberCameraPositionState {
position = CameraPosition.fromLatLngZoom(LatLng(39.0, -98.0), 4f)
}
GoogleMap(
cameraPositionState = cameraState,
) {
MapEffect(Unit) { map ->
underlyingMap = map
}
Clustering(
items = items,
clusterContent = { cluster ->
renderedClusters.add(cluster)
ClusteringMarkerProperties(rotation = 10f)
},
clusterItemContent = { item ->
renderedItems.add(item)
ClusteringMarkerProperties(rotation = 20f)
},
clusterItemDecoration = { item ->
decoratedItems.add(item)
},
)
}
}

composeTestRule.waitForIdle()
// ClusterManager computes clusters on a background dispatcher and hands them to the
// renderer through Dispatchers.Main. Under Robolectric's paused looper that hand-off only
// runs when the main looper is idled, which waitUntil does not do on its own.
composeTestRule.waitUntil(timeoutMillis = 5000) {
shadowOf(Looper.getMainLooper()).idle()
renderedClusters.isNotEmpty() &&
renderedItems.isNotEmpty() &&
decoratedItems.isNotEmpty() &&
(underlyingMap?.markers?.size ?: 0) >= 2
}

val map = checkNotNull(underlyingMap)
assertThat(renderedClusters.map { it.size }).contains(4)
assertThat(renderedItems).contains(nycItem)
assertThat(decoratedItems).contains(nycItem)
assertThat(map).hasMarkerCount(2)
assertThat(map.markers.map { it.rotation }).containsExactly(10f, 20f)
}

@Test
internal fun testClustering_dynamicItemUpdates() {
var itemsState by mutableStateOf(
listOf(SampleClusterItem(LatLng(10.0, 10.0), "Initial")),
)
var clusterManager: ClusterManager<SampleClusterItem>? = null

composeTestRule.setContent {
GoogleMap {
Clustering(
items = itemsState,
onClusterManager = { clusterManager = it },
)
}
}

composeTestRule.waitForIdle()
composeTestRule.waitUntil(timeoutMillis = 5000) {
clusterManager?.algorithm?.items?.size == 1
}
val manager = checkNotNull(clusterManager)
assertThat(manager.algorithm.items).hasSize(1)

// Update items list
val updatedItems = listOf(
SampleClusterItem(LatLng(20.0, 20.0), "Updated 1"),
SampleClusterItem(LatLng(20.1, 20.1), "Updated 2"),
)
itemsState = updatedItems
composeTestRule.waitForIdle()
composeTestRule.waitUntil(timeoutMillis = 5000) {
manager.algorithm.items.size == 2
}
assertThat(manager.algorithm.items).containsExactlyElementsIn(updatedItems)
}

@Test
internal fun testRememberClusterManagerAndRenderer_nonNullWhenMapReady() {
var rememberedManager: ClusterManager<SampleClusterItem>? = null
var rememberedRenderer: ClusterRenderer<SampleClusterItem>? = null

composeTestRule.setContent {
GoogleMap {
val manager = rememberClusterManager<SampleClusterItem>()
val renderer = rememberClusterRenderer(manager)
rememberedManager = manager
rememberedRenderer = renderer
}
}

composeTestRule.waitForIdle()

assertThat(rememberedManager).isNotNull()
assertThat(rememberedRenderer).isNotNull()
}
}
// [END maps_compose_utils_clustering_test]
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2026 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.wms

import androidx.activity.ComponentActivity
import androidx.compose.ui.test.junit4.AndroidComposeTestRule
import androidx.compose.ui.test.junit4.v2.createAndroidComposeRule
import androidx.test.ext.junit.rules.ActivityScenarioRule
import com.google.android.gms.maps.GoogleMap
import com.google.common.truth.Truth.assertThat
import com.google.maps.android.compose.GoogleMap
import com.google.maps.android.compose.MapEffect
import com.google.maps.android.compose.MapsComposeExperimentalApi
import com.google.maps.android.compose.TileOverlayState
import com.google.maps.android.compose.rememberTileOverlayState
import com.google.maps.android.robolectric.annotation.EnableMapsShadows
import com.google.maps.android.robolectric.ktx.shadow
import com.google.maps.android.robolectric.ktx.tileOverlays
import com.google.maps.android.robolectric.truth.GoogleMapSubject.Companion.assertThat
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

// [START maps_compose_utils_wms_tile_overlay_test]
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [34])
@EnableMapsShadows
@OptIn(MapsComposeExperimentalApi::class)
internal class WmsTileOverlayTest {

@get:Rule
internal val composeTestRule: AndroidComposeTestRule<ActivityScenarioRule<ComponentActivity>, ComponentActivity> =
createAndroidComposeRule<ComponentActivity>()

@Test
internal fun testWmsTileOverlay_rendersAndAppliesProperties() {
var underlyingMap: GoogleMap? = null
lateinit var overlayState: TileOverlayState

composeTestRule.setContent {
overlayState = rememberTileOverlayState()
GoogleMap {
MapEffect(Unit) { map ->
underlyingMap = map
}
WmsTileOverlay(
urlFormatter = { xMin, yMin, xMax, yMax, zoom ->
"https://example.com/tiles?bbox=$xMin,$yMin,$xMax,$yMax&zoom=$zoom"
},
state = overlayState,
transparency = 0.5f,
fadeIn = false,
visible = true,
zIndex = 2.0f,
)
}
}

composeTestRule.waitForIdle()

val map = checkNotNull(underlyingMap)
assertThat(map).hasTileOverlayCount(1)

val tileOverlay = map.tileOverlays.first()
val shadowOverlay = tileOverlay.shadow

assertThat(tileOverlay.transparency).isEqualTo(0.5f)
assertThat(tileOverlay.fadeIn).isFalse()
assertThat(tileOverlay.isVisible).isTrue()
assertThat(tileOverlay.zIndex).isEqualTo(2.0f)

// Verify clearTileCache via state
overlayState.clearTileCache()
assertThat(shadowOverlay.isTileCacheCleared).isTrue()
}
}
// [END maps_compose_utils_wms_tile_overlay_test]
Loading
Loading