Permalink
Browse files

added ShadowGestureDetector

  • Loading branch information...
1 parent 0a7ac38 commit ae3f0eadd427fa097ea0efba1c53cd5a1a509667 Ryan Richard and Sarah Chandler committed Oct 11, 2012
@@ -1,15 +1,5 @@
package com.xtremelabs.robolectric;
-import java.lang.reflect.Field;
-import java.lang.reflect.Modifier;
-import java.util.Arrays;
-import java.util.List;
-
-import org.apache.http.Header;
-import org.apache.http.HttpRequest;
-import org.apache.http.HttpResponse;
-import org.apache.http.impl.client.DefaultRequestDirector;
-
import android.accounts.AccountManager;
import android.app.*;
import android.appwidget.AppWidgetManager;
@@ -62,15 +52,23 @@
import android.view.inputmethod.InputMethodManager;
import android.webkit.*;
import android.widget.*;
-
+import com.xtremelabs.robolectric.bytecode.DirectCallPolicy.FullStackDirectCallPolicy;
import com.xtremelabs.robolectric.bytecode.RobolectricInternals;
import com.xtremelabs.robolectric.bytecode.ShadowWrangler;
-import com.xtremelabs.robolectric.bytecode.DirectCallPolicy.FullStackDirectCallPolicy;
import com.xtremelabs.robolectric.shadows.*;
import com.xtremelabs.robolectric.tester.org.apache.http.FakeHttpLayer;
import com.xtremelabs.robolectric.tester.org.apache.http.HttpRequestInfo;
import com.xtremelabs.robolectric.tester.org.apache.http.RequestMatcher;
import com.xtremelabs.robolectric.util.Scheduler;
+import org.apache.http.Header;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.impl.client.DefaultRequestDirector;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+import java.util.Arrays;
+import java.util.List;
public class Robolectric {
public static Application application;
@@ -207,6 +205,7 @@ public static void logMissingInvokedShadowMethods() {
ShadowGallery.class,
ShadowGeocoder.class,
ShadowGeoPoint.class,
+ ShadowGestureDetector.class,
ShadowGridView.class,
ShadowHandler.class,
ShadowHandlerThread.class,
@@ -650,6 +649,10 @@ public static ShadowGeocoder shadowOf(Geocoder instance) {
return (ShadowGeocoder) shadowOf_(instance);
}
+ public static ShadowGestureDetector shadowOf(GestureDetector instance) {
+ return (ShadowGestureDetector) shadowOf_(instance);
+ }
+
public static ShadowGridView shadowOf(GridView instance) {
return (ShadowGridView) shadowOf_(instance);
}
@@ -0,0 +1,39 @@
+package com.xtremelabs.robolectric.shadows;
+
+import android.view.GestureDetector;
+import android.view.MotionEvent;
+import com.xtremelabs.robolectric.internal.Implementation;
+import com.xtremelabs.robolectric.internal.Implements;
+
+@Implements(GestureDetector.class)
+public class ShadowGestureDetector {
+ private MotionEvent onTouchEventMotionEvent;
+ private boolean onTouchEventNextReturnValue = true;
+ private GestureDetector.OnGestureListener listener;
+
+ public void __constructor__(GestureDetector.OnGestureListener listener) {
+ this.listener = listener;
+ }
+
+ @Implementation
+ public boolean onTouchEvent(MotionEvent ev) {
+ onTouchEventMotionEvent = ev;
+ return onTouchEventNextReturnValue;
+ }
+
+ public MotionEvent getOnTouchEventMotionEvent() {
+ return onTouchEventMotionEvent;
+ }
+
+ public void reset() {
+ onTouchEventMotionEvent = null;
+ }
+
+ public void setNextOnTouchEventReturnValue(boolean nextReturnValue) {
+ onTouchEventNextReturnValue = nextReturnValue;
+ }
+
+ public GestureDetector.OnGestureListener getListener() {
+ return listener;
+ }
+}
@@ -0,0 +1,86 @@
+package com.xtremelabs.robolectric.shadows;
+
+import android.view.GestureDetector;
+import android.view.MotionEvent;
+import com.xtremelabs.robolectric.WithTestDefaultsRunner;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import static com.xtremelabs.robolectric.Robolectric.shadowOf;
+import static junit.framework.Assert.*;
+
+@RunWith(WithTestDefaultsRunner.class)
+public class GestureDetectorTest {
+
+ private GestureDetector detector;
+ private MotionEvent motionEvent;
+
+ @Before
+ public void setUp() throws Exception {
+ detector = new GestureDetector(null);
+ motionEvent = MotionEvent.obtain(-1, -1, MotionEvent.ACTION_UP, 100, 30, -1);
+ }
+
+ @Test
+ public void test_getOnTouchEventMotionEvent() throws Exception {
+ detector.onTouchEvent(motionEvent);
+ assertSame(motionEvent, shadowOf(detector).getOnTouchEventMotionEvent());
+ }
+
+ @Test
+ public void test_reset() throws Exception {
+ detector.onTouchEvent(motionEvent);
+ assertSame(motionEvent, shadowOf(detector).getOnTouchEventMotionEvent());
+
+ shadowOf(detector).reset();
+ assertNull(shadowOf(detector).getOnTouchEventMotionEvent());
+ }
+
+ @Test
+ public void test_setNextOnTouchEventReturnValue() throws Exception {
+ assertTrue(detector.onTouchEvent(motionEvent));
+
+ shadowOf(detector).setNextOnTouchEventReturnValue(true);
+ assertTrue(detector.onTouchEvent(motionEvent));
+
+ shadowOf(detector).setNextOnTouchEventReturnValue(false);
+ assertFalse(detector.onTouchEvent(motionEvent));
+ }
+
+ @Test
+ public void test_getListener() throws Exception {
+ TestOnGestureListener listener = new TestOnGestureListener();
+ assertSame(listener, shadowOf(new GestureDetector(listener)).getListener());
+ }
+
+ private static class TestOnGestureListener implements GestureDetector.OnGestureListener {
+ @Override
+ public boolean onDown(MotionEvent e) {
+ return false;
+ }
+
+ @Override
+ public void onShowPress(MotionEvent e) {
+ }
+
+ @Override
+ public boolean onSingleTapUp(MotionEvent e) {
+ return false;
+ }
+
+ @Override
+ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
+ return false;
+ }
+
+ @Override
+ public void onLongPress(MotionEvent e) {
+ }
+
+ @Override
+ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
+ return false;
+ }
+ }
+}

0 comments on commit ae3f0ea

Please sign in to comment.