Home | History | Annotate | Download | only in phone
      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;
     27 import android.os.HandlerThread;
     28 import android.os.Looper;
     29 import android.os.SystemClock;
     30 import android.support.test.filters.SmallTest;
     31 import android.support.test.runner.AndroidJUnit4;
     32 import android.view.MotionEvent;
     33 import android.view.ViewConfiguration;
     34 
     35 import com.android.systemui.SysuiTestCase;
     36 import com.android.systemui.pip.phone.PipTouchState;
     37 
     38 import org.junit.Before;
     39 import org.junit.Ignore;
     40 import org.junit.Test;
     41 import org.junit.runner.RunWith;
     42 
     43 import java.util.concurrent.CountDownLatch;
     44 import java.util.concurrent.TimeUnit;
     45 
     46 @RunWith(AndroidJUnit4.class)
     47 @SmallTest
     48 public 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 }