parent
1db8d74111
commit
b9f36aa327
@ -0,0 +1,39 @@ |
|||||||
|
package com.otaliastudios.cameraview; |
||||||
|
|
||||||
|
|
||||||
|
import android.content.Context; |
||||||
|
import android.os.Handler; |
||||||
|
import android.os.Looper; |
||||||
|
import android.support.test.InstrumentationRegistry; |
||||||
|
import android.support.test.rule.ActivityTestRule; |
||||||
|
import android.view.View; |
||||||
|
|
||||||
|
import org.junit.Rule; |
||||||
|
|
||||||
|
public class BaseTest { |
||||||
|
|
||||||
|
public static void ui(Runnable runnable) { |
||||||
|
InstrumentationRegistry.getInstrumentation().runOnMainSync(runnable); |
||||||
|
} |
||||||
|
|
||||||
|
public static void uiAsync(Runnable runnable) { |
||||||
|
new Handler(Looper.getMainLooper()).post(runnable); |
||||||
|
} |
||||||
|
|
||||||
|
public static Context context() { |
||||||
|
return InstrumentationRegistry.getInstrumentation().getContext(); |
||||||
|
} |
||||||
|
|
||||||
|
public static void uiRequestLayout(final View view) { |
||||||
|
ui(new Runnable() { |
||||||
|
@Override |
||||||
|
public void run() { |
||||||
|
view.requestLayout(); |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
public static void waitUi() { |
||||||
|
InstrumentationRegistry.getInstrumentation().waitForIdleSync(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,47 @@ |
|||||||
|
package com.otaliastudios.cameraview; |
||||||
|
|
||||||
|
|
||||||
|
import android.annotation.TargetApi; |
||||||
|
import android.content.Context; |
||||||
|
import android.support.test.rule.ActivityTestRule; |
||||||
|
import android.view.MotionEvent; |
||||||
|
import android.view.View; |
||||||
|
|
||||||
|
import org.junit.Before; |
||||||
|
import org.junit.Rule; |
||||||
|
|
||||||
|
@TargetApi(17) |
||||||
|
public abstract class GestureLayoutTest<T extends GestureLayout> extends BaseTest { |
||||||
|
|
||||||
|
protected abstract T create(Context context); |
||||||
|
|
||||||
|
@Rule |
||||||
|
public ActivityTestRule<TestActivity> rule = new ActivityTestRule<>(TestActivity.class); |
||||||
|
|
||||||
|
protected T layout; |
||||||
|
protected Task<Gesture> touch; |
||||||
|
|
||||||
|
@Before |
||||||
|
public void setUp() { |
||||||
|
ui(new Runnable() { |
||||||
|
@Override |
||||||
|
public void run() { |
||||||
|
TestActivity a = rule.getActivity(); |
||||||
|
layout = create(a); |
||||||
|
layout.enable(true); |
||||||
|
a.inflate(layout); |
||||||
|
|
||||||
|
touch = new Task<>(); |
||||||
|
layout.setOnTouchListener(new View.OnTouchListener() { |
||||||
|
@Override |
||||||
|
public boolean onTouch(View view, MotionEvent motionEvent) { |
||||||
|
boolean found = layout.onTouchEvent(motionEvent); |
||||||
|
if (found) touch.end(layout.getGestureType()); |
||||||
|
return true; |
||||||
|
} |
||||||
|
}); |
||||||
|
layout.setId(View.generateViewId()); |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,78 @@ |
|||||||
|
package com.otaliastudios.cameraview; |
||||||
|
|
||||||
|
|
||||||
|
import android.graphics.Canvas; |
||||||
|
import android.graphics.drawable.ColorDrawable; |
||||||
|
import android.support.test.InstrumentationRegistry; |
||||||
|
import android.support.test.filters.MediumTest; |
||||||
|
import android.support.test.rule.ActivityTestRule; |
||||||
|
import android.support.test.runner.AndroidJUnit4; |
||||||
|
|
||||||
|
import org.junit.Before; |
||||||
|
import org.junit.Rule; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
|
||||||
|
import static org.mockito.Mockito.*; |
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4.class) |
||||||
|
@MediumTest |
||||||
|
public class GridLinesLayoutTest extends BaseTest { |
||||||
|
|
||||||
|
@Rule |
||||||
|
public ActivityTestRule<TestActivity> rule = new ActivityTestRule<>(TestActivity.class); |
||||||
|
|
||||||
|
private GridLinesLayout layout; |
||||||
|
|
||||||
|
@Before |
||||||
|
public void setUp() { |
||||||
|
ui(new Runnable() { |
||||||
|
@Override |
||||||
|
public void run() { |
||||||
|
TestActivity a = rule.getActivity(); |
||||||
|
layout = new GridLinesLayout(a); |
||||||
|
layout.setGridMode(Grid.OFF); |
||||||
|
a.getContentView().addView(layout); |
||||||
|
layout.vert = mock(ColorDrawable.class); |
||||||
|
layout.horiz = mock(ColorDrawable.class); |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testOff() { |
||||||
|
layout.drawTask.listen(); |
||||||
|
layout.setGridMode(Grid.OFF); |
||||||
|
layout.drawTask.await(); |
||||||
|
verify(layout.vert, never()).draw(any(Canvas.class)); |
||||||
|
verify(layout.horiz, never()).draw(any(Canvas.class)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void test3x3() { |
||||||
|
layout.drawTask.listen(); |
||||||
|
layout.setGridMode(Grid.DRAW_3X3); |
||||||
|
layout.drawTask.await(); |
||||||
|
verify(layout.vert, times(2)).draw(any(Canvas.class)); |
||||||
|
verify(layout.horiz, times(2)).draw(any(Canvas.class)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testPhi() { |
||||||
|
layout.drawTask.listen(); |
||||||
|
layout.setGridMode(Grid.DRAW_PHI); |
||||||
|
layout.drawTask.await(); |
||||||
|
verify(layout.vert, times(2)).draw(any(Canvas.class)); |
||||||
|
verify(layout.horiz, times(2)).draw(any(Canvas.class)); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void test4x4() { |
||||||
|
layout.drawTask.listen(); |
||||||
|
layout.setGridMode(Grid.DRAW_4X4); |
||||||
|
layout.drawTask.await(); |
||||||
|
verify(layout.vert, times(3)).draw(any(Canvas.class)); |
||||||
|
verify(layout.horiz, times(3)).draw(any(Canvas.class)); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,86 @@ |
|||||||
|
package com.otaliastudios.cameraview; |
||||||
|
|
||||||
|
|
||||||
|
import android.content.Context; |
||||||
|
import android.support.test.espresso.Espresso; |
||||||
|
import android.support.test.espresso.UiController; |
||||||
|
import android.support.test.espresso.ViewAction; |
||||||
|
import android.support.test.espresso.action.CoordinatesProvider; |
||||||
|
import android.support.test.espresso.action.GeneralLocation; |
||||||
|
import android.support.test.espresso.action.GeneralSwipeAction; |
||||||
|
import android.support.test.espresso.action.MotionEvents; |
||||||
|
import android.support.test.espresso.action.PrecisionDescriber; |
||||||
|
import android.support.test.espresso.action.Press; |
||||||
|
import android.support.test.espresso.action.Swipe; |
||||||
|
import android.support.test.espresso.action.ViewActions; |
||||||
|
import android.support.test.espresso.assertion.ViewAssertions; |
||||||
|
import android.support.test.espresso.matcher.ViewMatchers; |
||||||
|
import android.support.test.filters.SmallTest; |
||||||
|
import android.support.test.runner.AndroidJUnit4; |
||||||
|
import android.view.MotionEvent; |
||||||
|
import android.view.ScaleGestureDetector; |
||||||
|
import android.view.View; |
||||||
|
|
||||||
|
import org.hamcrest.Matcher; |
||||||
|
import org.hamcrest.Matchers; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit; |
||||||
|
|
||||||
|
import static android.support.test.espresso.Espresso.onView; |
||||||
|
import static android.support.test.espresso.matcher.ViewMatchers.withId; |
||||||
|
import static org.junit.Assert.assertEquals; |
||||||
|
import static org.junit.Assert.assertNotNull; |
||||||
|
import static org.junit.Assert.assertNull; |
||||||
|
import static org.junit.Assert.assertTrue; |
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4.class) |
||||||
|
@SmallTest |
||||||
|
public class PinchGestureLayoutTest extends GestureLayoutTest<PinchGestureLayout> { |
||||||
|
|
||||||
|
@Override |
||||||
|
protected PinchGestureLayout create(Context context) { |
||||||
|
return new PinchGestureLayout(context); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testDefaults() { |
||||||
|
assertEquals(layout.getGestureType(), Gesture.PINCH); |
||||||
|
assertEquals(layout.getPoints().length, 2); |
||||||
|
assertEquals(layout.getPoints()[0].x, 0, 0); |
||||||
|
assertEquals(layout.getPoints()[0].y, 0, 0); |
||||||
|
assertEquals(layout.getPoints()[1].x, 0, 0); |
||||||
|
assertEquals(layout.getPoints()[1].y, 0, 0); |
||||||
|
} |
||||||
|
|
||||||
|
// TODO: test pinch open
|
||||||
|
// TODO: test pinch close
|
||||||
|
// TODO: test pinch disabled
|
||||||
|
|
||||||
|
// Possible approach: mimic pinch gesture and let espresso test.
|
||||||
|
// Too lazy to do this now, but it's possible.
|
||||||
|
// https://stackoverflow.com/questions/11523423/how-to-generate-zoom-pinch-gesture-for-testing-for-android
|
||||||
|
|
||||||
|
public abstract class PinchViewAction implements ViewAction { |
||||||
|
} |
||||||
|
|
||||||
|
private void testPinch(ViewAction action, boolean increasing) { |
||||||
|
touch.listen(); |
||||||
|
touch.start(); |
||||||
|
onView(withId(layout.getId())).perform(action); |
||||||
|
Gesture found = touch.await(10000, TimeUnit.MILLISECONDS); |
||||||
|
assertNotNull(found); |
||||||
|
|
||||||
|
// How will this move our parameter?
|
||||||
|
float curr = 0.5f, min = 0f, max = 1f; |
||||||
|
float newValue = layout.scaleValue(curr, min, max); |
||||||
|
if (increasing) { |
||||||
|
assertTrue(newValue > curr); |
||||||
|
assertTrue(newValue <= max); |
||||||
|
} else { |
||||||
|
assertTrue(newValue < curr); |
||||||
|
assertTrue(newValue >= min); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,94 @@ |
|||||||
|
package com.otaliastudios.cameraview; |
||||||
|
|
||||||
|
|
||||||
|
import android.content.Context; |
||||||
|
import android.support.test.espresso.ViewAction; |
||||||
|
import android.support.test.filters.SmallTest; |
||||||
|
import android.support.test.runner.AndroidJUnit4; |
||||||
|
|
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit; |
||||||
|
|
||||||
|
import static android.support.test.espresso.Espresso.onView; |
||||||
|
import static android.support.test.espresso.action.ViewActions.click; |
||||||
|
import static android.support.test.espresso.action.ViewActions.swipeDown; |
||||||
|
import static android.support.test.espresso.action.ViewActions.swipeLeft; |
||||||
|
import static android.support.test.espresso.action.ViewActions.swipeRight; |
||||||
|
import static android.support.test.espresso.action.ViewActions.swipeUp; |
||||||
|
import static android.support.test.espresso.matcher.ViewMatchers.withId; |
||||||
|
import static org.junit.Assert.assertEquals; |
||||||
|
import static org.junit.Assert.assertNull; |
||||||
|
import static org.junit.Assert.assertTrue; |
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4.class) |
||||||
|
@SmallTest |
||||||
|
public class ScrollGestureLayoutTest extends GestureLayoutTest<ScrollGestureLayout> { |
||||||
|
|
||||||
|
@Override |
||||||
|
protected ScrollGestureLayout create(Context context) { |
||||||
|
return new ScrollGestureLayout(context); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testDefaults() { |
||||||
|
assertNull(layout.getGestureType()); |
||||||
|
assertEquals(layout.getPoints().length, 2); |
||||||
|
assertEquals(layout.getPoints()[0].x, 0, 0); |
||||||
|
assertEquals(layout.getPoints()[0].y, 0, 0); |
||||||
|
assertEquals(layout.getPoints()[1].x, 0, 0); |
||||||
|
assertEquals(layout.getPoints()[1].y, 0, 0); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testScrollDisabled() { |
||||||
|
layout.enable(false); |
||||||
|
touch.listen(); |
||||||
|
touch.start(); |
||||||
|
onView(withId(layout.getId())).perform(swipeUp()); |
||||||
|
Gesture found = touch.await(500, TimeUnit.MILLISECONDS); |
||||||
|
assertNull(found); |
||||||
|
} |
||||||
|
|
||||||
|
private void testScroll(ViewAction scroll, Gesture expected, boolean increasing) { |
||||||
|
touch.listen(); |
||||||
|
touch.start(); |
||||||
|
onView(withId(layout.getId())).perform(scroll); |
||||||
|
Gesture found = touch.await(500, TimeUnit.MILLISECONDS); |
||||||
|
assertEquals(found, expected); |
||||||
|
|
||||||
|
// How will this move our parameter?
|
||||||
|
float curr = 0.5f, min = 0f, max = 1f; |
||||||
|
float newValue = layout.scaleValue(curr, min, max); |
||||||
|
if (increasing) { |
||||||
|
assertTrue(newValue > curr); |
||||||
|
assertTrue(newValue <= max); |
||||||
|
} else { |
||||||
|
assertTrue(newValue < curr); |
||||||
|
assertTrue(newValue >= min); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testScrollLeft() { |
||||||
|
testScroll(swipeLeft(), Gesture.SCROLL_HORIZONTAL, false); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testScrollRight() { |
||||||
|
testScroll(swipeRight(), Gesture.SCROLL_HORIZONTAL, true); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testScrollUp() { |
||||||
|
testScroll(swipeUp(), Gesture.SCROLL_VERTICAL, true); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testScrollDown() { |
||||||
|
testScroll(swipeDown(), Gesture.SCROLL_VERTICAL, false); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
} |
@ -0,0 +1,73 @@ |
|||||||
|
package com.otaliastudios.cameraview; |
||||||
|
|
||||||
|
|
||||||
|
import android.content.Context; |
||||||
|
import android.support.test.filters.SmallTest; |
||||||
|
import android.support.test.runner.AndroidJUnit4; |
||||||
|
|
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit; |
||||||
|
|
||||||
|
import static android.support.test.espresso.Espresso.*; |
||||||
|
import static android.support.test.espresso.matcher.ViewMatchers.*; |
||||||
|
import static android.support.test.espresso.action.ViewActions.*; |
||||||
|
import static org.junit.Assert.*; |
||||||
|
|
||||||
|
@RunWith(AndroidJUnit4.class) |
||||||
|
@SmallTest |
||||||
|
public class TapGestureLayoutTest extends GestureLayoutTest<TapGestureLayout> { |
||||||
|
|
||||||
|
@Override |
||||||
|
protected TapGestureLayout create(Context context) { |
||||||
|
return new TapGestureLayout(context); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testDefaults() { |
||||||
|
assertNull(layout.getGestureType()); |
||||||
|
assertEquals(layout.getPoints().length, 1); |
||||||
|
assertEquals(layout.getPoints()[0].x, 0, 0); |
||||||
|
assertEquals(layout.getPoints()[0].y, 0, 0); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
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)); |
||||||
|
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); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testTapWhileDisabled() { |
||||||
|
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)); |
||||||
|
Gesture found = touch.await(500, TimeUnit.MILLISECONDS); |
||||||
|
assertNull(found); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
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()); |
||||||
|
Gesture found = touch.await(500, TimeUnit.MILLISECONDS); |
||||||
|
assertEquals(found, Gesture.LONG_TAP); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue