Skip to content

Commit

Permalink
fix recycler view scrollToItem
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-tiurin committed Feb 22, 2024
1 parent 676bf34 commit f66309f
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ object ComposeListPage : Page<ComposeListPage>() {
)

fun assertContactStatus(contact: Contact) = apply {
getContactItemById(contact).status.assertTextEquals(contact.status)
getContactItemByTestTag(contact).status.assertTextEquals(contact.status)
}
fun getItemByPosition(position: Int): ComposeFriendListItem {
return lazyList.getItem(position)
}

fun getFirstVisibleItem(): ComposeFriendListItem = lazyList.getFirstVisibleItem()
fun getItemByIndex(index: Int): ComposeFriendListItem = lazyList.getVisibleItem(index)
fun getContactItemById(contact: Contact): ComposeFriendListItem = lazyList.getItem(hasTestTag(getContactItemTestTagById(contact)))
fun getContactItemByTestTag(contact: Contact): ComposeFriendListItem = lazyList.getItem(hasTestTag(getContactItemTestTagById(contact)))
fun getContactItemByName(contact: Contact): ComposeFriendListItem = lazyList.getItem(hasAnyDescendant(hasText(contact.name) and hasTestTag(contactNameTestTag)))
class ComposeFriendListItem : UltronComposeListItem() {
val name by lazy { getChild(hasTestTag(contactNameTestTag)) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,17 @@ class ComposeListTest: BaseTest() {
fun getItem_ByTestTag_assertNameAndStatusOfContact() {
val index = 20
val contact = CONTACTS[index]
listPage.getContactItemById(contact).apply {
listPage.getContactItemByTestTag(contact).apply {
name.assertTextEquals(contact.name)
status.assertTextContains(contact.status)
}
}

@Test
fun getItem_ByMatcher_assertNameAndStatusOfContact() {
val index = 20
val contact = CONTACTS[index]
listPage.getContactItemByName(contact).apply {
name.assertTextEquals(contact.name)
status.assertTextContains(contact.status)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,6 @@ class UltronUiObject2ActionsTest : UiElementsTest() {
@FlakyTest
@Test
fun swipeUpTest() {
page.eventStatus.hasText(getTargetString(R.string.button_text))
page.editTextContentDesc.replaceText("some text")
page.swipableImageView.withAssertion {
page.eventStatus.withTimeout(300).textContains(UiElementsActivity.Event.SWIPE_UP.name)
Expand All @@ -361,8 +360,6 @@ class UltronUiObject2ActionsTest : UiElementsTest() {
@FlakyTest
@Test
fun swipeDownTest() {
// Thread.sleep(2000)
page.eventStatus.hasText(getTargetString(R.string.button_text))
page.editTextContentDesc.replaceText("some text")
page.swipableImageView.withAssertion {
page.eventStatus.withTimeout(300).textContains(UiElementsActivity.Event.SWIPE_DOWN.name)
Expand All @@ -372,7 +369,6 @@ class UltronUiObject2ActionsTest : UiElementsTest() {
@Test
@FlakyTest
fun swipeRightTest() {
page.eventStatus.hasText(getTargetString(R.string.button_text))
page.editTextContentDesc.replaceText("some text")
page.swipableImageView.withAssertion {
page.eventStatus.withTimeout(300)
Expand All @@ -383,7 +379,6 @@ class UltronUiObject2ActionsTest : UiElementsTest() {
@Test
@FlakyTest
fun swipeLeftTest() {
page.eventStatus.hasText(getTargetString(R.string.button_text))
page.editTextContentDesc.replaceText("some text")
page.swipableImageView.withAssertion {
page.eventStatus.withTimeout(300).textContains(UiElementsActivity.Event.SWIPE_LEFT.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ class UltronUiObjectActionsTest: UiElementsTest() {
//swipe
@Test
fun swipeUpTest(){
page.eventStatus.hasText(getTargetString(R.string.button_text))
page.swipableImageView.withAssertion {
page.eventStatus.withTimeout(300).textContains(UiElementsActivity.Event.SWIPE_UP.name)
}.swipeUp(40)
Expand All @@ -216,15 +215,13 @@ class UltronUiObjectActionsTest: UiElementsTest() {

@Test
fun swipeRightTest(){
page.eventStatus.hasText(getTargetString(R.string.button_text))
page.swipableImageView.withAssertion {
page.eventStatus.withTimeout(300).textContains(UiElementsActivity.Event.SWIPE_RIGHT.name)
}.swipeRight(40)
}

@Test
fun swipeLeftTest(){
page.eventStatus.hasText(getTargetString(R.string.button_text))
page.swipableImageView.withAssertion {
page.eventStatus.withTimeout(300).textContains(UiElementsActivity.Event.SWIPE_LEFT.name)
}.swipeLeft(40)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,9 @@ class UltronUiObjectAssertionsTest: UiElementsTest() {
//isNotFocusable
@Test
fun isNotFocusable_ofNotFocusable(){
page.checkBoxFocusable.click()
page.button.isNotFocusable()
page.checkBoxFocusable.withAssertion {
page.button.isNotFocusable()
}.click()
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class RecyclerViewItemMatchingExecutor(
private val itemViewMatcher: Matcher<View>
) : RecyclerViewItemExecutor {
override fun scrollToItem(offset: Int) {
ultronRecyclerView.scrollToIem(itemViewMatcher, offset = offset)
ultronRecyclerView.scrollToItem(itemViewMatcher, offset = offset)
}

override fun getItemMatcher(): Matcher<View> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.contrib.RecyclerViewActions
import com.atiurin.ultron.exceptions.UltronOperationException
import com.atiurin.ultron.extensions.perform
import com.atiurin.ultron.extensions.withTimeout
import org.hamcrest.Matcher

class RecyclerViewItemPositionalExecutor(
Expand All @@ -18,20 +19,7 @@ class RecyclerViewItemPositionalExecutor(
}

override fun scrollToItem(offset: Int) {
ultronRecyclerView.assertHasItemAtPosition(position)
val itemCount = ultronRecyclerView.getSize()
val positionToScroll = position + offset
val finalPositionToScroll = when {
positionToScroll in 1 until itemCount -> positionToScroll
positionToScroll >= itemCount -> itemCount - 1
else -> 0
}
ultronRecyclerView.recyclerViewMatcher.perform(
viewAction = RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(
finalPositionToScroll
),
description = "RecyclerViewActions scrollToPosition $position with offset = $offset"
)
ultronRecyclerView.scrollToItem(position, offset)
}

override fun getItemMatcher(): Matcher<View> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.annotation.IntegerRes
import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.contrib.RecyclerViewActions
import androidx.test.espresso.matcher.BoundedMatcher
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.util.TreeIterables
Expand Down Expand Up @@ -385,13 +386,34 @@ open class UltronRecyclerView(
open fun assertMatches(matcher: Matcher<View>) =
apply { recyclerViewMatcher.withTimeout(getTimeout()).assertMatches(matcher) }

fun scrollToIem(itemMatcher: Matcher<View>, searchLimit: Int = this.itemSearchLimit, offset: Int = 0) = apply {

fun scrollToItem(itemMatcher: Matcher<View>, searchLimit: Int = this.itemSearchLimit, offset: Int = 0) = apply {
recyclerViewMatcher.withTimeout(getTimeout()).perform(
viewAction = RecyclerViewScrollAction(itemMatcher, searchLimit, offset),
description = "Scroll RecyclerView '$recyclerViewMatcher' to item = '$itemMatcher' with searchLimit = $searchLimit and offset = $offset"
)
}

@Deprecated("Use scrollToItem(itemMatcher, searchLimit, offset)")
fun scrollToIem(itemMatcher: Matcher<View>, searchLimit: Int = this.itemSearchLimit, offset: Int = 0) = scrollToItem(itemMatcher, searchLimit, offset)

fun scrollToItem(position: Int, offset: Int = 0){
assertHasItemAtPosition(position)
val itemCount = getSize()
val positionToScroll = position + offset
val finalPositionToScroll = when {
positionToScroll in 1 until itemCount -> positionToScroll
positionToScroll >= itemCount -> itemCount - 1
else -> 0
}
recyclerViewMatcher.withTimeout(getTimeout()).perform(
viewAction = RecyclerViewActions.scrollToPosition<RecyclerView.ViewHolder>(
finalPositionToScroll
),
description = "RecyclerViewActions scrollToPosition $position with offset = $offset"
)
}

/** set timeout for operations with RecyclerView.
* Note: it doesn't modify [loadTimeoutMs] (waiting a RecyclerView to be loaded)
* */
Expand Down

0 comments on commit f66309f

Please sign in to comment.