Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 7947810e authored by Mike Schneider's avatar Mike Schneider
Browse files

Testing utilities for verifying timeseries in code

Bug: 401500734
Test: Unit tests
Flag: TEST_ONLY
Change-Id: I623f9d413d671e184dbcd0e49832b763cbac9f97
parent bddb5752
Loading
Loading
Loading
Loading
+14 −4
Original line number Diff line number Diff line
@@ -148,7 +148,7 @@ enum class VerifyTimeSeriesResult {
/** A semantic value to capture in the golden. */
class CapturedSemantics<T>(
    val key: SemanticKey<T>,
    val dataPointType: DataPointType<T>,
    val dataPointType: DataPointType<T & Any>,
    val name: String = key.debugLabel,
) {
    fun toDataPoint(frameData: FrameData): DataPoint<T> {
@@ -213,9 +213,19 @@ sealed class MotionValueToolkit<MotionValueType, GestureContextType> {
        verificationFn: TimeSeries.() -> VerifyTimeSeriesResult,
    ) {
        val recordedMotion = motionTestRule.create(timeSeries, screenshots = null)
        val skipGoldenVerification = verificationFn.invoke(recordedMotion.timeSeries)
        if (skipGoldenVerification == VerifyTimeSeriesResult.AssertTimeSeriesMatchesGolden) {
        var assertTimeseriesMatchesGolden = false
        try {
            assertTimeseriesMatchesGolden =
                verificationFn.invoke(recordedMotion.timeSeries) ==
                    VerifyTimeSeriesResult.AssertTimeSeriesMatchesGolden
        } finally {
            try {
                motionTestRule.assertThat(recordedMotion).timeSeriesMatchesGolden()
            } catch (e: AssertionError) {
                if (assertTimeseriesMatchesGolden) {
                    throw e
                }
            }
        }
    }

+19 −5
Original line number Diff line number Diff line
@@ -32,10 +32,24 @@ val TimeSeries.outputTarget: List<Float>
val TimeSeries.isStable: List<Boolean>
    get() = dataPoints("isStable")

fun <T> TimeSeries.dataPoints(featureName: String): List<T> {
    @Suppress("UNCHECKED_CAST")
    return (features[featureName] as Feature<T>).dataPoints.map {
        require(it is ValueDataPoint)
        it.value
/**
 * Returns data points for the given [featureName].
 *
 * Throws a [ClassCastException] if any data point is not a [ValueDataPoint] of type [T].
 */
inline fun <reified T : Any> TimeSeries.dataPoints(featureName: String): List<T> {
    return (features[featureName] as Feature<*>).dataPoints.map {
        (it as ValueDataPoint).value as T
    }
}

/**
 * Returns data points for the given [featureName].
 *
 * Returns `null` for all data points that are not a [ValueDataPoint] of type [T].
 */
inline fun <reified T : Any> TimeSeries.nullableDataPoints(featureName: String): List<T?> {
    return (features[featureName] as Feature<*>).dataPoints.map {
        (it as? ValueDataPoint)?.value as T?
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -477,7 +477,7 @@ class MotionValueTest : MotionBuilderContext by FakeMotionSpecBuilderContext.Def
                    .containsExactlyElementsIn(dataPoints<Float>("derived-output"))
                    .inOrder()
                // and its never animated.
                assertThat(dataPoints<Float>("derived-isStable")).doesNotContain(false)
                assertThat(dataPoints<Boolean>("derived-isStable")).doesNotContain(false)

                AssertTimeSeriesMatchesGolden
            },