Skip to content

test(widgets): add JVM unit and composable tests for ScaleBar - #1003

Merged
dkhawk merged 2 commits into
feature/robolectric-testsfrom
feature/robolectric-widgets-tests
Nov 24, 2026
Merged

dkhawk merged 2 commits into
feature/robolectric-testsfrom
feature/robolectric-widgets-tests

Conversation

@dkhawk

@dkhawk dkhawk commented Sep 15, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Stacked PR on top of #1001 (feature/robolectric-tests).

Introduces JVM unit and Compose integration tests using Robolectric and the Android Maps Testing Toolkit (com.google.maps.android:maps-testing-robolectric) for maps-compose-widgets, bringing module line coverage up from 0.00% to 67.12% (instruction coverage from 0.00% to 80.68%).

Summary of Changes

  • Added test dependencies (robolectric, mockk, truth, androidx.test.compose.ui, maps.testing.robolectric) in maps-compose-widgets/build.gradle.kts.
  • Configured Robolectric Android resources and Java 21 test runner toolchain.
  • Added debug manifest with ComponentActivity for Robolectric Compose rule hosting.
  • Added ScaleBarTest.kt covering canvas distance calculation, unit conversions (ft, mi), ScaleBar composable rendering, and DisappearingScaleBar camera position animation/state changes.

cc @LoyalAbbas @kikoso

@github-actions

github-actions Bot commented Sep 15, 2026 •

Copy link
Copy Markdown

Coverage (unit tests)

Compared against 14f18d0 on feature/robolectric-tests (#991).

Module Line % Change Branch % Change
maps-compose 40.78% 🔻 -5.04 12.61% 🔻 -2.62
maps-compose-utils 26.29% no change 12.70% no change
maps-compose-widgets 72.26% 🔺 +68.39 40.48% 🔺 +40.48
TOTAL 38.94% 🔺 +0.01 13.60% 🔻 -0.62

Line and branch coverage from unit test reports. History is recorded on the coverage-history branch after each merge to feature/robolectric-tests.

@dkhawk
dkhawk requested a review from kikoso September 15, 2026 21:59
@googlemaps-bot

googlemaps-bot commented Sep 15, 2026 •

Copy link
Copy Markdown
Contributor

Code Coverage

Overall Project 44.45% 🍏

There is no coverage information present for the Files changed

@dkhawk
dkhawk force-pushed the feature/robolectric-widgets-tests branch from 9f260d3 to bab4d05 Compare October 1, 2026 21:07
@dkhawk
dkhawk force-pushed the feature/robolectric-tests branch from 34f2003 to 57c2723 Compare October 1, 2026 21:07
@dkhawk
dkhawk force-pushed the feature/robolectric-tests branch from 57c2723 to a63c557 Compare October 9, 2026 15:32
@dkhawk
dkhawk force-pushed the feature/robolectric-widgets-tests branch from bab4d05 to d74586e Compare October 9, 2026 15:32

@kikoso kikoso left a comment

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.

Minor comments @dkhawk , but looks good! The instrumentation failure is the emulator not starting in maps-app (adb could not connect), unrelated to this PR, so a rerun should do.


val expectedDistance = startLatLng.sphericalDistance(endLatLng)
val expectedResult = (expectedDistance * 8 / 9).toInt()

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 repeats the same formula as calculateDistance, so if someone changes the ratio by mistake the test still passes. What about asserting against a known value instead? 0.001° of longitude at the equator is roughly 111 m, so we would expect around 98.

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.

Addressed in 8b64c660 — replaced the repeated formula with known ground-truth assertions: a mocked Projection (0.001° longitude at the equator → 8/9 = 98 m) asserting isEqualTo(98), plus Mercator zoom 12 ground-truth checks at 0° (3350..3450 m) and 60° (1650..1750 m, verifying cos(60°) = 0.5).

// Update camera position to trigger visibility LaunchedEffect
cameraState.position = CameraPosition.fromLatLngZoom(LatLng(37.7749, -122.4194), 14f)
composeTestRule.waitForIdle()
}

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.

