Skip to content

Commit

Permalink
Added test case for testing splitted zim file with zero size file.
Browse files Browse the repository at this point in the history
  • Loading branch information
MohitMaliDeveloper committed May 6, 2024
1 parent 7961219 commit 5069816
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,23 @@ import androidx.test.core.app.ActivityScenario
import androidx.test.internal.runner.junit4.statement.UiThreadStatement
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import org.junit.Assert
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.jupiter.api.fail
import org.kiwix.kiwixmobile.BaseActivityTest
import org.kiwix.kiwixmobile.R
import org.kiwix.kiwixmobile.core.NightModeConfig
import org.kiwix.kiwixmobile.core.reader.ZimFileReader
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.main.KiwixMainActivity
import org.kiwix.kiwixmobile.nav.destination.library.LocalLibraryFragmentDirections
import org.kiwix.kiwixmobile.page.history.navigationHistory
import org.kiwix.kiwixmobile.testutils.RetryRule
import org.kiwix.kiwixmobile.testutils.TestUtils
import org.kiwix.libzim.Archive
import org.kiwix.libzim.SuggestionSearcher
import java.io.File
import java.io.FileOutputStream
import java.io.OutputStream
Expand Down Expand Up @@ -94,7 +99,28 @@ class ZimFileReaderWithSplittedZimFileTest : BaseActivityTest() {
}
}

private fun createAndGetSplitedZimFile(): File? {
@Test
fun testWithExtraZeroSizeFile() {
createAndGetSplitedZimFile(true)?.let { zimFile ->
// test the articleCount and mediaCount of this zim file.
val archive = Archive(zimFile.canonicalPath)
val zimFileReader = ZimFileReader(
zimFile,
null,
null,
archive,
NightModeConfig(SharedPreferenceUtil(context), context),
SuggestionSearcher(archive)
)
Assert.assertEquals(zimFileReader.mediaCount, 16)
Assert.assertEquals(zimFileReader.articleCount, 4)
} ?: kotlin.run {
// error in creating the zim file chunk
fail("Couldn't create the zim file chunk")
}
}

private fun createAndGetSplitedZimFile(shouldCreateExtraZeroSizeFile: Boolean = false): File? {
val loadFileStream =
EncodedUrlTest::class.java.classLoader.getResourceAsStream("testzim.zim")
val storageDir = ContextCompat.getExternalFilesDirs(context, null)[0]
Expand Down Expand Up @@ -135,6 +161,12 @@ class ZimFileReaderWithSplittedZimFileTest : BaseActivityTest() {
}
outputStream?.close()
}
if (shouldCreateExtraZeroSizeFile) {
File(storageDir, "testzim.zimad").apply {
if (exists()) delete() // delete if already exist.
createNewFile() // create new zero size file.
}
}
val splittedZimFile = File(storageDir, "testzim.zimaa")
return if (splittedZimFile.exists()) splittedZimFile else null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class ZimFileReader constructor(

val tags: String
get() = getSafeMetaData("Tags", "")
private val mediaCount: Int?
val mediaCount: Int?
get() = try {
jniKiwixReader.mediaCount
}
Expand All @@ -166,7 +166,7 @@ class ZimFileReader constructor(
Log.e(TAG, "Unable to find the media count $ignore")

Check warning on line 166 in core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimFileReader.kt

View check run for this annotation

Codecov / codecov/patch

core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimFileReader.kt#L165-L166

Added lines #L165 - L166 were not covered by tests
null
}
private val articleCount: Int?
val articleCount: Int?
get() = try {
jniKiwixReader.articleCount
}
Expand Down

0 comments on commit 5069816

Please sign in to comment.