### Question:
TakePictureTask extends FutureTask<Photo> { private static FocusResult autoFocus(CameraDevice cameraDevice) { int focusAttempts = 0; FocusResult focusResult = FocusResult.none(); while (focusAttempts < MAX_FOCUS_ATTEMPTS && !focusResult.succeeded) { focusResult = cameraDevice.autoFocus(); focusAttempts++; } return focusResult; } TakePictureTask(final CameraDevice cameraDevice);   }### Answer:
@Test public void noFocusAfter3Attempts_takePicture() throws Exception { given(cameraDevice.autoFocus()) .willReturn(new FocusResult(false, true)); Photo result = resultOf(testee); InOrder inOrder = inOrder(cameraDevice); inOrder.verify(cameraDevice, times(3)).autoFocus(); inOrder.verify(cameraDevice).takePicture(); inOrder.verify(cameraDevice).startPreview(); assertEquals(result, PHOTO); }
@Test public void exposureMeasurementRequired_takePhoto() throws Exception { given(cameraDevice.autoFocus()) .willReturn(new FocusResult(true, true)); Photo result = resultOf(testee); InOrder inOrder = inOrder(cameraDevice); inOrder.verify(cameraDevice).autoFocus(); inOrder.verify(cameraDevice).measureExposure(); inOrder.verify(cameraDevice).takePicture(); inOrder.verify(cameraDevice).startPreview(); assertEquals(result, PHOTO); }
@Test public void takePhoto() throws Exception { given(cameraDevice.autoFocus()) .willReturn(new FocusResult(true, false)); Photo result = resultOf(testee); InOrder inOrder = inOrder(cameraDevice); inOrder.verify(cameraDevice).autoFocus(); inOrder.verify(cameraDevice).takePicture(); inOrder.verify(cameraDevice).startPreview(); assertEquals(result, PHOTO); }
@Test public void doNotFocusInContinuousFocusMode() throws Exception { given(cameraDevice.getCurrentParameters()) .willReturn(PARAMETERS_WITH_CONTINUOUS_FOCUS); Photo result = resultOf(testee); verify(cameraDevice, never()).autoFocus(); assertEquals(result, PHOTO); }
@Test public void startPreviewFailed() throws Exception { given(cameraDevice.autoFocus()) .willReturn(new FocusResult(true, false)); doThrow(new CameraException("test")) .when(cameraDevice) .startPreview(); Photo result = resultOf(testee); verify(cameraDevice).startPreview(); assertEquals(result, PHOTO); }