1 /*
2  * Copyright (C) 2024 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 
17 package com.android.launcher3.celllayout.integrationtest
18 
19 import android.graphics.Point
20 import android.graphics.Rect
21 import android.view.View
22 import android.view.ViewGroup
23 import com.android.launcher3.CellLayout
24 import com.android.launcher3.Utilities
25 import com.android.launcher3.Workspace
26 import com.android.launcher3.util.CellAndSpan
27 import com.android.launcher3.widget.LauncherAppWidgetHostView
28 
29 object TestUtils {
searchChildrennull30     fun <T> searchChildren(viewGroup: ViewGroup, type: Class<T>): T? where T : View {
31         for (i in 0..<viewGroup.childCount) {
32             val child = viewGroup.getChildAt(i)
33             if (type.isInstance(child)) {
34                 return type.cast(child)
35             }
36             if (child is ViewGroup) {
37                 val result = searchChildren(child, type)
38                 if (result != null) {
39                     return result
40                 }
41             }
42         }
43         return null
44     }
45 
getWidgetAtCellnull46     fun getWidgetAtCell(
47         workspace: Workspace<*>,
48         cellX: Int,
49         cellY: Int
50     ): LauncherAppWidgetHostView {
51         val view =
52             (workspace.getPageAt(workspace.currentPage) as CellLayout).getChildAt(cellX, cellY)
53         assert(view != null) { "There is no view at $cellX , $cellY" }
54         assert(view is LauncherAppWidgetHostView) { "The view at $cellX , $cellY is not a widget" }
55         return view as LauncherAppWidgetHostView
56     }
57 
getCellTopLeftRelativeToWorkspacenull58     fun getCellTopLeftRelativeToWorkspace(
59         workspace: Workspace<*>,
60         cellAndSpan: CellAndSpan
61     ): Point {
62         val target = Rect()
63         val cellLayout = workspace.getPageAt(workspace.currentPage) as CellLayout
64         cellLayout.cellToRect(
65             cellAndSpan.cellX,
66             cellAndSpan.cellY,
67             cellAndSpan.spanX,
68             cellAndSpan.spanY,
69             target
70         )
71         val point = floatArrayOf(target.left.toFloat(), target.top.toFloat())
72         Utilities.getDescendantCoordRelativeToAncestor(cellLayout, workspace, point, false)
73         return Point(point[0].toInt(), point[1].toInt())
74     }
75 }
76