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
@@ -0,0 +1,94 @@
package dev.scuttle.inventory.ui

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.requiredWidth
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.semantics.SemanticsActions
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.text.TextLayoutResult
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.dp
import dev.scuttle.inventory.ui.common.SortOrder
import dev.scuttle.inventory.ui.products.ProductFilterSortRow
import org.junit.Rule
import org.junit.Test

/**
* The filter/sort row had the same defect as the product card (#31): the two chips are sized to
* their content and take their width first, so the sort label — the only flexible thing in the
* row — was left with the remainder and ellipsized down to "…". It only shows at font scales
* above ~1.3, which is why the flow tests, which run at the device's scale, never caught it.
*/
class ProductFilterSortRowTest {
@get:Rule
val composeRule = createComposeRule()

private fun renderAtFontScale(fontScale: Float) {
composeRule.setContent {
val base = LocalDensity.current
CompositionLocalProvider(
LocalDensity provides Density(density = base.density, fontScale = fontScale),
) {
MaterialTheme {
// A narrow-but-ordinary phone width, so the assertion isn't a property of
// whichever device happens to run the suite.
Box(Modifier.requiredWidth(360.dp)) {
ProductFilterSortRow(
mandatoryOnly = false,
outOfStockOnly = false,
sort = SortOrder.NAME_ASC,
onToggleMandatory = {},
onToggleOutOfStock = {},
onSortSelect = {},
)
}
}
}
}
}

private fun sortLabelLayout(): TextLayoutResult {
val layouts = mutableListOf<TextLayoutResult>()
composeRule
.onNodeWithTag("sort-menu")
.fetchSemanticsNode()
.config[SemanticsActions.GetTextLayoutResult]
.action
?.invoke(layouts)
return layouts.first()
}

/**
* Oracle: every character of the label is actually painted. `hasVisualOverflow` is not
* usable here — it reports true even when the text got the width it asked for (301.5px
* wanted, 302px granted), so it would fail a perfectly readable label.
*/
private fun truncatedChars(): Int {
val layout = sortLabelLayout()
val visible = layout.getLineEnd(layout.lineCount - 1, visibleEnd = true)
return layout.layoutInput.text.length - visible
}

@Test
fun sort_label_is_readable_at_default_font_scale() {
renderAtFontScale(1.0f)

assert(truncatedChars() == 0) {
"Sort label lost ${truncatedChars()} characters to ellipsis at font scale 1.0"
}
}

@Test
fun sort_label_is_readable_at_large_font_scale() {
renderAtFontScale(1.6f)

assert(truncatedChars() == 0) {
"Sort label lost ${truncatedChars()} characters to ellipsis at font scale 1.6 — " +
"the filter chips are starving it of width"
}
}
}
67 changes: 49 additions & 18 deletions app/src/main/java/dev/scuttle/inventory/ui/products/ProductsPane.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,49 @@ import dev.scuttle.inventory.R
import dev.scuttle.inventory.data.dto.ProductDto
import dev.scuttle.inventory.ui.common.SnackbarErrorEffect
import dev.scuttle.inventory.ui.common.SortMenu
import dev.scuttle.inventory.ui.common.SortOrder
import dev.scuttle.inventory.ui.theme.FrostCard

/**
* The mandatory / out-of-stock filter chips and the sort menu.
*
* Extracted so it can be rendered on its own at a forced font scale — the sort label is the part
* that breaks, and only at font scales the flow tests can't reach through a real Activity.
*/
@OptIn(ExperimentalLayoutApi::class)
@Composable
internal fun ProductFilterSortRow(
mandatoryOnly: Boolean,
outOfStockOnly: Boolean,
sort: SortOrder,
onToggleMandatory: () -> Unit,
onToggleOutOfStock: () -> Unit,
onSortSelect: (SortOrder) -> Unit,
modifier: Modifier = Modifier,
) {
// FlowRow, not Row: the chips are sized to their content and are measured first, so in a Row
// the sort label got only the leftover width — already too little at 360dp, and less again as
// the font scales up. Here every item keeps its width and the row wraps instead.
FlowRow(
modifier = modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.spacedBy(8.dp),
verticalArrangement = Arrangement.spacedBy(8.dp),
itemVerticalAlignment = Alignment.CenterVertically,
) {
FilterChip(
selected = mandatoryOnly,
onClick = onToggleMandatory,
label = { Text(stringResource(R.string.products_pane_filter_mandatory)) },
)
FilterChip(
selected = outOfStockOnly,
onClick = onToggleOutOfStock,
label = { Text(stringResource(R.string.products_pane_filter_out_of_stock)) },
)
SortMenu(current = sort, onSelect = onSortSelect)
}
}

@OptIn(ExperimentalMaterial3Api::class, ExperimentalLayoutApi::class)
@Composable
fun ProductsPane(
Expand Down Expand Up @@ -136,24 +177,14 @@ fun ProductsPane(
singleLine = true,
modifier = Modifier.fillMaxWidth().testTag("product-filter"),
)
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
FilterChip(
selected = state.mandatoryOnly,
onClick = viewModel::toggleMandatoryOnly,
label = { Text(stringResource(R.string.products_pane_filter_mandatory)) },
)
FilterChip(
selected = state.outOfStockOnly,
onClick = viewModel::toggleOutOfStockOnly,
label = { Text(stringResource(R.string.products_pane_filter_out_of_stock)) },
)
Spacer(Modifier.weight(1f))
SortMenu(current = state.sort, onSelect = viewModel::setSort)
}
ProductFilterSortRow(
mandatoryOnly = state.mandatoryOnly,
outOfStockOnly = state.outOfStockOnly,
sort = state.sort,
onToggleMandatory = viewModel::toggleMandatoryOnly,
onToggleOutOfStock = viewModel::toggleOutOfStockOnly,
onSortSelect = viewModel::setSort,
)
}

if (state.products.isEmpty() && !state.loading) {
Expand Down