Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 45059a2c authored by Cassy Chun-Crogan's avatar Cassy Chun-Crogan
Browse files

[DocsUI M3] Ensure no race condition when clearing selection in tests

Previously selectDocument() was used to deselect files (as
clicking on the selection region of a selected document deselects
it). Unfortunately this function doesn't wait for the deselection
to complete which can lead to test failures. Instead introduce
clearSelection() which clicks the cancel button and then waits for
it to disappear, indicating selection was cleared.

Bug: 411295692
Test: atest FileCopyUiTest
Test: atest FilesActivityUiTest
Test: atest InternalStorageUiTest
Test: atest PeekUiTest
Test: atest SearchViewUiTest
Flag: com.android.documentsui.flags.use_material3
Change-Id: Ieaf8464334af1cccc61d65b04b723ae84c018c3e
parent 8a881537
Loading
Loading
Loading
Loading
+19 −1
Original line number Diff line number Diff line
@@ -16,6 +16,12 @@

package com.android.documentsui.bots;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.matcher.ViewMatchers.isDescendantOfA;
import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription;
import static androidx.test.espresso.matcher.ViewMatchers.withId;

import static com.android.documentsui.util.FlagUtils.isUseMaterial3FlagEnabled;

import static junit.framework.Assert.assertEquals;
@@ -24,6 +30,8 @@ import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;

import static org.hamcrest.Matchers.allOf;

import android.app.UiAutomation;
import android.content.Context;
import android.graphics.Point;
@@ -44,6 +52,8 @@ import androidx.test.uiautomator.UiScrollable;
import androidx.test.uiautomator.UiSelector;
import androidx.test.uiautomator.Until;

import com.android.documentsui.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -188,7 +198,7 @@ public class DirectoryListBot extends Bots.BaseBot {
        Configurator.getInstance().setToolType(toolType);
    }

    public void selectDocument(String label) throws UiObjectNotFoundException {
    private void selectDocument(String label) throws UiObjectNotFoundException {
        waitForDocument(label);
        UiObject2 selectionHotspot = findSelectionHotspot(label);
        selectionHotspot.click();
@@ -230,6 +240,14 @@ public class DirectoryListBot extends Bots.BaseBot {
        return selectionHotspot;
    }

    /**
     * Clicks the "X" cancel selection button.
     */
    public void clearSelection() {
        onView(allOf(withContentDescription("Cancel"),
                isDescendantOfA(withId(R.id.toolbar)))).perform(click());
    }

    public void pasteFilesFromClipboard() {
        mDevice.pressKeyCode(KeyEvent.KEYCODE_V, KeyEvent.META_CTRL_ON);
    }
+1 −1
Original line number Diff line number Diff line
@@ -520,7 +520,7 @@ public class FileCopyUiTest extends ActivityTest<FilesActivity> {
        device.waitForIdle();

        // Select Download folder.
        bots.directory.selectDocument("Download");
        bots.directory.selectDocument("Download", 1);
        device.waitForIdle();

        // Click copy button.
+2 −2
Original line number Diff line number Diff line
@@ -209,7 +209,7 @@ public class FilesActivityUiTest extends ActivityTestJunit4<FilesActivity> {
        }
        Instrumentation.ActivityMonitor monitor = new Instrumentation.ActivityMonitor(
                InspectorActivity.class.getName(), null, false);
        bots.directory.selectDocument("file0.log");
        bots.directory.selectDocument("file0.log", 1);
        bots.main.clickActionItem("Get info");
        monitor.waitForActivityWithTimeout(TIMEOUT);
    }
@@ -274,7 +274,7 @@ public class FilesActivityUiTest extends ActivityTestJunit4<FilesActivity> {

            // Deselect the file and ensure the share menu disappears (this ensures the menu is
            // refreshed).
            bots.directory.selectDocument(fileName);
            bots.directory.clearSelection();
            device.wait(Until.gone(By.desc("Share")), /* timeout= */ 5000);
        } finally {
            cleanupFile(fileName, primaryRoot.title);
+3 −3
Original line number Diff line number Diff line
@@ -61,7 +61,7 @@ public class InternalStorageUiTest extends ActivityTestJunit4<FilesActivity> {
    public void testRenameFile() throws Exception {
        createTestFiles();

        bots.directory.selectDocument(fileName);
        bots.directory.selectDocument(fileName, 1);
        device.waitForIdle();

        bots.main.clickRename();
@@ -85,12 +85,12 @@ public class InternalStorageUiTest extends ActivityTestJunit4<FilesActivity> {
        boolean selected = false;
        // Delete the added file for not affect user and also avoid error on next test.
        if (bots.directory.hasDocuments(fileName)) {
            bots.directory.selectDocument(fileName);
            bots.directory.selectDocument(fileName, 1);
            device.waitForIdle();
            selected = true;
        }
        if (bots.directory.hasDocuments(newFileName)) {
            bots.directory.selectDocument(newFileName);
            bots.directory.selectDocument(newFileName, 1);
            device.waitForIdle();
            selected = true;
        }
+1 −1
Original line number Diff line number Diff line
@@ -386,7 +386,7 @@ public class SearchViewUiTest extends ActivityTestJunit4<FilesActivity> {

        // Deselect the document and click the clear button on the search view (if the selection bar
        // at the top is visible this won't be possible).
        bots.directory.selectDocument(TestFilesRule.FILE_NAME_1);
        bots.directory.clearSelection();
        bots.search.clickSearchViewClearButton();
        device.wait(Until.findObject(By.res(pkg + ":id/history_list")), mTimeout);
    }
Loading