Skip to content

Commit

Permalink
Merge pull request #122 from tarkalabs/mahi/TuiTab_used_in_tab_bar
Browse files Browse the repository at this point in the history
TUITab was used in TUITabBar.
  • Loading branch information
rajajawahar authored Sep 12, 2023
2 parents d89a877 + 7012815 commit 79bd118
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 468 deletions.
2 changes: 1 addition & 1 deletion tarkaui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ publishing {
run {
groupId = "com.tarkalabs"
artifactId = getLibraryArtifactId()
version = "0.44-alpha"
version = "0.45-alpha"
artifact("$buildDir/outputs/aar/${getLibraryArtifactId()}-release.aar")
}
}
Expand Down
294 changes: 24 additions & 270 deletions tarkaui/src/androidTest/java/com/tarkalabs/uicomponents/TUITabBarTest.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package com.tarkalabs.uicomponents

import androidx.compose.material3.Text
import androidx.compose.ui.test.assertCountEquals
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsNotDisplayed
import androidx.compose.ui.test.assertIsNotSelected
import androidx.compose.ui.test.assertIsSelected
import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onAllNodesWithContentDescription
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTouchInput
import androidx.compose.ui.test.swipeLeft
import androidx.compose.ui.test.swipeRight
import com.tarkalabs.tarkaicons.Tabs24
import com.tarkalabs.tarkaicons.TarkaIcons
import com.tarkalabs.uicomponents.components.TUITabBar
import com.tarkalabs.uicomponents.components.TUITabBarTags
import com.tarkalabs.uicomponents.components.TUITabTags
import com.tarkalabs.uicomponents.components.TabItem
import org.junit.Rule
import org.junit.Test
Expand All @@ -34,9 +30,9 @@ class TUITabBarTest {
@Test
fun is_passed_tabs_shown() {
val tabItems = listOf(
TabItem(name = "Tab 1", content = {}),
TabItem(name = "Tab 2", content = {}),
TabItem(name = "Tab 3", content = {}),
TabItem(name = "Tab 1"),
TabItem(name = "Tab 2"),
TabItem(name = "Tab 3"),
)

composeTestRule.setContent {
Expand All @@ -48,9 +44,9 @@ class TUITabBarTest {
)
}

composeTestRule.onNodeWithTag("Tab 1 ${testTags.tabId}").assertIsDisplayed()
composeTestRule.onNodeWithTag("Tab 2 ${testTags.tabId}").assertIsDisplayed()
composeTestRule.onNodeWithTag("Tab 2 ${testTags.tabId}").assertIsDisplayed()
composeTestRule.onNodeWithTag("Tab 1 ${TUITabTags().parentTag}").assertIsDisplayed()
composeTestRule.onNodeWithTag("Tab 2 ${TUITabTags().parentTag}").assertIsDisplayed()
composeTestRule.onNodeWithTag("Tab 2 ${TUITabTags().parentTag}").assertIsDisplayed()
}

@Test
Expand All @@ -59,9 +55,9 @@ class TUITabBarTest {
val leadingIcon = TarkaIcons.Regular.Tabs24

val tabItems = listOf(
TabItem(name = "Tab 1", leadingTabIcon = leadingIcon, content = {}),
TabItem(name = "Tab 2", leadingTabIcon = leadingIcon, content = {}),
TabItem(name = "Tab 3", leadingTabIcon = leadingIcon, content = {}),
TabItem(name = "Tab 1", leadingTabIcon = leadingIcon),
TabItem(name = "Tab 2", leadingTabIcon = leadingIcon),
TabItem(name = "Tab 3", leadingTabIcon = leadingIcon),
)

composeTestRule.setContent {
Expand All @@ -77,36 +73,12 @@ class TUITabBarTest {
.assertCountEquals(3)
}

@Test
fun is_passed_content_shown() {
val tabItems = listOf(
TabItem(name = "Tab 1", content = { Text(text = "Content 1") }),
TabItem(name = "Tab 2", content = { Text(text = "Content 2") }),
TabItem(name = "Tab 3", content = { Text(text = "Content 3") }),
)

composeTestRule.setContent {
TUITabBar(
tabItems = tabItems,
selectedTabIndex = 0,
tags = testTags,
onTabChanged = {},
)
}

//Initially the content of the first tab should be shown due to the selected index is 0.
composeTestRule.onNodeWithText("Content 1").assertIsDisplayed()
//Other tab contents should not be exist
composeTestRule.onNodeWithText("Content 2").assertDoesNotExist()
composeTestRule.onNodeWithText("Content 3").assertDoesNotExist()
}

@Test
fun is_passed_content_not_shown_when_pager_disabled() {
val tabItems = listOf(
TabItem(name = "Tab 1", content = { Text(text = "Content 1") }),
TabItem(name = "Tab 2", content = { Text(text = "Content 2") }),
TabItem(name = "Tab 3", content = { Text(text = "Content 3") }),
TabItem(name = "Tab 1"),
TabItem(name = "Tab 2"),
TabItem(name = "Tab 3"),
)

composeTestRule.setContent {
Expand All @@ -127,9 +99,9 @@ class TUITabBarTest {
@Test
fun is_correct_tab_selected_based_on_selected_index_value() {
val tabItems = listOf(
TabItem(name = "Tab 1", content = {}),
TabItem(name = "Tab 2", content = {}),
TabItem(name = "Tab 3", content = {}),
TabItem(name = "Tab 1"),
TabItem(name = "Tab 2"),
TabItem(name = "Tab 3"),
)

composeTestRule.setContent {
Expand All @@ -142,125 +114,10 @@ class TUITabBarTest {
}

//Third tab should be shown due to the selected index 2
composeTestRule.onNodeWithTag("Tab 3 ${testTags.tabId}").assertIsSelected()
composeTestRule.onNodeWithTag("Tab 3 ${TUITabTags().parentTag}").assertIsSelected()
//other tabs should not be shown
composeTestRule.onNodeWithTag("Tab 1 ${testTags.tabId}").assertIsNotSelected()
composeTestRule.onNodeWithTag("Tab 2 ${testTags.tabId}").assertIsNotSelected()
}

@Test
fun is_pager_disabled_properly_based_on_param() {

val tabItems = listOf(
TabItem(name = "Tab 1", content = {}),
TabItem(name = "Tab 2", content = {}),
TabItem(name = "Tab 3", content = {}),
)

composeTestRule.setContent {
TUITabBar(
tabItems = tabItems,
selectedTabIndex = 1,
tags = testTags,
onTabChanged = {},
)
}
composeTestRule.onNodeWithTag(testTags.contentId + 1).assertDoesNotExist()
}

@Test
fun is_pager_enabled_properly_based_on_param() {
val tabItems = listOf(
TabItem(name = "Tab 1", content = { Text(text = "Content 1") }),
TabItem(name = "Tab 2", content = { Text(text = "Content 2") }),
TabItem(name = "Tab 3", content = { Text(text = "Content 3") }),
)

composeTestRule.setContent {
TUITabBar(
tabItems = tabItems,
selectedTabIndex = 1,
tags = testTags,
onTabChanged = {},
)
}
composeTestRule.onNodeWithTag(testTags.contentId + 1).assertIsDisplayed()
}

@Test
fun is_proper_content_shown_when_switching_tab_by_click_tabRow() {
val tabItems = listOf(
TabItem(name = "Tab 1", content = { Text(text = "Content 1") }),
TabItem(name = "Tab 2", content = { Text(text = "Content 2") }),
TabItem(name = "Tab 3", content = { Text(text = "Content 3") }),
)

composeTestRule.setContent {
TUITabBar(
tabItems = tabItems,
selectedTabIndex = 0,
tags = testTags,
onTabChanged = {},
)
}

//Initially the content of the first tab should be shown due to the selected index is 0.
composeTestRule.onNodeWithText("Content 1").assertIsDisplayed()
//Other tab contents should not be exist
composeTestRule.onNodeWithText("Content 2").assertDoesNotExist()
composeTestRule.onNodeWithText("Content 3").assertDoesNotExist()

//Clicking the second Tab & updating Index
composeTestRule.onNodeWithTag("Tab 2 ${testTags.tabId}").performClick()

//Now the content of the second tab should be shown due to the selected index is 1.
composeTestRule.onNodeWithText("Content 2").assertIsDisplayed()
//Other tab contents should not be shown
composeTestRule.onNodeWithText("Content 1").assertIsNotDisplayed()
composeTestRule.onNodeWithText("Content 3").assertIsNotDisplayed()
}

@Test
fun is_proper_tab_shown_when_switching_tab_by_scrolling_pager() {
val tabItems = listOf(
TabItem(name = "Tab 1", content = { Text(text = "Content 1") }),
TabItem(name = "Tab 2", content = { Text(text = "Content 2") }),
TabItem(name = "Tab 3", content = { Text(text = "Content 3") }),
)

composeTestRule.setContent {
TUITabBar(
tabItems = tabItems,
selectedTabIndex = 0,
tags = testTags,
onTabChanged = {},
)
}

//state before scroll
composeTestRule.onNodeWithText("Content 1").assertIsDisplayed()
composeTestRule.onNodeWithText("Content 2").assertDoesNotExist()
composeTestRule.onNodeWithText("Content 3").assertDoesNotExist()

//if the scrolling was disabled the page content couldn't change
//First Tab Content
composeTestRule.onNodeWithTag(testTags.contentId + 0)
.performTouchInput { swipeLeft(startX = 450f, endX = 150f) }

//state after first scroll
composeTestRule.onNodeWithText("Content 2").assertIsDisplayed()
composeTestRule.onNodeWithText("Content 3").assertIsNotDisplayed()
composeTestRule.onNodeWithText("Content 1").assertIsNotDisplayed()

//Second Scroll to right
//Second Tab Content
composeTestRule.onNodeWithTag(testTags.contentId + 1)
.performTouchInput { swipeRight(startX = 150f, endX = 450f) }

//state after second scroll
composeTestRule.onNodeWithText("Content 1").assertIsDisplayed()
composeTestRule.onNodeWithText("Content 2").assertIsNotDisplayed()
composeTestRule.onNodeWithText("Content 3").assertIsNotDisplayed()
composeTestRule.onNodeWithTag("Tab 1 ${TUITabTags().parentTag}").assertIsNotSelected()
composeTestRule.onNodeWithTag("Tab 2 ${TUITabTags().parentTag}").assertIsNotSelected()
}

@Test
Expand All @@ -269,9 +126,9 @@ class TUITabBarTest {
val onTabChange: (Int) -> Unit = mock()

val tabItems = listOf(
TabItem(name = "Tab 1", content = {}),
TabItem(name = "Tab 2", content = {}),
TabItem(name = "Tab 3", content = {}),
TabItem(name = "Tab 1"),
TabItem(name = "Tab 2"),
TabItem(name = "Tab 3"),
)

composeTestRule.setContent {
Expand All @@ -284,115 +141,12 @@ class TUITabBarTest {
}

var clickedTabIndex = 1
composeTestRule.onNodeWithTag("Tab 2 ${testTags.tabId}").performClick()
composeTestRule.onNodeWithTag("Tab 2 ${TUITabTags().parentTag}").performClick()
verify(onTabChange).invoke(clickedTabIndex)

clickedTabIndex = 2
composeTestRule.onNodeWithTag("Tab 3 ${testTags.tabId}").performClick()
composeTestRule.onNodeWithTag("Tab 3 ${TUITabTags().parentTag}").performClick()
verify(onTabChange).invoke(clickedTabIndex)
}

@Test
fun is_onTabChange_invoked_while_scrolling_tabContent() {
val onTabChange: (Int) -> Unit = mock()

val tabItems = listOf(
TabItem(name = "Tab 1", content = { Text(text = "Content 1") }),
TabItem(name = "Tab 2", content = { Text(text = "Content 2") }),
TabItem(name = "Tab 3", content = { Text(text = "Content 3") }),
)

composeTestRule.setContent {
TUITabBar(
tabItems = tabItems,
selectedTabIndex = 0,
tags = testTags,
onTabChanged = onTabChange,
)
}

composeTestRule.onNodeWithTag(testTags.contentId + 0)
.performTouchInput { swipeLeft(startX = 450f, endX = 150f) }
composeTestRule.waitForIdle()
verify(onTabChange).invoke(1)
}

@Test
fun is_scrolling_enabled_in_pager_based_on_the_param() {

val tabItems = listOf(
TabItem(name = "Tab 1", content = { Text(text = "Content 1") }),
TabItem(name = "Tab 2", content = { Text(text = "Content 2") }),
TabItem(name = "Tab 3", content = { Text(text = "Content 3") }),
)

composeTestRule.setContent {
TUITabBar(
tabItems = tabItems,
selectedTabIndex = 0,
tags = testTags,
onTabChanged = {},
)
}

//state before scroll
composeTestRule.onNodeWithText("Content 1").assertIsDisplayed()
composeTestRule.onNodeWithText("Content 2").assertDoesNotExist()
composeTestRule.onNodeWithText("Content 3").assertDoesNotExist()

composeTestRule.onNodeWithTag(testTags.contentId + 0)
.performTouchInput { swipeLeft(startX = 450f, endX = 150f) }

composeTestRule.waitForIdle()
//if the scrolling was enabled the page content will change

//state after scroll
composeTestRule.onNodeWithText("Content 2").assertIsDisplayed()
composeTestRule.onNodeWithText("Content 1").assertIsNotDisplayed()
// here when content 2 is displayed then sequence tab - content 3 wil initialized but not shown
// that's why we used assertIsNotDisplayed instead of assertDoesNotExist
composeTestRule.onNodeWithText("Content 3").assertIsNotDisplayed()
}

@Test
fun is_scrolling_disabled_in_pager_based_on_the_param() {

val tabItems = listOf(
TabItem(name = "Tab 1", content = { Text(text = "Content 1") }),
TabItem(name = "Tab 2", content = { Text(text = "Content 2") }),
TabItem(name = "Tab 3", content = { Text(text = "Content 3") }),
)

composeTestRule.setContent {
TUITabBar(
tabItems = tabItems,
selectedTabIndex = 0,
tags = testTags,
onTabChanged = {},
)
}

//state before scroll
composeTestRule.onNodeWithText("Content 1").assertIsDisplayed()
composeTestRule.onNodeWithText("Content 2").assertDoesNotExist()
composeTestRule.onNodeWithText("Content 3").assertDoesNotExist()

//if the scrolling was disabled the page content couldn't change
composeTestRule.onNodeWithTag(testTags.contentId + 0)
.performTouchInput { swipeLeft(startX = 450f, endX = 150f) }

//state after scroll
composeTestRule.onNodeWithText("Content 1").assertIsDisplayed()
composeTestRule.onNodeWithText("Content 2").assertDoesNotExist()
composeTestRule.onNodeWithText("Content 3").assertDoesNotExist()

//second scroll to right
composeTestRule.onNodeWithTag(testTags.contentId + 0)
.performTouchInput { swipeRight(startX = 150f, endX = 450f) }

//state after second scroll
composeTestRule.onNodeWithText("Content 1").assertIsDisplayed()
composeTestRule.onNodeWithText("Content 2").assertDoesNotExist()
composeTestRule.onNodeWithText("Content 3").assertDoesNotExist()
}
}
Loading

0 comments on commit 79bd118

Please sign in to comment.