Improved the DownloadTest to download the smallest ZIM file, as it was previously downloading a 156MB file, which took too much time.

This commit is contained in:
MohitMaliFtechiz 2024-10-05 18:28:49 +05:30 committed by Kelson
parent 3c8b39058e
commit e9154cdace
3 changed files with 95 additions and 8 deletions

View File

@ -18,7 +18,11 @@
package org.kiwix.kiwixmobile.download package org.kiwix.kiwixmobile.download
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import androidx.test.espresso.Espresso.onView import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.UiController
import androidx.test.espresso.ViewAction
import androidx.test.espresso.action.ViewActions.click import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions.doesNotExist import androidx.test.espresso.assertion.ViewAssertions.doesNotExist
import androidx.test.espresso.assertion.ViewAssertions.matches import androidx.test.espresso.assertion.ViewAssertions.matches
@ -30,6 +34,7 @@ import applyWithViewHierarchyPrinting
import com.adevinta.android.barista.interaction.BaristaSleepInteractions import com.adevinta.android.barista.interaction.BaristaSleepInteractions
import com.adevinta.android.barista.interaction.BaristaSwipeRefreshInteractions.refresh import com.adevinta.android.barista.interaction.BaristaSwipeRefreshInteractions.refresh
import junit.framework.AssertionFailedError import junit.framework.AssertionFailedError
import org.hamcrest.Matcher
import org.junit.Assert import org.junit.Assert
import org.kiwix.kiwixmobile.BaseRobot import org.kiwix.kiwixmobile.BaseRobot
import org.kiwix.kiwixmobile.Findable.StringId.TextId import org.kiwix.kiwixmobile.Findable.StringId.TextId
@ -40,6 +45,7 @@ import org.kiwix.kiwixmobile.core.utils.files.Log
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
import org.kiwix.kiwixmobile.utils.RecyclerViewMatcher import org.kiwix.kiwixmobile.utils.RecyclerViewMatcher
import org.kiwix.kiwixmobile.zimManager.libraryView.adapter.LibraryListItem
fun downloadRobot(func: DownloadRobot.() -> Unit) = fun downloadRobot(func: DownloadRobot.() -> Unit) =
DownloadRobot().applyWithViewHierarchyPrinting(func) DownloadRobot().applyWithViewHierarchyPrinting(func)
@ -102,12 +108,12 @@ class DownloadRobot : BaseRobot() {
refresh(R.id.librarySwipeRefresh) refresh(R.id.librarySwipeRefresh)
} }
fun downloadZimFile() { fun downloadZimFile(position: Int = 1) {
pauseForBetterTestPerformance() pauseForBetterTestPerformance()
testFlakyView({ testFlakyView({
onView( onView(
RecyclerViewMatcher(R.id.libraryList).atPosition( RecyclerViewMatcher(R.id.libraryList).atPosition(
1 position
) )
).perform(click()) ).perform(click())
}) })
@ -190,4 +196,52 @@ class DownloadRobot : BaseRobot() {
) )
} }
} }
fun getSmallestZimFileIndex(it: List<LibraryListItem>?): Int {
var zimFileSizeWithIndex: Pair<Int, Long> = 0 to Long.MAX_VALUE
it?.forEachIndexed { index, libraryItem ->
if (libraryItem is LibraryListItem.BookItem) {
val bookSize = libraryItem.book.size.toLong()
if (bookSize < 20000L) {
return@getSmallestZimFileIndex index
} else if (bookSize < zimFileSizeWithIndex.second) {
zimFileSizeWithIndex = index to bookSize
}
}
}
return zimFileSizeWithIndex.first
}
fun scrollToZimFileIndex(index: Int) {
testFlakyView({
onView(withId(R.id.libraryList))
.perform(scrollToTop(index))
})
}
private fun scrollToTop(position: Int): ViewAction {
return object : ViewAction {
override fun getDescription(): String =
"scroll RecyclerView item at position $position to the top"
override fun getConstraints(): Matcher<View> =
androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom(RecyclerView::class.java)
override fun perform(uiController: UiController, view: View) {
val recyclerView = view as RecyclerView
val viewHolder = recyclerView.findViewHolderForAdapterPosition(position)
if (viewHolder?.itemView == null) {
recyclerView.scrollToPosition(position)
uiController.loopMainThreadUntilIdle()
}
val newViewHolder = recyclerView.findViewHolderForAdapterPosition(position)
newViewHolder?.let {
val top = newViewHolder.itemView.top
recyclerView.scrollBy(0, top)
}
}
}
}
} }

View File

