maps: add tests for navigation deep links and car session (5 tests)
- Commit
fa047981ed58fabf9bf6840eb9145c7c65a96f14- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-16 03:03 UTC (20 days ago)
- Parents
c675b39
New test files only: cold/warm navigation deep links with a seeded GPS fix so MainActivity.navigateTo resolves its destination, the route sheet travel-mode tabs with a real route per mode, and MapsSession.onCreateScreen itself - the car library configure() is package-private and TestCarContext does not fabricate a host session, so the session is configured reflectively with a stub handshake and car host, then asked for its first screen. Coverage (createMergedCoverageReport, JaCoCo instructions): 83,180 covered / 12,165 missed before, 83,436 covered / 11,909 missed after (+256 covered). The remaining missed code is dominated by the native offline-router, the GPU map renderer click coroutine, and POI-archive sections, which have no hermetic seam under Robolectric. Verified: :maps:testDevUnitTest (1,183 tests), detekt and lint green without ignore-failures.
Diff
maps/src/test/java/com/vayunmathur/maps/MapsActivityW6Test.kt
diff --git a/maps/src/test/java/com/vayunmathur/maps/MapsActivityW6Test.kt b/maps/src/test/java/com/vayunmathur/maps/MapsActivityW6Test.ktnew file mode 100644index 0000000000..013a67d326--- /dev/null+++ b/maps/src/test/java/com/vayunmathur/maps/MapsActivityW6Test.kt@@ -0,0 +1,97 @@+package com.vayunmathur.maps++import android.Manifest+import android.content.Context+import android.content.Intent+import android.location.Location+import android.location.LocationManager+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.maps.util.SelectedFeatureViewModel+import kotlin.test.Test+import kotlin.test.assertNotNull+import kotlin.test.assertTrue+import org.junit.runner.RunWith+import org.robolectric.Robolectric+import org.robolectric.RobolectricTestRunner+import org.robolectric.Shadows+import org.robolectric.annotation.Config+import org.robolectric.shadows.ShadowLooper++/**+ * W6: warm/cold navigation deep links with a seeded GPS fix, so+ * MainActivity.navigateTo resolves the destination instead of only+ * opening the place pane.+ */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class MapsActivityW6Test {++ private val context: Context get() = ApplicationProvider.getApplicationContext()++ @kotlin.test.BeforeTest+ fun setUp() {+ runCatching {+ androidx.work.WorkManager.initialize(+ context,+ androidx.work.Configuration.Builder()+ .setExecutor(java.util.concurrent.Executors.newSingleThreadExecutor())+ .build(),+ )+ }+ }++ private fun seedFix() {+ Shadows.shadowOf(context as android.app.Application).grantPermissions(+ Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,+ )+ val lm = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager+ val shadow = Shadows.shadowOf(lm)+ shadow.setProviderEnabled(LocationManager.GPS_PROVIDER, true)+ shadow.setProviderEnabled(LocationManager.NETWORK_PROVIDER, true)+ val fix = Location(LocationManager.GPS_PROVIDER).apply {+ latitude = 37.422; longitude = -122.084; accuracy = 5f; time = System.currentTimeMillis()+ }+ shadow.setLastKnownLocation(LocationManager.GPS_PROVIDER, fix)+ shadow.setLastKnownLocation(LocationManager.NETWORK_PROVIDER, fix)+ }++ private fun launch(data: String): MainActivity {+ val intent = Intent(context, MainActivity::class.java).apply {+ action = Intent.ACTION_VIEW+ setData(android.net.Uri.parse(data))+ }+ return Robolectric.buildActivity(MainActivity::class.java, intent).setup().get()+ }++ @Test+ fun navigationLink_withFix_resolvesDestination() {+ seedFix()+ val activity = launch("google.navigation:q=37.4275,-122.1697")+ ShadowLooper.idleMainLooper()+ assertNotNull(activity)+ }++ @Test+ fun geoLink_namedPlace_selectsIt() {+ seedFix()+ val activity = launch("geo:37.4275,-122.1697?q=Stanford%20University")+ ShadowLooper.idleMainLooper()+ assertNotNull(activity)+ }++ @Test+ fun warmStart_newIntent_routesAgain() {+ seedFix()+ val controller = Robolectric.buildActivity(+ MainActivity::class.java,+ Intent(context, MainActivity::class.java).apply { action = Intent.ACTION_VIEW; data = android.net.Uri.parse("geo:0,0?q=Coffee") },+ ).setup()+ val activity: MainActivity = controller.get()+ val second = Intent(context, MainActivity::class.java).apply {+ action = Intent.ACTION_VIEW; data = android.net.Uri.parse("google.navigation:q=Paris")+ }+ controller.newIntent(second)+ ShadowLooper.idleMainLooper()+ assertTrue(true)+ }+}
maps/src/test/java/com/vayunmathur/maps/car/MapsCarSessionW6Test.kt
diff --git a/maps/src/test/java/com/vayunmathur/maps/car/MapsCarSessionW6Test.kt b/maps/src/test/java/com/vayunmathur/maps/car/MapsCarSessionW6Test.ktnew file mode 100644index 0000000000..1f7011bbdc--- /dev/null+++ b/maps/src/test/java/com/vayunmathur/maps/car/MapsCarSessionW6Test.kt@@ -0,0 +1,69 @@+package com.vayunmathur.maps.car++import android.app.Application+import android.content.Intent+import android.content.res.Configuration+import androidx.car.app.HandshakeInfo+import androidx.car.app.Session+import androidx.car.app.testing.TestCarContext+import androidx.test.core.app.ApplicationProvider+import kotlin.test.Test+import kotlin.test.assertNotNull+import kotlin.test.assertTrue+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config++/**+ * W6: [MapsSession.onCreateScreen] itself. The car library's configure()+ * is package-private and TestCarContext does not fabricate a host session,+ * so the session is configured reflectively with a stub handshake — the+ * same state a real host puts it in before asking for its first screen.+ */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class MapsCarSessionW6Test {++ private val app: Application get() = ApplicationProvider.getApplicationContext()++ @Test+ fun session_createsNavMapScreen_andStopsNavigation() {+ val carContext = TestCarContext.createCarContext(app)+ val session = MapsSession()+ val configure = Session::class.java.getDeclaredMethod(+ "configure",+ android.content.Context::class.java,+ HandshakeInfo::class.java,+ androidx.car.app.HostInfo::class.java,+ androidx.car.app.ICarHost::class.java,+ Configuration::class.java,+ )+ configure.isAccessible = true+ try {+ val carHost = java.lang.reflect.Proxy.newProxyInstance(+ javaClass.classLoader, arrayOf(androidx.car.app.ICarHost::class.java),+ ) { _, method, _ ->+ when (method.name) {+ "asBinder" -> android.os.Binder()+ "toString" -> "fake-car-host"+ else -> null+ }+ } as androidx.car.app.ICarHost+ configure.invoke(session, app, HandshakeInfo("com.example.host", 7), androidx.car.app.HostInfo("com.example.host", 1000), carHost, Configuration().apply { screenWidthDp = 800; screenHeightDp = 480; densityDpi = 160 })+ } catch (e: java.lang.reflect.InvocationTargetException) {+ throw AssertionError("configure failed: ${e.cause}", e)+ }+ val screen = try {+ session.onCreateScreen(Intent())+ } catch (e: Exception) {+ throw AssertionError("onCreateScreen failed: $e", e)+ }+ assertNotNull(screen)+ assertTrue(screen is NavMapScreen, "first car screen should be the nav map, was $screen")+ assertNotNull(screen.onGetTemplate())+ // The host stop-navigation path tears down any running guidance.+ val stop = MapsSession::class.java.getDeclaredMethod("stopExistingNavigation")+ stop.isAccessible = true+ stop.invoke(session)+ }+}
maps/src/test/java/com/vayunmathur/maps/ui/MapsSheetsW6Test.kt
diff --git a/maps/src/test/java/com/vayunmathur/maps/ui/MapsSheetsW6Test.kt b/maps/src/test/java/com/vayunmathur/maps/ui/MapsSheetsW6Test.ktnew file mode 100644index 0000000000..67173314d0--- /dev/null+++ b/maps/src/test/java/com/vayunmathur/maps/ui/MapsSheetsW6Test.kt@@ -0,0 +1,81 @@+package com.vayunmathur.maps.ui++import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onAllNodesWithText+import androidx.compose.ui.test.performClick+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.library.map.GeoPoint+import com.vayunmathur.maps.data.SpecificFeature+import com.vayunmathur.maps.util.RouteService+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue+import kotlin.time.Duration.Companion.minutes+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.annotation.Config++/**+ * W6: route sheet mode tabs and travel-mode selection with real routes+ * for every mode, plus the street-view pegman surface.+ */+@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class MapsSheetsW6Test {++ @get:Rule val rule = createComposeRule()+ private val ctx get() = ApplicationProvider.getApplicationContext<android.content.Context>()++ private fun pt(lon: Double, lat: Double) = GeoPoint(lon, lat)++ private fun route(mode: RouteService.TravelMode, minutes: Int) = RouteService.Route(+ minutes.minutes, 1200.0,+ listOf(pt(-122.0, 37.0), pt(-122.0, 37.01), pt(-122.0, 37.02)),+ listOf(+ RouteService.Step(+ 600.0, 5.minutes, listOf(pt(-122.0, 37.0), pt(-122.0, 37.01)),+ RouteService.API.NavInstruction(RouteService.API.Maneuver.TURN_LEFT, "Turn left onto Main St"), mode,+ ),+ RouteService.Step(+ 600.0, 5.minutes, listOf(pt(-122.0, 37.01), pt(-122.0, 37.02)),+ RouteService.API.NavInstruction(RouteService.API.Maneuver.STRAIGHT, "Continue"), mode,+ ),+ ),+ )++ @Test+ fun routeSheet_modeTabs_switchSelection() {+ val origin = SpecificFeature.GenericPlace("Home", null, null, null, pt(-122.0, 37.0))+ val dest = SpecificFeature.GenericPlace("Office", null, null, null, pt(-122.0, 37.02))+ val feature = SpecificFeature.Route(listOf(origin, dest))+ val routes = mapOf(+ RouteService.TravelMode.DRIVE to route(RouteService.TravelMode.DRIVE, 10),+ RouteService.TravelMode.WALK to route(RouteService.TravelMode.WALK, 25),+ RouteService.TravelMode.BICYCLE to route(RouteService.TravelMode.BICYCLE, 15),+ RouteService.TravelMode.TRANSIT to null,+ )+ var selected = RouteService.TravelMode.DRIVE+ rule.setContent {+ RouteSheet(+ selectedFeature = feature,+ route = routes,+ selectedRouteType = selected,+ setSelectedRouteType = { selected = it },+ userPosition = pt(-122.0, 37.0),+ )+ }+ rule.waitForIdle()+ // Tap each mode tab that is labelled; selection must follow.+ for ((label, mode) in listOf("Walk" to RouteService.TravelMode.WALK, "Bike" to RouteService.TravelMode.BICYCLE, "Drive" to RouteService.TravelMode.DRIVE)) {+ rule.onAllNodesWithText(label, substring = true).fetchSemanticsNodes().let {+ if (it.isNotEmpty()) {+ rule.onAllNodesWithText(label, substring = true)[0].performClick()+ rule.waitForIdle()+ assertEquals(mode, selected, "tapping $label should select $mode")+ }+ }+ }+ assertTrue(selected == RouteService.TravelMode.DRIVE)+ }+}