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 Original line Diff line number Diff line
@@ -16,6 +16,12 @@


package com.android.documentsui.bots;
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 com.android.documentsui.util.FlagUtils.isUseMaterial3FlagEnabled;


import static junit.framework.Assert.assertEquals;
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.assertTrue;
import static junit.framework.Assert.fail;
import static junit.framework.Assert.fail;


import static org.hamcrest.Matchers.allOf;

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


import com.android.documentsui.R;

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


    public void selectDocument(String label) throws UiObjectNotFoundException {
    private void selectDocument(String label) throws UiObjectNotFoundException {
        waitForDocument(label);
        waitForDocument(label);
        UiObject2 selectionHotspot = findSelectionHotspot(label);
        UiObject2 selectionHotspot = findSelectionHotspot(label);
        selectionHotspot.click();
        selectionHotspot.click();
@@ -230,6 +240,14 @@ public class DirectoryListBot extends Bots.BaseBot {
        return selectionHotspot;
        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() {
    public void pasteFilesFromClipboard() {
        mDevice.pressKeyCode(KeyEvent.KEYCODE_V, KeyEvent.META_CTRL_ON);
        mDevice.pressKeyCode(KeyEvent.KEYCODE_V, KeyEvent.META_CTRL_ON);
    }
    }
+1 −1
Original line number Original line Diff line number Diff line
@@ -520,7 +520,7 @@ public class FileCopyUiTest extends ActivityTest<FilesActivity> {
        device.waitForIdle();
        device.waitForIdle();


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


        // Click copy button.
        // Click copy button.
+2 −2
Original line number Original line Diff line number Diff line
@@ -209,7 +209,7 @@ public class FilesActivityUiTest extends ActivityTestJunit4<FilesActivity> {
        }
        }
        Instrumentation.ActivityMonitor monitor = new Instrumentation.ActivityMonitor(
        Instrumentation.ActivityMonitor monitor = new Instrumentation.ActivityMonitor(
                InspectorActivity.class.getName(), null, false);
                InspectorActivity.class.getName(), null, false);
        bots.directory.selectDocument("file0.log");
        bots.directory.selectDocument("file0.log", 1);
        bots.main.clickActionItem("Get info");
        bots.main.clickActionItem("Get info");
        monitor.waitForActivityWithTimeout(TIMEOUT);
        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
            // Deselect the file and ensure the share menu disappears (this ensures the menu is
            // refreshed).
            // refreshed).
            bots.directory.selectDocument(fileName);
            bots.directory.clearSelection();
            device.wait(Until.gone(By.desc("Share")), /* timeout= */ 5000);
            device.wait(Until.gone(By.desc("Share")), /* timeout= */ 5000);
        } finally {
        } finally {
            cleanupFile(fileName, primaryRoot.title);
            cleanupFile(fileName, primaryRoot.title);
+3 −3
Original line number Original line Diff line number Diff line
@@ -61,7 +61,7 @@ public class InternalStorageUiTest extends ActivityTestJunit4<FilesActivity> {
    public void testRenameFile() throws Exception {
    public void testRenameFile() throws Exception {
        createTestFiles();
        createTestFiles();


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


        bots.main.clickRename();
        bots.main.clickRename();
@@ -85,12 +85,12 @@ public class InternalStorageUiTest extends ActivityTestJunit4<FilesActivity> {
        boolean selected = false;
        boolean selected = false;
        // Delete the added file for not affect user and also avoid error on next test.
        // Delete the added file for not affect user and also avoid error on next test.
        if (bots.directory.hasDocuments(fileName)) {
        if (bots.directory.hasDocuments(fileName)) {
            bots.directory.selectDocument(fileName);
            bots.directory.selectDocument(fileName, 1);
            device.waitForIdle();
            device.waitForIdle();
            selected = true;
            selected = true;
        }
        }
        if (bots.directory.hasDocuments(newFileName)) {
        if (bots.directory.hasDocuments(newFileName)) {
            bots.directory.selectDocument(newFileName);
            bots.directory.selectDocument(newFileName, 1);
            device.waitForIdle();
            device.waitForIdle();
            selected = true;
            selected = true;
        }
        }
+1 −1
Original line number Original line 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
        // 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).
        // at the top is visible this won't be possible).
        bots.directory.selectDocument(TestFilesRule.FILE_NAME_1);
        bots.directory.clearSelection();
        bots.search.clickSearchViewClearButton();
        bots.search.clickSearchViewClearButton();
        device.wait(Until.findObject(By.res(pkg + ":id/history_list")), mTimeout);
        device.wait(Until.findObject(By.res(pkg + ":id/history_list")), mTimeout);
    }
    }
Loading