Adding unit test to ReorderAlgorithm
Adding 100 different test cases for the ReorderAlgorithm.
The test cases are randomly generated using generateRandomTestCase()
the boards are generated once and then written in the file
reorder_algorithm_test_cases. I will leave the code to generate
the boards in the Test even though is not used anymore in case
we need to generate more boards later on.
Also, I found that the ReorderAlgorithm was not deterministic,
meaning that it could generate two different results with the same
inputs (views positions and view being drag positions), because
it was traversing a map whose has was the object id which is
random. So I sort the views before traversing them.
Bug: 229292911
Test: atest ReorderAlgorithmUnitTestCase
Change-Id: I196eb8f1dafcb57d5259969268c458129ae4f46b
diff --git a/src/com/android/launcher3/CellLayout.java b/src/com/android/launcher3/CellLayout.java
index 0988769..5163ede 100644
--- a/src/com/android/launcher3/CellLayout.java
+++ b/src/com/android/launcher3/CellLayout.java
@@ -85,6 +85,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
+import java.util.List;
import java.util.Stack;
public class CellLayout extends ViewGroup {
@@ -891,7 +892,7 @@
*
* @param result Array of 2 ints to hold the x and y coordinate of the point
*/
- void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
+ public void regionToCenterPoint(int cellX, int cellY, int spanX, int spanY, int[] result) {
cellToRect(cellX, cellY, spanX, spanY, mTempRect);
result[0] = mTempRect.centerX();
result[1] = mTempRect.centerY();
@@ -2340,7 +2341,16 @@
}
Rect r0 = new Rect(cellX, cellY, cellX + spanX, cellY + spanY);
Rect r1 = new Rect();
- for (View child: solution.map.keySet()) {
+ // The views need to be sorted so that the results are deterministic on the views positions
+ // and not by the views hash which is "random".
+ // The views are sorted twice, once for the X position and a second time for the Y position
+ // to ensure same order everytime.
+ Comparator comparator = Comparator.comparing(view ->
+ ((CellLayoutLayoutParams) ((View) view).getLayoutParams()).getCellX())
+ .thenComparing(view ->
+ ((CellLayoutLayoutParams) ((View) view).getLayoutParams()).getCellY());
+ List<View> views = solution.map.keySet().stream().sorted(comparator).toList();
+ for (View child : views) {
if (child == ignoreView) continue;
CellAndSpan c = solution.map.get(child);
CellLayoutLayoutParams lp = (CellLayoutLayoutParams) child.getLayoutParams();