Refactored and added new unit test cases.

This commit is contained in:
MohitMaliFtechiz 2024-11-14 17:09:59 +05:30
parent 7f94f48a06
commit 0b1b716c43
2 changed files with 41 additions and 42 deletions

View File

@ -82,7 +82,7 @@ class CopyMoveFileHandler @Inject constructor(
var shouldValidateZimFile: Boolean = false var shouldValidateZimFile: Boolean = false
private var fileSystemDisposable: Disposable? = null private var fileSystemDisposable: Disposable? = null
private lateinit var fragmentManager: FragmentManager private lateinit var fragmentManager: FragmentManager
private val storageDeviceList by lazy { val storageDeviceList by lazy {
StorageDeviceUtils.getWritableStorage(activity) StorageDeviceUtils.getWritableStorage(activity)
} }
@ -141,7 +141,7 @@ class CopyMoveFileHandler @Inject constructor(
lifecycleScope = coroutineScope lifecycleScope = coroutineScope
} }
private fun showStorageSelectDialog() = StorageSelectDialog() fun showStorageSelectDialog() = StorageSelectDialog()
.apply { .apply {
onSelectAction = ::copyMoveZIMFileInSelectedStorage onSelectAction = ::copyMoveZIMFileInSelectedStorage
titleSize = STORAGE_SELECT_STORAGE_TITLE_TEXTVIEW_SIZE titleSize = STORAGE_SELECT_STORAGE_TITLE_TEXTVIEW_SIZE
@ -167,7 +167,7 @@ class CopyMoveFileHandler @Inject constructor(
} }
} }
fun performCopyMoveOperation() { private fun performCopyMoveOperation() {
if (isMoveOperation) { if (isMoveOperation) {
performMoveOperation() performMoveOperation()
} else { } else {
@ -233,7 +233,7 @@ class CopyMoveFileHandler @Inject constructor(
} }
} }
private fun performCopyMoveOperationIfSufficientSpaceAvailable() { fun performCopyMoveOperationIfSufficientSpaceAvailable() {
val availableSpace = storageCalculator.availableBytes(File(sharedPreferenceUtil.prefStorage)) val availableSpace = storageCalculator.availableBytes(File(sharedPreferenceUtil.prefStorage))
if (hasNotSufficientStorageSpace(availableSpace)) { if (hasNotSufficientStorageSpace(availableSpace)) {
fileCopyMoveCallback?.insufficientSpaceInStorage(availableSpace) fileCopyMoveCallback?.insufficientSpaceInStorage(availableSpace)

View File

@ -126,7 +126,7 @@ class CopyMoveFileHandlerTest {
@Test @Test
fun validateZimFileCanCopyOrMoveShouldReturnFalseWhenDetectingFileSystem() { fun validateZimFileCanCopyOrMoveShouldReturnFalseWhenDetectingFileSystem() {
every { fileHandler.isBookLessThan4GB() } returns true every { fileHandler.isBookLessThan4GB() } returns true
every { fileHandler.performCopyMoveOperation() } just Runs every { fileHandler.performCopyMoveOperationIfSufficientSpaceAvailable() } just Runs
prepareFileSystemAndFileForMockk(fileSystemState = DetectingFileSystem) prepareFileSystemAndFileForMockk(fileSystemState = DetectingFileSystem)
val result = fileHandler.validateZimFileCanCopyOrMove(storageFile) val result = fileHandler.validateZimFileCanCopyOrMove(storageFile)
@ -151,15 +151,15 @@ class CopyMoveFileHandlerTest {
} }
@Test @Test
fun handleDetectingFileSystemStateShouldShowCopyMoveDialogIfBookLessThan4GB() { fun handleDetectingFileSystemStateShouldPerformCopyMoveOperationIfBookLessThan4GB() {
fileHandler = spyk(fileHandler) fileHandler = spyk(fileHandler)
prepareFileSystemAndFileForMockk() prepareFileSystemAndFileForMockk()
every { fileHandler.isBookLessThan4GB() } returns true every { fileHandler.isBookLessThan4GB() } returns true
every { fileHandler.showCopyMoveDialog() } just Runs every { fileHandler.performCopyMoveOperationIfSufficientSpaceAvailable() } just Runs
fileHandler.handleDetectingFileSystemState() fileHandler.handleDetectingFileSystemState()
verify { fileHandler.showCopyMoveDialog() } verify { fileHandler.performCopyMoveOperationIfSufficientSpaceAvailable() }
} }
@Test @Test
@ -174,15 +174,15 @@ class CopyMoveFileHandlerTest {
} }
@Test @Test
fun handleCannotWrite4GbFileStateShouldShowCopyMoveDialogIfBookLessThan4GB() { fun handleCannotWrite4GbFileStateShouldPerformCopyMoveOperationIfBookLessThan4GB() {
fileHandler = spyk(fileHandler) fileHandler = spyk(fileHandler)
prepareFileSystemAndFileForMockk() prepareFileSystemAndFileForMockk()
every { fileHandler.isBookLessThan4GB() } returns true every { fileHandler.isBookLessThan4GB() } returns true
every { fileHandler.showCopyMoveDialog() } just Runs every { fileHandler.performCopyMoveOperationIfSufficientSpaceAvailable() } just Runs
fileHandler.handleCannotWrite4GbFileState() fileHandler.handleCannotWrite4GbFileState()
verify { fileHandler.showCopyMoveDialog() } verify { fileHandler.performCopyMoveOperationIfSufficientSpaceAvailable() }
} }
@Test @Test
@ -202,52 +202,50 @@ class CopyMoveFileHandlerTest {
} }
@Test @Test
fun showMoveToPublicDirectoryPermissionDialogShouldShowPermissionDialogAtFirstLaunch() { fun showStorageConfigureDialogAtFirstLaunch() {
every { sharedPreferenceUtil.shouldShowStorageSelectionDialog } returns true
every { alertDialogShower.show(any(), any(), any()) } just Runs
fileHandler.showMoveFileToPublicDirectoryDialog(fragmentManager = fragmentManager)
verify {
alertDialogShower.show(
KiwixDialog.CopyMoveFileToPublicDirectoryDialog,
any(),
any()
)
}
}
@Test
fun copyMoveFunctionsShouldCallWhenClickingOnButtonsInPermissionDialog() {
val positiveButtonClickSlot = slot<() -> Unit>()
val negativeButtonClickSlot = slot<() -> Unit>()
fileHandler = spyk(fileHandler) fileHandler = spyk(fileHandler)
every { fileHandler.showStorageSelectDialog() } just Runs
every { sharedPreferenceUtil.shouldShowStorageSelectionDialog } returns true every { sharedPreferenceUtil.shouldShowStorageSelectionDialog } returns true
every { fileHandler.storageDeviceList } returns listOf(mockk(), mockk())
val positiveButtonClickSlot = slot<() -> Unit>()
every { every {
alertDialogShower.show( alertDialogShower.show(
KiwixDialog.CopyMoveFileToPublicDirectoryDialog, KiwixDialog.CopyMoveFileToPublicDirectoryDialog,
capture(positiveButtonClickSlot), capture(positiveButtonClickSlot),
capture(negativeButtonClickSlot) any()
) )
} just Runs } just Runs
fileHandler.showMoveFileToPublicDirectoryDialog(fragmentManager = fragmentManager) fileHandler.showMoveFileToPublicDirectoryDialog(fragmentManager = fragmentManager)
every { fileHandler.validateZimFileCanCopyOrMove() } returns true every { fileHandler.validateZimFileCanCopyOrMove() } returns true
every { fileHandler.performCopyOperation(false) } just Runs
positiveButtonClickSlot.captured.invoke() positiveButtonClickSlot.captured.invoke()
verify { fileHandler.performCopyOperation(false) } verify { fileHandler.showStorageSelectDialog() }
every { sharedPreferenceUtil.shouldShowStorageSelectionDialog } returns false
every { fileHandler.performMoveOperation(false) } just Runs
negativeButtonClickSlot.captured.invoke()
verify { fileHandler.performMoveOperation(false) }
verify { sharedPreferenceUtil.shouldShowStorageSelectionDialog = true }
} }
@Test @Test
fun showCopyMoveDialog() { fun shouldNotShowStorageConfigureDialogWhenThereIsOnlyInternalAvailable() {
fileHandler = spyk(fileHandler)
every { sharedPreferenceUtil.shouldShowStorageSelectionDialog } returns true
every { fileHandler.storageDeviceList } returns listOf(mockk())
val positiveButtonClickSlot = slot<() -> Unit>()
every {
alertDialogShower.show(
KiwixDialog.CopyMoveFileToPublicDirectoryDialog,
capture(positiveButtonClickSlot),
any()
)
} just Runs
every { fileHandler.validateZimFileCanCopyOrMove() } returns true
fileHandler.showMoveFileToPublicDirectoryDialog(fragmentManager = fragmentManager)
positiveButtonClickSlot.captured.invoke()
verify(exactly = 0) { fileHandler.showStorageSelectDialog() }
}
@Test
fun showDirectlyCopyMoveDialogAfterFirstLaunch() {
fileHandler = spyk(fileHandler)
every { sharedPreferenceUtil.shouldShowStorageSelectionDialog } returns false every { sharedPreferenceUtil.shouldShowStorageSelectionDialog } returns false
every { fileHandler.storageDeviceList } returns listOf(mockk(), mockk())
every { fileHandler.validateZimFileCanCopyOrMove() } returns true
prepareFileSystemAndFileForMockk() prepareFileSystemAndFileForMockk()
every { alertDialogShower.show(any(), any(), any()) } just Runs every { alertDialogShower.show(any(), any(), any()) } just Runs
fileHandler.showMoveFileToPublicDirectoryDialog(fragmentManager = fragmentManager) fileHandler.showMoveFileToPublicDirectoryDialog(fragmentManager = fragmentManager)
@ -266,6 +264,7 @@ class CopyMoveFileHandlerTest {
val positiveButtonClickSlot = slot<() -> Unit>() val positiveButtonClickSlot = slot<() -> Unit>()
val negativeButtonClickSlot = slot<() -> Unit>() val negativeButtonClickSlot = slot<() -> Unit>()
fileHandler = spyk(fileHandler) fileHandler = spyk(fileHandler)
every { fileHandler.storageDeviceList } returns listOf(mockk(), mockk())
every { sharedPreferenceUtil.shouldShowStorageSelectionDialog } returns false every { sharedPreferenceUtil.shouldShowStorageSelectionDialog } returns false
every { every {
alertDialogShower.show( alertDialogShower.show(