From 826303341a29064e8f565745de2b73c1148ff6e4 Mon Sep 17 00:00:00 2001 From: Riddle Hsu Date: Tue, 25 Jul 2023 15:39:04 +0000 Subject: [PATCH] Attempt to reduce flakiness of testTaskChangeCallBacks The test requires the device to be in a non-sleep state. Such as notifyTaskRemovalStarted is only called when a resumed activity is finishing. Sometimes SystemUI was crashed on other test module, then keyguard is shown after SystemUI is restarted. That causes the test activities to be unable to keep resumed state so the expected callback is missed. Bug: 291149606 Test: atest TaskStackChangedListenerTest#testTaskChangeCallBacks Change-Id: I5f512c548f0bf28a344e8cfab4e08c9fee5e7221 --- services/tests/wmtests/AndroidManifest.xml | 4 +++- .../server/wm/TaskStackChangedListenerTest.java | 8 ++++++++ .../com/android/server/wm/utils/CommonUtils.java | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/services/tests/wmtests/AndroidManifest.xml b/services/tests/wmtests/AndroidManifest.xml index 554b0f408ef9e..2e9a0f919abfb 100644 --- a/services/tests/wmtests/AndroidManifest.xml +++ b/services/tests/wmtests/AndroidManifest.xml @@ -58,7 +58,9 @@ - + diff --git a/services/tests/wmtests/src/com/android/server/wm/TaskStackChangedListenerTest.java b/services/tests/wmtests/src/com/android/server/wm/TaskStackChangedListenerTest.java index 453e4684e8595..e16208bf17f0e 100644 --- a/services/tests/wmtests/src/com/android/server/wm/TaskStackChangedListenerTest.java +++ b/services/tests/wmtests/src/com/android/server/wm/TaskStackChangedListenerTest.java @@ -54,7 +54,10 @@ import android.widget.LinearLayout; import androidx.test.filters.MediumTest; +import com.android.server.wm.utils.CommonUtils; + import org.junit.After; +import org.junit.Before; import org.junit.Test; import java.util.Arrays; @@ -77,6 +80,11 @@ public class TaskStackChangedListenerTest { private static final int WAIT_TIMEOUT_MS = 5000; private static final Object sLock = new Object(); + @Before + public void setUp() { + CommonUtils.dismissKeyguard(); + } + @After public void tearDown() throws Exception { if (mTaskStackListener != null) { diff --git a/services/tests/wmtests/src/com/android/server/wm/utils/CommonUtils.java b/services/tests/wmtests/src/com/android/server/wm/utils/CommonUtils.java index 99d73cf5f7d35..34f9c7579abc1 100644 --- a/services/tests/wmtests/src/com/android/server/wm/utils/CommonUtils.java +++ b/services/tests/wmtests/src/com/android/server/wm/utils/CommonUtils.java @@ -18,7 +18,11 @@ package com.android.server.wm.utils; import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; +import android.app.KeyguardManager; import android.app.UiAutomation; +import android.view.KeyEvent; + +import androidx.test.uiautomator.UiDevice; /** Provides common utility functions. */ public class CommonUtils { @@ -34,4 +38,16 @@ public class CommonUtils { getUiAutomation().dropShellPermissionIdentity(); } } + + /** Dismisses the Keyguard if it is locked. */ + public static void dismissKeyguard() { + final KeyguardManager keyguardManager = getInstrumentation().getContext().getSystemService( + KeyguardManager.class); + if (keyguardManager == null || !keyguardManager.isKeyguardLocked()) { + return; + } + final UiDevice device = UiDevice.getInstance(getInstrumentation()); + device.pressKeyCode(KeyEvent.KEYCODE_WAKEUP); + device.pressKeyCode(KeyEvent.KEYCODE_MENU); + } }