Dismiss ScreenRecordDialog when test finishes

The GlobalActionsDialogTest#testShoudLogScreenshotsLongPress test
opens ScreenRecordDialog while testing but remains it opened when test
finishes.

Bug: 171271162
Test: atest SystemUITest:GlobalActionsDialogTest
Change-Id: Ic84fed37d6eef818489ebb4a6e2052a4215014b2
diff --git a/packages/SystemUI/Android.bp b/packages/SystemUI/Android.bp
index a9a5671..80a6257 100644
--- a/packages/SystemUI/Android.bp
+++ b/packages/SystemUI/Android.bp
@@ -139,7 +139,9 @@
         "SystemUI-tags",
         "SystemUI-proto",
         "metrics-helper-lib",
-        "androidx.test.rules", "hamcrest-library",
+        "hamcrest-library",
+        "androidx.test.rules",
+        "androidx.test.uiautomator",
         "mockito-target-extended-minus-junit4",
         "testables",
         "truth-prebuilt",
diff --git a/packages/SystemUI/tests/src/com/android/systemui/SysuiTestCase.java b/packages/SystemUI/tests/src/com/android/systemui/SysuiTestCase.java
index 86b1e61..197c000 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/SysuiTestCase.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/SysuiTestCase.java
@@ -30,6 +30,7 @@
 import android.util.Log;
 
 import androidx.test.InstrumentationRegistry;
+import androidx.test.uiautomator.UiDevice;
 
 import com.android.keyguard.KeyguardUpdateMonitor;
 import com.android.settingslib.bluetooth.LocalBluetoothManager;
@@ -149,6 +150,10 @@
         return mContext;
     }
 
+    protected UiDevice getUiDevice() {
+        return UiDevice.getInstance(mRealInstrumentation);
+    }
+
     protected void runShellCommand(String command) throws IOException {
         ParcelFileDescriptor pfd = mRealInstrumentation.getUiAutomation()
                 .executeShellCommand(command);
diff --git a/packages/SystemUI/tests/src/com/android/systemui/globalactions/GlobalActionsDialogTest.java b/packages/SystemUI/tests/src/com/android/systemui/globalactions/GlobalActionsDialogTest.java
index b4af786..78ee593 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/globalactions/GlobalActionsDialogTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/globalactions/GlobalActionsDialogTest.java
@@ -53,6 +53,9 @@
 import android.widget.FrameLayout;
 
 import androidx.test.filters.SmallTest;
+import androidx.test.uiautomator.By;
+import androidx.test.uiautomator.UiObject2;
+import androidx.test.uiautomator.Until;
 
 import com.android.internal.colorextraction.ColorExtractor;
 import com.android.internal.logging.MetricsLogger;
@@ -86,11 +89,16 @@
 
 import java.util.List;
 import java.util.concurrent.Executor;
+import java.util.regex.Pattern;
 
 @SmallTest
 @RunWith(AndroidTestingRunner.class)
 @TestableLooper.RunWithLooper(setAsMainLooper = true)
 public class GlobalActionsDialogTest extends SysuiTestCase {
+    private static final long UI_TIMEOUT_MILLIS = 5000; // 5 sec
+    private static final Pattern CANCEL_BUTTON =
+            Pattern.compile("cancel", Pattern.CASE_INSENSITIVE);
+
     private GlobalActionsDialog mGlobalActionsDialog;
 
     @Mock private GlobalActions.GlobalActionsManager mWindowManagerFuncs;
@@ -240,6 +248,13 @@
                 mGlobalActionsDialog.makeScreenshotActionForTesting();
         screenshotAction.onLongPress();
         verifyLogPosted(GlobalActionsDialog.GlobalActionsEvent.GA_SCREENSHOT_LONG_PRESS);
+
+        // Dismiss ScreenRecordDialog opened by the long press above.
+        final UiObject2 cancelButton = getUiDevice().wait(
+                Until.findObject(By.text(CANCEL_BUTTON)), UI_TIMEOUT_MILLIS);
+        if (cancelButton != null) {
+            cancelButton.click();
+        }
     }
 
     @Test