Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,85 @@
/**
* 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)

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

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

assertTitlesAreNotEllipsized(ENGLISH_TITLE)
}

private fun assertTitlesAreNotEllipsized(title: String) {
val nodes = composeTestRule.onAllNodes(hasText(title), useUnmergedTree = true).fetchSemanticsNodes()
assertTrue("Expected the title to be displayed", nodes.isNotEmpty())
nodes.forEach { node ->
val results = mutableListOf<TextLayoutResult>()
node.config.getOrNull(SemanticsActions.GetTextLayoutResult)?.action?.invoke(results)
val layout = results.single()
Comment thread
peterInTown marked this conversation as resolved.
Resolved
Comment thread
peterInTown marked this conversation as resolved.
Resolved
assertFalse("Title '$title' is ellipsized", layout.isLineEllipsized(0))
assertFalse("Title '$title' overflows its width", layout.didOverflowWidth)
}
}

private fun setContent(title: String, layoutDirection: LayoutDirection) {
composeTestRule.setContent {
BpkTheme {
CompositionLocalProvider(LocalLayoutDirection provides layoutDirection) {
BpkTopNavBar(
state = rememberFixedTopAppBarState(TopNavBarStatus.Expanded),
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
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