Skip to content

Commit

Permalink
Lists improvements (#59)
Browse files Browse the repository at this point in the history
* compose list item by position
* Compose list assertItemDoesntExist
* fix recycler view scrollToItem
* fix test
  • Loading branch information
alex-tiurin authored Feb 23, 2024
1 parent 945028c commit 7420b38
Show file tree
Hide file tree
Showing 23 changed files with 432 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.compose.ui.test.hasAnyDescendant
import androidx.compose.ui.test.hasContentDescription
import androidx.compose.ui.test.hasTestTag
import androidx.compose.ui.test.hasText
import com.atiurin.sampleapp.compose.ListItemPositionPropertyKey
import com.atiurin.sampleapp.compose.contactNameTestTag
import com.atiurin.sampleapp.compose.contactStatusTestTag
import com.atiurin.sampleapp.compose.contactsListContentDesc
Expand All @@ -14,16 +15,23 @@ import com.atiurin.ultron.core.compose.list.composeList
import com.atiurin.ultron.page.Page

object ComposeListPage : Page<ComposeListPage>() {
val lazyList = composeList(hasContentDescription(contactsListContentDesc))
val lazyList = composeList(
listMatcher = hasContentDescription(contactsListContentDesc),
positionPropertyKey = ListItemPositionPropertyKey
)

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(){
class ComposeFriendListItem : UltronComposeListItem() {
val name by lazy { getChild(hasTestTag(contactNameTestTag)) }
val status by lazy { getChild(hasTestTag(contactStatusTestTag)) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.compose.ui.test.*
import com.atiurin.sampleapp.activity.ComposeListActivity
import com.atiurin.sampleapp.compose.*
import com.atiurin.sampleapp.data.repositories.CONTACTS
import com.atiurin.sampleapp.data.repositories.ContactRepositoty
import com.atiurin.sampleapp.framework.utils.AssertUtils
import com.atiurin.sampleapp.pages.ComposeListPage
import com.atiurin.sampleapp.pages.ComposeSecondPage
Expand All @@ -27,10 +28,11 @@ import org.junit.Test
class ComposeListTest: BaseTest() {
@get:Rule
val composeRule = createUltronComposeRule<ComposeListActivity>()
val listWithMergedTree = composeList(hasTestTag(contactsListTestTag), false)
val listPage = ComposeListPage
val notExistedList = composeList(hasTestTag("askjhsalk jdhas dlqk "))
val emptyListTestTag = "emptyList"

private val listWithMergedTree = composeList(hasTestTag(contactsListTestTag), false)
private val listPage = ComposeListPage
private val notExistedList = composeList(hasTestTag("askjhsalk jdhas dlqk "))
private val emptyListTestTag = "emptyList"

@Test
fun item_existItem() {
Expand Down Expand Up @@ -131,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 Expand Up @@ -254,6 +266,38 @@ class ComposeListTest: BaseTest() {
AssertUtils.assertException { listWithMergedTree.withTimeout(1000).assertVisibleItemsCount(100) }
}

@Test
fun itemByPosition_propertyConfiguredTest(){
val index = 20
val contact = CONTACTS[index]
val item = listPage.lazyList.item(20).assertIsDisplayed()
item.assertMatches(hasTestTag(getContactItemTestTagById(contact)))
}

@Test
fun getItemByPosition_propertyConfiguredTest(){
val index = 20
val contact = CONTACTS[index]
listPage.getItemByPosition(index).apply {
name.assertTextEquals(contact.name)
status.assertTextEquals(contact.status)
assertIsDisplayed()
}
}

@Test
fun assertItemDoesNotExistWithSearch_NotExistedItem(){
listWithMergedTree.assertItemDoesNotExist(hasText("NOT EXISTED TeXT"))
}

@Test
fun assertItemDoesNotExistWithSearch_ExistedItem(){
val contact = ContactRepositoty.getLast()
AssertUtils.assertException {
listWithMergedTree.withTimeout(2000).assertItemDoesNotExist(hasText(contact.name))
}
}

private fun setEmptyListContent() {
composeRule.setContent {
LazyColumn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package com.atiurin.sampleapp.tests.compose
import androidx.compose.ui.test.hasContentDescription
import androidx.compose.ui.test.hasTestTag
import com.atiurin.sampleapp.activity.ComposeListWithPositionTestTagActivity
import com.atiurin.sampleapp.compose.ListItemPositionPropertyKey
import com.atiurin.sampleapp.compose.contactsListContentDesc
import com.atiurin.sampleapp.compose.getContactItemTestTagByPosition
import com.atiurin.sampleapp.data.repositories.CONTACTS
import com.atiurin.sampleapp.framework.utils.AssertUtils
import com.atiurin.sampleapp.pages.ComposeListPage
import com.atiurin.ultron.core.compose.createUltronComposeRule
import com.atiurin.ultron.core.compose.list.composeList
import org.junit.Rule
Expand All @@ -15,6 +18,7 @@ class ComposeListWithPositionTestTagTest {
@get:Rule
val composeRule = createUltronComposeRule<ComposeListWithPositionTestTagActivity>()
val list = composeList(hasContentDescription(contactsListContentDesc), false)
val composeListWithProperty = composeList(hasContentDescription(contactsListContentDesc), false, ListItemPositionPropertyKey)

@Test
fun itemOutOfVisibleScope() {
Expand All @@ -34,4 +38,29 @@ class ComposeListWithPositionTestTagTest {
.assertTextContains(contact.name)
.assertTextContains(contact.status)
}
@Test
fun itemByPosition_propertyNOTConfiguredInTest(){
AssertUtils.assertException {
list.item(20).assertIsDisplayed()
}
}

@Test
fun itemByPosition_propertyNOTConfiguredInApplication(){
AssertUtils.assertException {
composeListWithProperty.withTimeout(1000).item(20).assertIsDisplayed()
}
}

@Test
fun getItemByPosition_propertyNOTConfiguredInTest(){
AssertUtils.assertException { list.getItem<ComposeListPage.ComposeFriendListItem>(20).assertIsDisplayed() }
}

@Test
fun getItemByPosition_propertyNOTConfiguredInApplication(){
AssertUtils.assertException {
composeListWithProperty.withTimeout(1000).getItem<ComposeListPage.ComposeFriendListItem>(20).assertIsDisplayed()
}
}
}
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 @@ -144,8 +144,9 @@ class UltronUiObjectActionsTest: UiElementsTest() {
//longClick
@Test
fun longClick_onLongClickable() {
page.button.exists().longClick()
page.eventStatus.textContains(getTargetString(R.string.button_event_long_click))
page.button.exists().withAssertion {
page.eventStatus.textContains(getTargetString(R.string.button_event_long_click))
}.longClick()
}

@Test
Expand Down Expand Up @@ -199,7 +200,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 +216,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
@@ -1,38 +1,25 @@
package com.atiurin.sampleapp.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.Divider
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.semantics.*
import androidx.compose.ui.unit.ExperimentalUnitApi
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat
import androidx.lifecycle.Observer
import com.atiurin.sampleapp.async.GetContacts
import com.atiurin.sampleapp.async.UseCase
import com.atiurin.sampleapp.compose.ContactsList
import com.atiurin.sampleapp.compose.LoadingAnimation
import com.atiurin.sampleapp.compose.getContactItemTestTagById
import com.atiurin.sampleapp.compose.listItemPosition
import com.atiurin.sampleapp.data.entities.Contact
import com.atiurin.sampleapp.data.repositories.ContactRepositoty
import com.atiurin.sampleapp.data.viewmodel.ContactsViewModel
Expand Down Expand Up @@ -60,8 +47,11 @@ class ComposeListActivity : ComponentActivity() {
setContent {
Column {
ContactsList(
contacts = ContactRepositoty.all(), this@ComposeListActivity
) { contact, _ -> getContactItemTestTagById(contact) }
contacts = ContactRepositoty.all(),
context = this@ComposeListActivity,
testTagProvider = { contact, _ -> getContactItemTestTagById(contact) },
modifierProvider = { position -> Modifier.listItemPosition(position) }
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import com.atiurin.sampleapp.async.GetContacts
import com.atiurin.sampleapp.async.UseCase
import com.atiurin.sampleapp.compose.ContactsList
import com.atiurin.sampleapp.compose.LoadingAnimation
import com.atiurin.sampleapp.compose.getContactItemTestTagById
import com.atiurin.sampleapp.compose.getContactItemTestTagByPosition
import com.atiurin.sampleapp.compose.listItemPosition
import com.atiurin.sampleapp.data.entities.Contact
import com.atiurin.sampleapp.data.repositories.ContactRepositoty
import com.atiurin.sampleapp.data.viewmodel.ContactsViewModel
Expand Down Expand Up @@ -46,14 +48,18 @@ class ComposeListWithPositionTestTagActivity: ComponentActivity() {
setContent {
Column {
ContactsList(
contacts = ContactRepositoty.all(), this@ComposeListWithPositionTestTagActivity, false
) { _, position -> getContactItemTestTagByPosition(position) }
contacts = ContactRepositoty.all(),
context = this@ComposeListWithPositionTestTagActivity,
addStickyHeader = false,
testTagProvider = { _, position -> getContactItemTestTagByPosition(position) },
modifierProvider = { _ -> Modifier }
)
}
}
}
model.contacts.observe(this, contactsObserver)
GlobalScope.async {
GetContacts()(
GetContacts(0)(
UseCase.None,
onSuccess = { model.contacts.value = it },
onFailure = { Toast.makeText(this@ComposeListWithPositionTestTagActivity, "Failed to load contacts", Toast.LENGTH_LONG).show() }
Expand Down
Loading

0 comments on commit 7420b38

Please sign in to comment.