1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.launcher3.celllayout;
17 
18 import com.android.launcher3.celllayout.board.CellLayoutBoard;
19 
20 import java.util.Iterator;
21 
22 /**
23  * Represents a test case for {@code ReorderAlgorithmUnitTest}. The test cases are generated from
24  * text, an example of a test is the following:
25  *
26  * board: 10x8
27  * aaaaaaaaai
28  * bbbbbcciii
29  * ---------f
30  * ---------f
31  * ---------f
32  * ---------i
33  * iiddddddii
34  * iieeiiiiii
35  * arguments: 2 5 7 1 3 1 widget valid
36  * board: 10x8
37  * bbbbbbbbbi
38  * eeeeecciii
39  * ---------a
40  * ---------a
41  * ---------a
42  * --zzzzzzzi
43  * iiddddddii
44  * iiffiiiiii
45  *
46  *
47  * This represents a Workspace boards and a dragged widget that wants to be dropped on the
48  * workspace. The endBoard represents the result from such drag
49  * The first board is the startBoard, the arguments are as follow: cellX, cellY, widget spanX,
50  * widget spanY, minimum spanX, minimum spanX, type of object being drag (icon, widget, folder ),
51  * if the resulting board is a valid solution or not reorder was found.
52  *
53  * For more information on how to read the board please go to the text file
54  * reorder_algorithm_test_cases
55  */
56 public class ReorderAlgorithmUnitTestCase {
57 
58     CellLayoutBoard startBoard;
59 
60     int x, y, spanX, spanY, minSpanX, minSpanY;
61     String type;
62     boolean isValidSolution;
63     CellLayoutBoard endBoard;
64 
readNextCase( Iterator<CellLayoutTestCaseReader.TestSection> sections)65     public static ReorderAlgorithmUnitTestCase readNextCase(
66             Iterator<CellLayoutTestCaseReader.TestSection> sections) {
67         ReorderAlgorithmUnitTestCase testCase = new ReorderAlgorithmUnitTestCase();
68         CellLayoutTestCaseReader.Board startBoard =
69                 (CellLayoutTestCaseReader.Board) sections.next();
70         testCase.startBoard = CellLayoutBoard.boardFromString(startBoard.board);
71         CellLayoutTestCaseReader.Arguments arguments =
72                 (CellLayoutTestCaseReader.Arguments) sections.next();
73         testCase.x = Integer.parseInt(arguments.arguments[0]);
74         testCase.y = Integer.parseInt(arguments.arguments[1]);
75         testCase.spanX = Integer.parseInt(arguments.arguments[2]);
76         testCase.spanY = Integer.parseInt(arguments.arguments[3]);
77         testCase.minSpanX = Integer.parseInt(arguments.arguments[4]);
78         testCase.minSpanY = Integer.parseInt(arguments.arguments[5]);
79         testCase.type = arguments.arguments[6];
80         testCase.isValidSolution = arguments.arguments[7].compareTo("valid") == 0;
81 
82         CellLayoutTestCaseReader.Board endBoard = (CellLayoutTestCaseReader.Board) sections.next();
83         testCase.endBoard = CellLayoutBoard.boardFromString(endBoard.board);
84         return testCase;
85     }
86 
getStartBoard()87     public CellLayoutBoard getStartBoard() {
88         return startBoard;
89     }
90 
getX()91     public int getX() {
92         return x;
93     }
94 
setX(int x)95     public void setX(int x) {
96         this.x = x;
97     }
98 
getY()99     public int getY() {
100         return y;
101     }
102 
setY(int y)103     public void setY(int y) {
104         this.y = y;
105     }
106 
getSpanX()107     public int getSpanX() {
108         return spanX;
109     }
110 
setSpanX(int spanX)111     public void setSpanX(int spanX) {
112         this.spanX = spanX;
113     }
114 
getSpanY()115     public int getSpanY() {
116         return spanY;
117     }
118 
setSpanY(int spanY)119     public void setSpanY(int spanY) {
120         this.spanY = spanY;
121     }
122 
getType()123     public String getType() {
124         return type;
125     }
126 
setType(String type)127     public void setType(String type) {
128         this.type = type;
129     }
130 
isValidSolution()131     public boolean isValidSolution() {
132         return isValidSolution;
133     }
134 
setValidSolution(boolean validSolution)135     public void setValidSolution(boolean validSolution) {
136         isValidSolution = validSolution;
137     }
138 
getEndBoard()139     public CellLayoutBoard getEndBoard() {
140         return endBoard;
141     }
142 
setEndBoard(CellLayoutBoard endBoard)143     public void setEndBoard(CellLayoutBoard endBoard) {
144         this.endBoard = endBoard;
145     }
146 
147     @Override
toString()148     public String toString() {
149         String valid = isValidSolution ? "valid" : "invalid";
150         return startBoard + "arguments: " + x + " " + y + " " + spanX + " " + spanY + " " + minSpanX
151                 + " " + minSpanY + " " + type + " " + valid + "\n" + endBoard;
152     }
153 }
154