Skip to content

Commit

Permalink
feat(tablayout): add count and selector by title
Browse files Browse the repository at this point in the history
  • Loading branch information
Vacxe committed Jul 13, 2023
1 parent 3cb9520 commit 7be9c3b
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
package io.github.kakaocup.kakao.tabs

import android.view.View
import androidx.test.espresso.PerformException
import androidx.test.espresso.UiController
import androidx.test.espresso.ViewAction
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.util.HumanReadables
import io.github.kakaocup.kakao.common.actions.BaseActions
import com.google.android.material.tabs.TabLayout

Expand All @@ -32,6 +34,40 @@ interface TabLayoutActions : BaseActions {
})
}

/**
* Selects tab at given text
*
* @param text to be selected
*/
fun selectTab(text: String) {
view.perform(object : ViewAction {
override fun getDescription() = "Selects the tab with text: $text"

override fun getConstraints() = ViewMatchers.isAssignableFrom(TabLayout::class.java)

override fun perform(uiController: UiController, view: View) {
if (view is TabLayout) {
for (i in 0 until view.tabCount) {
view.getTabAt(i)?.let { tab ->
if (tab.text == text) {
tab.select()
return
}
}
}

throw PerformException.Builder()
.withActionDescription(description)
.withViewDescription(HumanReadables.describe(view))
.withCause(
RuntimeException("Tab with text $text not found")
)
.build()
}
}
})
}

/**
* Returns the currently selected tab position
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

package io.github.kakaocup.kakao.tabs

import androidx.test.espresso.ViewAssertion
import io.github.kakaocup.kakao.common.assertions.BaseAssertions
import com.google.android.material.tabs.TabLayout

Expand All @@ -16,17 +15,62 @@ interface TabLayoutAssertions : BaseAssertions {
* @param index tab index to be checked
*/
fun isTabSelected(index: Int) {
view.check(ViewAssertion { view, notFoundException ->
view.check { view, notFoundException ->
if (view is TabLayout) {
if (view.selectedTabPosition != index) {
throw AssertionError(
"Expected selected item index is $index," +
" but actual is ${view.selectedTabPosition}"
" but actual is ${view.selectedTabPosition}"
)
}
} else {
notFoundException?.let { throw AssertionError(it) }
}
})
}
}

/**
* Checks if TabLayout have selected tab with given text
*
* @param text tab title to be checked
*/
@Suppress("ThrowsCount")
fun isTabSelected(text: String) {
view.check { view, notFoundException ->
if (view is TabLayout) {
val tab = view.getTabAt(view.selectedTabPosition) ?: throw AssertionError(
"Expected selected item text is $text, but tab not selected"
)

if (tab.text != text) {
throw AssertionError(
"Expected selected item text is $text," +
" but actual is ${tab.text ?: ""}"
)
}
} else {
notFoundException?.let { throw AssertionError(it) }
}
}
}

/**
* Checks the number of tabs currently registered with the action bar
*
* @param count tabs
*/
fun tabCount(count: Int) {
view.check { view, notFoundException ->
if (view is TabLayout) {
if (view.tabCount != count) {
throw AssertionError(
"Expected tabs currently registered with the action bar $count," +
" but actual is ${view.tabCount}"
)
}
} else {
notFoundException?.let { throw AssertionError(it) }
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ class TabLayoutTest {
fun testTabLayout() {
onScreen<TabLayoutActivityScreen> {
tabLayout {
tabCount(3)

isTabSelected(0)
assertEquals(0, getSelectedItem())

selectTab(1)

isTabSelected(1)
assertEquals(1, getSelectedItem())

selectTab("Tab3")
isTabSelected("Tab3")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TabLayoutActivity : AppCompatActivity() {
setContentView(R.layout.activity_tab_layout)

val tabLayout = findViewById<TabLayout>(R.id.tab_layout)
val tabs = listOf("Tab1", "Tab2")
val tabs = listOf("Tab1", "Tab2", "Tab3")
tabs.forEach { tab ->
tabLayout.addTab(tabLayout.newTab().setText(tab))
}
Expand Down

0 comments on commit 7be9c3b

Please sign in to comment.