/
SnapshotTestCaseContext.kt
203 lines (194 loc) · 8.14 KB
/
SnapshotTestCaseContext.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package com.airbnb.lottie.snapshots
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.PorterDuff
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.ui.platform.ComposeView
import com.airbnb.lottie.FontAssetDelegate
import com.airbnb.lottie.LottieAnimationView
import com.airbnb.lottie.LottieComposition
import com.airbnb.lottie.LottieCompositionFactory
import com.airbnb.lottie.LottieDrawable
import com.airbnb.lottie.model.LottieCompositionCache
import com.airbnb.lottie.snapshots.utils.BitmapPool
import com.airbnb.lottie.snapshots.utils.HappoSnapshotter
import com.airbnb.lottie.snapshots.utils.ObjectPool
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
import kotlin.coroutines.resume
/**
* Set of properties that are available to all [SnapshotTestCase] runs.
*/
interface SnapshotTestCaseContext {
val context: Context
val snapshotter: HappoSnapshotter
val bitmapPool: BitmapPool
val animationViewPool: ObjectPool<LottieAnimationView>
val filmStripViewPool: ObjectPool<FilmStripView>
fun onActivity(callback: (SnapshotTestActivity) -> Unit)
}
@Suppress("unused")
fun SnapshotTestCaseContext.log(message: String) {
Log.d("LottieTestCase", message)
}
suspend fun SnapshotTestCaseContext.withDrawable(
assetName: String,
snapshotName: String,
snapshotVariant: String,
callback: (LottieDrawable) -> Unit,
) {
val result = LottieCompositionFactory.fromAssetSync(context, assetName)
val composition = result.value ?: throw IllegalArgumentException("Unable to parse $assetName.", result.exception)
val drawable = LottieDrawable()
drawable.composition = composition
callback(drawable)
val bitmap = bitmapPool.acquire(drawable.intrinsicWidth, drawable.intrinsicHeight)
val canvas = Canvas(bitmap)
log("Drawing $assetName")
drawable.draw(canvas)
snapshotter.record(bitmap, snapshotName, snapshotVariant)
LottieCompositionCache.getInstance().clear()
bitmapPool.release(bitmap)
}
suspend fun SnapshotTestCaseContext.withAnimationView(
assetName: String,
snapshotName: String = assetName,
snapshotVariant: String = "default",
widthPx: Int = context.resources.displayMetrics.widthPixels,
heightPx: Int = context.resources.displayMetrics.heightPixels,
callback: (LottieAnimationView) -> Unit,
) {
val result = LottieCompositionFactory.fromAssetSync(context, assetName)
val composition = result.value ?: throw IllegalArgumentException("Unable to parse $assetName.", result.exception)
val animationView = animationViewPool.acquire()
animationView.setComposition(composition)
animationView.layoutParams = FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)
animationView.scale = 1f
animationView.scaleType = ImageView.ScaleType.FIT_CENTER
callback(animationView)
val animationViewContainer = animationView.parent as ViewGroup
val widthSpec = View.MeasureSpec.makeMeasureSpec(
widthPx,
View.MeasureSpec.EXACTLY,
)
val heightSpec: Int = View.MeasureSpec.makeMeasureSpec(
heightPx,
View.MeasureSpec.EXACTLY,
)
animationViewContainer.measure(widthSpec, heightSpec)
animationViewContainer.layout(0, 0, animationViewContainer.measuredWidth, animationViewContainer.measuredHeight)
val bitmap = bitmapPool.acquire(animationView.width, animationView.height)
val canvas = Canvas(bitmap)
log("Drawing $assetName")
animationView.draw(canvas)
animationViewPool.release(animationView)
snapshotter.record(bitmap, snapshotName, snapshotVariant)
bitmapPool.release(bitmap)
}
suspend fun SnapshotTestCaseContext.withFilmStripView(
assetName: String,
snapshotName: String = assetName,
snapshotVariant: String = "default",
callback: (FilmStripView) -> Unit,
) {
val result = LottieCompositionFactory.fromAssetSync(context, assetName)
val composition = result.value ?: throw IllegalArgumentException("Unable to parse $assetName.", result.exception)
snapshotComposition(snapshotName, snapshotVariant, composition, callback)
}
suspend fun SnapshotTestCaseContext.snapshotComposition(
name: String,
variant: String = "default",
composition: LottieComposition,
callback: ((FilmStripView) -> Unit)? = null,
) = withContext(Dispatchers.Default) {
log("Snapshotting $name")
val spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
val filmStripView = filmStripViewPool.acquire()
filmStripView.setOutlineMasksAndMattes(false)
filmStripView.setApplyingOpacityToLayersEnabled(false)
filmStripView.setImageAssetDelegate { BitmapFactory.decodeResource(context.resources, R.drawable.airbnb) }
filmStripView.setFontAssetDelegate(object : FontAssetDelegate() {
override fun getFontPath(fontFamily: String?): String {
return "fonts/Roboto.ttf"
}
})
callback?.invoke(filmStripView)
filmStripView.measure(spec, spec)
filmStripView.layout(0, 0, filmStripView.measuredWidth, filmStripView.measuredHeight)
val bitmap = bitmapPool.acquire(filmStripView.width, filmStripView.height)
val canvas = Canvas(bitmap)
filmStripView.setComposition(composition, name)
canvas.drawColor(Color.BLACK, PorterDuff.Mode.CLEAR)
withContext(Dispatchers.Main) {
log("Drawing $name")
filmStripView.draw(canvas)
}
filmStripViewPool.release(filmStripView)
LottieCompositionCache.getInstance().clear()
snapshotter.record(bitmap, name, variant)
bitmapPool.release(bitmap)
}
/**
* Use this to signal that the composition is not ready to be snapshot yet.
* This use useful if you are using things like `rememberLottieComposition` which parses a composition asynchronously.
*/
val LocalSnapshotReady = compositionLocalOf { MutableStateFlow<Boolean?>(true) }
suspend fun SnapshotTestCaseContext.snapshotComposable(
name: String,
variant: String = "default",
content: @Composable () -> Unit,
) = withContext(Dispatchers.Default) {
log("Snapshotting $name")
val composeView = ComposeView(context)
composeView.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
val bitmap = withContext(Dispatchers.Main) {
val readyFlow = MutableStateFlow<Boolean?>(null)
composeView.setContent {
CompositionLocalProvider(LocalSnapshotReady provides readyFlow) {
content()
}
if (readyFlow.value == null) readyFlow.value = true
}
lateinit var onDrawListener: ViewTreeObserver.OnDrawListener
suspendCancellableCoroutine<Bitmap> { cont ->
onDrawListener = ViewTreeObserver.OnDrawListener {
composeView.post {
if (readyFlow.value != true) {
return@post
}
log("Drawing $name")
val bitmap = bitmapPool.acquire(composeView.width, composeView.height)
val canvas = Canvas(bitmap)
composeView.draw(canvas)
cont.resume(bitmap)
}
}
composeView.viewTreeObserver.addOnDrawListener(onDrawListener)
onActivity { activity ->
activity.binding.content.addView(composeView)
}
}.also {
composeView.viewTreeObserver.removeOnDrawListener(onDrawListener)
}
}
onActivity { activity ->
activity.binding.content.removeView(composeView)
}
LottieCompositionCache.getInstance().clear()
snapshotter.record(bitmap, name, variant)
bitmapPool.release(bitmap)
}