Skip to content

Commit

Permalink
BottomNavigationBarTests.kt test fixes (#66)
Browse files Browse the repository at this point in the history
** This has some extra addons `testTags` in the MainActivity.kt file.
  • Loading branch information
iamjosephmj authored May 26, 2021
1 parent 8aa70bb commit 219eed4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 51 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,5 @@ lint/tmp/
# lint/reports/

# Android Profiling
*.hprof
*.hprof
/.idea/inspectionProfiles/Project_Default.xml
Original file line number Diff line number Diff line change
@@ -1,98 +1,80 @@
package com.guru.composecookbook

import androidx.activity.ComponentActivity
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.test.*
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import com.guru.composecookbook.theme.AppThemeState
import org.junit.Before
import org.junit.Rule
import org.junit.Test

class BottomNavigationBarTests {

@get: Rule
val composeAndroidTestRule = createAndroidComposeRule<ComponentActivity>()
val composeAndroidTestRule = createAndroidComposeRule<MainActivity>()

private val bottomNavigationBarTestTag: String = "bottom_navigation_bar"


@ExperimentalFoundationApi
@ExperimentalMaterialApi
@Before
fun setUp() {
composeAndroidTestRule.setContent {
MainAppContent(appThemeState = mutableStateOf(AppThemeState()))
}
}

@Test
fun bottomNavigationBarMustHaveFiveEntries() {
composeAndroidTestRule.apply {
onNodeWithTag(bottomNavigationBarTestTag)
.onChild()
.onChildren()
.filter(hasClickAction())
.assertCountEquals(4)
.assertCountEquals(5)
}
}

@Test
fun bottomNavigationBarMustHaveHomeEntry() {
composeAndroidTestRule.apply {
onNodeWithTag(bottomNavigationBarTestTag)
.onChildren()
.filterToOne(matcher = hasText("Home").and(hasClickAction()))
onNodeWithTag("Home")
.assertIsSelectable()
.assertExists()
}
}

@Test
fun bottomNavigationBarMustHaveWidgetsEntry() {
composeAndroidTestRule.apply {
onNodeWithTag(bottomNavigationBarTestTag)
.onChildren()
.filterToOne(matcher = hasText("Widgets").and(hasClickAction()))
onNodeWithTag("Widgets")
.assertIsSelectable()
.assertExists()
}
}

@Test
fun bottomNavigationBarMustHaveAnimEntry() {
composeAndroidTestRule.apply {
onNodeWithTag(bottomNavigationBarTestTag)
.onChildren()
.filterToOne(matcher = hasText("Anim").and(hasClickAction()))
onNodeWithTag("Anim")
.assertIsSelectable()
.assertExists()

}
}

@Test
fun bottomNavigationBarMustHaveDemoUiEntry() {
composeAndroidTestRule.apply {
onNodeWithTag(bottomNavigationBarTestTag)
.onChildren()
.filterToOne(matcher = hasText("DemoUI").and(hasClickAction()))
onNodeWithTag("DemoUI")
.assertIsSelectable()
.assertExists()
}
}

@Test
fun bottomNavigationBarMustHaveTemplateEntry() {
composeAndroidTestRule.apply {
onNodeWithTag(bottomNavigationBarTestTag)
.onChildren()
.filterToOne(matcher = hasText("Template").and(hasClickAction()))
.assertExists()
composeAndroidTestRule.apply {
onNodeWithTag("Template")
.assertIsSelectable()
.assertExists()
}
}
}

@Test
fun bottomNavigationBartHomeIsSelectedByDefault() {
composeAndroidTestRule.apply {
onNodeWithText("Home")
.assertHasClickAction()
onNodeWithTag("Home")
.performClick()
.assertIsSelected()
.onSiblings()
.assertAny(isNotSelected())
Expand All @@ -102,7 +84,7 @@ class BottomNavigationBarTests {
@Test
fun whenBottomNavEntryHomeClickedTheHomeScreenIsDisplayed() {
composeAndroidTestRule.apply {
onNodeWithText("Home")
onNodeWithTag("Home")
.performClick() // Click on the Home entry in the bottom navigation bar
.onAncestors() // returns BottomNavigation, BottomNavigationContent, Column, MainAppContent, BaseView
.onLast() // returns BaseView - which is the root
Expand All @@ -117,7 +99,7 @@ class BottomNavigationBarTests {
fun whenBottomNavEntryWidgetsClickedTheWidgetScreenIsDisplayed() {
composeAndroidTestRule.apply {

onNodeWithText("Widgets")
onNodeWithTag("Widgets")
.performClick()

// The widgets screen contains infinite animations.
Expand All @@ -134,7 +116,7 @@ class BottomNavigationBarTests {
@Test
fun whenBottomNavBarAnimEntryClickedAnimationScreenIsDisplayed() {
composeAndroidTestRule.apply {
onNodeWithText("Anim")
onNodeWithTag("Anim")
.performClick()


Expand All @@ -149,7 +131,7 @@ class BottomNavigationBarTests {
@Test
fun whenBottomNavBarDemoUiEntryClickedDemoUiScreenIsDisplayed() {
composeAndroidTestRule.apply {
onNodeWithText("DemoUI")
onNodeWithTag("DemoUI")
.performClick()


Expand All @@ -164,13 +146,10 @@ class BottomNavigationBarTests {
@Test
fun whenBottomNavigationBarTemplateEntryClickedTemplateScreenIsDisplayed() {
composeAndroidTestRule.apply {
onNodeWithText("Template")
onNodeWithTag("Template")
.performClick()


onRoot()
.onChildren()
.filterToOne(hasTestTag("Template Screen"))
onNodeWithTag("Template Screen")
.assertExists()
}
}
Expand Down
15 changes: 11 additions & 4 deletions app/src/main/java/com/guru/composecookbook/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ fun BottomNavigationContent(
animate = false
},
label = { Text(text = stringResource(id = R.string.navigation_item_home)) },
modifier = Modifier.testTag("Home")
)
BottomNavigationItem(
icon = {
Expand All @@ -146,7 +147,8 @@ fun BottomNavigationContent(
homeScreenState.value = BottomNavType.WIDGETS
animate = false
},
label = { Text(text = stringResource(id = R.string.navigation_item_widgets)) }
label = { Text(text = stringResource(id = R.string.navigation_item_widgets)) },
modifier = Modifier.testTag("Widgets")
)
BottomNavigationItem(
icon = {
Expand All @@ -162,7 +164,9 @@ fun BottomNavigationContent(
homeScreenState.value = BottomNavType.ANIMATION
animate = true
},
label = { Text(text = stringResource(id = R.string.navigation_item_animation)) }
label = { Text(text = stringResource(id = R.string.navigation_item_animation)) },
modifier = Modifier.testTag("Anim")

)
BottomNavigationItem(
icon = {
Expand All @@ -178,7 +182,8 @@ fun BottomNavigationContent(
homeScreenState.value = BottomNavType.DEMOUI
animate = false
},
label = { Text(text = stringResource(id = R.string.navigation_item_demoui)) }
label = { Text(text = stringResource(id = R.string.navigation_item_demoui)) },
modifier = Modifier.testTag("DemoUI")
)
BottomNavigationItem(
icon = {
Expand All @@ -195,7 +200,9 @@ fun BottomNavigationContent(
homeScreenState.value = BottomNavType.TEMPLATE
animate = false
},
label = { Text(text = stringResource(id = R.string.navigation_item_profile)) }
label = { Text(text = stringResource(id = R.string.navigation_item_profile)) },
modifier = Modifier.testTag("Template")

)
}
}
Expand Down

0 comments on commit 219eed4

Please sign in to comment.