runBlocking in test

Ignore warnings
This commit is contained in:
Oupson 2021-03-04 13:45:34 +01:00
parent ffb60346a7
commit 7ca74af4d4
3 changed files with 91 additions and 56 deletions

View File

@ -5,6 +5,7 @@ import android.graphics.drawable.AnimationDrawable
import android.graphics.drawable.BitmapDrawable import android.graphics.drawable.BitmapDrawable
import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry
import junit.framework.TestCase import junit.framework.TestCase
import kotlinx.coroutines.runBlocking
import org.junit.Test import org.junit.Test
import oupson.apng.Utils.Companion.areBitmapSimilar import oupson.apng.Utils.Companion.areBitmapSimilar
import oupson.apng.Utils.Companion.getFrame import oupson.apng.Utils.Companion.getFrame
@ -16,11 +17,13 @@ class ApngDecoderInstrumentedTest {
fun testBtmConfigDecoding() { fun testBtmConfigDecoding() {
val context = InstrumentationRegistry.getInstrumentation().context val context = InstrumentationRegistry.getInstrumentation().context
val input = context.assets.open("sushi.png") val input = context.assets.open("sushi.png")
val anim = ApngDecoder.decodeApng( val anim = runBlocking {
ApngDecoder.decodeApng(
context, context,
input, input,
ApngDecoder.Config(bitmapConfig = Bitmap.Config.RGB_565) ApngDecoder.Config(bitmapConfig = Bitmap.Config.RGB_565)
) as AnimationDrawable ) as AnimationDrawable
}
for (i in 0 until anim.numberOfFrames) { for (i in 0 until anim.numberOfFrames) {
TestCase.assertTrue((anim.getFrame(i) as BitmapDrawable).bitmap.config == Bitmap.Config.RGB_565) TestCase.assertTrue((anim.getFrame(i) as BitmapDrawable).bitmap.config == Bitmap.Config.RGB_565)
@ -33,11 +36,13 @@ class ApngDecoderInstrumentedTest {
val list = context.assets.list("bunny")?.map { getFrame(context, "bunny/$it") }!! val list = context.assets.list("bunny")?.map { getFrame(context, "bunny/$it") }!!
val input = context.assets.open("bugbuckbunny.png") val input = context.assets.open("bugbuckbunny.png")
val anim = ApngDecoder.decodeApng( val anim = runBlocking {
ApngDecoder.decodeApng(
context, context,
input, input,
ApngDecoder.Config(bitmapConfig = Bitmap.Config.ARGB_8888, decodeCoverFrame = true) ApngDecoder.Config(bitmapConfig = Bitmap.Config.ARGB_8888, decodeCoverFrame = true)
) as ApngDrawable ) as ApngDrawable
}
TestCase.assertTrue(areBitmapSimilar(list[0], anim.coverFrame!!)) TestCase.assertTrue(areBitmapSimilar(list[0], anim.coverFrame!!))
for (i in 0 until anim.numberOfFrames) { for (i in 0 until anim.numberOfFrames) {

View File

@ -5,6 +5,7 @@ import android.graphics.drawable.BitmapDrawable
import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry
import junit.framework.TestCase.assertFalse import junit.framework.TestCase.assertFalse
import junit.framework.TestCase.assertTrue import junit.framework.TestCase.assertTrue
import kotlinx.coroutines.runBlocking
import org.junit.Test import org.junit.Test
import oupson.apng.Utils.Companion.areBitmapSimilar import oupson.apng.Utils.Companion.areBitmapSimilar
import oupson.apng.Utils.Companion.getFrame import oupson.apng.Utils.Companion.getFrame
@ -57,7 +58,8 @@ class ApngEncoderInstrumentedTest {
val list = context.assets.list("ball")?.map { getFrame(context, "ball/$it") }!! val list = context.assets.list("ball")?.map { getFrame(context, "ball/$it") }!!
val optimisedOutputStream = ByteArrayOutputStream() val optimisedOutputStream = ByteArrayOutputStream()
val optimisedEncoder = ApngEncoder(optimisedOutputStream, list[0].width, list[0].height, list.size) val optimisedEncoder =
ApngEncoder(optimisedOutputStream, list[0].width, list[0].height, list.size)
.setOptimiseApng(true) .setOptimiseApng(true)
list.forEach { list.forEach {
@ -70,8 +72,14 @@ class ApngEncoderInstrumentedTest {
val bytes = optimisedOutputStream.toByteArray() val bytes = optimisedOutputStream.toByteArray()
val optimisedInputStream = ByteArrayInputStream(bytes) val optimisedInputStream = ByteArrayInputStream(bytes)
val optimisedApng = val optimisedApng =
ApngDecoder.decodeApng(context, optimisedInputStream) as AnimationDrawable runBlocking {
ApngDecoder.decodeApng(
context,
optimisedInputStream
) as AnimationDrawable
}
optimisedInputStream.close() optimisedInputStream.close()
@ -86,7 +94,8 @@ class ApngEncoderInstrumentedTest {
val nonOptimisedOutputStream = ByteArrayOutputStream() val nonOptimisedOutputStream = ByteArrayOutputStream()
val nonOptimisedEncoder = ApngEncoder(nonOptimisedOutputStream, list[0].width, list[0].height, list.size) val nonOptimisedEncoder =
ApngEncoder(nonOptimisedOutputStream, list[0].width, list[0].height, list.size)
.setOptimiseApng(false) .setOptimiseApng(false)
list.forEach { list.forEach {
@ -101,7 +110,12 @@ class ApngEncoderInstrumentedTest {
val nonOptimisedInputStream = ByteArrayInputStream(nonOptimisedBytes) val nonOptimisedInputStream = ByteArrayInputStream(nonOptimisedBytes)
val nonOptimisedApng = val nonOptimisedApng =
ApngDecoder.decodeApng(context, nonOptimisedInputStream) as AnimationDrawable runBlocking {
ApngDecoder.decodeApng(
context,
nonOptimisedInputStream
) as AnimationDrawable
}
nonOptimisedInputStream.close() nonOptimisedInputStream.close()
for (i in 0 until optimisedApng.numberOfFrames) { for (i in 0 until optimisedApng.numberOfFrames) {
@ -120,7 +134,8 @@ class ApngEncoderInstrumentedTest {
val list = context.assets.list("bunny")?.map { getFrame(context, "bunny/$it") }!! val list = context.assets.list("bunny")?.map { getFrame(context, "bunny/$it") }!!
val optimisedOutputStream = ByteArrayOutputStream() val optimisedOutputStream = ByteArrayOutputStream()
val optimisedEncoder = ApngEncoder(optimisedOutputStream, list[0].width, list[0].height, list.size) val optimisedEncoder =
ApngEncoder(optimisedOutputStream, list[0].width, list[0].height, list.size)
.setOptimiseApng(true) .setOptimiseApng(true)
list.forEach { list.forEach {
@ -134,7 +149,12 @@ class ApngEncoderInstrumentedTest {
val optimisedInputStream = ByteArrayInputStream(bytes) val optimisedInputStream = ByteArrayInputStream(bytes)
val optimisedApng = val optimisedApng =
ApngDecoder.decodeApng(context, optimisedInputStream) as AnimationDrawable runBlocking {
ApngDecoder.decodeApng(
context,
optimisedInputStream
) as AnimationDrawable
}
optimisedInputStream.close() optimisedInputStream.close()
@ -149,7 +169,8 @@ class ApngEncoderInstrumentedTest {
val nonOptimisedOutputStream = ByteArrayOutputStream() val nonOptimisedOutputStream = ByteArrayOutputStream()
val nonOptimisedEncoder = ApngEncoder(nonOptimisedOutputStream, list[0].width, list[0].height, list.size) val nonOptimisedEncoder =
ApngEncoder(nonOptimisedOutputStream, list[0].width, list[0].height, list.size)
.setOptimiseApng(false) .setOptimiseApng(false)
list.forEach { list.forEach {
@ -164,7 +185,12 @@ class ApngEncoderInstrumentedTest {
val nonOptimisedInputStream = ByteArrayInputStream(nonOptimisedBytes) val nonOptimisedInputStream = ByteArrayInputStream(nonOptimisedBytes)
val nonOptimisedApng = val nonOptimisedApng =
ApngDecoder.decodeApng(context, nonOptimisedInputStream) as AnimationDrawable runBlocking {
ApngDecoder.decodeApng(
context,
nonOptimisedInputStream
) as AnimationDrawable
}
nonOptimisedInputStream.close() nonOptimisedInputStream.close()
for (i in 0 until optimisedApng.numberOfFrames) { for (i in 0 until optimisedApng.numberOfFrames) {

View File

@ -64,6 +64,7 @@ class ApngDecoder {
fun isDecodingCoverFrame(): Boolean { fun isDecodingCoverFrame(): Boolean {
return this.decodeCoverFrame return this.decodeCoverFrame
} }
fun setIsDecodingCoverFrame(decodeCoverFrame: Boolean): Config { fun setIsDecodingCoverFrame(decodeCoverFrame: Boolean): Config {
this.decodeCoverFrame = decodeCoverFrame this.decodeCoverFrame = decodeCoverFrame
return this return this
@ -91,7 +92,10 @@ class ApngDecoder {
* @return [ApngDrawable] if successful and an [AnimatedImageDrawable] if the image decoded is not an APNG but a gif. If it is not an animated image, it is a [Drawable]. * @return [ApngDrawable] if successful and an [AnimatedImageDrawable] if the image decoded is not an APNG but a gif. If it is not an animated image, it is a [Drawable].
*/ */
// TODO DOCUMENT CONFIG // TODO DOCUMENT CONFIG
@Suppress("MemberVisibilityCanBePrivate") @Suppress(
"MemberVisibilityCanBePrivate",
"BlockingMethodInNonBlockingContext"
) // BlockingMethodInNonBlockingContext is a warning generated by java @Throws
@JvmStatic @JvmStatic
@JvmOverloads @JvmOverloads
suspend fun decodeApng( suspend fun decodeApng(
@ -564,7 +568,7 @@ class ApngDecoder {
* @param config Decoder configuration * @param config Decoder configuration
* @return [ApngDrawable] if successful and an [AnimatedImageDrawable] if the image decoded is not an APNG but a gif. If it is not an animated image, it is a [Drawable]. * @return [ApngDrawable] if successful and an [AnimatedImageDrawable] if the image decoded is not an APNG but a gif. If it is not an animated image, it is a [Drawable].
*/ */
@Suppress("unused") @Suppress("unused", "BlockingMethodInNonBlockingContext")
@JvmStatic @JvmStatic
// TODO DOCUMENT // TODO DOCUMENT
suspend fun decodeApng( suspend fun decodeApng(
@ -574,7 +578,7 @@ class ApngDecoder {
): Drawable = ): Drawable =
decodeApng( decodeApng(
context, context,
FileInputStream(file), config withContext(Dispatchers.IO) { FileInputStream(file) }, config
) )
/** /**
@ -649,7 +653,7 @@ class ApngDecoder {
* @param callback [ApngDecoder.Callback] to handle success and error. * @param callback [ApngDecoder.Callback] to handle success and error.
* @param config Decoder configuration * @param config Decoder configuration
*/ */
@Suppress("unused") @Suppress("unused", "BlockingMethodInNonBlockingContext")
@JvmStatic @JvmStatic
@JvmOverloads @JvmOverloads
fun decodeApngAsyncInto( fun decodeApngAsyncInto(
@ -781,7 +785,7 @@ class ApngDecoder {
* @param callback [ApngDecoder.Callback] to handle success and error. * @param callback [ApngDecoder.Callback] to handle success and error.
* @param config Decoder configuration * @param config Decoder configuration
*/ */
@Suppress("unused") @Suppress("unused", "BlockingMethodInNonBlockingContext")
@JvmStatic @JvmStatic
@JvmOverloads @JvmOverloads
fun decodeApngAsyncInto( fun decodeApngAsyncInto(
@ -877,7 +881,7 @@ class ApngDecoder {
} }
} else { } else {
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
callback?.onError(java.lang.Exception("Cannot open string")) callback?.onError(Exception("Cannot open string"))
} }
} }
} catch (e: Exception) { } catch (e: Exception) {