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
@@ -0,0 +1,80 @@
/*
* 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 androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.assertIsFocused
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onAllNodesWithTag
import androidx.compose.ui.test.performKeyInput
import androidx.compose.ui.test.pressKey
import androidx.compose.ui.test.requestFocus
import com.google.android.gms.maps.model.LatLng
import org.junit.Rule
import org.junit.Test

/**
* Verifies keyboard focus traversal behavior for [GoogleMap] when integrated
* inside Compose layouts, ensuring the map behaves as a single focus stop.
*/
class GoogleMapFocusTraversalTests {
@get:Rule
val composeTestRule = createComposeRule()

private val mapItems = listOf(
MapListItem(id = "1", location = LatLng(1.23, 4.56), zoom = 10f, title = "Item 1"),
MapListItem(id = "2", location = LatLng(7.89, 0.12), zoom = 12f, title = "Item 2"),
MapListItem(id = "3", location = LatLng(3.45, 6.78), zoom = 11f, title = "Item 3"),
)

private fun initMaps() {
check(hasValidApiKey) { "Maps API key not specified" }

composeTestRule.setContent {
MapsInLazyColumn(
mapItems,
lazyListState = rememberLazyListState(),
onMapLoaded = {},
)
}

composeTestRule.waitUntil(timeoutMillis = 5_000) {
composeTestRule
.onAllNodesWithTag("Map", useUnmergedTree = true)
.fetchSemanticsNodes()
.size >= 2
}
}

@OptIn(ExperimentalTestApi::class)
@Test
fun tabFromMapFocusesNextMap() {
initMaps()

val visibleMaps = composeTestRule.onAllNodesWithTag("Map", useUnmergedTree = true)
visibleMaps[0].requestFocus()
visibleMaps[0].assertIsFocused()

visibleMaps[0].performKeyInput {
pressKey(Key.Tab)
}

visibleMaps[1].assertIsFocused()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import android.content.res.Configuration
import android.location.Location
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.focusable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -150,11 +152,20 @@ public fun GoogleMap(
val parentCompositionScope = rememberCoroutineScope()

AndroidView(
modifier = modifier,
// Make the AndroidView wrapper focusable in Compose so the Compose focus
// system can target it during tab traversal.
modifier = modifier.focusable(),
factory = { context ->
val options = googleMapOptionsFactory()
cameraPositionState.isLiteMode = options.liteMode == true
mapViewFactory(context, options).also { mapView ->
// Treat the MapView as a single focus stop. FOCUS_BEFORE_DESCENDANTS
// ensures the map container gets focused first, and prevents the keyboard
// focus from tabbing through all internal map elements (like zoom buttons
// or the Google logo) by default.
mapView.isFocusable = true
mapView.descendantFocusability = ViewGroup.FOCUS_BEFORE_DESCENDANTS

val componentCallbacks = object : ComponentCallbacks2 {
override fun onConfigurationChanged(newConfig: Configuration) {}

Expand Down