Refactored the NoteFragmentTest test according to compose UI.

This commit is contained in:
MohitMaliFtechiz 2025-04-15 15:15:52 +05:30
parent bb8a78a620
commit d02ef05390
4 changed files with 56 additions and 53 deletions

View File

@ -121,9 +121,8 @@ class NoteFragmentTest : BaseActivityTest() {
it.navigate(R.id.notesFragment) it.navigate(R.id.notesFragment)
} }
note { note {
assertToolbarExist() assertToolbarExist(composeTestRule)
assertNoteRecyclerViewExist() assertSwitchWidgetExist(composeTestRule)
assertSwitchWidgetExist()
} }
LeakAssertions.assertNoLeaks() LeakAssertions.assertNoLeaks()
} }
@ -140,9 +139,8 @@ class NoteFragmentTest : BaseActivityTest() {
saveNote(composeTestRule) saveNote(composeTestRule)
pressBack() pressBack()
openNoteFragment() openNoteFragment()
assertToolbarExist() assertToolbarExist(composeTestRule)
assertNoteRecyclerViewExist() clickOnSavedNote(composeTestRule)
clickOnSavedNote()
clickOnOpenNote() clickOnOpenNote()
assertNoteSaved(composeTestRule) assertNoteSaved(composeTestRule)
// to close the note dialog. // to close the note dialog.
@ -164,9 +162,8 @@ class NoteFragmentTest : BaseActivityTest() {
note { note {
openNoteFragment() openNoteFragment()
assertToolbarExist() assertToolbarExist(composeTestRule)
assertNoteRecyclerViewExist() clickOnSavedNote(composeTestRule)
clickOnSavedNote()
clickOnOpenNote() clickOnOpenNote()
assertNoteSaved(composeTestRule) assertNoteSaved(composeTestRule)
pressBack() pressBack()
@ -189,9 +186,8 @@ class NoteFragmentTest : BaseActivityTest() {
saveNote(composeTestRule) saveNote(composeTestRule)
pressBack() pressBack()
openNoteFragment() openNoteFragment()
assertToolbarExist() assertToolbarExist(composeTestRule)
assertNoteRecyclerViewExist() clickOnSavedNote(composeTestRule)
clickOnSavedNote()
clickOnOpenNote() clickOnOpenNote()
assertNoteSaved(composeTestRule) assertNoteSaved(composeTestRule)
// to close the note dialog. // to close the note dialog.
@ -213,14 +209,13 @@ class NoteFragmentTest : BaseActivityTest() {
saveNote(composeTestRule) saveNote(composeTestRule)
pressBack() pressBack()
openNoteFragment() openNoteFragment()
assertToolbarExist() assertToolbarExist(composeTestRule)
assertNoteRecyclerViewExist() clickOnSavedNote(composeTestRule)
clickOnSavedNote()
clickOnOpenNote() clickOnOpenNote()
assertNoteSaved(composeTestRule) assertNoteSaved(composeTestRule)
clickOnDeleteIcon(composeTestRule) clickOnDeleteIcon(composeTestRule)
pressBack() pressBack()
assertNoNotesTextDisplayed() assertNoNotesTextDisplayed(composeTestRule)
} }
} }
} }
@ -252,12 +247,11 @@ class NoteFragmentTest : BaseActivityTest() {
// delete the notes if any saved to properly run the test scenario // delete the notes if any saved to properly run the test scenario
note { note {
openNoteFragment() openNoteFragment()
assertToolbarExist() assertToolbarExist(composeTestRule)
assertNoteRecyclerViewExist() clickOnTrashIcon(composeTestRule)
clickOnTrashIcon()
assertDeleteNoteDialogDisplayed() assertDeleteNoteDialogDisplayed()
clickOnDeleteButton() clickOnDeleteButton()
assertNoNotesTextDisplayed() assertNoNotesTextDisplayed(composeTestRule)
pressBack() pressBack()
} }
} }

View File

