mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-08 23:07:26 -04:00
Merge pull request #3261 from danielzgtg/fix/java19
Fix build on Java 19
This commit is contained in:
commit
b78b4bf75e
@ -23,9 +23,11 @@ import org.junit.After
|
||||
import org.junit.Assert
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.jupiter.api.Assertions
|
||||
import org.kiwix.kiwixmobile.core.utils.files.FileUtils.getAllZimParts
|
||||
import org.kiwix.kiwixmobile.core.utils.files.FileUtils.hasPart
|
||||
import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity
|
||||
import org.kiwix.kiwixmobile.core.utils.files.FileUtils
|
||||
import java.io.File
|
||||
import java.io.IOException
|
||||
import java.util.Random
|
||||
@ -199,4 +201,50 @@ class FileUtilsInstrumentationTest {
|
||||
it.delete()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDecodeFileName() {
|
||||
val dummyUrlArray = listOf(
|
||||
DummyUrlData(
|
||||
"https://kiwix.org/contributors/contributors_list.pdf",
|
||||
"contributors_list.pdf"
|
||||
),
|
||||
DummyUrlData(
|
||||
"https://kiwix.org/contributors/",
|
||||
null
|
||||
),
|
||||
DummyUrlData(
|
||||
"android_tutorials.pdf",
|
||||
null
|
||||
),
|
||||
DummyUrlData(
|
||||
null,
|
||||
null
|
||||
),
|
||||
DummyUrlData(
|
||||
"/html/images/test.png",
|
||||
"test.png"
|
||||
),
|
||||
DummyUrlData(
|
||||
"/html/images/",
|
||||
null
|
||||
),
|
||||
DummyUrlData(
|
||||
"https://kiwix.org/contributors/images/wikipedia.png",
|
||||
"wikipedia.png"
|
||||
),
|
||||
DummyUrlData(
|
||||
"https://kiwix.org/contributors/images/wikipedia",
|
||||
null
|
||||
)
|
||||
)
|
||||
dummyUrlArray.forEach {
|
||||
Assertions.assertEquals(
|
||||
FileUtils.getDecodedFileName(it.url),
|
||||
it.expectedFileName
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
data class DummyUrlData(val url: String?, val expectedFileName: String?)
|
||||
}
|
||||
|
@ -42,7 +42,7 @@ object Versions {
|
||||
|
||||
const val io_objectbox: String = "3.5.0"
|
||||
|
||||
const val io_mockk: String = "1.12.0"
|
||||
const val io_mockk: String = "1.13.4"
|
||||
|
||||
const val android_arch_lifecycle_extensions: String = "1.1.1"
|
||||
|
||||
|
@ -22,6 +22,7 @@ import android.net.ConnectivityManager
|
||||
import android.net.NetworkInfo
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import androidx.annotation.VisibleForTesting
|
||||
import org.kiwix.kiwixmobile.core.R
|
||||
import java.lang.Exception
|
||||
import java.util.UUID
|
||||
@ -41,6 +42,9 @@ object NetworkUtils {
|
||||
fun isNetworkConnectionOK(networkInfo: NetworkInfo): Boolean =
|
||||
networkInfo.state == NetworkInfo.State.CONNECTED
|
||||
|
||||
@VisibleForTesting
|
||||
internal var sdkVersionForTesting = Build.VERSION.SDK_INT
|
||||
|
||||
/**
|
||||
* check if network of type WIFI is connected
|
||||
*
|
||||
@ -52,7 +56,7 @@ object NetworkUtils {
|
||||
fun isWiFi(context: Context): Boolean {
|
||||
val connectivity = context
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
return if (sdkVersionForTesting >= Build.VERSION_CODES.M) {
|
||||
val networkInfo = connectivity.activeNetworkInfo ?: return false
|
||||
networkInfo.type == ConnectivityManager.TYPE_WIFI && networkInfo.isConnected
|
||||
} else {
|
||||
|
@ -21,8 +21,6 @@ package org.kiwix.kiwixmobile.core.utils
|
||||
import android.content.Context
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.NetworkInfo
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
@ -31,8 +29,6 @@ import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.kiwix.kiwixmobile.core.R
|
||||
import java.lang.reflect.Field
|
||||
import java.lang.reflect.Modifier
|
||||
import java.util.regex.Pattern
|
||||
|
||||
class NetworkUtilsTest {
|
||||
@ -79,11 +75,7 @@ class NetworkUtilsTest {
|
||||
every { (connectivity.activeNetworkInfo) } returns networkInfo
|
||||
|
||||
// SDK >= 23
|
||||
try {
|
||||
setSDKVersion(Build.VERSION::class.java.getField("SDK_INT"), 23)
|
||||
} catch (e: Exception) {
|
||||
Log.d("NetworkUtilsTest", "Unable to set Build SDK Version")
|
||||
}
|
||||
NetworkUtils.sdkVersionForTesting = 23
|
||||
|
||||
// on Mobile Data
|
||||
every { (networkInfo.type) } returns ConnectivityManager.TYPE_MOBILE
|
||||
@ -110,11 +102,7 @@ class NetworkUtilsTest {
|
||||
verify(exactly = 0) { connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI) }
|
||||
|
||||
// SDK < 23
|
||||
try {
|
||||
setSDKVersion(Build.VERSION::class.java.getField("SDK_INT"), 22)
|
||||
} catch (e: Exception) {
|
||||
Log.d("NetworkUtilsTest", "Unable to set Build SDK Version")
|
||||
}
|
||||
NetworkUtils.sdkVersionForTesting = 22
|
||||
|
||||
// WIFI connected
|
||||
every { (connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI)) } returns networkInfo
|
||||
@ -236,17 +224,4 @@ class NetworkUtilsTest {
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
// Sets the Build SDK version
|
||||
@Throws(Exception::class)
|
||||
private fun setSDKVersion(
|
||||
field: Field,
|
||||
newValue: Any
|
||||
) {
|
||||
field.isAccessible = true
|
||||
val modifiersField = Field::class.java.getDeclaredField("modifiers")
|
||||
modifiersField.isAccessible = true
|
||||
modifiersField.setInt(field, field.modifiers and Modifier.FINAL.inv())
|
||||
field.set(null, newValue)
|
||||
}
|
||||
}
|
||||
|
@ -16,42 +16,25 @@
|
||||
*
|
||||
*/
|
||||
|
||||
package org.kiwix.kiwixmobile.utils.files
|
||||
package org.kiwix.kiwixmobile.core.utils.files
|
||||
|
||||
import androidx.core.content.edit
|
||||
import androidx.preference.PreferenceManager
|
||||
import androidx.test.platform.app.InstrumentationRegistry
|
||||
import androidx.test.uiautomator.UiDevice
|
||||
import io.mockk.clearMocks
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.kiwix.kiwixmobile.BaseActivityTest
|
||||
import org.kiwix.kiwixmobile.core.CoreApp
|
||||
import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity.Book
|
||||
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
|
||||
import org.kiwix.kiwixmobile.core.utils.files.FileUtils
|
||||
import java.io.File
|
||||
|
||||
class FileUtilsTest : BaseActivityTest() {
|
||||
class FileUtilsTest {
|
||||
|
||||
private val mockFile: File = mockk()
|
||||
private val testBook = Book().apply { file = mockFile }
|
||||
private val testId = "8ce5775a-10a9-bbf3-178a-9df69f23263c"
|
||||
private val fileName = "/data/user/0/org.kiwix.kiwixmobile/files${File.separator}$testId"
|
||||
|
||||
@Before
|
||||
override fun waitForIdle() {
|
||||
UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).waitForIdle()
|
||||
PreferenceManager.getDefaultSharedPreferences(context).edit {
|
||||
putBoolean(SharedPreferenceUtil.PREF_SHOW_INTRO, false)
|
||||
putBoolean(SharedPreferenceUtil.PREF_WIFI_ONLY, false)
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
fun init() {
|
||||
clearMocks(mockFile)
|
||||
@ -117,50 +100,4 @@ class FileUtilsTest : BaseActivityTest() {
|
||||
every { mockFile.path } returns "$fileName$extension"
|
||||
every { mockFile.exists() } returns fileExists
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testDecodeFileName() {
|
||||
val dummyUrlArray = listOf(
|
||||
DummyUrlData(
|
||||
"https://kiwix.org/contributors/contributors_list.pdf",
|
||||
"contributors_list.pdf"
|
||||
),
|
||||
DummyUrlData(
|
||||
"https://kiwix.org/contributors/",
|
||||
null
|
||||
),
|
||||
DummyUrlData(
|
||||
"android_tutorials.pdf",
|
||||
null
|
||||
),
|
||||
DummyUrlData(
|
||||
null,
|
||||
null
|
||||
),
|
||||
DummyUrlData(
|
||||
"/html/images/test.png",
|
||||
"test.png"
|
||||
),
|
||||
DummyUrlData(
|
||||
"/html/images/",
|
||||
null
|
||||
),
|
||||
DummyUrlData(
|
||||
"https://kiwix.org/contributors/images/wikipedia.png",
|
||||
"wikipedia.png"
|
||||
),
|
||||
DummyUrlData(
|
||||
"https://kiwix.org/contributors/images/wikipedia",
|
||||
null
|
||||
)
|
||||
)
|
||||
dummyUrlArray.forEach {
|
||||
assertEquals(
|
||||
FileUtils.getDecodedFileName(it.url),
|
||||
it.expectedFileName
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
data class DummyUrlData(val url: String?, val expectedFileName: String?)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user