blob: aa721fdaa71cd24e0c39c7082cbbb7641af151b7 [file] [log] [blame]
jnichol62646652021-11-02 12:09:14 +00001/*
2 * Copyright 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package androidx.wear.compose.material
18
jnichol50c68822022-01-17 15:08:52 +000019import androidx.compose.foundation.layout.Arrangement
jnichol62646652021-11-02 12:09:14 +000020import androidx.compose.foundation.layout.Spacer
21import androidx.compose.foundation.layout.height
22import androidx.compose.foundation.layout.requiredHeight
23import androidx.compose.foundation.layout.width
24import androidx.compose.foundation.text.BasicText
25import androidx.compose.ui.Modifier
26import androidx.compose.ui.platform.testTag
27import androidx.compose.ui.test.assertIsDisplayed
28import androidx.compose.ui.test.assertIsNotDisplayed
29import androidx.compose.ui.test.assertTopPositionInRootIsEqualTo
30import androidx.compose.ui.test.junit4.createComposeRule
31import androidx.compose.ui.test.onNodeWithTag
32import androidx.compose.ui.test.onNodeWithText
33import androidx.compose.ui.unit.dp
34import org.junit.Rule
35import org.junit.Test
36
37class ScalingLazyColumnIndexedTest {
38
39 @get:Rule
40 val rule = createComposeRule()
41
42 @Test
43 fun scalingLazyColumnShowsIndexedItems() {
jnicholfd4289f2022-01-25 12:36:59 +000044 lateinit var state: ScalingLazyListState
jnichol62646652021-11-02 12:09:14 +000045 val items = (1..4).map { it.toString() }
46
47 rule.setContent {
48 ScalingLazyColumn(
jnicholfd4289f2022-01-25 12:36:59 +000049 state = rememberScalingLazyListState().also { state = it },
jnichol62646652021-11-02 12:09:14 +000050 modifier = Modifier.height(200.dp),
51 scalingParams = ScalingLazyColumnDefaults.scalingParams(edgeScale = 1.0f)
52 ) {
53 itemsIndexed(items) { index, item ->
54 Spacer(
55 Modifier.height(101.dp).width(100.dp)
56 .testTag("$index-$item")
57 )
58 }
59 }
60 }
61
jnicholfd4289f2022-01-25 12:36:59 +000062 // TODO(b/210654937): Remove the waitUntil once we no longer need 2 stage initialization
63 rule.waitUntil { state.initialized.value }
jnichol62646652021-11-02 12:09:14 +000064 // Fully visible
65 rule.onNodeWithTag("0-1")
66 .assertIsDisplayed()
67
68 // Partially visible
69 rule.onNodeWithTag("1-2")
70 .assertIsDisplayed()
71
72 // Will have been composed but should not be visible
73 rule.onNodeWithTag("2-3")
74 .assertIsNotDisplayed()
75
76 // Should not have been composed
77 rule.onNodeWithTag("3-4")
78 .assertDoesNotExist()
79 }
80
81 @Test
82 fun columnWithIndexesComposedWithCorrectIndexAndItem() {
jnicholfd4289f2022-01-25 12:36:59 +000083 lateinit var state: ScalingLazyListState
jnichol62646652021-11-02 12:09:14 +000084 val items = (0..1).map { it.toString() }
85
86 rule.setContent {
jnicholfd4289f2022-01-25 12:36:59 +000087 ScalingLazyColumn(
88 state = rememberScalingLazyListState().also { state = it },
89 modifier = Modifier.height(200.dp),
90 autoCentering = false
91 ) {
jnichol62646652021-11-02 12:09:14 +000092 itemsIndexed(items) { index, item ->
93 BasicText(
94 "${index}x$item", Modifier.requiredHeight(100.dp)
95 )
96 }
97 }
98 }
99
jnicholfd4289f2022-01-25 12:36:59 +0000100 // TODO(b/210654937): Remove the waitUntil once we no longer need 2 stage initialization
101 rule.waitUntil { state.initialized.value }
jnichol62646652021-11-02 12:09:14 +0000102 rule.onNodeWithText("0x0")
103 .assertTopPositionInRootIsEqualTo(0.dp)
104
105 rule.onNodeWithText("1x1")
106 .assertTopPositionInRootIsEqualTo(104.dp)
107 }
jnichol50c68822022-01-17 15:08:52 +0000108
109 @Test
110 fun columnWithIndexesComposedWithCorrectIndexAndItemWithAutoCentering() {
jnicholfd4289f2022-01-25 12:36:59 +0000111 lateinit var state: ScalingLazyListState
jnichol50c68822022-01-17 15:08:52 +0000112 val items = (0..1).map { it.toString() }
113 val viewPortHeight = 200.dp
114 val itemHeight = 100.dp
115 val gapBetweenItems = 4.dp
116 rule.setContent {
117 ScalingLazyColumn(
jnicholfd4289f2022-01-25 12:36:59 +0000118 state = rememberScalingLazyListState().also { state = it },
jnichol50c68822022-01-17 15:08:52 +0000119 modifier = Modifier.height(viewPortHeight),
120 autoCentering = true,
121 verticalArrangement = Arrangement.spacedBy(gapBetweenItems)
122 ) {
123 itemsIndexed(items) { index, item ->
124 BasicText(
125 "${index}x$item", Modifier.requiredHeight(itemHeight)
126 )
127 }
128 }
129 }
130
jnicholfd4289f2022-01-25 12:36:59 +0000131 // TODO(b/210654937): Remove the waitUntil once we no longer need 2 stage initialization
132 rule.waitUntil { state.initialized.value }
jnichol50c68822022-01-17 15:08:52 +0000133 // Check that first item is in the center of the viewport
134 val firstItemStart = viewPortHeight / 2f - itemHeight / 2f
135 rule.onNodeWithText("0x0")
136 .assertTopPositionInRootIsEqualTo(firstItemStart)
137
138 // And that the second item is item height + gap between items below it
139 rule.onNodeWithText("1x1")
140 .assertTopPositionInRootIsEqualTo(firstItemStart + itemHeight + gapBetweenItems)
141 }
jnichol62646652021-11-02 12:09:14 +0000142}