From 7be9c3b634764c990ba1b5d735845301136014da Mon Sep 17 00:00:00 2001 From: Konstantin Aksenov Date: Thu, 13 Jul 2023 20:12:33 +1000 Subject: [PATCH] feat(tablayout): add count and selector by title --- .../kakaocup/kakao/tabs/TabLayoutActions.kt | 36 +++++++++++++ .../kakao/tabs/TabLayoutAssertions.kt | 52 +++++++++++++++++-- .../github/kakaocup/sample/TabLayoutTest.kt | 5 ++ .../kakaocup/sample/TabLayoutActivity.kt | 2 +- 4 files changed, 90 insertions(+), 5 deletions(-) diff --git a/kakao/src/main/kotlin/io/github/kakaocup/kakao/tabs/TabLayoutActions.kt b/kakao/src/main/kotlin/io/github/kakaocup/kakao/tabs/TabLayoutActions.kt index c2866fb89..098ab9f93 100644 --- a/kakao/src/main/kotlin/io/github/kakaocup/kakao/tabs/TabLayoutActions.kt +++ b/kakao/src/main/kotlin/io/github/kakaocup/kakao/tabs/TabLayoutActions.kt @@ -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 @@ -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 * diff --git a/kakao/src/main/kotlin/io/github/kakaocup/kakao/tabs/TabLayoutAssertions.kt b/kakao/src/main/kotlin/io/github/kakaocup/kakao/tabs/TabLayoutAssertions.kt index e7b03ad3f..d38aade75 100644 --- a/kakao/src/main/kotlin/io/github/kakaocup/kakao/tabs/TabLayoutAssertions.kt +++ b/kakao/src/main/kotlin/io/github/kakaocup/kakao/tabs/TabLayoutAssertions.kt @@ -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 @@ -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) } + } + } } } diff --git a/sample/src/androidTest/kotlin/io/github/kakaocup/sample/TabLayoutTest.kt b/sample/src/androidTest/kotlin/io/github/kakaocup/sample/TabLayoutTest.kt index ece4862bf..60af3ee7b 100644 --- a/sample/src/androidTest/kotlin/io/github/kakaocup/sample/TabLayoutTest.kt +++ b/sample/src/androidTest/kotlin/io/github/kakaocup/sample/TabLayoutTest.kt @@ -19,6 +19,8 @@ class TabLayoutTest { fun testTabLayout() { onScreen { tabLayout { + tabCount(3) + isTabSelected(0) assertEquals(0, getSelectedItem()) @@ -26,6 +28,9 @@ class TabLayoutTest { isTabSelected(1) assertEquals(1, getSelectedItem()) + + selectTab("Tab3") + isTabSelected("Tab3") } } } diff --git a/sample/src/main/kotlin/io/github/kakaocup/sample/TabLayoutActivity.kt b/sample/src/main/kotlin/io/github/kakaocup/sample/TabLayoutActivity.kt index 7521f3053..5a7e25588 100644 --- a/sample/src/main/kotlin/io/github/kakaocup/sample/TabLayoutActivity.kt +++ b/sample/src/main/kotlin/io/github/kakaocup/sample/TabLayoutActivity.kt @@ -11,7 +11,7 @@ class TabLayoutActivity : AppCompatActivity() { setContentView(R.layout.activity_tab_layout) val tabLayout = findViewById(R.id.tab_layout) - val tabs = listOf("Tab1", "Tab2") + val tabs = listOf("Tab1", "Tab2", "Tab3") tabs.forEach { tab -> tabLayout.addTab(tabLayout.newTab().setText(tab)) }