Fix ContextMenuTest and RenameUiTest on Angler.

When doing consecutive selections, due the fact we are using
GestureDetector, consecutive selections that happen too close to each
other can result it being recognized as double-clicking. By making sure
"1 selected" appears first, we can wait a bit longer to make sure
onSingleTapConfirmed() gets called in the code as opposed to
onDoubleTap().

Bug: 31568862
Change-Id: I639b1854766d1a338b3ab397a4e6309885dba5a9
diff --git a/tests/common/com/android/documentsui/bots/DirectoryListBot.java b/tests/common/com/android/documentsui/bots/DirectoryListBot.java
index 5bddac1..0207557 100644
--- a/tests/common/com/android/documentsui/bots/DirectoryListBot.java
+++ b/tests/common/com/android/documentsui/bots/DirectoryListBot.java
@@ -134,10 +134,18 @@
         findDocument(label).click();
     }
 
-    public void selectDocument(String label) throws UiObjectNotFoundException {
+    /**
+     * @param label The filename of the document
+     * @param number Which nth document it is. The number corresponding to "n selected"
+     */
+    public void selectDocument(String label, int number) throws UiObjectNotFoundException {
         waitForDocument(label);
         UiObject2 selectionHotspot = findSelectionHotspot(label);
         selectionHotspot.click();
+
+        // wait until selection is fully done to avoid future click being registered as double
+        // clicking
+        assertSelection(number);
     }
 
     public UiObject2 findSelectionHotspot(String label) {
diff --git a/tests/functional/com/android/documentsui/ContextMenuUiTest.java b/tests/functional/com/android/documentsui/ContextMenuUiTest.java
index a81f3a7..5755078 100644
--- a/tests/functional/com/android/documentsui/ContextMenuUiTest.java
+++ b/tests/functional/com/android/documentsui/ContextMenuUiTest.java
@@ -40,6 +40,7 @@
     public void setUp() throws Exception {
         super.setUp();
         initTestFiles();
+        bots.roots.closeDrawer();
         menuItems = new HashMap<>();
 
         menuItems.put("Share", false);
@@ -96,9 +97,8 @@
         menuItems.put("Cut", true);
         menuItems.put("Copy", true);
         menuItems.put("Delete", true);
-        bots.directory.selectDocument("file1.png");
-        bots.directory.selectDocument("Dir1");
-        bots.directory.assertSelection(2); //wait until selection is fully done
+        bots.directory.selectDocument("file1.png", 1);
+        bots.directory.selectDocument("Dir1", 2);
         bots.directory.rightClickDocument("Dir1");
         bots.menu.assertPresentMenuItems(menuItems);
     }
diff --git a/tests/functional/com/android/documentsui/RenameDocumentUiTest.java b/tests/functional/com/android/documentsui/RenameDocumentUiTest.java
index 3bbf691..5ed4d79 100644
--- a/tests/functional/com/android/documentsui/RenameDocumentUiTest.java
+++ b/tests/functional/com/android/documentsui/RenameDocumentUiTest.java
@@ -18,7 +18,6 @@
 
 import android.support.test.uiautomator.UiObjectNotFoundException;
 import android.test.suitebuilder.annotation.LargeTest;
-
 import com.android.documentsui.R;
 import com.android.documentsui.R.string;
 import com.android.documentsui.manager.ManageActivity;
@@ -41,7 +40,7 @@
 
     // TODO: Move this over to the FilesMenuManagerTest.
     public void testRenameEnabled_SingleSelection() throws Exception {
-        bots.directory.selectDocument(fileName1);
+        bots.directory.selectDocument(fileName1, 1);
         bots.main.openOverflowMenu();
         bots.main.assertMenuEnabled(R.string.menu_rename, true);
 
@@ -51,7 +50,7 @@
 
     // TODO: Move this over to the FilesMenuManagerTest.
     public void testNoRenameSupport_SingleSelection() throws Exception {
-        bots.directory.selectDocument(fileNameNoRename);
+        bots.directory.selectDocument(fileNameNoRename, 1);
         bots.main.openOverflowMenu();
         bots.main.assertMenuEnabled(R.string.menu_rename, false);
 
@@ -61,8 +60,8 @@
 
     // TODO: Move this over to the FilesMenuManagerTest.
     public void testOneHasRenameSupport_MultipleSelection() throws Exception {
-        bots.directory.selectDocument(fileName1);
-        bots.directory.selectDocument(fileNameNoRename);
+        bots.directory.selectDocument(fileName1, 1);
+        bots.directory.selectDocument(fileNameNoRename, 2);
         bots.main.openOverflowMenu();
         bots.main.assertMenuEnabled(R.string.menu_rename, false);
 
@@ -72,8 +71,8 @@
 
     // TODO: Move this over to the FilesMenuManagerTest.
     public void testRenameDisabled_MultipleSelection() throws Exception {
-        bots.directory.selectDocument(fileName1);
-        bots.directory.selectDocument(fileName2);
+        bots.directory.selectDocument(fileName1, 1);
+        bots.directory.selectDocument(fileName2, 2);
         bots.main.openOverflowMenu();
         bots.main.assertMenuEnabled(R.string.menu_rename, false);
 
@@ -82,7 +81,7 @@
     }
 
     public void testRenameFile_OkButton() throws Exception {
-        bots.directory.selectDocument(fileName1);
+        bots.directory.selectDocument(fileName1, 1);
 
         clickRename();
 
@@ -98,7 +97,7 @@
     }
 
     public void testRenameFile_Enter() throws Exception {
-        bots.directory.selectDocument(fileName1);
+        bots.directory.selectDocument(fileName1, 1);
 
         clickRename();
 
@@ -114,7 +113,7 @@
     }
 
     public void testRenameFile_Cancel() throws Exception {
-        bots.directory.selectDocument(fileName1);
+        bots.directory.selectDocument(fileName1, 1);
 
         clickRename();
 
@@ -130,7 +129,7 @@
     public void testRenameDir() throws Exception {
         String oldName = "Dir1";
         String newName = "Dir123";
-        bots.directory.selectDocument(oldName);
+        bots.directory.selectDocument(oldName, 1);
 
         clickRename();
 
@@ -146,7 +145,7 @@
     public void testRename_NameExists() throws Exception {
         // Check that document with the new name exists
         bots.directory.assertDocumentsPresent(fileName2);
-        bots.directory.selectDocument(fileName1);
+        bots.directory.selectDocument(fileName1, 1);
 
         clickRename();
 
diff --git a/tests/functional/com/android/documentsui/SidebarUiTest.java b/tests/functional/com/android/documentsui/SidebarUiTest.java
index cd56d1b..48cd902 100644
--- a/tests/functional/com/android/documentsui/SidebarUiTest.java
+++ b/tests/functional/com/android/documentsui/SidebarUiTest.java
@@ -49,7 +49,7 @@
 
     @Suppress
     public void testRootChanged_ClearSelection() throws Exception {
-        bots.directory.selectDocument(fileName1);
+        bots.directory.selectDocument(fileName1, 1);
         bots.main.assertInActionMode(true);
 
         bots.roots.openRoot(ROOT_1_ID);