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