Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Co-authored-by: Claude Code (Claude Opus 5.5)
package com.android.polybazaar.map

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.test.junit4.v2.createComposeRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.android.polybazaar.listing.model.ListingType
import com.android.polybazaar.map.model.Campus
import com.android.polybazaar.map.model.Coordinates
import com.android.polybazaar.map.ui.ListingsMap
import com.android.polybazaar.map.viewmodel.MapCentre
import com.android.polybazaar.map.viewmodel.MapMarker
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/**
* Runs the Google map on a real device (the CI emulator has Google Play services), which unit tests
* can't: an item and a skill marker, with the renter's circle and then a campus marker. It waits
* for the map to be created (onMapReady) rather than loaded: tiles need a key that accepts this
* build, which debug builds don't have, so onMapLoaded might never fire.
*/
@RunWith(AndroidJUnit4::class)
class ListingsMapTest {

@get:Rule val composeTestRule = createComposeRule()

private val markers =
listOf(
MapMarker(
"drill",
"Bosch drill set",
ListingType.ITEM,
Coordinates(46.519, 6.567),
"CHF 8",
),
MapMarker(
"maths",
"Analysis tutoring",
ListingType.SERVICE,
Coordinates(46.522, 6.579),
"CHF 25",
),
)

@Test
fun mapWithMarkers_isCreated_aroundTheRenter_onACampus_andWhileChoosing() {
var centre: MapCentre by mutableStateOf(MapCentre.Located(Coordinates(46.52, 6.57)))
var ready = false
composeTestRule.setContent {
ListingsMap(centre = centre, markers = markers, onMapReady = { ready = true })
}

// The map exists, with its markers and the renter's circle: no crash so far.
composeTestRule.waitUntil(MAP_READY_MILLIS) { ready }

// Moving to a campus marker, then to the overview, recomposes without crashing.
composeTestRule.runOnIdle { centre = MapCentre.OnCampus(Campus.EPFL) }
composeTestRule.waitForIdle()
composeTestRule.runOnIdle { centre = MapCentre.NeedsChoice() }
composeTestRule.waitForIdle()
assertTrue(ready)
}

private companion object {
/** Generous for a cold emulator; the map is usually created within a second or two. */
const val MAP_READY_MILLIS = 20_000L
}
}
7 changes: 7 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="${MAPS_API_KEY}" />
<!-- Google Maps' Play Services module still uses Apache HTTP, which Android 9+ no longer
loads by default: without this the map crashes with NoClassDefFoundError
org/apache/http/ProtocolVersion (seen on the CI emulator, #391). Not required, so
devices without the library can still install the app. -->
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
</application>

</manifest>
99 changes: 99 additions & 0 deletions app/src/main/java/com/android/polybazaar/map/ui/ListingsMap.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Co-authored-by: Claude Code (Claude Opus 5.5)
package com.android.polybazaar.map.ui

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.key
import androidx.compose.ui.Modifier
import com.android.polybazaar.R
import com.android.polybazaar.auth.ui.AuthColors
import com.android.polybazaar.map.model.Coordinates
import com.android.polybazaar.map.viewmodel.MapCentre
import com.android.polybazaar.map.viewmodel.MapMarker
import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.LatLng
import com.google.maps.android.compose.Circle
import com.google.maps.android.compose.GoogleMap
import com.google.maps.android.compose.MapEffect
import com.google.maps.android.compose.MapUiSettings
import com.google.maps.android.compose.MapsComposeExperimentalApi
import com.google.maps.android.compose.MarkerComposable
import com.google.maps.android.compose.rememberCameraPositionState
import com.google.maps.android.compose.rememberUpdatedMarkerState

/** Radius of the "approximate location" circle (Figma "Map — Centred on you"), in metres. */
private const val APPROXIMATE_RADIUS_METRES = 150.0

private fun Coordinates.toLatLng() = LatLng(latitude, longitude)

/**
* The Google map itself: camera from [cameraFor], one marker per listing with its item or skill
* icon, and the renter's approximate circle or their campus marker. [onMapReady] is called once the
* map is created (tests wait for it). Kept to drawing only, since Google Maps can't run in unit
* tests: every decision is in the tested functions of MapScreen.kt.
*/
@OptIn(MapsComposeExperimentalApi::class)
@Composable
internal fun ListingsMap(
centre: MapCentre,
markers: List<MapMarker>,
onMapReady: () -> Unit = {},
) {
val camera = cameraFor(centre)
val cameraState = rememberCameraPositionState {
position = CameraPosition.fromLatLngZoom(camera.target.toLatLng(), camera.zoom)
}
// Moves when the centre changes, for example once the location is read or a campus is picked.
// Setting the position rather than animating: CameraUpdateFactory needs the map to be
// initialised first, which isn't guaranteed this early on a slow device.
LaunchedEffect(camera) {
cameraState.position = CameraPosition.fromLatLngZoom(camera.target.toLatLng(), camera.zoom)
}
GoogleMap(
modifier = Modifier.fillMaxSize(),
cameraPositionState = cameraState,
// The location layer and the bottom bar already cover the edges: keep the map's own
// controls out of their way.
uiSettings = MapUiSettings(zoomControlsEnabled = false, mapToolbarEnabled = false),
) {
// Once the map exists, whether or not its tiles can load: lets the instrumented test wait for
// the map instead of sleeping.
MapEffect(Unit) { onMapReady() }
markers.forEach { marker ->
// Keyed by listing, not by its place in the list: after a reload that drops or reorders
// listings, each marker keeps its own state instead of taking its neighbour's, and a listing
// its owner moved follows its new position (rememberUpdatedMarkerState).
key(marker.id) {
MarkerComposable(
keys = arrayOf<Any>(marker.id, marker.type),
state = rememberUpdatedMarkerState(position = marker.coordinates.toLatLng()),
title = markerLabel(marker),
) {
MarkerPin(icon = markerIcon(marker.type))
}
}
}
when (centre) {
is MapCentre.Located ->
Circle(
center = centre.coordinates.toLatLng(),
radius = APPROXIMATE_RADIUS_METRES,
fillColor = AuthColors.Accent.copy(alpha = 0.15f),
strokeColor = AuthColors.Accent.copy(alpha = 0.4f),
strokeWidth = 2f,
)
is MapCentre.OnCampus ->
key(centre.campus) {
MarkerComposable(
keys = arrayOf<Any>(centre.campus),
state = rememberUpdatedMarkerState(position = centre.campus.centre.toLatLng()),
title = centre.campus.name,
) {
MarkerPin(icon = R.drawable.ic_campus)
}
}
else -> Unit
}
}
}
Loading
Loading