Skip to content

Commit

Permalink
Using uiDevice.waitForIdle() method instead of `BaristaSleepInterac…
Browse files Browse the repository at this point in the history
…tions.sleep` in our test cases. Because `BaristaSleepInteractions.sleep` runs everytime even the view is displaying on the screen which takes the extra time to run the test case, but now we are using `uiDevice.waitForIdle()` method which perform the further operation if desired view is already displaying otherwise wait for that view.

* Improved the deleting of zim files in localibrary, since we already pause for a second to check if the delete dialog is displayed or not, and again we are waiting before clicking on it. The same issue for while clicking on the PlayStoreRestrictionDialogTest where we are first waiting for the dialog to visible and then waiting for clicking on it. So we have removed the second wait since this dialog is displaying in both test cases.
* Improved the TopLevelDestinationTest. Because we were testing the drawer options fragments (Settings, Bookmarks etc) from the download screen and when we pressed the back button it came to the download screen and refreshed its data due to this our next functionality had to wait to finish this. Now we are testing all the navigation screens from the LocalLibraryScreen which makes this test case faster.
  • Loading branch information
MohitMaliDeveloper committed Mar 12, 2024
1 parent 427b07f commit e21a89a
Show file tree
Hide file tree
Showing 12 changed files with 13 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ abstract class BaseRobot(
var uiObject2: UiObject2? = null
for (i in 0 until retryCount) {
try {
uiDevice.waitForIdle()
uiObject2 = uiDevice.wait(Until.findObject(findable.selector(this)), timeout)
break
} catch (noMatchingViewException: NoMatchingViewException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ class DownloadRobot : BaseRobot() {
}

private fun assertStopDownloadDialogDisplayed() {
pauseForBetterTestPerformance()
isVisible(Text("Stop download?"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import com.adevinta.android.barista.interaction.BaristaSleepInteractions
import leakcanary.LeakAssertions
import org.junit.After
import org.junit.Assert
Expand All @@ -41,7 +40,6 @@ import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.main.KiwixMainActivity
import org.kiwix.kiwixmobile.nav.destination.library.library
import org.kiwix.kiwixmobile.testutils.RetryRule
import org.kiwix.kiwixmobile.testutils.TestUtils
import org.kiwix.kiwixmobile.testutils.TestUtils.closeSystemDialogs
import org.kiwix.kiwixmobile.testutils.TestUtils.isSystemUINotRespondingDialogVisible
import org.kiwix.kiwixmobile.utils.KiwixIdlingResource.Companion.getInstance
Expand Down Expand Up @@ -80,7 +78,6 @@ class DownloadTest : BaseActivityTest() {

@Test
fun downloadTest() {
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong())
try {
downloadRobot {
clickLibraryOnBottomNav()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,22 @@ class LocalFileTransferRobot : BaseRobot() {
}

fun assertClickNearbyDeviceMessageVisible() {
pauseForBetterTestPerformance()
isVisible(TextId(R.string.click_nearby_devices_message))
}

fun clickOnGotItButton() {
pauseForBetterTestPerformance()
clickOn(TextId(R.string.got_it))
}

fun assertDeviceNameMessageVisible() {
pauseForBetterTestPerformance()
isVisible(TextId(R.string.your_device_name_message))
}

fun assertNearbyDeviceListMessageVisible() {
pauseForBetterTestPerformance()
isVisible(TextId(R.string.nearby_devices_list_message))
}

fun assertTransferZimFilesListMessageVisible() {
pauseForBetterTestPerformance()
isVisible(TextId(R.string.transfer_zim_files_list_message))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ class TopLevelDestinationTest : BaseActivityTest() {
topLevel {
clickReaderOnBottomNav {
}
clickDownloadOnBottomNav(OnlineLibraryRobot::assertLibraryListDisplayed)
clickLibraryOnBottomNav {
assertGetZimNearbyDeviceDisplayed()
clickFileTransferIcon(LocalFileTransferRobot::assertReceiveFileTitleVisible)
}
clickDownloadOnBottomNav(OnlineLibraryRobot::assertLibraryListDisplayed)
clickBookmarksOnNavDrawer {
assertBookMarksDisplayed()
clickOnTrashIcon()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ fun playStoreRestriction(func: PlayStoreRestrictionDialogRobot.() -> Unit) =
class PlayStoreRestrictionDialogRobot : BaseRobot() {

fun clickLibraryOnBottomNav() {
pauseForBetterTestPerformance()
clickOn(ViewId(R.id.libraryFragment))
}

Expand All @@ -54,7 +53,6 @@ class PlayStoreRestrictionDialogRobot : BaseRobot() {
}

fun clickOnUnderstood() {
pauseForBetterTestPerformance()
onView(withText("UNDERSTOOD"))
.perform(click())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,24 @@

package org.kiwix.kiwixmobile.nav.destination.library

import org.kiwix.kiwixmobile.core.utils.files.Log
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.action.ViewActions.longClick
import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition
import androidx.test.espresso.contrib.RecyclerViewActions.scrollToPosition
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import applyWithViewHierarchyPrinting
import com.adevinta.android.barista.interaction.BaristaSleepInteractions
import junit.framework.AssertionFailedError
import org.kiwix.kiwixmobile.BaseRobot
import org.kiwix.kiwixmobile.Findable.ViewId
import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.core.utils.files.Log
import org.kiwix.kiwixmobile.localFileTransfer.LocalFileTransferRobot
import org.kiwix.kiwixmobile.localFileTransfer.localFileTransfer
import org.kiwix.kiwixmobile.testutils.TestUtils
Expand All @@ -59,12 +61,18 @@ class LibraryRobot : BaseRobot() {
}

fun assertNoFilesTextDisplayed() {
pauseForBetterTestPerformance()
isVisible(ViewId(R.id.file_management_no_files))
}

fun deleteZimIfExists() {
try {
try {
onView(withId(R.id.file_management_no_files)).check(matches(isDisplayed()))
// if this view is displaying then we do not need to run the further code.
return
} catch (e: AssertionFailedError) {
Log.e("DELETE_ZIM_FILE", "Zim files found in local library so we are deleting them")
}
val recyclerViewId: Int = R.id.zimfilelist
val recyclerViewItemsCount = RecyclerViewItemCount(recyclerViewId).checkRecyclerViewCount()
// Scroll to the end of the RecyclerView to ensure all items are visible
Expand All @@ -79,7 +87,6 @@ class LibraryRobot : BaseRobot() {
clickOnFileDeleteIcon()
assertDeleteDialogDisplayed()
clickOnDeleteZimFile()
pauseForBetterTestPerformance()
} catch (e: Exception) {
Log.i(
"TEST_DELETE_ZIM",
Expand All @@ -90,7 +97,6 @@ class LibraryRobot : BaseRobot() {
}

private fun clickOnFileDeleteIcon() {
pauseForBetterTestPerformance()
clickOn(ViewId(R.id.zim_file_delete_item))
}

Expand All @@ -101,7 +107,6 @@ class LibraryRobot : BaseRobot() {
}

private fun clickOnDeleteZimFile() {
pauseForBetterTestPerformance()
onView(withText("DELETE")).perform(click())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ class NoteRobot : BaseRobot() {
}

fun assertBackwardNavigationHistoryDialogDisplayed() {
pauseForBetterTestPerformance()
isVisible(TextId(R.string.note))
}

Expand All @@ -76,7 +75,6 @@ class NoteRobot : BaseRobot() {
}

fun saveNote() {
pauseForBetterTestPerformance()
clickOn(ViewId(R.id.save_note))
}

Expand All @@ -95,7 +93,6 @@ class NoteRobot : BaseRobot() {
}

fun clickOnOpenNote() {
pauseForBetterTestPerformance()
clickOn(Text("OPEN NOTE"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class BookmarksRobot : BaseRobot() {
}

fun clickOnSaveBookmarkImage() {
pauseForBetterTestPerformance()
clickOn(ViewId(R.id.bottom_toolbar_bookmark))
}

Expand All @@ -90,7 +89,6 @@ class BookmarksRobot : BaseRobot() {
}

fun assertBookmarkSaved() {
pauseForBetterTestPerformance()
isVisible(Text("Test Zim"))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ fun navigationHistory(func: NavigationHistoryRobot.() -> Unit) =
class NavigationHistoryRobot : BaseRobot() {

fun checkZimFileLoadedSuccessful(readerFragment: Int) {
pauseForBetterTestPerformance()
isVisible(ViewId(readerFragment))
}

Expand All @@ -52,37 +51,30 @@ class NavigationHistoryRobot : BaseRobot() {
}

fun longClickOnBackwardButton() {
pauseForBetterTestPerformance()
longClickOn(ViewId(R.id.bottom_toolbar_arrow_back))
}

fun longClickOnForwardButton() {
pauseForBetterTestPerformance()
longClickOn(ViewId(R.id.bottom_toolbar_arrow_forward))
}

fun assertBackwardNavigationHistoryDialogDisplayed() {
pauseForBetterTestPerformance()
isVisible(TextId(R.string.backward_history))
}

fun clickOnBackwardButton() {
pauseForBetterTestPerformance()
clickOn(ViewId(R.id.bottom_toolbar_arrow_back))
}

fun assertForwardNavigationHistoryDialogDisplayed() {
pauseForBetterTestPerformance()
isVisible(TextId(R.string.forward_history))
}

fun clickOnDeleteHistory() {
pauseForBetterTestPerformance()
clickOn(ViewId(R.id.menu_pages_clear))
}

fun assertDeleteDialogDisplayed() {
pauseForBetterTestPerformance()
isVisible(TextId(R.string.clear_all_history_dialog_title))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,14 @@ class ReaderRobot : BaseRobot() {
private var retryCountForClickOnUndoButton = 5

fun checkZimFileLoadedSuccessful(readerFragment: Int) {
pauseForBetterTestPerformance()
isVisible(ViewId(readerFragment))
}

fun clickOnTabIcon() {
pauseForBetterTestPerformance()
clickOn(ViewId(R.id.ic_tab_switcher_text))
}

fun clickOnClosedAllTabsButton() {
pauseForBetterTestPerformance()
clickOn(ViewId(R.id.tab_switcher_close_all_tabs))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.assertThat
import androidx.test.espresso.matcher.ViewMatchers.withText
import applyWithViewHierarchyPrinting
import com.adevinta.android.barista.interaction.BaristaSleepInteractions
import com.adevinta.android.barista.interaction.BaristaSwipeRefreshInteractions.refresh
import junit.framework.AssertionFailedError
import org.hamcrest.CoreMatchers
Expand All @@ -35,7 +34,6 @@ import org.kiwix.kiwixmobile.Findable.StringId.TextId
import org.kiwix.kiwixmobile.Findable.Text
import org.kiwix.kiwixmobile.Findable.ViewId
import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.testutils.TestUtils
import org.kiwix.kiwixmobile.utils.RecyclerViewItemCount
import org.kiwix.kiwixmobile.utils.RecyclerViewMatcher
import org.kiwix.kiwixmobile.utils.RecyclerViewSelectedCheckBoxCountAssertion
Expand All @@ -50,12 +48,10 @@ class ZimHostRobot : BaseRobot() {
}

fun refreshLibraryList() {
pauseForBetterTestPerformance()
refresh(R.id.zim_swiperefresh)
}

fun assertZimFilesLoaded() {
pauseForBetterTestPerformance()
isVisible(Text("Test_Zim"))
}

Expand All @@ -65,7 +61,6 @@ class ZimHostRobot : BaseRobot() {
}

fun clickOnTestZim() {
pauseForBetterTestPerformance()
clickOn(Text("Test_Zim"))
}

Expand All @@ -78,12 +73,10 @@ class ZimHostRobot : BaseRobot() {
}

private fun assetWifiDialogDisplayed() {
pauseForBetterTestPerformance()
isVisible(Text("WiFi connection detected"))
}

fun assertServerStarted() {
pauseForBetterTestPerformance()
isVisible(Text("STOP SERVER"))
}

Expand Down Expand Up @@ -149,11 +142,6 @@ class ZimHostRobot : BaseRobot() {
}

fun assertServerStopped() {
pauseForBetterTestPerformance()
isVisible(Text("START SERVER"))
}

private fun pauseForBetterTestPerformance() {
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong())
}
}

0 comments on commit e21a89a

Please sign in to comment.