Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,86 @@ class GoogleMapViewTests {
assertThat(cameraPositionState.position.target).isEqualTo(startingPosition)
}

@Test
fun testInitialCameraPositionIsAppliedToGoogleMapOptions() {
val expectedPosition = cameraPositionState.position
var capturedOptions: GoogleMapOptions? = null
composeTestRule.setContent {
GoogleMap(
cameraPositionState = cameraPositionState,
mapViewFactory = { context, options ->
capturedOptions = options
MapView(context, options)
},
)
}

assertThat(capturedOptions?.camera).isEqualTo(expectedPosition)
}

@Test
fun testInitialCameraPositionOverridesGoogleMapOptionsFactory() {
val configuredPosition = LatLng(7.89, 10.11)
val configuredZoom = 12f
val expectedPosition = cameraPositionState.position
var capturedOptions: GoogleMapOptions? = null
composeTestRule.setContent {
GoogleMap(
cameraPositionState = cameraPositionState,
googleMapOptionsFactory = {
GoogleMapOptions().camera(
CameraPosition.fromLatLngZoom(configuredPosition, configuredZoom),
)
},
mapViewFactory = { context, options ->
capturedOptions = options
MapView(context, options)
},
)
}

assertThat(capturedOptions?.camera).isEqualTo(expectedPosition)
}

@Test
fun testLoadedCameraPositionOverridesGoogleMapOptionsFactory() {
assumeValidApiKey()
val expectedPosition = cameraPositionState.position
val mapLoaded = CountDownLatch(1)
var mapView: MapView? = null
composeTestRule.setContent {
GoogleMap(
modifier = Modifier.fillMaxSize(),
cameraPositionState = cameraPositionState,
googleMapOptionsFactory = {
GoogleMapOptions().camera(
CameraPosition.fromLatLngZoom(LatLng(7.89, 10.11), 12f),
)
},
mapViewFactory = { context, options ->
MapView(context, options).also { mapView = it }
},
onMapLoaded = { mapLoaded.countDown() },
)
}
assertThat(mapLoaded.await(MAP_LOAD_TIMEOUT_SECONDS, TimeUnit.SECONDS)).isTrue()

val cameraChecked = CountDownLatch(1)
var loadedPosition: CameraPosition? = null
composeTestRule.runOnIdle {
checkNotNull(mapView).getMapAsync { map ->
Comment thread
RotBolt marked this conversation as resolved.
Resolved
loadedPosition = map.cameraPosition
cameraChecked.countDown()
}
}
assertThat(cameraChecked.await(MAP_LOAD_TIMEOUT_SECONDS, TimeUnit.SECONDS)).isTrue()
val actualPosition = checkNotNull(loadedPosition)
assertThat(actualPosition.target).isEqualTo(expectedPosition.target)
assertThat(actualPosition.zoom).isWithin(0.001f).of(expectedPosition.zoom)
assertThat(actualPosition.tilt).isWithin(0.001f).of(expectedPosition.tilt)
assertThat(actualPosition.bearing).isWithin(0.001f).of(expectedPosition.bearing)
}

