Connie Shi | 69897da | 2021-10-08 14:59:36 -0400 | [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 | package androidx.compose.material3 |
| 17 | |
| 18 | import android.os.Build |
| 19 | import androidx.compose.foundation.layout.size |
| 20 | import androidx.compose.material.icons.Icons |
| 21 | import androidx.compose.material.icons.filled.Favorite |
Mariano Martin | 49ce217 | 2022-01-11 16:27:06 -0500 | [diff] [blame] | 22 | import androidx.compose.material3.tokens.BadgeTokens |
Connie Shi | 69897da | 2021-10-08 14:59:36 -0400 | [diff] [blame] | 23 | import androidx.compose.testutils.assertShape |
| 24 | import androidx.compose.ui.Modifier |
| 25 | import androidx.compose.ui.graphics.Color |
| 26 | import androidx.compose.ui.platform.testTag |
| 27 | import androidx.compose.ui.semantics.contentDescription |
| 28 | import androidx.compose.ui.semantics.semantics |
| 29 | import androidx.compose.ui.test.assertContentDescriptionEquals |
| 30 | import androidx.compose.ui.test.assertHeightIsEqualTo |
| 31 | import androidx.compose.ui.test.assertPositionInRootIsEqualTo |
| 32 | import androidx.compose.ui.test.assertWidthIsAtLeast |
| 33 | import androidx.compose.ui.test.assertWidthIsEqualTo |
| 34 | import androidx.compose.ui.test.captureToImage |
| 35 | import androidx.compose.ui.test.getUnclippedBoundsInRoot |
| 36 | import androidx.compose.ui.test.junit4.createComposeRule |
| 37 | import androidx.compose.ui.test.onNodeWithTag |
| 38 | import androidx.compose.ui.test.onSibling |
| 39 | import androidx.compose.ui.unit.dp |
| 40 | import androidx.compose.ui.unit.height |
| 41 | import androidx.compose.ui.unit.max |
| 42 | import androidx.compose.ui.unit.width |
| 43 | import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 44 | import androidx.test.filters.LargeTest |
| 45 | import androidx.test.filters.SdkSuppress |
| 46 | import org.junit.Rule |
| 47 | import org.junit.Test |
| 48 | import org.junit.runner.RunWith |
| 49 | |
| 50 | @LargeTest |
| 51 | @RunWith(AndroidJUnit4::class) |
| 52 | class BadgeTest { |
| 53 | |
| 54 | private val icon = Icons.Filled.Favorite |
| 55 | |
| 56 | @get:Rule |
| 57 | val rule = createComposeRule() |
| 58 | |
| 59 | @Test |
| 60 | fun badge_noContent_size() { |
| 61 | rule |
| 62 | .setMaterialContentForSizeAssertions { |
| 63 | Badge() |
| 64 | } |
Mariano Martin | 49ce217 | 2022-01-11 16:27:06 -0500 | [diff] [blame] | 65 | .assertHeightIsEqualTo(BadgeTokens.Size) |
| 66 | .assertWidthIsEqualTo(BadgeTokens.Size) |
Connie Shi | 69897da | 2021-10-08 14:59:36 -0400 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Test |
| 70 | fun badge_shortContent_size() { |
| 71 | rule |
| 72 | .setMaterialContentForSizeAssertions { |
| 73 | Badge { Text("1") } |
| 74 | } |
Mariano Martin | 49ce217 | 2022-01-11 16:27:06 -0500 | [diff] [blame] | 75 | .assertHeightIsEqualTo(BadgeTokens.LargeSize) |
| 76 | .assertWidthIsEqualTo(BadgeTokens.LargeSize) |
Connie Shi | 69897da | 2021-10-08 14:59:36 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | @Test |
| 80 | fun badge_longContent_size() { |
| 81 | rule |
| 82 | .setMaterialContentForSizeAssertions { |
| 83 | Badge { Text("999+") } |
| 84 | } |
Mariano Martin | 49ce217 | 2022-01-11 16:27:06 -0500 | [diff] [blame] | 85 | .assertHeightIsEqualTo(BadgeTokens.LargeSize) |
| 86 | .assertWidthIsAtLeast(BadgeTokens.LargeSize) |
Connie Shi | 69897da | 2021-10-08 14:59:36 -0400 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Test |
| 90 | fun badge_shortContent_customSizeModifier_size() { |
| 91 | val customWidth = 24.dp |
| 92 | val customHeight = 6.dp |
| 93 | rule |
| 94 | .setMaterialContentForSizeAssertions { |
| 95 | Badge(modifier = Modifier.size(customWidth, customHeight)) { |
| 96 | Text("1") |
| 97 | } |
| 98 | } |
| 99 | .assertHeightIsEqualTo(customHeight) |
| 100 | .assertWidthIsEqualTo(customWidth) |
| 101 | } |
| 102 | |
| 103 | @SdkSuppress(minSdkVersion = Build.VERSION_CODES.O) |
| 104 | @Test |
| 105 | fun badge_noContent_shape() { |
| 106 | var errorColor = Color.Unspecified |
Shalom Gibly | 7d59ffd | 2021-12-16 15:58:06 -0800 | [diff] [blame] | 107 | rule.setMaterialContent(lightColorScheme()) { |
Mariano Martin | 49ce217 | 2022-01-11 16:27:06 -0500 | [diff] [blame] | 108 | errorColor = MaterialTheme.colorScheme.fromToken(BadgeTokens.Color) |
Connie Shi | 69897da | 2021-10-08 14:59:36 -0400 | [diff] [blame] | 109 | Badge(modifier = Modifier.testTag(TestBadgeTag)) |
| 110 | } |
| 111 | |
| 112 | rule.onNodeWithTag(TestBadgeTag) |
| 113 | .captureToImage() |
| 114 | .assertShape( |
| 115 | density = rule.density, |
Mariano Martin | 49ce217 | 2022-01-11 16:27:06 -0500 | [diff] [blame] | 116 | shape = BadgeTokens.Shape, |
Connie Shi | 69897da | 2021-10-08 14:59:36 -0400 | [diff] [blame] | 117 | shapeColor = errorColor, |
| 118 | backgroundColor = Color.White, |
| 119 | shapeOverlapPixelCount = with(rule.density) { 1.dp.toPx() } |
| 120 | ) |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | fun badgeBox_noContent_position() { |
| 125 | rule |
Shalom Gibly | 7d59ffd | 2021-12-16 15:58:06 -0800 | [diff] [blame] | 126 | .setMaterialContent(lightColorScheme()) { |
Connie Shi | 69897da | 2021-10-08 14:59:36 -0400 | [diff] [blame] | 127 | BadgedBox(badge = { Badge(Modifier.testTag(TestBadgeTag)) }) { |
| 128 | Icon( |
| 129 | icon, |
| 130 | null, |
| 131 | modifier = Modifier.testTag(TestAnchorTag) |
| 132 | ) |
| 133 | } |
| 134 | } |
| 135 | val badge = rule.onNodeWithTag(TestBadgeTag) |
| 136 | val anchorBounds = rule.onNodeWithTag(TestAnchorTag).getUnclippedBoundsInRoot() |
| 137 | val badgeBounds = badge.getUnclippedBoundsInRoot() |
| 138 | badge.assertPositionInRootIsEqualTo( |
| 139 | expectedLeft = |
Shalom Gibly | 7d59ffd | 2021-12-16 15:58:06 -0800 | [diff] [blame] | 140 | anchorBounds.right + BadgeOffset + |
Mariano Martin | 49ce217 | 2022-01-11 16:27:06 -0500 | [diff] [blame] | 141 | max((BadgeTokens.Size - badgeBounds.width) / 2, 0.dp), |
Connie Shi | 69897da | 2021-10-08 14:59:36 -0400 | [diff] [blame] | 142 | expectedTop = -badgeBounds.height / 2 |
| 143 | ) |
| 144 | } |
| 145 | |
| 146 | @Test |
| 147 | fun badgeBox_shortContent_position() { |
| 148 | rule |
Shalom Gibly | 7d59ffd | 2021-12-16 15:58:06 -0800 | [diff] [blame] | 149 | .setMaterialContent(lightColorScheme()) { |
Connie Shi | 69897da | 2021-10-08 14:59:36 -0400 | [diff] [blame] | 150 | BadgedBox(badge = { Badge { Text("8") } }) { |
| 151 | Icon( |
| 152 | icon, |
| 153 | null, |
| 154 | modifier = Modifier.testTag(TestAnchorTag) |
| 155 | ) |
| 156 | } |
| 157 | } |
| 158 | val badge = rule.onNodeWithTag(TestAnchorTag).onSibling() |
| 159 | val anchorBounds = rule.onNodeWithTag(TestAnchorTag).getUnclippedBoundsInRoot() |
| 160 | val badgeBounds = badge.getUnclippedBoundsInRoot() |
| 161 | badge.assertPositionInRootIsEqualTo( |
| 162 | expectedLeft = anchorBounds.right + BadgeWithContentHorizontalOffset + max |
Shalom Gibly | 7d59ffd | 2021-12-16 15:58:06 -0800 | [diff] [blame] | 163 | ( |
Connie Shi | 69897da | 2021-10-08 14:59:36 -0400 | [diff] [blame] | 164 | ( |
Mariano Martin | 49ce217 | 2022-01-11 16:27:06 -0500 | [diff] [blame] | 165 | BadgeTokens.LargeSize - badgeBounds.width |
Connie Shi | 69897da | 2021-10-08 14:59:36 -0400 | [diff] [blame] | 166 | ) / 2, |
| 167 | 0.dp |
| 168 | ), |
| 169 | expectedTop = -badgeBounds.height / 2 + BadgeWithContentVerticalOffset |
| 170 | ) |
| 171 | } |
| 172 | |
| 173 | @Test |
| 174 | fun badgeBox_longContent_position() { |
| 175 | rule |
Shalom Gibly | 7d59ffd | 2021-12-16 15:58:06 -0800 | [diff] [blame] | 176 | .setMaterialContent(lightColorScheme()) { |
Connie Shi | 69897da | 2021-10-08 14:59:36 -0400 | [diff] [blame] | 177 | BadgedBox(badge = { Badge { Text("999+") } }) { |
| 178 | Icon( |
| 179 | icon, |
| 180 | null, |
| 181 | modifier = Modifier.testTag(TestAnchorTag) |
| 182 | ) |
| 183 | } |
| 184 | } |
| 185 | val badge = rule.onNodeWithTag(TestAnchorTag).onSibling() |
| 186 | val anchorBounds = rule.onNodeWithTag(TestAnchorTag).getUnclippedBoundsInRoot() |
| 187 | val badgeBounds = badge.getUnclippedBoundsInRoot() |
| 188 | |
| 189 | val totalBadgeHorizontalOffset = BadgeWithContentHorizontalOffset + |
| 190 | BadgeWithContentHorizontalPadding |
| 191 | badge.assertPositionInRootIsEqualTo( |
| 192 | expectedLeft = anchorBounds.right + totalBadgeHorizontalOffset, |
| 193 | expectedTop = -badgeBounds.height / 2 + BadgeWithContentVerticalOffset |
| 194 | ) |
| 195 | } |
| 196 | |
| 197 | @Test |
| 198 | fun badge_notMergingDescendants_withOwnContentDescription() { |
Shalom Gibly | 7d59ffd | 2021-12-16 15:58:06 -0800 | [diff] [blame] | 199 | rule.setMaterialContent(lightColorScheme()) { |
Connie Shi | 69897da | 2021-10-08 14:59:36 -0400 | [diff] [blame] | 200 | BadgedBox( |
| 201 | badge = { |
| 202 | Badge { Text("99+") } |
| 203 | }, |
| 204 | modifier = Modifier.testTag(TestBadgeTag).semantics { |
| 205 | this.contentDescription = "more than 99 new email" |
| 206 | } |
| 207 | ) { |
| 208 | Text( |
| 209 | "inbox", |
| 210 | Modifier.semantics { |
| 211 | this.contentDescription = "inbox" |
| 212 | }.testTag(TestAnchorTag) |
| 213 | ) |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | rule.onNodeWithTag(TestBadgeTag).assertContentDescriptionEquals("more than 99 new email") |
| 218 | rule.onNodeWithTag(TestAnchorTag).assertContentDescriptionEquals("inbox") |
| 219 | } |
| 220 | |
| 221 | @Test |
| 222 | fun badgeBox_size() { |
| 223 | rule.setMaterialContentForSizeAssertions { |
| 224 | BadgedBox(badge = { Badge { Text("999+") } }) { |
| 225 | Icon(icon, null) |
| 226 | } |
| 227 | } |
| 228 | .assertWidthIsEqualTo(icon.defaultWidth) |
| 229 | .assertHeightIsEqualTo(icon.defaultHeight) |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | private const val TestBadgeTag = "badge" |
| 234 | private const val TestAnchorTag = "anchor" |