SubwayTooter-Android-App/exif/src/test/java/it/sephiroth/android/library/exif2/Test1.kt

115 lines
2.9 KiB
Kotlin
Raw Normal View History

2019-10-06 22:46:56 +02:00
package it.sephiroth.android.library.exif2
import android.util.Log
import android.util.SparseIntArray
2019-10-07 20:46:19 +02:00
import org.junit.Assert.*
2019-10-06 22:46:56 +02:00
import org.junit.Test
import java.io.File
import java.io.FileInputStream
class Test1 {
@Test
fun testLog() {
2019-10-07 20:46:19 +02:00
Log.v("TEST", "test")
2019-10-06 22:46:56 +02:00
assertTrue("using android.util.Log", true)
}
@Test
fun testSparseIntArray() {
val a = SparseIntArray()
a.put(1, 2)
assertTrue("get existing value", a[1] == 2)
assertTrue("fallback to default value ", a.get(0, - 1) == - 1)
}
// get File object from files in src/test/resources/
private fun getFile(fileName : String) : File {
return when(val resource = this.javaClass.classLoader !!.getResource(fileName)) {
null -> error("missing file $fileName")
else -> File(resource.path)
}
}
2019-10-07 20:46:19 +02:00
private fun getOrientation(fileName : String) : Pair<Int?, Throwable?> =
try {
val o = FileInputStream(getFile(fileName)).use { inStream ->
ExifInterface()
2019-10-07 23:19:45 +02:00
.readExif(
inStream,
ExifInterface.Options.OPTION_IFD_0
or ExifInterface.Options.OPTION_IFD_1
or ExifInterface.Options.OPTION_IFD_EXIF
)
2019-10-07 20:46:19 +02:00
.getTagIntValue(ExifInterface.TAG_ORIENTATION)
}
Pair(o, null)
} catch(ex : Throwable) {
Pair(null, ex)
}
private fun getThumbnailBytes(fileName : String) : Pair<ByteArray?, Throwable?> =
try {
val o = FileInputStream(getFile(fileName)).use { inStream ->
ExifInterface()
2019-10-07 23:19:45 +02:00
.readExif(
inStream,
ExifInterface.Options.OPTION_IFD_0
or ExifInterface.Options.OPTION_IFD_1
or ExifInterface.Options.OPTION_IFD_EXIF
)
2019-10-07 20:46:19 +02:00
.thumbnailBytes
}
Pair(o, null)
} catch(ex : Throwable) {
Pair(null, ex)
2019-10-06 22:46:56 +02:00
}
@Test
fun testNotJpeg() {
2019-10-07 23:19:45 +02:00
fun testNotJpegSub(fileName : String) {
val (o, ex) = getOrientation(fileName)
assertTrue("testNotJpegSub", o == null && ex != null)
}
2019-10-06 22:46:56 +02:00
testNotJpegSub("test.gif")
testNotJpegSub("test.png")
testNotJpegSub("test.webp")
}
@Test
fun testJpeg() {
2019-10-07 20:46:19 +02:00
var fileName : String
var rvO : Pair<Int?, Throwable?>
var rvT : Pair<ByteArray?, Throwable?>
// this file has orientation 6.
fileName = "test3.jpg"
rvO = getOrientation(fileName)
assertEquals(fileName, 6, rvO.first)
rvT = getThumbnailBytes(fileName)
2019-10-07 23:19:45 +02:00
assertNull(fileName, rvT.first)
2019-10-07 20:46:19 +02:00
2019-10-06 22:46:56 +02:00
// this file has orientation 1
fileName = "test1.jpg"
2019-10-07 20:46:19 +02:00
rvO = getOrientation(fileName)
assertEquals(fileName, 1, rvO.first)
rvT = getThumbnailBytes(fileName)
2019-10-07 23:19:45 +02:00
assertNull(fileName, rvT.first)
2019-10-06 22:46:56 +02:00
2019-10-06 23:00:21 +02:00
// this file has no orientation, it raises exception.
2019-10-06 22:46:56 +02:00
fileName = "test2.jpg"
2019-10-07 20:46:19 +02:00
rvO = getOrientation(fileName)
2019-10-07 23:19:45 +02:00
assertNotNull(
fileName,
rvO.second
) // <java.lang.IllegalStateException: stop before hitting compressed data>
2019-10-07 20:46:19 +02:00
rvT = getThumbnailBytes(fileName)
2019-10-07 23:19:45 +02:00
assertNotNull(
fileName,
rvT.second
) // <java.lang.IllegalStateException: stop before hitting compressed data>
2019-10-06 22:46:56 +02:00
}
}