blob: 3155e57d8ab3c10fe4fe991254abb06c47c86b71 [file] [log] [blame]
Winson Chungbca03112017-08-16 10:38:15 -07001/*
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;
Winson Chungbca03112017-08-16 10:38:15 -070027import android.os.Looper;
28import android.os.SystemClock;
Jason Monk6dceace2018-05-15 20:24:07 -040029import android.testing.AndroidTestingRunner;
30import android.testing.TestableLooper;
31import android.testing.TestableLooper.RunWithLooper;
Winson Chungbca03112017-08-16 10:38:15 -070032import android.view.MotionEvent;
33import android.view.ViewConfiguration;
34
Brett Chabot84151d92019-02-27 15:37:59 -080035import androidx.test.filters.SmallTest;
36
Winson Chungbca03112017-08-16 10:38:15 -070037import com.android.systemui.SysuiTestCase;
Winson Chungbca03112017-08-16 10:38:15 -070038
39import org.junit.Before;
Winson Chungbca03112017-08-16 10:38:15 -070040import org.junit.Test;
41import org.junit.runner.RunWith;
42
43import java.util.concurrent.CountDownLatch;
Winson Chungbca03112017-08-16 10:38:15 -070044
Jason Monk6dceace2018-05-15 20:24:07 -040045@RunWith(AndroidTestingRunner.class)
Winson Chungbca03112017-08-16 10:38:15 -070046@SmallTest
Jason Monk6dceace2018-05-15 20:24:07 -040047@RunWithLooper
Winson Chungbca03112017-08-16 10:38:15 -070048public class PipTouchStateTest extends SysuiTestCase {
49
Winson Chungbca03112017-08-16 10:38:15 -070050 private PipTouchState mTouchState;
51 private CountDownLatch mDoubleTapCallbackTriggeredLatch;
52
53 @Before
54 public void setUp() throws Exception {
Winson Chungbca03112017-08-16 10:38:15 -070055 mDoubleTapCallbackTriggeredLatch = new CountDownLatch(1);
56 mTouchState = new PipTouchState(ViewConfiguration.get(getContext()),
Jason Monk6dceace2018-05-15 20:24:07 -040057 Handler.createAsync(Looper.myLooper()), () -> {
Winson Chungbca03112017-08-16 10:38:15 -070058 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 Monk6dceace2018-05-15 20:24:07 -040088
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 Chungbca03112017-08-16 10:38:15 -070092 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}