Add tests to SwipeDetector (formerly VerticalPullDetector).

Change-Id: I09ab4f22d7204ad806825ab0d6374c2b9616bf39
diff --git a/src/com/android/launcher3/allapps/AllAppsCaretController.java b/src/com/android/launcher3/allapps/AllAppsCaretController.java
index 622322b..583b49f 100644
--- a/src/com/android/launcher3/allapps/AllAppsCaretController.java
+++ b/src/com/android/launcher3/allapps/AllAppsCaretController.java
@@ -22,13 +22,14 @@
 import com.android.launcher3.Launcher;
 import com.android.launcher3.R;
 import com.android.launcher3.pageindicators.CaretDrawable;
+import com.android.launcher3.touch.SwipeDetector;
 
 public class AllAppsCaretController {
     // Determines when the caret should flip. Should be accessed via getThreshold()
     private static final float CARET_THRESHOLD = 0.015f;
     private static final float CARET_THRESHOLD_LAND = 0.5f;
     // The velocity at which the caret will peak (i.e. exhibit a 90 degree bend)
-    private static final float PEAK_VELOCITY = VerticalPullDetector.RELEASE_VELOCITY_PX_MS * .7f;
+    private static final float PEAK_VELOCITY = SwipeDetector.RELEASE_VELOCITY_PX_MS * .7f;
 
     private Launcher mLauncher;
 
diff --git a/src/com/android/launcher3/allapps/AllAppsRecyclerView.java b/src/com/android/launcher3/allapps/AllAppsRecyclerView.java
index fb785fb..75dd760 100644
--- a/src/com/android/launcher3/allapps/AllAppsRecyclerView.java
+++ b/src/com/android/launcher3/allapps/AllAppsRecyclerView.java
@@ -34,6 +34,7 @@
 import com.android.launcher3.anim.SpringAnimationHandler;
 import com.android.launcher3.config.FeatureFlags;
 import com.android.launcher3.graphics.DrawableFactory;
+import com.android.launcher3.touch.SwipeDetector;
 import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
 
 import java.util.List;
@@ -57,7 +58,7 @@
 
     private SpringAnimationHandler mSpringAnimationHandler;
     private OverScrollHelper mOverScrollHelper;
-    private VerticalPullDetector mPullDetector;
+    private SwipeDetector mPullDetector;
 
     private float mContentTranslationY = 0;
     public static final Property<AllAppsRecyclerView, Float> CONTENT_TRANS_Y =
@@ -94,9 +95,9 @@
                 R.dimen.all_apps_empty_search_bg_top_offset);
 
         mOverScrollHelper = new OverScrollHelper();
-        mPullDetector = new VerticalPullDetector(getContext());
+        mPullDetector = new SwipeDetector(getContext());
         mPullDetector.setListener(mOverScrollHelper);
-        mPullDetector.setDetectableScrollConditions(VerticalPullDetector.DIRECTION_BOTH, true);
+        mPullDetector.setDetectableScrollConditions(SwipeDetector.DIRECTION_BOTH, true);
     }
 
     public void setSpringAnimationHandler(SpringAnimationHandler springAnimationHandler) {
@@ -479,7 +480,7 @@
                 y + mEmptySearchBackground.getIntrinsicHeight());
     }
 
