mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-08-03 18:56:44 -04:00
Added the test cases for CustomFileValidator
with edge cases.
This commit is contained in:
parent
e25f0f0aad
commit
c3d9bb74f7
@ -25,7 +25,6 @@ import android.content.res.AssetManager
|
|||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
import io.mockk.every
|
import io.mockk.every
|
||||||
import io.mockk.mockk
|
import io.mockk.mockk
|
||||||
import io.mockk.mockkStatic
|
|
||||||
import org.junit.jupiter.api.Assertions.assertEquals
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
import org.junit.jupiter.api.Assertions.assertTrue
|
import org.junit.jupiter.api.Assertions.assertTrue
|
||||||
import org.junit.jupiter.api.Assertions.fail
|
import org.junit.jupiter.api.Assertions.fail
|
||||||
@ -52,8 +51,8 @@ class CustomFileValidatorTest {
|
|||||||
fun `validate should call onFilesFound when both OBB and ZIM files are found`() {
|
fun `validate should call onFilesFound when both OBB and ZIM files are found`() {
|
||||||
val obbFile = mockk<File>()
|
val obbFile = mockk<File>()
|
||||||
val zimFile = mockk<File>()
|
val zimFile = mockk<File>()
|
||||||
mockZimFiles(arrayOf(obbFile), obbFile, "obb")
|
mockZimFiles(arrayOf(obbFile), "obb")
|
||||||
mockZimFiles(arrayOf(zimFile), zimFile, "zim")
|
mockZimFiles(arrayOf(zimFile), "zim")
|
||||||
|
|
||||||
customFileValidator.validate(
|
customFileValidator.validate(
|
||||||
onFilesFound = {
|
onFilesFound = {
|
||||||
@ -68,8 +67,8 @@ class CustomFileValidatorTest {
|
|||||||
@Test
|
@Test
|
||||||
fun `validate should call onFilesFound when only OBB file is found`() {
|
fun `validate should call onFilesFound when only OBB file is found`() {
|
||||||
val obbFile = mockk<File>()
|
val obbFile = mockk<File>()
|
||||||
mockZimFiles(arrayOf(obbFile), obbFile, "obb")
|
mockZimFiles(arrayOf(obbFile), "obb")
|
||||||
mockZimFiles(emptyArray(), obbFile, "zim")
|
mockZimFiles(arrayOf(), "zim")
|
||||||
|
|
||||||
customFileValidator.validate(
|
customFileValidator.validate(
|
||||||
onFilesFound = {
|
onFilesFound = {
|
||||||
@ -83,8 +82,8 @@ class CustomFileValidatorTest {
|
|||||||
@Test
|
@Test
|
||||||
fun `validate should call onFilesFound when only ZIM file is found`() {
|
fun `validate should call onFilesFound when only ZIM file is found`() {
|
||||||
val zimFile = mockk<File>()
|
val zimFile = mockk<File>()
|
||||||
mockZimFiles(emptyArray(), zimFile, "obb")
|
mockZimFiles(arrayOf(), "obb")
|
||||||
mockZimFiles(arrayOf(zimFile), zimFile, "zim")
|
mockZimFiles(arrayOf(zimFile), "zim")
|
||||||
|
|
||||||
customFileValidator.validate(
|
customFileValidator.validate(
|
||||||
onFilesFound = {
|
onFilesFound = {
|
||||||
@ -97,12 +96,47 @@ class CustomFileValidatorTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `validate should call onNoFilesFound when no OBB or ZIM files are found`() {
|
fun `validate should call onNoFilesFound when no OBB or ZIM files are found`() {
|
||||||
mockZimFiles(emptyArray(), mockk(), extension = "zim")
|
mockZimFiles(arrayOf(), extension = "zim")
|
||||||
mockZimFiles(emptyArray(), mockk(), extension = "obb")
|
mockZimFiles(arrayOf(), extension = "obb")
|
||||||
|
|
||||||
customFileValidator.validate(
|
customFileValidator.validate(
|
||||||
onFilesFound = { fail("Should not call onFilesFound") },
|
onFilesFound = { fail("Should not call onFilesFound") },
|
||||||
onNoFilesFound = { /* Success */ }
|
onNoFilesFound = { }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `validate should call onNoFilesFound when directories are null`() {
|
||||||
|
mockZimFiles(null, "zim")
|
||||||
|
mockZimFiles(null, "obb")
|
||||||
|
|
||||||
|
customFileValidator.validate(
|
||||||
|
onFilesFound = { fail("Should not call onFilesFound") },
|
||||||
|
onNoFilesFound = { }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `validate should call onNoFilesFound when no matching files are found`() {
|
||||||
|
val textFile = mockk<File>()
|
||||||
|
mockZimFiles(arrayOf(textFile), "txt")
|
||||||
|
|
||||||
|
customFileValidator.validate(
|
||||||
|
onFilesFound = { fail("Should not call onFilesFound") },
|
||||||
|
onNoFilesFound = { }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `validate should call onFilesFound for case insensitive file extensions`() {
|
||||||
|
val zimFile = mockk<File>()
|
||||||
|
mockZimFiles(arrayOf(zimFile), "ZIM")
|
||||||
|
|
||||||
|
customFileValidator.validate(
|
||||||
|
onFilesFound = {
|
||||||
|
fail("Should not call onFilesFound")
|
||||||
|
},
|
||||||
|
onNoFilesFound = {}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,35 +168,31 @@ class CustomFileValidatorTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun mockZimFiles(
|
private fun mockZimFiles(
|
||||||
storageDirectory: Array<File?>,
|
zimFilesArray: Array<File?>?,
|
||||||
zimFile: File,
|
|
||||||
extension: String
|
extension: String
|
||||||
) {
|
) {
|
||||||
every { zimFile.exists() } returns true
|
zimFilesArray?.forEach {
|
||||||
every { zimFile.isFile } returns true
|
it?.let {
|
||||||
every { zimFile.extension } returns extension
|
every { it.exists() } returns true
|
||||||
mockkStatic(File::walk)
|
every { it.isFile } returns true
|
||||||
storageDirectory.forEach { dir ->
|
every { it.extension } returns extension
|
||||||
dir?.let {
|
every { it.isDirectory } returns false
|
||||||
every { dir.exists() } returns true
|
every { it.name } returns "sample.$extension"
|
||||||
every { dir.isDirectory } returns true
|
|
||||||
every { dir.extension } returns ""
|
|
||||||
every { dir.parent } returns null
|
|
||||||
every { dir.listFiles() } returns arrayOf(zimFile)
|
|
||||||
every { dir.walk() } answers { mockFileWalk(listOf(zimFile)) }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
val storageDirectory = mockk<File>()
|
||||||
|
every { storageDirectory.exists() } returns true
|
||||||
|
every { storageDirectory.isDirectory } returns true
|
||||||
|
every { storageDirectory.extension } returns ""
|
||||||
|
every { storageDirectory.parent } returns null
|
||||||
|
every { storageDirectory.listFiles() } returns zimFilesArray
|
||||||
|
|
||||||
if (extension == "zim") {
|
if (extension == "zim") {
|
||||||
every { ContextCompat.getExternalFilesDirs(context, null) } returns storageDirectory
|
every {
|
||||||
|
ContextCompat.getExternalFilesDirs(context, null)
|
||||||
|
} returns arrayOf(storageDirectory)
|
||||||
} else {
|
} else {
|
||||||
every { ContextCompat.getObbDirs(context) } returns storageDirectory
|
every { ContextCompat.getObbDirs(context) } returns arrayOf(storageDirectory)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun mockFileWalk(files: List<File>): FileTreeWalk {
|
|
||||||
val fileTreeWalk = mockk<FileTreeWalk>()
|
|
||||||
every { fileTreeWalk.iterator() } returns files.iterator()
|
|
||||||
return fileTreeWalk
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user