Refactored the remaining UI test cases.

This commit is contained in:
MohitMaliFtechiz 2025-06-26 17:21:59 +05:30
parent a51615e249
commit a6eb00f6b9
7 changed files with 72 additions and 43 deletions

View File

@ -133,7 +133,7 @@ class NoteFragmentTest : BaseActivityTest() {
loadZimFileInReader("testzim.zim")
StandardActions.closeDrawer() // close the drawer if open before running the test cases.
note {
clickOnNoteMenuItem(context)
clickOnNoteMenuItem(composeTestRule)
assertNoteDialogDisplayed(composeTestRule)
writeDemoNote(composeTestRule)
saveNote(composeTestRule)
@ -180,7 +180,7 @@ class NoteFragmentTest : BaseActivityTest() {
loadZimFileInReader("testzim.zim")
note {
assertHomePageIsLoadedOfTestZimFile()
clickOnNoteMenuItem(context)
clickOnNoteMenuItem(composeTestRule)
assertNoteDialogDisplayed(composeTestRule)
writeDemoNote(composeTestRule)
saveNote(composeTestRule)
@ -203,7 +203,7 @@ class NoteFragmentTest : BaseActivityTest() {
deletePreviouslySavedNotes()
loadZimFileInReader("testzim.zim")
note {
clickOnNoteMenuItem(context)
clickOnNoteMenuItem(composeTestRule)
assertNoteDialogDisplayed(composeTestRule)
writeDemoNote(composeTestRule)
saveNote(composeTestRule)
@ -226,7 +226,7 @@ class NoteFragmentTest : BaseActivityTest() {
loadZimFileInReader("testzim.zim")
// Save a note.
note {
clickOnNoteMenuItem(context)
clickOnNoteMenuItem(composeTestRule)
assertNoteDialogDisplayed(composeTestRule)
writeDemoNote(composeTestRule)
saveNote(composeTestRule)
@ -236,7 +236,7 @@ class NoteFragmentTest : BaseActivityTest() {
deletePreviouslySavedNotes()
// Test the note file is deleted or not.
note {
clickOnNoteMenuItem(context)
clickOnNoteMenuItem(composeTestRule)
assertNoteDialogDisplayed(composeTestRule)
assertNotDoesNotExist(composeTestRule)
pressBack()

View File

@ -18,18 +18,19 @@
package org.kiwix.kiwixmobile.note
import android.content.Context
import androidx.compose.ui.test.assertTextContains
import androidx.compose.ui.test.assertTextEquals
import androidx.compose.ui.test.filter
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.isDisplayed
import androidx.compose.ui.test.junit4.ComposeContentTestRule
import androidx.compose.ui.test.onAllNodesWithTag
import androidx.compose.ui.test.onFirst
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextReplacement
import androidx.test.espresso.Espresso.closeSoftKeyboard
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.espresso.web.sugar.Web.onWebView
@ -37,21 +38,23 @@ import androidx.test.espresso.web.webdriver.DriverAtoms.findElement
import androidx.test.espresso.web.webdriver.Locator
import com.adevinta.android.barista.interaction.BaristaSleepInteractions
import org.kiwix.kiwixmobile.BaseRobot
import org.kiwix.kiwixmobile.Findable.StringId.TextId
import org.kiwix.kiwixmobile.core.R
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.SAVE_MENU_BUTTON_TESTING_TAG
import org.kiwix.kiwixmobile.core.main.reader.TAKE_NOTE_MENU_ITEM_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.OVERFLOW_MENU_BUTTON_TESTING_TAG
import org.kiwix.kiwixmobile.core.ui.components.TOOLBAR_TITLE_TESTING_TAG
import org.kiwix.kiwixmobile.core.utils.dialog.ALERT_DIALOG_CONFIRM_BUTTON_TESTING_TAG
import org.kiwix.kiwixmobile.core.utils.dialog.ALERT_DIALOG_DISMISS_BUTTON_TESTING_TAG
import org.kiwix.kiwixmobile.core.utils.dialog.ALERT_DIALOG_TITLE_TEXT_TESTING_TAG
import org.kiwix.kiwixmobile.testutils.TestUtils
import org.kiwix.kiwixmobile.testutils.TestUtils.testFlakyView
import org.kiwix.kiwixmobile.testutils.TestUtils.waitUntilTimeout
import org.kiwix.kiwixmobile.utils.StandardActions.openDrawer
fun note(func: NoteRobot.() -> Unit) = NoteRobot().apply(func)
@ -75,12 +78,13 @@ class NoteRobot : BaseRobot() {
}
}
fun clickOnNoteMenuItem(context: Context) {
// wait a bit to properly load the readerFragment and setting up the
// overFlowOptionMenu so that we can easily click on it.
pauseForBetterTestPerformance()
openActionBarOverflowOrOptionsMenu(context)
clickOn(TextId(R.string.take_notes))
fun clickOnNoteMenuItem(composeTestRule: ComposeContentTestRule) {
composeTestRule.apply {
waitUntilTimeout()
onNodeWithTag(OVERFLOW_MENU_BUTTON_TESTING_TAG).performClick()
waitUntilTimeout()
onNodeWithTag(TAKE_NOTE_MENU_ITEM_TESTING_TAG).performClick()
}
}
fun assertNoteDialogDisplayed(composeTestRule: ComposeContentTestRule) {
@ -88,9 +92,12 @@ class NoteRobot : BaseRobot() {
composeTestRule.waitForIdle()
composeTestRule.waitUntil(
TestUtils.TEST_PAUSE_MS.toLong()
) { composeTestRule.onNodeWithTag(TOOLBAR_TITLE_TESTING_TAG).isDisplayed() }
composeTestRule.onNodeWithTag(TOOLBAR_TITLE_TESTING_TAG)
.assertTextEquals(context.getString(R.string.note))
) {
composeTestRule.onAllNodesWithTag(TOOLBAR_TITLE_TESTING_TAG)
.filter(hasText(context.getString(R.string.note)))
.onFirst()
.isDisplayed()
}
})
}

View File

@ -23,19 +23,19 @@ import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.isDisplayed
import androidx.compose.ui.test.junit4.ComposeContentTestRule
import androidx.compose.ui.test.junit4.ComposeTestRule
import androidx.compose.ui.test.longClick
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performScrollToNode
import androidx.compose.ui.test.performTouchInput
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.longClick
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import applyWithViewHierarchyPrinting
import com.adevinta.android.barista.interaction.BaristaSleepInteractions
import org.kiwix.kiwixmobile.BaseRobot
import org.kiwix.kiwixmobile.Findable.ViewId
import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.page.DELETE_MENU_ICON_TESTING_TAG
import org.kiwix.kiwixmobile.core.page.NO_ITEMS_TEXT_TESTING_TAG
@ -45,9 +45,10 @@ import org.kiwix.kiwixmobile.core.page.bookmark.adapter.LibkiwixBookmarkItem
import org.kiwix.kiwixmobile.core.utils.dialog.ALERT_DIALOG_CONFIRM_BUTTON_TESTING_TAG
import org.kiwix.kiwixmobile.core.utils.dialog.ALERT_DIALOG_TITLE_TEXT_TESTING_TAG
import org.kiwix.kiwixmobile.testutils.TestUtils
import org.kiwix.kiwixmobile.testutils.TestUtils.TEST_PAUSE_MS
import org.kiwix.kiwixmobile.testutils.TestUtils.testFlakyView
import org.kiwix.kiwixmobile.testutils.TestUtils.waitUntilTimeout
import org.kiwix.kiwixmobile.utils.StandardActions.openDrawer
import java.util.concurrent.TimeUnit
fun bookmarks(func: BookmarksRobot.() -> Unit) =
BookmarksRobot().applyWithViewHierarchyPrinting(func)
@ -100,15 +101,26 @@ class BookmarksRobot : BaseRobot() {
}
}
fun clickOnSaveBookmarkImage() {
pauseForBetterTestPerformance()
clickOn(ViewId(R.id.bottom_toolbar_bookmark))
fun clickOnSaveBookmarkImage(composeTestRule: ComposeContentTestRule) {
composeTestRule.apply {
waitUntilTimeout()
onNodeWithContentDescription(context.getString(R.string.bookmarks))
.performClick()
}
}
fun longClickOnSaveBookmarkImage() {
// wait for disappearing the snack-bar after removing the bookmark
BaristaSleepInteractions.sleep(5L, TimeUnit.SECONDS)
testFlakyView({ onView(withId(R.id.bottom_toolbar_bookmark)).perform(longClick()) })
fun longClickOnSaveBookmarkImage(
composeTestRule: ComposeContentTestRule,
timeout: Long = TEST_PAUSE_MS.toLong()
) {
composeTestRule.apply {
// wait for disappearing the snack-bar after removing the bookmark
waitUntilTimeout(timeout)
onNodeWithContentDescription(context.getString(R.string.bookmarks))
.performTouchInput {
longClick()
}
}
}
fun assertBookmarkSaved(composeTestRule: ComposeContentTestRule) {

View File

@ -52,6 +52,8 @@ import org.kiwix.kiwixmobile.main.topLevel
import org.kiwix.kiwixmobile.nav.destination.library.local.LocalLibraryFragmentDirections
import org.kiwix.kiwixmobile.testutils.RetryRule
import org.kiwix.kiwixmobile.testutils.TestUtils
import org.kiwix.kiwixmobile.testutils.TestUtils.TEST_PAUSE_MS_FOR_DOWNLOAD_TEST
import org.kiwix.kiwixmobile.testutils.TestUtils.waitUntilTimeout
import org.kiwix.libkiwix.Book
import org.kiwix.libkiwix.Bookmark
import java.io.File
@ -126,24 +128,25 @@ class LibkiwixBookmarkTest : BaseActivityTest() {
}
bookmarks {
// delete any bookmark if already saved to properly perform this test case.
longClickOnSaveBookmarkImage()
longClickOnSaveBookmarkImage(composeTestRule)
clickOnTrashIcon(composeTestRule)
assertDeleteBookmarksDialogDisplayed(composeTestRule)
clickOnDeleteButton(composeTestRule)
assertNoBookMarkTextDisplayed(composeTestRule)
pressBack()
// Test saving bookmark
clickOnSaveBookmarkImage()
clickOnSaveBookmarkImage(composeTestRule)
openBookmarkScreen()
assertBookmarkSaved(composeTestRule)
pressBack()
// Test removing bookmark
clickOnSaveBookmarkImage()
longClickOnSaveBookmarkImage()
composeTestRule.waitUntilTimeout()
clickOnSaveBookmarkImage(composeTestRule)
longClickOnSaveBookmarkImage(composeTestRule, TEST_PAUSE_MS_FOR_DOWNLOAD_TEST.toLong())
assertBookmarkRemoved(composeTestRule)
pressBack()
// Save the bookmark to test whether it remains saved after the application restarts or not.
clickOnSaveBookmarkImage()
clickOnSaveBookmarkImage(composeTestRule)
}
}
@ -167,13 +170,14 @@ class LibkiwixBookmarkTest : BaseActivityTest() {
}
bookmarks {
// delete any bookmark if already saved to properly perform this test case.
longClickOnSaveBookmarkImage()
longClickOnSaveBookmarkImage(composeTestRule)
clickOnTrashIcon(composeTestRule)
assertDeleteBookmarksDialogDisplayed(composeTestRule)
clickOnDeleteButton(composeTestRule)
assertNoBookMarkTextDisplayed(composeTestRule)
pressBack()
}
composeTestRule.waitUntilTimeout()
val navHostFragment: NavHostFragment =
kiwixMainActivity.supportFragmentManager
.findFragmentById(R.id.nav_host_fragment) as NavHostFragment

View File

@ -236,7 +236,6 @@ class ReaderMenuState(
private fun addReaderMenuItems() {
if (menuItemVisibility[MenuItemType.AddNote] == true) {
menuItems += ActionMenuItem(
icon = IconItem.Drawable(R.drawable.ic_add_note),
contentDescription = R.string.take_notes,
onClick = { menuClickListener.onAddNoteMenuClicked() },
testingTag = TAKE_NOTE_MENU_ITEM_TESTING_TAG,

View File

@ -68,6 +68,7 @@ import org.kiwix.kiwixmobile.core.ui.theme.White
import org.kiwix.kiwixmobile.core.utils.ComposeDimens.SIXTEEN_DP
const val TOOLBAR_TITLE_TESTING_TAG = "toolbarTitle"
const val OVERFLOW_MENU_BUTTON_TESTING_TAG = "overflowMenuButtonTestingTag"
@OptIn(ExperimentalMaterial3Api::class)
@Composable
@ -147,7 +148,10 @@ private fun ActionMenu(actionMenuItems: List<ActionMenuItem>) {
val (mainActions, overflowActions) = actionMenuItems.partition { !it.isInOverflow }
MainMenuItems(mainActions)
if (overflowActions.isNotEmpty()) {
IconButton(onClick = { overflowExpanded = true }) {
IconButton(
onClick = { overflowExpanded = true },
modifier = Modifier.testTag(OVERFLOW_MENU_BUTTON_TESTING_TAG)
) {
Icon(
imageVector = Icons.Default.MoreVert,
contentDescription = null,
@ -224,7 +228,8 @@ private fun OverflowMenuItems(
onDismiss()
menuItem.onClick()
},
enabled = menuItem.isEnabled
enabled = menuItem.isEnabled,
modifier = Modifier.testTag(menuItem.testingTag)
)
}
}

View File

@ -47,6 +47,7 @@ import com.adevinta.android.barista.interaction.BaristaDrawerInteractions.openDr
import com.adevinta.android.barista.interaction.BaristaSleepInteractions
import org.hamcrest.CoreMatchers.containsString
import org.kiwix.kiwixmobile.core.R
import org.kiwix.kiwixmobile.core.page.SEARCH_ICON_TESTING_TAG
import org.kiwix.kiwixmobile.core.search.SEARCH_FIELD_TESTING_TAG
import org.kiwix.kiwixmobile.core.search.SEARCH_ITEM_TESTING_TAG
import org.kiwix.kiwixmobile.custom.R.id
@ -109,17 +110,18 @@ class SearchRobot {
composeTestRule.onNodeWithTag(SEARCH_FIELD_TESTING_TAG).performTextClearance()
}
private fun openSearchScreen() {
testFlakyView({
Espresso.onView(ViewMatchers.withId(R.id.menu_search))
.perform(ViewActions.click())
})
private fun openSearchScreen(composeTestRule: ComposeContentTestRule) {
testFlakyView(
{
composeTestRule.onNodeWithTag(SEARCH_ICON_TESTING_TAG).performClick()
}
)
}
fun searchAndClickOnArticle(searchString: String, composeTestRule: ComposeContentTestRule) {
// wait a bit to properly load the ZIM file in the reader
composeTestRule.waitUntilTimeout()
openSearchScreen()
openSearchScreen(composeTestRule)
// Wait a bit to properly visible the search screen.
composeTestRule.waitUntilTimeout()
searchWithFrequentlyTypedWords(searchString, composeTestRule = composeTestRule)