-    private class OverScrollHelper implements VerticalPullDetector.Listener {
+    private class OverScrollHelper implements SwipeDetector.Listener {
 
         private static final float MAX_RELEASE_VELOCITY = 5000; // px / s
         private static final float MAX_OVERSCROLL_PERCENTAGE = 0.07f;
diff --git a/src/com/android/launcher3/allapps/AllAppsTransitionController.java b/src/com/android/launcher3/allapps/AllAppsTransitionController.java
index 0859e06..ecb9724 100644
--- a/src/com/android/launcher3/allapps/AllAppsTransitionController.java
+++ b/src/com/android/launcher3/allapps/AllAppsTransitionController.java
@@ -26,6 +26,7 @@
 import com.android.launcher3.config.FeatureFlags;
 import com.android.launcher3.graphics.GradientView;
 import com.android.launcher3.graphics.ScrimView;
+import com.android.launcher3.touch.SwipeDetector;
 import com.android.launcher3.userevent.nano.LauncherLogProto.Action;
 import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType;
 import com.android.launcher3.util.SystemUiController;
@@ -42,7 +43,7 @@
  * If release velocity < THRES1, snap according to either top or bottom depending on whether it's
  * closer to top or closer to the page indicator.
  */
-public class AllAppsTransitionController implements TouchController, VerticalPullDetector.Listener,
+public class AllAppsTransitionController implements TouchController, SwipeDetector.Listener,
          SearchUiManager.OnScrollRangeChangeListener {
 
     private static final String TAG = "AllAppsTrans";
@@ -52,8 +53,8 @@
     private final Interpolator mHotseatAccelInterpolator = new AccelerateInterpolator(1.5f);
     private final Interpolator mDecelInterpolator = new DecelerateInterpolator(3f);
     private final Interpolator mFastOutSlowInInterpolator = new FastOutSlowInInterpolator();
-    private final VerticalPullDetector.ScrollInterpolator mScrollInterpolator
-            = new VerticalPullDetector.ScrollInterpolator();
+    private final SwipeDetector.ScrollInterpolator mScrollInterpolator
+            = new SwipeDetector.ScrollInterpolator();
 
     private static final float PARALLAX_COEFFICIENT = .125f;
     private static final int SINGLE_FRAME_MS = 16;
@@ -69,7 +70,7 @@
     private float mStatusBarHeight;
 
     private final Launcher mLauncher;
-    private final VerticalPullDetector mDetector;
+    private final SwipeDetector mDetector;
     private final ArgbEvaluator mEvaluator;
     private final boolean mIsDarkTheme;
 
@@ -106,7 +107,7 @@
 
     public AllAppsTransitionController(Launcher l) {
         mLauncher = l;
-        mDetector = new VerticalPullDetector(l);
+        mDetector = new SwipeDetector(l);
         mDetector.setListener(this);
         mShiftRange = DEFAULT_SHIFT_RANGE;
         mProgress = 1f;
@@ -136,17 +137,17 @@
 
                 if (mDetector.isIdleState()) {
                     if (mLauncher.isAllAppsVisible()) {
-                        directionsToDetectScroll |= VerticalPullDetector.DIRECTION_DOWN;
+                        directionsToDetectScroll |= SwipeDetector.DIRECTION_DOWN;
                     } else {
-                        directionsToDetectScroll |= VerticalPullDetector.DIRECTION_UP;
+                        directionsToDetectScroll |= SwipeDetector.DIRECTION_UP;
                     }
                 } else {
                     if (isInDisallowRecatchBottomZone()) {
-                        directionsToDetectScroll |= VerticalPullDetector.DIRECTION_UP;
+                        directionsToDetectScroll |= SwipeDetector.DIRECTION_UP;
                     } else if (isInDisallowRecatchTopZone()) {
-                        directionsToDetectScroll |= VerticalPullDetector.DIRECTION_DOWN;
+                        directionsToDetectScroll |= SwipeDetector.DIRECTION_DOWN;
                     } else {
-                        directionsToDetectScroll |= VerticalPullDetector.DIRECTION_BOTH;
+                        directionsToDetectScroll |= SwipeDetector.DIRECTION_BOTH;
                         ignoreSlopWhenSettling = true;
                     }
                 }
diff --git a/src/com/android/launcher3/allapps/VerticalPullDetector.java b/src/com/android/launcher3/touch/SwipeDetector.java
similarity index 95%
rename from src/com/android/launcher3/allapps/VerticalPullDetector.java
rename to src/com/android/launcher3/touch/SwipeDetector.java
index 13c4f63..b470654 100644
--- a/src/com/android/launcher3/allapps/VerticalPullDetector.java
+++ b/src/com/android/launcher3/touch/SwipeDetector.java
@@ -1,4 +1,4 @@
-package com.android.launcher3.allapps;
+package com.android.launcher3.touch;
 
 import android.content.Context;
 import android.util.Log;
@@ -7,15 +7,12 @@
 import android.view.animation.Interpolator;
 
 /**
- * One dimensional scroll gesture detector for all apps container pull up interaction.
- * Client (e.g., AllAppsTransitionController) of this class can register a listener.
- * <p/>
- * Features that this gesture detector can support.
+ * One dimensional scroll/drag/swipe gesture detector.
  */
-public class VerticalPullDetector {
+public class SwipeDetector {
 
     private static final boolean DBG = false;
-    private static final String TAG = "VerticalPullDetector";
+    private static final String TAG = "SwipeDetector";
 
     private final float mTouchSlop;
 
@@ -122,7 +119,7 @@
         void onDragEnd(float velocity, boolean fling);
     }
 
-    public VerticalPullDetector(Context context) {
+    public SwipeDetector(Context context) {
         mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
     }
 
diff --git a/src/com/android/launcher3/widget/WidgetsBottomSheet.java b/src/com/android/launcher3/widget/WidgetsBottomSheet.java
index a754375..99e6056 100644
--- a/src/com/android/launcher3/widget/WidgetsBottomSheet.java
+++ b/src/com/android/launcher3/widget/WidgetsBottomSheet.java
@@ -40,7 +40,7 @@
 import com.android.launcher3.LauncherAppState;
 import com.android.launcher3.R;
 import com.android.launcher3.Utilities;
-import com.android.launcher3.allapps.VerticalPullDetector;
+import com.android.launcher3.touch.SwipeDetector;
 import com.android.launcher3.anim.PropertyListBuilder;
 import com.android.launcher3.dragndrop.DragController;
 import com.android.launcher3.dragndrop.DragOptions;
@@ -58,7 +58,7 @@
  * Bottom sheet for the "Widgets" system shortcut in the long-press popup.
  */
 public class WidgetsBottomSheet extends AbstractFloatingView implements Insettable, TouchController,
-        VerticalPullDetector.Listener, View.OnClickListener, View.OnLongClickListener,
+        SwipeDetector.Listener, View.OnClickListener, View.OnLongClickListener,
         DragController.DragListener {
 
     private int mTranslationYOpen;
@@ -69,9 +69,9 @@
     private ItemInfo mOriginalItemInfo;
     private ObjectAnimator mOpenCloseAnimator;
     private Interpolator mFastOutSlowInInterpolator;
-    private VerticalPullDetector.ScrollInterpolator mScrollInterpolator;
+    private SwipeDetector.ScrollInterpolator mScrollInterpolator;
     private Rect mInsets;
-    private VerticalPullDetector mVerticalPullDetector;
+    private SwipeDetector mSwipeDetector;
     private GradientView mGradientBackground;
 
     public WidgetsBottomSheet(Context context, AttributeSet attrs) {
@@ -85,10 +85,10 @@
         mOpenCloseAnimator = LauncherAnimUtils.ofPropertyValuesHolder(this);
         mFastOutSlowInInterpolator =
                 AnimationUtils.loadInterpolator(context, android.R.interpolator.fast_out_slow_in);
-        mScrollInterpolator = new VerticalPullDetector.ScrollInterpolator();
+        mScrollInterpolator = new SwipeDetector.ScrollInterpolator();
         mInsets = new Rect();
-        mVerticalPullDetector = new VerticalPullDetector(context);
-        mVerticalPullDetector.setListener(this);
+        mSwipeDetector = new SwipeDetector(context);
+        mSwipeDetector.setListener(this);
         mGradientBackground = (GradientView) mLauncher.findViewById(R.id.gradient_bg);
     }
 
@@ -192,7 +192,7 @@
             mOpenCloseAnimator.addListener(new AnimatorListenerAdapter() {
                 @Override
                 public void onAnimationEnd(Animator animation) {
-                    mVerticalPullDetector.finishedScrolling();
+                    mSwipeDetector.finishedScrolling();
                 }
             });
             mOpenCloseAnimator.setInterpolator(mFastOutSlowInInterpolator);
@@ -214,13 +214,13 @@
                 @Override
                 public void onAnimationEnd(Animator animation) {
                     mIsOpen = false;
-                    mVerticalPullDetector.finishedScrolling();
+                    mSwipeDetector.finishedScrolling();
                     ((ViewGroup) getParent()).removeView(WidgetsBottomSheet.this);
                     mLauncher.getSystemUiController().updateUiState(
                             SystemUiController.UI_STATE_WIDGET_BOTTOM_SHEET, 0);
                 }
             });
-            mOpenCloseAnimator.setInterpolator(mVerticalPullDetector.isIdleState()
+            mOpenCloseAnimator.setInterpolator(mSwipeDetector.isIdleState()
                     ? mFastOutSlowInInterpolator : mScrollInterpolator);
             mOpenCloseAnimator.start();
         } else {
@@ -259,7 +259,7 @@
                 getPaddingRight() + rightInset, getPaddingBottom() + bottomInset);
     }
 
-    /* VerticalPullDetector.Listener */
+    /* SwipeDetector.Listener */
 
     @Override
     public void onDragStart(boolean start) {
@@ -283,12 +283,12 @@
     public void onDragEnd(float velocity, boolean fling) {
         if ((fling && velocity > 0) || getTranslationY() > (mTranslationYRange) / 2) {
             mScrollInterpolator.setVelocityAtZero(velocity);
-            mOpenCloseAnimator.setDuration(mVerticalPullDetector.calculateDuration(velocity,
+            mOpenCloseAnimator.setDuration(mSwipeDetector.calculateDuration(velocity,
                     (mTranslationYClosed - getTranslationY()) / mTranslationYRange));
             close(true);
         } else {
             mIsOpen = false;
-            mOpenCloseAnimator.setDuration(mVerticalPullDetector.calculateDuration(velocity,
+            mOpenCloseAnimator.setDuration(mSwipeDetector.calculateDuration(velocity,
                     (getTranslationY() - mTranslationYOpen) / mTranslationYRange));
             open(true);
         }
@@ -296,17 +296,17 @@
 
     @Override
     public boolean onControllerTouchEvent(MotionEvent ev) {
-        return mVerticalPullDetector.onTouchEvent(ev);
+        return mSwipeDetector.onTouchEvent(ev);
     }
 
     @Override
     public boolean onControllerInterceptTouchEvent(MotionEvent ev) {
-        int directionsToDetectScroll = mVerticalPullDetector.isIdleState() ?
-                VerticalPullDetector.DIRECTION_DOWN : 0;
-        mVerticalPullDetector.setDetectableScrollConditions(
+        int directionsToDetectScroll = mSwipeDetector.isIdleState() ?
+                SwipeDetector.DIRECTION_DOWN : 0;
+        mSwipeDetector.setDetectableScrollConditions(
                 directionsToDetectScroll, false);
-        mVerticalPullDetector.onTouchEvent(ev);
-        return mVerticalPullDetector.isDraggingOrSettling();
+        mSwipeDetector.onTouchEvent(ev);
+        return mSwipeDetector.isDraggingOrSettling();
     }
 
     /* DragListener */
diff --git a/tests/src/com/android/launcher3/testcomponent/TouchEventGenerator.java b/tests/src/com/android/launcher3/testcomponent/TouchEventGenerator.java
new file mode 100644
index 0000000..80d6341
--- /dev/null
+++ b/tests/src/com/android/launcher3/testcomponent/TouchEventGenerator.java
@@ -0,0 +1,275 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.launcher3.testcomponent;
+
+import android.graphics.Point;
+import android.util.Pair;
+import android.view.InputDevice;
+import android.view.MotionEvent;
+import android.view.MotionEvent.PointerCoords;
+import android.view.MotionEvent.PointerProperties;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Utility class to generate MotionEvent event sequences for testing touch gesture detectors.
+ */
+public class TouchEventGenerator {
+
+    /**
+     * Amount of time between two generated events.
+     */
+    private static final long TIME_INCREMENT_MS = 20L;
+
+    /**
+     * Id of the fake device generating the events.
+     */
+    private static final int DEVICE_ID = 2104;
+
+    /**
+     * The fingers currently present on the emulated touch screen.
+     */
+    private Map<Integer, Point> mFingers;
+
+    /**
+     * Initial event time for the current sequence.
+     */
+    private long mInitialTime;
+
+    /**
+     * Time of the last generated event.
+     */
+    private long mLastEventTime;
+
+    /**
+     * Time of the next event.
+     */
+    private long mTime;
+
+    /**
+     * Receives the generated events.
+     */
+    public interface Listener {
+
+        /**
+         * Called when an event was generated.
+         */
+        void onTouchEvent(MotionEvent event);
+    }
+    private final Listener mListener;
+
+    public TouchEventGenerator(Listener listener) {
+        mListener = listener;
+        mFingers = new HashMap<Integer, Point>();
+    }
+
+    /**
+     * Adds a finger on the touchscreen.
+     */
+    public TouchEventGenerator put(int id, int x, int y, long ms) {
+        checkFingerExistence(id, false);
+        boolean isInitialDown = mFingers.isEmpty();
+        mFingers.put(id, new Point(x, y));
+        int action;
+        if (isInitialDown) {
+            action = MotionEvent.ACTION_DOWN;
+        } else {
+            action = MotionEvent.ACTION_POINTER_DOWN;
+            // Set the id of the changed pointer.
+            action |= id << MotionEvent.ACTION_POINTER_INDEX_SHIFT;
+        }
+        generateEvent(action, ms);
+        return this;
+    }
+
+    /**
+     * Adds a finger on the touchscreen after advancing default time interval.
+     */
+    public TouchEventGenerator put(int id, int x, int y) {
+        return put(id, x, y, TIME_INCREMENT_MS);
+    }
+
+    /**
+     * Adjusts the position of a finger for an upcoming move event.
+     *
+     * @see #move(long ms)
+     */
+    public TouchEventGenerator position(int id, int x, int y) {
+        checkFingerExistence(id, true);
+        mFingers.get(id).set(x, y);
+        return this;
+    }
+
+    /**
+     * Commits the finger position changes of {@link #position(int, int, int)} by generating a move
+     * event.
+     *
+     * @see #position(int, int, int)
+     */
+    public TouchEventGenerator move(long ms) {
+        generateEvent(MotionEvent.ACTION_MOVE, ms);
+        return this;
+    }
+
+    /**
+     * Commits the finger position changes of {@link #position(int, int, int)} by generating a move
+     * event after advancing the default time interval.
+     *
+     * @see #position(int, int, int)
+     */
+    public TouchEventGenerator move() {
+        return move(TIME_INCREMENT_MS);
+    }
+
+    /**
+     * Moves a single finger on the touchscreen.
+     */
+    public TouchEventGenerator move(int id, int x, int y, long ms) {
+        return position(id, x, y).move(ms);
+    }
+
+    /**
+     * Moves a single finger on the touchscreen after advancing default time interval.
+     */
+    public TouchEventGenerator move(int id, int x, int y) {
+        return move(id, x, y, TIME_INCREMENT_MS);
+    }
+
+    /**
+     * Removes an existing finger from the touchscreen.
+     */
+    public TouchEventGenerator lift(int id, long ms) {
+        checkFingerExistence(id, true);
+        boolean isFinalUp = mFingers.size() == 1;
+        int action;
+        if (isFinalUp) {
+            action = MotionEvent.ACTION_UP;
+        } else {
+            action = MotionEvent.ACTION_POINTER_UP;
+            // Set the id of the changed pointer.
+            action |= id << MotionEvent.ACTION_POINTER_INDEX_SHIFT;
+        }
+        generateEvent(action, ms);
+        mFingers.remove(id);
+        return this;
+    }
+
+    /**
+     * Removes a finger from the touchscreen.
+     */
+    public TouchEventGenerator lift(int id, int x, int y, long ms) {
+        checkFingerExistence(id, true);
+        mFingers.get(id).set(x, y);
+        return lift(id, ms);
+    }
+
+    /**
+     * Removes an existing finger from the touchscreen after advancing default time interval.
+     */
+    public TouchEventGenerator lift(int id) {
+        return lift(id, TIME_INCREMENT_MS);
+    }
+
+    /**
+     * Cancels an ongoing sequence.
+     */
+    public TouchEventGenerator cancel(long ms) {
+        generateEvent(MotionEvent.ACTION_CANCEL, ms);
+        mFingers.clear();
+        return this;
+    }
+
+    /**
+     * Cancels an ongoing sequence.
+     */
+    public TouchEventGenerator cancel() {
+        return cancel(TIME_INCREMENT_MS);
+    }
+
+    private void checkFingerExistence(int id, boolean shouldExist) {
+        if (shouldExist != mFingers.containsKey(id)) {
+            throw new IllegalArgumentException(
+                    shouldExist ? "Finger does not exist" : "Finger already exists");
+        }
+    }
+
+    private void generateEvent(int action, long ms) {
+        mTime = mLastEventTime + ms;
+        Pair<PointerProperties[], PointerCoords[]> state = getFingerState();
+        MotionEvent event = MotionEvent.obtain(
+                mInitialTime,
+                mTime,
+                action,
+                state.first.length,
+                state.first,
+                state.second,
+                0 /* metaState */,
+                0 /* buttonState */,
+                1.0f /* xPrecision */,
+                1.0f /* yPrecision */,
+                DEVICE_ID,
+                0 /* edgeFlags */,
+                InputDevice.SOURCE_TOUCHSCREEN,
+                0 /* flags */);
+        mListener.onTouchEvent(event);
+        if (action == MotionEvent.ACTION_UP) {
+            resetTime();
+        }
+        event.recycle();
+        mLastEventTime = mTime;
+    }
+
+    /**
+     * Returns the description of the fingers' state expected by MotionEvent.
+     */
+    private Pair<PointerProperties[], PointerCoords[]> getFingerState() {
+        int nFingers = mFingers.size();
+        PointerProperties[] properties = new PointerProperties[nFingers];
+        PointerCoords[] coordinates = new PointerCoords[nFingers];
+
+        int index = 0;
+        for (Map.Entry<Integer, Point> entry : mFingers.entrySet()) {
+            int id = entry.getKey();
+            Point location = entry.getValue();
+
+            PointerProperties property = new PointerProperties();
+            property.id = id;
+            property.toolType = MotionEvent.TOOL_TYPE_FINGER;
+            properties[index] = property;
+
+            PointerCoords coordinate = new PointerCoords();
+            coordinate.x = location.x;
+            coordinate.y = location.y;
+            coordinate.pressure = 1.0f;
+            coordinates[index] = coordinate;
+
+            index++;
+        }
+
+        return new Pair<MotionEvent.PointerProperties[], MotionEvent.PointerCoords[]>(
+                properties, coordinates);
+    }
+
+    /**
+     * Resets the time references for a new sequence.
+     */
+    private void resetTime() {
+        mInitialTime = 0L;
+        mLastEventTime = -1L;
+        mTime = 0L;
+    }
+}
diff --git a/tests/src/com/android/launcher3/touch/SwipeDetectorTest.java b/tests/src/com/android/launcher3/touch/SwipeDetectorTest.java
new file mode 100644
index 0000000..8724704
--- /dev/null
+++ b/tests/src/com/android/launcher3/touch/SwipeDetectorTest.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.launcher3.touch;
+
+import android.content.Context;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.filters.SmallTest;
+import android.support.test.runner.AndroidJUnit4;
+import android.util.Log;
+import android.view.MotionEvent;
+import android.view.ViewConfiguration;
+
+import com.android.launcher3.testcomponent.TouchEventGenerator;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import static org.mockito.Matchers.anyBoolean;
+import static org.mockito.Matchers.anyFloat;
+import static org.mockito.Mockito.verify;
+
+@SmallTest
+@RunWith(AndroidJUnit4.class)
+public class SwipeDetectorTest{
+
+    private static final String TAG = SwipeDetectorTest.class.getSimpleName();
+    public static void L(String s, Object... parts) {
+        Log.d(TAG, (parts.length == 0) ? s : String.format(s, parts));
+    }
+
+    private TouchEventGenerator mGenerator;
+    private SwipeDetector mDetector;
+    private int mTouchSlop;
+
+    @Mock
+    private SwipeDetector.Listener mMockListener;
+
+    @Before
+    public void setup() {
+        MockitoAnnotations.initMocks(this);
+        Context context = InstrumentationRegistry.getTargetContext();
+        mDetector = new SwipeDetector(context);
+        mGenerator = new TouchEventGenerator(new TouchEventGenerator.Listener() {
+            @Override
+            public void onTouchEvent(MotionEvent event) {
+                mDetector.onTouchEvent(event);
+            }
+        });
+        mDetector.setListener(mMockListener);
+        mDetector.setDetectableScrollConditions(SwipeDetector.DIRECTION_BOTH, false);
+        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
+        L("mTouchSlop=", mTouchSlop);
+    }
+
+    @Test
+    public void testDragStart() throws Exception {
+        mGenerator.put(0, 100, 100);
+        mGenerator.move(0, 100, 100 + mTouchSlop);
+        // TODO: actually calculate the following parameters and do exact value checks.
+        verify(mMockListener).onDragStart(anyBoolean());
+    }
+
+    @Test
+    public void testDrag() throws Exception {
+        mGenerator.put(0, 100, 100);
+        mGenerator.move(0, 100, 100 + mTouchSlop);
+        // TODO: actually calculate the following parameters and do exact value checks.
+        verify(mMockListener).onDrag(anyFloat(), anyFloat());
+    }
+
+    @Test
+    public void testDragEnd() throws Exception {
+        mGenerator.put(0, 100, 100);
+        mGenerator.move(0, 100, 100 + mTouchSlop);
+        mGenerator.move(0, 100, 100 + mTouchSlop * 2);
+        mGenerator.lift(0);
+        // TODO: actually calculate the following parameters and do exact value checks.
+        verify(mMockListener).onDragEnd(anyFloat(), anyBoolean());
+    }
+}