From 714d228168f29981dfb3ec253826452f02d22dca Mon Sep 17 00:00:00 2001 From: Mattia Iavarone Date: Fri, 21 Dec 2018 20:37:05 +0100 Subject: [PATCH] Fix tests --- cameraview/build.gradle | 3 +- .../otaliastudios/cameraview/BaseTest.java | 42 +++++++-------- .../cameraview/CameraLoggerTest.java | 2 +- .../cameraview/CameraOptions1Test.java | 4 +- .../cameraview/CameraPreviewTest.java | 38 ++++++++++---- .../cameraview/CameraUtilsTest.java | 11 ++-- .../cameraview/CameraViewCallbacksTest.java | 7 +-- .../cameraview/CameraViewTest.java | 8 +-- .../cameraview/CropHelperTest.java | 3 +- .../cameraview/GestureLayoutTest.java | 3 ++ .../cameraview/GlCameraPreviewTest.java | 3 +- .../cameraview/GridLinesLayoutTest.java | 2 +- .../cameraview/IntegrationTest.java | 6 ++- .../otaliastudios/cameraview/Mapper1Test.java | 3 +- .../otaliastudios/cameraview/MapperTest.java | 26 ---------- .../cameraview/MockCameraPreview.java | 17 +++---- .../cameraview/OrientationHelperTest.java | 2 +- .../cameraview/PictureRecorderTest.java | 2 +- .../cameraview/PictureResultTest.java | 5 +- .../cameraview/PinchGestureLayoutTest.java | 2 +- .../cameraview/ScrollGestureLayoutTest.java | 2 +- .../cameraview/SurfaceCameraPreviewTest.java | 4 +- .../cameraview/TapGestureLayoutTest.java | 2 +- .../cameraview/TextureCameraPreviewTest.java | 3 +- .../cameraview/VideoRecorderTest.java | 2 +- .../cameraview/VideoResultTest.java | 3 +- .../cameraview/WorkerHandlerTest.java | 6 +-- .../cameraview/YuvHelperTest.java | 51 ------------------- .../cameraview/CameraController.java | 6 ++- .../cameraview/CameraLogger.java | 6 +-- .../cameraview/CameraPreview.java | 13 +++-- .../cameraview/GlCameraPreview.java | 16 ++++-- .../cameraview/SurfaceCameraPreview.java | 8 +++ .../cameraview/TextureCameraPreview.java | 9 ++++ .../cameraview/AspectRatioTest.java | 7 +-- .../otaliastudios/cameraview/SizeTest.java | 11 ++-- 36 files changed, 164 insertions(+), 174 deletions(-) delete mode 100644 cameraview/src/androidTest/java/com/otaliastudios/cameraview/MapperTest.java delete mode 100644 cameraview/src/androidTest/java/com/otaliastudios/cameraview/YuvHelperTest.java diff --git a/cameraview/build.gradle b/cameraview/build.gradle index de424d8c..36a7ca22 100644 --- a/cameraview/build.gradle +++ b/cameraview/build.gradle @@ -41,10 +41,11 @@ android { dependencies { testImplementation 'junit:junit:4.12' - testImplementation 'org.mockito:mockito-core:1.10.19' + testImplementation 'org.mockito:mockito-core:2.23.0' androidTestImplementation 'androidx.test:runner:1.1.1' androidTestImplementation 'androidx.test:rules:1.1.1' + androidTestImplementation 'androidx.test.ext:junit:1.1.0' androidTestImplementation 'com.google.dexmaker:dexmaker:1.2' androidTestImplementation 'com.google.dexmaker:dexmaker-mockito:1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/BaseTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/BaseTest.java index 4f2e961e..769c78d4 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/BaseTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/BaseTest.java @@ -10,14 +10,16 @@ import android.os.Build; import android.os.Handler; import android.os.Looper; import android.os.PowerManager; -import androidx.test.InstrumentationRegistry; import androidx.test.annotation.UiThreadTest; import androidx.test.espresso.core.internal.deps.guava.collect.ObjectArrays; +import androidx.test.platform.app.InstrumentationRegistry; import androidx.test.rule.ActivityTestRule; import android.view.View; +import org.junit.After; import org.junit.AfterClass; import org.junit.BeforeClass; +import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import org.mockito.stubbing.Stubber; @@ -67,6 +69,19 @@ public class BaseTest { keyguardLock.reenableKeyguard(); } + /** + * This will make mockito report the error when it should. + * Mockito reports failure on the next mockito invocation, which is terrible + * since it might be on the next test or even never happen. + * + * Calling this + */ + @After + public void syncMockito() { + Object object = Mockito.mock(Object.class); + object.toString(); + } + public static void ui(Runnable runnable) { InstrumentationRegistry.getInstrumentation().runOnMainSync(runnable); } @@ -112,33 +127,10 @@ public class BaseTest { InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand(command); } - public static byte[] mockJpeg(int width, int height) { - Bitmap source = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); - ByteArrayOutputStream os = new ByteArrayOutputStream(); - source.compress(Bitmap.CompressFormat.JPEG, 100, os); - return os.toByteArray(); - } - - public static YuvImage mockYuv(int width, int height) { - YuvImage y = mock(YuvImage.class); - when(y.getWidth()).thenReturn(width); - when(y.getHeight()).thenReturn(height); - when(y.compressToJpeg(any(Rect.class), anyInt(), any(OutputStream.class))).thenAnswer(new Answer() { - @Override - public Boolean answer(InvocationOnMock invocation) throws Throwable { - Rect rect = (Rect) invocation.getArguments()[0]; - OutputStream stream = (OutputStream) invocation.getArguments()[2]; - stream.write(mockJpeg(rect.width(), rect.height())); - return true; - } - }); - return y; - } - public static Stubber doCountDown(final CountDownLatch latch) { return doAnswer(new Answer() { @Override - public Object answer(InvocationOnMock invocation) throws Throwable { + public Object answer(InvocationOnMock invocation) { latch.countDown(); return null; } diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraLoggerTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraLoggerTest.java index 57f25c56..8a3dcbbb 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraLoggerTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraLoggerTest.java @@ -1,8 +1,8 @@ package com.otaliastudios.cameraview; +import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; import org.junit.After; import org.junit.Before; diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraOptions1Test.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraOptions1Test.java index 09664dd5..195e6dfe 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraOptions1Test.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraOptions1Test.java @@ -2,8 +2,9 @@ package com.otaliastudios.cameraview; import android.hardware.Camera; + +import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; import org.junit.Test; import org.junit.runner.RunWith; @@ -283,6 +284,7 @@ public class CameraOptions1Test extends BaseTest { Camera.Parameters params = mock(Camera.Parameters.class); when(params.isVideoSnapshotSupported()).thenReturn(true); when(params.isZoomSupported()).thenReturn(true); + //noinspection ArraysAsListWithZeroOrOneArgument when(params.getSupportedFocusModes()).thenReturn(Arrays.asList(Camera.Parameters.FOCUS_MODE_AUTO)); CameraOptions o = new CameraOptions(params, false); assertTrue(o.isZoomSupported()); diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraPreviewTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraPreviewTest.java index 7994029d..05f5e462 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraPreviewTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraPreviewTest.java @@ -24,29 +24,43 @@ public abstract class CameraPreviewTest extends BaseTest { @Rule public ActivityTestRule rule = new ActivityTestRule<>(TestActivity.class); + @SuppressWarnings("WeakerAccess") protected CameraPreview preview; + @SuppressWarnings("WeakerAccess") protected Size surfaceSize; private CameraPreview.SurfaceCallback callback; - private Task availability; + + private Task available; + private Task destroyed; @Before public void setUp() { - availability = new Task<>(true); + available = new Task<>(true); + destroyed = new Task<>(true); ui(new Runnable() { @Override public void run() { TestActivity a = rule.getActivity(); surfaceSize = a.getContentSize(); - callback = mock(CameraPreview.SurfaceCallback.class); + doAnswer(new Answer() { @Override - public Object answer(InvocationOnMock invocation) throws Throwable { - if (availability != null) availability.end(true); + public Object answer(InvocationOnMock invocation) { + if (available != null) available.end(true); return null; } }).when(callback).onSurfaceAvailable(); + + doAnswer(new Answer() { + @Override + public Object answer(InvocationOnMock invocation) { + if (destroyed != null) destroyed.end(true); + return null; + } + }).when(callback).onSurfaceDestroyed(); + preview = createPreview(a, a.getContentView(), callback); } }); @@ -54,7 +68,7 @@ public abstract class CameraPreviewTest extends BaseTest { // Wait for surface to be available. protected void ensureAvailable() { - assertNotNull(availability.await(2000)); + assertNotNull(available.await(2000)); } // Trigger a destroy. @@ -62,10 +76,10 @@ public abstract class CameraPreviewTest extends BaseTest { ui(new Runnable() { @Override public void run() { - rule.getActivity().getContentView().removeView(preview.getView()); + rule.getActivity().getContentView().removeView(preview.getRootView()); } }); - idle(); + assertNotNull(destroyed.await(2000)); } @After @@ -73,7 +87,8 @@ public abstract class CameraPreviewTest extends BaseTest { preview = null; callback = null; surfaceSize = null; - availability = null; + available = null; + destroyed = null; } @Test @@ -81,6 +96,7 @@ public abstract class CameraPreviewTest extends BaseTest { ensureAvailable(); assertTrue(preview.hasSurface()); assertNotNull(preview.getView()); + assertNotNull(preview.getRootView()); assertNotNull(preview.getOutputClass()); } @@ -103,12 +119,14 @@ public abstract class CameraPreviewTest extends BaseTest { public void testSurfaceDestroyed() { ensureAvailable(); ensureDestroyed(); + // This might be called twice in Texture because it overrides ensureDestroyed method + verify(callback, atLeastOnce()).onSurfaceDestroyed(); assertEquals(0, preview.getOutputSurfaceSize().getWidth()); assertEquals(0, preview.getOutputSurfaceSize().getHeight()); } @Test - public void testCropCenter() throws Exception { + public void testCropCenter() { ensureAvailable(); // This is given by the activity, it's the fixed size. diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraUtilsTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraUtilsTest.java index 2a8988cf..d87d2064 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraUtilsTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraUtilsTest.java @@ -5,8 +5,9 @@ import android.content.Context; import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.graphics.Color; + +import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; import org.junit.Test; import org.junit.runner.RunWith; @@ -102,13 +103,13 @@ public class CameraUtilsTest extends BaseTest { task = encodeDecodeTask(source, Integer.MAX_VALUE, Integer.MAX_VALUE); other = task.await(800); assertNotNull(other); - assertTrue(other.getWidth() == width); - assertTrue(other.getHeight() == height); + assertEquals(other.getWidth(), width); + assertEquals(other.getHeight(), height); task = encodeDecodeTask(source, 6000, 6000); other = task.await(800); assertNotNull(other); - assertTrue(other.getWidth() == width); - assertTrue(other.getHeight() == height); + assertEquals(other.getWidth(), width); + assertEquals(other.getHeight(), height); } } diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewCallbacksTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewCallbacksTest.java index b8b9108e..44319ea1 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewCallbacksTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewCallbacksTest.java @@ -3,8 +3,10 @@ package com.otaliastudios.cameraview; import android.content.Context; import android.graphics.PointF; + +import androidx.annotation.NonNull; +import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.MediumTest; -import androidx.test.runner.AndroidJUnit4; import android.view.ViewGroup; import org.junit.After; @@ -38,7 +40,6 @@ public class CameraViewCallbacksTest extends BaseTest { private MockCameraPreview mockPreview; private Task task; - @Before public void setUp() { ui(new Runnable() { @@ -61,7 +62,7 @@ public class CameraViewCallbacksTest extends BaseTest { } @Override - protected boolean checkPermissions(Mode mode, Audio audio) { + protected boolean checkPermissions(@NonNull Mode mode, @NonNull Audio audio) { return true; } }; diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java index 282160ce..fcb8ae2e 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CameraViewTest.java @@ -4,8 +4,8 @@ package com.otaliastudios.cameraview; import android.content.Context; import android.location.Location; import androidx.annotation.NonNull; +import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.MediumTest; -import androidx.test.runner.AndroidJUnit4; import android.view.MotionEvent; import android.view.ViewGroup; @@ -39,18 +39,18 @@ public class CameraViewTest extends BaseTest { cameraView = new CameraView(context) { @Override protected CameraController instantiateCameraController(CameraCallbacks callbacks) { - mockController = new MockCameraController(callbacks); + mockController = spy(new MockCameraController(callbacks)); return mockController; } @Override protected CameraPreview instantiatePreview(Context context, ViewGroup container) { - mockPreview = new MockCameraPreview(context, container); + mockPreview = spy(new MockCameraPreview(context, container)); return mockPreview; } @Override - protected boolean checkPermissions(Mode mode, Audio audio) { + protected boolean checkPermissions(@NonNull Mode mode, @NonNull Audio audio) { return hasPermissions; } }; diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CropHelperTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CropHelperTest.java index 53baa03e..dd59e724 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CropHelperTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/CropHelperTest.java @@ -2,8 +2,9 @@ package com.otaliastudios.cameraview; import android.graphics.Rect; + +import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/GestureLayoutTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/GestureLayoutTest.java index 57c9bcce..a0b10152 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/GestureLayoutTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/GestureLayoutTest.java @@ -28,7 +28,9 @@ public abstract class GestureLayoutTest extends BaseTes @Rule public ActivityTestRule rule = new ActivityTestRule<>(TestActivity.class); + @SuppressWarnings("WeakerAccess") protected T layout; + @SuppressWarnings("WeakerAccess") protected Task touch; @Before @@ -54,6 +56,7 @@ public abstract class GestureLayoutTest extends BaseTes }); } + @SuppressWarnings("WeakerAccess") protected final ViewInteraction onLayout() { return onView(Matchers.is(layout)) .inRoot(RootMatchers.withDecorView( diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/GlCameraPreviewTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/GlCameraPreviewTest.java index 3c274583..36e7e7cd 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/GlCameraPreviewTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/GlCameraPreviewTest.java @@ -2,8 +2,9 @@ package com.otaliastudios.cameraview; import android.content.Context; + +import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; import android.view.ViewGroup; import org.junit.runner.RunWith; diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/GridLinesLayoutTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/GridLinesLayoutTest.java index 962f2625..424a8787 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/GridLinesLayoutTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/GridLinesLayoutTest.java @@ -1,9 +1,9 @@ package com.otaliastudios.cameraview; +import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.MediumTest; import androidx.test.rule.ActivityTestRule; -import androidx.test.runner.AndroidJUnit4; import org.junit.Before; import org.junit.Rule; diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/IntegrationTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/IntegrationTest.java index a9031188..f084fd56 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/IntegrationTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/IntegrationTest.java @@ -5,13 +5,15 @@ import android.graphics.Bitmap; import android.graphics.PointF; import android.hardware.Camera; import android.os.Build; + +import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.MediumTest; import androidx.test.rule.ActivityTestRule; -import androidx.test.runner.AndroidJUnit4; import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; +import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @@ -36,7 +38,7 @@ import static org.mockito.Mockito.mock; */ @RunWith(AndroidJUnit4.class) @MediumTest -// @Ignore +@Ignore public class IntegrationTest extends BaseTest { @Rule diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/Mapper1Test.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/Mapper1Test.java index 0001c2ef..38f82579 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/Mapper1Test.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/Mapper1Test.java @@ -2,8 +2,9 @@ package com.otaliastudios.cameraview; import android.hardware.Camera; + +import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/MapperTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/MapperTest.java deleted file mode 100644 index a82170c6..00000000 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/MapperTest.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.otaliastudios.cameraview; - - -import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; - -import org.junit.runner.RunWith; - -import static org.junit.Assert.assertEquals; - - -@RunWith(AndroidJUnit4.class) -@SmallTest -public class MapperTest extends BaseTest { - - private Mapper mapper = new Mapper() { - T map(Flash flash) { return null; } - T map(Facing facing) { return null; } - T map(WhiteBalance whiteBalance) { return null; } - T map(Hdr hdr) { return null; } - Flash unmapFlash(T cameraConstant) { return null; } - Facing unmapFacing(T cameraConstant) { return null; } - WhiteBalance unmapWhiteBalance(T cameraConstant) { return null; } - Hdr unmapHdr(T cameraConstant) { return null; } - }; -} diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/MockCameraPreview.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/MockCameraPreview.java index fdc8efdb..92e6f9a2 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/MockCameraPreview.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/MockCameraPreview.java @@ -13,20 +13,14 @@ public class MockCameraPreview extends CameraPreview { super(context, parent, null); } - private boolean mCropping = false; - - public void setIsCropping(boolean crop) { - mCropping = crop; - } - @Override - boolean isCropping() { - return mCropping; + boolean supportsCropping() { + return true; } @NonNull @Override - protected View onCreateView(Context context, ViewGroup parent) { + protected View onCreateView(@NonNull Context context, @NonNull ViewGroup parent) { return new View(context); } @@ -42,4 +36,9 @@ public class MockCameraPreview extends CameraPreview { return null; } + @NonNull + @Override + View getRootView() { + return null; + } } diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/OrientationHelperTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/OrientationHelperTest.java index 1a885300..ff251639 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/OrientationHelperTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/OrientationHelperTest.java @@ -1,8 +1,8 @@ package com.otaliastudios.cameraview; +import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; import android.view.OrientationEventListener; import org.junit.After; diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/PictureRecorderTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/PictureRecorderTest.java index 5622bebc..9264dfd4 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/PictureRecorderTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/PictureRecorderTest.java @@ -1,8 +1,8 @@ package com.otaliastudios.cameraview; +import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/PictureResultTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/PictureResultTest.java index 83566da1..950315b4 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/PictureResultTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/PictureResultTest.java @@ -2,8 +2,9 @@ package com.otaliastudios.cameraview; import android.location.Location; + +import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; import org.junit.Test; import org.junit.runner.RunWith; @@ -32,6 +33,7 @@ public class PictureResultTest extends BaseTest { result.size = size; result.data = jpeg; result.location = location; + //noinspection ConstantConditions result.isSnapshot = isSnapshot; assertEquals(result.getFormat(), format); @@ -39,6 +41,7 @@ public class PictureResultTest extends BaseTest { assertEquals(result.getSize(), size); assertEquals(result.getData(), jpeg); assertEquals(result.getLocation(), location); + //noinspection ConstantConditions assertEquals(result.isSnapshot(), isSnapshot); } } diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/PinchGestureLayoutTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/PinchGestureLayoutTest.java index 1efe11a0..8746a8ea 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/PinchGestureLayoutTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/PinchGestureLayoutTest.java @@ -3,8 +3,8 @@ package com.otaliastudios.cameraview; import android.content.Context; import androidx.test.espresso.ViewAction; +import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/ScrollGestureLayoutTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/ScrollGestureLayoutTest.java index d235b6b8..1414b1fd 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/ScrollGestureLayoutTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/ScrollGestureLayoutTest.java @@ -3,8 +3,8 @@ package com.otaliastudios.cameraview; import android.content.Context; import androidx.test.espresso.ViewAction; +import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/SurfaceCameraPreviewTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/SurfaceCameraPreviewTest.java index 2ba90761..d65e8863 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/SurfaceCameraPreviewTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/SurfaceCameraPreviewTest.java @@ -2,10 +2,12 @@ package com.otaliastudios.cameraview; import android.content.Context; + +import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; import android.view.ViewGroup; +import org.junit.Test; import org.junit.runner.RunWith; @RunWith(AndroidJUnit4.class) diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/TapGestureLayoutTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/TapGestureLayoutTest.java index 66c34a84..7bd75a83 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/TapGestureLayoutTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/TapGestureLayoutTest.java @@ -6,8 +6,8 @@ import androidx.test.espresso.action.GeneralClickAction; import androidx.test.espresso.action.GeneralLocation; import androidx.test.espresso.action.Press; import androidx.test.espresso.action.Tap; +import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; import android.view.InputDevice; import android.view.MotionEvent; diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/TextureCameraPreviewTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/TextureCameraPreviewTest.java index ed532c26..0f604fc7 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/TextureCameraPreviewTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/TextureCameraPreviewTest.java @@ -2,8 +2,9 @@ package com.otaliastudios.cameraview; import android.content.Context; + +import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; import android.view.ViewGroup; import org.junit.runner.RunWith; diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/VideoRecorderTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/VideoRecorderTest.java index 24f234bd..2399f215 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/VideoRecorderTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/VideoRecorderTest.java @@ -1,8 +1,8 @@ package com.otaliastudios.cameraview; +import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/VideoResultTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/VideoResultTest.java index 51ca77c2..ec174210 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/VideoResultTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/VideoResultTest.java @@ -2,8 +2,9 @@ package com.otaliastudios.cameraview; import android.location.Location; + +import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; import org.junit.Test; import org.junit.runner.RunWith; diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/WorkerHandlerTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/WorkerHandlerTest.java index 922a5400..3f1f386d 100644 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/WorkerHandlerTest.java +++ b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/WorkerHandlerTest.java @@ -1,8 +1,8 @@ package com.otaliastudios.cameraview; +import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; import org.junit.Test; import org.junit.runner.RunWith; @@ -18,8 +18,8 @@ public class WorkerHandlerTest extends BaseTest { WorkerHandler w1 = WorkerHandler.get("handler1"); WorkerHandler w1a = WorkerHandler.get("handler1"); WorkerHandler w2 = WorkerHandler.get("handler2"); - assertTrue(w1 == w1a); - assertFalse(w1 == w2); + assertSame(w1, w1a); + assertNotSame(w1, w2); } @Test diff --git a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/YuvHelperTest.java b/cameraview/src/androidTest/java/com/otaliastudios/cameraview/YuvHelperTest.java deleted file mode 100644 index 6b92a332..00000000 --- a/cameraview/src/androidTest/java/com/otaliastudios/cameraview/YuvHelperTest.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.otaliastudios.cameraview; - - -import android.graphics.Rect; - -import androidx.test.filters.SmallTest; -import androidx.test.runner.AndroidJUnit4; - -import org.junit.Test; -import org.junit.runner.RunWith; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotEquals; -import static org.junit.Assert.assertTrue; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; - -@RunWith(AndroidJUnit4.class) -@SmallTest -public class YuvHelperTest extends BaseTest { - - @Test - public void testCrop() { - testCrop(new Size(1600, 1600), AspectRatio.of(16, 16)); - testCrop(new Size(1600, 1600), AspectRatio.of(16, 9)); - testCrop(new Size(1600, 1600), AspectRatio.of(9, 16)); - } - - private void testCrop(final Size inSize, final AspectRatio outRatio) { - AspectRatio inRatio = AspectRatio.of(inSize.getWidth(), inSize.getHeight()); - Rect out = YuvHelper.computeCrop(inSize, outRatio); - Size outSize = new Size(out.width(), out.height()); - assertTrue(outRatio.matches(outSize)); - - if (outRatio.matches(inSize)) { - // They are equal. - assertEquals(outSize.getWidth(), inSize.getWidth()); - assertEquals(outSize.getHeight(), inSize.getHeight()); - } else if (outRatio.toFloat() > inRatio.toFloat()) { - // Width must match. - assertEquals(outSize.getWidth(), inSize.getWidth()); - assertNotEquals(outSize.getHeight(), inSize.getHeight()); - } else { - // Height must match. - assertEquals(outSize.getHeight(), inSize.getHeight()); - assertNotEquals(outSize.getWidth(), inSize.getWidth()); - } - } - -} diff --git a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java index 64d297eb..f283e5af 100644 --- a/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java +++ b/cameraview/src/main/java/com/otaliastudios/cameraview/CameraController.java @@ -145,7 +145,8 @@ abstract class CameraController implements } } - final void destroy() { + // Public & not final so we can verify with mockito in CameraViewTest + public void destroy() { LOG.i("destroy:", "state:", ss()); // Prevent CameraController leaks. Don't set to null, or exceptions // inside the standard stop() method might crash the main thread. @@ -188,7 +189,8 @@ abstract class CameraController implements } // Stops the preview asynchronously. - final void stop() { + // Public & not final so we can verify with mockito in CameraViewTest + public void stop() { LOG.i("Stop:", "posting runnable. State:", ss()); mHandler.post(new Runnable() { @Override diff --git a/cameraview/src/main/utils/com/otaliastudios/cameraview/CameraLogger.java b/cameraview/src/main/utils/com/otaliastudios/cameraview/CameraLogger.java index 75e98373..6a75d42c 100644 --- a/cameraview/src/main/utils/com/otaliastudios/cameraview/CameraLogger.java +++ b/cameraview/src/main/utils/com/otaliastudios/cameraview/CameraLogger.java @@ -149,11 +149,11 @@ public final class CameraLogger { message.append(String.valueOf(object)); message.append(" "); } + String string = message.toString().trim(); for (Logger logger : sLoggers) { - logger.log(level, mTag, message.toString().trim(), throwable); + logger.log(level, mTag, string, throwable); } - - lastMessage = message.toString(); + lastMessage = string; lastTag = mTag; } } diff --git a/cameraview/src/main/views/com/otaliastudios/cameraview/CameraPreview.java b/cameraview/src/main/views/com/otaliastudios/cameraview/CameraPreview.java index a6e73b42..43bb0ec2 100644 --- a/cameraview/src/main/views/com/otaliastudios/cameraview/CameraPreview.java +++ b/cameraview/src/main/views/com/otaliastudios/cameraview/CameraPreview.java @@ -47,6 +47,10 @@ abstract class CameraPreview { return mView; } + // Only used in tests + @NonNull + abstract View getRootView(); + @NonNull abstract Class getOutputClass(); @@ -119,11 +123,14 @@ abstract class CameraPreview { mSurfaceCallback.onSurfaceDestroyed(); } - void onResume() {} + // Public for mockito (CameraViewTest) + public void onResume() {} - void onPause() {} + // Public for mockito (CameraViewTest) + public void onPause() {} - void onDestroy() {} + // Public for mockito (CameraViewTest) + public void onDestroy() {} final boolean hasSurface() { return mOutputSurfaceWidth > 0 && mOutputSurfaceHeight > 0; diff --git a/cameraview/src/main/views/com/otaliastudios/cameraview/GlCameraPreview.java b/cameraview/src/main/views/com/otaliastudios/cameraview/GlCameraPreview.java index 94b345cf..fd7851b9 100644 --- a/cameraview/src/main/views/com/otaliastudios/cameraview/GlCameraPreview.java +++ b/cameraview/src/main/views/com/otaliastudios/cameraview/GlCameraPreview.java @@ -9,6 +9,7 @@ import androidx.annotation.Nullable; import android.view.LayoutInflater; import android.view.SurfaceHolder; +import android.view.View; import android.view.ViewGroup; import java.util.Collections; @@ -62,6 +63,8 @@ class GlCameraPreview extends CameraPreview imple /* for tests */ float mScaleY = 1F; private float[] mScaleXY = new float[] { mScaleX, mScaleY }; + private View mRootView; + GlCameraPreview(@NonNull Context context, @NonNull ViewGroup parent, @Nullable SurfaceCallback callback) { super(context, parent, callback); } @@ -88,23 +91,30 @@ class GlCameraPreview extends CameraPreview imple } }); parent.addView(root, 0); + mRootView = root; return glView; } + @NonNull + @Override + View getRootView() { + return mRootView; + } + @Override - void onResume() { + public void onResume() { super.onResume(); getView().onResume(); } @Override - void onPause() { + public void onPause() { super.onPause(); getView().onPause(); } @Override - void onDestroy() { + public void onDestroy() { super.onDestroy(); // View is gone, so EGL context is gone: callbacks make no sense anymore. mRendererFrameCallbacks.clear(); diff --git a/cameraview/src/main/views/com/otaliastudios/cameraview/SurfaceCameraPreview.java b/cameraview/src/main/views/com/otaliastudios/cameraview/SurfaceCameraPreview.java index 6f055244..7defa65b 100644 --- a/cameraview/src/main/views/com/otaliastudios/cameraview/SurfaceCameraPreview.java +++ b/cameraview/src/main/views/com/otaliastudios/cameraview/SurfaceCameraPreview.java @@ -18,6 +18,7 @@ class SurfaceCameraPreview extends CameraPreview { private final static CameraLogger LOG = CameraLogger.create(SurfaceCameraPreview.class.getSimpleName()); private boolean mDispatched; + private View mRootView; SurfaceCameraPreview(@NonNull Context context, @NonNull ViewGroup parent, @Nullable SurfaceCallback callback) { super(context, parent, callback); @@ -57,9 +58,16 @@ class SurfaceCameraPreview extends CameraPreview { mDispatched = false; } }); + mRootView = root; return surfaceView; } + @NonNull + @Override + View getRootView() { + return mRootView; + } + @NonNull @Override SurfaceHolder getOutput() { diff --git a/cameraview/src/main/views/com/otaliastudios/cameraview/TextureCameraPreview.java b/cameraview/src/main/views/com/otaliastudios/cameraview/TextureCameraPreview.java index 95d681f3..c5664013 100644 --- a/cameraview/src/main/views/com/otaliastudios/cameraview/TextureCameraPreview.java +++ b/cameraview/src/main/views/com/otaliastudios/cameraview/TextureCameraPreview.java @@ -12,6 +12,8 @@ import android.view.ViewGroup; class TextureCameraPreview extends CameraPreview { + private View mRootView; + TextureCameraPreview(@NonNull Context context, @NonNull ViewGroup parent, @Nullable SurfaceCallback callback) { super(context, parent, callback); } @@ -44,9 +46,16 @@ class TextureCameraPreview extends CameraPreview { public void onSurfaceTextureUpdated(SurfaceTexture surface) { } }); + mRootView = root; return texture; } + @NonNull + @Override + View getRootView() { + return mRootView; + } + @NonNull @Override Class getOutputClass() { diff --git a/cameraview/src/test/java/com/otaliastudios/cameraview/AspectRatioTest.java b/cameraview/src/test/java/com/otaliastudios/cameraview/AspectRatioTest.java index 8c5ac9e1..98af6c1f 100644 --- a/cameraview/src/test/java/com/otaliastudios/cameraview/AspectRatioTest.java +++ b/cameraview/src/test/java/com/otaliastudios/cameraview/AspectRatioTest.java @@ -19,14 +19,14 @@ public class AspectRatioTest { public void testEquals() { AspectRatio ratio = AspectRatio.of(50, 10); assertNotNull(ratio); - assertTrue(ratio.equals(ratio)); + assertEquals(ratio, ratio); AspectRatio ratio1 = AspectRatio.of(5, 1); - assertTrue(ratio.equals(ratio1)); + assertEquals(ratio, ratio1); AspectRatio.sCache.clear(); AspectRatio ratio2 = AspectRatio.of(500, 100); - assertTrue(ratio.equals(ratio2)); + assertEquals(ratio, ratio2); Size size = new Size(500, 100); assertTrue(ratio.matches(size)); @@ -39,6 +39,7 @@ public class AspectRatioTest { AspectRatio ratio3 = AspectRatio.of(2, 10); assertTrue(ratio1.compareTo(ratio2) < 0); assertTrue(ratio1.compareTo(ratio3) > 0); + //noinspection EqualsWithItself,SimplifiableJUnitAssertion assertTrue(ratio1.compareTo(ratio1) == 0); assertNotEquals(ratio1.hashCode(), ratio2.hashCode()); } diff --git a/cameraview/src/test/java/com/otaliastudios/cameraview/SizeTest.java b/cameraview/src/test/java/com/otaliastudios/cameraview/SizeTest.java index 1bce75c5..67e9892a 100644 --- a/cameraview/src/test/java/com/otaliastudios/cameraview/SizeTest.java +++ b/cameraview/src/test/java/com/otaliastudios/cameraview/SizeTest.java @@ -27,14 +27,14 @@ public class SizeTest { @Test public void testEquals() { Size s1 = new Size(10, 20); - assertTrue(s1.equals(s1)); - assertFalse(s1.equals(null)); - assertFalse(s1.equals("")); + assertEquals(s1, s1); + assertNotEquals(s1, null); + assertNotEquals(s1, ""); Size s2 = new Size(10, 0); Size s3 = new Size(10, 20); - assertTrue(s1.equals(s3)); - assertFalse(s1.equals(s2)); + assertEquals(s1, s3); + assertNotEquals(s1, s2); } @Test @@ -49,6 +49,7 @@ public class SizeTest { Size s1 = new Size(10, 20); Size s2 = new Size(10, 0); Size s3 = new Size(10, 20); + //noinspection SimplifiableJUnitAssertion assertTrue(s1.compareTo(s3) == 0); assertTrue(s1.compareTo(s2) > 0); assertTrue(s2.compareTo(s1) < 0);