@SmallTest @Test public void mouseDragUsingTouchListener() throws Throwable { final DragStartListener listener = createListener(true); final DragStartHelper helper = createDragStartHelper(listener); mDragSource.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { helper.onTouch(view, motionEvent); return true; } }); sendMouseEvent(MotionEvent.ACTION_DOWN, MotionEvent.BUTTON_PRIMARY, mDragSource, 0, 0); sendMouseEvent(MotionEvent.ACTION_MOVE, MotionEvent.BUTTON_PRIMARY, mDragSource, 1, 2); sendMouseEvent(MotionEvent.ACTION_MOVE, MotionEvent.BUTTON_PRIMARY, mDragSource, 3, 4); sendMouseEvent(MotionEvent.ACTION_MOVE, MotionEvent.BUTTON_PRIMARY, mDragSource, 5, 6); verify(listener, times(1)).onDragStart( eq(mDragSource), eq(helper), argThat(new TouchPositionMatcher(mDragSource, 1, 2))); verifyNoMoreInteractions(listener); }
public boolean onTouch(View v, MotionEvent event) { final int x = (int) event.getX(); final int y = (int) event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mLastTouchX = x; mLastTouchY = y; break; case MotionEvent.ACTION_MOVE: if (!MotionEventCompat.isFromSource(event, InputDeviceCompat.SOURCE_MOUSE) || (event.getButtonState() & MotionEvent.BUTTON_PRIMARY) == 0) { break; } if (mDragging) { break; } if (mLastTouchX == x && mLastTouchY == y) { break; } mLastTouchX = x; mLastTouchY = y; mDragging = mListener.onDragStart(v, this); return mDragging; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: mDragging = false; break; } return false; }
DragStartHelper { public boolean onTouch(View v, MotionEvent event) { final int x = (int) event.getX(); final int y = (int) event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mLastTouchX = x; mLastTouchY = y; break; case MotionEvent.ACTION_MOVE: if (!MotionEventCompat.isFromSource(event, InputDeviceCompat.SOURCE_MOUSE) || (event.getButtonState() & MotionEvent.BUTTON_PRIMARY) == 0) { break; } if (mDragging) { break; } if (mLastTouchX == x && mLastTouchY == y) { break; } mLastTouchX = x; mLastTouchY = y; mDragging = mListener.onDragStart(v, this); return mDragging; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: mDragging = false; break; } return false; } }
DragStartHelper { public boolean onTouch(View v, MotionEvent event) { final int x = (int) event.getX(); final int y = (int) event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mLastTouchX = x; mLastTouchY = y; break; case MotionEvent.ACTION_MOVE: if (!MotionEventCompat.isFromSource(event, InputDeviceCompat.SOURCE_MOUSE) || (event.getButtonState() & MotionEvent.BUTTON_PRIMARY) == 0) { break; } if (mDragging) { break; } if (mLastTouchX == x && mLastTouchY == y) { break; } mLastTouchX = x; mLastTouchY = y; mDragging = mListener.onDragStart(v, this); return mDragging; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: mDragging = false; break; } return false; } DragStartHelper(View view, OnDragStartListener listener); }
DragStartHelper { public boolean onTouch(View v, MotionEvent event) { final int x = (int) event.getX(); final int y = (int) event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mLastTouchX = x; mLastTouchY = y; break; case MotionEvent.ACTION_MOVE: if (!MotionEventCompat.isFromSource(event, InputDeviceCompat.SOURCE_MOUSE) || (event.getButtonState() & MotionEvent.BUTTON_PRIMARY) == 0) { break; } if (mDragging) { break; } if (mLastTouchX == x && mLastTouchY == y) { break; } mLastTouchX = x; mLastTouchY = y; mDragging = mListener.onDragStart(v, this); return mDragging; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: mDragging = false; break; } return false; } DragStartHelper(View view, OnDragStartListener listener); void attach(); void detach(); boolean onTouch(View v, MotionEvent event); boolean onLongClick(View v); void getTouchPosition(Point point); }
DragStartHelper { public boolean onTouch(View v, MotionEvent event) { final int x = (int) event.getX(); final int y = (int) event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mLastTouchX = x; mLastTouchY = y; break; case MotionEvent.ACTION_MOVE: if (!MotionEventCompat.isFromSource(event, InputDeviceCompat.SOURCE_MOUSE) || (event.getButtonState() & MotionEvent.BUTTON_PRIMARY) == 0) { break; } if (mDragging) { break; } if (mLastTouchX == x && mLastTouchY == y) { break; } mLastTouchX = x; mLastTouchY = y; mDragging = mListener.onDragStart(v, this); return mDragging; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: mDragging = false; break; } return false; } DragStartHelper(View view, OnDragStartListener listener); void attach(); void detach(); boolean onTouch(View v, MotionEvent event); boolean onLongClick(View v); void getTouchPosition(Point point);  }