-
Notifications
You must be signed in to change notification settings - Fork 138
Fix cluster sites not rendering #3430
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
Open
andreia-ferreira
wants to merge
5
commits into
master
Choose a base branch
from
andreia/3387/fix-sites-not-rendering
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+187
−13
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0849ae9
Fix individual items not rendering when clusters stay the same but zo…
andreia-ferreira 25d3b74
Added unit tests
andreia-ferreira 3bb1a33
Remove test code
andreia-ferreira 5a9376c
Fix comment text
andreia-ferreira 7afa455
Merge branch 'master' into andreia/3387/fix-sites-not-rendering
andreia-ferreira File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
...rc/test/java/org/groundplatform/android/ui/map/gms/features/FeatureClusterRendererTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| /* | ||
| * 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 | ||
| * | ||
| * https://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 org.groundplatform.android.ui.map.gms.features | ||
|
|
||
| import android.content.Context | ||
| import androidx.test.core.app.ApplicationProvider | ||
| import com.google.android.gms.maps.GoogleMap | ||
| import com.google.android.gms.maps.model.LatLng | ||
| import com.google.maps.android.clustering.Cluster | ||
| import com.google.maps.android.clustering.ClusterManager | ||
| import dagger.hilt.android.testing.HiltAndroidTest | ||
| import org.groundplatform.android.BaseHiltTest | ||
| import org.groundplatform.android.FakeData.LOCATION_OF_INTEREST_CLUSTER_ITEM | ||
| import org.groundplatform.android.common.Constants.CLUSTERING_ZOOM_THRESHOLD | ||
| import org.junit.Assert.assertFalse | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.mockito.Mock | ||
| import org.mockito.Mockito.mock | ||
| import org.mockito.kotlin.whenever | ||
| import org.robolectric.RobolectricTestRunner | ||
|
|
||
| @HiltAndroidTest | ||
| @RunWith(RobolectricTestRunner::class) | ||
| class FeatureClusterRendererTest : BaseHiltTest() { | ||
|
|
||
| @Mock private lateinit var map: GoogleMap | ||
| @Mock private lateinit var clusterManager: ClusterManager<FeatureClusterItem> | ||
| @Mock private lateinit var cluster: Cluster<FeatureClusterItem> | ||
|
|
||
| private lateinit var context: Context | ||
| private lateinit var featureClusterRenderer: FeatureClusterRenderer | ||
|
|
||
| @Before | ||
| override fun setUp() { | ||
| super.setUp() | ||
| context = ApplicationProvider.getApplicationContext() | ||
| com.google.android.gms.maps.MapsInitializer.initialize(context) | ||
| featureClusterRenderer = FeatureClusterRenderer(context, map, clusterManager, 10f) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Should render as a cluster if the zoom is below the threshold`() { | ||
| featureClusterRenderer.setZoom(CLUSTERING_ZOOM_THRESHOLD - 1f) | ||
| assertTrue(featureClusterRenderer.invoke("shouldRenderAsCluster", cluster)) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Should not render as a cluster when the zoom is above the threshold`() { | ||
| featureClusterRenderer.setZoom(CLUSTERING_ZOOM_THRESHOLD + 1f) | ||
| assertFalse(featureClusterRenderer.invoke("shouldRenderAsCluster", cluster)) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Should force render if the zoom threshold was crossed from below to above`() { | ||
| featureClusterRenderer.setZoom(CLUSTERING_ZOOM_THRESHOLD - 1f) | ||
| featureClusterRenderer.setZoom(CLUSTERING_ZOOM_THRESHOLD + 1f) | ||
|
|
||
| assertTrue( | ||
| featureClusterRenderer.invoke( | ||
| "shouldRender", | ||
| emptySet<Cluster<FeatureClusterItem>>(), | ||
| emptySet<Cluster<FeatureClusterItem>>(), | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Should force render if the zoom threshold was crossed from above to below`() { | ||
| featureClusterRenderer.setZoom(CLUSTERING_ZOOM_THRESHOLD + 1f) | ||
| featureClusterRenderer.setZoom(CLUSTERING_ZOOM_THRESHOLD - 1f) | ||
|
|
||
| assertTrue( | ||
| featureClusterRenderer.invoke( | ||
| "shouldRender", | ||
| emptySet<Cluster<FeatureClusterItem>>(), | ||
| emptySet<Cluster<FeatureClusterItem>>(), | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Should not force render if the clusters haven't changed and the zoom doesn't cross the threshold`() { | ||
| featureClusterRenderer.setZoom(10f) | ||
| featureClusterRenderer.setZoom(11f) | ||
|
|
||
| // Assuming clusters are same (empty vs empty), super implementation would return false | ||
| assertFalse( | ||
| featureClusterRenderer.invoke( | ||
| "shouldRender", | ||
| emptySet<Cluster<FeatureClusterItem>>(), | ||
| emptySet<Cluster<FeatureClusterItem>>(), | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `Should force render when clusters change even if the zoom doesn't cross the threshold`() { | ||
| featureClusterRenderer.setZoom(10f) | ||
| featureClusterRenderer.setZoom(11f) | ||
|
|
||
| val oldClusters = emptySet<Cluster<FeatureClusterItem>>() | ||
| val newClusters = | ||
| setOf<Cluster<FeatureClusterItem>>( | ||
| createMockCluster(listOf(LOCATION_OF_INTEREST_CLUSTER_ITEM)) | ||
| ) | ||
|
|
||
| val result = featureClusterRenderer.invoke<Boolean>("shouldRender", oldClusters, newClusters) | ||
|
|
||
| assertTrue(result) | ||
| } | ||
|
|
||
| private fun createMockCluster(items: List<FeatureClusterItem>): Cluster<FeatureClusterItem> { | ||
| val cluster = mock<Cluster<FeatureClusterItem>>() | ||
| whenever(cluster.items).thenReturn(items) | ||
| whenever(cluster.size).thenReturn(items.size) | ||
| whenever(cluster.position).thenReturn(LatLng(0.0, 0.0)) | ||
| return cluster | ||
| } | ||
|
|
||
| // Helper to invoke any protected method on FeatureClusterRenderer | ||
| @Suppress("UNCHECKED_CAST") | ||
| private fun <T> FeatureClusterRenderer.invoke(methodName: String, vararg args: Any?): T { | ||
| val method = | ||
| this::class.java.declaredMethods.firstOrNull { it.name == methodName } | ||
| ?: throw IllegalArgumentException("Method $methodName not found") | ||
| method.isAccessible = true | ||
| return method.invoke(this, *args) as T | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should we update the value of
oldZoomin this method after it has returned true?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.
that value is updated by
setZoom, which is called by eachonCameraIdle. Then rendering related methods, such as this one, only consume that state to decide whether a re-render is neededThere 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.
I was just wondering if there's a way to optimize things by re-rendering the clusters even if the zoom hasn't changed.