11package 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
310import androidx.compose.ui.test.junit4.createComposeRule
11+ import androidx.compose.ui.test.onNodeWithTag
412import androidx.compose.ui.test.onRoot
513import androidx.compose.ui.test.performTouchInput
614import 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}
0 commit comments