diff options
Diffstat (limited to 'tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java')
-rw-r--r-- | tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java | 85 |
1 files changed, 30 insertions, 55 deletions
diff --git a/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java b/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java index dcb6dc1b02..075505eb93 100644 --- a/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java +++ b/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java @@ -36,9 +36,9 @@ import android.content.pm.PackageManager; import android.os.Debug; import android.os.Process; import android.os.RemoteException; -import android.os.StrictMode; import android.os.UserHandle; import android.os.UserManager; +import android.system.OsConstants; import android.util.Log; import androidx.test.InstrumentationRegistry; @@ -52,7 +52,6 @@ import com.android.launcher3.LauncherAppState; import com.android.launcher3.LauncherSettings; import com.android.launcher3.LauncherState; import com.android.launcher3.Utilities; -import com.android.launcher3.common.WidgetUtils; import com.android.launcher3.model.data.ItemInfo; import com.android.launcher3.statemanager.StateManager; import com.android.launcher3.tapl.LauncherInstrumentation; @@ -63,6 +62,7 @@ import com.android.launcher3.testing.TestProtocol; import com.android.launcher3.util.LooperExecutor; import com.android.launcher3.util.PackageManagerHelper; import com.android.launcher3.util.Wait; +import com.android.launcher3.util.WidgetUtils; import com.android.launcher3.util.rule.FailureWatcher; import com.android.launcher3.util.rule.LauncherActivityRule; import com.android.launcher3.util.rule.ScreenRecordRule; @@ -84,6 +84,7 @@ import java.lang.annotation.Target; import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; @@ -99,11 +100,9 @@ public abstract class AbstractLauncherUiTest { public static final long DEFAULT_UI_TIMEOUT = 10000; private static final String TAG = "AbstractLauncherUiTest"; - private static String sStrictmodeDetectedActivityLeak; private static boolean sDumpWasGenerated = false; - private static boolean sActivityLeakReported; + private static boolean sActivityLeakReported = false; private static final String SYSTEMUI_PACKAGE = "com.android.systemui"; - protected static final ActivityLeakTracker ACTIVITY_LEAK_TRACKER = new ActivityLeakTracker(); protected LooperExecutor mMainThreadExecutor = MAIN_EXECUTOR; protected final UiDevice mDevice = UiDevice.getInstance(getInstrumentation()); @@ -112,45 +111,25 @@ public abstract class AbstractLauncherUiTest { protected String mTargetPackage; private int mLauncherPid; - static { - if (TestHelpers.isInLauncherProcess()) { - StrictMode.VmPolicy.Builder builder = - new StrictMode.VmPolicy.Builder() - .penaltyLog() - .penaltyListener(Runnable::run, violation -> { - if (sStrictmodeDetectedActivityLeak == null) { - sStrictmodeDetectedActivityLeak = violation.toString() + ", " - + dumpHprofData() + "."; - } - }); - StrictMode.setVmPolicy(builder.build()); - } - } - public static void checkDetectedLeaks(LauncherInstrumentation launcher) { if (sActivityLeakReported) return; - if (sStrictmodeDetectedActivityLeak != null) { - // Report from the test thread strictmode violations detected in the main thread. - sActivityLeakReported = true; - Assert.fail(sStrictmodeDetectedActivityLeak); - } - // Check whether activity leak detector has found leaked activities. - Wait.atMost(AbstractLauncherUiTest::getActivityLeakErrorMessage, + Wait.atMost(() -> getActivityLeakErrorMessage(launcher), () -> { launcher.forceGc(); return MAIN_EXECUTOR.submit( - () -> ACTIVITY_LEAK_TRACKER.noLeakedActivities()).get(); + () -> launcher.noLeakedActivities()).get(); }, DEFAULT_UI_TIMEOUT, launcher); } - private static String getActivityLeakErrorMessage() { + private static String getActivityLeakErrorMessage(LauncherInstrumentation launcher) { sActivityLeakReported = true; - return "Activity leak detector has found leaked activities, " + dumpHprofData() + "."; + return "Activity leak detector has found leaked activities, " + + dumpHprofData(launcher) + "."; } - public static String dumpHprofData() { + public static String dumpHprofData(LauncherInstrumentation launcher) { String result; if (sDumpWasGenerated) { Log.d("b/195319692", "dump has already been generated by another test", @@ -170,14 +149,13 @@ public abstract class AbstractLauncherUiTest { } sDumpWasGenerated = true; Log.d("b/195319692", "sDumpWasGenerated := true", new Exception()); - result = "memory dump filename: " + fileName; + result = "saved memory dump as an artifact"; } catch (Throwable e) { Log.e(TAG, "dumpHprofData failed", e); result = "failed to save memory dump"; } } - return result - + ". Full list of activities: " + ACTIVITY_LEAK_TRACKER.getActivitiesList(); + return result + ". Full list of activities: " + launcher.getRootedActivitiesList(); } protected AbstractLauncherUiTest() { @@ -235,7 +213,8 @@ public abstract class AbstractLauncherUiTest { } protected TestRule getRulesInsideActivityMonitor() { - final RuleChain inner = RuleChain.outerRule(new PortraitLandscapeRunner(this)) + final RuleChain inner = RuleChain + .outerRule(new PortraitLandscapeRunner(this)) .around(new FailureWatcher(mDevice, mLauncher)); return TestHelpers.isInLauncherProcess() @@ -254,26 +233,12 @@ public abstract class AbstractLauncherUiTest { return mDevice; } - private boolean hasSystemUiObject(String resId) { - return mDevice.hasObject(By.res(SYSTEMUI_PACKAGE, resId)); - } - @Before public void setUp() throws Exception { mLauncher.onTestStart(); - Log.d(TAG, "Before disabling battery defender"); - mDevice.executeShellCommand("setprop vendor.battery.defender.disable 1"); - Log.d(TAG, "Before enabling stay awake"); - mDevice.executeShellCommand("settings put global stay_on_while_plugged_in 3"); - for (int i = 0; i < 10 && hasSystemUiObject("keyguard_status_view"); ++i) { - Log.d(TAG, "Before unlocking the phone"); - mDevice.executeShellCommand("input keyevent 82"); - mDevice.waitForIdle(); - } - Assert.assertTrue("Keyguard still visible", + Assert.assertTrue("Keyguard is visible, which is likely caused by a crash in SysUI", TestHelpers.wait( Until.gone(By.res(SYSTEMUI_PACKAGE, "keyguard_status_view")), 60000)); - Log.d(TAG, "Keyguard is not visible"); final String launcherPackageName = mDevice.getLauncherPackageName(); try { @@ -300,8 +265,6 @@ public abstract class AbstractLauncherUiTest { if (userManager != null) { for (UserHandle userHandle : userManager.getUserProfiles()) { if (!userHandle.isSystem()) { - Log.d(TestProtocol.WORK_PROFILE_REMOVED, - "removing user " + userHandle.getIdentifier()); mDevice.executeShellCommand("pm remove-user " + userHandle.getIdentifier()); } } @@ -366,7 +329,12 @@ public abstract class AbstractLauncherUiTest { */ protected <T> T getOnUiThread(final Callable<T> callback) { try { - return mMainThreadExecutor.submit(callback).get(); + return mMainThreadExecutor.submit(callback).get(DEFAULT_UI_TIMEOUT, + TimeUnit.MILLISECONDS); + } catch (TimeoutException e) { + Log.e(TAG, "Timeout in getOnUiThread, sending SIGABRT", e); + Process.sendSignal(Process.myPid(), OsConstants.SIGABRT); + throw new RuntimeException(e); } catch (Throwable e) { throw new RuntimeException(e); } @@ -385,13 +353,20 @@ public abstract class AbstractLauncherUiTest { } // Cannot be used in TaplTests between a Tapl call injecting a gesture and a tapl call - // expecting - // the results of that gesture because the wait can hide flakeness. + // expecting the results of that gesture because the wait can hide flakeness. protected void waitForState(String message, Supplier<LauncherState> state) { waitForLauncherCondition(message, launcher -> launcher.getStateManager().getCurrentStableState() == state.get()); } + // Cannot be used in TaplTests between a Tapl call injecting a gesture and a tapl call + // expecting the results of that gesture because the wait can hide flakeness. + protected void waitForStateTransitionToEnd(String message, Supplier<LauncherState> state) { + waitForLauncherCondition(message, + launcher -> launcher.getStateManager().isInStableState(state.get()) + && !launcher.getStateManager().isInTransition()); + } + protected void waitForResumed(String message) { waitForLauncherCondition(message, launcher -> launcher.hasBeenResumed()); } |