From f549eb9b2fe9fb4455230cbd6ace8e260e6a0c47 Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Wed, 16 Oct 2019 22:23:42 +0200 Subject: [PATCH] Fix tests --- .../internal/utils/OrientationHelperTest.java | 101 ++++++++++-------- 1 file changed, 56 insertions(+), 45 deletions(-) diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/internal/utils/OrientationHelperTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/internal/utils/OrientationHelperTest.java index b015c739..b5fde579 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/internal/utils/OrientationHelperTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/internal/utils/OrientationHelperTest.java @@ -41,57 +41,68 @@ public class OrientationHelperTest extends BaseTest { @Test public void testEnable() { - assertEquals(helper.getLastDisplayOffset(), -1); - assertEquals(helper.getLastDeviceOrientation(), -1); + // On some API levels, enable() needs to be run on the UI thread. + uiSync(new Runnable() { + @Override + public void run() { + assertEquals(helper.getLastDisplayOffset(), -1); + assertEquals(helper.getLastDeviceOrientation(), -1); - helper.enable(); - assertNotEquals(helper.getLastDisplayOffset(), -1); // Don't know about device orientation. + helper.enable(); + assertNotEquals(helper.getLastDisplayOffset(), -1); // Don't know about device orientation. - // Ensure nothing bad if called twice. - helper.enable(); - assertNotEquals(helper.getLastDisplayOffset(), -1); + // Ensure nothing bad if called twice. + helper.enable(); + assertNotEquals(helper.getLastDisplayOffset(), -1); - helper.disable(); - assertEquals(helper.getLastDisplayOffset(), -1); - assertEquals(helper.getLastDeviceOrientation(), -1); + helper.disable(); + assertEquals(helper.getLastDisplayOffset(), -1); + assertEquals(helper.getLastDeviceOrientation(), -1); + } + }); } @Test public void testRotation() { - - // Sometimes (on some APIs) the helper will trigger an update to 0 - // right after enabling. But that's fine for us, times(1) will be OK either way. - helper.enable(); - helper.mDeviceOrientationListener.onOrientationChanged(OrientationEventListener.ORIENTATION_UNKNOWN); - assertEquals(helper.getLastDeviceOrientation(), 0); - helper.mDeviceOrientationListener.onOrientationChanged(10); - assertEquals(helper.getLastDeviceOrientation(), 0); - helper.mDeviceOrientationListener.onOrientationChanged(-10); - assertEquals(helper.getLastDeviceOrientation(), 0); - helper.mDeviceOrientationListener.onOrientationChanged(44); - assertEquals(helper.getLastDeviceOrientation(), 0); - helper.mDeviceOrientationListener.onOrientationChanged(360); - assertEquals(helper.getLastDeviceOrientation(), 0); - - // Callback called just once. - verify(callback, times(1)).onDeviceOrientationChanged(0); - - helper.mDeviceOrientationListener.onOrientationChanged(90); - helper.mDeviceOrientationListener.onOrientationChanged(91); - assertEquals(helper.getLastDeviceOrientation(), 90); - verify(callback, times(1)).onDeviceOrientationChanged(90); - - helper.mDeviceOrientationListener.onOrientationChanged(180); - assertEquals(helper.getLastDeviceOrientation(), 180); - verify(callback, times(1)).onDeviceOrientationChanged(180); - - helper.mDeviceOrientationListener.onOrientationChanged(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); + // On some API levels, enable() needs to be run on the UI thread. + uiSync(new Runnable() { + @Override + public void run() { + // Sometimes (on some APIs) the helper will trigger an update to 0 + // right after enabling. But that's fine for us, times(1) will be OK either way. + helper.enable(); + helper.mDeviceOrientationListener.onOrientationChanged(OrientationEventListener.ORIENTATION_UNKNOWN); + assertEquals(helper.getLastDeviceOrientation(), 0); + helper.mDeviceOrientationListener.onOrientationChanged(10); + assertEquals(helper.getLastDeviceOrientation(), 0); + helper.mDeviceOrientationListener.onOrientationChanged(-10); + assertEquals(helper.getLastDeviceOrientation(), 0); + helper.mDeviceOrientationListener.onOrientationChanged(44); + assertEquals(helper.getLastDeviceOrientation(), 0); + helper.mDeviceOrientationListener.onOrientationChanged(360); + assertEquals(helper.getLastDeviceOrientation(), 0); + + // Callback called just once. + verify(callback, times(1)).onDeviceOrientationChanged(0); + + helper.mDeviceOrientationListener.onOrientationChanged(90); + helper.mDeviceOrientationListener.onOrientationChanged(91); + assertEquals(helper.getLastDeviceOrientation(), 90); + verify(callback, times(1)).onDeviceOrientationChanged(90); + + helper.mDeviceOrientationListener.onOrientationChanged(180); + assertEquals(helper.getLastDeviceOrientation(), 180); + verify(callback, times(1)).onDeviceOrientationChanged(180); + + helper.mDeviceOrientationListener.onOrientationChanged(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); + } + }); } }