pull/651/head
Mattia Iavarone 6 years ago
parent 06fbf93f06
commit f549eb9b2f
  1. 101
      cameraview/src/androidTest/java/com/otaliastudios/cameraview/internal/utils/OrientationHelperTest.java

@ -41,57 +41,68 @@ public class OrientationHelperTest extends BaseTest {
@Test @Test
public void testEnable() { public void testEnable() {
assertEquals(helper.getLastDisplayOffset(), -1); // On some API levels, enable() needs to be run on the UI thread.
assertEquals(helper.getLastDeviceOrientation(), -1); uiSync(new Runnable() {
@Override
public void run() {
assertEquals(helper.getLastDisplayOffset(), -1);
assertEquals(helper.getLastDeviceOrientation(), -1);
helper.enable(); helper.enable();
assertNotEquals(helper.getLastDisplayOffset(), -1); // Don't know about device orientation. assertNotEquals(helper.getLastDisplayOffset(), -1); // Don't know about device orientation.
// Ensure nothing bad if called twice. // Ensure nothing bad if called twice.
helper.enable(); helper.enable();
assertNotEquals(helper.getLastDisplayOffset(), -1); assertNotEquals(helper.getLastDisplayOffset(), -1);
helper.disable(); helper.disable();
assertEquals(helper.getLastDisplayOffset(), -1); assertEquals(helper.getLastDisplayOffset(), -1);
assertEquals(helper.getLastDeviceOrientation(), -1); assertEquals(helper.getLastDeviceOrientation(), -1);
}
});
} }
@Test @Test
public void testRotation() { public void testRotation() {
// On some API levels, enable() needs to be run on the UI thread.
// Sometimes (on some APIs) the helper will trigger an update to 0 uiSync(new Runnable() {
// right after enabling. But that's fine for us, times(1) will be OK either way. @Override
helper.enable(); public void run() {
helper.mDeviceOrientationListener.onOrientationChanged(OrientationEventListener.ORIENTATION_UNKNOWN); // Sometimes (on some APIs) the helper will trigger an update to 0
assertEquals(helper.getLastDeviceOrientation(), 0); // right after enabling. But that's fine for us, times(1) will be OK either way.
helper.mDeviceOrientationListener.onOrientationChanged(10); helper.enable();
assertEquals(helper.getLastDeviceOrientation(), 0); helper.mDeviceOrientationListener.onOrientationChanged(OrientationEventListener.ORIENTATION_UNKNOWN);
helper.mDeviceOrientationListener.onOrientationChanged(-10); assertEquals(helper.getLastDeviceOrientation(), 0);
assertEquals(helper.getLastDeviceOrientation(), 0); helper.mDeviceOrientationListener.onOrientationChanged(10);
helper.mDeviceOrientationListener.onOrientationChanged(44); assertEquals(helper.getLastDeviceOrientation(), 0);
assertEquals(helper.getLastDeviceOrientation(), 0); helper.mDeviceOrientationListener.onOrientationChanged(-10);
helper.mDeviceOrientationListener.onOrientationChanged(360); assertEquals(helper.getLastDeviceOrientation(), 0);
assertEquals(helper.getLastDeviceOrientation(), 0); helper.mDeviceOrientationListener.onOrientationChanged(44);
assertEquals(helper.getLastDeviceOrientation(), 0);
// Callback called just once. helper.mDeviceOrientationListener.onOrientationChanged(360);
verify(callback, times(1)).onDeviceOrientationChanged(0); assertEquals(helper.getLastDeviceOrientation(), 0);
helper.mDeviceOrientationListener.onOrientationChanged(90); // Callback called just once.
helper.mDeviceOrientationListener.onOrientationChanged(91); verify(callback, times(1)).onDeviceOrientationChanged(0);
assertEquals(helper.getLastDeviceOrientation(), 90);
verify(callback, times(1)).onDeviceOrientationChanged(90); helper.mDeviceOrientationListener.onOrientationChanged(90);
helper.mDeviceOrientationListener.onOrientationChanged(91);
helper.mDeviceOrientationListener.onOrientationChanged(180); assertEquals(helper.getLastDeviceOrientation(), 90);
assertEquals(helper.getLastDeviceOrientation(), 180); verify(callback, times(1)).onDeviceOrientationChanged(90);
verify(callback, times(1)).onDeviceOrientationChanged(180);
helper.mDeviceOrientationListener.onOrientationChanged(180);
helper.mDeviceOrientationListener.onOrientationChanged(270); assertEquals(helper.getLastDeviceOrientation(), 180);
assertEquals(helper.getLastDeviceOrientation(), 270); verify(callback, times(1)).onDeviceOrientationChanged(180);
verify(callback, times(1)).onDeviceOrientationChanged(270);
helper.mDeviceOrientationListener.onOrientationChanged(270);
// It is still 270 after ORIENTATION_UNKNOWN assertEquals(helper.getLastDeviceOrientation(), 270);
helper.mDeviceOrientationListener.onOrientationChanged(OrientationEventListener.ORIENTATION_UNKNOWN); verify(callback, times(1)).onDeviceOrientationChanged(270);
assertEquals(helper.getLastDeviceOrientation(), 270);
verify(callback, times(1)).onDeviceOrientationChanged(270); // It is still 270 after ORIENTATION_UNKNOWN
helper.mDeviceOrientationListener.onOrientationChanged(OrientationEventListener.ORIENTATION_UNKNOWN);
assertEquals(helper.getLastDeviceOrientation(), 270);
verify(callback, times(1)).onDeviceOrientationChanged(270);
}
});
} }
} }

Loading…
Cancel
Save