Skip to content

Commit

Permalink
Merge pull request flutter#2 from bottlepay/android-rework-picture-re…
Browse files Browse the repository at this point in the history
…quest-state-tests

Android rework picture request state tests
  • Loading branch information
acoutts authored Mar 3, 2021
2 parents 358a790 + e552379 commit 818cc0d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -99,9 +103,8 @@ public void setState(PictureCaptureRequestState newState) {
return;
}

final PictureCaptureRequestState oldState = state;
state = newState;
onStateChange(oldState);
onStateChange(newState);
}

public boolean isFinished() {
Expand Down Expand Up @@ -152,8 +155,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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 818cc0d

Please sign in to comment.