1 /*
<lambda>null2  * Copyright (C) 2022 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.systemui.util.view
18 
19 import android.graphics.Rect
20 import android.view.View
21 import android.widget.TextView
22 import androidx.test.ext.junit.runners.AndroidJUnit4
23 import androidx.test.filters.SmallTest
24 import com.android.systemui.SysuiTestCase
25 import com.android.systemui.util.mockito.any
26 import com.google.common.truth.Truth.assertThat
27 import org.junit.Before
28 import org.junit.Test
29 import org.junit.runner.RunWith
30 import org.mockito.Mockito.doAnswer
31 import org.mockito.Mockito.spy
32 import org.mockito.Mockito.`when`
33 
34 @SmallTest
35 @RunWith(AndroidJUnit4::class)
36 class ViewUtilTest : SysuiTestCase() {
37     private val viewUtil = ViewUtil()
38     private lateinit var view: View
39 
40     @Before
41     fun setUp() {
42         view = TextView(context)
43         view.setLeftTopRightBottom(VIEW_LEFT, VIEW_TOP, VIEW_RIGHT, VIEW_BOTTOM)
44 
45         view = spy(view)
46         val location = IntArray(2)
47         location[0] = VIEW_LEFT
48         location[1] = VIEW_TOP
49         `when`(view.locationOnScreen).thenReturn(location)
50         doAnswer { invocation ->
51                 val pos = invocation.arguments[0] as IntArray
52                 pos[0] = VIEW_LEFT
53                 pos[1] = VIEW_TOP
54                 null
55             }
56             .`when`(view)
57             .getLocationInWindow(any())
58     }
59 
60     @Test
61     fun touchIsWithinView_inBounds_returnsTrue() {
62         assertThat(viewUtil.touchIsWithinView(view, VIEW_LEFT + 1f, VIEW_TOP + 1f)).isTrue()
63     }
64 
65     @Test
66     fun touchIsWithinView_onTopLeftCorner_returnsTrue() {
67         assertThat(viewUtil.touchIsWithinView(view, VIEW_LEFT.toFloat(), VIEW_TOP.toFloat()))
68             .isTrue()
69     }
70 
71     @Test
72     fun touchIsWithinView_onBottomRightCorner_returnsTrue() {
73         assertThat(viewUtil.touchIsWithinView(view, VIEW_RIGHT.toFloat(), VIEW_BOTTOM.toFloat()))
74             .isTrue()
75     }
76 
77     @Test
78     fun touchIsWithinView_xTooSmall_returnsFalse() {
79         assertThat(viewUtil.touchIsWithinView(view, VIEW_LEFT - 1f, VIEW_TOP + 1f)).isFalse()
80     }
81 
82     @Test
83     fun touchIsWithinView_xTooLarge_returnsFalse() {
84         assertThat(viewUtil.touchIsWithinView(view, VIEW_RIGHT + 1f, VIEW_TOP + 1f)).isFalse()
85     }
86 
87     @Test
88     fun touchIsWithinView_yTooSmall_returnsFalse() {
89         assertThat(viewUtil.touchIsWithinView(view, VIEW_LEFT + 1f, VIEW_TOP - 1f)).isFalse()
90     }
91 
92     @Test
93     fun touchIsWithinView_yTooLarge_returnsFalse() {
94         assertThat(viewUtil.touchIsWithinView(view, VIEW_LEFT + 1f, VIEW_BOTTOM + 1f)).isFalse()
95     }
96 
97     @Test
98     fun setRectToViewWindowLocation_rectHasLocation() {
99         val outRect = Rect()
100 
101         viewUtil.setRectToViewWindowLocation(view, outRect)
102 
103         assertThat(outRect.left).isEqualTo(VIEW_LEFT)
104         assertThat(outRect.right).isEqualTo(VIEW_RIGHT)
105         assertThat(outRect.top).isEqualTo(VIEW_TOP)
106         assertThat(outRect.bottom).isEqualTo(VIEW_BOTTOM)
107     }
108 
109     @Test
110     fun viewBoundsOnScreen_viewAnchoredAtOriginInWindow() {
111         // view is anchored at 0,0 in its window
112         view.setLeftTopRightBottom(0, 0, VIEW_RIGHT - VIEW_LEFT, VIEW_BOTTOM - VIEW_TOP)
113 
114         val outRect = Rect()
115         view.viewBoundsOnScreen(outRect)
116 
117         assertThat(outRect.left).isEqualTo(VIEW_LEFT)
118         assertThat(outRect.right).isEqualTo(VIEW_RIGHT)
119         assertThat(outRect.top).isEqualTo(VIEW_TOP)
120         assertThat(outRect.bottom).isEqualTo(VIEW_BOTTOM)
121     }
122 }
123 
124 private const val VIEW_LEFT = 30
125 private const val VIEW_RIGHT = 100
126 private const val VIEW_TOP = 40
127 private const val VIEW_BOTTOM = 100
128