@ -22,18 +22,15 @@ import android.content.Context
import androidx.compose.ui.test.assertTextContains import androidx.compose.ui.test.assertTextContains
import androidx.compose.ui.test.assertTextEquals import androidx.compose.ui.test.assertTextEquals
import androidx.compose.ui.test.junit4.ComposeContentTestRule import androidx.compose.ui.test.junit4.ComposeContentTestRule
import androidx.compose.ui.test.onAllNodesWithTag
import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextReplacement import androidx.compose.ui.test.performTextReplacement
import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.Espresso.closeSoftKeyboard import androidx.test.espresso.Espresso.closeSoftKeyboard
import androidx.test.espresso.Espresso.onView import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu
import androidx.test.espresso.action.ViewActions.click import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition
import androidx.test.espresso.matcher.ViewMatchers import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.withContentDescription
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.espresso.web.sugar.Web.onWebView import androidx.test.espresso.web.sugar.Web.onWebView
import androidx.test.espresso.web.webdriver.DriverAtoms.findElement import androidx.test.espresso.web.webdriver.DriverAtoms.findElement
@ -42,11 +39,14 @@ import com.adevinta.android.barista.interaction.BaristaSleepInteractions
import org.kiwix.kiwixmobile.BaseRobot import org.kiwix.kiwixmobile.BaseRobot
import org.kiwix.kiwixmobile.Findable.StringId.TextId import org.kiwix.kiwixmobile.Findable.StringId.TextId
import org.kiwix.kiwixmobile.Findable.Text import org.kiwix.kiwixmobile.Findable.Text
import org.kiwix.kiwixmobile.Findable.ViewId
import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.main.ADD_NOTE_TEXT_FILED_TESTING_TAG import org.kiwix.kiwixmobile.core.main.ADD_NOTE_TEXT_FILED_TESTING_TAG
import org.kiwix.kiwixmobile.core.main.DELETE_MENU_BUTTON_TESTING_TAG import org.kiwix.kiwixmobile.core.main.DELETE_MENU_BUTTON_TESTING_TAG
import org.kiwix.kiwixmobile.core.main.SAVE_MENU_BUTTON_TESTING_TAG import org.kiwix.kiwixmobile.core.main.SAVE_MENU_BUTTON_TESTING_TAG
import org.kiwix.kiwixmobile.core.page.DELETE_MENU_ICON_TESTING_TAG
import org.kiwix.kiwixmobile.core.page.NO_ITEMS_TEXT_TESTING_TAG
import org.kiwix.kiwixmobile.core.page.PAGE_ITEM_TESTING_TAG
import org.kiwix.kiwixmobile.core.page.SWITCH_TEXT_TESTING_TAG
import org.kiwix.kiwixmobile.core.ui.components.TOOLBAR_TITLE_TESTING_TAG import org.kiwix.kiwixmobile.core.ui.components.TOOLBAR_TITLE_TESTING_TAG
import org.kiwix.kiwixmobile.testutils.TestUtils import org.kiwix.kiwixmobile.testutils.TestUtils
import org.kiwix.kiwixmobile.testutils.TestUtils.testFlakyView import org.kiwix.kiwixmobile.testutils.TestUtils.testFlakyView
@ -57,16 +57,20 @@ fun note(func: NoteRobot.() -> Unit) = NoteRobot().apply(func)
class NoteRobot : BaseRobot() { class NoteRobot : BaseRobot() {
private val noteText = "Test Note" private val noteText = "Test Note"
fun assertToolbarExist() { fun assertToolbarExist(composeTestRule: ComposeContentTestRule) {
isVisible(ViewId(R.id.toolbar)) testFlakyView({
composeTestRule.waitForIdle()
composeTestRule.onNodeWithTag(TOOLBAR_TITLE_TESTING_TAG)
.assertTextEquals(context.getString(R.string.pref_notes))
})
} }
fun assertNoteRecyclerViewExist() { fun assertSwitchWidgetExist(composeTestRule: ComposeContentTestRule) {
isVisible(ViewId(R.id.recycler_view)) composeTestRule.apply {
waitForIdle()
onNodeWithTag(SWITCH_TEXT_TESTING_TAG)
.assertTextEquals(context.getString(R.string.notes_from_all_books))
} }
fun assertSwitchWidgetExist() {
isVisible(ViewId(R.id.page_switch))
} }
fun clickOnNoteMenuItem(context: Context) { fun clickOnNoteMenuItem(context: Context) {
@ -117,15 +121,11 @@ class NoteRobot : BaseRobot() {
testFlakyView({ onView(withText(R.string.pref_notes)).perform(click()) }) testFlakyView({ onView(withText(R.string.pref_notes)).perform(click()) })
} }
fun clickOnSavedNote() { fun clickOnSavedNote(composeTestRule: ComposeContentTestRule) {
testFlakyView({ composeTestRule.apply {
onView(withId(R.id.recycler_view)).perform( waitForIdle()
actionOnItemAtPosition<RecyclerView.ViewHolder>( onAllNodesWithTag(PAGE_ITEM_TESTING_TAG)[0].performClick()
0, }
click()
)
)
})
} }
fun clickOnOpenNote() { fun clickOnOpenNote() {
@ -158,8 +158,14 @@ class NoteRobot : BaseRobot() {
}) })
} }
fun clickOnTrashIcon() { fun clickOnTrashIcon(composeTestRule: ComposeContentTestRule) {
testFlakyView({ onView(withContentDescription(R.string.pref_clear_notes)).perform(click()) }) testFlakyView({
composeTestRule.apply {
waitForIdle()
onNodeWithTag(DELETE_MENU_ICON_TESTING_TAG)
.performClick()
}
})
} }
fun assertDeleteNoteDialogDisplayed() { fun assertDeleteNoteDialogDisplayed() {
@ -171,8 +177,12 @@ class NoteRobot : BaseRobot() {
testFlakyView({ onView(ViewMatchers.withText("DELETE")).perform(click()) }) testFlakyView({ onView(ViewMatchers.withText("DELETE")).perform(click()) })
} }
fun assertNoNotesTextDisplayed() { fun assertNoNotesTextDisplayed(composeTestRule: ComposeContentTestRule) {
testFlakyView({ isVisible(TextId(R.string.no_notes)) }) composeTestRule.apply {
waitForIdle()
onNodeWithTag(NO_ITEMS_TEXT_TESTING_TAG)
.assertTextEquals(context.getString(R.string.no_notes))
}
} }
fun assertHomePageIsLoadedOfTestZimFile() { fun assertHomePageIsLoadedOfTestZimFile() {

View File

@ -53,7 +53,7 @@ fun bookmarks(func: BookmarksRobot.() -> Unit) =
class BookmarksRobot : BaseRobot() { class BookmarksRobot : BaseRobot() {
private var retryCountForBookmarkAddedButton = 5 private var retryCountForBookmarkAddedButton = 5
fun assertBookMarksDisplayed(composeTestRule: ComposeTestRule) { fun assertBookMarksDisplayed(composeTestRule: ComposeContentTestRule) {
composeTestRule.apply { composeTestRule.apply {
waitForIdle() waitForIdle()
onNodeWithTag(SWITCH_TEXT_TESTING_TAG) onNodeWithTag(SWITCH_TEXT_TESTING_TAG)
@ -61,7 +61,7 @@ class BookmarksRobot : BaseRobot() {
} }
} }
fun clickOnTrashIcon(composeTestRule: ComposeTestRule) { fun clickOnTrashIcon(composeTestRule: ComposeContentTestRule) {
composeTestRule.apply { composeTestRule.apply {
waitForIdle() waitForIdle()
onNodeWithTag(DELETE_MENU_ICON_TESTING_TAG) onNodeWithTag(DELETE_MENU_ICON_TESTING_TAG)

View File

@ -47,7 +47,7 @@ import org.kiwix.kiwixmobile.core.utils.ComposeDimens.EIGHT_DP
import org.kiwix.kiwixmobile.core.utils.ComposeDimens.PAGE_LIST_ITEM_FAVICON_SIZE import org.kiwix.kiwixmobile.core.utils.ComposeDimens.PAGE_LIST_ITEM_FAVICON_SIZE
import org.kiwix.kiwixmobile.core.utils.ComposeDimens.SIXTEEN_DP import org.kiwix.kiwixmobile.core.utils.ComposeDimens.SIXTEEN_DP
const val PAGE_ITEM_TITLE_TESTING_TAG = "pageItemTitleTestingTag" const val PAGE_ITEM_TESTING_TAG = "pageItemTestingTag"
@OptIn(ExperimentalFoundationApi::class) @OptIn(ExperimentalFoundationApi::class)
@Composable @Composable
@ -66,7 +66,8 @@ fun PageListItem(
.padding( .padding(
horizontal = SIXTEEN_DP, horizontal = SIXTEEN_DP,
vertical = EIGHT_DP vertical = EIGHT_DP
), )
.semantics { testTag = PAGE_ITEM_TESTING_TAG },
verticalAlignment = Alignment.CenterVertically verticalAlignment = Alignment.CenterVertically
) { ) {
Image( Image(
@ -85,9 +86,7 @@ fun PageListItem(
Text( Text(
text = page.title, text = page.title,
style = MaterialTheme.typography.bodyLarge, style = MaterialTheme.typography.bodyLarge,
modifier = Modifier modifier = Modifier.weight(1f),
.weight(1f)
.semantics { testTag = PAGE_ITEM_TITLE_TESTING_TAG },
maxLines = 1, maxLines = 1,
overflow = TextOverflow.Ellipsis overflow = TextOverflow.Ellipsis
) )