|
| 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