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