Fixed MimeTypeTest, EncodedUrlTest, and ZimFileReaderWithSplittedZimFileTest which sometimes failing on CI

This commit is contained in:
MohitMaliFtechiz 2024-11-08 16:42:31 +05:30
parent f9bf7b2d00
commit f3d115cde1
3 changed files with 110 additions and 125 deletions

View File

@ -21,12 +21,11 @@ package org.kiwix.kiwixmobile.mimetype
import androidx.core.content.ContextCompat
import androidx.core.content.edit
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
import androidx.test.core.app.ActivityScenario
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Assert
import org.junit.Before
@ -70,7 +69,7 @@ class MimeTypeTest : BaseActivityTest() {
}
@Test
fun testMimeType() {
fun testMimeType() = runBlocking {
val loadFileStream = MimeTypeTest::class.java.classLoader.getResourceAsStream("testzim.zim")
val zimFile = File(
ContextCompat.getExternalFilesDirs(context, null)[0],
@ -89,41 +88,37 @@ class MimeTypeTest : BaseActivityTest() {
}
}
val zimSource = ZimReaderSource(zimFile)
activityScenario.onActivity {
it.lifecycleScope.launch {
val archive = zimSource.createArchive()
val zimFileReader = ZimFileReader(
zimSource,
archive!!,
DarkModeConfig(SharedPreferenceUtil(context), context),
SuggestionSearcher(archive)
val archive = zimSource.createArchive()
val zimFileReader = ZimFileReader(
zimSource,
archive!!,
DarkModeConfig(SharedPreferenceUtil(context), context),
SuggestionSearcher(archive)
)
zimFileReader.getRandomArticleUrl()?.let { randomArticle ->
val mimeType = zimFileReader.getMimeTypeFromUrl(randomArticle)
if (mimeType?.contains("^([^ ]+).*$") == true || mimeType?.contains(";") == true) {
Assert.fail(
"Unable to get mime type from zim file. File = " +
" $zimFile and url of article = $randomArticle"
)
zimFileReader.getRandomArticleUrl()?.let { randomArticle ->
val mimeType = zimFileReader.getMimeTypeFromUrl(randomArticle)
if (mimeType?.contains("^([^ ]+).*$") == true || mimeType?.contains(";") == true) {
Assert.fail(
"Unable to get mime type from zim file. File = " +
" $zimFile and url of article = $randomArticle"
)
}
} ?: kotlin.run {
Assert.fail("Unable to get article from zim file $zimFile")
}
// test mimetypes for some actual url
Assert.assertEquals(
"text/html",
zimFileReader.getMimeTypeFromUrl("https://kiwix.app/A/index.html")
)
Assert.assertEquals(
"text/css",
zimFileReader.getMimeTypeFromUrl("https://kiwix.app/-/assets/style1.css")
)
// test mimetype for invalid url
Assert.assertEquals(null, zimFileReader.getMimeTypeFromUrl("https://kiwix.app/A/test.html"))
// dispose the ZimFileReader
zimFileReader.dispose()
}
} ?: kotlin.run {
Assert.fail("Unable to get article from zim file $zimFile")
}
// test mimetypes for some actual url
Assert.assertEquals(
"text/html",
zimFileReader.getMimeTypeFromUrl("https://kiwix.app/A/index.html")
)
Assert.assertEquals(
"text/css",
zimFileReader.getMimeTypeFromUrl("https://kiwix.app/-/assets/style1.css")
)
// test mimetype for invalid url
Assert.assertEquals(null, zimFileReader.getMimeTypeFromUrl("https://kiwix.app/A/test.html"))
// dispose the ZimFileReader
zimFileReader.dispose()
}
@After

View File

@ -21,12 +21,11 @@ package org.kiwix.kiwixmobile.reader
import androidx.core.content.ContextCompat
import androidx.core.content.edit
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
import androidx.test.core.app.ActivityScenario
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.UiDevice
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Assert
import org.junit.Before
@ -76,7 +75,7 @@ class EncodedUrlTest : BaseActivityTest() {
}
@Test
fun testEncodedUrls() {
fun testEncodedUrls() = runBlocking {
val loadFileStream =
EncodedUrlTest::class.java.classLoader.getResourceAsStream("characters_encoding.zim")
val zimFile = File(
@ -96,78 +95,74 @@ class EncodedUrlTest : BaseActivityTest() {
}
}
val zimReaderSource = ZimReaderSource(zimFile)
activityScenario.onActivity {
it.lifecycleScope.launch {
val archive = zimReaderSource.createArchive()
val zimFileReader = ZimFileReader(
zimReaderSource,
archive!!,
DarkModeConfig(SharedPreferenceUtil(context), context),
SuggestionSearcher(archive)
)
val encodedUrls = arrayOf(
EncodedUrl(
"https://kiwix.app/foo/part%2520with%2520space/bar%253Fkey%253Dvalue",
"https://kiwix.app/foo/part%2520with%2520space/bar%253Fkey%253Dvalue"
),
EncodedUrl(
"https://kiwix.app/foo/part%20with%20space/bar%3Fkey%3Dvalue",
"https://kiwix.app/foo/part%20with%20space/bar%3Fkey%3Dvalue"
),
EncodedUrl(
"https://kiwix.app/foo%2Fpart%20with%20space%2Fbar%3Fkey%3Dvalue",
"https://kiwix.app/foo%2Fpart%20with%20space%2Fbar%3Fkey%3Dvalue"
),
EncodedUrl(
"https://kiwix.app/foo/part%20with%20space/bar?key=value",
"https://kiwix.app/foo/part%20with%20space/bar?key=value"
),
EncodedUrl(
"https://kiwix.app/foo/part/file%20with%20%3F%20and%20+?who=Chip%26Dale&quer=Is%20" +
"there%20any%20%2B%20here%3F",
"https://kiwix.app/foo/part/file%20with%20%3F%20and%20+?who=Chip%26Dale&quer" +
"=Is%20there%20any%20%2B%20here%3F"
),
EncodedUrl(
"https://kiwix.app/foo/part/file%20with%20%253F%20and%20%2B%3Fwho%3DChip%2526Dale%26" +
"quer%3DIs%2520there%2520any%2520%252B%2520here%253F",
"https://kiwix.app/foo/part/file%20with%20%253F%20and%20%2B%3Fwho%3DChip" +
"%2526Dale%26quer%3DIs%2520there%2520any%2520%252B%2520here%253F"
),
EncodedUrl(
"https://kiwix.app/foo/part/file%20with%20%3F%20and%20%2B%3Fwho%3DChip%26Dale%26" +
"question%3DIt%20there%20any%20%2B%20here%3F",
"https://kiwix.app/foo/part/file%20with%20%3F%20and%20%2B%3Fwho%3DChip%26" +
"Dale%26question%3DIt%20there%20any%20%2B%20here%3F"
),
EncodedUrl(
"https://kiwix.app/foo/part/file%3Fquestion%3DIs%2520there%2520any" +
"%2520%252B%2520here%253F",
"https://kiwix.app/foo/part/file%3Fquestion%3DIs%2520there%2520" +
"any%2520%252B%2520here%253F"
),
EncodedUrl(
"https://kiwix.app/foo/part/file%3Fquestion%3DIs%2Bthere%2Bany%2B%252B%2Bhere%253F",
"https://kiwix.app/foo/part/file%3Fquestion%3DIs%2Bthere%2Bany%2B%252B%2B" +
"here%253F"
),
EncodedUrl(
"https://kiwix.app/%F0%9F%A5%B3%F0%9F%A5%B0%F0%9F%98%98%F0%9F%A4%A9%F0%9F%98%8D%F0%9F" +
"%A4%8D%F0%9F%8E%80%F0%9F%A7%B8%F0%9F%8C%B7%F0%9F%8D%AD",
"https://kiwix.app/%F0%9F%A5%B3%F0%9F%A5%B0%F0%9F%98%98%F0%9F%A4%A9%F0%9F%98%8D" +
"%F0%9F%A4%8D%F0%9F%8E%80%F0%9F%A7%B8%F0%9F%8C%B7%F0%9F%8D%AD"
)
)
encodedUrls.forEach { encodedUrl ->
Assert.assertEquals(
encodedUrl.expectedUrl,
zimFileReader.getRedirect(encodedUrl.url)
)
}
// dispose the ZimFileReader
zimFileReader.dispose()
}
val archive = zimReaderSource.createArchive()
val zimFileReader = ZimFileReader(
zimReaderSource,
archive!!,
DarkModeConfig(SharedPreferenceUtil(context), context),
SuggestionSearcher(archive)
)
val encodedUrls = arrayOf(
EncodedUrl(
"https://kiwix.app/foo/part%2520with%2520space/bar%253Fkey%253Dvalue",
"https://kiwix.app/foo/part%2520with%2520space/bar%253Fkey%253Dvalue"
),
EncodedUrl(
"https://kiwix.app/foo/part%20with%20space/bar%3Fkey%3Dvalue",
"https://kiwix.app/foo/part%20with%20space/bar%3Fkey%3Dvalue"
),
EncodedUrl(
"https://kiwix.app/foo%2Fpart%20with%20space%2Fbar%3Fkey%3Dvalue",
"https://kiwix.app/foo%2Fpart%20with%20space%2Fbar%3Fkey%3Dvalue"
),
EncodedUrl(
"https://kiwix.app/foo/part%20with%20space/bar?key=value",
"https://kiwix.app/foo/part%20with%20space/bar?key=value"
),
EncodedUrl(
"https://kiwix.app/foo/part/file%20with%20%3F%20and%20+?who=Chip%26Dale&quer=Is%20" +
"there%20any%20%2B%20here%3F",
"https://kiwix.app/foo/part/file%20with%20%3F%20and%20+?who=Chip%26Dale&quer" +
"=Is%20there%20any%20%2B%20here%3F"
),
EncodedUrl(
"https://kiwix.app/foo/part/file%20with%20%253F%20and%20%2B%3Fwho%3DChip%2526Dale%26" +
"quer%3DIs%2520there%2520any%2520%252B%2520here%253F",
"https://kiwix.app/foo/part/file%20with%20%253F%20and%20%2B%3Fwho%3DChip" +
"%2526Dale%26quer%3DIs%2520there%2520any%2520%252B%2520here%253F"
),
EncodedUrl(
"https://kiwix.app/foo/part/file%20with%20%3F%20and%20%2B%3Fwho%3DChip%26Dale%26" +
"question%3DIt%20there%20any%20%2B%20here%3F",
"https://kiwix.app/foo/part/file%20with%20%3F%20and%20%2B%3Fwho%3DChip%26" +
"Dale%26question%3DIt%20there%20any%20%2B%20here%3F"
),
EncodedUrl(
"https://kiwix.app/foo/part/file%3Fquestion%3DIs%2520there%2520any" +
"%2520%252B%2520here%253F",
"https://kiwix.app/foo/part/file%3Fquestion%3DIs%2520there%2520" +
"any%2520%252B%2520here%253F"
),
EncodedUrl(
"https://kiwix.app/foo/part/file%3Fquestion%3DIs%2Bthere%2Bany%2B%252B%2Bhere%253F",
"https://kiwix.app/foo/part/file%3Fquestion%3DIs%2Bthere%2Bany%2B%252B%2B" +
"here%253F"
),
EncodedUrl(
"https://kiwix.app/%F0%9F%A5%B3%F0%9F%A5%B0%F0%9F%98%98%F0%9F%A4%A9%F0%9F%98%8D%F0%9F" +
"%A4%8D%F0%9F%8E%80%F0%9F%A7%B8%F0%9F%8C%B7%F0%9F%8D%AD",
"https://kiwix.app/%F0%9F%A5%B3%F0%9F%A5%B0%F0%9F%98%98%F0%9F%A4%A9%F0%9F%98%8D" +
"%F0%9F%A4%8D%F0%9F%8E%80%F0%9F%A7%B8%F0%9F%8C%B7%F0%9F%8D%AD"
)
)
encodedUrls.forEach { encodedUrl ->
Assert.assertEquals(
encodedUrl.expectedUrl,
zimFileReader.getRedirect(encodedUrl.url)
)
}
// dispose the ZimFileReader
zimFileReader.dispose()
}
@After

View File

@ -22,7 +22,6 @@ import androidx.core.content.ContextCompat
import androidx.core.content.edit
import androidx.core.net.toUri
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
import androidx.test.core.app.ActivityScenario
import androidx.test.espresso.accessibility.AccessibilityChecks
@ -33,7 +32,7 @@ import androidx.test.uiautomator.UiDevice
import com.google.android.apps.common.testing.accessibility.framework.AccessibilityCheckResultUtils.matchesCheck
import com.google.android.apps.common.testing.accessibility.framework.AccessibilityCheckResultUtils.matchesViews
import com.google.android.apps.common.testing.accessibility.framework.checks.TouchTargetSizeCheck
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.hamcrest.Matchers.allOf
import org.junit.After
import org.junit.Assert
@ -133,23 +132,19 @@ class ZimFileReaderWithSplittedZimFileTest : BaseActivityTest() {
}
@Test
fun testWithExtraZeroSizeFile() {
fun testWithExtraZeroSizeFile() = runBlocking {
createAndGetSplitedZimFile(true)?.let { zimFile ->
// test the articleCount and mediaCount of this zim file.
val zimReaderSource = ZimReaderSource(zimFile)
activityScenario.onActivity {
it.lifecycleScope.launch {
val archive = zimReaderSource.createArchive()
val zimFileReader = ZimFileReader(
zimReaderSource,
archive!!,
DarkModeConfig(SharedPreferenceUtil(context), context),
SuggestionSearcher(archive)
)
Assert.assertEquals(zimFileReader.mediaCount, 16)
Assert.assertEquals(zimFileReader.articleCount, 4)
}
}
val archive = zimReaderSource.createArchive()
val zimFileReader = ZimFileReader(
zimReaderSource,
archive!!,
DarkModeConfig(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")