diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/localLibrary/CopyMoveFileHandlerRobot.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/localLibrary/CopyMoveFileHandlerRobot.kt index 46d238f50..7d9f176f9 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/localLibrary/CopyMoveFileHandlerRobot.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/localLibrary/CopyMoveFileHandlerRobot.kt @@ -21,6 +21,7 @@ package org.kiwix.kiwixmobile.localLibrary import androidx.test.espresso.Espresso.onView import androidx.test.espresso.action.ViewActions.click 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.withText import androidx.test.espresso.web.sugar.Web @@ -50,6 +51,12 @@ class CopyMoveFileHandlerRobot : BaseRobot() { 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() { testFlakyView({ onView(withText(R.string.action_copy)).perform(click()) diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/localLibrary/CopyMoveFileHandlerTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/localLibrary/CopyMoveFileHandlerTest.kt index a388daa1c..f67741105 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/localLibrary/CopyMoveFileHandlerTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/localLibrary/CopyMoveFileHandlerTest.kt @@ -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 { val loadFileStream = CopyMoveFileHandlerTest::class.java.classLoader.getResourceAsStream("testzim.zim") @@ -225,6 +238,16 @@ class CopyMoveFileHandlerTest : BaseActivityTest() { 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 fun testGetDestinationFile() { 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() { selectedFile.deleteFile() destinationFile.deleteFile() diff --git a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/CopyMoveFileHandler.kt b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/CopyMoveFileHandler.kt index 37aae9b35..2ee444871 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/CopyMoveFileHandler.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/nav/destination/library/CopyMoveFileHandler.kt @@ -72,7 +72,7 @@ class CopyMoveFileHandler @Inject constructor( private var progressBar: ProgressBar? = null private var progressBarTextView: TextView? = null var isMoveOperation = false - private var shouldValidateZimFile: Boolean = false + var shouldValidateZimFile: Boolean = false private var fileSystemDisposable: Disposable? = null private val copyMoveTitle: String by lazy { @@ -275,7 +275,6 @@ class CopyMoveFileHandler @Inject constructor( return try { val contentResolver = activity.contentResolver if (documentCanMove(selectedUri, contentResolver)) { - DocumentsContract.moveDocument( contentResolver, selectedUri, diff --git a/app/src/test/java/org/kiwix/kiwixmobile/localLibrary/CopyMoveFileHandlerTest.kt b/app/src/test/java/org/kiwix/kiwixmobile/localLibrary/CopyMoveFileHandlerTest.kt index b4f10c879..124436ae1 100644 --- a/app/src/test/java/org/kiwix/kiwixmobile/localLibrary/CopyMoveFileHandlerTest.kt +++ b/app/src/test/java/org/kiwix/kiwixmobile/localLibrary/CopyMoveFileHandlerTest.kt @@ -28,7 +28,6 @@ import io.mockk.coVerify import io.mockk.every import io.mockk.just import io.mockk.mockk -import io.mockk.mockkConstructor import io.mockk.slot import io.mockk.spyk 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.BeforeEach import org.junit.jupiter.api.Test +import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.settings.StorageCalculator import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil 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.DetectingFileSystem import java.io.File -import org.kiwix.kiwixmobile.core.R -import org.kiwix.kiwixmobile.core.reader.ZimReaderSource -import org.kiwix.libzim.Archive class CopyMoveFileHandlerTest { private lateinit var fileHandler: CopyMoveFileHandler @@ -332,11 +329,9 @@ class CopyMoveFileHandlerTest { @Test fun `notifyFileOperationSuccess should handle invalid ZIM file`() = runTest { - mockkConstructor(Archive::class) - val archiveMock = mockk(relaxed = true) - every { constructedWith(any()) } returns archiveMock + fileHandler = spyk(fileHandler) + fileHandler.shouldValidateZimFile = true coEvery { fileHandler.isValidZimFile(destinationFile) } returns false - fileHandler.notifyFileOperationSuccess(destinationFile, sourceUri) verify { fileHandler.handleInvalidZimFile(destinationFile, sourceUri) } @@ -344,7 +339,9 @@ class CopyMoveFileHandlerTest { @Test fun `handleInvalidZimFile should call onError if move is successful`() { + fileHandler = spyk(fileHandler) every { fileHandler.tryMoveWithDocumentContract(any(), any(), any()) } returns true + every { destinationFile.parentFile } returns mockk() fileHandler.isMoveOperation = true fileHandler.handleInvalidZimFile(destinationFile, sourceUri) @@ -355,7 +352,9 @@ class CopyMoveFileHandlerTest { @Test fun `handleInvalidZimFile should delete file and show error if move fails`() { + fileHandler = spyk(fileHandler) every { fileHandler.tryMoveWithDocumentContract(any(), any(), any()) } returns false + every { destinationFile.parentFile } returns mockk() fileHandler.isMoveOperation = true 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 fun dispose() { fileHandler.dispose()