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 17package com.android.systemui.pip.phone; 18 19import static android.view.MotionEvent.ACTION_DOWN; 20import static android.view.MotionEvent.ACTION_MOVE; 21import static android.view.MotionEvent.ACTION_UP; 22 23import static org.junit.Assert.assertFalse; 24import static org.junit.Assert.assertTrue; 25 26import android.os.Handler; 27import android.os.HandlerThread; 28import android.os.Looper; 29import android.os.SystemClock; 30import android.support.test.filters.SmallTest; 31import android.support.test.runner.AndroidJUnit4; 32import android.view.MotionEvent; 33import android.view.ViewConfiguration; 34 35import com.android.systemui.SysuiTestCase; 36import com.android.systemui.pip.phone.PipTouchState; 37 38import org.junit.Before; 39import org.junit.Ignore; 40import org.junit.Test; 41import org.junit.runner.RunWith; 42 43import java.util.concurrent.CountDownLatch; 44import java.util.concurrent.TimeUnit; 45 46@RunWith(AndroidJUnit4.class) 47@SmallTest 48public class PipTouchStateTest extends SysuiTestCase { 49 50 private Handler mHandler; 51 private HandlerThread mHandlerThread; 52 private PipTouchState mTouchState; 53 private CountDownLatch mDoubleTapCallbackTriggeredLatch; 54 55 @Before 56 public void setUp() throws Exception { 57 mHandlerThread = new HandlerThread("PipTouchStateTestThread"); 58 mHandlerThread.start(); 59 mHandler = new Handler(mHandlerThread.getLooper()); 60 61 mDoubleTapCallbackTriggeredLatch = new CountDownLatch(1); 62 mTouchState = new PipTouchState(ViewConfiguration.get(getContext()), 63 mHandler, () -> { 64 mDoubleTapCallbackTriggeredLatch.countDown(); 65 }); 66 assertFalse(mTouchState.isDoubleTap()); 67 assertFalse(mTouchState.isWaitingForDoubleTap()); 68 } 69 70 @Test 71 public void testDoubleTapLongSingleTap_notDoubleTapAndNotWaiting() { 72 final long currentTime = SystemClock.uptimeMillis(); 73 74 mTouchState.onTouchEvent(createMotionEvent(ACTION_DOWN, currentTime, 0, 0)); 75 mTouchState.onTouchEvent(createMotionEvent(ACTION_UP, 76 currentTime + PipTouchState.DOUBLE_TAP_TIMEOUT + 10, 0, 0)); 77 assertFalse(mTouchState.isDoubleTap()); 78 assertFalse(mTouchState.isWaitingForDoubleTap()); 79 assertTrue(mTouchState.getDoubleTapTimeoutCallbackDelay() == -1); 80 } 81 82 @Test 83 public void testDoubleTapTimeout_timeoutCallbackCalled() throws Exception { 84 final long currentTime = SystemClock.uptimeMillis(); 85 86 mTouchState.onTouchEvent(createMotionEvent(ACTION_DOWN, currentTime, 0, 0)); 87 mTouchState.onTouchEvent(createMotionEvent(ACTION_UP, 88 currentTime + PipTouchState.DOUBLE_TAP_TIMEOUT - 10, 0, 0)); 89 assertFalse(mTouchState.isDoubleTap()); 90 assertTrue(mTouchState.isWaitingForDoubleTap()); 91 92 assertTrue(mTouchState.getDoubleTapTimeoutCallbackDelay() == 10); 93 mTouchState.scheduleDoubleTapTimeoutCallback(); 94 mDoubleTapCallbackTriggeredLatch.await(1, TimeUnit.SECONDS); 95 assertTrue(mDoubleTapCallbackTriggeredLatch.getCount() == 0); 96 } 97 98 @Test 99 public void testDoubleTapDrag_doubleTapCanceled() { 100 final long currentTime = SystemClock.uptimeMillis(); 101 102 mTouchState.onTouchEvent(createMotionEvent(ACTION_DOWN, currentTime, 0, 0)); 103 mTouchState.onTouchEvent(createMotionEvent(ACTION_MOVE, currentTime + 10, 500, 500)); 104 mTouchState.onTouchEvent(createMotionEvent(ACTION_UP, currentTime + 20, 500, 500)); 105 assertTrue(mTouchState.isDragging()); 106 assertFalse(mTouchState.isDoubleTap()); 107 assertFalse(mTouchState.isWaitingForDoubleTap()); 108 assertTrue(mTouchState.getDoubleTapTimeoutCallbackDelay() == -1); 109 } 110 111 @Test 112 public void testDoubleTap_doubleTapRegistered() { 113 final long currentTime = SystemClock.uptimeMillis(); 114 115 mTouchState.onTouchEvent(createMotionEvent(ACTION_DOWN, currentTime, 0, 0)); 116 mTouchState.onTouchEvent(createMotionEvent(ACTION_UP, currentTime + 10, 0, 0)); 117 mTouchState.onTouchEvent(createMotionEvent(ACTION_DOWN, 118 currentTime + PipTouchState.DOUBLE_TAP_TIMEOUT - 20, 0, 0)); 119 mTouchState.onTouchEvent(createMotionEvent(ACTION_UP, 120 currentTime + PipTouchState.DOUBLE_TAP_TIMEOUT - 10, 0, 0)); 121 assertTrue(mTouchState.isDoubleTap()); 122 assertFalse(mTouchState.isWaitingForDoubleTap()); 123 assertTrue(mTouchState.getDoubleTapTimeoutCallbackDelay() == -1); 124 } 125 126 private MotionEvent createMotionEvent(int action, long eventTime, float x, float y) { 127 return MotionEvent.obtain(0, eventTime, action, x, y, 0); 128 } 129}