Merge "Adding unit test for pointToCellExact" into main
diff --git a/tests/multivalentTests/src/com/android/launcher3/celllayout/CellLayoutMethodsTest.kt b/tests/multivalentTests/src/com/android/launcher3/celllayout/CellLayoutMethodsTest.kt
new file mode 100644
index 0000000..e8459d6
--- /dev/null
+++ b/tests/multivalentTests/src/com/android/launcher3/celllayout/CellLayoutMethodsTest.kt
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2024 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
+
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import org.junit.Rule
+import org.junit.Test
+import org.junit.runner.RunWith
+
+@RunWith(AndroidJUnit4::class)
+class CellLayoutMethodsTest {
+
+ @JvmField @Rule var cellLayoutBuilder = UnitTestCellLayoutBuilderRule()
+
+ @Test
+ fun pointToCellExact() {
+ val width = 1000
+ val height = 1000
+ val columns = 30
+ val rows = 30
+ val cl = cellLayoutBuilder.createCellLayout(columns, rows, false, width, height)
+
+ val res = intArrayOf(0, 0)
+ for (col in 0..<columns) {
+ for (row in 0..<rows) {
+ val x = (width / columns) * col
+ val y = (height / rows) * row
+ cl.pointToCellExact(x, y, res)
+ cl.pointToCellExact(x, y, res)
+ assertValues(col, res, row, columns, rows, width, height, x, y)
+ }
+ }
+
+ cl.pointToCellExact(-10, -10, res)
+ assertValues(0, res, 0, columns, rows, width, height, -10, -10)
+ cl.pointToCellExact(width + 10, height + 10, res)
+ assertValues(columns - 1, res, rows - 1, columns, rows, width, height, -10, -10)
+ }
+
+ private fun assertValues(
+ col: Int,
+ res: IntArray,
+ row: Int,
+ columns: Int,
+ rows: Int,
+ width: Int,
+ height: Int,
+ x: Int,
+ y: Int
+ ) {
+ assert(col == res[0] && row == res[1]) {
+ "Cell Layout with values (c= $columns, r= $rows, w= $width, h= $height) didn't mapped correctly the pixels ($x, $y) to the cells ($col, $row) with result (${res[0]}, ${res[1]})"
+ }
+ }
+}
diff --git a/tests/multivalentTests/src/com/android/launcher3/celllayout/ReorderAlgorithmUnitTest.java b/tests/multivalentTests/src/com/android/launcher3/celllayout/ReorderAlgorithmUnitTest.java
index 8a9711d..30953cc 100644
--- a/tests/multivalentTests/src/com/android/launcher3/celllayout/ReorderAlgorithmUnitTest.java
+++ b/tests/multivalentTests/src/com/android/launcher3/celllayout/ReorderAlgorithmUnitTest.java
@@ -144,8 +144,8 @@
public ItemConfiguration solve(CellLayoutBoard board, int x, int y, int spanX,
int spanY, int minSpanX, int minSpanY, boolean isMulti) {
- CellLayout cl = mCellLayoutBuilder.createCellLayout(board.getWidth(), board.getHeight(),
- isMulti);
+ CellLayout cl = mCellLayoutBuilder.createCellLayoutDefaultSize(board.getWidth(),
+ board.getHeight(), isMulti);
// The views have to be sorted or the result can vary
board.getIcons()
diff --git a/tests/multivalentTests/src/com/android/launcher3/celllayout/UnitTestCellLayoutBuilderRule.kt b/tests/multivalentTests/src/com/android/launcher3/celllayout/UnitTestCellLayoutBuilderRule.kt
index b63966d..f624be1 100644
--- a/tests/multivalentTests/src/com/android/launcher3/celllayout/UnitTestCellLayoutBuilderRule.kt
+++ b/tests/multivalentTests/src/com/android/launcher3/celllayout/UnitTestCellLayoutBuilderRule.kt
@@ -64,11 +64,21 @@
dp.inv.numRows = prevNumRows
}
- fun createCellLayout(width: Int, height: Int, isMulti: Boolean): CellLayout {
+ fun createCellLayoutDefaultSize(columns: Int, rows: Int, isMulti: Boolean): CellLayout {
+ return createCellLayout(columns, rows, isMulti)
+ }
+
+ fun createCellLayout(
+ columns: Int,
+ rows: Int,
+ isMulti: Boolean,
+ width: Int = 1000,
+ height: Int = 1000
+ ): CellLayout {
val dp = getDeviceProfile()
// modify the device profile.
- dp.inv.numColumns = if (isMulti) width / 2 else width
- dp.inv.numRows = height
+ dp.inv.numColumns = if (isMulti) columns / 2 else columns
+ dp.inv.numRows = rows
dp.cellLayoutBorderSpacePx = Point(0, 0)
val cl =
if (isMulti) MultipageCellLayout(getWrappedContext(applicationContext, dp))
@@ -76,8 +86,8 @@
// I put a very large number for width and height so that all the items can fit, it doesn't
// need to be exact, just bigger than the sum of cell border
cl.measure(
- View.MeasureSpec.makeMeasureSpec(10000, View.MeasureSpec.EXACTLY),
- View.MeasureSpec.makeMeasureSpec(10000, View.MeasureSpec.EXACTLY)
+ View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
+ View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY)
)
return cl
}