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
18 
19 import android.content.Context
20 import android.view.View
21 import android.view.ViewGroup
22 import androidx.test.core.app.ApplicationProvider.getApplicationContext
23 import androidx.test.ext.junit.runners.AndroidJUnit4
24 import com.android.launcher3.util.ActivityContextWrapper
25 import org.junit.Assert.*
26 import org.junit.Before
27 import org.junit.Test
28 import org.junit.runner.RunWith
29 
30 @RunWith(AndroidJUnit4::class)
31 class UtilitiesTest {
32 
33     private lateinit var mContext: Context
34 
35     @Before
setUpnull36     fun setUp() {
37         mContext = ActivityContextWrapper(getApplicationContext())
38     }
39 
40     @Test
testIsPropertyEnablednull41     fun testIsPropertyEnabled() {
42         // This assumes the property "propertyName" is not enabled by default
43         assertFalse(Utilities.isPropertyEnabled("propertyName"))
44     }
45 
46     @Test
testGetDescendantCoordRelativeToAncestornull47     fun testGetDescendantCoordRelativeToAncestor() {
48         val ancestor =
49             object : ViewGroup(mContext) {
50                 override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {}
51             }
52         val descendant = View(mContext)
53 
54         descendant.x = 50f
55         descendant.y = 30f
56         descendant.scaleX = 2f
57         descendant.scaleY = 2f
58 
59         ancestor.addView(descendant)
60 
61         val coord = floatArrayOf(10f, 15f)
62         val scale =
63             Utilities.getDescendantCoordRelativeToAncestor(descendant, ancestor, coord, false)
64 
65         assertEquals(2f, scale) // Expecting scale to be 2f
66         assertEquals(70f, coord[0])
67         assertEquals(60f, coord[1])
68     }
69 
70     @Test
testRoundArraynull71     fun testRoundArray() {
72         val floatArray = floatArrayOf(1.2f, 3.7f, 5.5f)
73         val intArray = IntArray(3)
74         Utilities.roundArray(floatArray, intArray)
75         assertArrayEquals(intArrayOf(1, 4, 6), intArray)
76     }
77 
78     @Test
testOffsetPointsnull79     fun testOffsetPoints() {
80         val points = floatArrayOf(1f, 2f, 3f, 4f)
81         Utilities.offsetPoints(points, 5f, 6f)
82 
83         val expected = listOf(6f, 8f, 8f, 10f)
84         assertEquals(expected, points.toList())
85     }
86 
87     @Test
testPointInViewnull88     fun testPointInView() {
89         val view = View(mContext)
90         view.layout(0, 0, 100, 100)
91 
92         assertTrue(Utilities.pointInView(view, 50f, 50f, 0f)) // Inside view
93         assertFalse(Utilities.pointInView(view, -10f, -10f, 0f)) // Outside view
94         assertTrue(Utilities.pointInView(view, -5f, -5f, 10f)) // Inside slop
95         assertFalse(Utilities.pointInView(view, 115f, 115f, 10f)) // Outside slop
96     }
97 }
98