Winson Chung | bca0311 | 2017-08-16 10:38:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.pip.phone; |
| 18 | |
| 19 | import static android.view.MotionEvent.ACTION_DOWN; |
| 20 | import static android.view.MotionEvent.ACTION_MOVE; |
| 21 | import static android.view.MotionEvent.ACTION_UP; |
| 22 | |
| 23 | import static org.junit.Assert.assertFalse; |
| 24 | import static org.junit.Assert.assertTrue; |
| 25 | |
| 26 | import android.os.Handler; |
Winson Chung | bca0311 | 2017-08-16 10:38:15 -0700 | [diff] [blame] | 27 | import android.os.Looper; |
| 28 | import android.os.SystemClock; |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 29 | import android.testing.AndroidTestingRunner; |
| 30 | import android.testing.TestableLooper; |
| 31 | import android.testing.TestableLooper.RunWithLooper; |
Winson Chung | bca0311 | 2017-08-16 10:38:15 -0700 | [diff] [blame] | 32 | import android.view.MotionEvent; |
| 33 | import android.view.ViewConfiguration; |
| 34 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 35 | import androidx.test.filters.SmallTest; |
| 36 | |
Winson Chung | bca0311 | 2017-08-16 10:38:15 -0700 | [diff] [blame] | 37 | import com.android.systemui.SysuiTestCase; |
Winson Chung | bca0311 | 2017-08-16 10:38:15 -0700 | [diff] [blame] | 38 | |
| 39 | import org.junit.Before; |
Winson Chung | bca0311 | 2017-08-16 10:38:15 -0700 | [diff] [blame] | 40 | import org.junit.Test; |
| 41 | import org.junit.runner.RunWith; |
| 42 | |
| 43 | import java.util.concurrent.CountDownLatch; |
Winson Chung | bca0311 | 2017-08-16 10:38:15 -0700 | [diff] [blame] | 44 | |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 45 | @RunWith(AndroidTestingRunner.class) |
Winson Chung | bca0311 | 2017-08-16 10:38:15 -0700 | [diff] [blame] | 46 | @SmallTest |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 47 | @RunWithLooper |
Winson Chung | bca0311 | 2017-08-16 10:38:15 -0700 | [diff] [blame] | 48 | public class PipTouchStateTest extends SysuiTestCase { |
| 49 | |
Winson Chung | bca0311 | 2017-08-16 10:38:15 -0700 | [diff] [blame] | 50 | private PipTouchState mTouchState; |
| 51 | private CountDownLatch mDoubleTapCallbackTriggeredLatch; |
| 52 | |
| 53 | @Before |
| 54 | public void setUp() throws Exception { |
Winson Chung | bca0311 | 2017-08-16 10:38:15 -0700 | [diff] [blame] | 55 | mDoubleTapCallbackTriggeredLatch = new CountDownLatch(1); |
| 56 | mTouchState = new PipTouchState(ViewConfiguration.get(getContext()), |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 57 | Handler.createAsync(Looper.myLooper()), () -> { |
Winson Chung | bca0311 | 2017-08-16 10:38:15 -0700 | [diff] [blame] | 58 | mDoubleTapCallbackTriggeredLatch.countDown(); |
| 59 | }); |
| 60 | assertFalse(mTouchState.isDoubleTap()); |
| 61 | assertFalse(mTouchState.isWaitingForDoubleTap()); |
| 62 | } |
| 63 | |
| 64 | @Test |
| 65 | public void testDoubleTapLongSingleTap_notDoubleTapAndNotWaiting() { |
| 66 | final long currentTime = SystemClock.uptimeMillis(); |
| 67 | |
| 68 | mTouchState.onTouchEvent(createMotionEvent(ACTION_DOWN, currentTime, 0, 0)); |
| 69 | mTouchState.onTouchEvent(createMotionEvent(ACTION_UP, |
| 70 | currentTime + PipTouchState.DOUBLE_TAP_TIMEOUT + 10, 0, 0)); |
| 71 | assertFalse(mTouchState.isDoubleTap()); |
| 72 | assertFalse(mTouchState.isWaitingForDoubleTap()); |
| 73 | assertTrue(mTouchState.getDoubleTapTimeoutCallbackDelay() == -1); |
| 74 | } |
| 75 | |
| 76 | @Test |
| 77 | public void testDoubleTapTimeout_timeoutCallbackCalled() throws Exception { |
| 78 | final long currentTime = SystemClock.uptimeMillis(); |
| 79 | |
| 80 | mTouchState.onTouchEvent(createMotionEvent(ACTION_DOWN, currentTime, 0, 0)); |
| 81 | mTouchState.onTouchEvent(createMotionEvent(ACTION_UP, |
| 82 | currentTime + PipTouchState.DOUBLE_TAP_TIMEOUT - 10, 0, 0)); |
| 83 | assertFalse(mTouchState.isDoubleTap()); |
| 84 | assertTrue(mTouchState.isWaitingForDoubleTap()); |
| 85 | |
| 86 | assertTrue(mTouchState.getDoubleTapTimeoutCallbackDelay() == 10); |
| 87 | mTouchState.scheduleDoubleTapTimeoutCallback(); |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 88 | |
| 89 | // TODO: Remove this sleep. Its only being added because it speeds up this test a bit. |
| 90 | Thread.sleep(15); |
| 91 | TestableLooper.get(this).processAllMessages(); |
Winson Chung | bca0311 | 2017-08-16 10:38:15 -0700 | [diff] [blame] | 92 | assertTrue(mDoubleTapCallbackTriggeredLatch.getCount() == 0); |
| 93 | } |
| 94 | |
| 95 | @Test |
| 96 | public void testDoubleTapDrag_doubleTapCanceled() { |
| 97 | final long currentTime = SystemClock.uptimeMillis(); |
| 98 | |
| 99 | mTouchState.onTouchEvent(createMotionEvent(ACTION_DOWN, currentTime, 0, 0)); |
| 100 | mTouchState.onTouchEvent(createMotionEvent(ACTION_MOVE, currentTime + 10, 500, 500)); |
| 101 | mTouchState.onTouchEvent(createMotionEvent(ACTION_UP, currentTime + 20, 500, 500)); |
| 102 | assertTrue(mTouchState.isDragging()); |
| 103 | assertFalse(mTouchState.isDoubleTap()); |
| 104 | assertFalse(mTouchState.isWaitingForDoubleTap()); |
| 105 | assertTrue(mTouchState.getDoubleTapTimeoutCallbackDelay() == -1); |
| 106 | } |
| 107 | |
| 108 | @Test |
| 109 | public void testDoubleTap_doubleTapRegistered() { |
| 110 | final long currentTime = SystemClock.uptimeMillis(); |
| 111 | |
| 112 | mTouchState.onTouchEvent(createMotionEvent(ACTION_DOWN, currentTime, 0, 0)); |
| 113 | mTouchState.onTouchEvent(createMotionEvent(ACTION_UP, currentTime + 10, 0, 0)); |
| 114 | mTouchState.onTouchEvent(createMotionEvent(ACTION_DOWN, |
| 115 | currentTime + PipTouchState.DOUBLE_TAP_TIMEOUT - 20, 0, 0)); |
| 116 | mTouchState.onTouchEvent(createMotionEvent(ACTION_UP, |
| 117 | currentTime + PipTouchState.DOUBLE_TAP_TIMEOUT - 10, 0, 0)); |
| 118 | assertTrue(mTouchState.isDoubleTap()); |
| 119 | assertFalse(mTouchState.isWaitingForDoubleTap()); |
| 120 | assertTrue(mTouchState.getDoubleTapTimeoutCallbackDelay() == -1); |
| 121 | } |
| 122 | |
| 123 | private MotionEvent createMotionEvent(int action, long eventTime, float x, float y) { |
| 124 | return MotionEvent.obtain(0, eventTime, action, x, y, 0); |
| 125 | } |
| 126 | } |