Skip to content

Commit a8bc4f2

Browse files
committed
chore: test for scrolling
1 parent 2a15a68 commit a8bc4f2

File tree

2 files changed

+59
-12
lines changed

2 files changed

+59
-12
lines changed

maps-app/src/androidTest/java/com/google/maps/android/compose/MapsInLazyColumnTest.kt

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
package com.google.maps.android.compose
22

3+
import androidx.compose.foundation.lazy.rememberLazyListState
4+
import androidx.compose.runtime.LaunchedEffect
5+
import androidx.compose.runtime.derivedStateOf
6+
import androidx.compose.runtime.getValue
7+
import androidx.compose.runtime.mutableStateOf
8+
import androidx.compose.runtime.remember
9+
import androidx.compose.ui.test.assertIsDisplayed
310
import androidx.compose.ui.test.junit4.createComposeRule
11+
import androidx.compose.ui.test.onNodeWithTag
412
import androidx.compose.ui.test.onRoot
513
import androidx.compose.ui.test.performTouchInput
614
import androidx.compose.ui.test.swipeUp
@@ -18,23 +26,53 @@ class MapsInLazyColumnTests {
1826
val composeTestRule = createComposeRule()
1927

2028
private val mapItems = listOf(
21-
MapListItem(id = "1", location = LatLng(1.23, 4.56), zoom = 10f, title = "A"),
22-
MapListItem(id = "2", location = LatLng(7.89, 0.12), zoom = 12f, title = "B"),
29+
MapListItem(id = "1", location = LatLng(1.23, 4.56), zoom = 10f, title = "Item 1"),
30+
MapListItem(id = "2", location = LatLng(7.89, 0.12), zoom = 12f, title = "Item 2"),
31+
MapListItem(id = "3", location = LatLng(3.45, 6.78), zoom = 11f, title = "Item 3"),
32+
MapListItem(id = "4", location = LatLng(9.01, 2.34), zoom = 13f, title = "Item 4"),
33+
MapListItem(id = "5", location = LatLng(5.67, 8.90), zoom = 9f, title = "Item 5"),
34+
MapListItem(id = "6", location = LatLng(4.32, 7.65), zoom = 14f, title = "Item 6"),
35+
MapListItem(id = "7", location = LatLng(8.76, 1.23), zoom = 10f, title = "Item 7"),
36+
MapListItem(id = "8", location = LatLng(2.98, 6.54), zoom = 12f, title = "Item 8"),
37+
MapListItem(id = "9", location = LatLng(7.65, 3.21), zoom = 11f, title = "Item 9"),
38+
MapListItem(id = "10", location = LatLng(0.12, 9.87), zoom = 13f, title = "Item 10"),
2339
)
2440

41+
2542
private lateinit var cameraPositionStates: Map<MapItemId, CameraPositionState>
2643

2744
private fun initMaps() {
2845
check(hasValidApiKey) { "Maps API key not specified" }
29-
val countDownLatch = CountDownLatch(mapItems.size)
46+
3047
composeTestRule.setContent {
31-
MapsInLazyColumn(mapItems, onMapLoaded = {
32-
countDownLatch.countDown()
33-
})
34-
}
48+
val lazyListState = rememberLazyListState()
49+
val visibleMapCount = remember { mutableStateOf(0) }
50+
51+
val visibleItems by remember {
52+
derivedStateOf {
53+
lazyListState.layoutInfo.visibleItemsInfo.size
54+
}
55+
}
56+
57+
LaunchedEffect(visibleItems) {
58+
visibleMapCount.value = visibleItems
59+
}
60+
61+
val countDownLatch = CountDownLatch(visibleMapCount.value)
62+
63+
MapsInLazyColumn(
64+
mapItems,
65+
lazyListState = lazyListState,
66+
onMapLoaded = {
67+
countDownLatch.countDown()
68+
}
69+
)
3570

36-
val mapsLoaded = countDownLatch.await(30, TimeUnit.SECONDS)
37-
assertTrue("Maps loaded", mapsLoaded)
71+
LaunchedEffect(Unit) {
72+
val mapsLoaded = countDownLatch.await(30, TimeUnit.SECONDS)
73+
assertTrue("Visible maps loaded", mapsLoaded)
74+
}
75+
}
3876
}
3977

4078
@Before
@@ -64,4 +102,12 @@ class MapsInLazyColumnTests {
64102
item.location.assertEquals(cameraPositionStates[item.id]?.position?.target!!)
65103
}
66104
}
105+
106+
@Test
107+
fun testScrollToBottom() {
108+
initMaps()
109+
composeTestRule.onRoot().performTouchInput { swipeUp(durationMillis = 1000) }
110+
composeTestRule.waitForIdle()
111+
composeTestRule.onNodeWithTag("Item 5").assertIsDisplayed()
112+
}
67113
}

maps-app/src/main/java/com/google/maps/android/compose/MapsInLazyColumnActivity.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import androidx.compose.foundation.layout.height
2020
import androidx.compose.foundation.layout.padding
2121
import androidx.compose.foundation.layout.systemBarsPadding
2222
import androidx.compose.foundation.lazy.LazyColumn
23+
import androidx.compose.foundation.lazy.LazyListState
2324
import androidx.compose.foundation.lazy.items
2425
import androidx.compose.foundation.lazy.rememberLazyListState
2526
import androidx.compose.foundation.rememberScrollState
@@ -41,7 +42,6 @@ import androidx.compose.ui.Alignment
4142
import androidx.compose.ui.Modifier
4243
import androidx.compose.ui.graphics.Color
4344
import androidx.compose.ui.platform.testTag
44-
import androidx.compose.ui.text.TextStyle
4545
import androidx.compose.ui.text.font.FontWeight
4646
import androidx.compose.ui.unit.dp
4747
import androidx.compose.ui.unit.sp
@@ -142,12 +142,13 @@ class MapsInLazyColumnActivity : ComponentActivity() {
142142
@Composable
143143
fun MapsInLazyColumn(
144144
mapItems: List<MapListItem>,
145+
lazyListState: LazyListState = rememberLazyListState(),
145146
onMapLoaded: () -> Unit
146147
) {
147148

148149
var isMapLoaded by remember { mutableStateOf(false) }
149150

150-
val lazyListState = rememberLazyListState()
151+
val lazyListState = lazyListState
151152

152153
val cameraPositionStates = mapItems.associate { item ->
153154
item.id to rememberCameraPositionState(
@@ -266,7 +267,7 @@ private fun MapCard(
266267
@Composable
267268
fun TextWithBackground(text: String, fontWeight: FontWeight = FontWeight.Medium) {
268269
Text(
269-
modifier = Modifier.background(Color.White.copy(0.7f)),
270+
modifier = Modifier.background(Color.White.copy(0.7f)).testTag(text),
270271
text = text,
271272
fontWeight = fontWeight,
272273
fontSize = 10.sp

0 commit comments

Comments
 (0)