MimeTypeTest added

This commit is contained in:
MohitMali 2022-05-05 17:43:47 +05:30 committed by Emmanuel Engelhart
parent e5a58521b9
commit 019bfea06c
No known key found for this signature in database
GPG Key ID: 120B30D020B553D3
2 changed files with 72 additions and 1 deletions

View File

@ -0,0 +1,71 @@
/*
* Kiwix Android
* Copyright (c) 2022 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.mimetype
import android.util.Log
import androidx.core.content.edit
import androidx.preference.PreferenceManager
import androidx.test.rule.ActivityTestRule
import org.junit.Test
import org.kiwix.kiwixlib.JNIKiwixReader
import org.kiwix.kiwixmobile.BaseActivityTest
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 java.io.File
import java.io.FileOutputStream
import java.io.OutputStream
class MimeTypeTest : BaseActivityTest() {
override var activityRule: ActivityTestRule<KiwixMainActivity> = activityTestRule {
PreferenceManager.getDefaultSharedPreferences(context).edit {
putBoolean(SharedPreferenceUtil.PREF_SHOW_INTRO, false)
putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false)
}
}
@Test
fun testMimeType() {
val loadFileStream = MimeTypeTest::class.java.classLoader.getResourceAsStream("testzim.zim")
val zimFile = File(context.cacheDir, "testzim.zim")
if (zimFile.exists()) zimFile.delete()
zimFile.createNewFile()
loadFileStream.use { inputStream ->
val out: OutputStream = FileOutputStream(zimFile)
out.use { out ->
// Transfer bytes from in to out
val buf = ByteArray(1024)
var len: Int
while (inputStream.read(buf).also { len = it } > 0) {
out.write(buf, 0, len)
}
}
}
val zimFileReader = ZimFileReader(
zimFile,
JNIKiwixReader(zimFile.canonicalPath),
NightModeConfig(SharedPreferenceUtil(context), context)
)
zimFileReader.readMimeType(zimFileReader.getRandomArticleUrl()!!).also {
Log.e("ZIMFILEREADER", "testMimeType: $it")
}
}
}

View File

@ -127,7 +127,7 @@ class ZimFileReader constructor(
}
fun readMimeType(uri: String): String = getContentAndMimeType(uri)
.second.substringBefore(";").also {
.second.replace("^([^ ]+).*$", "$1").substringBefore(";").also {
Log.d(TAG, "getting mimetype for $uri = $it")
}