Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Link Button content descriptions #9350

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 deletions link/src/main/java/com/stripe/android/link/ui/LinkButton.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package com.stripe.android.link.ui

import androidx.annotation.RestrictTo
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.RowScope
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.defaultMinSize
Expand All @@ -24,20 +25,26 @@ import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.remember
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.invisibleToUser
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.em
import androidx.compose.ui.unit.sp
import com.stripe.android.core.strings.resolvableString
import com.stripe.android.link.R
import com.stripe.android.link.theme.DefaultLinkTheme
import com.stripe.android.link.theme.linkColors
Expand Down Expand Up @@ -136,24 +143,33 @@ fun LinkButton(
}

@Composable
private fun RowScope.SignedInButtonContent(email: String) {
private fun SignedInButtonContent(email: String) {
val annotatedEmail = remember(email) {
buildAnnotatedString {
append(email)
}
}

val color = MaterialTheme.linkColors.buttonLabel.copy(alpha = LocalContentAlpha.current)
val payWithLinkText = resolvableString(R.string.stripe_pay_with_link).resolve(LocalContext.current)

LinkIconAndDivider()
Text(
text = annotatedEmail,
color = color,
fontSize = LINK_EMAIL_FONT_SIZE.sp,
overflow = TextOverflow.Ellipsis,
modifier = Modifier.weight(LINK_EMAIL_TEXT_WEIGHT, fill = false),
maxLines = 1
)
Row(
modifier = Modifier.semantics(
mergeDescendants = true
) {
this.contentDescription = payWithLinkText
}
) {
LinkIconAndDivider()
Text(
text = annotatedEmail,
color = color,
fontSize = LINK_EMAIL_FONT_SIZE.sp,
overflow = TextOverflow.Ellipsis,
modifier = Modifier.weight(LINK_EMAIL_TEXT_WEIGHT, fill = false),
maxLines = 1
)
}
}

@Suppress("UnusedReceiverParameter")
Expand All @@ -178,14 +194,18 @@ private fun RowScope.SignedOutButtonContent() {
}.build(),
modifier = Modifier
.padding(start = 6.dp)
.fillMaxWidth(),
.fillMaxWidth()
.semantics {
this.contentDescription = text
},
color = MaterialTheme.linkColors.buttonLabel.copy(alpha = LocalContentAlpha.current),
fontSize = LINK_PAY_WITH_FONT_SIZE.sp,
overflow = TextOverflow.Ellipsis,
maxLines = 1
)
}

@OptIn(ExperimentalComposeUiApi::class)
@Composable
private fun LinkIconAndDivider() {
val annotatedLinkAndDivider = remember {
Expand Down Expand Up @@ -218,7 +238,8 @@ private fun LinkIconAndDivider() {
add(id = LINK_ICON_ID, width = 3.em, height = 1.1.em) { LinkIcon() }
add(id = LINK_DIVIDER_ID, width = 0.1.em, height = 1.3.em) { LinkDivider() }
addSpacer(id = LINK_DIVIDER_SPACER_ID, width = 0.5.em)
}.build()
}.build(),
modifier = Modifier.semantics { this.invisibleToUser() },
)
}

Expand Down
45 changes: 45 additions & 0 deletions link/src/test/java/com/stripe/android/link/ui/LinkButtonTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.stripe.android.link.ui

import android.os.Build
import androidx.compose.ui.test.assertAny
import androidx.compose.ui.test.assertContentDescriptionContains
import androidx.compose.ui.test.hasContentDescription
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onChildren
import androidx.compose.ui.test.onNodeWithTag
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

@RunWith(RobolectricTestRunner::class)
@Config(sdk = [Build.VERSION_CODES.Q])
class LinkButtonTest {
@get:Rule
val composeRule = createComposeRule()

@Test
fun signedInButton_hasUsefulContentDescription() {
composeRule.setContent {
LinkButton(email = "email@email.com", enabled = true, onClick = {})
}

composeRule.onNodeWithTag(
LinkButtonTestTag
).onChildren().assertAny(
hasContentDescription("Pay with Link")
)
}

@Test
fun signedOutButton_hasUsefulContentDescription() {
composeRule.setContent {
LinkButton(email = null, enabled = true, onClick = {})
}

composeRule.onNodeWithTag(
LinkButtonTestTag
).assertContentDescriptionContains("Pay with Link")
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really didn't intend to update the UI at all. I can't visually tell the difference between any of these, except if you use the swipe feature, you can see the the email is moved down the tiniest bit. I think this is because I added a Row to the SignedInButtonContent to set a content description. Lmk if I should rework the PR for this

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading