1 /* 2 * Copyright (C) 2021 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.statusbar.phone 18 19 import android.view.MotionEvent 20 import android.view.ViewGroup 21 import androidx.test.filters.SmallTest 22 import com.android.systemui.Gefingerpoken 23 import com.android.systemui.SysuiTestCase 24 import com.android.systemui.shade.ShadeViewController 25 import com.google.common.truth.Truth.assertThat 26 import org.junit.Before 27 import org.junit.Test 28 import org.mockito.Mock 29 import org.mockito.MockitoAnnotations 30 31 @SmallTest 32 class PhoneStatusBarViewTest : SysuiTestCase() { 33 34 @Mock 35 private lateinit var shadeViewController: ShadeViewController 36 @Mock 37 private lateinit var panelView: ViewGroup 38 39 private lateinit var view: PhoneStatusBarView 40 41 @Before 42 fun setUp() { 43 MockitoAnnotations.initMocks(this) 44 45 view = PhoneStatusBarView(mContext, null) 46 } 47 48 @Test 49 fun onTouchEvent_listenerNotified() { 50 val handler = TestTouchEventHandler() 51 view.setTouchEventHandler(handler) 52 53 val event = MotionEvent.obtain(0L, 0L, MotionEvent.ACTION_DOWN, 0f, 0f, 0) 54 view.onTouchEvent(event) 55 56 assertThat(handler.lastEvent).isEqualTo(event) 57 } 58 59 @Test 60 fun onInterceptTouchEvent_listenerNotified() { 61 val handler = TestTouchEventHandler() 62 view.setTouchEventHandler(handler) 63 64 val event = MotionEvent.obtain(0L, 0L, MotionEvent.ACTION_DOWN, 0f, 0f, 0) 65 view.onInterceptTouchEvent(event) 66 67 assertThat(handler.lastInterceptEvent).isEqualTo(event) 68 } 69 70 @Test 71 fun onTouchEvent_listenerReturnsTrue_viewReturnsTrue() { 72 val handler = TestTouchEventHandler() 73 view.setTouchEventHandler(handler) 74 val event = MotionEvent.obtain(0L, 0L, MotionEvent.ACTION_DOWN, 0f, 0f, 0) 75 76 handler.handleTouchReturnValue = true 77 78 assertThat(view.onTouchEvent(event)).isTrue() 79 } 80 81 @Test 82 fun onTouchEvent_listenerReturnsFalse_viewReturnsFalse() { 83 val handler = TestTouchEventHandler() 84 view.setTouchEventHandler(handler) 85 val event = MotionEvent.obtain(0L, 0L, MotionEvent.ACTION_DOWN, 0f, 0f, 0) 86 87 handler.handleTouchReturnValue = false 88 89 assertThat(view.onTouchEvent(event)).isFalse() 90 } 91 92 @Test 93 fun onTouchEvent_noListener_noCrash() { 94 view.onTouchEvent(MotionEvent.obtain(0L, 0L, MotionEvent.ACTION_DOWN, 0f, 0f, 0)) 95 // No assert needed, just testing no crash 96 } 97 98 private class TestTouchEventHandler : Gefingerpoken { 99 var lastInterceptEvent: MotionEvent? = null 100 var lastEvent: MotionEvent? = null 101 var handleTouchReturnValue: Boolean = false 102 103 override fun onInterceptTouchEvent(event: MotionEvent?): Boolean { 104 lastInterceptEvent = event 105 return handleTouchReturnValue 106 } 107 108 override fun onTouchEvent(event: MotionEvent?): Boolean { 109 lastEvent = event 110 return handleTouchReturnValue 111 } 112 } 113 } 114