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

88 lines
2.2 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
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import java.io.File
import java.io.FileInputStream
class Test1 {
@Test
fun testLog() {
2019-10-06 23:00:21 +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)
}
}
private fun getOrientation(fileName : String) : Pair<Int?,Throwable?> =
try{
val o = FileInputStream(getFile(fileName)).use { inStream ->
ExifInterface()
.apply {
readExif(
inStream,
ExifInterface.Options.OPTION_IFD_0
or ExifInterface.Options.OPTION_IFD_1
or ExifInterface.Options.OPTION_IFD_EXIF
)
}
.getTagIntValue(ExifInterface.TAG_ORIENTATION)
}
Pair(o,null)
}catch(ex:Throwable){
Pair(null,ex)
}
private fun testNotJpegSub(fileName : String) {
val(o,ex) = getOrientation(fileName)
assertTrue("testNotJpegSub",o ==null && ex!=null)
if( ex!= null) println("exception raised: ${ex::class.java} ${ex.message}")
}
@Test
fun testNotJpeg() {
testNotJpegSub("test.gif")
testNotJpegSub("test.png")
testNotJpegSub("test.webp")
}
@Test
fun testJpeg() {
var fileName :String
var rv : Pair<Int?,Throwable?>
// this file has orientation 1
fileName = "test1.jpg"
rv = getOrientation(fileName)
assertEquals(fileName,1,rv.first)
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"
rv = getOrientation(fileName)
assertTrue(fileName,rv.second != null)
// this file has orientation 6.
fileName = "test3.jpg"
rv = getOrientation(fileName)
assertEquals(fileName,rv.first , 6)
}
}