Improve Gesture Tests

pull/22/head
Mattia Iavarone 8 years ago
parent b9f36aa327
commit b0b9517f68
  1. 8
      cameraview/src/androidTest/java/com/otaliastudios/cameraview/GestureLayoutTest.java
  2. 2
      cameraview/src/androidTest/java/com/otaliastudios/cameraview/PinchGestureLayoutTest.java
  3. 4
      cameraview/src/androidTest/java/com/otaliastudios/cameraview/ScrollGestureLayoutTest.java
  4. 36
      cameraview/src/androidTest/java/com/otaliastudios/cameraview/TapGestureLayoutTest.java

@ -3,10 +3,13 @@ package com.otaliastudios.cameraview;
import android.annotation.TargetApi;
import android.content.Context;
import android.support.test.espresso.Espresso;
import android.support.test.espresso.ViewInteraction;
import android.support.test.rule.ActivityTestRule;
import android.view.MotionEvent;
import android.view.View;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Rule;
@ -40,8 +43,11 @@ public abstract class GestureLayoutTest<T extends GestureLayout> extends BaseTes
return true;
}
});
layout.setId(View.generateViewId());
}
});
}
protected ViewInteraction onLayout() {
return Espresso.onView(Matchers.<View>is(layout));
}
}

@ -68,7 +68,7 @@ public class PinchGestureLayoutTest extends GestureLayoutTest<PinchGestureLayout
private void testPinch(ViewAction action, boolean increasing) {
touch.listen();
touch.start();
onView(withId(layout.getId())).perform(action);
onLayout().perform(action);
Gesture found = touch.await(10000, TimeUnit.MILLISECONDS);
assertNotNull(found);

@ -46,7 +46,7 @@ public class ScrollGestureLayoutTest extends GestureLayoutTest<ScrollGestureLayo
layout.enable(false);
touch.listen();
touch.start();
onView(withId(layout.getId())).perform(swipeUp());
onLayout().perform(swipeUp());
Gesture found = touch.await(500, TimeUnit.MILLISECONDS);
assertNull(found);
}
@ -54,7 +54,7 @@ public class ScrollGestureLayoutTest extends GestureLayoutTest<ScrollGestureLayo
private void testScroll(ViewAction scroll, Gesture expected, boolean increasing) {
touch.listen();
touch.start();
onView(withId(layout.getId())).perform(scroll);
onLayout().perform(scroll);
Gesture found = touch.await(500, TimeUnit.MILLISECONDS);
assertEquals(found, expected);

@ -2,8 +2,14 @@ package com.otaliastudios.cameraview;
import android.content.Context;
import android.support.test.espresso.action.GeneralClickAction;
import android.support.test.espresso.action.GeneralLocation;
import android.support.test.espresso.action.Press;
import android.support.test.espresso.action.Tap;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import android.view.InputDevice;
import android.view.MotionEvent;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -36,14 +42,16 @@ public class TapGestureLayoutTest extends GestureLayoutTest<TapGestureLayout> {
public void testTap() {
touch.listen();
touch.start();
Size size = rule.getActivity().getContentSize();
int x = (int) (size.getWidth() / 2f);
int y = (int) (size.getHeight() / 2f);
onView(withId(layout.getId())).perform(click(x, y));
GeneralClickAction a = new GeneralClickAction(Tap.SINGLE,
GeneralLocation.CENTER, Press.FINGER,
InputDevice.SOURCE_UNKNOWN, MotionEvent.BUTTON_PRIMARY);
onLayout().perform(a);
Gesture found = touch.await(500, TimeUnit.MILLISECONDS);
assertEquals(found, Gesture.TAP);
assertEquals(layout.getPoints()[0].x, x, 1);
assertEquals(layout.getPoints()[0].y, y, 1);
Size size = rule.getActivity().getContentSize();
assertEquals(layout.getPoints()[0].x, (size.getWidth() / 2f), 1f);
assertEquals(layout.getPoints()[0].y, (size.getHeight() / 2f), 1f);
}
@Test
@ -51,10 +59,7 @@ public class TapGestureLayoutTest extends GestureLayoutTest<TapGestureLayout> {
layout.enable(false);
touch.listen();
touch.start();
Size size = rule.getActivity().getContentSize();
int x = (int) (size.getWidth() / 2f);
int y = (int) (size.getHeight() / 2f);
onView(withId(layout.getId())).perform(click(x, y));
onLayout().perform(click());
Gesture found = touch.await(500, TimeUnit.MILLISECONDS);
assertNull(found);
}
@ -63,11 +68,14 @@ public class TapGestureLayoutTest extends GestureLayoutTest<TapGestureLayout> {
public void testLongTap() {
touch.listen();
touch.start();
Size size = rule.getActivity().getContentSize();
int x = (int) (size.getWidth() / 2f);
int y = (int) (size.getHeight() / 2f);
onView(withId(layout.getId())).perform(longClick());
GeneralClickAction a = new GeneralClickAction(Tap.LONG,
GeneralLocation.CENTER, Press.FINGER,
InputDevice.SOURCE_UNKNOWN, MotionEvent.BUTTON_PRIMARY);
onLayout().perform(a);
Gesture found = touch.await(500, TimeUnit.MILLISECONDS);
assertEquals(found, Gesture.LONG_TAP);
Size size = rule.getActivity().getContentSize();
assertEquals(layout.getPoints()[0].x, (size.getWidth() / 2f), 1f);
assertEquals(layout.getPoints()[0].y, (size.getHeight() / 2f), 1f);
}
}

Loading…
Cancel
Save