Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#457: Converted java to Kotlin #835

Open
wants to merge 11 commits into
base: dev
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@ package pl.droidsonroids.gif

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
import org.assertj.core.api.Assertions
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import pl.droidsonroids.gif.test.R

@RunWith(AndroidJUnit4::class)
class AllocationByteCountTest {

@Test
fun allocationByteCountIsConsistent() {
val resources = getInstrumentation().context.resources
val drawable = GifDrawable(resources, pl.droidsonroids.gif.test.R.raw.test)
val metaData = GifAnimationMetaData(resources, pl.droidsonroids.gif.test.R.raw.test)
Assertions.assertThat(drawable.frameByteCount + metaData.allocationByteCount)

val drawable = GifDrawable(resources, R.raw.test)

val metaData = GifAnimationMetaData(resources, R.raw.test)

assertThat(drawable.frameByteCount + metaData.allocationByteCount)
.isEqualTo(drawable.allocationByteCount)
Assertions.assertThat(metaData.getDrawableAllocationByteCount(null, 1))
assertThat(metaData.getDrawableAllocationByteCount(null, 1))
.isEqualTo(drawable.allocationByteCount)
Assertions.assertThat(metaData.getDrawableAllocationByteCount(drawable, 1))
assertThat(metaData.getDrawableAllocationByteCount(drawable, 1))
.isEqualTo(drawable.allocationByteCount)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ class ErrnoMessageTest {
@Test
fun errnoMessageAppendedToOpenFailed() {
mExpectedException.expect(GifIOException::class.java)

mExpectedException.expectMessage("GifError 101: Failed to open given input: No such file or directory")

val nonExistentFile = File(mTemporaryFolder.root, "non-existent")

GifDrawable(nonExistentFile)
}

@Test
fun errnoMessageAppendedToReadFailed() {
mExpectedException.expect(GifIOException::class.java)

mExpectedException.expectMessage("GifError 102: Failed to read from given input: Is a directory")

GifDrawable(mTemporaryFolder.root)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package pl.droidsonroids.gif
import android.graphics.drawable.Drawable
import org.assertj.core.api.AbstractAssert
import org.assertj.core.api.Assertions
import org.assertj.core.api.Assertions.assertThat
import pl.droidsonroids.gif.GifDrawable

internal class GifDrawableAssert private constructor(actual: GifDrawable) :
AbstractAssert<GifDrawableAssert?, GifDrawable?>(actual, GifDrawableAssert::class.java) {
fun hasLoopCountEqualTo(loopCount: Int): GifDrawableAssert {
Assertions.assertThat(actual!!.loopCount).isEqualTo(loopCount)
assertThat(actual!!.loopCount).isEqualTo(loopCount)
return this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.runner.RunWith
import pl.droidsonroids.gif.test.R

@RunWith(AndroidJUnit4::class)
class GifDrawableExceptionTest {
Expand All @@ -21,7 +22,7 @@ class GifDrawableExceptionTest {
@Before
fun setUp() {
val resources = InstrumentationRegistry.getInstrumentation().context.resources
gifDrawable = GifDrawable(resources, pl.droidsonroids.gif.test.R.raw.test)
gifDrawable = GifDrawable(resources, R.raw.test)
}

@After
Expand All @@ -33,7 +34,8 @@ class GifDrawableExceptionTest {
fun frameIndexOutOfBoundsMessageContainsRange() {
val numberOfFrames = gifDrawable.numberOfFrames
val invalidFrameIndex = numberOfFrames + 10
expectedException.expectMessage(CoreMatchers.containsString(Integer.toString(numberOfFrames)))

expectedException.expectMessage(CoreMatchers.containsString(numberOfFrames.toString()))
expectedException.expect(IndexOutOfBoundsException::class.java)
gifDrawable.getFrameDuration(invalidFrameIndex)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import pl.droidsonroids.gif.GifDrawableAssert.Companion.assertThat
import pl.droidsonroids.gif.test.R

@RunWith(AndroidJUnit4::class)
class GifViewDescriptorTest {
Expand All @@ -17,27 +18,31 @@ class GifViewDescriptorTest {
@Before
fun setUp() {
val context = InstrumentationRegistry.getInstrumentation().context
rootView = LayoutInflater.from(context).inflate(pl.droidsonroids.gif.test.R.layout.attrributes, null, false)
rootView = LayoutInflater.from(context).inflate(R.layout.attrributes, null, false)
}

@Test
fun loopCountSetOnGifImageButton() {
val view = rootView.findViewById<GifImageButton>(pl.droidsonroids.gif.test.R.id.imageButton)
val view = rootView.findViewById<GifImageButton>(R.id.imageButton)

assertThat(view.background).hasLoopCountEqualTo(IMAGE_BUTTON_LOOP_COUNT)
assertThat(view.drawable).hasLoopCountEqualTo(IMAGE_BUTTON_LOOP_COUNT)
}

@Test
fun loopCountSetOnGifImageView() {
val view = rootView.findViewById<GifImageView>(pl.droidsonroids.gif.test.R.id.imageView)
val view = rootView.findViewById<GifImageView>(R.id.imageView)

assertThat(view.background).hasLoopCountEqualTo(IMAGE_VIEW_LOOP_COUNT)
assertThat(view.drawable).hasLoopCountEqualTo(IMAGE_VIEW_LOOP_COUNT)
}

@Test
fun loopCountSetOnGifTextView() {
val view = rootView.findViewById<GifTextView>(pl.droidsonroids.gif.test.R.id.textView)
val view = rootView.findViewById<GifTextView>(R.id.textView)

assertThat(view.background).hasLoopCountEqualTo(TEXT_VIEW_LOOP_COUNT)

for (drawable in view.compoundDrawablesRelative) {
assertThat(drawable).hasLoopCountEqualTo(TEXT_VIEW_LOOP_COUNT)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package pl.droidsonroids.gif

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.assertj.core.api.Assertions
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import pl.droidsonroids.gif.test.R
import java.io.ByteArrayInputStream
import java.io.InputStream

Expand All @@ -14,17 +15,24 @@ class InputStreamTest {
@Test
fun gifDrawableCreatedFromInputStream() {
val assetFileDescriptor = InstrumentationRegistry.getInstrumentation()
.context.resources.openRawResourceFd(pl.droidsonroids.gif.test.R.raw.test)
.context.resources.openRawResourceFd(R.raw.test)

val buffer = ByteArray(assetFileDescriptor.declaredLength.toInt())

val inputStream = assetFileDescriptor.createInputStream()

val bufferedByteCount = inputStream.read(buffer)
inputStream.close()
assetFileDescriptor.close()
Assertions.assertThat(bufferedByteCount).isEqualTo(buffer.size)

assertThat(bufferedByteCount).isEqualTo(buffer.size)

val responseStream: InputStream = ByteArrayInputStream(buffer)

val gifDrawable = GifDrawable(responseStream)
Assertions.assertThat(gifDrawable.error).isEqualTo(GifError.NO_ERROR)
Assertions.assertThat(gifDrawable.intrinsicWidth).isEqualTo(278)
Assertions.assertThat(gifDrawable.intrinsicHeight).isEqualTo(183)

assertThat(gifDrawable.error).isEqualTo(GifError.NO_ERROR)
assertThat(gifDrawable.intrinsicWidth).isEqualTo(278)
assertThat(gifDrawable.intrinsicHeight).isEqualTo(183)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ class GifAnimationMetaData : Serializable, Parcelable {
/**
* See [GifDrawable.getDuration]
*
* @return duration of of one loop the animation in milliseconds. Result is always multiple of 10.
* @return duration of one loop the animation in milliseconds. Result is always multiple of 10.
*/
val duration: Int

/**
* @return height od the GIF canvas in pixels
* @return height of the GIF canvas in pixels
*/
val height: Int

/**
* @return width od the GIF canvas in pixels
* @return width of the GIF canvas in pixels
*/
val width: Int

Expand Down Expand Up @@ -230,8 +230,7 @@ class GifAnimationMetaData : Serializable, Parcelable {
): Long {
check(!(sampleSize < 1 || sampleSize > Character.MAX_VALUE.code)) { "Sample size " + sampleSize + " out of range <1, " + Character.MAX_VALUE + ">" }
val sampleSizeFactor = sampleSize * sampleSize
val bufferSize: Long
bufferSize = if (oldDrawable != null && !oldDrawable.mBuffer.isRecycled) {
val bufferSize: Long = if (oldDrawable != null && !oldDrawable.mBuffer.isRecycled) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
oldDrawable.mBuffer.allocationByteCount.toLong()
} else {
Expand All @@ -244,9 +243,7 @@ class GifAnimationMetaData : Serializable, Parcelable {
}

override fun toString(): String {
val loopCount = if (loopCount == 0) "Infinity" else Integer.toString(
loopCount
)
val loopCount = if (loopCount == 0) "Infinity" else loopCount.toString()
val suffix = String.format(
Locale.ENGLISH,
"GIF: size: %dx%d, frames: %d, loops: %s, duration: %d",
Expand All @@ -273,14 +270,14 @@ class GifAnimationMetaData : Serializable, Parcelable {
dest.writeLong(allocationByteCount)
}

private constructor(`in`: Parcel) {
loopCount = `in`.readInt()
duration = `in`.readInt()
height = `in`.readInt()
width = `in`.readInt()
numberOfFrames = `in`.readInt()
metadataAllocationByteCount = `in`.readLong()
allocationByteCount = `in`.readLong()
private constructor(parcel: Parcel) {
loopCount = parcel.readInt()
duration = parcel.readInt()
height = parcel.readInt()
width = parcel.readInt()
numberOfFrames = parcel.readInt()
metadataAllocationByteCount = parcel.readLong()
allocationByteCount = parcel.readLong()
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,19 @@ import kotlin.jvm.Throws
* GifDecoder allows lightweight access to GIF frames, without wrappers like Drawable or View.
* [Bitmap] with size equal to or greater than size of the GIF is needed.
* For access only metadata (size, number of frames etc.) without pixels see [GifAnimationMetaData].
*
*
* @constructor new GifDecoder
* @param inputSource source
* @param options null-ok; options controlling subsampling and opacity
* @throws IOException when creation fails
*
*/

class GifDecoder @JvmOverloads constructor(inputSource: InputSource, options: GifOptions? = null) {
//TODO extract common container
private val mGifInfoHandle: GifInfoHandle
/**
* Constructs new GifDecoder
*
* @param inputSource source
* @param options null-ok; options controlling subsampling and opacity
* @throws IOException when creation fails
*/
/**
* Constructs new GifDecoder.
* Equivalent of [.GifDecoder] with null `options`
*
* @param inputSource source
* @throws IOException when creation fails
*/

init {
mGifInfoHandle = inputSource.open()
if (options != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package pl.droidsonroids.gif
* by reusing old ones.
*/
class GifDrawableBuilder : GifDrawableInit<GifDrawableBuilder>() {
override fun self(): GifDrawableBuilder {
return this
}

override fun self(): GifDrawableBuilder = this
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import java.util.concurrent.ScheduledThreadPoolExecutor

/**
* The base class for using the builder pattern with subclasses.
*
* @param <T> The type of the builder that is a subclass of this class.
* @see [link](https://community.oracle.com/blogs/emcmanus/2010/10/24/using-builder-pattern-subclasses)
</T> */
abstract class GifDrawableInit<T : GifDrawableInit<T>?> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class GifIOException internal constructor(errorCode: Int, private val mErrnoMess
override val message: String
get() = if (mErrnoMessage == null) {
reason.formattedDescription
} else reason.formattedDescription + ": " + mErrnoMessage
} else "${reason.formattedDescription} : $mErrnoMessage"
koral-- marked this conversation as resolved.
Show resolved Hide resolved

init {
reason = GifError.fromCode(errorCode)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import java.util.concurrent.ScheduledThreadPoolExecutor
abstract class InputSource private constructor() {
@Throws(IOException::class)
abstract fun open(): GifInfoHandle

@Throws(IOException::class)
fun createGifDrawable(
oldDrawable: GifDrawable?, executor: ScheduledThreadPoolExecutor?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ object LibraryLoader {
private const val BASE_LIBRARY_NAME = "pl_droidsonroids_gif"

@SuppressLint("StaticFieldLeak") //workaround for Android bug

private var sAppContext: Context? = null

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.SystemClock
import java.util.concurrent.TimeUnit

internal class RenderTask(gifDrawable: GifDrawable) : SafeRunnable(gifDrawable) {

override fun doWork() {
val invalidationDelay = mGifDrawable.mNativeInfoHandle.renderFrame(mGifDrawable.mBuffer)
if (invalidationDelay >= 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package pl.droidsonroids.gif

import android.graphics.drawable.Drawable
import org.assertj.core.api.Java6Assertions
import org.assertj.core.api.Java6Assertions.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
Expand All @@ -20,35 +20,44 @@ class CallbackWeakReferenceTest {
@Test
fun testEquals() {
val reference = CallbackWeakReference(callback)

val anotherReference = CallbackWeakReference(callback)
Java6Assertions.assertThat(reference).isEqualTo(anotherReference)

assertThat(reference).isEqualTo(anotherReference)
}

@Test
fun testNotEqualReferences() {
val reference = CallbackWeakReference(callback)

val anotherReference = CallbackWeakReference(anotherCallback)
Java6Assertions.assertThat(reference).isNotEqualTo(anotherReference)

assertThat(reference).isNotEqualTo(anotherReference)
}

@Test
fun testNotEqualDifferentObjects() {
val reference = CallbackWeakReference(callback)
Java6Assertions.assertThat(reference).isNotEqualTo(null)
Java6Assertions.assertThat(reference).isNotEqualTo(callback)

assertThat(reference).isNotEqualTo(null)
assertThat(reference).isNotEqualTo(callback)
}

@Test
fun testHashCode() {
val reference = CallbackWeakReference(callback)

val anotherReference = CallbackWeakReference(callback)
Java6Assertions.assertThat(reference.hashCode()).isEqualTo(anotherReference.hashCode())

assertThat(reference.hashCode()).isEqualTo(anotherReference.hashCode())
}

@Test
fun testHashCodeNull() {
val reference = CallbackWeakReference(callback)

val anotherReference = CallbackWeakReference(null)
Java6Assertions.assertThat(reference.hashCode()).isNotEqualTo(anotherReference.hashCode())

assertThat(reference.hashCode()).isNotEqualTo(anotherReference.hashCode())
}
}
Loading