More tests

This commit is contained in:
Oupson 2021-02-06 18:58:56 +01:00
parent be9e5426e0
commit ac3a448834
6 changed files with 107 additions and 12 deletions

View File

@ -18,8 +18,6 @@ import java.io.ByteArrayOutputStream
class ApngEncoderInstrumentedTest { class ApngEncoderInstrumentedTest {
// TODO TEST IF OPTIMISED ANIMATIONS AND NON-OPTIMISED ANIMATIONS ARE THE SAME
@Test @Test
fun testDiffBunny() { fun testDiffBunny() {
val context = InstrumentationRegistry.getInstrumentation().context val context = InstrumentationRegistry.getInstrumentation().context

View File

@ -3,7 +3,6 @@ package oupson.apng.chunks
/** /**
* An interface for the png chunks * An interface for the png chunks
*/ */
@Deprecated("Deprecated", level = DeprecationLevel.WARNING)
interface Chunk { interface Chunk {
var body : ByteArray var body : ByteArray

View File

@ -2,7 +2,6 @@ package oupson.apng.chunks
import oupson.apng.utils.Utils import oupson.apng.utils.Utils
@Deprecated("Deprecated", level = DeprecationLevel.WARNING)
class IHDR : Chunk { class IHDR : Chunk {
override var body = byteArrayOf() override var body = byteArrayOf()
var pngWidth = -1 var pngWidth = -1

View File

@ -5,7 +5,6 @@ import oupson.apng.utils.Utils.Companion.decodeBlendOp
import oupson.apng.utils.Utils.Companion.decodeDisposeOp import oupson.apng.utils.Utils.Companion.decodeDisposeOp
@Suppress("ClassName") @Suppress("ClassName")
@Deprecated("Deprecated", level = DeprecationLevel.WARNING)
class fcTL : Chunk { class fcTL : Chunk {
override var body : ByteArray = byteArrayOf() override var body : ByteArray = byteArrayOf()

View File

@ -109,7 +109,6 @@ class ApngEncoder(
* @param encodeAlpha If the alpha channel must be encoded. * @param encodeAlpha If the alpha channel must be encoded.
* @return [ApngEncoder] for chaining. * @return [ApngEncoder] for chaining.
*/ */
@Suppress("unused")
fun setEncodeAlpha(encodeAlpha: Boolean): ApngEncoder { fun setEncodeAlpha(encodeAlpha: Boolean): ApngEncoder {
if (optimise && !encodeAlpha) if (optimise && !encodeAlpha)
throw BadParameterException("If encodeAlpha is false, then optimise must be false") throw BadParameterException("If encodeAlpha is false, then optimise must be false")
@ -135,12 +134,12 @@ class ApngEncoder(
* - [FILTER_LAST] * - [FILTER_LAST]
* @return [ApngEncoder] for chaining. * @return [ApngEncoder] for chaining.
*/ */
@Suppress("unused") @Throws(BadParameterException::class)
fun setFilter(filter: Int): ApngEncoder { fun setFilter(filter: Int): ApngEncoder {
if (filter <= FILTER_LAST) { if (filter <= FILTER_LAST) {
this.filter = filter this.filter = filter
} else { } else {
Log.e(TAG, "Invalid filter") throw BadParameterException("Invalid filter value")
} }
return this return this
} }
@ -168,6 +167,7 @@ class ApngEncoder(
* Get the repetition count. * Get the repetition count.
* @return An [Int], the number of repetition, zero for infinite. * @return An [Int], the number of repetition, zero for infinite.
*/ */
@Suppress("unused")
fun getRepetitionCount() : Int { fun getRepetitionCount() : Int {
return this.repetitionCount return this.repetitionCount
} }
@ -177,19 +177,25 @@ class ApngEncoder(
* @param compressionLevel A integer between 0 and 9 (not include). * @param compressionLevel A integer between 0 and 9 (not include).
* @return [ApngEncoder] for chaining. * @return [ApngEncoder] for chaining.
*/ */
@Throws(BadParameterException::class)
fun setCompressionLevel(compressionLevel: Int): ApngEncoder { fun setCompressionLevel(compressionLevel: Int): ApngEncoder {
if (compressionLevel in 0..9) { if (compressionLevel in 0..9) {
this.compressionLevel = compressionLevel this.compressionLevel = compressionLevel
} else { } else {
Log.e( throw BadParameterException("Invalid compression level : $compressionLevel, expected a number in range 0..9")
TAG,
"Invalid compression level : $compressionLevel, expected a number in range 0..9"
)
} }
return this return this
} }
/**
* Get compression level.
* @return [Int] the compression level, an integer in between 0 and 9
*/
fun getCompressionLevel() : Int {
return this.compressionLevel
}
/** /**
* Set if the first frame will be included in the animation. * Set if the first frame will be included in the animation.
* @param firstFrameInAnim A boolean. * @param firstFrameInAnim A boolean.
@ -204,6 +210,7 @@ class ApngEncoder(
* Get if the first frame will be included in the animation. * Get if the first frame will be included in the animation.
* @return [Boolean] True if the first frame will be included in the animation. * @return [Boolean] True if the first frame will be included in the animation.
*/ */
@SuppressWarnings("unused")
fun isFirstFrameInAnim() : Boolean { fun isFirstFrameInAnim() : Boolean {
return this.firstFrameInAnim return this.firstFrameInAnim
} }

View File

@ -0,0 +1,93 @@
package oupson.apng
import junit.framework.TestCase.*
import org.junit.Test
import oupson.apng.encoder.ApngEncoder
import oupson.apng.exceptions.BadParameterException
import java.io.ByteArrayOutputStream
class ApngEncoderTest {
@Test
fun testOptimiseApngGetterSetter() {
val outputStream = ByteArrayOutputStream()
val encoder = ApngEncoder(outputStream, 500, 500, 10)
assertTrue(encoder.isAlphaEncoded())
assertTrue(encoder.isOptimisingApng())
encoder.setOptimiseApng(false)
encoder.setEncodeAlpha(false)
assertFalse(encoder.isOptimisingApng())
try {
encoder.setOptimiseApng(true)
fail("setOptimiseApng(true) must throw an exception when encode alpha is false")
} catch (e : BadParameterException) {
// Good behavior
}
encoder.setEncodeAlpha(true)
encoder.setOptimiseApng(true)
assertTrue(encoder.isAlphaEncoded())
outputStream.close()
}
@Test
fun testEncodeAlphaGetterSetter() {
val outputStream = ByteArrayOutputStream()
val encoder = ApngEncoder(outputStream, 500, 500, 10)
assertTrue(encoder.isAlphaEncoded())
assertTrue(encoder.isOptimisingApng())
try {
encoder.setEncodeAlpha(false)
fail("setEncodeAlpha(false) must throw an exception when optimise apng is true")
} catch (e : BadParameterException) {
// Good behavior
}
encoder.setOptimiseApng(false)
encoder.setEncodeAlpha(false)
assertFalse(encoder.isAlphaEncoded())
outputStream.close()
}
@Test
fun testFilters() {
val outputStream = ByteArrayOutputStream()
val encoder = ApngEncoder(outputStream, 500, 500, 10)
for (filters in arrayListOf(ApngEncoder.FILTER_LAST, ApngEncoder.FILTER_NONE, ApngEncoder.FILTER_SUB, ApngEncoder.FILTER_UP)) {
encoder.setFilter(filters)
assertEquals(filters, encoder.getFilter())
}
try {
encoder.setFilter(999)
fail("Invalid filter must throw and exception")
} catch (e : BadParameterException) {
// Good behavior
}
outputStream.close()
}
@Test
fun testCompressionLevel() {
val outputStream = ByteArrayOutputStream()
val encoder = ApngEncoder(outputStream, 500, 500, 10)
for (i in 0..9) {
encoder.setCompressionLevel(i)
assertEquals(encoder.getCompressionLevel(), i)
}
try {
encoder.setCompressionLevel(999)
fail("Invalid compression level must throw and exception")
} catch (e : BadParameterException) {
// Good behavior
}
outputStream.close()
}
}