From 5069816fa7cd1e113b1cf4e50a21f384e77298d0 Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Mon, 6 May 2024 23:28:15 +0530 Subject: [PATCH] Added test case for testing splitted zim file with zero size file. --- .../ZimFileReaderWithSplittedZimFileTest.kt | 34 ++++++++++++++++++- .../kiwixmobile/core/reader/ZimFileReader.kt | 4 +-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/app/src/androidTest/java/org/kiwix/kiwixmobile/reader/ZimFileReaderWithSplittedZimFileTest.kt b/app/src/androidTest/java/org/kiwix/kiwixmobile/reader/ZimFileReaderWithSplittedZimFileTest.kt index e3f5c40986..e7dab4544a 100644 --- a/app/src/androidTest/java/org/kiwix/kiwixmobile/reader/ZimFileReaderWithSplittedZimFileTest.kt +++ b/app/src/androidTest/java/org/kiwix/kiwixmobile/reader/ZimFileReaderWithSplittedZimFileTest.kt @@ -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 @@ -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] @@ -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 } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimFileReader.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimFileReader.kt index 429a266aa5..baee22b77b 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimFileReader.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/reader/ZimFileReader.kt @@ -157,7 +157,7 @@ class ZimFileReader constructor( val tags: String get() = getSafeMetaData("Tags", "") - private val mediaCount: Int? + val mediaCount: Int? get() = try { jniKiwixReader.mediaCount } @@ -166,7 +166,7 @@ class ZimFileReader constructor( Log.e(TAG, "Unable to find the media count $ignore") null } - private val articleCount: Int? + val articleCount: Int? get() = try { jniKiwixReader.articleCount }