Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nhaarman committed Aug 21, 2014
1 parent 0b8777b commit deb573c
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static void dispatchDragScrollDownMotionEvents(final Instrumentation inst
dynamicListView.getLocationOnScreen(location);

View view = dynamicListView.getChildAt(fromPosition);
float fromY = (int) (view.getY() ) + location[1];
float fromY = (int) (view.getY()) + location[1];

View toView = dynamicListView.getChildAt(dynamicListView.getLastVisiblePosition());
float toY = (int) (toView.getY() + toView.getHeight()) + location[1] + 2;
Expand Down Expand Up @@ -90,7 +90,19 @@ public static List<MotionEvent> createMotionEvents(final AbsListView absListView

public static void dispatchMotionEvents(final Instrumentation instrumentation, final Iterable<MotionEvent> motionEvents, final boolean wait) throws InterruptedException {
for (final MotionEvent event : motionEvents) {
instrumentation.sendPointerSync(event);
int i = 0;
boolean success = false;
do {
try {
instrumentation.sendPointerSync(event);
success = true;
} catch (SecurityException e) {
i++;
if (i > 3) {
throw e;
}
}
} while (i < 3 && !success);
Thread.sleep(100);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

package com.nhaarman.listviewanimations.itemmanipulation.swipedismiss;

import android.app.Activity;
import android.app.Instrumentation;
import android.os.SystemClock;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsListView;
Expand All @@ -31,28 +32,29 @@ public class MotionEventUtils {
private MotionEventUtils() {
}

public static void dispatchSwipeMotionEventsAndWait(final Activity activity, final AbsListView absListView, final int position) throws InterruptedException {
dispatchMotionEventsAndWait(activity, absListView, createSwipeMotionEvents(absListView, position));
public static void dispatchSwipeMotionEventsAndWait(final Instrumentation instrumentation, final AbsListView absListView, final int position) throws InterruptedException {
dispatchMotionEventsAndWait(instrumentation, createSwipeMotionEvents(absListView, position));
}

public static void dispatchReverseSwipeMotionEventsAndWait(final Activity activity, final AbsListView absListView, final int position) throws InterruptedException {
dispatchMotionEventsAndWait(activity, absListView, createReverseSwipeMotionEvents(absListView, position));
public static void dispatchReverseSwipeMotionEventsAndWait(final Instrumentation instrumentation, final AbsListView absListView,
final int position) throws InterruptedException {
dispatchMotionEventsAndWait(instrumentation, createReverseSwipeMotionEvents(absListView, position));
}

public static void dispatchMotionEventsAndWait(final Activity activity, final View view, final Iterable<MotionEvent> motionEvents) throws InterruptedException {
dispatchMotionEvents(activity, view, motionEvents, true);
public static void dispatchMotionEventsAndWait(final Instrumentation instrumentation, final Iterable<MotionEvent> motionEvents) throws InterruptedException {
dispatchMotionEvents(instrumentation, motionEvents, true);
}

public static void dispatchSwipeMotionEvents(final Activity activity, final AbsListView absListView, final int position) throws InterruptedException {
dispatchMotionEvents(activity, absListView, createSwipeMotionEvents(absListView, position));
public static void dispatchSwipeMotionEvents(final Instrumentation instrumentation, final AbsListView absListView, final int position) throws InterruptedException {
dispatchMotionEvents(instrumentation, createSwipeMotionEvents(absListView, position));
}

public static void dispatchReverseSwipeMotionEvents(final Activity activity, final AbsListView absListView, final int position) throws InterruptedException {
dispatchMotionEvents(activity, absListView, createReverseSwipeMotionEvents(absListView, position));
public static void dispatchReverseSwipeMotionEvents(final Instrumentation instrumentation, final AbsListView absListView, final int position) throws InterruptedException {
dispatchMotionEvents(instrumentation, createReverseSwipeMotionEvents(absListView, position));
}

public static void dispatchMotionEvents(final Activity activity, final View view, final Iterable<MotionEvent> motionEvents) throws InterruptedException {
dispatchMotionEvents(activity, view, motionEvents, false);
public static void dispatchMotionEvents(final Instrumentation instrumentation, final Iterable<MotionEvent> motionEvents) throws InterruptedException {
dispatchMotionEvents(instrumentation, motionEvents, false);
}

public static List<MotionEvent> createSwipeMotionEvents(final AbsListView absListView, final int position) {
Expand All @@ -73,21 +75,30 @@ public static List<MotionEvent> createMotionEvents(final AbsListView absListView
int y = (int) (ViewHelper.getY(view) + view.getHeight() / 2) + listViewCoords[1];

List<MotionEvent> results = new ArrayList<>();
results.add(MotionEvent.obtain(0, 0, MotionEvent.ACTION_DOWN, fromX, y, 0));
results.add(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, fromX, y, 0));

float diff = fromX - toX;
for (int i = 1; i < 10; i++) {
float x = fromX + diff / 10 * i;
results.add(MotionEvent.obtain(0, 0, MotionEvent.ACTION_MOVE, x, y, 0));
results.add(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE, x, y, 0));
}
results.add(MotionEvent.obtain(0, 0, MotionEvent.ACTION_UP, toX, y, 0));
results.add(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, toX, y, 0));

return results;
}

private static void dispatchMotionEvents(final Activity activity, final View view, final Iterable<MotionEvent> motionEvents, final boolean wait) throws InterruptedException {
public static void dispatchMotionEvents(final Instrumentation instrumentation, final Iterable<MotionEvent> motionEvents, final boolean wait) throws InterruptedException {
for (final MotionEvent event : motionEvents) {
activity.runOnUiThread(new DispatchTouchEventRunnable(event, view));
int i = 0;
boolean success = false;
do {
try {
instrumentation.sendPointerSync(event);
success = true;
} catch (SecurityException ignored) {
i++;
}
} while (i < 3 && !success);
Thread.sleep(100);
}

Expand All @@ -97,21 +108,4 @@ private static void dispatchMotionEvents(final Activity activity, final View vie
}
}

private static class DispatchTouchEventRunnable implements Runnable {

private final MotionEvent mEvent;

private final View mView;

private DispatchTouchEventRunnable(final MotionEvent event, final View view) {
mEvent = event;
mView = view;
}

@Override
public void run() {
mView.dispatchTouchEvent(mEvent);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void setUp() throws Exception {
* Tests whether dismissing an item triggers a call to OnDismissCallback#onDismiss.
*/
public void testSimpleDismiss() throws InterruptedException {
dispatchSwipeMotionEventsAndWait(mActivity, mAbsListView, 0);
dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, 0);

verify(mOnDismissCallback).onDismiss(eq(mAbsListView), aryEq(new int[]{0}));
}
Expand All @@ -83,8 +83,8 @@ public void testSimpleDismiss() throws InterruptedException {
* Tests whether dismissing the first and second items triggers a correct call to OnDismissCallback#onDismiss.
*/
public void testDoubleDismiss() throws InterruptedException {
dispatchSwipeMotionEvents(mActivity, mAbsListView, 0);
dispatchSwipeMotionEventsAndWait(mActivity, mAbsListView, 1);
dispatchSwipeMotionEvents(getInstrumentation(), mAbsListView, 0);
dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, 1);

verify(mOnDismissCallback).onDismiss(eq(mAbsListView), aryEq(new int[]{1, 0}));
}
Expand All @@ -93,9 +93,9 @@ public void testDoubleDismiss() throws InterruptedException {
* Tests whether dismissing mixed positions triggers a correct call to OnDismissCallback#onDismiss.
*/
public void testComplexDismiss() throws InterruptedException {
dispatchSwipeMotionEvents(mActivity, mAbsListView, 0);
dispatchSwipeMotionEvents(mActivity, mAbsListView, 3);
dispatchSwipeMotionEventsAndWait(mActivity, mAbsListView, 2);
dispatchSwipeMotionEvents(getInstrumentation(), mAbsListView, 0);
dispatchSwipeMotionEvents(getInstrumentation(), mAbsListView, 3);
dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, 2);

verify(mOnDismissCallback).onDismiss(eq(mAbsListView), aryEq(new int[]{3, 2, 0}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void testAbsListViewSet() {
* Tests whether swiping the first View triggers a call to SwipeTouchListener#afterViewFling.
*/
public void testSwipeFirstViewCallback() throws InterruptedException {
dispatchSwipeMotionEventsAndWait(mActivity, mAbsListView, 0);
dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, 0);

assertThat(mSwipeTouchListener.afterViewFlingCalled, is(true));
assertThat(mSwipeTouchListener.position, is(0));
Expand All @@ -95,7 +95,7 @@ public void testSwipeFirstViewCallback() throws InterruptedException {
* Tests whether swiping the first View from right to left triggers a call to SwipeTouchListener#afterViewFling.
*/
public void testReverseSwipeFirstViewCallback() throws InterruptedException {
MotionEventUtils.dispatchReverseSwipeMotionEventsAndWait(mActivity, mAbsListView, 0);
MotionEventUtils.dispatchReverseSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, 0);

assertThat(mSwipeTouchListener.afterViewFlingCalled, is(true));
assertThat(mSwipeTouchListener.position, is(0));
Expand All @@ -105,18 +105,18 @@ public void testReverseSwipeFirstViewCallback() throws InterruptedException {
* Tests whether swiping the last View triggers a call to SwipeTouchListener#afterViewFling.
*/
public void testSwipeLastViewCallback() throws InterruptedException {
dispatchSwipeMotionEventsAndWait(mActivity, mAbsListView, mAbsListView.getLastVisiblePosition());
dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, mAbsListView.getLastVisiblePosition() - 1);

assertThat(mSwipeTouchListener.afterViewFlingCalled, is(true));
assertThat(mSwipeTouchListener.position, is(mAbsListView.getLastVisiblePosition()));
assertThat(mSwipeTouchListener.position, is(mAbsListView.getLastVisiblePosition() - 1));
}

/**
* Tests whether swiping shorter than half of the view width doesn't trigger a call to SwipeTouchLister#afterViewFling.
*/
public void testShortSwipe() throws InterruptedException {
List<MotionEvent> motionEvents = MotionEventUtils.createMotionEvents(mAbsListView, 0, 10, mViewWidth / 2 - mViewWidth / 10);
MotionEventUtils.dispatchMotionEventsAndWait(mActivity, mAbsListView, motionEvents);
MotionEventUtils.dispatchMotionEventsAndWait(getInstrumentation(), motionEvents);

assertThat(mSwipeTouchListener.afterViewFlingCalled, is(false));
}
Expand All @@ -126,7 +126,7 @@ public void testShortSwipe() throws InterruptedException {
*/
public void testReverseShortSwipe() throws InterruptedException {
List<MotionEvent> motionEvents = MotionEventUtils.createMotionEvents(mAbsListView, 0, mViewWidth - 10, mViewWidth / 2 + mViewWidth / 10);
MotionEventUtils.dispatchMotionEventsAndWait(mActivity, mAbsListView, motionEvents);
MotionEventUtils.dispatchMotionEventsAndWait(getInstrumentation(), motionEvents);

assertThat(mSwipeTouchListener.afterViewFlingCalled, is(false));
}
Expand Down Expand Up @@ -166,7 +166,7 @@ public boolean isDismissable(final long id, final int position) {
);

List<MotionEvent> motionEvents = MotionEventUtils.createMotionEvents(mAbsListView, 0, 10, mViewWidth - 10);
MotionEventUtils.dispatchMotionEvents(mActivity, mAbsListView, motionEvents);
MotionEventUtils.dispatchMotionEvents(getInstrumentation(), motionEvents);

assertThat(mSwipeTouchListener.afterViewFlingCalled, is(false));
}
Expand All @@ -180,12 +180,12 @@ public void testIsSwiping() throws InterruptedException {
assertThat(mSwipeTouchListener.isSwiping(), is(false));

/* Send first half of the MotionEvents */
MotionEventUtils.dispatchMotionEvents(mActivity, mAbsListView, motionEvents.subList(0, motionEvents.size() / 2));
MotionEventUtils.dispatchMotionEvents(getInstrumentation(), motionEvents.subList(0, motionEvents.size() / 2));

assertThat(mSwipeTouchListener.isSwiping(), is(true));

/* Send second half of the MotionEvents */
MotionEventUtils.dispatchMotionEvents(mActivity, mAbsListView, motionEvents.subList(motionEvents.size() / 2, motionEvents.size()));
MotionEventUtils.dispatchMotionEvents(getInstrumentation(), motionEvents.subList(motionEvents.size() / 2, motionEvents.size()));

assertThat(mSwipeTouchListener.isSwiping(), is(false));
}
Expand All @@ -196,20 +196,21 @@ public void testIsSwiping() throws InterruptedException {
public void testEnableDisableSwipe() throws InterruptedException {
mSwipeTouchListener.disableSwipe();

dispatchSwipeMotionEventsAndWait(mActivity, mAbsListView, 0);
dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, 0);

assertThat(mSwipeTouchListener.afterViewFlingCalled, is(false));

mSwipeTouchListener.enableSwipe();

dispatchSwipeMotionEventsAndWait(mActivity, mAbsListView, 0);
dispatchSwipeMotionEventsAndWait(getInstrumentation(), mAbsListView, 0);

assertThat(mSwipeTouchListener.afterViewFlingCalled, is(true));
}

private static class TestSwipeTouchListener extends SwipeTouchListener {

boolean afterViewFlingCalled;

int position;

TestSwipeTouchListener(final AbsListViewWrapper absListViewWrapper) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Debug;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
Expand Down Expand Up @@ -56,6 +58,22 @@ protected void onCreate(final Bundle savedInstanceState) {
setContentView(mListView);
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
int[] location = new int[2];
mListView.getLocationOnScreen(location);

ev = MotionEvent.obtain(ev.getDownTime(), ev.getEventTime(), ev.getAction(), ev.getX() - location[0], ev.getY() - location[1], ev.getMetaState());
boolean handled = mListView.onInterceptTouchEvent(ev);
if (!handled) {
handled = mListView.dispatchTouchEvent(ev);
}
if (!handled) {
handled = onTouchEvent(ev);
}
return handled;
}

public AbsListView getAbsListView() {
return mListView;
}
Expand Down
Loading

0 comments on commit deb573c

Please sign in to comment.