From f6b79f7caf27c3955b1849a6d2020d0afc2e3c21 Mon Sep 17 00:00:00 2001 From: Sunny Goyal Date: Thu, 13 Jul 2023 15:37:12 -0700 Subject: [PATCH] Removing LauncherActivityRule as it can be flaky based on when the rule was initialized Bug: 289161193 Test: Presubmit and PostSubmit Flag: N/A Change-Id: I0e1e9c6f932d2a98a2f1e47bd1665e26f2db9eb4 --- src/com/android/launcher3/Launcher.java | 16 --- tests/Android.bp | 2 - .../testing/shared/TestProtocol.java | 1 - .../launcher3/ui/AbstractLauncherUiTest.java | 9 +- .../ui/widget/AddConfigWidgetTest.java | 6 +- .../launcher3/ui/widget/AddWidgetTest.java | 7 - .../ui/widget/RequestPinItemTest.java | 6 +- .../util/rule/LauncherActivityRule.java | 59 -------- .../util/rule/SimpleActivityRule.java | 131 ------------------ 9 files changed, 13 insertions(+), 224 deletions(-) delete mode 100644 tests/src/com/android/launcher3/util/rule/LauncherActivityRule.java delete mode 100644 tests/src/com/android/launcher3/util/rule/SimpleActivityRule.java diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java index 9efab36edf..d96eea4bbd 100644 --- a/src/com/android/launcher3/Launcher.java +++ b/src/com/android/launcher3/Launcher.java @@ -424,7 +424,6 @@ public class Launcher extends StatefulActivity @Override @TargetApi(Build.VERSION_CODES.S) protected void onCreate(Bundle savedInstanceState) { - TestProtocol.testLogD(TestProtocol.ACTIVITY_LIFECYCLE_RULE, "Launcher.onCreate 1"); mStartupLatencyLogger = createStartupLatencyLogger( sIsNewProcess ? LockedUserState.get(this).isUserUnlockedAtLauncherStartup() @@ -583,7 +582,6 @@ public class Launcher extends StatefulActivity } setTitle(R.string.home_screen); mStartupLatencyLogger.logEnd(LAUNCHER_LATENCY_STARTUP_ACTIVITY_ON_CREATE); - TestProtocol.testLogD(TestProtocol.ACTIVITY_LIFECYCLE_RULE, "Launcher.onCreate 2"); } /** @@ -1058,7 +1056,6 @@ public class Launcher extends StatefulActivity @Override protected void onStop() { - TestProtocol.testLogD(TestProtocol.ACTIVITY_LIFECYCLE_RULE, "Launcher.onStop 1"); super.onStop(); if (mDeferOverlayCallbacks) { checkIfOverlayStillDeferred(); @@ -1070,12 +1067,10 @@ public class Launcher extends StatefulActivity mAppWidgetHolder.setActivityStarted(false); NotificationListener.removeNotificationsChangedListener(getPopupDataProvider()); FloatingIconView.resetIconLoadResult(); - TestProtocol.testLogD(TestProtocol.ACTIVITY_LIFECYCLE_RULE, "Launcher.onStop 2"); } @Override protected void onStart() { - TestProtocol.testLogD(TestProtocol.ACTIVITY_LIFECYCLE_RULE, "Launcher.onStart 1"); TraceHelper.INSTANCE.beginSection(ON_START_EVT); super.onStart(); if (!mDeferOverlayCallbacks) { @@ -1084,7 +1079,6 @@ public class Launcher extends StatefulActivity mAppWidgetHolder.setActivityStarted(true); TraceHelper.INSTANCE.endSection(); - TestProtocol.testLogD(TestProtocol.ACTIVITY_LIFECYCLE_RULE, "Launcher.onStart 2"); } @Override @@ -1255,7 +1249,6 @@ public class Launcher extends StatefulActivity @Override protected void onResume() { - TestProtocol.testLogD(TestProtocol.ACTIVITY_LIFECYCLE_RULE, "Launcher.onResume 1"); TraceHelper.INSTANCE.beginSection(ON_RESUME_EVT); super.onResume(); @@ -1267,12 +1260,10 @@ public class Launcher extends StatefulActivity DragView.removeAllViews(this); TraceHelper.INSTANCE.endSection(); - TestProtocol.testLogD(TestProtocol.ACTIVITY_LIFECYCLE_RULE, "Launcher.onResume 2"); } @Override protected void onPause() { - TestProtocol.testLogD(TestProtocol.ACTIVITY_LIFECYCLE_RULE, "Launcher.onPause 1"); // Ensure that items added to Launcher are queued until Launcher returns ItemInstallQueue.INSTANCE.get(this).pauseModelPush(FLAG_ACTIVITY_PAUSED); @@ -1285,7 +1276,6 @@ public class Launcher extends StatefulActivity mOverlayManager.onActivityPaused(this); } mAppWidgetHolder.setActivityResumed(false); - TestProtocol.testLogD(TestProtocol.ACTIVITY_LIFECYCLE_RULE, "Launcher.onPause 2"); } /** @@ -1758,8 +1748,6 @@ public class Launcher extends StatefulActivity @Override protected void onSaveInstanceState(Bundle outState) { - TestProtocol.testLogD( - TestProtocol.ACTIVITY_LIFECYCLE_RULE, "Launcher.onSaveInstanceState 1"); outState.putIntArray(RUNTIME_STATE_CURRENT_SCREEN_IDS, mWorkspace.getCurrentPageScreenIds().getArray().toArray()); outState.putInt(RUNTIME_STATE, mStateManager.getState().ordinal); @@ -1791,13 +1779,10 @@ public class Launcher extends StatefulActivity super.onSaveInstanceState(outState); mOverlayManager.onActivitySaveInstanceState(this, outState); - TestProtocol.testLogD( - TestProtocol.ACTIVITY_LIFECYCLE_RULE, "Launcher.onSaveInstanceState 2"); } @Override public void onDestroy() { - TestProtocol.testLogD(TestProtocol.ACTIVITY_LIFECYCLE_RULE, "Launcher.onDestroy 1"); super.onDestroy(); ACTIVITY_TRACKER.onActivityDestroyed(this); @@ -1820,7 +1805,6 @@ public class Launcher extends StatefulActivity LauncherAppState.getIDP(this).removeOnChangeListener(this); mOverlayManager.onActivityDestroyed(this); - TestProtocol.testLogD(TestProtocol.ACTIVITY_LIFECYCLE_RULE, "Launcher.onDestroy 2"); } public LauncherAccessibilityDelegate getAccessibilityDelegate() { diff --git a/tests/Android.bp b/tests/Android.bp index e7f408487d..329b175505 100644 --- a/tests/Android.bp +++ b/tests/Android.bp @@ -50,12 +50,10 @@ filegroup { "src/com/android/launcher3/util/Wait.java", "src/com/android/launcher3/util/WidgetUtils.java", "src/com/android/launcher3/util/rule/FailureWatcher.java", - "src/com/android/launcher3/util/rule/LauncherActivityRule.java", "src/com/android/launcher3/util/rule/ViewCaptureRule.kt", "src/com/android/launcher3/util/rule/SamplerRule.java", "src/com/android/launcher3/util/rule/ScreenRecordRule.java", "src/com/android/launcher3/util/rule/ShellCommandRule.java", - "src/com/android/launcher3/util/rule/SimpleActivityRule.java", "src/com/android/launcher3/util/rule/TestStabilityRule.java", "src/com/android/launcher3/util/rule/TISBindRule.java", "src/com/android/launcher3/testcomponent/BaseTestingActivity.java", diff --git a/tests/shared/com/android/launcher3/testing/shared/TestProtocol.java b/tests/shared/com/android/launcher3/testing/shared/TestProtocol.java index 75cee2f7dc..4073517b5e 100644 --- a/tests/shared/com/android/launcher3/testing/shared/TestProtocol.java +++ b/tests/shared/com/android/launcher3/testing/shared/TestProtocol.java @@ -157,7 +157,6 @@ public final class TestProtocol { public static final String TWO_TASKBAR_LONG_CLICKS = "b/262282528"; public static final String FLAKY_ACTIVITY_COUNT = "b/260260325"; public static final String ICON_MISSING = "b/282963545"; - public static final String ACTIVITY_LIFECYCLE_RULE = "b/289161193"; public static final String REQUEST_EMULATE_DISPLAY = "emulate-display"; public static final String REQUEST_STOP_EMULATE_DISPLAY = "stop-emulate-display"; diff --git a/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java b/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java index a82b005292..d22a353707 100644 --- a/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java +++ b/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java @@ -65,7 +65,6 @@ import com.android.launcher3.util.SimpleBroadcastReceiver; import com.android.launcher3.util.TestUtil; import com.android.launcher3.util.Wait; import com.android.launcher3.util.rule.FailureWatcher; -import com.android.launcher3.util.rule.LauncherActivityRule; import com.android.launcher3.util.rule.SamplerRule; import com.android.launcher3.util.rule.ScreenRecordRule; import com.android.launcher3.util.rule.ShellCommandRule; @@ -182,8 +181,6 @@ public abstract class AbstractLauncherUiTest { mLauncher.setOnLauncherCrashed(() -> mLauncherPid = 0); } - protected final LauncherActivityRule mActivityMonitor = new LauncherActivityRule(); - @Rule public ShellCommandRule mDisableHeadsUpNotification = ShellCommandRule.disableHeadsUpNotification(); @@ -204,7 +201,8 @@ public abstract class AbstractLauncherUiTest { } protected TestRule getRulesInsideActivityMonitor() { - final ViewCaptureRule viewCaptureRule = new ViewCaptureRule(mActivityMonitor::getActivity); + final ViewCaptureRule viewCaptureRule = new ViewCaptureRule( + Launcher.ACTIVITY_TRACKER::getCreatedActivity); final RuleChain inner = RuleChain .outerRule(new PortraitLandscapeRunner(this)) .around(new FailureWatcher(mLauncher, viewCaptureRule::getViewCaptureData)) @@ -219,7 +217,6 @@ public abstract class AbstractLauncherUiTest { public TestRule mOrderSensitiveRules = RuleChain .outerRule(new SamplerRule()) .around(new TestStabilityRule()) - .around(mActivityMonitor) .around(getRulesInsideActivityMonitor()); public UiDevice getDevice() { @@ -322,7 +319,7 @@ public abstract class AbstractLauncherUiTest { protected T getFromLauncher(Function f) { if (!TestHelpers.isInLauncherProcess()) return null; - return getOnUiThread(() -> f.apply(mActivityMonitor.getActivity())); + return getOnUiThread(() -> f.apply(Launcher.ACTIVITY_TRACKER.getCreatedActivity())); } protected void executeOnLauncher(Consumer f) { diff --git a/tests/src/com/android/launcher3/ui/widget/AddConfigWidgetTest.java b/tests/src/com/android/launcher3/ui/widget/AddConfigWidgetTest.java index 82bf644791..b2ce400d8d 100644 --- a/tests/src/com/android/launcher3/ui/widget/AddConfigWidgetTest.java +++ b/tests/src/com/android/launcher3/ui/widget/AddConfigWidgetTest.java @@ -30,6 +30,7 @@ import android.view.View; import androidx.test.filters.LargeTest; import androidx.test.runner.AndroidJUnit4; +import com.android.launcher3.Launcher; import com.android.launcher3.celllayout.FavoriteItemsTransaction; import com.android.launcher3.model.data.ItemInfo; import com.android.launcher3.model.data.LauncherAppWidgetInfo; @@ -124,7 +125,10 @@ public class AddConfigWidgetTest extends AbstractLauncherUiTest { @Override public boolean isTrue() throws Throwable { - return mMainThreadExecutor.submit(mActivityMonitor.itemExists(this)).get(); + return mMainThreadExecutor.submit(() -> { + Launcher l = Launcher.ACTIVITY_TRACKER.getCreatedActivity(); + return l != null && l.getWorkspace().getFirstMatch(this) != null; + }).get(); } @Override diff --git a/tests/src/com/android/launcher3/ui/widget/AddWidgetTest.java b/tests/src/com/android/launcher3/ui/widget/AddWidgetTest.java index b05ebf8182..9dca24bb28 100644 --- a/tests/src/com/android/launcher3/ui/widget/AddWidgetTest.java +++ b/tests/src/com/android/launcher3/ui/widget/AddWidgetTest.java @@ -18,7 +18,6 @@ package com.android.launcher3.ui.widget; import static com.android.launcher3.ui.TaplTestsLauncher3.getAppPackageName; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; import android.platform.test.annotations.PlatinumTest; import android.platform.test.rule.ScreenRecordRule; @@ -27,7 +26,6 @@ import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.LargeTest; import com.android.launcher3.celllayout.FavoriteItemsTransaction; -import com.android.launcher3.model.data.LauncherAppWidgetInfo; import com.android.launcher3.tapl.Widget; import com.android.launcher3.tapl.WidgetResizeFrame; import com.android.launcher3.ui.AbstractLauncherUiTest; @@ -70,11 +68,6 @@ public class AddWidgetTest extends AbstractLauncherUiTest { .getWidget(widgetInfo.getLabel(mTargetContext.getPackageManager())) .dragWidgetToWorkspace(); - assertTrue(mActivityMonitor.itemExists( - (info, view) -> info instanceof LauncherAppWidgetInfo && - ((LauncherAppWidgetInfo) info).providerName.getClassName().equals( - widgetInfo.provider.getClassName())).call()); - assertNotNull("Widget resize frame not shown after widget add", resizeFrame); resizeFrame.dismiss(); diff --git a/tests/src/com/android/launcher3/ui/widget/RequestPinItemTest.java b/tests/src/com/android/launcher3/ui/widget/RequestPinItemTest.java index 384fa468ef..a6b53692f6 100644 --- a/tests/src/com/android/launcher3/ui/widget/RequestPinItemTest.java +++ b/tests/src/com/android/launcher3/ui/widget/RequestPinItemTest.java @@ -32,6 +32,7 @@ import android.view.View; import androidx.test.filters.LargeTest; import androidx.test.runner.AndroidJUnit4; +import com.android.launcher3.Launcher; import com.android.launcher3.LauncherSettings.Favorites; import com.android.launcher3.celllayout.FavoriteItemsTransaction; import com.android.launcher3.model.data.ItemInfo; @@ -193,7 +194,10 @@ public class RequestPinItemTest extends AbstractLauncherUiTest { @Override public boolean isTrue() throws Throwable { - return mMainThreadExecutor.submit(mActivityMonitor.itemExists(mOp)).get(); + return mMainThreadExecutor.submit(() -> { + Launcher l = Launcher.ACTIVITY_TRACKER.getCreatedActivity(); + return l != null && l.getWorkspace().getFirstMatch(mOp) != null; + }).get(); } } } diff --git a/tests/src/com/android/launcher3/util/rule/LauncherActivityRule.java b/tests/src/com/android/launcher3/util/rule/LauncherActivityRule.java deleted file mode 100644 index e9a52f84df..0000000000 --- a/tests/src/com/android/launcher3/util/rule/LauncherActivityRule.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (C) 2017 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. - */ -package com.android.launcher3.util.rule; - -import android.app.Activity; - -import com.android.launcher3.Launcher; -import com.android.launcher3.util.LauncherBindableItemsContainer.ItemOperator; - -import org.junit.runner.Description; -import org.junit.runners.model.Statement; - -import java.util.concurrent.Callable; - -/** - * Test rule to get the current Launcher activity. - */ -public class LauncherActivityRule extends SimpleActivityRule { - - public LauncherActivityRule() { - super(Launcher.class); - } - - @Override - public Statement apply(Statement base, Description description) { - - return new MyStatement(base) { - @Override - public void onActivityStarted(Activity activity) { - if (activity instanceof Launcher) { - ((Launcher) activity).getRotationHelper().forceAllowRotationForTesting(true); - } - } - }; - } - - public Callable itemExists(final ItemOperator op) { - return () -> { - Launcher launcher = getActivity(); - if (launcher == null) { - return false; - } - return launcher.getWorkspace().getFirstMatch(op) != null; - }; - } -} \ No newline at end of file diff --git a/tests/src/com/android/launcher3/util/rule/SimpleActivityRule.java b/tests/src/com/android/launcher3/util/rule/SimpleActivityRule.java deleted file mode 100644 index b5d819318c..0000000000 --- a/tests/src/com/android/launcher3/util/rule/SimpleActivityRule.java +++ /dev/null @@ -1,131 +0,0 @@ -/* - * Copyright (C) 2019 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. - */ -package com.android.launcher3.util.rule; - -import android.app.Activity; -import android.app.Application; -import android.app.Application.ActivityLifecycleCallbacks; -import android.os.Bundle; - -import androidx.test.InstrumentationRegistry; - -import com.android.launcher3.testing.shared.TestProtocol; - -import org.junit.rules.TestRule; -import org.junit.runner.Description; -import org.junit.runners.model.Statement; - -/** - * Test rule to get the current activity. - */ -public class SimpleActivityRule implements TestRule { - - private final Class mClass; - private T mActivity; - - public SimpleActivityRule(Class clazz) { - mClass = clazz; - } - - @Override - public Statement apply(Statement base, Description description) { - return new MyStatement(base); - } - - public T getActivity() { - return mActivity; - } - - protected class MyStatement extends Statement implements ActivityLifecycleCallbacks { - - private final Statement mBase; - - public MyStatement(Statement base) { - mBase = base; - } - - @Override - public void evaluate() throws Throwable { - Application app = (Application) - InstrumentationRegistry.getTargetContext().getApplicationContext(); - app.registerActivityLifecycleCallbacks(this); - try { - mBase.evaluate(); - } finally { - app.unregisterActivityLifecycleCallbacks(this); - mActivity = null; - } - } - - @Override - public void onActivityCreated(Activity activity, Bundle bundle) { - if (activity != null && mClass.isInstance(activity)) { - TestProtocol.testLogD( - TestProtocol.ACTIVITY_LIFECYCLE_RULE, "MyStatement.onActivityCreated"); - mActivity = (T) activity; - } - } - - @Override - public void onActivityStarted(Activity activity) { - if (activity == mActivity) { - TestProtocol.testLogD( - TestProtocol.ACTIVITY_LIFECYCLE_RULE, "MyStatement.onActivityStarted"); - } - } - - @Override - public void onActivityResumed(Activity activity) { - if (activity == mActivity) { - TestProtocol.testLogD( - TestProtocol.ACTIVITY_LIFECYCLE_RULE, "MyStatement.onActivityResumed"); - } - } - - @Override - public void onActivityPaused(Activity activity) { - if (activity == mActivity) { - TestProtocol.testLogD( - TestProtocol.ACTIVITY_LIFECYCLE_RULE, "MyStatement.onActivityPaused"); - } - } - - @Override - public void onActivityStopped(Activity activity) { - if (activity == mActivity) { - TestProtocol.testLogD( - TestProtocol.ACTIVITY_LIFECYCLE_RULE, "MyStatement.onAcgtivityStopped"); - } - } - - @Override - public void onActivitySaveInstanceState(Activity activity, Bundle bundle) { - if (activity == mActivity) { - TestProtocol.testLogD(TestProtocol.ACTIVITY_LIFECYCLE_RULE, - "MyStatement.onActivitySaveInstanceState"); - } - } - - @Override - public void onActivityDestroyed(Activity activity) { - if (activity == mActivity) { - TestProtocol.testLogD( - TestProtocol.ACTIVITY_LIFECYCLE_RULE, "MyStatement.onActivityDestroyed"); - mActivity = null; - } - } - } -} \ No newline at end of file