Merge pull request #3261 from danielzgtg/fix/java19

Fix build on Java 19
This commit is contained in:
Kelson 2023-03-30 16:13:44 +02:00 committed by GitHub
commit b78b4bf75e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 95 deletions

View File

@ -23,9 +23,11 @@ import org.junit.After
import org.junit.Assert import org.junit.Assert
import org.junit.Before import org.junit.Before
import org.junit.Test 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.getAllZimParts
import org.kiwix.kiwixmobile.core.utils.files.FileUtils.hasPart import org.kiwix.kiwixmobile.core.utils.files.FileUtils.hasPart
import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity
import org.kiwix.kiwixmobile.core.utils.files.FileUtils
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
import java.util.Random import java.util.Random
@ -199,4 +201,50 @@ class FileUtilsInstrumentationTest {
it.delete() 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?)
} }

View File

@ -42,7 +42,7 @@ object Versions {
const val io_objectbox: String = "3.5.0" 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" const val android_arch_lifecycle_extensions: String = "1.1.1"

View File

@ -22,6 +22,7 @@ import android.net.ConnectivityManager
import android.net.NetworkInfo import android.net.NetworkInfo
import android.os.Build import android.os.Build
import android.util.Log import android.util.Log
import androidx.annotation.VisibleForTesting
import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.R
import java.lang.Exception import java.lang.Exception
import java.util.UUID import java.util.UUID
@ -41,6 +42,9 @@ object NetworkUtils {
fun isNetworkConnectionOK(networkInfo: NetworkInfo): Boolean = fun isNetworkConnectionOK(networkInfo: NetworkInfo): Boolean =
networkInfo.state == NetworkInfo.State.CONNECTED networkInfo.state == NetworkInfo.State.CONNECTED
@VisibleForTesting
internal var sdkVersionForTesting = Build.VERSION.SDK_INT
/** /**
* check if network of type WIFI is connected * check if network of type WIFI is connected
* *
@ -52,7 +56,7 @@ object NetworkUtils {
fun isWiFi(context: Context): Boolean { fun isWiFi(context: Context): Boolean {
val connectivity = context val connectivity = context
.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager .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 val networkInfo = connectivity.activeNetworkInfo ?: return false
networkInfo.type == ConnectivityManager.TYPE_WIFI && networkInfo.isConnected networkInfo.type == ConnectivityManager.TYPE_WIFI && networkInfo.isConnected
} else { } else {

View File

@ -21,8 +21,6 @@ package org.kiwix.kiwixmobile.core.utils
import android.content.Context import android.content.Context
import android.net.ConnectivityManager import android.net.ConnectivityManager
import android.net.NetworkInfo import android.net.NetworkInfo
import android.os.Build
import android.util.Log
import io.mockk.every import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
import io.mockk.verify import io.mockk.verify
@ -31,8 +29,6 @@ import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue import org.junit.Assert.assertTrue
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.kiwix.kiwixmobile.core.R import org.kiwix.kiwixmobile.core.R
import java.lang.reflect.Field
import java.lang.reflect.Modifier
import java.util.regex.Pattern import java.util.regex.Pattern
class NetworkUtilsTest { class NetworkUtilsTest {
@ -79,11 +75,7 @@ class NetworkUtilsTest {
every { (connectivity.activeNetworkInfo) } returns networkInfo every { (connectivity.activeNetworkInfo) } returns networkInfo
// SDK >= 23 // SDK >= 23
try { NetworkUtils.sdkVersionForTesting = 23
setSDKVersion(Build.VERSION::class.java.getField("SDK_INT"), 23)
} catch (e: Exception) {
Log.d("NetworkUtilsTest", "Unable to set Build SDK Version")
}
// on Mobile Data // on Mobile Data
every { (networkInfo.type) } returns ConnectivityManager.TYPE_MOBILE every { (networkInfo.type) } returns ConnectivityManager.TYPE_MOBILE
@ -110,11 +102,7 @@ class NetworkUtilsTest {
verify(exactly = 0) { connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI) } verify(exactly = 0) { connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI) }
// SDK < 23 // SDK < 23
try { NetworkUtils.sdkVersionForTesting = 22
setSDKVersion(Build.VERSION::class.java.getField("SDK_INT"), 22)
} catch (e: Exception) {
Log.d("NetworkUtilsTest", "Unable to set Build SDK Version")
}
// WIFI connected // WIFI connected
every { (connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI)) } returns networkInfo 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)
}
} }

View File

@ -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.clearMocks
import io.mockk.every import io.mockk.every
import io.mockk.mockk import io.mockk.mockk
import org.junit.Before import org.junit.jupiter.api.Test
import org.junit.Test
import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.BeforeEach
import org.kiwix.kiwixmobile.BaseActivityTest
import org.kiwix.kiwixmobile.core.CoreApp import org.kiwix.kiwixmobile.core.CoreApp
import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity.Book 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 import java.io.File
class FileUtilsTest : BaseActivityTest() { class FileUtilsTest {
private val mockFile: File = mockk() private val mockFile: File = mockk()
private val testBook = Book().apply { file = mockFile } private val testBook = Book().apply { file = mockFile }
private val testId = "8ce5775a-10a9-bbf3-178a-9df69f23263c" private val testId = "8ce5775a-10a9-bbf3-178a-9df69f23263c"
private val fileName = "/data/user/0/org.kiwix.kiwixmobile/files${File.separator}$testId" 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 @BeforeEach
fun init() { fun init() {
clearMocks(mockFile) clearMocks(mockFile)
@ -117,50 +100,4 @@ class FileUtilsTest : BaseActivityTest() {
every { mockFile.path } returns "$fileName$extension" every { mockFile.path } returns "$fileName$extension"
every { mockFile.exists() } returns fileExists 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?)
} }