Skip to content

Commit db00a85

Browse files
committed
test(widgets): add JVM unit and composable tests for ScaleBar (#1003)
* test(widgets): add JVM unit and composable tests for ScaleBar * test(widgets): address review feedback in ScaleBarTest - Assert calculateDistance against known ground-truth values (mocked equatorial span = 98m, plus Mercator zoom 12 checks at 0 deg and 60 deg latitude) instead of repeating the implementation formula. - Remove unused sphericalDistance import from ScaleBarTest. - Add displayed text assertions in ScaleBar test and mainClock-driven show/hide/re-show/hide assertions in DisappearingScaleBar test.
1 parent bcb407a commit db00a85

3 files changed

Lines changed: 198 additions & 0 deletions

File tree

‎maps-compose-widgets/build.gradle.kts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ android {
5858
enableAndroidTestCoverage = true
5959
}
6060
}
61+
62+
testOptions {
63+
unitTests {
64+
isIncludeAndroidResources = true
65+
}
66+
}
6167
}
6268

6369
kotlin {
@@ -106,6 +112,15 @@ dependencies {
106112
api(libs.maps.utils)
107113

108114
testImplementation(libs.test.junit)
115+
testImplementation(libs.robolectric)
116+
testImplementation(libs.truth)
117+
testImplementation(libs.mockk)
118+
testImplementation(libs.androidx.test.compose.ui)
119+
testImplementation(libs.androidx.activity.compose)
120+
testImplementation(libs.androidx.test.core)
121+
testImplementation(libs.kotlinx.coroutines.test)
122+
testImplementation(libs.maps.testing.robolectric)
123+
109124
androidTestImplementation(platform(libs.androidx.compose.bom))
110125
androidTestImplementation(libs.androidx.test.espresso)
111126
androidTestImplementation(libs.androidx.test.junit.ktx)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2026 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
18+
<application>
19+
<activity
20+
android:name="androidx.activity.ComponentActivity"
21+
android:exported="true" />
22+
</application>
23+
</manifest>
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.maps.android.compose.widgets
18+
19+
import android.graphics.Point
20+
import androidx.activity.ComponentActivity
21+
import androidx.compose.foundation.layout.Box
22+
import androidx.compose.foundation.layout.fillMaxSize
23+
import androidx.compose.runtime.snapshots.Snapshot
24+
import androidx.compose.ui.Modifier
25+
import androidx.compose.ui.graphics.Color
26+
import androidx.compose.ui.test.assertIsDisplayed
27+
import androidx.compose.ui.test.junit4.AndroidComposeTestRule
28+
import androidx.compose.ui.test.junit4.v2.createAndroidComposeRule
29+
import androidx.compose.ui.test.onNodeWithText
30+
import androidx.compose.ui.unit.Density
31+
import androidx.compose.ui.unit.dp
32+
import androidx.test.ext.junit.rules.ActivityScenarioRule
33+
import com.google.android.gms.maps.CameraUpdateFactory
34+
import com.google.android.gms.maps.Projection
35+
import com.google.android.gms.maps.model.CameraPosition
36+
import com.google.android.gms.maps.model.LatLng
37+
import com.google.common.truth.Truth.assertThat
38+
import com.google.maps.android.compose.CameraPositionState
39+
import com.google.maps.android.robolectric.annotation.EnableMapsShadows
40+
import com.google.maps.android.robolectric.ktx.createTestMap
41+
import io.mockk.every
42+
import io.mockk.mockk
43+
import org.junit.Rule
44+
import org.junit.Test
45+
import org.junit.runner.RunWith
46+
import org.robolectric.RobolectricTestRunner
47+
import org.robolectric.annotation.Config
48+
49+
// [START maps_compose_widgets_scale_bar_test]
50+
@RunWith(RobolectricTestRunner::class)
51+
@Config(sdk = [34])
52+
@EnableMapsShadows
53+
internal class ScaleBarTest {
54+
55+
@get:Rule
56+
internal val composeTestRule: AndroidComposeTestRule<ActivityScenarioRule<ComponentActivity>, ComponentActivity> =
57+
createAndroidComposeRule<ComponentActivity>()
58+
59+
@Test
60+
internal fun testCalculateDistance_computesExpectedCanvasDistance() {
61+
val projection = mockk<Projection>()
62+
every { projection.fromScreenLocation(Point(0, 0)) } returns LatLng(0.0, 0.0)
63+
every { projection.fromScreenLocation(Point(100, 0)) } returns LatLng(0.0, 0.001)
64+
65+
val result = calculateDistance(
66+
projection = projection,
67+
width = 100.dp,
68+
density = Density(1f, 1f),
69+
)
70+
71+
// 0.001 deg of longitude at the equator is ~111.19 m; the drawn scale bar spans 8/9 of the canvas (~98 m).
72+
assertThat(result).isEqualTo(98)
73+
}
74+
75+
@Test
76+
internal fun testCalculateDistance_atTarget_computesExpectedCenterSpanDistance() {
77+
val equator = LatLng(0.0, 0.0)
78+
val highLatitude = LatLng(60.0, 0.0)
79+
val density = Density(1f, 1f)
80+
val width = 100.dp
81+
82+
val equatorMap = createTestMap().apply {
83+
moveCamera(CameraUpdateFactory.newLatLngZoom(equator, 12f))
84+
}
85+
val equatorDistance = calculateDistance(equatorMap.projection, width, density, target = equator)
86+
87+
// At zoom 12 on the equator, 100 px spans ~3,822 m; 8/9 of that is ~3,397 m.
88+
assertThat(equatorDistance).isIn(3350..3450)
89+
90+
val highLatMap = createTestMap().apply {
91+
moveCamera(CameraUpdateFactory.newLatLngZoom(highLatitude, 12f))
92+
}
93+
val highLatDistance = calculateDistance(highLatMap.projection, width, density, target = highLatitude)
94+
95+
// At 60 deg latitude, cos(60 deg) = 0.5, so the same pixel width spans half the ground distance (~1,699 m).
96+
assertThat(highLatDistance).isIn(1650..1750)
97+
}
98+
99+
@Test
100+
internal fun testScaleBar_rendersWithoutCrashing() {
101+
val cameraState = CameraPositionState(
102+
position = CameraPosition.fromLatLngZoom(LatLng(37.7749, -122.4194), 12f),
103+
)
104+
105+
composeTestRule.setContent {
106+
ScaleBar(
107+
cameraPositionState = cameraState,
108+
width = 80.dp,
109+
height = 50.dp,
110+
textColor = Color.Black,
111+
lineColor = Color.Blue,
112+
shadowColor = Color.White,
113+
)
114+
}
115+
116+
composeTestRule.waitForIdle()
117+
composeTestRule.onNodeWithText("0 ft").assertIsDisplayed()
118+
composeTestRule.onNodeWithText("0 m").assertIsDisplayed()
119+
}
120+
121+
@Test
122+
internal fun testDisappearingScaleBar_showsOnCameraChangeAndHidesAfterDuration() {
123+
composeTestRule.mainClock.autoAdvance = false
124+
val cameraState = CameraPositionState(
125+
position = CameraPosition.fromLatLngZoom(LatLng(37.7749, -122.4194), 10f),
126+
)
127+
128+
composeTestRule.setContent {
129+
Box(modifier = Modifier.fillMaxSize()) {
130+
DisappearingScaleBar(
131+
cameraPositionState = cameraState,
132+
visibilityDurationMillis = 1_000,
133+
)
134+
}
135+
}
136+
137+
composeTestRule.mainClock.advanceTimeByFrame()
138+
composeTestRule.onNodeWithText("0 m").assertIsDisplayed()
139+
140+
// Advance past visibilityDurationMillis (1,000 ms) and the exit fade animation.
141+
composeTestRule.mainClock.advanceTimeBy(2_000L)
142+
composeTestRule.mainClock.advanceTimeByFrame()
143+
composeTestRule.onNodeWithText("0 m").assertDoesNotExist()
144+
145+
// Updating camera position re-triggers the LaunchedEffect and shows the scale bar again.
146+
Snapshot.withMutableSnapshot {
147+
cameraState.position = CameraPosition.fromLatLngZoom(LatLng(37.7749, -122.4194), 14f)
148+
}
149+
composeTestRule.mainClock.advanceTimeByFrame()
150+
composeTestRule.mainClock.advanceTimeBy(200L)
151+
composeTestRule.mainClock.advanceTimeByFrame()
152+
composeTestRule.onNodeWithText("0 m").assertIsDisplayed()
153+
154+
// Advance past visibilityDurationMillis again and confirm the scale bar hides.
155+
composeTestRule.mainClock.advanceTimeBy(2_000L)
156+
composeTestRule.mainClock.advanceTimeByFrame()
157+
composeTestRule.onNodeWithText("0 m").assertDoesNotExist()
158+
}
159+
}
160+
// [END maps_compose_widgets_scale_bar_test]

0 commit comments

Comments
 (0)