Repository navigation
test(utils): add JVM unit and composable integration tests for Clustering and WmsTileOverlay #1004
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
bf6d5bc
46e4281
bbac8ac
751913d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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] | ||
| @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() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one only checks
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch — updated in |
||
| 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() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: we set
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated in |
||
| 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] |
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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 withGoogleMapTest,CameraPositionStateTest, andScaleBarTest.