@Test
fun testDefaultColorSchemeIsFollowSystem() {
var capturedOptions: GoogleMapOptions? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ import kotlinx.coroutines.launch
* @param contentDescription the content description for the map used by accessibility services to
* describe the map. If none is specified, the default is "Google Map".
* @param googleMapOptionsFactory the block for creating the [GoogleMapOptions] provided when the
* map is created
* map is created. Its camera is overridden by [cameraPositionState]'s position; configure the
* initial camera through [cameraPositionState].
* @param properties the properties for the map
* @param locationSource the [LocationSource] to be used to provide location data
* @param uiSettings the [MapUiSettings] to be used for UI-specific settings on the map
Expand Down Expand Up @@ -164,7 +165,8 @@ public fun GoogleMap(
* @param contentDescription the content description for the map used by accessibility services to
* describe the map. If none is specified, the default is "Google Map".
* @param googleMapOptionsFactory the block for creating the [GoogleMapOptions] provided when the
* map is created
* map is created. Its camera is overridden by [cameraPositionState]'s position; configure the
* initial camera through [cameraPositionState].
* @param properties the properties for the map
* @param locationSource the [LocationSource] to be used to provide location data
* @param uiSettings the [MapUiSettings] to be used for UI-specific settings on the map
Expand Down Expand Up @@ -268,6 +270,9 @@ public fun GoogleMap(
modifier = if (focusable) modifier.focusable() else modifier,
factory = { context ->
val baseOptions = googleMapOptionsFactory()
// Match the camera applied by CameraPositionState.setMap when the map attaches,
// avoiding a startup jump from a different options camera.
baseOptions.camera(cameraPositionState.position)
val isLiteMode = baseOptions.liteMode == true
// If mapColorScheme is passed to GoogleMap() and has not been explicitly set
// in googleMapOptionsFactory, apply it to GoogleMapOptions so MapView is created
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,12 @@ internal class GoogleMapTest {

@Test
internal fun testGoogleMap_customMapViewFactory_preservesBackwardCompatibility() {
val expectedPosition = CameraPosition.fromLatLngZoom(sydney, 12f)
var capturedOptions: GoogleMapOptions? = null

composeTestRule.setContent {
val cameraState = rememberCameraPositionState {
position = CameraPosition.fromLatLngZoom(sydney, 12f)
position = expectedPosition
}
GoogleMap(
cameraPositionState = cameraState,
Expand All @@ -215,22 +216,58 @@ internal class GoogleMapTest {

composeTestRule.waitForIdle()
assertThat(capturedOptions).isNotNull()
assertThat(capturedOptions?.camera).isEqualTo(expectedPosition)
}

@Test
internal fun testGoogleMap_initialCameraOverridesOptionsFactory() {
val expectedPosition = CameraPosition.fromLatLngZoom(sydney, 12f)
var capturedOptions: GoogleMapOptions? = null

composeTestRule.setContent {
val cameraState = rememberCameraPositionState {
position = expectedPosition
}
GoogleMap(
cameraPositionState = cameraState,
googleMapOptionsFactory = {
GoogleMapOptions().camera(
CameraPosition.fromLatLngZoom(LatLng(35.6762, 139.6503), 5f),
)
},
mapViewFactory = { context, options ->
capturedOptions = options
MapView(context, options)
},
)
}

composeTestRule.waitForIdle()
assertThat(capturedOptions?.camera).isEqualTo(expectedPosition)
}

@Test
internal fun testGoogleMap_customMapViewDelegateFactory_lifecycleAndMarkerComposable() {
val expectedPosition = CameraPosition.fromLatLngZoom(sydney, 13f)
val lifecycleEvents = mutableListOf<String>()
val fakeGoogleMap = ShadowMapView.createFakeGoogleMap()
var capturedOptions: GoogleMapOptions? = null
var customDelegate: MapViewDelegate<FrameLayout>? = null

composeTestRule.setContent {
val cameraState = rememberCameraPositionState {
position = CameraPosition.fromLatLngZoom(sydney, 13f)
position = expectedPosition
}
GoogleMap(
cameraPositionState = cameraState,
modifier = Modifier.fillMaxSize(),
mapViewDelegateFactory = { context, _ ->
googleMapOptionsFactory = {
GoogleMapOptions().camera(
CameraPosition.fromLatLngZoom(LatLng(35.6762, 139.6503), 5f),
)
},
mapViewDelegateFactory = { context, options ->
capturedOptions = options
object : MapViewDelegate<FrameLayout> {
override val mapView: FrameLayout = FrameLayout(context)
override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -274,6 +311,7 @@ internal class GoogleMapTest {
composeTestRule.waitForIdle()

assertThat(customDelegate).isNotNull()
assertThat(capturedOptions?.camera).isEqualTo(expectedPosition)
assertThat(lifecycleEvents).containsExactly("onCreate", "onStart", "onResume").inOrder()

val shadowMap = Shadow.extract(fakeGoogleMap) as ShadowGoogleMap
Expand Down
Loading