Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -237,37 +237,6 @@ private fun RecoveryKeyStep(
}
}

@Composable
private fun PinField(
value: String,
onValueChange: (String) -> Unit,
visible: Boolean,
onVisibleChange: (Boolean) -> Unit,
isError: Boolean,
supportingText: String?,
) {
OutlinedTextField(
value = value,
onValueChange = onValueChange,
label = { Text("PIN or passphrase") },
singleLine = true,
isError = isError,
supportingText = supportingText?.let { { Text(it) } },
visualTransformation = if (visible) VisualTransformation.None else PasswordVisualTransformation(),
// Password (not NumberPassword): a 4-digit PIN is the suggested
// default, but a longer alphanumeric passphrase must remain typeable.
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
trailingIcon = {
IconButton(onClick = { onVisibleChange(!visible) }) {
Icon(
if (visible) Icons.Filled.VisibilityOff else Icons.Filled.Visibility,
contentDescription = if (visible) "Hide" else "Show",
)
}
},
modifier = Modifier.fillMaxWidth(),
)
}

@Composable
private fun RecoveryKeyQr(content: String) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package xyz.libravault.feature.vault

import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Visibility
import androidx.compose.material.icons.filled.VisibilityOff
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation

/**
* The vault PIN / passphrase entry field, with its show-hide toggle.
*
* Lifted out of [CreateVaultScreen] so [UnlockVaultScreen] can use the same
* one. The two screens previously carried byte-for-byte copies of this field —
* same label, same `PasswordVisualTransformation`, same trailing toggle — which
* is how the *entry point to an encrypted vault* ends up behaving differently
* in two places after someone fixes only the copy they were looking at. That is
* not hypothetical: the accessibility fix below had to be applied twice before
* this was unified.
*
* `internal` rather than `private` so it can be unit-tested directly, per
* `AGENTS.md`. It is a pure function of its arguments — no ViewModel, no DI.
*/
@Composable
internal fun PinField(
value: String,
onValueChange: (String) -> Unit,
visible: Boolean,
onVisibleChange: (Boolean) -> Unit,
isError: Boolean,
supportingText: String?,
) {
OutlinedTextField(
value = value,
onValueChange = onValueChange,
label = { Text("PIN or passphrase") },
singleLine = true,
isError = isError,
supportingText = supportingText?.let { { Text(it) } },
visualTransformation = if (visible) VisualTransformation.None else PasswordVisualTransformation(),
// Password (not NumberPassword): a 4-digit PIN is the suggested
// default, but a longer alphanumeric passphrase must remain typeable.
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
trailingIcon = {
IconButton(onClick = { onVisibleChange(!visible) }) {
Icon(
if (visible) Icons.Filled.VisibilityOff else Icons.Filled.Visibility,
// "Show"/"Hide" alone is what TalkBack read before: a bare
// verb with no object, on a screen that also has a Back
// button and a submit button. Naming the thing being
// toggled is the whole difference between a usable
// announcement and a guess.
contentDescription = if (visible) "Hide PIN" else "Show PIN",
)
}
},
modifier = Modifier.fillMaxWidth(),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,16 @@ private fun PinUnlockBody(state: UnlockVaultUiState, viewModel: UnlockVaultViewM
Column(modifier = Modifier.fillMaxSize().padding(24.dp), verticalArrangement = Arrangement.Center) {
Text("Enter your PIN", style = MaterialTheme.typography.titleMedium)
Spacer24()
OutlinedTextField(
// Was a byte-for-byte copy of CreateVaultScreen's field. Shared now, so
// the two entry points into an encrypted vault cannot drift — see
// PinField.
PinField(
value = state.pin,
onValueChange = viewModel::onPinChanged,
label = { Text("PIN or passphrase") },
singleLine = true,
visible = pinVisible,
onVisibleChange = { pinVisible = it },
isError = state.errorMessage != null,
supportingText = state.errorMessage?.let { { Text(it) } },
visualTransformation = if (pinVisible) VisualTransformation.None else PasswordVisualTransformation(),
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
trailingIcon = {
IconButton(onClick = { pinVisible = !pinVisible }) {
Icon(
if (pinVisible) Icons.Filled.VisibilityOff else Icons.Filled.Visibility,
contentDescription = if (pinVisible) "Hide" else "Show",
)
}
},
modifier = Modifier.fillMaxWidth(),
supportingText = state.errorMessage,
)
Spacer24()
if (state.isUnlocking) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package xyz.libravault.feature.vault

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.semantics.SemanticsProperties
import androidx.compose.ui.semantics.getOrNull
import androidx.compose.ui.test.hasClickAction
import androidx.compose.ui.test.junit4.ComposeContentTestRule
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.performClick
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import xyz.libravault.core.ui.theme.LibravaultTheme

/**
* Accessibility and behaviour of [PinField] — Phase 5 of
* docs/TEST_COVERAGE_PRD.md.
*
* This is the entry point to an encrypted vault, and it used to exist twice:
* [CreateVaultScreen] and [UnlockVaultScreen] carried byte-for-byte copies.
* Both are now this one component, which is the reason it is worth testing once
* properly rather than twice badly.
*
* The show/hide toggle announced a bare **"Show"** / **"Hide"** — a verb with
* no object, on a screen that also has a Back button and a submit button.
* TalkBack said "Hide, button" and left the user to infer what. It now names
* the thing it toggles.
*
* That is the same class of defect as the reader's "A-"/"A+" font buttons
* (#269): a label that *exists*, so a generic "everything is labelled" sweep
* passes, but that does not tell the user what the control does. Only a test
* that pins the wording catches it.
*/
@RunWith(RobolectricTestRunner::class)
@Config(sdk = [34])
class PinFieldAccessibilityTest {

@get:Rule
val composeTestRule = createComposeRule()

/** Renders a [PinField] whose visibility toggle is actually wired up. */
private fun setPinField(initiallyVisible: Boolean = false) {
composeTestRule.setContent {
LibravaultTheme {
// `remember` is load-bearing: without it every recomposition
// rebuilds the state and the toggle silently snaps back, so the
// relabel tests below fail against correct production code.
var visible by remember { mutableStateOf(initiallyVisible) }
PinField(
value = "1234",
onValueChange = {},
visible = visible,
onVisibleChange = { visible = it },
isError = false,
supportingText = null,
)
}
}
}

@Test
fun `toggle names the thing it toggles, not just the verb`() {
setPinField(initiallyVisible = false)

val labels = composeTestRule.clickableLabels()
assertTrue(
"The show/hide control must say what it acts on. \"Show\" alone is a verb with no " +
"object — TalkBack reads \"Show, button\" on a screen with several buttons. Labels: $labels",
labels.any { it.equals("Show PIN", ignoreCase = true) },
)
assertTrue(
"The bare verb must not be the announcement. Labels: $labels",
labels.none { it.trim().equals("Show", ignoreCase = true) },
)
}

/**
* The label must track the state, and the *old* label must be gone.
*
* A control announcing both, or announcing "Show PIN" while the PIN is
* already visible, is exactly as wrong as one with no label — and a
* presence-only assertion would pass in both cases.
*/
@Test
fun `toggle relabels itself when the PIN becomes visible`() {
setPinField(initiallyVisible = false)

composeTestRule.onNodeWithContentDescription("Show PIN").performClick()

val labels = composeTestRule.clickableLabels()
assertTrue(
"After revealing the PIN the control must offer to hide it. Labels: $labels",
labels.any { it.equals("Hide PIN", ignoreCase = true) },
)
assertTrue(
"After revealing the PIN nothing should still offer to show it. Labels: $labels",
labels.none { it.equals("Show PIN", ignoreCase = true) },
)
}

@Test
fun `toggle relabels itself when the PIN is hidden again`() {
setPinField(initiallyVisible = true)

composeTestRule.onNodeWithContentDescription("Hide PIN").performClick()

val labels = composeTestRule.clickableLabels()
assertEquals(
"Toggling back must restore the show affordance exactly once. Labels: $labels",
1,
labels.count { it.equals("Show PIN", ignoreCase = true) },
)
}

@Test
fun `every clickable control in the field is labelled`() {
setPinField()

val nodes = composeTestRule.onAllNodes(hasClickAction()).fetchSemanticsNodes()
assertTrue(
"No clickable nodes found — the harness rendered nothing, so this would pass vacuously.",
nodes.isNotEmpty(),
)
assertEquals(
"Unlabelled controls in the PIN field: TalkBack announces these as a bare \"button\".",
0,
composeTestRule.clickableLabels().count { it.isEmpty() },
)
}

/** The accessible name Compose exposes: `contentDescription`, else merged `Text`. */
private fun ComposeContentTestRule.clickableLabels(): List<String> =
onAllNodes(hasClickAction()).fetchSemanticsNodes().map { node ->
val description = node.config.getOrNull(SemanticsProperties.ContentDescription)
?.joinToString(" ")
?.trim()
.orEmpty()
val text = node.config.getOrNull(SemanticsProperties.Text)
?.joinToString(" ") { it.text }
?.trim()
.orEmpty()
description.ifEmpty { text }
}
}
Loading