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

Commit 286adf09 authored by Ben Reich's avatar Ben Reich
Browse files

Fix BandSelectionUiTest when use_material3 flag is enabled

When the use_material3 flag is enabled, the size of the items in the
grid mode is different. This can cause inconsistencies in the number of
items that are selected. The test is actually fundamentally flawed as
when it is ran on a larger screen the number of items differs as well.
This updates the test to dynamically calculate the number of items in
the top row and assert the selection is based on that result.

Bug: 412776499
Test: atest com.android.documentsui.BandSelectionUiTest
Flag: com.android.documentsui.flags.use_material3
Change-Id: Ia987461d9b895b5c5f9d3e6bec3dd7c797075864
parent 64e4eb90
Loading
Loading
Loading
Loading
+19 −0
Original line number Diff line number Diff line
@@ -304,6 +304,25 @@ public class DocumentsProviderHelper {
        return children;
    }

    /** List all the children for the specific `root`. */
    public List<DocumentInfo> listAllChildren(RootInfo root) throws Exception {
        List<DocumentInfo> children = new ArrayList<>();
        try (Cursor cursor =
                mClient.query(
                        buildChildDocumentsUri(root.authority, root.documentId),
                        null,
                        null,
                        null,
                        null,
                        null)) {
            Cursor wrapper = new RootCursorWrapper(mUserId, mAuthority, root.rootId, cursor, 100);
            while (wrapper.moveToNext()) {
                children.add(DocumentInfo.fromDirectoryCursor(wrapper));
            }
        }
        return children;
    }

    public void assertFileContents(Uri documentUri, byte[] expected) throws Exception {
        MoreAsserts.assertEquals(
                "Copied file contents differ",
+38 −8
Original line number Diff line number Diff line
@@ -17,16 +17,23 @@ package com.android.documentsui;

import android.graphics.Point;
import android.graphics.Rect;
import android.view.View;

import androidx.test.filters.LargeTest;

import com.android.documentsui.base.DocumentInfo;
import com.android.documentsui.base.RootInfo;
import com.android.documentsui.files.FilesActivity;
import com.android.documentsui.rules.TestFilesRule;
import com.android.documentsui.sorting.SortDimension;
import com.android.documentsui.sorting.SortModel;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.util.List;

@LargeTest
public class BandSelectionUiTest extends ActivityTestJunit4<FilesActivity> {

@@ -52,16 +59,39 @@ public class BandSelectionUiTest extends ActivityTestJunit4<FilesActivity> {

    @Test
    public void testBandSelection_someFiles() throws Exception {
        // Switch to Grid mode and ensure the list is sorted by title.
        bots.main.switchToGridMode();
        bots.sort.sortBy(SortModel.SORT_DIMENSION_ID_TITLE, SortDimension.SORT_DIRECTION_ASCENDING);

        // Get all the documents to enumerate through and find all the documents in the top row.
        RootInfo root = mTestFiles.docsHelper.getRootList().get(0);
        List<DocumentInfo> createdDocs = mTestFiles.docsHelper.listAllChildren(root);

        // Work out the start and end of the drag selection to go select the top row of documents
        // if this is done in the opposite direction it will trigger the drawer to be swiped open.
        Rect dirListBounds = bots.directory.findDocumentsList().getBounds();
        Rect startDoc = bots.directory.findDocument(TestFilesRule.FILE_NAME_NO_RENAME).getBounds();
        Rect endDoc = bots.directory.findDocument(TestFilesRule.FILE_NAME_1).getBounds();
        // Start from right side of file NoRename.
        Point start = new Point(dirListBounds.right - 1, startDoc.bottom - 1);
        // End is center of file1
        Point end = new Point(endDoc.centerX(), endDoc.centerY());
        bots.gesture.bandSelection(start, end);
        Rect startDoc = bots.directory.findDocument(createdDocs.get(0).displayName).getBounds();
        Point endPoint = new Point(dirListBounds.left + 1, startDoc.centerY());
        Point startPoint = new Point(dirListBounds.right - 1, startDoc.centerY());

        bots.directory.assertSelection(3);
        // Count the number of documents in the top row. This can differ based on the screen size so
        // calculate it dynamically to avoid failing on different devices.
        int docsInTopRow = 0;
        for (DocumentInfo doc : createdDocs) {
            Rect docBounds = bots.directory.findDocument(doc.displayName).getBounds();
            if (docBounds.centerY() != startDoc.centerY()) {
                break;
            }
            docsInTopRow++;
        }

        // Perform drag and assert that the number of docs in the top row is selected.
        if (context.getResources().getConfiguration().getLayoutDirection()
                == View.LAYOUT_DIRECTION_RTL) {
            bots.gesture.bandSelection(endPoint, startPoint);
        } else {
            bots.gesture.bandSelection(startPoint, endPoint);
        }
        bots.directory.assertSelection(docsInTopRow);
    }
}