Fixed: The NoteFragmentTest was failing.

* Improved the setting of text in noteTextField for better maintainability.
This commit is contained in:
MohitMaliFtechiz 2025-03-11 12:44:40 +05:30
parent 4444a6f236
commit 58b32b5350
5 changed files with 53 additions and 25 deletions

View File

@ -221,7 +221,7 @@ class NoteFragmentTest : BaseActivityTest() {
clickOnSavedNote()
clickOnOpenNote()
assertNoteSaved(composeTestRule)
clickOnDeleteIcon()
clickOnDeleteIcon(composeTestRule)
pressBack()
assertNoNotesTextDisplayed()
}
@ -245,7 +245,7 @@ class NoteFragmentTest : BaseActivityTest() {
note {
clickOnNoteMenuItem(context)
assertNoteDialogDisplayed(composeTestRule)
assertNotDoesNotExist()
assertNotDoesNotExist(composeTestRule)
pressBack()
}
}

View File

@ -19,17 +19,17 @@
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.junit4.ComposeContentTestRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextInput
import androidx.compose.ui.test.performTextReplacement
import androidx.recyclerview.widget.RecyclerView
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.assertion.ViewAssertions.doesNotExist
import androidx.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.withContentDescription
@ -45,6 +45,7 @@ import org.kiwix.kiwixmobile.Findable.Text
import org.kiwix.kiwixmobile.Findable.ViewId
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.ui.components.TOOLBAR_TITLE_TESTING_TAG
import org.kiwix.kiwixmobile.testutils.TestUtils
@ -77,21 +78,26 @@ class NoteRobot : BaseRobot() {
}
fun assertNoteDialogDisplayed(composeTestRule: ComposeContentTestRule) {
pauseForBetterTestPerformance()
testFlakyView({
composeTestRule.waitForIdle()
composeTestRule.onNodeWithTag(TOOLBAR_TITLE_TESTING_TAG)
.assertTextEquals(context.getString(R.string.note))
})
}
fun writeDemoNote(composeTestRule: ComposeContentTestRule) {
pauseForBetterTestPerformance()
testFlakyView({
composeTestRule.waitForIdle()
// Click on the TextField to focus it
composeTestRule.onNodeWithTag(ADD_NOTE_TEXT_FILED_TESTING_TAG)
.assertExists("TextField not found in dialog")
.performClick()
.performTextInput(noteText)
.performTextReplacement(noteText)
composeTestRule.waitForIdle()
composeTestRule.onNodeWithTag(ADD_NOTE_TEXT_FILED_TESTING_TAG)
.assertTextContains(noteText, substring = true)
// Close the keyboard after typing
closeSoftKeyboard()
@ -99,8 +105,8 @@ class NoteRobot : BaseRobot() {
}
fun saveNote(composeTestRule: ComposeContentTestRule) {
pauseForBetterTestPerformance()
testFlakyView({
composeTestRule.waitForIdle()
composeTestRule.onNodeWithTag(SAVE_MENU_BUTTON_TESTING_TAG)
.performClick()
})
@ -130,18 +136,26 @@ class NoteRobot : BaseRobot() {
// This is flaky since it is shown in a dialog and sometimes
// UIDevice does not found the view immediately due to rendering process.
testFlakyView({
composeTestRule.waitForIdle()
composeTestRule.onNodeWithTag(ADD_NOTE_TEXT_FILED_TESTING_TAG)
.assertTextEquals(noteText)
})
}
fun assertNotDoesNotExist() {
testFlakyView({ onView(withText(noteText)).check(doesNotExist()) })
fun assertNotDoesNotExist(composeTestRule: ComposeContentTestRule) {
testFlakyView({
composeTestRule.waitForIdle()
composeTestRule.onNodeWithTag(ADD_NOTE_TEXT_FILED_TESTING_TAG)
.assertTextContains("", ignoreCase = true)
})
}
fun clickOnDeleteIcon() {
pauseForBetterTestPerformance()
testFlakyView({ clickOn(ViewId(R.id.delete_note)) })
fun clickOnDeleteIcon(composeTestRule: ComposeContentTestRule) {
testFlakyView({
composeTestRule.waitForIdle()
composeTestRule.onNodeWithTag(DELETE_MENU_BUTTON_TESTING_TAG)
.performClick()
})
}
fun clickOnTrashIcon() {

View File

@ -187,7 +187,9 @@ class AddNoteDialog : DialogFragment() {
},
noteText = noteText.value,
actionMenuItems = menuItems.value,
onTextChange = { text -> enableSaveAndShareMenuButtonAndSetTextEdited(text) },
onTextChange = { textInputFiled ->
enableSaveAndShareMenuButtonAndSetTextEdited(textInputFiled)
},
isNoteFileExist = noteFileExists.value,
snackBarHostState = snackBarHostState
)
@ -228,13 +230,9 @@ class AddNoteDialog : DialogFragment() {
)
)
private fun enableSaveAndShareMenuButtonAndSetTextEdited(text: String) {
private fun enableSaveAndShareMenuButtonAndSetTextEdited(textFieldValue: TextFieldValue) {
noteEdited = true
noteText.value = TextFieldValue(
text = text,
// Moves cursor to end
selection = TextRange(text.length)
)
noteText.value = textFieldValue
enableSaveNoteMenuItem()
enableShareNoteMenuItem()
}
@ -430,7 +428,12 @@ class AddNoteDialog : DialogFragment() {
}
private fun restoreDeletedNote(text: String) {
enableSaveAndShareMenuButtonAndSetTextEdited(text)
val restoreNoteTextFieldValue = TextFieldValue(
text = text,
// Moves cursor to end
selection = TextRange(text.length)
)
enableSaveAndShareMenuButtonAndSetTextEdited(restoreNoteTextFieldValue)
}
/* String content of the note text file given at:

View File

@ -57,6 +57,7 @@ import org.kiwix.kiwixmobile.core.utils.ComposeDimens.FOUR_DP
import org.kiwix.kiwixmobile.core.utils.ComposeDimens.MINIMUM_HEIGHT_OF_NOTE_TEXT_FILED
import org.kiwix.kiwixmobile.core.utils.ComposeDimens.TEN_DP
import org.kiwix.kiwixmobile.core.utils.ComposeDimens.TWENTY_DP
import org.kiwix.kiwixmobile.core.utils.TestingUtils.isRunningTest
const val ADD_NOTE_TEXT_FILED_TESTING_TAG = "addNoteTextFiledTestingTag"
const val SAVE_MENU_BUTTON_TESTING_TAG = "saveMenuButtonTestingTag"
@ -69,7 +70,7 @@ fun AddNoteDialogScreen(
articleTitle: String,
noteText: TextFieldValue,
actionMenuItems: List<ActionMenuItem>,
onTextChange: (String) -> Unit,
onTextChange: (TextFieldValue) -> Unit,
isNoteFileExist: Boolean,
snackBarHostState: SnackbarHostState,
navigationIcon: @Composable () -> Unit
@ -103,7 +104,7 @@ fun AddNoteDialogScreen(
}
LaunchedEffect(isNoteFileExist) {
if (!isNoteFileExist) {
if (!isNoteFileExist && !isRunningTest()) {
focusRequester.requestFocus()
focusManager.moveFocus(FocusDirection.Down)
}
@ -123,12 +124,12 @@ private fun ArticleTitleText(articleTitle: String) {
@Composable
private fun NoteTextField(
noteText: TextFieldValue,
onTextChange: (String) -> Unit,
onTextChange: (TextFieldValue) -> Unit,
focusRequester: FocusRequester
) {
TextField(
value = noteText,
onValueChange = { onTextChange(it.text) },
onValueChange = { onTextChange(it) },
maxLines = 6,
modifier = Modifier
.fillMaxWidth()

View File

@ -42,6 +42,16 @@ object TestingUtils {
}
}
@JvmStatic
fun isRunningTest(): Boolean {
return try {
Class.forName("androidx.test.espresso.Espresso")
true
} catch (_: ClassNotFoundException) {
false
}
}
@JvmStatic fun registerIdleCallback(listListener: IdleListener) {
resources.clear()
callback = listListener