@ -19,6 +19,7 @@ package org.kiwix.kiwixmobile.download
import androidx.core.content.edit import androidx.core.content.edit
import androidx.lifecycle.Lifecycle import androidx.lifecycle.Lifecycle
import androidx.navigation.fragment.NavHostFragment
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import androidx.test.core.app.ActivityScenario import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.IdlingPolicies import androidx.test.espresso.IdlingPolicies
@ -39,7 +40,6 @@ import org.junit.After
import org.junit.Assert import org.junit.Assert
import org.junit.Before import org.junit.Before
import org.junit.BeforeClass import org.junit.BeforeClass
import org.junit.Rule
import org.junit.Test import org.junit.Test
import org.junit.runner.RunWith import org.junit.runner.RunWith
import org.kiwix.kiwixmobile.BaseActivityTest import org.kiwix.kiwixmobile.BaseActivityTest
@ -49,21 +49,24 @@ import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.main.KiwixMainActivity import org.kiwix.kiwixmobile.main.KiwixMainActivity
import org.kiwix.kiwixmobile.main.topLevel import org.kiwix.kiwixmobile.main.topLevel
import org.kiwix.kiwixmobile.nav.destination.library.LibraryRobot import org.kiwix.kiwixmobile.nav.destination.library.LibraryRobot
import org.kiwix.kiwixmobile.nav.destination.library.OnlineLibraryFragment
import org.kiwix.kiwixmobile.nav.destination.library.library 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
import org.kiwix.kiwixmobile.testutils.TestUtils.closeSystemDialogs import org.kiwix.kiwixmobile.testutils.TestUtils.closeSystemDialogs
import org.kiwix.kiwixmobile.testutils.TestUtils.isSystemUINotRespondingDialogVisible import org.kiwix.kiwixmobile.testutils.TestUtils.isSystemUINotRespondingDialogVisible
import org.kiwix.kiwixmobile.utils.KiwixIdlingResource.Companion.getInstance import org.kiwix.kiwixmobile.utils.KiwixIdlingResource.Companion.getInstance
import org.kiwix.kiwixmobile.zimManager.libraryView.adapter.LibraryListItem
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@LargeTest @LargeTest
@RunWith(AndroidJUnit4::class) @RunWith(AndroidJUnit4::class)
class DownloadTest : BaseActivityTest() { class DownloadTest : BaseActivityTest() {
@Rule // @Rule
@JvmField // @JvmField
var retryRule = RetryRule() // var retryRule = RetryRule()
private lateinit var kiwixMainActivity: KiwixMainActivity
init { init {
AccessibilityChecks.enable().apply { AccessibilityChecks.enable().apply {
@ -115,14 +118,24 @@ class DownloadTest : BaseActivityTest() {
fun downloadTest() { fun downloadTest() {
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong()) BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong())
activityScenario.onActivity { activityScenario.onActivity {
kiwixMainActivity = it
it.navigate(R.id.libraryFragment) it.navigate(R.id.libraryFragment)
} }
try { try {
// delete all the ZIM files showing in the LocalLibrary
// screen to properly test the scenario.
library {
refreshList()
waitUntilZimFilesRefreshing()
deleteZimIfExists()
}
downloadRobot { downloadRobot {
clickDownloadOnBottomNav() clickDownloadOnBottomNav()
waitForDataToLoad() waitForDataToLoad()
val smallestZimFileIndex = getSmallestZimFileIndex(getOnlineLibraryList())
scrollToZimFileIndex(smallestZimFileIndex)
stopDownloadIfAlreadyStarted() stopDownloadIfAlreadyStarted()
downloadZimFile() downloadZimFile(smallestZimFileIndex)
assertDownloadStart() assertDownloadStart()
pauseDownload() pauseDownload()
assertDownloadPaused() assertDownloadPaused()
@ -142,6 +155,15 @@ class DownloadTest : BaseActivityTest() {
LeakAssertions.assertNoLeaks() LeakAssertions.assertNoLeaks()
} }
private fun getOnlineLibraryList(): List<LibraryListItem> {
val navHostFragment: NavHostFragment =
kiwixMainActivity.supportFragmentManager
.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val onlineLibraryFragment =
navHostFragment.childFragmentManager.fragments[0] as OnlineLibraryFragment
return onlineLibraryFragment.getOnlineLibraryList()
}
@Test @Test
fun testPauseAndResumeInOtherLanguage() { fun testPauseAndResumeInOtherLanguage() {
BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong()) BaristaSleepInteractions.sleep(TestUtils.TEST_PAUSE_MS.toLong())
@ -149,6 +171,13 @@ class DownloadTest : BaseActivityTest() {
it.navigate(R.id.libraryFragment) it.navigate(R.id.libraryFragment)
} }
try { try {
// delete all the ZIM files showing in the LocalLibrary
// screen to properly test the scenario.
library {
refreshList()
waitUntilZimFilesRefreshing()
deleteZimIfExists()
}
downloadRobot { downloadRobot {
// change the application language // change the application language
topLevel { topLevel {

View File

@ -34,6 +34,7 @@ import android.view.MenuItem
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.Toast import android.widget.Toast
import androidx.annotation.VisibleForTesting
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.SearchView
import androidx.appcompat.widget.Toolbar import androidx.appcompat.widget.Toolbar
@ -111,6 +112,9 @@ class OnlineLibraryFragment : BaseFragment(), FragmentActivityExtensions {
requireActivity().viewModel<ZimManageViewModel>(viewModelFactory) requireActivity().viewModel<ZimManageViewModel>(viewModelFactory)
} }
@VisibleForTesting
fun getOnlineLibraryList() = libraryAdapter.items
private val libraryAdapter: LibraryAdapter by lazy { private val libraryAdapter: LibraryAdapter by lazy {
LibraryAdapter( LibraryAdapter(
LibraryDelegate.BookDelegate(bookUtils, ::onBookItemClick, availableSpaceCalculator), LibraryDelegate.BookDelegate(bookUtils, ::onBookItemClick, availableSpaceCalculator),