Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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.ui.material |
| 18 | |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 19 | import androidx.test.filters.SmallTest |
Louis Pullen-Freilich | ca6eca2 | 2020-07-20 00:31:45 +0100 | [diff] [blame] | 20 | import androidx.compose.foundation.text.FirstBaseline |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 21 | import androidx.ui.core.LayoutCoordinates |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 22 | import androidx.ui.core.Modifier |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 23 | import androidx.ui.core.Ref |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 24 | import androidx.ui.core.onPositioned |
Louis Pullen-Freilich | ddda7be | 2020-07-17 18:28:12 +0100 | [diff] [blame] | 25 | import androidx.compose.foundation.Box |
| 26 | import androidx.compose.foundation.Image |
| 27 | import androidx.compose.foundation.Text |
Louis Pullen-Freilich | f434a13e | 2020-07-22 14:19:24 +0100 | [diff] [blame] | 28 | import androidx.compose.ui.geometry.Offset |
Louis Pullen-Freilich | 4dc4dac | 2020-07-22 14:39:14 +0100 | [diff] [blame] | 29 | import androidx.compose.ui.graphics.ImageAsset |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 30 | import androidx.ui.test.assertHeightIsEqualTo |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 31 | import androidx.ui.test.createComposeRule |
Louis Pullen-Freilich | a7eeb10 | 2020-07-22 17:54:24 +0100 | [diff] [blame] | 32 | import androidx.compose.ui.unit.Dp |
| 33 | import androidx.compose.ui.unit.IntSize |
| 34 | import androidx.compose.ui.unit.dp |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 35 | import com.google.common.truth.Truth.assertThat |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 36 | import org.junit.Rule |
| 37 | import org.junit.Test |
| 38 | import org.junit.runner.RunWith |
| 39 | import org.junit.runners.JUnit4 |
| 40 | import kotlin.math.roundToInt |
| 41 | |
| 42 | @SmallTest |
| 43 | @RunWith(JUnit4::class) |
| 44 | class ListItemTest { |
| 45 | |
| 46 | @get:Rule |
| 47 | val composeTestRule = createComposeRule() |
| 48 | |
Nader Jawad | 6ec796a | 2020-02-28 13:40:11 -0800 | [diff] [blame] | 49 | val icon24x24 by lazy { ImageAsset(width = 24.dp.toIntPx(), height = 24.dp.toIntPx()) } |
| 50 | val icon40x40 by lazy { ImageAsset(width = 40.dp.toIntPx(), height = 40.dp.toIntPx()) } |
| 51 | val icon56x56 by lazy { ImageAsset(width = 56.dp.toIntPx(), height = 56.dp.toIntPx()) } |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 52 | |
| 53 | @Test |
| 54 | fun listItem_oneLine_size() { |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 55 | val expectedHeightNoIcon = 48.dp |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 56 | composeTestRule |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 57 | .setMaterialContentForSizeAssertions { |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 58 | ListItem(text = "Primary text") |
| 59 | } |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 60 | .assertHeightIsEqualTo(expectedHeightNoIcon) |
| 61 | .assertWidthFillsRoot() |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Test |
| 65 | fun listItem_oneLine_withIcon24_size() { |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 66 | val expectedHeightSmallIcon = 56.dp |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 67 | composeTestRule |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 68 | .setMaterialContentForSizeAssertions { |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 69 | ListItem(text = "Primary text", icon = icon24x24) |
| 70 | } |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 71 | .assertHeightIsEqualTo(expectedHeightSmallIcon) |
| 72 | .assertWidthFillsRoot() |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | @Test |
| 76 | fun listItem_oneLine_withIcon56_size() { |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 77 | val expectedHeightLargeIcon = 72.dp |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 78 | composeTestRule |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 79 | .setMaterialContentForSizeAssertions { |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 80 | ListItem(text = "Primary text", icon = icon56x56) |
| 81 | } |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 82 | .assertHeightIsEqualTo(expectedHeightLargeIcon) |
| 83 | .assertWidthFillsRoot() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | @Test |
| 87 | fun listItem_twoLine_size() { |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 88 | val expectedHeightNoIcon = 64.dp |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 89 | composeTestRule |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 90 | .setMaterialContentForSizeAssertions { |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 91 | ListItem(text = "Primary text", secondaryText = "Secondary text") |
| 92 | } |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 93 | .assertHeightIsEqualTo(expectedHeightNoIcon) |
| 94 | .assertWidthFillsRoot() |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Test |
| 98 | fun listItem_twoLine_withIcon_size() { |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 99 | val expectedHeightWithIcon = 72.dp |
| 100 | |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 101 | composeTestRule |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 102 | .setMaterialContentForSizeAssertions { |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 103 | ListItem( |
| 104 | text = "Primary text", |
| 105 | secondaryText = "Secondary text", |
| 106 | icon = icon24x24 |
| 107 | ) |
| 108 | } |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 109 | .assertHeightIsEqualTo(expectedHeightWithIcon) |
| 110 | .assertWidthFillsRoot() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Test |
| 114 | fun listItem_threeLine_size() { |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 115 | val expectedHeight = 88.dp |
| 116 | composeTestRule |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 117 | .setMaterialContentForSizeAssertions { |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 118 | ListItem( |
| 119 | overlineText = "OVERLINE", |
| 120 | text = "Primary text", |
| 121 | secondaryText = "Secondary text" |
| 122 | ) |
| 123 | } |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 124 | .assertHeightIsEqualTo(expectedHeight) |
| 125 | .assertWidthFillsRoot() |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Test |
| 129 | fun listItem_threeLine_noSingleLine_size() { |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 130 | val expectedHeight = 88.dp |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 131 | composeTestRule |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 132 | .setMaterialContentForSizeAssertions { |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 133 | ListItem( |
| 134 | text = "Primary text", |
| 135 | secondaryText = "Secondary text with long text", |
| 136 | singleLineSecondaryText = false |
| 137 | ) |
| 138 | } |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 139 | .assertHeightIsEqualTo(expectedHeight) |
| 140 | .assertWidthFillsRoot() |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Test |
| 144 | fun listItem_threeLine_metaText_size() { |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 145 | val expectedHeight = 88.dp |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 146 | composeTestRule |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 147 | .setMaterialContentForSizeAssertions { |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 148 | ListItem( |
| 149 | overlineText = "OVERLINE", |
| 150 | text = "Primary text", |
| 151 | secondaryText = "Secondary text", |
| 152 | metaText = "meta" |
| 153 | ) |
| 154 | } |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 155 | .assertHeightIsEqualTo(expectedHeight) |
| 156 | .assertWidthFillsRoot() |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Test |
| 160 | fun listItem_threeLine_noSingleLine_metaText_size() { |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 161 | val expectedHeight = 88.dp |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 162 | composeTestRule |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 163 | .setMaterialContentForSizeAssertions { |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 164 | ListItem( |
| 165 | text = "Primary text", |
| 166 | secondaryText = "Secondary text with long text", |
| 167 | singleLineSecondaryText = false, |
| 168 | metaText = "meta" |
| 169 | ) |
| 170 | } |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 171 | .assertHeightIsEqualTo(expectedHeight) |
| 172 | .assertWidthFillsRoot() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Test |
| 176 | fun listItem_oneLine_positioning_noIcon() { |
| 177 | val listItemHeight = 48.dp |
| 178 | val expectedLeftPadding = 16.dp |
| 179 | val expectedRightPadding = 16.dp |
| 180 | |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 181 | val textPosition = Ref<Offset>() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 182 | val textSize = Ref<IntSize>() |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 183 | val trailingPosition = Ref<Offset>() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 184 | val trailingSize = Ref<IntSize>() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 185 | composeTestRule.setMaterialContent { |
Matvei Malkov | 201726d | 2020-03-13 14:03:54 +0000 | [diff] [blame] | 186 | Box { |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 187 | ListItem( |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 188 | text = { Text("Primary text", saveLayout(textPosition, textSize)) }, |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 189 | trailing = { |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 190 | Image(icon24x24, saveLayout(trailingPosition, trailingSize)) |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 191 | } |
| 192 | ) |
| 193 | } |
| 194 | } |
Filip Pavlis | a4d73a5 | 2020-07-14 11:30:00 +0100 | [diff] [blame] | 195 | composeTestRule.runOnIdleWithDensity { |
Nader Jawad | 63153c1 | 2020-05-21 14:17:08 -0700 | [diff] [blame] | 196 | assertThat(textPosition.value!!.x).isEqualTo(expectedLeftPadding.toIntPx() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 197 | .toFloat()) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 198 | assertThat(textPosition.value!!.y).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 199 | ((listItemHeight.toIntPx() - textSize.value!!.height) / 2f).roundToInt().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 200 | ) |
| 201 | val dm = composeTestRule.displayMetrics |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 202 | assertThat(trailingPosition.value!!.x).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 203 | dm.widthPixels - trailingSize.value!!.width - |
| 204 | expectedRightPadding.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 205 | ) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 206 | assertThat(trailingPosition.value!!.y).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 207 | ((listItemHeight.toIntPx() - trailingSize.value!!.height) / 2f).roundToInt() |
| 208 | .toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 209 | ) |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | @Test |
| 214 | fun listItem_oneLine_positioning_withIcon() { |
| 215 | val listItemHeight = 56.dp |
| 216 | val expectedLeftPadding = 16.dp |
| 217 | val expectedTextLeftPadding = 32.dp |
| 218 | |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 219 | val textPosition = Ref<Offset>() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 220 | val textSize = Ref<IntSize>() |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 221 | val iconPosition = Ref<Offset>() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 222 | val iconSize = Ref<IntSize>() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 223 | composeTestRule.setMaterialContent { |
Matvei Malkov | 201726d | 2020-03-13 14:03:54 +0000 | [diff] [blame] | 224 | Box { |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 225 | ListItem( |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 226 | text = { Text("Primary text", saveLayout(textPosition, textSize)) }, |
| 227 | icon = { Image(icon24x24, saveLayout(iconPosition, iconSize)) } |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 228 | ) |
| 229 | } |
| 230 | } |
Filip Pavlis | a4d73a5 | 2020-07-14 11:30:00 +0100 | [diff] [blame] | 231 | composeTestRule.runOnIdleWithDensity { |
Nader Jawad | 63153c1 | 2020-05-21 14:17:08 -0700 | [diff] [blame] | 232 | assertThat(iconPosition.value!!.x).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 233 | expectedLeftPadding.toIntPx().toFloat() |
Nader Jawad | 63153c1 | 2020-05-21 14:17:08 -0700 | [diff] [blame] | 234 | ) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 235 | assertThat(iconPosition.value!!.y).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 236 | ((listItemHeight.toIntPx() - iconSize.value!!.height) / 2f).roundToInt().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 237 | ) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 238 | assertThat(textPosition.value!!.x).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 239 | expectedLeftPadding.toIntPx().toFloat() + |
| 240 | iconSize.value!!.width + |
| 241 | expectedTextLeftPadding.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 242 | ) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 243 | assertThat(textPosition.value!!.y).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 244 | ((listItemHeight.toIntPx() - textSize.value!!.height) / 2f).roundToInt().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 245 | ) |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | @Test |
| 250 | fun listItem_twoLine_positioning_noIcon() { |
| 251 | val expectedLeftPadding = 16.dp |
| 252 | val expectedRightPadding = 16.dp |
| 253 | val expectedTextBaseline = 28.dp |
| 254 | val expectedSecondaryTextBaselineOffset = 20.dp |
| 255 | |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 256 | val textPosition = Ref<Offset>() |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 257 | val textBaseline = Ref<Float>() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 258 | val textSize = Ref<IntSize>() |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 259 | val secondaryTextPosition = Ref<Offset>() |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 260 | val secondaryTextBaseline = Ref<Float>() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 261 | val secondaryTextSize = Ref<IntSize>() |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 262 | val trailingPosition = Ref<Offset>() |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 263 | val trailingBaseline = Ref<Float>() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 264 | val trailingSize = Ref<IntSize>() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 265 | composeTestRule.setMaterialContent { |
Matvei Malkov | 201726d | 2020-03-13 14:03:54 +0000 | [diff] [blame] | 266 | Box { |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 267 | ListItem( |
| 268 | text = { |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 269 | Text("Primary text", saveLayout(textPosition, textSize, textBaseline)) |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 270 | }, |
| 271 | secondaryText = { |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 272 | Text( |
| 273 | "Secondary text", |
| 274 | saveLayout( |
| 275 | secondaryTextPosition, |
| 276 | secondaryTextSize, |
| 277 | secondaryTextBaseline |
| 278 | ) |
| 279 | ) |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 280 | }, |
| 281 | trailing = { |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 282 | Text("meta", saveLayout(trailingPosition, trailingSize, trailingBaseline)) |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 283 | } |
| 284 | ) |
| 285 | } |
| 286 | } |
Filip Pavlis | a4d73a5 | 2020-07-14 11:30:00 +0100 | [diff] [blame] | 287 | composeTestRule.runOnIdleWithDensity { |
Nader Jawad | 63153c1 | 2020-05-21 14:17:08 -0700 | [diff] [blame] | 288 | assertThat(textPosition.value!!.x).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 289 | expectedLeftPadding.toIntPx().toFloat() |
Nader Jawad | 63153c1 | 2020-05-21 14:17:08 -0700 | [diff] [blame] | 290 | ) |
| 291 | assertThat(textBaseline.value!!).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 292 | expectedTextBaseline.toIntPx().toFloat() |
Nader Jawad | 63153c1 | 2020-05-21 14:17:08 -0700 | [diff] [blame] | 293 | ) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 294 | assertThat(secondaryTextPosition.value!!.x).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 295 | expectedLeftPadding.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 296 | ) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 297 | assertThat(secondaryTextBaseline.value!!).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 298 | expectedTextBaseline.toIntPx().toFloat() + |
| 299 | expectedSecondaryTextBaselineOffset.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 300 | ) |
| 301 | val dm = composeTestRule.displayMetrics |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 302 | assertThat(trailingPosition.value!!.x).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 303 | dm.widthPixels - trailingSize.value!!.width - |
| 304 | expectedRightPadding.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 305 | ) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 306 | assertThat(trailingBaseline.value!!).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 307 | expectedTextBaseline.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 308 | ) |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | @Test |
| 313 | fun listItem_twoLine_positioning_withSmallIcon() { |
| 314 | val expectedLeftPadding = 16.dp |
| 315 | val expectedIconTopPadding = 16.dp |
| 316 | val expectedContentLeftPadding = 32.dp |
| 317 | val expectedTextBaseline = 32.dp |
| 318 | val expectedSecondaryTextBaselineOffset = 20.dp |
| 319 | |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 320 | val textPosition = Ref<Offset>() |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 321 | val textBaseline = Ref<Float>() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 322 | val textSize = Ref<IntSize>() |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 323 | val secondaryTextPosition = Ref<Offset>() |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 324 | val secondaryTextBaseline = Ref<Float>() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 325 | val secondaryTextSize = Ref<IntSize>() |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 326 | val iconPosition = Ref<Offset>() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 327 | val iconSize = Ref<IntSize>() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 328 | composeTestRule.setMaterialContent { |
Matvei Malkov | 201726d | 2020-03-13 14:03:54 +0000 | [diff] [blame] | 329 | Box { |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 330 | ListItem( |
| 331 | text = { |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 332 | Text("Primary text", saveLayout(textPosition, textSize, textBaseline)) |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 333 | }, |
| 334 | secondaryText = { |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 335 | Text( |
| 336 | "Secondary text", |
| 337 | saveLayout( |
| 338 | secondaryTextPosition, |
| 339 | secondaryTextSize, |
| 340 | secondaryTextBaseline |
| 341 | ) |
| 342 | ) |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 343 | }, |
| 344 | icon = { |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 345 | Image(icon24x24, saveLayout(iconPosition, iconSize)) |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 346 | } |
| 347 | ) |
| 348 | } |
| 349 | } |
Filip Pavlis | a4d73a5 | 2020-07-14 11:30:00 +0100 | [diff] [blame] | 350 | composeTestRule.runOnIdleWithDensity { |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 351 | assertThat(textPosition.value!!.x).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 352 | expectedLeftPadding.toIntPx().toFloat() + iconSize.value!!.width + |
| 353 | expectedContentLeftPadding.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 354 | ) |
Nader Jawad | 63153c1 | 2020-05-21 14:17:08 -0700 | [diff] [blame] | 355 | assertThat(textBaseline.value!!).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 356 | expectedTextBaseline.toIntPx().toFloat() |
Nader Jawad | 63153c1 | 2020-05-21 14:17:08 -0700 | [diff] [blame] | 357 | ) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 358 | assertThat(secondaryTextPosition.value!!.x).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 359 | expectedLeftPadding.toIntPx().toFloat() + |
| 360 | iconSize.value!!.width + |
| 361 | expectedContentLeftPadding.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 362 | ) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 363 | assertThat(secondaryTextBaseline.value!!).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 364 | expectedTextBaseline.toIntPx().toFloat() + |
| 365 | expectedSecondaryTextBaselineOffset.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 366 | ) |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 367 | assertThat(iconPosition.value!!.x).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 368 | expectedLeftPadding.toIntPx().toFloat() |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 369 | ) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 370 | assertThat(iconPosition.value!!.y).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 371 | expectedIconTopPadding.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 372 | ) |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | @Test |
| 377 | fun listItem_twoLine_positioning_withLargeIcon() { |
| 378 | val listItemHeight = 72.dp |
| 379 | val expectedLeftPadding = 16.dp |
| 380 | val expectedIconTopPadding = 16.dp |
| 381 | val expectedContentLeftPadding = 16.dp |
| 382 | val expectedTextBaseline = 32.dp |
| 383 | val expectedSecondaryTextBaselineOffset = 20.dp |
| 384 | val expectedRightPadding = 16.dp |
| 385 | |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 386 | val textPosition = Ref<Offset>() |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 387 | val textBaseline = Ref<Float>() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 388 | val textSize = Ref<IntSize>() |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 389 | val secondaryTextPosition = Ref<Offset>() |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 390 | val secondaryTextBaseline = Ref<Float>() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 391 | val secondaryTextSize = Ref<IntSize>() |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 392 | val iconPosition = Ref<Offset>() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 393 | val iconSize = Ref<IntSize>() |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 394 | val trailingPosition = Ref<Offset>() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 395 | val trailingSize = Ref<IntSize>() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 396 | composeTestRule.setMaterialContent { |
Matvei Malkov | 201726d | 2020-03-13 14:03:54 +0000 | [diff] [blame] | 397 | Box { |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 398 | ListItem( |
| 399 | text = { |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 400 | Text("Primary text", saveLayout(textPosition, textSize, textBaseline)) |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 401 | }, |
| 402 | secondaryText = { |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 403 | Text( |
| 404 | "Secondary text", |
| 405 | saveLayout( |
| 406 | secondaryTextPosition, |
| 407 | secondaryTextSize, |
| 408 | secondaryTextBaseline |
| 409 | ) |
| 410 | ) |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 411 | }, |
| 412 | icon = { |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 413 | Image(icon40x40, saveLayout(iconPosition, iconSize)) |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 414 | }, |
| 415 | trailing = { |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 416 | Image(icon24x24, saveLayout(trailingPosition, trailingSize)) |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 417 | } |
| 418 | ) |
| 419 | } |
| 420 | } |
Filip Pavlis | a4d73a5 | 2020-07-14 11:30:00 +0100 | [diff] [blame] | 421 | composeTestRule.runOnIdleWithDensity { |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 422 | assertThat(textPosition.value!!.x).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 423 | expectedLeftPadding.toIntPx().toFloat() + iconSize.value!!.width + |
| 424 | expectedContentLeftPadding.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 425 | ) |
Nader Jawad | 63153c1 | 2020-05-21 14:17:08 -0700 | [diff] [blame] | 426 | assertThat(textBaseline.value!!).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 427 | expectedTextBaseline.toIntPx().toFloat() |
Nader Jawad | 63153c1 | 2020-05-21 14:17:08 -0700 | [diff] [blame] | 428 | ) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 429 | assertThat(secondaryTextPosition.value!!.x).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 430 | expectedLeftPadding.toIntPx().toFloat() + |
| 431 | iconSize.value!!.width + |
| 432 | expectedContentLeftPadding.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 433 | ) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 434 | assertThat(secondaryTextBaseline.value!!).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 435 | expectedTextBaseline.toIntPx().toFloat() + |
| 436 | expectedSecondaryTextBaselineOffset.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 437 | ) |
Nader Jawad | 63153c1 | 2020-05-21 14:17:08 -0700 | [diff] [blame] | 438 | assertThat(iconPosition.value!!.x).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 439 | expectedLeftPadding.toIntPx().toFloat() |
Nader Jawad | 63153c1 | 2020-05-21 14:17:08 -0700 | [diff] [blame] | 440 | ) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 441 | assertThat(iconPosition.value!!.y).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 442 | expectedIconTopPadding.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 443 | ) |
| 444 | val dm = composeTestRule.displayMetrics |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 445 | assertThat(trailingPosition.value!!.x).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 446 | dm.widthPixels - trailingSize.value!!.width - |
| 447 | expectedRightPadding.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 448 | ) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 449 | assertThat(trailingPosition.value!!.y).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 450 | ((listItemHeight.toIntPx() - trailingSize.value!!.height) / 2).toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 451 | ) |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | @Test |
| 456 | fun listItem_threeLine_positioning_noOverline_metaText() { |
| 457 | val expectedLeftPadding = 16.dp |
| 458 | val expectedIconTopPadding = 16.dp |
| 459 | val expectedContentLeftPadding = 32.dp |
| 460 | val expectedTextBaseline = 28.dp |
| 461 | val expectedSecondaryTextBaselineOffset = 20.dp |
| 462 | val expectedRightPadding = 16.dp |
| 463 | |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 464 | val textPosition = Ref<Offset>() |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 465 | val textBaseline = Ref<Float>() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 466 | val textSize = Ref<IntSize>() |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 467 | val secondaryTextPosition = Ref<Offset>() |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 468 | val secondaryTextBaseline = Ref<Float>() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 469 | val secondaryTextSize = Ref<IntSize>() |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 470 | val iconPosition = Ref<Offset>() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 471 | val iconSize = Ref<IntSize>() |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 472 | val trailingPosition = Ref<Offset>() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 473 | val trailingSize = Ref<IntSize>() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 474 | composeTestRule.setMaterialContent { |
Matvei Malkov | 201726d | 2020-03-13 14:03:54 +0000 | [diff] [blame] | 475 | Box { |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 476 | ListItem( |
| 477 | text = { |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 478 | Text("Primary text", saveLayout(textPosition, textSize, textBaseline)) |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 479 | }, |
| 480 | secondaryText = { |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 481 | Text( |
| 482 | "Secondary text", |
| 483 | saveLayout( |
| 484 | secondaryTextPosition, |
| 485 | secondaryTextSize, |
| 486 | secondaryTextBaseline |
| 487 | ) |
| 488 | ) |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 489 | }, |
| 490 | singleLineSecondaryText = false, |
| 491 | icon = { |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 492 | Image(icon24x24, saveLayout(iconPosition, iconSize)) |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 493 | }, |
| 494 | trailing = { |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 495 | Image(icon24x24, saveLayout(trailingPosition, trailingSize)) |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 496 | } |
| 497 | ) |
| 498 | } |
| 499 | } |
Filip Pavlis | a4d73a5 | 2020-07-14 11:30:00 +0100 | [diff] [blame] | 500 | composeTestRule.runOnIdleWithDensity { |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 501 | assertThat(textPosition.value!!.x).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 502 | expectedLeftPadding.toIntPx().toFloat() + iconSize.value!!.width + |
| 503 | expectedContentLeftPadding.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 504 | ) |
Nader Jawad | 63153c1 | 2020-05-21 14:17:08 -0700 | [diff] [blame] | 505 | assertThat(textBaseline.value!!).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 506 | expectedTextBaseline.toIntPx().toFloat() |
Nader Jawad | 63153c1 | 2020-05-21 14:17:08 -0700 | [diff] [blame] | 507 | ) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 508 | assertThat(secondaryTextPosition.value!!.x).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 509 | expectedLeftPadding.toIntPx().toFloat() + iconSize.value!!.width + |
| 510 | expectedContentLeftPadding.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 511 | ) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 512 | assertThat(secondaryTextBaseline.value!!).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 513 | expectedTextBaseline.toIntPx().toFloat() + |
| 514 | expectedSecondaryTextBaselineOffset.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 515 | ) |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 516 | assertThat(iconPosition.value!!.x).isEqualTo(expectedLeftPadding.toIntPx().toFloat()) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 517 | assertThat(iconPosition.value!!.y).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 518 | expectedIconTopPadding.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 519 | ) |
| 520 | val dm = composeTestRule.displayMetrics |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 521 | assertThat(trailingPosition.value!!.x).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 522 | dm.widthPixels - trailingSize.value!!.width.toFloat() - |
| 523 | expectedRightPadding.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 524 | ) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 525 | assertThat(trailingPosition.value!!.y).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 526 | expectedIconTopPadding.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 527 | ) |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | @Test |
| 532 | fun listItem_threeLine_positioning_overline_trailingIcon() { |
| 533 | val expectedLeftPadding = 16.dp |
| 534 | val expectedIconTopPadding = 16.dp |
| 535 | val expectedContentLeftPadding = 16.dp |
| 536 | val expectedOverlineBaseline = 28.dp |
| 537 | val expectedTextBaselineOffset = 20.dp |
| 538 | val expectedSecondaryTextBaselineOffset = 20.dp |
| 539 | val expectedRightPadding = 16.dp |
| 540 | |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 541 | val textPosition = Ref<Offset>() |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 542 | val textBaseline = Ref<Float>() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 543 | val textSize = Ref<IntSize>() |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 544 | val overlineTextPosition = Ref<Offset>() |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 545 | val overlineTextBaseline = Ref<Float>() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 546 | val overlineTextSize = Ref<IntSize>() |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 547 | val secondaryTextPosition = Ref<Offset>() |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 548 | val secondaryTextBaseline = Ref<Float>() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 549 | val secondaryTextSize = Ref<IntSize>() |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 550 | val iconPosition = Ref<Offset>() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 551 | val iconSize = Ref<IntSize>() |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 552 | val trailingPosition = Ref<Offset>() |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 553 | val trailingSize = Ref<IntSize>() |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 554 | val trailingBaseline = Ref<Float>() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 555 | composeTestRule.setMaterialContent { |
Matvei Malkov | 201726d | 2020-03-13 14:03:54 +0000 | [diff] [blame] | 556 | Box { |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 557 | ListItem( |
| 558 | overlineText = { |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 559 | Text( |
| 560 | "OVERLINE", |
| 561 | saveLayout( |
| 562 | overlineTextPosition, |
| 563 | overlineTextSize, |
| 564 | overlineTextBaseline |
| 565 | ) |
| 566 | ) |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 567 | }, |
| 568 | text = { |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 569 | Text("Primary text", saveLayout(textPosition, textSize, textBaseline)) |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 570 | }, |
| 571 | secondaryText = { |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 572 | Text( |
| 573 | "Secondary text", |
| 574 | saveLayout( |
| 575 | secondaryTextPosition, |
| 576 | secondaryTextSize, |
| 577 | secondaryTextBaseline |
| 578 | ) |
| 579 | ) |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 580 | }, |
| 581 | icon = { |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 582 | Image( |
| 583 | icon40x40, |
| 584 | saveLayout(iconPosition, iconSize) |
| 585 | ) |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 586 | }, |
| 587 | trailing = { |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 588 | Text( |
| 589 | "meta", |
| 590 | saveLayout( |
| 591 | trailingPosition, |
| 592 | trailingSize, |
| 593 | trailingBaseline |
| 594 | ) |
| 595 | ) |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 596 | } |
| 597 | ) |
| 598 | } |
| 599 | } |
Filip Pavlis | a4d73a5 | 2020-07-14 11:30:00 +0100 | [diff] [blame] | 600 | composeTestRule.runOnIdleWithDensity { |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 601 | assertThat(textPosition.value!!.x).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 602 | expectedLeftPadding.toIntPx().toFloat() + |
| 603 | iconSize.value!!.width + |
| 604 | expectedContentLeftPadding.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 605 | ) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 606 | assertThat(textBaseline.value!!).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 607 | expectedOverlineBaseline.toIntPx().toFloat() + |
| 608 | expectedTextBaselineOffset.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 609 | ) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 610 | assertThat(overlineTextPosition.value!!.x).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 611 | expectedLeftPadding.toIntPx().toFloat() + |
| 612 | iconSize.value!!.width + |
| 613 | expectedContentLeftPadding.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 614 | ) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 615 | assertThat(overlineTextBaseline.value!!).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 616 | expectedOverlineBaseline.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 617 | ) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 618 | assertThat(secondaryTextPosition.value!!.x).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 619 | expectedLeftPadding.toIntPx().toFloat() + |
| 620 | iconSize.value!!.width + |
| 621 | expectedContentLeftPadding.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 622 | ) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 623 | assertThat(secondaryTextBaseline.value!!).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 624 | expectedOverlineBaseline.toIntPx().toFloat() + |
| 625 | expectedTextBaselineOffset.toIntPx().toFloat() + |
| 626 | expectedSecondaryTextBaselineOffset.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 627 | ) |
Nader Jawad | 63153c1 | 2020-05-21 14:17:08 -0700 | [diff] [blame] | 628 | assertThat(iconPosition.value!!.x).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 629 | expectedLeftPadding.toIntPx().toFloat() |
Nader Jawad | 63153c1 | 2020-05-21 14:17:08 -0700 | [diff] [blame] | 630 | ) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 631 | assertThat(iconPosition.value!!.y).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 632 | expectedIconTopPadding.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 633 | ) |
| 634 | val dm = composeTestRule.displayMetrics |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 635 | assertThat(trailingPosition.value!!.x).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 636 | dm.widthPixels - trailingSize.value!!.width - |
| 637 | expectedRightPadding.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 638 | ) |
Filip Pavlis | 2b161e4 | 2019-11-22 17:40:08 +0000 | [diff] [blame] | 639 | assertThat(trailingBaseline.value!!).isEqualTo( |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 640 | expectedOverlineBaseline.toIntPx().toFloat() |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 641 | ) |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | private fun Dp.toIntPx() = (this.value * composeTestRule.density.density).roundToInt() |
| 646 | |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 647 | private fun saveLayout( |
Nader Jawad | 6df0612 | 2020-06-03 15:27:08 -0700 | [diff] [blame] | 648 | coords: Ref<Offset>, |
George Mount | 8f23757 | 2020-04-30 12:08:30 -0700 | [diff] [blame] | 649 | size: Ref<IntSize>, |
Nader Jawad | e6a9b33 | 2020-05-21 13:49:20 -0700 | [diff] [blame] | 650 | baseline: Ref<Float> = Ref() |
Adam Powell | 999a89b | 2020-03-11 09:08:07 -0700 | [diff] [blame] | 651 | ): Modifier = Modifier.onPositioned { coordinates: LayoutCoordinates -> |
Filip Pavlis | 028c0d5 | 2020-06-29 15:07:53 +0100 | [diff] [blame] | 652 | coords.value = coordinates.localToRoot(Offset.Zero) |
Mihai Popa | 08c4795 | 2020-06-04 20:01:27 +0100 | [diff] [blame] | 653 | baseline.value = coordinates[FirstBaseline].toFloat() + coords.value!!.y |
George Mount | bfa1fb2 | 2020-03-11 09:35:05 -0700 | [diff] [blame] | 654 | size.value = coordinates.size |
Mihai Popa | 746e230 | 2019-09-30 17:31:56 +0100 | [diff] [blame] | 655 | } |
| 656 | } |