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