From 2f8d774e28f207f7748e1ae927fed2831d77f97e Mon Sep 17 00:00:00 2001 From: Maurits van Beusekom Date: Wed, 3 Mar 2021 10:11:55 +0100 Subject: [PATCH 1/3] Removed unused parameter --- .../io/flutter/plugins/camera/PictureCaptureRequest.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/PictureCaptureRequest.java b/packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/PictureCaptureRequest.java index d5d49cec93eb..f5a7eefa3907 100644 --- a/packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/PictureCaptureRequest.java +++ b/packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/PictureCaptureRequest.java @@ -99,9 +99,8 @@ public void setState(PictureCaptureRequestState newState) { return; } - final PictureCaptureRequestState oldState = state; state = newState; - onStateChange(oldState); + onStateChange(newState); } public boolean isFinished() { @@ -152,8 +151,8 @@ public void setPreCaptureStartTime() { } /** Handle new state changes. */ - private void onStateChange(PictureCaptureRequestState oldState) { - switch (state) { + private void onStateChange(PictureCaptureRequestState newState) { + switch (newState) { case STATE_CAPTURING: case STATE_WAITING_FOCUS: case STATE_WAITING_PRECAPTURE_START: From 2e6337982c4876fda3b909538d2ae2d3180ec5b9 Mon Sep 17 00:00:00 2001 From: Maurits van Beusekom Date: Wed, 3 Mar 2021 10:12:48 +0100 Subject: [PATCH 2/3] Added missing test cases PictureCaptureRequest --- .../plugins/camera/DartMessengerTest.java | 3 ++ .../camera/PictureCaptureRequestTest.java | 46 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/packages/camera/camera/android/src/test/java/io/flutter/plugins/camera/DartMessengerTest.java b/packages/camera/camera/android/src/test/java/io/flutter/plugins/camera/DartMessengerTest.java index e835b08f441a..44ba6040a467 100644 --- a/packages/camera/camera/android/src/test/java/io/flutter/plugins/camera/DartMessengerTest.java +++ b/packages/camera/camera/android/src/test/java/io/flutter/plugins/camera/DartMessengerTest.java @@ -19,7 +19,10 @@ import java.util.List; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +@RunWith(RobolectricTestRunner.class) public class DartMessengerTest { /** A {@link BinaryMessenger} implementation that does nothing but save its messages. */ private static class FakeBinaryMessenger implements BinaryMessenger { diff --git a/packages/camera/camera/android/src/test/java/io/flutter/plugins/camera/PictureCaptureRequestTest.java b/packages/camera/camera/android/src/test/java/io/flutter/plugins/camera/PictureCaptureRequestTest.java index 99d905180c96..54509857ba3a 100644 --- a/packages/camera/camera/android/src/test/java/io/flutter/plugins/camera/PictureCaptureRequestTest.java +++ b/packages/camera/camera/android/src/test/java/io/flutter/plugins/camera/PictureCaptureRequestTest.java @@ -15,7 +15,10 @@ import io.flutter.plugin.common.MethodChannel; import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; +@RunWith(RobolectricTestRunner.class) public class PictureCaptureRequestTest { @Test @@ -45,6 +48,21 @@ public void setState_sets_state() { "State is awaitingPreCapture", req.getState(), PictureCaptureRequestState.STATE_CAPTURING); } + @Test + public void setState_sends_camera_error_event_When_already_finished() { + DartMessenger mockMessenger = mock(DartMessenger.class); + PictureCaptureRequest req = new PictureCaptureRequest(null, null, mockMessenger); + + // Make sure state is set to finished + req.setState(PictureCaptureRequestState.STATE_FINISHED); + + // Try to update state + req.setState(PictureCaptureRequestState.STATE_CAPTURING); + + verify(mockMessenger, times(1)).sendCameraErrorEvent("Request has already been finished"); + assertEquals("State is finished", req.getState(), PictureCaptureRequestState.STATE_FINISHED); + } + @Test public void setState_resets_timeout() { PictureCaptureRequest.TimeoutHandler mockTimeoutHandler = @@ -114,6 +132,20 @@ public void isFinished_is_true_When_state_is_finished_or_error() { assertTrue(req.isFinished()); } + @Test + public void finish_returns_When_in_error_state() { + MethodChannel.Result mockResult = mock(MethodChannel.Result.class); + PictureCaptureRequest req = new PictureCaptureRequest(mockResult, null, null); + + // Make sure state is set to error + req.setState(PictureCaptureRequestState.STATE_ERROR); + + req.finish("/test/path"); + + assertEquals("State is error", req.getState(), PictureCaptureRequestState.STATE_ERROR); + verify(mockResult, never()).success(any()); + } + @Test(expected = IllegalStateException.class) public void finish_throws_When_already_finished() { // Setup @@ -147,6 +179,20 @@ public void error_clears_timeout() { verify(mockTimeoutHandler).clearTimeout(any()); } + @Test + public void error_returns_When_in_error_state() { + MethodChannel.Result mockResult = mock(MethodChannel.Result.class); + PictureCaptureRequest req = new PictureCaptureRequest(mockResult, null, null); + + // Make sure state is set to error + req.setState(PictureCaptureRequestState.STATE_ERROR); + + req.error("ERROR_CODE", "Error Message", null); + + assertEquals("State is error", req.getState(), PictureCaptureRequestState.STATE_ERROR); + verify(mockResult, never()).error(any(), any(), any()); + } + @Test(expected = IllegalStateException.class) public void error_throws_When_already_finished() { // Setup From e552379b351b5664d883d0155764a50b9aa43e66 Mon Sep 17 00:00:00 2001 From: Maurits van Beusekom Date: Wed, 3 Mar 2021 11:34:28 +0100 Subject: [PATCH 3/3] Renamed mFile to file to match code style --- .../plugins/camera/PictureCaptureRequest.java | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/PictureCaptureRequest.java b/packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/PictureCaptureRequest.java index f5a7eefa3907..eb3cd6451316 100644 --- a/packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/PictureCaptureRequest.java +++ b/packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/PictureCaptureRequest.java @@ -27,7 +27,7 @@ class PictureCaptureRequest { * This is the output file for the curent capture. The file is created in Camera and passed here * as reference to it. */ - final File mFile; + final File file; /** Dart method chanel result. */ private final MethodChannel.Result result; @@ -54,25 +54,29 @@ class PictureCaptureRequest { * Constructor to create a picture capture request. * * @param result - * @param mFile + * @param file */ public PictureCaptureRequest( - MethodChannel.Result result, File mFile, DartMessenger dartMessenger) { - this.result = result; - this.timeoutHandler = new TimeoutHandler(); - this.mFile = mFile; - this.dartMessenger = dartMessenger; + MethodChannel.Result result, + File file, + DartMessenger dartMessenger) { + this( + result, + file, + dartMessenger, + new TimeoutHandler() + ); } /** Constructor for unit tests where we can mock the timeout handler */ public PictureCaptureRequest( MethodChannel.Result result, - File mFile, + File file, DartMessenger dartMessenger, TimeoutHandler timeoutHandler) { this.result = result; this.timeoutHandler = timeoutHandler; - this.mFile = mFile; + this.file = file; this.dartMessenger = dartMessenger; }