mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-08 14:52:13 -04:00
Added UI test cases for testing this functionality.
* Improved the unit test cases for this functionality.
This commit is contained in:
parent
a45f94add7
commit
9f6184bbd0
@ -21,6 +21,7 @@ package org.kiwix.kiwixmobile.localLibrary
|
|||||||
import androidx.test.espresso.Espresso.onView
|
import androidx.test.espresso.Espresso.onView
|
||||||
import androidx.test.espresso.action.ViewActions.click
|
import androidx.test.espresso.action.ViewActions.click
|
||||||
import androidx.test.espresso.assertion.ViewAssertions
|
import androidx.test.espresso.assertion.ViewAssertions
|
||||||
|
import androidx.test.espresso.assertion.ViewAssertions.doesNotExist
|
||||||
import androidx.test.espresso.matcher.ViewMatchers
|
import androidx.test.espresso.matcher.ViewMatchers
|
||||||
import androidx.test.espresso.matcher.ViewMatchers.withText
|
import androidx.test.espresso.matcher.ViewMatchers.withText
|
||||||
import androidx.test.espresso.web.sugar.Web
|
import androidx.test.espresso.web.sugar.Web
|
||||||
@ -50,6 +51,12 @@ class CopyMoveFileHandlerRobot : BaseRobot() {
|
|||||||
isVisible(TextId(R.string.copy_move_files_dialog_description))
|
isVisible(TextId(R.string.copy_move_files_dialog_description))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun assertCopyMoveDialogNotDisplayed() {
|
||||||
|
testFlakyView({
|
||||||
|
onView(withText(R.string.copy_move_files_dialog_description)).check(doesNotExist())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
fun clickOnCopy() {
|
fun clickOnCopy() {
|
||||||
testFlakyView({
|
testFlakyView({
|
||||||
onView(withText(R.string.action_copy)).perform(click())
|
onView(withText(R.string.action_copy)).perform(click())
|
||||||
|
@ -203,6 +203,19 @@ class CopyMoveFileHandlerTest : BaseActivityTest() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun tryOpeningInvalidZimFiles(uri: Uri) {
|
||||||
|
UiThreadStatement.runOnUiThread {
|
||||||
|
val navHostFragment: NavHostFragment =
|
||||||
|
kiwixMainActivity.supportFragmentManager
|
||||||
|
.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
|
||||||
|
val localLibraryFragment =
|
||||||
|
navHostFragment.childFragmentManager.fragments[0] as LocalLibraryFragment
|
||||||
|
localLibraryFragment.handleSelectedFileUri(
|
||||||
|
uri,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun getSelectedFile(): File {
|
private fun getSelectedFile(): File {
|
||||||
val loadFileStream =
|
val loadFileStream =
|
||||||
CopyMoveFileHandlerTest::class.java.classLoader.getResourceAsStream("testzim.zim")
|
CopyMoveFileHandlerTest::class.java.classLoader.getResourceAsStream("testzim.zim")
|
||||||
@ -225,6 +238,16 @@ class CopyMoveFileHandlerTest : BaseActivityTest() {
|
|||||||
return zimFile
|
return zimFile
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun getInvalidZimFileUri(extension: String): Uri {
|
||||||
|
val zimFile = File(
|
||||||
|
ContextCompat.getExternalFilesDirs(context, null)[0],
|
||||||
|
"testzim$extension"
|
||||||
|
)
|
||||||
|
if (zimFile.exists()) zimFile.delete()
|
||||||
|
zimFile.createNewFile()
|
||||||
|
return Uri.fromFile(zimFile)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testGetDestinationFile() {
|
fun testGetDestinationFile() {
|
||||||
activityScenario.onActivity {
|
activityScenario.onActivity {
|
||||||
@ -272,6 +295,33 @@ class CopyMoveFileHandlerTest : BaseActivityTest() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testInvalidFileShouldNotOpenInReader() {
|
||||||
|
deleteAllFilesInDirectory(parentFile)
|
||||||
|
// Test the scenario in playStore build on Android 11 and above.
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
|
||||||
|
selectedFile = getSelectedFile()
|
||||||
|
activityScenario.onActivity {
|
||||||
|
kiwixMainActivity = it
|
||||||
|
kiwixMainActivity.navigate(R.id.libraryFragment)
|
||||||
|
}
|
||||||
|
copyMoveFileHandler(CopyMoveFileHandlerRobot::pauseForBetterTestPerformance)
|
||||||
|
sharedPreferenceUtil.apply {
|
||||||
|
copyMoveZimFilePermissionDialog = true
|
||||||
|
setIsPlayStoreBuildType(true)
|
||||||
|
}
|
||||||
|
// test opening images
|
||||||
|
tryOpeningInvalidZimFiles(getInvalidZimFileUri(".jpg"))
|
||||||
|
copyMoveFileHandler(CopyMoveFileHandlerRobot::assertCopyMoveDialogNotDisplayed)
|
||||||
|
// test opening videos
|
||||||
|
tryOpeningInvalidZimFiles(getInvalidZimFileUri(".mp4"))
|
||||||
|
copyMoveFileHandler(CopyMoveFileHandlerRobot::assertCopyMoveDialogNotDisplayed)
|
||||||
|
// test opening pdf
|
||||||
|
tryOpeningInvalidZimFiles(getInvalidZimFileUri(".pdf"))
|
||||||
|
copyMoveFileHandler(CopyMoveFileHandlerRobot::assertCopyMoveDialogNotDisplayed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private suspend fun deleteBothPreviousFiles() {
|
private suspend fun deleteBothPreviousFiles() {
|
||||||
selectedFile.deleteFile()
|
selectedFile.deleteFile()
|
||||||
destinationFile.deleteFile()
|
destinationFile.deleteFile()
|
||||||
|
@ -72,7 +72,7 @@ class CopyMoveFileHandler @Inject constructor(
|
|||||||
private var progressBar: ProgressBar? = null
|
private var progressBar: ProgressBar? = null
|
||||||
private var progressBarTextView: TextView? = null
|
private var progressBarTextView: TextView? = null
|
||||||
var isMoveOperation = false
|
var isMoveOperation = false
|
||||||
private var shouldValidateZimFile: Boolean = false
|
var shouldValidateZimFile: Boolean = false
|
||||||
private var fileSystemDisposable: Disposable? = null
|
private var fileSystemDisposable: Disposable? = null
|
||||||
|
|
||||||
private val copyMoveTitle: String by lazy {
|
private val copyMoveTitle: String by lazy {
|
||||||
@ -275,7 +275,6 @@ class CopyMoveFileHandler @Inject constructor(
|
|||||||
return try {
|
return try {
|
||||||
val contentResolver = activity.contentResolver
|
val contentResolver = activity.contentResolver
|
||||||
if (documentCanMove(selectedUri, contentResolver)) {
|
if (documentCanMove(selectedUri, contentResolver)) {
|
||||||
|
|
||||||
DocumentsContract.moveDocument(
|
DocumentsContract.moveDocument(
|
||||||
contentResolver,
|
contentResolver,
|
||||||
selectedUri,
|
selectedUri,
|
||||||
|
@ -28,7 +28,6 @@ import io.mockk.coVerify
|
|||||||
import io.mockk.every
|
import io.mockk.every
|
||||||
import io.mockk.just
|
import io.mockk.just
|
||||||
import io.mockk.mockk
|
import io.mockk.mockk
|
||||||
import io.mockk.mockkConstructor
|
|
||||||
import io.mockk.slot
|
import io.mockk.slot
|
||||||
import io.mockk.spyk
|
import io.mockk.spyk
|
||||||
import io.mockk.verify
|
import io.mockk.verify
|
||||||
@ -40,6 +39,7 @@ import org.junit.jupiter.api.Assertions.assertFalse
|
|||||||
import org.junit.jupiter.api.Assertions.assertTrue
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
import org.junit.jupiter.api.BeforeEach
|
import org.junit.jupiter.api.BeforeEach
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.kiwix.kiwixmobile.core.R
|
||||||
import org.kiwix.kiwixmobile.core.settings.StorageCalculator
|
import org.kiwix.kiwixmobile.core.settings.StorageCalculator
|
||||||
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
|
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
|
||||||
import org.kiwix.kiwixmobile.core.utils.dialog.AlertDialogShower
|
import org.kiwix.kiwixmobile.core.utils.dialog.AlertDialogShower
|
||||||
@ -51,9 +51,6 @@ import org.kiwix.kiwixmobile.zimManager.Fat32Checker.FileSystemState.CanWrite4Gb
|
|||||||
import org.kiwix.kiwixmobile.zimManager.Fat32Checker.FileSystemState.CannotWrite4GbFile
|
import org.kiwix.kiwixmobile.zimManager.Fat32Checker.FileSystemState.CannotWrite4GbFile
|
||||||
import org.kiwix.kiwixmobile.zimManager.Fat32Checker.FileSystemState.DetectingFileSystem
|
import org.kiwix.kiwixmobile.zimManager.Fat32Checker.FileSystemState.DetectingFileSystem
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import org.kiwix.kiwixmobile.core.R
|
|
||||||
import org.kiwix.kiwixmobile.core.reader.ZimReaderSource
|
|
||||||
import org.kiwix.libzim.Archive
|
|
||||||
|
|
||||||
class CopyMoveFileHandlerTest {
|
class CopyMoveFileHandlerTest {
|
||||||
private lateinit var fileHandler: CopyMoveFileHandler
|
private lateinit var fileHandler: CopyMoveFileHandler
|
||||||
@ -332,11 +329,9 @@ class CopyMoveFileHandlerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `notifyFileOperationSuccess should handle invalid ZIM file`() = runTest {
|
fun `notifyFileOperationSuccess should handle invalid ZIM file`() = runTest {
|
||||||
mockkConstructor(Archive::class)
|
fileHandler = spyk(fileHandler)
|
||||||
val archiveMock = mockk<Archive>(relaxed = true)
|
fileHandler.shouldValidateZimFile = true
|
||||||
every { constructedWith<Archive>(any()) } returns archiveMock
|
|
||||||
coEvery { fileHandler.isValidZimFile(destinationFile) } returns false
|
coEvery { fileHandler.isValidZimFile(destinationFile) } returns false
|
||||||
|
|
||||||
fileHandler.notifyFileOperationSuccess(destinationFile, sourceUri)
|
fileHandler.notifyFileOperationSuccess(destinationFile, sourceUri)
|
||||||
|
|
||||||
verify { fileHandler.handleInvalidZimFile(destinationFile, sourceUri) }
|
verify { fileHandler.handleInvalidZimFile(destinationFile, sourceUri) }
|
||||||
@ -344,7 +339,9 @@ class CopyMoveFileHandlerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `handleInvalidZimFile should call onError if move is successful`() {
|
fun `handleInvalidZimFile should call onError if move is successful`() {
|
||||||
|
fileHandler = spyk(fileHandler)
|
||||||
every { fileHandler.tryMoveWithDocumentContract(any(), any(), any()) } returns true
|
every { fileHandler.tryMoveWithDocumentContract(any(), any(), any()) } returns true
|
||||||
|
every { destinationFile.parentFile } returns mockk()
|
||||||
fileHandler.isMoveOperation = true
|
fileHandler.isMoveOperation = true
|
||||||
|
|
||||||
fileHandler.handleInvalidZimFile(destinationFile, sourceUri)
|
fileHandler.handleInvalidZimFile(destinationFile, sourceUri)
|
||||||
@ -355,7 +352,9 @@ class CopyMoveFileHandlerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `handleInvalidZimFile should delete file and show error if move fails`() {
|
fun `handleInvalidZimFile should delete file and show error if move fails`() {
|
||||||
|
fileHandler = spyk(fileHandler)
|
||||||
every { fileHandler.tryMoveWithDocumentContract(any(), any(), any()) } returns false
|
every { fileHandler.tryMoveWithDocumentContract(any(), any(), any()) } returns false
|
||||||
|
every { destinationFile.parentFile } returns mockk()
|
||||||
fileHandler.isMoveOperation = true
|
fileHandler.isMoveOperation = true
|
||||||
|
|
||||||
fileHandler.handleInvalidZimFile(destinationFile, sourceUri)
|
fileHandler.handleInvalidZimFile(destinationFile, sourceUri)
|
||||||
@ -368,26 +367,6 @@ class CopyMoveFileHandlerTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `isValidZimFile should return true if ZIM file has main entry`() = runTest {
|
|
||||||
val archive: Archive = mockk()
|
|
||||||
every { archive.hasMainEntry() } returns true
|
|
||||||
coEvery { ZimReaderSource(destinationFile).createArchive() } returns archive
|
|
||||||
|
|
||||||
val result = fileHandler.isValidZimFile(destinationFile)
|
|
||||||
|
|
||||||
assertTrue(result)
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `isValidZimFile should return false if ZIM file creation fails`() = runTest {
|
|
||||||
coEvery { ZimReaderSource(destinationFile).createArchive() } throws Exception()
|
|
||||||
|
|
||||||
val result = fileHandler.isValidZimFile(destinationFile)
|
|
||||||
|
|
||||||
assertFalse(result)
|
|
||||||
}
|
|
||||||
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
fun dispose() {
|
fun dispose() {
|
||||||
fileHandler.dispose()
|
fileHandler.dispose()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user