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

Commit 36ff1a13 authored by Steve McKay's avatar Steve McKay Committed by android-build-merger
Browse files

Show correct sort header after root switch.

am: 743bc1be

Change-Id: I561508c5ff3f7bd92e494633668b5239b6d295af
parents d13287d4 743bc1be
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -267,6 +267,7 @@ public abstract class BaseActivity extends Activity
        }

        mState.derivedMode = LocalPreferences.getViewMode(this, root, MODE_GRID);
        mSortController.onViewModeChanged(mState.derivedMode);

        // Set summary header's visibility. Only recents and downloads root may have summary in
        // their docs.
+23 −0
Original line number Diff line number Diff line
@@ -83,4 +83,27 @@ public class FilesActivityUiTest extends ActivityTest<FilesActivity> {
        bots.breadcrumb.clickItem("TEST_ROOT_0");
        bots.directory.waitForDocument(dirName1);
    }

    public void testRootChange_UpdatesSortHeader() throws Exception {

        // switch to separate display modes for two separate roots. Each
        // mode has its own distinct sort header. This should be remembered
        // by files app.
        bots.roots.openRoot("Images");
        bots.main.switchToGridMode();
        bots.roots.openRoot("Videos");
        bots.main.switchToListMode();

        // Now switch back and assert the correct mode sort header mode
        // is restored when we load the root with that display mode.
        bots.roots.openRoot("Images");
        bots.sortHeader.assertDropdownMode();
        if (bots.main.inFixedLayout()) {
            bots.roots.openRoot("Videos");
            bots.sortHeader.assertColumnMode();
        } else {
            bots.roots.openRoot("Videos");
            bots.sortHeader.assertDropdownMode();
        }
    }
}
+10 −10
Original line number Diff line number Diff line
@@ -90,7 +90,7 @@ public class SortDocumentUiTest extends ActivityTest<FilesActivity> {

        bots.main.switchToListMode();

        bots.directory.sortBy(
        bots.sortHeader.sortBy(
                SortModel.SORT_DIMENSION_ID_TITLE, SortDimension.SORT_DIRECTION_DESCENDING);
        bots.directory.assertOrder(DIRS_IN_NAME_DESC, FILES_IN_NAME_DESC);
    }
@@ -100,7 +100,7 @@ public class SortDocumentUiTest extends ActivityTest<FilesActivity> {

        bots.main.switchToListMode();

        bots.directory.sortBy(
        bots.sortHeader.sortBy(
                SortModel.SORT_DIMENSION_ID_SIZE, SortDimension.SORT_DIRECTION_ASCENDING);
        bots.directory.assertOrder(DIRS_IN_NAME_ASC, FILES_IN_SIZE_ASC);
    }
@@ -110,7 +110,7 @@ public class SortDocumentUiTest extends ActivityTest<FilesActivity> {

        bots.main.switchToListMode();

        bots.directory.sortBy(
        bots.sortHeader.sortBy(
                SortModel.SORT_DIMENSION_ID_SIZE, SortDimension.SORT_DIRECTION_DESCENDING);
        bots.directory.assertOrder(DIRS_IN_NAME_ASC, FILES_IN_SIZE_DESC);
    }
@@ -120,7 +120,7 @@ public class SortDocumentUiTest extends ActivityTest<FilesActivity> {

        bots.main.switchToListMode();

        bots.directory.sortBy(
        bots.sortHeader.sortBy(
                SortModel.SORT_DIMENSION_ID_DATE, SortDimension.SORT_DIRECTION_ASCENDING);
        bots.directory.assertOrder(DIRS, FILES);
    }
@@ -130,7 +130,7 @@ public class SortDocumentUiTest extends ActivityTest<FilesActivity> {

        bots.main.switchToListMode();

        bots.directory.sortBy(
        bots.sortHeader.sortBy(
                SortModel.SORT_DIMENSION_ID_DATE, SortDimension.SORT_DIRECTION_DESCENDING);
        bots.directory.assertOrder(DIRS_IN_MODIFIED_DESC, FILES_IN_MODIFIED_DESC);
    }
@@ -140,7 +140,7 @@ public class SortDocumentUiTest extends ActivityTest<FilesActivity> {

        bots.main.switchToGridMode();

        bots.directory.sortBy(
        bots.sortHeader.sortBy(
                SortModel.SORT_DIMENSION_ID_TITLE, SortDimension.SORT_DIRECTION_DESCENDING);
        bots.directory.assertOrder(DIRS_IN_NAME_DESC, FILES_IN_NAME_DESC);
    }
@@ -150,7 +150,7 @@ public class SortDocumentUiTest extends ActivityTest<FilesActivity> {

        bots.main.switchToGridMode();

        bots.directory.sortBy(
        bots.sortHeader.sortBy(
                SortModel.SORT_DIMENSION_ID_SIZE, SortDimension.SORT_DIRECTION_ASCENDING);
        bots.directory.assertOrder(DIRS_IN_NAME_ASC, FILES_IN_SIZE_ASC);
    }
@@ -160,7 +160,7 @@ public class SortDocumentUiTest extends ActivityTest<FilesActivity> {

        bots.main.switchToGridMode();

        bots.directory.sortBy(
        bots.sortHeader.sortBy(
                SortModel.SORT_DIMENSION_ID_SIZE, SortDimension.SORT_DIRECTION_DESCENDING);
        bots.directory.assertOrder(DIRS_IN_NAME_ASC, FILES_IN_SIZE_DESC);
    }
@@ -170,7 +170,7 @@ public class SortDocumentUiTest extends ActivityTest<FilesActivity> {

        bots.main.switchToGridMode();

        bots.directory.sortBy(
        bots.sortHeader.sortBy(
                SortModel.SORT_DIMENSION_ID_DATE, SortDimension.SORT_DIRECTION_ASCENDING);
        bots.directory.assertOrder(DIRS, FILES);
    }
@@ -180,7 +180,7 @@ public class SortDocumentUiTest extends ActivityTest<FilesActivity> {

        bots.main.switchToGridMode();

        bots.directory.sortBy(
        bots.sortHeader.sortBy(
                SortModel.SORT_DIMENSION_ID_DATE, SortDimension.SORT_DIRECTION_DESCENDING);
        bots.directory.assertOrder(DIRS_IN_MODIFIED_DESC, FILES_IN_MODIFIED_DESC);
    }
+3 −1
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ public final class Bots {

    public final BreadBot breadcrumb;
    public final DirectoryListBot directory;
    public final SortHeaderBot sortHeader;
    public final KeyboardBot keyboard;
    public final RootsListBot roots;
    public final SearchBot search;
@@ -46,6 +47,7 @@ public final class Bots {
        breadcrumb = new BreadBot(device, context, TIMEOUT, main);
        roots = new RootsListBot(device, context, TIMEOUT);
        directory = new DirectoryListBot(device, context, TIMEOUT);
        sortHeader = new SortHeaderBot(device, context, TIMEOUT);
        keyboard = new KeyboardBot(device, context, TIMEOUT);
        search = new SearchBot(device, context, TIMEOUT);
    }
@@ -55,7 +57,7 @@ public final class Bots {
     * and making assertions against the state of it.
     */
    static abstract class BaseBot {
        final UiDevice mDevice;
        public final UiDevice mDevice;
        final Context mContext;
        final int mTimeout;

+3 −135
Original line number Diff line number Diff line
@@ -16,26 +16,13 @@

package com.android.documentsui.bots;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA;
import static android.support.test.espresso.matcher.ViewMatchers.withChild;
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

import static com.android.documentsui.sorting.SortDimension.SORT_DIRECTION_ASCENDING;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;

import static org.hamcrest.Matchers.allOf;
import static junit.framework.Assert.fail;

import android.content.Context;
import android.graphics.Rect;
import android.support.annotation.StringRes;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.BySelector;
import android.support.test.uiautomator.Configurator;
@@ -49,16 +36,6 @@ import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

import com.android.documentsui.R;
import com.android.documentsui.sorting.SortDimension;
import com.android.documentsui.sorting.SortDimension.SortDirection;
import com.android.documentsui.sorting.SortModel;
import com.android.documentsui.sorting.SortModel.SortDimensionId;

import junit.framework.Assert;

import org.hamcrest.Matcher;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -75,15 +52,8 @@ public class DirectoryListBot extends Bots.BaseBot {
    private static final BySelector SNACK_DELETE =
            By.desc(Pattern.compile("^Deleting [0-9]+ file.+"));

    private final SortModel mSortModel = SortModel.createModel();

    private final DropdownSortBot mDropdownBot;
    private final HeaderSortBot mHeaderBot;

    public DirectoryListBot(UiDevice device, Context context, int timeout) {
        super(device, context, timeout);
        mDropdownBot = new DropdownSortBot();
        mHeaderBot = new HeaderSortBot();
    }

    public void assertDocumentsCount(int count) throws UiObjectNotFoundException {
@@ -99,7 +69,7 @@ public class DirectoryListBot extends Bots.BaseBot {
            }
        }
        if (!absent.isEmpty()) {
            Assert.fail("Expected documents " + Arrays.asList(labels)
            fail("Expected documents " + Arrays.asList(labels)
                    + ", but missing " + absent);
        }
    }
@@ -112,7 +82,7 @@ public class DirectoryListBot extends Bots.BaseBot {
            }
        }
        if (!found.isEmpty()) {
            Assert.fail("Expected documents not present" + Arrays.asList(labels)
            fail("Expected documents not present" + Arrays.asList(labels)
                    + ", but present " + found);
        }
    }
@@ -234,22 +204,6 @@ public class DirectoryListBot extends Bots.BaseBot {
        assertHasFocus(DIR_LIST_ID);
    }

    public void sortBy(@SortDimensionId int id, @SortDirection int direction) {
        assert(direction != SortDimension.SORT_DIRECTION_NONE);

        final @StringRes int labelId = mSortModel.getDimensionById(id).getLabelId();
        final String label = mContext.getString(labelId);
        final boolean result;
        if (Matchers.present(mDropdownBot.MATCHER)) {
            result = mDropdownBot.sortBy(label, direction);
        } else {
            result = mHeaderBot.sortBy(label, direction);
        }

        assertTrue("Sorting by id: " + id + " in direction: " + direction + " failed.",
                result);
    }

    public void assertOrder(String[] dirs, String[] files) throws UiObjectNotFoundException {
        for (int i = 0; i < dirs.length - 1; ++i) {
            assertOrder(dirs[i], dirs[i + 1]);
@@ -284,90 +238,4 @@ public class DirectoryListBot extends Bots.BaseBot {
                    firstBound.bottom < secondBound.top || firstBound.left > secondBound.right);
        }
    }

    private static class DropdownSortBot {

        private static final Matcher<View> MATCHER = withId(R.id.dropdown_sort_widget);
        private static final Matcher<View> DROPDOWN_MATCHER = allOf(
                withId(R.id.sort_dimen_dropdown),
                withParent(MATCHER));
        private static final Matcher<View> SORT_ARROW_MATCHER = allOf(
                withId(R.id.sort_arrow),
                withParent(MATCHER));

        private boolean sortBy(String label, @SortDirection int direction) {
            onView(DROPDOWN_MATCHER).perform(click());
            onView(withText(label)).perform(click());

            if (direction != getDirection()) {
                onView(SORT_ARROW_MATCHER).perform(click());
            }

            return Matchers.present(allOf(
                    DROPDOWN_MATCHER,
                    withText(label)))
                    && getDirection() == direction;
        }

        private @SortDirection int getDirection() {
            final boolean ascending = Matchers.present(
                    allOf(
                            SORT_ARROW_MATCHER,
                            withContentDescription(R.string.sort_direction_ascending)));

            if (ascending) {
                return SORT_DIRECTION_ASCENDING;
            }

            final boolean descending = Matchers.present(
                    allOf(
                            SORT_ARROW_MATCHER,
                            withContentDescription(R.string.sort_direction_descending)));

            return descending
                    ? SortDimension.SORT_DIRECTION_DESCENDING
                    : SortDimension.SORT_DIRECTION_NONE;
        }
    }

    private static class HeaderSortBot {

        private static final Matcher<View> MATCHER = withId(R.id.table_header);

        private boolean sortBy(String label, @SortDirection int direction) {
            final Matcher<View> cellMatcher = allOf(
                    withChild(withText(label)),
                    isDescendantOfA(MATCHER));
            onView(cellMatcher).perform(click());

            final @SortDirection int viewDirection = getDirection(cellMatcher);

            if (viewDirection != direction) {
                onView(cellMatcher).perform(click());
            }

            return getDirection(cellMatcher) == direction;
        }

        private @SortDirection int getDirection(Matcher<View> cellMatcher) {
            final boolean ascending =
                    Matchers.present(
                            allOf(
                                    withContentDescription(R.string.sort_direction_ascending),
                                    withParent(cellMatcher)));
            if (ascending) {
                return SORT_DIRECTION_ASCENDING;
            }

            final boolean descending =
                    Matchers.present(
                            allOf(
                                    withContentDescription(R.string.sort_direction_descending),
                                    withParent(cellMatcher)));

            return descending
                    ? SortDimension.SORT_DIRECTION_DESCENDING
                    : SortDimension.SORT_DIRECTION_NONE;
        }
    }
}
Loading