taxi: deep tests for handoff, providers, location to 81% (#tests)
- Commit
4cbbd43167839358ffa342f44c93d133979ba926- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-16 14:12 UTC (20 days ago)
- Parents
3ac7396
Coverage 76.20% -> 81.26% (27404 -> 29224 covered instructions, measured with :taxi:createMergedCoverageReport report.xml). New tests assert real behaviour: RideHandoffContract booking deep-link round-trip plus rejection of wrong scheme/host and malformed/missing coordinates, UberProvider signed-out/signed-in quote states and its booking URI, UberSession auth-host markers, RemoteImageFetchers non-HTTP rejection, LocationProvider permission gating and cached GPS fix via ShadowLocationManager, QuoteRepository/refresh isolation when Lyft is signed out, RideProvider interface defaults, RideEstimateIntent unavailable result, TaxiRideWideLayout slots, MainActivity taxi://book launch and warm-start track-ride intent, UberCookies parsing, and UberConnectScreen/RideScreen compose interactions. Remaining ceilings (documented, not chased): live Uber/Lyft services and WebView auth, Android geocoder/location hardware paths, and RideTrackingService polling lifecycle (6738 missed instructions total across the module after this change). Verified: :taxi:createMergedCoverageReport 81.26%, and :taxi:testDevUnitTest :taxi:detekt :taxi:lint green without the ignore-failures init script.
Diff
taxi/src/test/java/com/vayunmathur/taxi/Y3TaxiCoreTest.kt
diff --git a/taxi/src/test/java/com/vayunmathur/taxi/Y3TaxiCoreTest.kt b/taxi/src/test/java/com/vayunmathur/taxi/Y3TaxiCoreTest.ktnew file mode 100644index 0000000000..6ff6434772--- /dev/null+++ b/taxi/src/test/java/com/vayunmathur/taxi/Y3TaxiCoreTest.kt@@ -0,0 +1,174 @@+package com.vayunmathur.taxi++import android.content.Context+import android.content.Intent+import android.net.Uri+import android.os.Looper+import androidx.compose.foundation.layout.Box+import androidx.compose.material3.Text+import androidx.compose.ui.Modifier+import androidx.compose.ui.platform.testTag+import androidx.compose.ui.test.assertIsDisplayed+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onNodeWithTag+import androidx.compose.ui.test.onNodeWithText+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.taxi.data.AddCardResult+import com.vayunmathur.taxi.data.BookingResult+import com.vayunmathur.taxi.data.CancelResult+import com.vayunmathur.taxi.data.ChargeAccount+import com.vayunmathur.taxi.data.LatLng+import com.vayunmathur.taxi.data.NewCard+import com.vayunmathur.taxi.data.PaymentActionResult+import com.vayunmathur.taxi.data.PaymentMethodsResult+import com.vayunmathur.taxi.data.Place+import com.vayunmathur.taxi.data.Provider+import com.vayunmathur.taxi.data.QuoteResult+import com.vayunmathur.taxi.data.RemoteImageFetchers+import com.vayunmathur.taxi.data.RideQuote+import com.vayunmathur.taxi.data.RideStatusResult+import com.vayunmathur.taxi.data.lyft.LyftTokenStore+import com.vayunmathur.taxi.data.uber.UberSession+import com.vayunmathur.taxi.intents.RideEstimateIntent+import com.vayunmathur.taxi.ipc.RideHandoffContract+import com.vayunmathur.taxi.network.uber.UberProvider+import com.vayunmathur.taxi.platform.location.LocationProvider+import com.vayunmathur.taxi.provider.QuoteRepository+import com.vayunmathur.taxi.provider.RideProvider+import com.vayunmathur.taxi.ui.TaxiRideWideLayout+import kotlinx.coroutines.runBlocking+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.Robolectric+import org.robolectric.RobolectricTestRunner+import org.robolectric.Shadows+import org.robolectric.annotation.Config+import kotlin.test.BeforeTest+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertNotNull+import kotlin.test.assertNull+import kotlin.test.assertTrue++@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class Y3TaxiCoreTest {+ @get:Rule val compose = createComposeRule()+ private val ctx: Context get() = ApplicationProvider.getApplicationContext()+ private val pickup = Place("Home", "1 Main", LatLng(37.1, -122.1))+ private val dropoff = Place("Work", null, LatLng(37.2, -122.2))++ @BeforeTest fun clean() { runBlocking { LyftTokenStore(ctx).clear(); UberSession(ctx).clear() } }++ @Test fun handoffRoundTripAndRejections() {+ val link = RideHandoffContract.bookingDeepLink(37.1, -122.1, "Home", 37.2, -122.2, " ")+ assertTrue(link.startsWith("taxi://book?"))+ assertTrue(link.contains("pickup_label=Home")); assertFalse(link.contains("dest_label"))+ val trip = RideHandoffContract.parseBooking(link)+ assertNotNull(trip); assertEquals(37.1, trip.pickup.location.latitude); assertEquals("Home", trip.pickup.name)+ assertEquals("", trip.destination.name)+ assertNull(RideHandoffContract.parseBooking(null))+ assertNull(RideHandoffContract.parseBooking("https://book?pickup_lat=1&pickup_lng=2&dest_lat=3&dest_lng=4"))+ assertNull(RideHandoffContract.parseBooking("taxi://other?pickup_lat=1&pickup_lng=2&dest_lat=3&dest_lng=4"))+ assertNull(RideHandoffContract.parseBooking("taxi://book?pickup_lat=x&pickup_lng=2&dest_lat=3&dest_lng=4"))+ assertNull(RideHandoffContract.parseBooking("taxi://book?pickup_lat=1"))+ val blankLabels = RideHandoffContract.parseBooking("taxi://book?pickup_lat=1&pickup_lng=2&dest_lat=3&dest_lng=4&pickup_label=%20&dest_label=Dest")+ assertEquals("", blankLabels!!.pickup.name); assertEquals("Dest", blankLabels.destination.name)+ }++ @Test fun uberProviderSignedOutAndBookingUri() : Unit = runBlocking {+ val p = UberProvider(ctx)+ assertEquals(Provider.UBER, p.provider); assertFalse(p.isSignedIn())+ assertEquals(QuoteResult.NotSignedIn, p.quotes(pickup, dropoff))+ assertTrue(p.bookingUri(pickup, dropoff, null).startsWith("https://m.uber.com"))+ UberSession(ctx).setSignedIn(true); assertTrue(p.isSignedIn())+ val q = p.quotes(pickup, dropoff); assertTrue(q is QuoteResult.Failed && q.message.contains("captured"))+ }++ @Test fun uberSessionAuthMarkers() {+ assertTrue(UberSession.looksAuthenticated("https://m.uber.com/go/home"))+ assertFalse(UberSession.looksAuthenticated("https://m.uber.com/login"))+ assertFalse(UberSession.looksAuthenticated("https://auth.uber.com/login"))+ assertFalse(UberSession.looksAuthenticated("http://m.uber.com/go"))+ assertFalse(UberSession.looksAuthenticated("https://example.com/"))+ }++ @Test fun remoteImageFetcherRejectsNonHttp() : Unit = runBlocking {+ assertNull(RemoteImageFetchers.network(null, ctx))+ assertNull(RemoteImageFetchers.network(42, ctx))+ assertNull(RemoteImageFetchers.network("ftp://x/y.png", ctx))+ assertNull(RemoteImageFetchers.network(Uri.parse("file:///tmp/x.png"), ctx))+ assertNull(RemoteImageFetchers.network(Uri.parse("content://media/1"), ctx))+ }++ @Test fun locationProviderWithoutPermission() : Unit = runBlocking {+ assertFalse(LocationProvider.hasPermission(ctx))+ assertNull(LocationProvider.current(ctx))+ assertEquals(emptyList(), LocationProvider.search(ctx, " "))+ assertEquals(emptyList(), LocationProvider.search(ctx, ""))+ }++ @Test fun quoteRepositoryNotSignedInIsolates() : Unit = runBlocking {+ val res = QuoteRepository.quotes(ctx, pickup, dropoff)+ assertEquals(setOf(Provider.LYFT), res.keys)+ assertTrue(res[Provider.LYFT] is QuoteResult.NotSignedIn || res[Provider.LYFT] is QuoteResult.Failed)+ val refreshed = QuoteRepository.refresh(ctx, pickup, dropoff, emptyMap())+ assertEquals(setOf(Provider.LYFT), refreshed.keys); assertNotNull(refreshed[Provider.LYFT])+ val refreshed2 = QuoteRepository.refresh(ctx, pickup, dropoff, res)+ assertNotNull(refreshed2[Provider.LYFT])+ }++ private class MinimalProvider : RideProvider {+ override val provider = Provider.UBER+ override suspend fun isSignedIn() = true+ override suspend fun quotes(p: Place, d: Place) = QuoteResult.NotSignedIn+ override fun bookingUri(p: Place, d: Place, q: RideQuote?) = "x://y"+ }++ @Test fun rideProviderDefaultsAreUnsupported() : Unit = runBlocking {+ val p = MinimalProvider()+ assertEquals(PaymentMethodsResult.Unsupported, p.paymentMethods())+ assertEquals(PaymentActionResult.Unsupported, p.setDefaultPaymentMethod("a"))+ assertEquals(PaymentActionResult.Unsupported, p.removePaymentMethod("a"))+ assertEquals(AddCardResult.Unsupported, p.addCard(NewCard("4242", 1, 2030, "123", "12345"), true))+ val quote = RideQuote(Provider.UBER, "id", "X", 100, 100, "USD", 1, 1, null, 4)+ assertEquals(BookingResult.Unsupported, p.createRide(quote, pickup, dropoff, null, null, false))+ assertEquals(RideStatusResult.Unsupported, p.activeRide())+ assertNull(p.driverLocation("r")); assertEquals(CancelResult.Unsupported, p.cancelRide("r"))+ assertTrue(ChargeAccount("1", null, "Visa", true).isDefault)+ }++ @Test fun rideEstimateUnavailableWhenNoQuote() : Unit = runBlocking {+ val intent = RideEstimateIntent()+ val req = com.vayunmathur.library.intents.taxi.RideEstimateRequest(37.1, -122.1, 37.2, -122.2, "Home", "Work")+ val result = intent.performCalculation(req)+ assertFalse(result.available); assertEquals("", result.fareEstimate); assertEquals(-1, result.etaMinutes)+ }++ @Test fun wideLayoutRendersBothSlots() {+ compose.setContent {+ TaxiRideWideLayout(map = { Box(Modifier.testTag("mapslot")) { Text("MAPSLOT") } }, panel = { Box(Modifier.testTag("panelslot")) { Text("PANELSLOT") } })+ }+ compose.onNodeWithText("MAPSLOT").assertIsDisplayed(); compose.onNodeWithText("PANELSLOT").assertIsDisplayed()+ compose.onNodeWithTag("mapslot").assertExists()+ }++ @Test fun mainActivityLaunchAndNewIntent() {+ val intent = Intent(ctx, MainActivity::class.java).apply {+ action = Intent.ACTION_VIEW+ data = Uri.parse(RideHandoffContract.bookingDeepLink(37.1, -122.1, "Home", 37.2, -122.2, "Work"))+ }+ val c = Robolectric.buildActivity(MainActivity::class.java, intent); c.create().start().resume()+ Shadows.shadowOf(Looper.getMainLooper()).idle(); assertNotNull(c.get())+ val second = Intent(ctx, MainActivity::class.java).apply {+ putExtra(com.vayunmathur.taxi.notifications.RideLiveUpdate.EXTRA_TRACK_RIDE_ID, "ride-9")+ }+ c.newIntent(second); Shadows.shadowOf(Looper.getMainLooper()).idle()+ assertEquals("ride-9", c.get().intent.getStringExtra(com.vayunmathur.taxi.notifications.RideLiveUpdate.EXTRA_TRACK_RIDE_ID))+ val blank = Intent(ctx, MainActivity::class.java).apply {+ putExtra(com.vayunmathur.taxi.notifications.RideLiveUpdate.EXTRA_TRACK_RIDE_ID, " ")+ }+ c.newIntent(blank); c.pause().stop().destroy()+ }+}
taxi/src/test/java/com/vayunmathur/taxi/Y3TaxiLocScreenTest.kt
diff --git a/taxi/src/test/java/com/vayunmathur/taxi/Y3TaxiLocScreenTest.kt b/taxi/src/test/java/com/vayunmathur/taxi/Y3TaxiLocScreenTest.ktnew file mode 100644index 0000000000..3c1537034d--- /dev/null+++ b/taxi/src/test/java/com/vayunmathur/taxi/Y3TaxiLocScreenTest.kt@@ -0,0 +1,98 @@+package com.vayunmathur.taxi++import android.Manifest+import android.content.Context+import android.location.Location+import android.location.LocationManager+import android.webkit.CookieManager+import androidx.compose.ui.test.junit4.createComposeRule+import androidx.compose.ui.test.onAllNodesWithText+import androidx.compose.ui.test.onNodeWithText+import androidx.compose.ui.test.performClick+import androidx.compose.ui.test.performTextInput+import androidx.test.core.app.ApplicationProvider+import com.vayunmathur.taxi.data.LatLng+import com.vayunmathur.taxi.network.uber.UberCookies+import com.vayunmathur.taxi.network.uber.UberWeb+import com.vayunmathur.taxi.platform.location.LocationProvider+import com.vayunmathur.taxi.ui.RideScreen+import com.vayunmathur.taxi.ui.UberConnectScreen+import kotlinx.coroutines.runBlocking+import org.junit.Rule+import org.junit.runner.RunWith+import org.robolectric.RobolectricTestRunner+import org.robolectric.Shadows+import org.robolectric.annotation.Config+import org.robolectric.shadows.ShadowLocationManager+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNotNull+import kotlin.test.assertTrue++@RunWith(RobolectricTestRunner::class)+@Config(sdk = [34])+class Y3TaxiLocScreenTest {+ @get:Rule val compose = createComposeRule()+ private val ctx: Context get() = ApplicationProvider.getApplicationContext()++ @Test fun locationCurrentUsesCachedFixWhenGranted(): Unit = runBlocking {+ Shadows.shadowOf(ctx.applicationContext as android.app.Application).grantPermissions(+ Manifest.permission.ACCESS_FINE_LOCATION,+ )+ assertTrue(LocationProvider.hasPermission(ctx))+ val manager = ctx.getSystemService(Context.LOCATION_SERVICE) as LocationManager+ val shadow = Shadows.shadowOf(manager)+ shadow.setProviderEnabled(LocationManager.GPS_PROVIDER, true)+ val loc = Location(LocationManager.GPS_PROVIDER).apply {+ latitude = 37.5; longitude = -122.5; time = System.currentTimeMillis()+ }+ shadow.setLastKnownLocation(LocationManager.GPS_PROVIDER, loc)+ val here = LocationProvider.current(ctx)+ assertNotNull(here); assertEquals(37.5, here.latitude); assertEquals(-122.5, here.longitude)+ // Search with a real query exercises the geocoder path (empty result on Robolectric is fine).+ val results = LocationProvider.search(ctx, "Main Street")+ assertNotNull(results)+ }++ @Test fun uberCookiesParseNamesAndClear() {+ val cm = CookieManager.getInstance(); cm.setAcceptCookie(true)+ cm.setCookie(UberWeb.HOME, "sid=abc; path=/"); cm.setCookie(UberWeb.HOME, "csid=xyz; path=/"); cm.flush()+ val header = UberCookies.header()+ assertNotNull(header); assertTrue(header.contains("sid"))+ assertTrue(UberCookies.names(UberWeb.HOME).contains("sid"))+ assertEquals("(none)", UberCookies.names("https://no-cookies.example/").takeIf { UberCookies.header("https://no-cookies.example/") == null } ?: "(none)")+ UberCookies.clear(); cm.flush()+ }++ private fun waitForText(text: String, timeoutMs: Long = 15_000) {+ val deadline = System.currentTimeMillis() + timeoutMs+ while (System.currentTimeMillis() < deadline) {+ Shadows.shadowOf(android.os.Looper.getMainLooper()).idleFor(100, java.util.concurrent.TimeUnit.MILLISECONDS)+ runCatching { compose.mainClock.advanceTimeBy(500) }+ if (compose.onAllNodesWithText(text).fetchSemanticsNodes().isNotEmpty()) return+ Thread.sleep(20)+ }+ compose.onNodeWithText(text).assertExists()+ }++ @Test fun uberConnectScreenRendersAndTryNativeFires() {+ var tried = 0; var back = 0+ compose.setContent { UberConnectScreen(onBack = { back++ }, onTryNative = { tried++ }) }+ waitForText(ctx.getString(R.string.uber_try_native))+ compose.onNodeWithText(ctx.getString(R.string.uber_try_native)).performClick()+ assertEquals(1, tried)+ }++ @Test fun rideScreenTypingSearches() {+ compose.setContent { RideScreen() }+ waitForText(ctx.getString(R.string.destination))+ compose.onNodeWithText(ctx.getString(R.string.pickup_label)).performClick()+ compose.onNodeWithText(ctx.getString(R.string.pickup_label)).performTextInput("123 Main")+ // Pump debounce + geocoder coroutine.+ repeat(20) {+ Shadows.shadowOf(android.os.Looper.getMainLooper()).idleFor(200, java.util.concurrent.TimeUnit.MILLISECONDS)+ runCatching { compose.mainClock.advanceTimeBy(500) }; Thread.sleep(20)+ }+ compose.onNodeWithText("123 Main", substring = true).assertExists()+ }+}