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

@ -16,6 +16,9 @@
package com.google.samples.apps.nowinandroid.core.ui 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.AnimatedVisibility
import androidx.compose.animation.core.animateFloatAsState import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween import androidx.compose.animation.core.tween
@ -56,6 +59,7 @@ import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.window.layout.WindowMetricsCalculator 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) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
fun <T> AdaptiveScaffold( fun <T> AdaptiveScaffold(
@ -123,12 +162,12 @@ fun <T> AdaptiveScaffold(
content: @Composable (padding: PaddingValues) -> Unit, content: @Composable (padding: PaddingValues) -> Unit,
) { ) {
val context = LocalContext.current 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 widthDp = metrics.bounds.width() / context.resources.displayMetrics.density
val movableContent = remember(content) { val movableContent = remember(content) {
movableContentOf(content) movableContentOf(content)
} }
when { when {
widthDp >= 1240f -> DrawerScaffold( widthDp >= 1240f -> DrawerScaffold(
modifier = modifier, modifier = modifier,
@ -205,6 +244,7 @@ private fun <T> BottomBarScaffold(
bottomBar = { bottomBar = {
NavigationBar( NavigationBar(
containerColor = colors.bottomBarContainerColor, containerColor = colors.bottomBarContainerColor,
modifier = Modifier.testTag("adaptiveScaffold:navigationBar"),
) { ) {
navigationItems.forEach { item -> navigationItems.forEach { item ->
NavigationBarItem( NavigationBarItem(
@ -267,7 +307,9 @@ private fun <T> RailScaffold(
Row(modifier = modifier) { Row(modifier = modifier) {
NavigationRail( NavigationRail(
modifier = Modifier.safeDrawingPadding(), modifier = Modifier
.safeDrawingPadding()
.testTag("adaptiveScaffold:navigationRail"),
containerColor = colors.railContainerColor, containerColor = colors.railContainerColor,
) { ) {
navigationItems.forEach { item -> navigationItems.forEach { item ->
@ -337,6 +379,9 @@ private fun <T> DrawerScaffold(
drawerContent = { drawerContent = {
PermanentDrawerSheet( PermanentDrawerSheet(
drawerContainerColor = colors.drawerContainerColor, drawerContainerColor = colors.drawerContainerColor,
modifier = Modifier
.safeDrawingPadding()
.testTag("adaptiveScaffold:permanentDrawer"),
) { ) {
navigationItems.forEach { item -> navigationItems.forEach { item ->
NavigationDrawerItem( NavigationDrawerItem(
@ -383,37 +428,8 @@ private fun <T> DrawerScaffold(
} }
} }
@OptIn(ExperimentalMaterial3Api::class) private fun Context.getActivity(): Activity? = when (this) {
@Composable is Activity -> this
fun <T> AdaptiveScaffold( is ContextWrapper -> baseContext.getActivity()
navigator: AdaptiveScaffoldNavigator, else -> null
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,
)
} }

Loading…
Cancel
Save