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 */ 16package com.android.launcher3.touch; 17 18import android.support.test.InstrumentationRegistry; 19import android.support.test.filters.SmallTest; 20import android.support.test.runner.AndroidJUnit4; 21import android.util.Log; 22import android.view.MotionEvent; 23import android.view.ViewConfiguration; 24 25import com.android.launcher3.testcomponent.TouchEventGenerator; 26 27import org.junit.Before; 28import org.junit.Test; 29import org.junit.runner.RunWith; 30import org.mockito.Mock; 31import org.mockito.MockitoAnnotations; 32 33import static org.mockito.Matchers.anyBoolean; 34import static org.mockito.Matchers.anyFloat; 35import static org.mockito.Mockito.never; 36import static org.mockito.Mockito.verify; 37 38@SmallTest 39@RunWith(AndroidJUnit4.class) 40public class SwipeDetectorTest { 41 42 private static final String TAG = SwipeDetectorTest.class.getSimpleName(); 43 public static void L(String s, Object... parts) { 44 Log.d(TAG, (parts.length == 0) ? s : String.format(s, parts)); 45 } 46 47 private TouchEventGenerator mGenerator; 48 private SwipeDetector mDetector; 49 private int mTouchSlop; 50 51 @Mock 52 private SwipeDetector.Listener mMockListener; 53 54 @Before 55 public void setup() { 56 MockitoAnnotations.initMocks(this); 57 mGenerator = new TouchEventGenerator(new TouchEventGenerator.Listener() { 58 @Override 59 public void onTouchEvent(MotionEvent event) { 60 mDetector.onTouchEvent(event); 61 } 62 }); 63 64 mDetector = new SwipeDetector(mTouchSlop, mMockListener, SwipeDetector.VERTICAL); 65 mDetector.setDetectableScrollConditions(SwipeDetector.DIRECTION_BOTH, false); 66 mTouchSlop = ViewConfiguration.get(InstrumentationRegistry.getTargetContext()) 67 .getScaledTouchSlop(); 68 L("mTouchSlop=", mTouchSlop); 69 } 70 71 @Test 72 public void testDragStart_vertical() throws Exception { 73 mGenerator.put(0, 100, 100); 74 mGenerator.move(0, 100, 100 + mTouchSlop); 75 // TODO: actually calculate the following parameters and do exact value checks. 76 verify(mMockListener).onDragStart(anyBoolean()); 77 } 78 79 @Test 80 public void testDragStart_failed() throws Exception { 81 mGenerator.put(0, 100, 100); 82 mGenerator.move(0, 100 + mTouchSlop, 100); 83 // TODO: actually calculate the following parameters and do exact value checks. 84 verify(mMockListener, never()).onDragStart(anyBoolean()); 85 } 86 87 @Test 88 public void testDragStart_horizontal() throws Exception { 89 mDetector = new SwipeDetector(mTouchSlop, mMockListener, SwipeDetector.HORIZONTAL); 90 mDetector.setDetectableScrollConditions(SwipeDetector.DIRECTION_BOTH, false); 91 92 mGenerator.put(0, 100, 100); 93 mGenerator.move(0, 100 + mTouchSlop, 100); 94 // TODO: actually calculate the following parameters and do exact value checks. 95 verify(mMockListener).onDragStart(anyBoolean()); 96 } 97 98 @Test 99 public void testDrag() throws Exception { 100 mGenerator.put(0, 100, 100); 101 mGenerator.move(0, 100, 100 + mTouchSlop); 102 // TODO: actually calculate the following parameters and do exact value checks. 103 verify(mMockListener).onDrag(anyFloat(), anyFloat()); 104 } 105 106 @Test 107 public void testDragEnd() throws Exception { 108 mGenerator.put(0, 100, 100); 109 mGenerator.move(0, 100, 100 + mTouchSlop); 110 mGenerator.move(0, 100, 100 + mTouchSlop * 2); 111 mGenerator.lift(0); 112 // TODO: actually calculate the following parameters and do exact value checks. 113 verify(mMockListener).onDragEnd(anyFloat(), anyBoolean()); 114 } 115} 116