Skip to content

Commit

Permalink
Fix more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrtwhite committed Mar 29, 2020
1 parent 351749b commit d65863e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
4 changes: 2 additions & 2 deletions coil-base/src/main/java/coil/decode/BitmapFactoryDecoder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ internal class BitmapFactoryDecoder(private val context: Context) : Decoder {
}
}

/** Wrap [delegate] so that it always returns [Int.MAX_VALUE] for [available]. */
/** Wrap [delegate] so that it always returns 1GB for [available]. */
private class AlwaysAvailableInputStream(private val delegate: InputStream) : InputStream() {

override fun read() = delegate.read()
Expand All @@ -243,7 +243,7 @@ internal class BitmapFactoryDecoder(private val context: Context) : Decoder {

override fun skip(n: Long) = delegate.skip(n)

override fun available() = Int.MAX_VALUE
override fun available() = 1024 * 1024 * 1024 // 1GB

override fun close() = delegate.close()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package coil.fetch
import android.content.ContentResolver.SCHEME_FILE
import android.content.Context
import android.graphics.drawable.BitmapDrawable
import android.os.Build.VERSION.SDK_INT
import androidx.core.net.toUri
import androidx.test.core.app.ApplicationProvider
import coil.bitmappool.BitmapPool
Expand All @@ -14,6 +15,7 @@ import coil.util.createOptions
import coil.util.decodeBitmapAsset
import coil.util.isSimilarTo
import kotlinx.coroutines.runBlocking
import org.junit.Assume.assumeTrue
import org.junit.Before
import org.junit.Test
import kotlin.test.assertFalse
Expand All @@ -35,6 +37,9 @@ class VideoFrameFetcherTest {

@Test
fun noSetFrameTime() {
// MediaMetadataRetriever.getFrameAtTime does not work on the emulator pre-API 23.
assumeTrue(SDK_INT >= 23)

val result = runBlocking {
fetcher.fetch(
pool = pool,
Expand All @@ -55,6 +60,9 @@ class VideoFrameFetcherTest {

@Test
fun specificFrameTime() {
// MediaMetadataRetriever.getFrameAtTime does not work on the emulator pre-API 23.
assumeTrue(SDK_INT >= 23)

val result = runBlocking {
fetcher.fetch(
pool = pool,
Expand Down
11 changes: 7 additions & 4 deletions coil-video/src/main/java/coil/fetch/VideoFrameFetcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ abstract class VideoFrameFetcher<T : Any>(private val context: Context) : Fetche
val option = options.parameters.videoFrameOption() ?: OPTION_CLOSEST_SYNC
val frameMicros = options.parameters.videoFrameMicros() ?: 0L

// Resolve the dimensions to decode the video frame at
// accounting for the source's aspect ratio and the target's size.
// Resolve the dimensions to decode the video frame at accounting
// for the source's aspect ratio and the target's size.
var srcWidth = 0
var srcHeight = 0
val destSize = when (size) {
Expand Down Expand Up @@ -132,14 +132,17 @@ abstract class VideoFrameFetcher<T : Any>(private val context: Context) : Fetche
is OriginalSize -> OriginalSize
}

val rawBitmap = if (SDK_INT >= 27 && destSize is PixelSize) {
val rawBitmap: Bitmap? = if (SDK_INT >= 27 && destSize is PixelSize) {
retriever.getScaledFrameAtTime(frameMicros, option, destSize.width, destSize.height)
} else {
retriever.getFrameAtTime(frameMicros, option).also {
retriever.getFrameAtTime(frameMicros, option)?.also {
srcWidth = it.width
srcHeight = it.height
}
}

// If you encounter this exception, ensure your video is encoded in a supported codec.
// https://developer.android.com/guide/topics/media/media-formats#video-formats
checkNotNull(rawBitmap) { "Failed to decode frame at $frameMicros microseconds." }

val bitmap = normalizeBitmap(pool, rawBitmap, destSize, options)
Expand Down

0 comments on commit d65863e

Please sign in to comment.