Could we assert something here? Right now the test passes as long as nothing crashes. Setting composeTestRule.mainClock.autoAdvance = false and advancing past visibilityDurationMillis would let us check that the bar shows after the camera change and hides again, which is the actual behaviour of this composable.

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.

Addressed in 8b64c660 — added assertIsDisplayed() checks for "0 ft" / "0 m" in testScaleBar_rendersWithoutCrashing, and updated testDisappearingScaleBar_showsOnCameraChangeAndHidesAfterDuration with mainClock.autoAdvance = false to verify the bar displays on initial composition, hides after visibilityDurationMillis, re-appears on camera position change, and hides again.

import com.google.common.truth.Truth.assertThat
import com.google.maps.android.compose.CameraPositionState
import com.google.maps.android.ktx.utils.sphericalDistance
import io.mockk.every

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: ScaleBar.kt uses com.google.maps.android.sphericalDistance. Should we use the same one here so the test and the production code stay on the same implementation?

Suggested change
import io.mockk.every
import com.google.maps.android.sphericalDistance

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.

Addressed in 8b64c660 — now that the test asserts known ground-truth values instead of recomputing the distance formula, the sphericalDistance import has been removed from ScaleBarTest.kt altogether.

@dkhawk
dkhawk force-pushed the feature/robolectric-widgets-tests branch from d74586e to 95218a8 Compare November 9, 2026 22:00
@dkhawk
dkhawk force-pushed the feature/robolectric-tests branch from a63c557 to 1a8f666 Compare November 9, 2026 22:00
@dkhawk
dkhawk force-pushed the feature/robolectric-widgets-tests branch from 95218a8 to 8cb7775 Compare November 10, 2026 21:58
@dkhawk
dkhawk force-pushed the feature/robolectric-tests branch 2 times, most recently from df2a0d9 to ee29efc Compare November 10, 2026 23:38
@dkhawk
dkhawk force-pushed the feature/robolectric-widgets-tests branch 2 times, most recently from 253ef05 to a837bf7 Compare November 17, 2026 23:00
@dkhawk
dkhawk force-pushed the feature/robolectric-tests branch from ee29efc to 5dcab93 Compare November 17, 2026 23:00
@dkhawk
dkhawk force-pushed the feature/robolectric-tests branch from 5dcab93 to 6134e13 Compare November 17, 2026 23:13
@dkhawk
dkhawk force-pushed the feature/robolectric-widgets-tests branch from a837bf7 to 2551feb Compare November 17, 2026 23:13
- 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.
@dkhawk
dkhawk marked this pull request as ready for review November 24, 2026 20:07
@snippet-bot

snippet-bot Bot commented Nov 24, 2026

Copy link
Copy Markdown

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@dkhawk
dkhawk requested a review from kikoso November 24, 2026 20:08
@googlemaps-bot

Copy link
Copy Markdown
Contributor

Coverage (instrumentation tests)

Overall Project 45.96% 🍏

There is no coverage information present for the Files changed

@dkhawk
dkhawk merged commit 32bc253 into feature/robolectric-tests Nov 24, 2026
8 checks passed
@dkhawk
dkhawk deleted the feature/robolectric-widgets-tests branch November 24, 2026 20:57
dkhawk added a commit that referenced this pull request Nov 24, 2026
* 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.
dkhawk added a commit that referenced this pull request Nov 30, 2026
* 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.
dkhawk added a commit that referenced this pull request Dec 2, 2026
…st to KTX helpers (#1001)

* test: add JVM unit and composable integration tests using Android Maps Testing Toolkit

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

* test(widgets): address PR #1001 review feedback

- Delete maps-compose-widgets/src/debug/AndroidManifest.xml and use testImplementation(libs.androidx.test.compose.ui.manifest).
- Remove unused androidx.test.core and kotlinx-coroutines-test dependencies from maps-compose-widgets/build.gradle.kts.
- Document null-projection fallback behavior in ScaleBarTest.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants