Fix NavigationUiTest

Change-Id: Ied714177a358fd8e908a392f56aeea18d4f7854a
feature/adaptive_scaffold
Miłosz Moczkowski 1 year ago
parent 971a2f36e3
commit 2b828655d8

@ -102,8 +102,9 @@ class NavigationUiTest {
}
}
composeTestRule.onNodeWithTag("NiaBottomBar").assertIsDisplayed()
composeTestRule.onNodeWithTag("NiaNavRail").assertDoesNotExist()
composeTestRule.onNodeWithTag("adaptiveScaffold:navigationBar").assertIsDisplayed()
composeTestRule.onNodeWithTag("adaptiveScaffold:navigationRail").assertDoesNotExist()
composeTestRule.onNodeWithTag("adaptiveScaffold:permanentDrawer").assertDoesNotExist()
}
@Test
@ -122,8 +123,9 @@ class NavigationUiTest {
}
}
composeTestRule.onNodeWithTag("NiaNavRail").assertIsDisplayed()
composeTestRule.onNodeWithTag("NiaBottomBar").assertDoesNotExist()
composeTestRule.onNodeWithTag("adaptiveScaffold:navigationRail").assertIsDisplayed()
composeTestRule.onNodeWithTag("adaptiveScaffold:navigationBar").assertDoesNotExist()
composeTestRule.onNodeWithTag("adaptiveScaffold:permanentDrawer").assertDoesNotExist()
}
@Test
@ -142,8 +144,9 @@ class NavigationUiTest {
}
}
composeTestRule.onNodeWithTag("NiaNavRail").assertIsDisplayed()
composeTestRule.onNodeWithTag("NiaBottomBar").assertDoesNotExist()
composeTestRule.onNodeWithTag("adaptiveScaffold:navigationRail").assertIsDisplayed()
composeTestRule.onNodeWithTag("adaptiveScaffold:navigationBar").assertDoesNotExist()
composeTestRule.onNodeWithTag("adaptiveScaffold:permanentDrawer").assertDoesNotExist()
}
@Test
@ -162,8 +165,9 @@ class NavigationUiTest {
}
}
composeTestRule.onNodeWithTag("NiaBottomBar").assertIsDisplayed()
composeTestRule.onNodeWithTag("NiaNavRail").assertDoesNotExist()
composeTestRule.onNodeWithTag("adaptiveScaffold:navigationBar").assertIsDisplayed()
composeTestRule.onNodeWithTag("adaptiveScaffold:navigationRail").assertDoesNotExist()
composeTestRule.onNodeWithTag("adaptiveScaffold:permanentDrawer").assertDoesNotExist()
}
@Test
@ -182,8 +186,9 @@ class NavigationUiTest {
}
}
composeTestRule.onNodeWithTag("NiaNavRail").assertIsDisplayed()
composeTestRule.onNodeWithTag("NiaBottomBar").assertDoesNotExist()
composeTestRule.onNodeWithTag("adaptiveScaffold:navigationRail").assertIsDisplayed()
composeTestRule.onNodeWithTag("adaptiveScaffold:navigationBar").assertDoesNotExist()
composeTestRule.onNodeWithTag("adaptiveScaffold:permanentDrawer").assertDoesNotExist()
}
@Test
@ -202,14 +207,15 @@ class NavigationUiTest {
}
}
composeTestRule.onNodeWithTag("NiaNavRail").assertIsDisplayed()
composeTestRule.onNodeWithTag("NiaBottomBar").assertDoesNotExist()
composeTestRule.onNodeWithTag("adaptiveScaffold:navigationRail").assertIsDisplayed()
composeTestRule.onNodeWithTag("adaptiveScaffold:navigationBar").assertDoesNotExist()
composeTestRule.onNodeWithTag("adaptiveScaffold:permanentDrawer").assertDoesNotExist()
}
@Test
fun compactWidth_expandedHeight_showsNavigationBar() {
composeTestRule.setContent {
TestHarness(size = DpSize(400.dp, 1000.dp)) {
TestHarness(size = DpSize(400.dp, 900.dp)) {
BoxWithConstraints {
NiaApp(
windowSizeClass = WindowSizeClass.calculateFromSize(
@ -222,14 +228,15 @@ class NavigationUiTest {
}
}
composeTestRule.onNodeWithTag("NiaBottomBar").assertIsDisplayed()
composeTestRule.onNodeWithTag("NiaNavRail").assertDoesNotExist()
composeTestRule.onNodeWithTag("adaptiveScaffold:navigationBar").assertIsDisplayed()
composeTestRule.onNodeWithTag("adaptiveScaffold:navigationRail").assertDoesNotExist()
composeTestRule.onNodeWithTag("adaptiveScaffold:permanentDrawer").assertDoesNotExist()
}
@Test
fun mediumWidth_expandedHeight_showsNavigationRail() {
composeTestRule.setContent {
TestHarness(size = DpSize(610.dp, 1000.dp)) {
TestHarness(size = DpSize(610.dp, 900.dp)) {
BoxWithConstraints {
NiaApp(
windowSizeClass = WindowSizeClass.calculateFromSize(
@ -242,14 +249,15 @@ class NavigationUiTest {
}
}
composeTestRule.onNodeWithTag("NiaNavRail").assertIsDisplayed()
composeTestRule.onNodeWithTag("NiaBottomBar").assertDoesNotExist()
composeTestRule.onNodeWithTag("adaptiveScaffold:navigationRail").assertIsDisplayed()
composeTestRule.onNodeWithTag("adaptiveScaffold:navigationBar").assertDoesNotExist()
composeTestRule.onNodeWithTag("adaptiveScaffold:permanentDrawer").assertDoesNotExist()
}
@Test
fun expandedWidth_expandedHeight_showsNavigationRail() {
composeTestRule.setContent {
TestHarness(size = DpSize(900.dp, 1000.dp)) {
TestHarness(size = DpSize(900.dp, 900.dp)) {
BoxWithConstraints {
NiaApp(
windowSizeClass = WindowSizeClass.calculateFromSize(
@ -262,7 +270,8 @@ class NavigationUiTest {
}
}
composeTestRule.onNodeWithTag("NiaNavRail").assertIsDisplayed()
composeTestRule.onNodeWithTag("NiaBottomBar").assertDoesNotExist()
composeTestRule.onNodeWithTag("adaptiveScaffold:navigationRail").assertIsDisplayed()
composeTestRule.onNodeWithTag("adaptiveScaffold:navigationBar").assertDoesNotExist()
composeTestRule.onNodeWithTag("adaptiveScaffold:permanentDrawer").assertDoesNotExist()
}
}

@ -16,6 +16,9 @@
package com.google.samples.apps.nowinandroid.core.ui
import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
@ -56,6 +59,7 @@ import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.dp
import androidx.window.layout.WindowMetricsCalculator
@ -105,6 +109,41 @@ object AdaptiveScaffoldNavigationComponentDefaults {
)
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun <T> AdaptiveScaffold(
navigator: AdaptiveScaffoldNavigator,
modifier: Modifier = Modifier,
navigationItems: List<T>,
navigationItemTitle: @Composable (item: T, isSelected: Boolean) -> Unit,
navigationItemIcon: @Composable (item: T, isSelected: Boolean) -> Unit,
isItemSelected: @Composable (item: T) -> Boolean,
onNavigationItemClick: (item: T) -> Unit,
topBar: @Composable () -> Unit = {},
snackbarHost: @Composable () -> Unit = {},
colors: AdaptiveScaffoldNavigationComponentColors = AdaptiveScaffoldNavigationComponentDefaults.colors(),
contentWindowInsets: WindowInsets = ScaffoldDefaults.contentWindowInsets,
content: @Composable (padding: PaddingValues) -> Unit,
) {
val isDetailsPaneVisible by navigator.isVisible.collectAsState(initial = false)
AdaptiveScaffold(
modifier = modifier,
navigationItems = navigationItems,
navigationItemTitle = navigationItemTitle,
navigationItemIcon = navigationItemIcon,
isItemSelected = isItemSelected,
onNavigationItemClick = onNavigationItemClick,
topBar = topBar,
snackbarHost = snackbarHost,
colors = colors,
contentWindowInsets = contentWindowInsets,
isDetailsPaneVisible = isDetailsPaneVisible,
detailsPane = navigator.detailsPaneContent,
content = content,
)
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun <T> AdaptiveScaffold(
@ -123,12 +162,12 @@ fun <T> AdaptiveScaffold(
content: @Composable (padding: PaddingValues) -> Unit,
) {
val context = LocalContext.current
val metrics = WindowMetricsCalculator.getOrCreate().computeCurrentWindowMetrics(context)
val activity = context.getActivity() ?: throw IllegalStateException("Activity not found")
val metrics = WindowMetricsCalculator.getOrCreate().computeCurrentWindowMetrics(activity)
val widthDp = metrics.bounds.width() / context.resources.displayMetrics.density
val movableContent = remember(content) {
movableContentOf(content)
}
when {
widthDp >= 1240f -> DrawerScaffold(
modifier = modifier,
@ -205,6 +244,7 @@ private fun <T> BottomBarScaffold(
bottomBar = {
NavigationBar(
containerColor = colors.bottomBarContainerColor,
modifier = Modifier.testTag("adaptiveScaffold:navigationBar"),
) {
navigationItems.forEach { item ->
NavigationBarItem(
@ -267,7 +307,9 @@ private fun <T> RailScaffold(
Row(modifier = modifier) {
NavigationRail(
modifier = Modifier.safeDrawingPadding(),
modifier = Modifier
.safeDrawingPadding()
.testTag("adaptiveScaffold:navigationRail"),
containerColor = colors.railContainerColor,
) {
navigationItems.forEach { item ->
@ -337,6 +379,9 @@ private fun <T> DrawerScaffold(
drawerContent = {
PermanentDrawerSheet(
drawerContainerColor = colors.drawerContainerColor,
modifier = Modifier
.safeDrawingPadding()
.testTag("adaptiveScaffold:permanentDrawer"),
) {
navigationItems.forEach { item ->
NavigationDrawerItem(
@ -383,37 +428,8 @@ private fun <T> DrawerScaffold(
}
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun <T> AdaptiveScaffold(
navigator: AdaptiveScaffoldNavigator,
modifier: Modifier = Modifier,
navigationItems: List<T>,
navigationItemTitle: @Composable (item: T, isSelected: Boolean) -> Unit,
navigationItemIcon: @Composable (item: T, isSelected: Boolean) -> Unit,
isItemSelected: @Composable (item: T) -> Boolean,
onNavigationItemClick: (item: T) -> Unit,
topBar: @Composable () -> Unit = {},
snackbarHost: @Composable () -> Unit = {},
colors: AdaptiveScaffoldNavigationComponentColors = AdaptiveScaffoldNavigationComponentDefaults.colors(),
contentWindowInsets: WindowInsets = ScaffoldDefaults.contentWindowInsets,
content: @Composable (padding: PaddingValues) -> Unit,
) {
val isDetailsPaneVisible by navigator.isVisible.collectAsState(initial = false)
AdaptiveScaffold(
modifier = modifier,
navigationItems = navigationItems,
navigationItemTitle = navigationItemTitle,
navigationItemIcon = navigationItemIcon,
isItemSelected = isItemSelected,
onNavigationItemClick = onNavigationItemClick,
topBar = topBar,
snackbarHost = snackbarHost,
colors = colors,
contentWindowInsets = contentWindowInsets,
isDetailsPaneVisible = isDetailsPaneVisible,
detailsPane = navigator.detailsPaneContent,
content = content,
)
private fun Context.getActivity(): Activity? = when (this) {
is Activity -> this
is ContextWrapper -> baseContext.getActivity()
else -> null
}

Loading…
Cancel
Save