jnichol | 6264665 | 2021-11-02 12:09:14 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package androidx.wear.compose.material |
| 18 | |
jnichol | 50c6882 | 2022-01-17 15:08:52 +0000 | [diff] [blame] | 19 | import androidx.compose.foundation.layout.Arrangement |
jnichol | 6264665 | 2021-11-02 12:09:14 +0000 | [diff] [blame] | 20 | import androidx.compose.foundation.layout.Spacer |
| 21 | import androidx.compose.foundation.layout.height |
| 22 | import androidx.compose.foundation.layout.requiredHeight |
| 23 | import androidx.compose.foundation.layout.width |
| 24 | import androidx.compose.foundation.text.BasicText |
| 25 | import androidx.compose.ui.Modifier |
| 26 | import androidx.compose.ui.platform.testTag |
| 27 | import androidx.compose.ui.test.assertIsDisplayed |
| 28 | import androidx.compose.ui.test.assertIsNotDisplayed |
| 29 | import androidx.compose.ui.test.assertTopPositionInRootIsEqualTo |
| 30 | import androidx.compose.ui.test.junit4.createComposeRule |
| 31 | import androidx.compose.ui.test.onNodeWithTag |
| 32 | import androidx.compose.ui.test.onNodeWithText |
| 33 | import androidx.compose.ui.unit.dp |
| 34 | import org.junit.Rule |
| 35 | import org.junit.Test |
| 36 | |
| 37 | class ScalingLazyColumnIndexedTest { |
| 38 | |
| 39 | @get:Rule |
| 40 | val rule = createComposeRule() |
| 41 | |
| 42 | @Test |
| 43 | fun scalingLazyColumnShowsIndexedItems() { |
jnichol | fd4289f | 2022-01-25 12:36:59 +0000 | [diff] [blame] | 44 | lateinit var state: ScalingLazyListState |
jnichol | 6264665 | 2021-11-02 12:09:14 +0000 | [diff] [blame] | 45 | val items = (1..4).map { it.toString() } |
| 46 | |
| 47 | rule.setContent { |
| 48 | ScalingLazyColumn( |
jnichol | fd4289f | 2022-01-25 12:36:59 +0000 | [diff] [blame] | 49 | state = rememberScalingLazyListState().also { state = it }, |
jnichol | 6264665 | 2021-11-02 12:09:14 +0000 | [diff] [blame] | 50 | 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 | |
jnichol | fd4289f | 2022-01-25 12:36:59 +0000 | [diff] [blame] | 62 | // TODO(b/210654937): Remove the waitUntil once we no longer need 2 stage initialization |
| 63 | rule.waitUntil { state.initialized.value } |
jnichol | 6264665 | 2021-11-02 12:09:14 +0000 | [diff] [blame] | 64 | // 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() { |
jnichol | fd4289f | 2022-01-25 12:36:59 +0000 | [diff] [blame] | 83 | lateinit var state: ScalingLazyListState |
jnichol | 6264665 | 2021-11-02 12:09:14 +0000 | [diff] [blame] | 84 | val items = (0..1).map { it.toString() } |
| 85 | |
| 86 | rule.setContent { |
jnichol | fd4289f | 2022-01-25 12:36:59 +0000 | [diff] [blame] | 87 | ScalingLazyColumn( |
| 88 | state = rememberScalingLazyListState().also { state = it }, |
| 89 | modifier = Modifier.height(200.dp), |
| 90 | autoCentering = false |
| 91 | ) { |
jnichol | 6264665 | 2021-11-02 12:09:14 +0000 | [diff] [blame] | 92 | itemsIndexed(items) { index, item -> |
| 93 | BasicText( |
| 94 | "${index}x$item", Modifier.requiredHeight(100.dp) |
| 95 | ) |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
jnichol | fd4289f | 2022-01-25 12:36:59 +0000 | [diff] [blame] | 100 | // TODO(b/210654937): Remove the waitUntil once we no longer need 2 stage initialization |
| 101 | rule.waitUntil { state.initialized.value } |
jnichol | 6264665 | 2021-11-02 12:09:14 +0000 | [diff] [blame] | 102 | rule.onNodeWithText("0x0") |
| 103 | .assertTopPositionInRootIsEqualTo(0.dp) |
| 104 | |
| 105 | rule.onNodeWithText("1x1") |
| 106 | .assertTopPositionInRootIsEqualTo(104.dp) |
| 107 | } |
jnichol | 50c6882 | 2022-01-17 15:08:52 +0000 | [diff] [blame] | 108 | |
| 109 | @Test |
| 110 | fun columnWithIndexesComposedWithCorrectIndexAndItemWithAutoCentering() { |
jnichol | fd4289f | 2022-01-25 12:36:59 +0000 | [diff] [blame] | 111 | lateinit var state: ScalingLazyListState |
jnichol | 50c6882 | 2022-01-17 15:08:52 +0000 | [diff] [blame] | 112 | 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( |
jnichol | fd4289f | 2022-01-25 12:36:59 +0000 | [diff] [blame] | 118 | state = rememberScalingLazyListState().also { state = it }, |
jnichol | 50c6882 | 2022-01-17 15:08:52 +0000 | [diff] [blame] | 119 | 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 | |
jnichol | fd4289f | 2022-01-25 12:36:59 +0000 | [diff] [blame] | 131 | // TODO(b/210654937): Remove the waitUntil once we no longer need 2 stage initialization |
| 132 | rule.waitUntil { state.initialized.value } |
jnichol | 50c6882 | 2022-01-17 15:08:52 +0000 | [diff] [blame] | 133 | // 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 | } |
jnichol | 6264665 | 2021-11-02 12:09:14 +0000 | [diff] [blame] | 142 | } |