1 /*
<lambda>null2  * Copyright (C) 2023 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.temporarydisplay.chipbar
18 
19 import android.graphics.Rect
20 import android.view.MotionEvent
21 import android.view.View
22 import androidx.test.ext.junit.runners.AndroidJUnit4
23 import androidx.test.filters.SmallTest
24 import com.android.dx.mockito.inline.extended.ExtendedMockito.doAnswer
25 import com.android.systemui.SysuiTestCase
26 import com.android.systemui.settings.FakeDisplayTracker
27 import com.android.systemui.util.mockito.any
28 import com.android.systemui.util.mockito.mock
29 import com.android.systemui.util.mockito.whenever
30 import com.google.common.truth.Truth.assertThat
31 import org.junit.Before
32 import org.junit.Test
33 import org.junit.runner.RunWith
34 
35 @SmallTest
36 @RunWith(AndroidJUnit4::class)
37 class SwipeChipbarAwayGestureHandlerTest : SysuiTestCase() {
38 
39     private lateinit var underTest: SwipeChipbarAwayGestureHandler
40 
41     @Before
42     fun setUp() {
43         underTest = SwipeChipbarAwayGestureHandler(context, FakeDisplayTracker(mContext), mock())
44     }
45 
46     @Test
47     fun startOfGestureIsWithinBounds_noViewFetcher_returnsFalse() {
48         assertThat(underTest.startOfGestureIsWithinBounds(createMotionEvent())).isFalse()
49     }
50 
51     @Test
52     fun startOfGestureIsWithinBounds_usesViewFetcher_aboveBottom_returnsTrue() {
53         val view = createMockView()
54 
55         underTest.setViewFetcher { view }
56 
57         val motionEvent = createMotionEvent(y = VIEW_BOTTOM - 100f)
58         assertThat(underTest.startOfGestureIsWithinBounds(motionEvent)).isTrue()
59     }
60 
61     @Test
62     fun startOfGestureIsWithinBounds_usesViewFetcher_slightlyBelowBottom_returnsTrue() {
63         val view = createMockView()
64 
65         underTest.setViewFetcher { view }
66 
67         val motionEvent = createMotionEvent(y = VIEW_BOTTOM + 20f)
68         assertThat(underTest.startOfGestureIsWithinBounds(motionEvent)).isTrue()
69     }
70 
71     @Test
72     fun startOfGestureIsWithinBounds_usesViewFetcher_tooFarDown_returnsFalse() {
73         val view = createMockView()
74 
75         underTest.setViewFetcher { view }
76 
77         val motionEvent = createMotionEvent(y = VIEW_BOTTOM * 4f)
78         assertThat(underTest.startOfGestureIsWithinBounds(motionEvent)).isFalse()
79     }
80 
81     @Test
82     fun startOfGestureIsWithinBounds_viewFetcherReset_returnsFalse() {
83         val view = createMockView()
84 
85         underTest.setViewFetcher { view }
86 
87         val motionEvent = createMotionEvent(y = VIEW_BOTTOM - 100f)
88         assertThat(underTest.startOfGestureIsWithinBounds(motionEvent)).isTrue()
89 
90         underTest.resetViewFetcher()
91         assertThat(underTest.startOfGestureIsWithinBounds(motionEvent)).isFalse()
92     }
93 
94     private fun createMotionEvent(y: Float = 0f): MotionEvent {
95         return MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, 0f, y, 0)
96     }
97 
98     private fun createMockView(): View {
99         return mock<View>().also {
100             doAnswer { invocation ->
101                     val out: Rect = invocation.getArgument(0)
102                     out.set(0, 0, 0, VIEW_BOTTOM)
103                     null
104                 }
105                 .whenever(it)
106                 .getBoundsOnScreen(any())
107         }
108     }
109 
110     private companion object {
111         const val VIEW_BOTTOM = 455
112     }
113 }
114