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

Commit 4a10fd11 authored by Luca Zuccarini's avatar Luca Zuccarini Committed by Android (Google) Code Review
Browse files

Merge "Add filmstrip matching to AnimatorTestRuleToolkit." into main

parents 2403e770 22cd0857
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -77,7 +77,10 @@ class TransitionAnimatorTest(val useSpring: Boolean) : SysuiTestCase() {
    @get:Rule(order = 2) val animatorTestRule = android.animation.AnimatorTestRule(this)
    @get:Rule(order = 3)
    val motionRule =
        MotionTestRule(AnimatorTestRuleToolkit(animatorTestRule, kosmos.testScope), pathManager)
        MotionTestRule(
            AnimatorTestRuleToolkit(animatorTestRule, kosmos.testScope) { activityRule.scenario },
            pathManager,
        )

    @Test
    fun backgroundAnimation_whenLaunching() {
+66 −10
Original line number Diff line number Diff line
@@ -17,7 +17,13 @@
package android.animation

import android.animation.AnimatorTestRuleToolkit.Companion.TAG
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.util.Log
import android.view.View
import androidx.core.graphics.drawable.toBitmap
import androidx.test.core.app.ActivityScenario
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
@@ -36,13 +42,45 @@ import platform.test.motion.golden.FrameId
import platform.test.motion.golden.TimeSeries
import platform.test.motion.golden.TimeSeriesCaptureScope
import platform.test.motion.golden.TimestampFrameId
import platform.test.screenshot.captureToBitmapAsync

class AnimatorTestRuleToolkit(val animatorTestRule: AnimatorTestRule, val testScope: TestScope) {
class AnimatorTestRuleToolkit(
    internal val animatorTestRule: AnimatorTestRule,
    internal val testScope: TestScope,
    internal val currentActivityScenario: () -> ActivityScenario<*>,
) {
    internal companion object {
        const val TAG = "AnimatorRuleToolkit"
    }
}

/** Capture utility to extract a [Bitmap] from a [drawable]. */
fun captureDrawable(drawable: Drawable): Bitmap {
    val width = drawable.bounds.right - drawable.bounds.left
    val height = drawable.bounds.bottom - drawable.bounds.top

    // If either dimension is 0 this will fail, so we set it to 1 pixel instead.
    return drawable.toBitmap(
        width =
        if (width > 0) {
            width
        } else {
            1
        },
        height =
        if (height > 0) {
            height
        } else {
            1
        },
    )
}

/** Capture utility to extract a [Bitmap] from a [view]. */
fun captureView(view: View): Bitmap {
    return view.captureToBitmapAsync().get(10, TimeUnit.SECONDS)
}

/**
 * Controls the timing of the motion recording.
 *
@@ -71,26 +109,41 @@ data class AnimatorRuleRecordingSpec<T>(
    /** Time interval between frame captures, in milliseconds. */
    val frameDurationMs: Long = 16L,

    /** Whether a sequence of screenshots should also be recorded. */
    val visualCapture: ((captureRoot: T) -> Bitmap)? = null,

    /** Produces the time-series, invoked on each animation frame. */
    val timeSeriesCapture: TimeSeriesCaptureScope<T>.() -> Unit,
)

/** Records the time-series of the features specified in [recordingSpec]. */
fun <T> MotionTestRule<AnimatorTestRuleToolkit>.recordMotion(
    recordingSpec: AnimatorRuleRecordingSpec<T>,
    recordingSpec: AnimatorRuleRecordingSpec<T>
): RecordedMotion {
    with(toolkit.animatorTestRule) {
        val activityScenario = toolkit.currentActivityScenario()
        val frameIdCollector = mutableListOf<FrameId>()
        val propertyCollector = mutableMapOf<String, MutableList<DataPoint<*>>>()
        val screenshotCollector =
            if (recordingSpec.visualCapture != null) {
                mutableListOf<Bitmap>()
            } else {
                null
            }

        fun recordFrame(frameId: FrameId) {
            Log.i(TAG, "recordFrame($frameId)")
            frameIdCollector.add(frameId)
            activityScenario.onActivity {
                recordingSpec.timeSeriesCapture.invoke(
                    TimeSeriesCaptureScope(recordingSpec.captureRoot, propertyCollector)
                )
            }

            val bitmap = recordingSpec.visualCapture?.invoke(recordingSpec.captureRoot)
            if (bitmap != null) screenshotCollector!!.add(bitmap)
        }

        val motionControl =
            MotionControlImpl(
                toolkit.animatorTestRule,
@@ -101,10 +154,13 @@ fun <T> MotionTestRule<AnimatorTestRuleToolkit>.recordMotion(

        Log.i(TAG, "recordMotion() begin recording")

        val startFrameTime = currentTime
        var startFrameTime: Long? = null
        toolkit.currentActivityScenario().onActivity { startFrameTime = currentTime }
        while (!motionControl.recordingEnded) {
            recordFrame(TimestampFrameId(currentTime - startFrameTime))
            motionControl.nextFrame()
            var time: Long? = null
            toolkit.currentActivityScenario().onActivity { time = currentTime }
            recordFrame(TimestampFrameId(time!! - startFrameTime!!))
            toolkit.currentActivityScenario().onActivity { motionControl.nextFrame() }
        }

        Log.i(TAG, "recordMotion() end recording")
@@ -115,7 +171,7 @@ fun <T> MotionTestRule<AnimatorTestRuleToolkit>.recordMotion(
                propertyCollector.entries.map { entry -> Feature(entry.key, entry.value) },
            )

        return create(timeSeries, null)
        return create(timeSeries, screenshotCollector)
    }
}

+3 −1
Original line number Diff line number Diff line
@@ -37,10 +37,11 @@ android_test {
        "androidx.core_core-ktx",
        "androidx.test.ext.junit",
        "androidx.test.rules",
        "androidx.test.ext.junit",
        "hamcrest-library",
        "kotlinx_coroutines_test",
        "mockito-target-inline-minus-junit4",
        "platform-screenshot-diff-core",
        "platform-test-annotations",
        "testables",
        "truth",
    ],
@@ -55,6 +56,7 @@ android_test {
        "android.test.mock.stubs.system",
    ],
    certificate: "platform",
    test_config: "AndroidTest.xml",
    test_suites: [
        "device-tests",
        "automotive-tests",
+4 −0
Original line number Diff line number Diff line
@@ -23,6 +23,10 @@

    <application android:debuggable="true">
        <uses-library android:name="android.test.runner" />
        <activity
            android:name="platform.test.screenshot.ScreenshotActivity"
            android:exported="true">
        </activity>
    </application>

    <instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
+51 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!--
     Copyright (C) 2022 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<configuration description="Runs Tests for Testables.">
    <target_preparer class="com.android.tradefed.targetprep.TestAppInstallSetup">
        <option name="test-file-name" value="TestablesTests.apk" />
        <option name="install-arg" value="-t" />
    </target_preparer>

    <target_preparer class="com.android.tradefed.targetprep.RootTargetPreparer">
        <option name="force-root" value="true" />
    </target_preparer>

    <target_preparer class="com.android.tradefed.targetprep.DeviceSetup">
        <option name="screen-always-on" value="on" />
    </target_preparer>

    <target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer">
        <option name="run-command" value="input keyevent KEYCODE_WAKEUP" />
        <option name="run-command" value="wm dismiss-keyguard" />
    </target_preparer>

    <option name="test-suite-tag" value="apct" />
    <option name="test-suite-tag" value="framework-base-presubmit" />
    <option name="test-tag" value="TestableTests" />
    <test class="com.android.tradefed.testtype.AndroidJUnitTest" >
        <option name="package" value="com.android.testables" />
        <option name="runner" value="androidx.test.runner.AndroidJUnitRunner" />
        <option name="test-filter-dir" value="/data/data/com.android.testables" />
        <option name="hidden-api-checks" value="false"/>
    </test>

    <metrics_collector class="com.android.tradefed.device.metric.FilePullerLogCollector">
        <option name="directory-keys" value="/data/user/0/com.android.testables/files"/>
        <option name="collect-on-run-ended-only" value="true"/>
        <option name="clean-up" value="true"/>
    </metrics_collector>
</configuration>
Loading