-
Notifications
You must be signed in to change notification settings - Fork 637
/
VideoFrameDecoderTest.kt
152 lines (128 loc) · 4.8 KB
/
VideoFrameDecoderTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package coil.decode
import android.content.Context
import android.graphics.drawable.BitmapDrawable
import android.os.Build.VERSION.SDK_INT
import androidx.test.core.app.ApplicationProvider
import coil.decode.VideoFrameDecoder.Companion.VIDEO_FRAME_MICROS_KEY
import coil.decode.VideoFrameDecoder.Companion.VIDEO_FRAME_PERCENT_KEY
import coil.request.Options
import coil.request.Parameters
import coil.size.Size
import coil.util.assertIsSimilarTo
import coil.util.assumeTrue
import coil.util.decodeBitmapAsset
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import okio.buffer
import okio.source
import org.junit.Before
import org.junit.Test
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
@OptIn(ExperimentalCoroutinesApi::class)
class VideoFrameDecoderTest {
private lateinit var context: Context
@Before
fun before() {
context = ApplicationProvider.getApplicationContext()
}
@Test
fun noSetFrameTime() = runTest {
// MediaMetadataRetriever does not work on the emulator pre-API 23.
assumeTrue(SDK_INT >= 23)
val result = VideoFrameDecoder(
source = ImageSource(
source = context.assets.open("video.mp4").source().buffer(),
context = context
),
options = Options(context)
).decode()
val actual = (result.drawable as? BitmapDrawable)?.bitmap
assertNotNull(actual)
assertFalse(result.isSampled)
val expected = context.decodeBitmapAsset("video_frame_1.jpg")
actual.assertIsSimilarTo(expected)
}
@Test
fun specificFrameTime() = runTest {
// MediaMetadataRetriever does not work on the emulator pre-API 23.
assumeTrue(SDK_INT >= 23)
val result = VideoFrameDecoder(
source = ImageSource(
source = context.assets.open("video.mp4").source().buffer(),
context = context
),
options = Options(
context = context,
parameters = Parameters.Builder()
.set(VIDEO_FRAME_MICROS_KEY, 32600000L)
.build()
)
).decode()
val actual = (result.drawable as? BitmapDrawable)?.bitmap
assertNotNull(actual)
assertFalse(result.isSampled)
val expected = context.decodeBitmapAsset("video_frame_2.jpg")
actual.assertIsSimilarTo(expected)
}
@Test
fun specificFramePercent() = runTest {
// MediaMetadataRetriever does not work on the emulator pre-API 23.
assumeTrue(SDK_INT >= 23)
val result = VideoFrameDecoder(
source = ImageSource(
source = context.assets.open("video.mp4").source().buffer(),
context = context
),
options = Options(
context = context,
parameters = Parameters.Builder()
.set(VIDEO_FRAME_PERCENT_KEY, 0.525)
.build()
)
).decode()
val actual = (result.drawable as? BitmapDrawable)?.bitmap
assertNotNull(actual)
assertFalse(result.isSampled)
val expected = context.decodeBitmapAsset("video_frame_2.jpg")
actual.assertIsSimilarTo(expected)
}
@Test
fun rotation() = runTest {
// MediaMetadataRetriever does not work on the emulator pre-API 23.
assumeTrue(SDK_INT >= 23)
val result = VideoFrameDecoder(
source = ImageSource(
source = context.assets.open("video_rotated.mp4").source().buffer(),
context = context
),
options = Options(context, size = Size(150, 150))
).decode()
val actual = (result.drawable as? BitmapDrawable)?.bitmap
assertNotNull(actual)
assertTrue(result.isSampled)
val expected = context.decodeBitmapAsset("video_frame_rotated.jpg")
actual.assertIsSimilarTo(expected, threshold = 0.97)
}
/** Regression test: https://github.com/coil-kt/coil/issues/1482 */
@Test
fun nestedAsset() = runTest {
// MediaMetadataRetriever does not work on the emulator pre-API 23.
assumeTrue(SDK_INT >= 23)
val path = "nested/video.mp4"
val result = VideoFrameDecoder(
source = ImageSource(
source = context.assets.open(path).source().buffer(),
context = context,
metadata = AssetMetadata(path)
),
options = Options(context)
).decode()
val actual = (result.drawable as? BitmapDrawable)?.bitmap
assertNotNull(actual)
assertFalse(result.isSampled)
val expected = context.decodeBitmapAsset("video_frame_1.jpg")
actual.assertIsSimilarTo(expected)
}
}