Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* Backpack for Android - Skyscanner's Design System
*
* Copyright 2018 - 2026 Skyscanner Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.skyscanner.backpack.compose.navigationbar

import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.semantics.SemanticsActions
import androidx.compose.ui.semantics.getOrNull
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.text.TextLayoutResult
import androidx.compose.ui.unit.LayoutDirection
import net.skyscanner.backpack.compose.theme.BpkTheme
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test

class BpkTopNavBarTitleTest {

@get:Rule
val composeTestRule = createComposeRule()

@Test
fun givenShortArabicTitle_whenExpandedInRtl_thenTitleIsNotEllipsized() {
setContent(title = ARABIC_TITLE, layoutDirection = LayoutDirection.Rtl, status = TopNavBarStatus.Expanded)

assertTitlesAreNotEllipsized(ARABIC_TITLE)
}

@Test
fun givenShortArabicTitle_whenCollapsedInRtl_thenTitleIsNotEllipsized() {
setContent(title = ARABIC_TITLE, layoutDirection = LayoutDirection.Rtl, status = TopNavBarStatus.Collapsed)

assertTitlesAreNotEllipsized(ARABIC_TITLE)
}
Comment thread
peterInTown marked this conversation as resolved.
Resolved

@Test
fun givenShortEnglishTitle_whenExpanded_thenTitleIsNotEllipsized() {
setContent(title = ENGLISH_TITLE, layoutDirection = LayoutDirection.Ltr, status = TopNavBarStatus.Expanded)

assertTitlesAreNotEllipsized(ENGLISH_TITLE)
}

private fun assertTitlesAreNotEllipsized(title: String) {
val layouts = composeTestRule.onAllNodes(hasText(title), useUnmergedTree = true)
.fetchSemanticsNodes()
.mapNotNull { node ->
val results = mutableListOf<TextLayoutResult>()
node.config.getOrNull(SemanticsActions.GetTextLayoutResult)?.action?.invoke(results)
results.firstOrNull()
}
assertTrue("Expected a text layout for the title '$title'", layouts.isNotEmpty())
layouts.forEach { layout ->
assertFalse("Title '$title' is ellipsized", layout.isLineEllipsized(0))
assertFalse("Title '$title' overflows its width", layout.didOverflowWidth)
}
}

private fun setContent(title: String, layoutDirection: LayoutDirection, status: TopNavBarStatus) {
composeTestRule.setContent {
BpkTheme {
CompositionLocalProvider(LocalLayoutDirection provides layoutDirection) {
BpkTopNavBar(
state = rememberFixedTopAppBarState(status),
navIcon = NavIcon.Close(contentDescription = "Close", onClick = {}),
title = title,
style = NavBarStyle.SurfaceContrast,
)
Comment thread
peterInTown marked this conversation as resolved.
Resolved
Comment thread
peterInTown marked this conversation as resolved.
Resolved
}
}
}
}

private companion object {
const val ARABIC_TITLE = "ابحث عن رحلة"
const val ENGLISH_TITLE = "Search flights"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/**
* Backpack for Android - Skyscanner's Design System
*
* Copyright 2018 - 2026 Skyscanner Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.skyscanner.backpack.compose.segmentedcontrol

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.width
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.SemanticsActions
import androidx.compose.ui.semantics.getOrNull
import androidx.compose.ui.test.getUnclippedBoundsInRoot
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.text.TextLayoutResult
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.width
import net.skyscanner.backpack.compose.theme.BpkTheme
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test

class BpkSegmentedControlLabelsTest {

@get:Rule
val composeTestRule = createComposeRule()

@Test
fun givenLabelsThatDoNotFitEqualWidths_whenShown_thenNoLabelIsEllipsized() {
setContent(GERMAN_LABELS)

GERMAN_LABELS.forEach { assertLabelIsNotEllipsized(it) }
}

@Test
fun givenLabelsThatFitEqualWidths_whenShown_thenButtonsHaveEqualWidths() {
setContent(ENGLISH_LABELS)

val widths = ENGLISH_LABELS.map { buttonWidth(it) }
widths.forEach { assertEquals(widths.first().value, it.value, WIDTH_TOLERANCE_DP) }
}

@Test
fun givenLabelsThatDoNotFitEqualWidths_whenShown_thenLongestLabelGetsTheMostWidth() {
setContent(GERMAN_LABELS)

val widths = GERMAN_LABELS.map { buttonWidth(it) }
assertTrue(widths.first() > widths[1])
assertTrue(widths.first() > widths[2])
}

@Test
fun givenLabelsThatCannotAllFit_whenShown_thenButtonsStillFillTheControlWidth() {
setContent(VERY_LONG_LABELS, width = NARROW_WIDTH)

val total = VERY_LONG_LABELS.sumOf { buttonWidth(it).value.toDouble() }
assertTrue(total <= NARROW_WIDTH.value)
assertTrue(total >= NARROW_WIDTH.value - DIVIDERS_AND_TOLERANCE_DP)
Comment thread
peterInTown marked this conversation as resolved.
Resolved
}
Comment thread
peterInTown marked this conversation as resolved.
Resolved

@Test
fun givenOneLabelThatCannotFit_whenShown_thenOnlyThatLabelIsEllipsized() {
setContent(ONE_VERY_LONG_LABEL)

assertTrue(isLabelEllipsized(ONE_VERY_LONG_LABEL.first()))
assertFalse(isLabelEllipsized(ONE_VERY_LONG_LABEL[1]))
assertFalse(isLabelEllipsized(ONE_VERY_LONG_LABEL[2]))
}

private fun buttonWidth(label: String): Dp =
composeTestRule.onNodeWithText(label).getUnclippedBoundsInRoot().width
Comment thread
peterInTown marked this conversation as resolved.
Resolved

private fun assertLabelIsNotEllipsized(label: String) {
assertFalse("'$label' is ellipsized", isLabelEllipsized(label))
}

private fun isLabelEllipsized(label: String): Boolean {
val layout = composeTestRule.onAllNodes(hasText(label), useUnmergedTree = true)
.fetchSemanticsNodes()
.firstNotNullOfOrNull { node ->
val results = mutableListOf<TextLayoutResult>()
node.config.getOrNull(SemanticsActions.GetTextLayoutResult)?.action?.invoke(results)
results.firstOrNull()
}
return checkNotNull(layout) { "Expected a text layout for the label '$label'" }.isLineEllipsized(0)
}

private fun setContent(labels: List<String>, width: Dp = CONTROL_WIDTH) {
composeTestRule.setContent {
BpkTheme {
Box(modifier = Modifier.width(width)) {
BpkSegmentedControl(
buttonContents = labels,
onItemClick = {},
selectedIndex = 0,
type = BpkSegmentedControlStyle.SurfaceContrast,
)
}
}
}
}

private companion object {
val CONTROL_WIDTH = 343.dp
val NARROW_WIDTH = 240.dp
const val WIDTH_TOLERANCE_DP = 1f
const val DIVIDERS_AND_TOLERANCE_DP = 4f
val GERMAN_LABELS = listOf("Hin- und Rückflug", "Nur Hinflug", "Gabelflug")
val ENGLISH_LABELS = listOf("Return", "One way", "Multi-city")
val ONE_VERY_LONG_LABEL = listOf("Eine sehr lange Beschriftung die nicht passt", "Hinflug", "Gabelflug")
val VERY_LONG_LABELS = listOf("Hin- und Rückflug buchen", "Nur Hinflug buchen", "Gabelflug buchen")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.compose.animation.core.animateDpAsState
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.ExperimentalMaterial3Api
Expand Down Expand Up @@ -120,7 +121,7 @@ internal fun BpkTopNavBarImpl(
maxLines = 1,
style = BpkTheme.typography.heading2,
overflow = TextOverflow.Ellipsis,
modifier = Modifier.semantics { heading() },
modifier = Modifier.fillMaxWidth().semantics { heading() },
)
},
smallTitle = {
Expand All @@ -129,7 +130,7 @@ internal fun BpkTopNavBarImpl(
maxLines = 1,
style = BpkTheme.typography.heading4,
overflow = TextOverflow.Ellipsis,
modifier = Modifier.semantics { heading() },
modifier = Modifier.fillMaxWidth().semantics { heading() },
)
},
modifier = modifier
Expand Down
Loading
Loading