From 62f24f63ade43a0d11e6018fac3d5657e6fc9314 Mon Sep 17 00:00:00 2001 From: vadimt Date: Wed, 14 Jun 2023 14:08:57 -0700 Subject: [PATCH] Moving ViewCaptureRule to inside of FailureWatcher Goal: ViewCaptureRule finishes recording before FailureWatcher starts saving artifacts, including view capture. Also starting recoding without waiting for activity creation if there is already a Launcher activity. Test: local, presubmit Flag: N/A Bug: 286251603 Change-Id: I191d3cdde76e9f906453b20325862f2bcff9024c --- .../quickstep/FallbackRecentsTest.java | 5 ++-- .../launcher3/ui/AbstractLauncherUiTest.java | 4 +-- .../launcher3/util/rule/ViewCaptureRule.kt | 27 ++++++++++++++----- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/quickstep/tests/src/com/android/quickstep/FallbackRecentsTest.java b/quickstep/tests/src/com/android/quickstep/FallbackRecentsTest.java index 083b4f8c19..3edda6ba5b 100644 --- a/quickstep/tests/src/com/android/quickstep/FallbackRecentsTest.java +++ b/quickstep/tests/src/com/android/quickstep/FallbackRecentsTest.java @@ -117,12 +117,13 @@ public class FallbackRecentsTest { Utilities.enableRunningInTestHarnessForTests(); } - final ViewCaptureRule viewCaptureRule = new ViewCaptureRule(); + final ViewCaptureRule viewCaptureRule = new ViewCaptureRule( + RecentsActivity.ACTIVITY_TRACKER::getCreatedActivity); mOrderSensitiveRules = RuleChain .outerRule(new SamplerRule()) .around(new NavigationModeSwitchRule(mLauncher)) - .around(viewCaptureRule) .around(new FailureWatcher(mDevice, mLauncher, viewCaptureRule.getViewCapture())) + .around(viewCaptureRule) .around(new ViewCaptureAnalysisRule(viewCaptureRule.getViewCapture())); mOtherLauncherActivity = context.getPackageManager().queryIntentActivities( diff --git a/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java b/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java index 60f1418314..68e586c5af 100644 --- a/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java +++ b/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java @@ -204,11 +204,11 @@ public abstract class AbstractLauncherUiTest { } protected TestRule getRulesInsideActivityMonitor() { - final ViewCaptureRule viewCaptureRule = new ViewCaptureRule(); + final ViewCaptureRule viewCaptureRule = new ViewCaptureRule(mActivityMonitor::getActivity); final RuleChain inner = RuleChain .outerRule(new PortraitLandscapeRunner(this)) - .around(viewCaptureRule) .around(new FailureWatcher(mDevice, mLauncher, viewCaptureRule.getViewCapture())) + .around(viewCaptureRule) .around(new ViewCaptureAnalysisRule(viewCaptureRule.getViewCapture())); return TestHelpers.isInLauncherProcess() diff --git a/tests/src/com/android/launcher3/util/rule/ViewCaptureRule.kt b/tests/src/com/android/launcher3/util/rule/ViewCaptureRule.kt index 0c6553998d..ca3147d811 100644 --- a/tests/src/com/android/launcher3/util/rule/ViewCaptureRule.kt +++ b/tests/src/com/android/launcher3/util/rule/ViewCaptureRule.kt @@ -23,6 +23,7 @@ import androidx.test.core.app.ApplicationProvider import com.android.app.viewcapture.SimpleViewCapture import com.android.app.viewcapture.ViewCapture.MAIN_EXECUTOR import com.android.launcher3.util.ActivityLifecycleCallbacksAdapter +import java.util.function.Supplier import org.junit.rules.TestRule import org.junit.runner.Description import org.junit.runners.model.Statement @@ -33,7 +34,7 @@ import org.junit.runners.model.Statement * * This rule will not work in OOP tests that don't have access to the activity under test. */ -class ViewCaptureRule : TestRule { +class ViewCaptureRule(var alreadyOpenActivitySupplier: Supplier) : TestRule { val viewCapture = SimpleViewCapture("test-view-capture") override fun apply(base: Statement, description: Description): Statement { @@ -41,16 +42,16 @@ class ViewCaptureRule : TestRule { override fun evaluate() { val windowListenerCloseables = mutableListOf() + val alreadyOpenActivity = alreadyOpenActivitySupplier.get() + if (alreadyOpenActivity != null) { + startCapture(windowListenerCloseables, alreadyOpenActivity) + } + val lifecycleCallbacks = object : ActivityLifecycleCallbacksAdapter { override fun onActivityCreated(activity: Activity, bundle: Bundle?) { super.onActivityCreated(activity, bundle) - windowListenerCloseables.add( - viewCapture.startCapture( - activity.window.decorView, - "${description.testClass?.simpleName}.${description.methodName}" - ) - ) + startCapture(windowListenerCloseables, activity) } override fun onActivityDestroyed(activity: Activity) { @@ -75,6 +76,18 @@ class ViewCaptureRule : TestRule { MAIN_EXECUTOR.execute { windowListenerCloseables.onEach(SafeCloseable::close) } } } + + private fun startCapture( + windowListenerCloseables: MutableCollection, + activity: Activity + ) { + windowListenerCloseables.add( + viewCapture.startCapture( + activity.window.decorView, + "${description.testClass?.simpleName}.${description.methodName}" + ) + ) + } } } }