Skip to content
Draft
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
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ materialIconsExtendedAndroid = "1.7.8"
# Google Maps Platform
mapsecrets = "2.0.1"
mapsktx = "6.4.1"
mapsRobolectric = "1.0.0"

# Testing
androidCore = "1.7.0"
Expand Down Expand Up @@ -74,6 +75,7 @@ material = { group = "com.google.android.material", name = "material", version.r
maps-ktx-std = { module = "com.google.maps.android:maps-ktx", version.ref = "mapsktx" }
maps-ktx-utils = { module = "com.google.maps.android:maps-utils-ktx", version.ref = "mapsktx" }
maps-secrets-plugin = { module = "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin", version.ref = "mapsecrets" }
maps-robolectric-shadows = { module = "com.google.android.maps.robolectric:shadows", version.ref = "mapsRobolectric" }

# Testing
androidx-test-compose-ui = { module = "androidx.compose.ui:ui-test-junit4" }
Expand Down
20 changes: 20 additions & 0 deletions maps-compose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ android {
enableAndroidTestCoverage = true
}
}

testOptions {
unitTests {
isIncludeAndroidResources = true
}
}
}

kotlin {
Expand Down Expand Up @@ -93,6 +99,14 @@ dependencies {
api(libs.maps.ktx.std)

testImplementation(libs.test.junit)
testImplementation(libs.robolectric)
testImplementation(libs.truth)
testImplementation(libs.kotlinx.coroutines.test)
testImplementation(libs.maps.robolectric.shadows)
testImplementation(libs.mockk)
testImplementation(libs.androidx.test.compose.ui)
testImplementation(libs.androidx.activity.compose)
testImplementation(libs.androidx.test.core)

androidTestImplementation(platform(libs.androidx.compose.bom))
androidTestImplementation(libs.androidx.test.espresso)
Expand Down Expand Up @@ -132,4 +146,10 @@ dokka {
dokkaSourceSets.configureEach {

}
}

tasks.withType<Test>().configureEach {
javaLauncher.set(javaToolchains.launcherFor {
languageVersion.set(JavaLanguageVersion.of(21))
})
}
23 changes: 23 additions & 0 deletions maps-compose/src/debug/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2026 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application>
<activity
android:name="androidx.activity.ComponentActivity"
android:exported="true" />
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.maps.android.compose

import android.app.Activity
import android.os.Bundle
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.MapView
import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.LatLng
import com.google.android.maps.robolectric.annotation.EnableMapsShadows
import com.google.android.maps.robolectric.shadows.ShadowGoogleMap
import com.google.android.maps.robolectric.truth.LatLngSubject
import com.google.common.truth.Truth.assertThat
import java.util.concurrent.atomic.AtomicReference
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.robolectric.shadow.api.Shadow
import org.robolectric.shadows.ShadowLooper

// [START maps_compose_camera_position_state_test]
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [34])
@EnableMapsShadows
internal class CameraPositionStateTest {

private lateinit var googleMap: GoogleMap
private lateinit var shadowMap: ShadowGoogleMap

private val sydney: LatLng = LatLng(-33.852, 151.211)
private val tokyo: LatLng = LatLng(35.6762, 139.6503)

@Before
internal fun setUp(): Unit {
val activity = Robolectric.buildActivity(Activity::class.java).create().get()
val mapView = MapView(activity)
mapView.onCreate(Bundle())

val mapRef = AtomicReference<GoogleMap>()
mapView.getMapAsync { mapRef.set(it) }
ShadowLooper.idleMainLooper()

googleMap = mapRef.get()
shadowMap = Shadow.extract(googleMap) as ShadowGoogleMap
}

@Test
internal fun testInitialPosition_withoutMapBound(): Unit {
val state = CameraPositionState(position = CameraPosition.fromLatLngZoom(sydney, 10f))
assertThat(state.position.target).isEqualTo(sydney)
assertThat(state.position.zoom).isEqualTo(10f)
assertThat(state.isMoving).isFalse()
assertThat(state.projection).isNull()
}

@Test
internal fun testSetMap_syncsPositionFromStateToMap(): Unit {
val initialPosition = CameraPosition.fromLatLngZoom(sydney, 12f)
val state = CameraPositionState(position = initialPosition)

state.setMap(googleMap)

assertThat(googleMap.cameraPosition.target).isEqualTo(sydney)
assertThat(googleMap.cameraPosition.zoom).isEqualTo(12f)
assertThat(state.projection).isNotNull()
}

private fun bindState(state: CameraPositionState) {
state.setMap(googleMap)
googleMap.setOnCameraMoveStartedListener { reason ->
state.cameraMoveStartedReason = CameraMoveStartedReason.fromInt(reason)
state.isMoving = true
}
googleMap.setOnCameraMoveListener {
state.rawPosition = googleMap.cameraPosition
}
googleMap.setOnCameraIdleListener {
state.isMoving = false
state.rawPosition = googleMap.cameraPosition
}
}

@Test
internal fun testMove_instantaneouslyUpdatesCameraPosition(): Unit {
val state = CameraPositionState()
bindState(state)

state.move(CameraUpdateFactory.newLatLngZoom(tokyo, 14f))

assertThat(googleMap.cameraPosition.target).isEqualTo(tokyo)
LatLngSubject.assertThat(state.position.target)
.isWithin(1.0)
.of(tokyo)
assertThat(state.position.zoom).isEqualTo(14f)
assertThat(state.isMoving).isFalse()
}

@Test
internal fun testAnimate_updatesCameraPosition(): Unit = runTest {
val state = CameraPositionState()
bindState(state)

state.animate(CameraUpdateFactory.newLatLngZoom(tokyo, 15f), durationMs = 1000)

assertThat(googleMap.cameraPosition.target).isEqualTo(tokyo)
LatLngSubject.assertThat(state.position.target)
.isWithin(1.0)
.of(tokyo)
assertThat(state.position.zoom).isEqualTo(15f)
assertThat(state.isMoving).isFalse()
}

@Test
internal fun testProjection_convertsCoordinates(): Unit {
val state = CameraPositionState()
state.setMap(googleMap)

state.move(CameraUpdateFactory.newLatLngZoom(sydney, 10f))

val projection = state.projection
assertThat(projection).isNotNull()
val screenPoint = projection?.toScreenLocation(sydney)
assertThat(screenPoint).isNotNull()
val roundTripLatLng = projection?.fromScreenLocation(screenPoint!!)
LatLngSubject.assertThat(roundTripLatLng)
.isWithin(1.0)
.of(sydney)
}
}
// [END maps_compose_camera_position_state_test]
Loading