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

Commit 68f56673 authored by Sebastián Franco's avatar Sebastián Franco Committed by Android (Google) Code Review
Browse files

Merge "Adding the avility to add icons on multiple CellLayouts on tests." into tm-qpr-dev

parents 74484f4a de1fe14a
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -201,6 +201,13 @@ public class TestInformationHandler implements ResourceBasedOverride {
                });
            }

            case TestProtocol.REQUEST_WORKSPACE_COLUMNS_ROWS: {
                return getLauncherUIProperty(Bundle::putParcelable, launcher -> new Point(
                        InvariantDeviceProfile.INSTANCE.get(mContext).numColumns,
                        InvariantDeviceProfile.INSTANCE.get(mContext).numRows)
                );
            }

            case TestProtocol.REQUEST_HOTSEAT_CELL_CENTER: {
                final HotseatCellCenterRequest request = extra.getParcelable(
                        TestProtocol.TEST_INFO_REQUEST_FIELD);
+1 −0
Original line number Diff line number Diff line
@@ -123,6 +123,7 @@ public final class TestProtocol {

    public static final String REQUEST_WORKSPACE_CELL_LAYOUT_SIZE = "workspace-cell-layout-size";
    public static final String REQUEST_WORKSPACE_CELL_CENTER = "workspace-cell-center";
    public static final String REQUEST_WORKSPACE_COLUMNS_ROWS = "workspace-columns-rows";

    public static final String REQUEST_HOTSEAT_CELL_CENTER = "hotseat-cell-center";

+1 −1
Original line number Diff line number Diff line
@@ -124,7 +124,7 @@ public class WorkspaceCellCenterRequest implements TestInformationRequest {
         * Set span Height in cells
         */
        public WorkspaceCellCenterRequest.Builder setSpanY(int y) {
            this.mCellY = y;
            this.mSpanY = y;
            return this;
        }

+105 −8
Original line number Diff line number Diff line
@@ -29,7 +29,73 @@ import java.util.Set;
import java.util.stream.Collectors;


public class CellLayoutBoard {
public class CellLayoutBoard implements Comparable<CellLayoutBoard> {

    private boolean intersects(Rect r1, Rect r2) {
        // If one rectangle is on left side of other
        if (r1.left > r2.right || r2.left > r1.right) {
            return false;
        }

        // If one rectangle is above other
        if (r1.bottom > r2.top || r2.bottom > r1.top) {
            return false;
        }

        return true;
    }

    private boolean overlapsWithIgnored(Set<Rect> ignoredRectangles, Rect rect) {
        for (Rect ignoredRect : ignoredRectangles) {
            // Using the built in intersects doesn't work because it doesn't account for area 0
            if (intersects(ignoredRect, rect)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int compareTo(CellLayoutBoard cellLayoutBoard) {
        // to be equal they need to have the same number of widgets and the same dimensions
        // their order can be different
        Set<Rect> widgetsSet = new HashSet<>();
        Set<Rect> ignoredRectangles = new HashSet<>();
        for (WidgetRect rect : mWidgetsRects) {
            if (rect.shouldIgnore()) {
                ignoredRectangles.add(rect.mBounds);
            } else {
                widgetsSet.add(rect.mBounds);
            }
        }
        for (WidgetRect rect : cellLayoutBoard.mWidgetsRects) {
            // ignore rectangles overlapping with the area marked by x
            if (overlapsWithIgnored(ignoredRectangles, rect.mBounds)) {
                continue;
            }
            if (!widgetsSet.contains(rect.mBounds)) {
                return -1;
            }
            widgetsSet.remove(rect.mBounds);
        }
        if (!widgetsSet.isEmpty()) {
            return 1;
        }

        // to be equal they need to have the same number of icons their order can be different
        Set<Point> iconsSet = new HashSet<>();
        mIconPoints.forEach(icon -> iconsSet.add(icon.getCoord()));
        for (IconPoint icon : cellLayoutBoard.mIconPoints) {
            if (!iconsSet.contains(icon.getCoord())) {
                return -1;
            }
            iconsSet.remove(icon.getCoord());
        }
        if (!iconsSet.isEmpty()) {
            return 1;
        }
        return 0;
    }

    public static class CellType {
        // The cells marked by this will be filled by 1x1 widgets and will be ignored when
@@ -115,7 +181,7 @@ public class CellLayoutBoard {
    List<IconPoint> mIconPoints = new ArrayList<>();
    Map<Character, IconPoint> mIconsMap = new HashMap<>();

    Point mMain = new Point();
    WidgetRect mMain = null;

    CellLayoutBoard() {
        for (int x = 0; x < mWidget.length; x++) {
@@ -133,7 +199,7 @@ public class CellLayoutBoard {
        return mIconPoints;
    }

    public Point getMain() {
    public WidgetRect getMain() {
        return mMain;
    }

@@ -273,6 +339,16 @@ public class CellLayoutBoard {
        return iconPoints;
    }

    public static WidgetRect getMainFromList(List<CellLayoutBoard> boards) {
        for (CellLayoutBoard board : boards) {
            WidgetRect main = board.getMain();
            if (main != null) {
                return main;
            }
        }
        return null;
    }

    public static CellLayoutBoard boardFromString(String boardStr) {
        String[] lines = boardStr.split("\n");
        CellLayoutBoard board = new CellLayoutBoard();
@@ -281,17 +357,18 @@ public class CellLayoutBoard {
            String line = lines[y];
            for (int x = 0; x < line.length(); x++) {
                char c = line.charAt(x);
                if (c == CellType.MAIN_WIDGET) {
                    board.mMain = new Point(x, y);
                }
                if (c != CellType.EMPTY) {
                    board.mWidget[x][y] = line.charAt(x);
                }
            }
        }
        board.mWidgetsRects = getRects(board.mWidget);
        board.mWidgetsRects.forEach(
                widgetRect -> board.mWidgetsMap.put(widgetRect.mType, widgetRect));
        board.mWidgetsRects.forEach(widgetRect -> {
            if (widgetRect.mType == CellType.MAIN_WIDGET) {
                board.mMain = widgetRect;
            }
            board.mWidgetsMap.put(widgetRect.mType, widgetRect);
        });
        board.mIconPoints = getIconPoints(board.mWidget);
        return board;
    }
@@ -308,4 +385,24 @@ public class CellLayoutBoard {
        }
        return s.toString();
    }

    public static List<CellLayoutBoard> boardListFromString(String boardsStr) {
        String[] lines = boardsStr.split("\n");
        ArrayList<String> individualBoards = new ArrayList<>();
        ArrayList<CellLayoutBoard> boards = new ArrayList<>();
        for (String line : lines) {
            String[] boardSegment = line.split("\\|");
            for (int i = 0; i < boardSegment.length; i++) {
                if (i >= individualBoards.size()) {
                    individualBoards.add(boardSegment[i]);
                } else {
                    individualBoards.set(i, individualBoards.get(i) + "\n" + boardSegment[i]);
                }
            }
        }
        for (String board : individualBoards) {
            boards.add(CellLayoutBoard.boardFromString(board));
        }
        return boards;
    }
}
+88 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.launcher3.celllayout.testcases;

import android.graphics.Point;

import java.util.Map;

/**
 * The grids represent the workspace to be build by TestWorkspaceBuilder, to see what each character
 * in the board mean refer to {@code CellType}
 */
public class MultipleCellLayoutsSimpleReorder {

    /** 5x5 Test
     **/
    private static final String START_BOARD_STR_5x5 = ""
            + "xxxxx|-----\n"
            + "--mm-|-----\n"
            + "--mm-|-----\n"
            + "-----|-----\n"
            + "-----|-----";
    private static final Point MOVE_TO_5x5 = new Point(8, 3);
    private static final String END_BOARD_STR_5x5 = ""
            + "xxxxx|-----\n"
            + "-----|-----\n"
            + "-----|-----\n"
            + "-----|---mm\n"
            + "-----|---mm";
    private static final ReorderTestCase TEST_CASE_5x5 = new ReorderTestCase(START_BOARD_STR_5x5,
            MOVE_TO_5x5,
            END_BOARD_STR_5x5);

    /** 4x4 Test
     **/
    private static final String START_BOARD_STR_4x4 = ""
            + "xxxx|----\n"
            + "--mm|----\n"
            + "--mm|----\n"
            + "----|----";
    private static final Point MOVE_TO_4x4 = new Point(5, 3);
    private static final String END_BOARD_STR_4x4 = ""
            + "xxxx|----\n"
            + "----|----\n"
            + "----|-mm-\n"
            + "----|-mm-";
    private static final ReorderTestCase TEST_CASE_4x4 = new ReorderTestCase(START_BOARD_STR_4x4,
            MOVE_TO_4x4,
            END_BOARD_STR_4x4);


    /** 6x5 Test
     **/
    private static final String START_BOARD_STR_6x5 = ""
            + "xxxxxx|------\n"
            + "--m---|------\n"
            + "------|------\n"
            + "------|------\n"
            + "------|------";
    private static final Point MOVE_TO_6x5 = new Point(10, 4);
    private static final String END_BOARD_STR_6x5 = ""
            + "xxxxxx|------\n"
            + "------|------\n"
            + "------|------\n"
            + "------|------\n"
            + "------|----m-";
    private static final ReorderTestCase TEST_CASE_6x5 = new ReorderTestCase(START_BOARD_STR_6x5,
            MOVE_TO_6x5,
            END_BOARD_STR_6x5);

    public static final Map<Point, ReorderTestCase> TEST_BY_GRID_SIZE =
            Map.of(new Point(5, 5), TEST_CASE_5x5,
                    new Point(4, 4), TEST_CASE_4x4,
                    new Point(6, 5), TEST_CASE_6x5);
}
Loading