/
LottieSnapshotTest.kt
155 lines (142 loc) · 6.23 KB
/
LottieSnapshotTest.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
package com.airbnb.lottie.snapshots
import android.Manifest
import android.content.ComponentCallbacks2
import android.content.Context
import android.content.res.Configuration
import android.util.Log
import android.widget.FrameLayout
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.LargeTest
import androidx.test.rule.GrantPermissionRule
import com.airbnb.lottie.LottieAnimationView
import com.airbnb.lottie.model.LottieCompositionCache
import com.airbnb.lottie.snapshots.tests.ApplyOpacityToLayerTestCase
import com.airbnb.lottie.snapshots.tests.AssetsTestCase
import com.airbnb.lottie.snapshots.tests.ClipChildrenTestCase
import com.airbnb.lottie.snapshots.tests.ColorStateListColorFilterTestCase
import com.airbnb.lottie.snapshots.tests.ComposeDynamicPropertiesTestCase
import com.airbnb.lottie.snapshots.tests.ComposeScaleTypesTestCase
import com.airbnb.lottie.snapshots.tests.CustomBoundsTestCase
import com.airbnb.lottie.snapshots.tests.DynamicPropertiesTestCase
import com.airbnb.lottie.snapshots.tests.FailureTestCase
import com.airbnb.lottie.snapshots.tests.FrameBoundariesTestCase
import com.airbnb.lottie.snapshots.tests.LargeCompositionSoftwareRendering
import com.airbnb.lottie.snapshots.tests.MarkersTestCase
import com.airbnb.lottie.snapshots.tests.NightModeTestCase
import com.airbnb.lottie.snapshots.tests.OutlineMasksAndMattesTestCase
import com.airbnb.lottie.snapshots.tests.PartialFrameProgressTestCase
import com.airbnb.lottie.snapshots.tests.ProdAnimationsTestCase
import com.airbnb.lottie.snapshots.tests.ScaleTypesTestCase
import com.airbnb.lottie.snapshots.tests.SoftwareRenderingDynamicPropertiesInvalidationTestCase
import com.airbnb.lottie.snapshots.tests.TextTestCase
import com.airbnb.lottie.snapshots.utils.BitmapPool
import com.airbnb.lottie.snapshots.utils.HappoSnapshotter
import com.airbnb.lottie.snapshots.utils.ObjectPool
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import java.util.concurrent.TimeUnit
@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
@LargeTest
class LottieSnapshotTest {
@get:Rule
val snapshotActivityRule = ActivityScenarioRule(SnapshotTestActivity::class.java)
@get:Rule
val permissionRule: GrantPermissionRule = GrantPermissionRule.grant(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
)
lateinit var testCaseContext: SnapshotTestCaseContext
lateinit var snapshotter: HappoSnapshotter
@Before
fun setup() {
LottieCompositionCache.getInstance().resize(1)
val context = ApplicationProvider.getApplicationContext<Context>()
snapshotter = HappoSnapshotter(context) { name, variant ->
snapshotActivityRule.scenario.onActivity { activity ->
activity.updateUiForSnapshot(name, variant)
}
}
testCaseContext = object : SnapshotTestCaseContext {
override val context: Context = context
override val snapshotter: HappoSnapshotter = this@LottieSnapshotTest.snapshotter
override val bitmapPool: BitmapPool = BitmapPool()
override val animationViewPool: ObjectPool<LottieAnimationView> = ObjectPool {
val animationViewContainer = FrameLayout(context)
NoCacheLottieAnimationView(context).apply {
animationViewContainer.addView(this)
}
}
override val filmStripViewPool: ObjectPool<FilmStripView> = ObjectPool {
FilmStripView(context)
}
override fun onActivity(callback: (SnapshotTestActivity) -> Unit) {
snapshotActivityRule.scenario.onActivity(callback)
}
}
snapshotActivityRule.scenario.onActivity { activity ->
activity.registerComponentCallbacks(object : ComponentCallbacks2 {
override fun onConfigurationChanged(newConfig: Configuration) {}
override fun onLowMemory() {
testCaseContext.bitmapPool.clear()
}
override fun onTrimMemory(level: Int) {
testCaseContext.bitmapPool.clear()
}
})
}
}
@Test
fun testAll() = runBlocking {
val testCases = listOf(
CustomBoundsTestCase(),
ColorStateListColorFilterTestCase(),
FailureTestCase(),
FrameBoundariesTestCase(),
ScaleTypesTestCase(),
ComposeScaleTypesTestCase(),
DynamicPropertiesTestCase(),
MarkersTestCase(),
AssetsTestCase(),
TextTestCase(),
PartialFrameProgressTestCase(),
NightModeTestCase(),
ApplyOpacityToLayerTestCase(),
OutlineMasksAndMattesTestCase(),
LargeCompositionSoftwareRendering(),
ComposeDynamicPropertiesTestCase(),
ProdAnimationsTestCase(),
ClipChildrenTestCase(),
SoftwareRenderingDynamicPropertiesInvalidationTestCase(),
)
withTimeout(TimeUnit.MINUTES.toMillis(45)) {
launch {
with(testCases.filterIsInstance<ProdAnimationsTestCase>().firstOrNull() ?: return@launch) {
// Kick off the downloads ahead of time so it can start while the other tests are snapshotting
testCaseContext.downloadAnimations()
}
}
for (testCase in testCases) {
Log.d(TAG, "Running test case ${testCase::class.java}")
with(testCase) {
testCaseContext.bitmapPool.clear()
LottieCompositionCache.getInstance().clear()
testCaseContext.run()
}
Log.d(TAG, "Finished running test case ${testCase::class.java}")
}
snapshotter.finalizeReportAndUpload()
}
}
companion object {
private const val TAG = "LottieSnapshotTest"
}
}