-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:dmitry-zaitsev/Fotoapparat
- Loading branch information
Showing
11 changed files
with
385 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
fotoapparat/src/main/java/io/fotoapparat/FotoapparatSwitcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package io.fotoapparat; | ||
|
||
import android.support.annotation.NonNull; | ||
|
||
/** | ||
* Switches between different instances of {@link Fotoapparat}. Convenient when you want to allow | ||
* user to switch between different cameras or configurations. | ||
* <p> | ||
* This class is not thread safe. Consider using it from a single thread. | ||
*/ | ||
public class FotoapparatSwitcher { | ||
|
||
@NonNull | ||
private Fotoapparat fotoapparat; | ||
|
||
private boolean started = false; | ||
|
||
private FotoapparatSwitcher(@NonNull Fotoapparat fotoapparat) { | ||
this.fotoapparat = fotoapparat; | ||
} | ||
|
||
/** | ||
* @return {@link FotoapparatSwitcher} with given {@link Fotoapparat} used by default. | ||
*/ | ||
public static FotoapparatSwitcher withDefault(@NonNull Fotoapparat fotoapparat) { | ||
return new FotoapparatSwitcher(fotoapparat); | ||
} | ||
|
||
/** | ||
* Starts {@link Fotoapparat} associated with this switcher. Every new {@link Fotoapparat} will | ||
* be started automatically until {@link #stop()} is called. | ||
* | ||
* @throws IllegalStateException if switcher is already started. | ||
*/ | ||
public void start() { | ||
fotoapparat.start(); | ||
|
||
started = true; | ||
} | ||
|
||
/** | ||
* Stops currently used {@link Fotoapparat}. | ||
* | ||
* @throws IllegalStateException if switcher is already stopped. | ||
*/ | ||
public void stop() { | ||
fotoapparat.stop(); | ||
|
||
started = false; | ||
} | ||
|
||
/** | ||
* Switches to another {@link Fotoapparat}. If switcher is already started then previously used | ||
* {@link Fotoapparat} will be stopped automatically and new {@link Fotoapparat} will be | ||
* started. | ||
* | ||
* @param fotoapparat new {@link Fotoapparat} to use. | ||
* @throws NullPointerException if given {@link Fotoapparat} is {@code null}. | ||
*/ | ||
public void switchTo(@NonNull Fotoapparat fotoapparat) { | ||
if (started) { | ||
this.fotoapparat.stop(); | ||
fotoapparat.start(); | ||
} | ||
|
||
this.fotoapparat = fotoapparat; | ||
} | ||
|
||
/** | ||
* @return currently used instance of {@link Fotoapparat}. | ||
*/ | ||
@NonNull | ||
public Fotoapparat getCurrentFotoapparat() { | ||
return fotoapparat; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
fotoapparat/src/main/java/io/fotoapparat/routine/CheckAvailabilityRoutine.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package io.fotoapparat.routine; | ||
|
||
import io.fotoapparat.hardware.CameraDevice; | ||
import io.fotoapparat.parameter.LensPosition; | ||
import io.fotoapparat.parameter.selector.SelectorFunction; | ||
|
||
/** | ||
* Checks whether {@link LensPosition} provided by {@link SelectorFunction} is available or not. | ||
*/ | ||
public class CheckAvailabilityRoutine { | ||
|
||
private final CameraDevice cameraDevice; | ||
private final SelectorFunction<LensPosition> lensPositionSelector; | ||
|
||
public CheckAvailabilityRoutine(CameraDevice cameraDevice, | ||
SelectorFunction<LensPosition> lensPositionSelector) { | ||
this.cameraDevice = cameraDevice; | ||
this.lensPositionSelector = lensPositionSelector; | ||
} | ||
|
||
/** | ||
* @return {@code true} if selected lens position is available. {@code false} if it is not | ||
* available. | ||
*/ | ||
public boolean isAvailable() { | ||
return selectedLensPosition() != null; | ||
} | ||
|
||
private LensPosition selectedLensPosition() { | ||
return lensPositionSelector.select(cameraDevice.getAvailableLensPositions()); | ||
} | ||
|
||
} |
87 changes: 87 additions & 0 deletions
87
fotoapparat/src/test/java/io/fotoapparat/FotoapparatSwitcherTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package io.fotoapparat; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InOrder; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import static junit.framework.Assert.assertSame; | ||
import static org.mockito.Mockito.inOrder; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyZeroInteractions; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class FotoapparatSwitcherTest { | ||
|
||
@Mock | ||
Fotoapparat fotoapparatA; | ||
@Mock | ||
Fotoapparat fotoapparatB; | ||
|
||
FotoapparatSwitcher testee; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
testee = FotoapparatSwitcher.withDefault(fotoapparatA); | ||
} | ||
|
||
@Test | ||
public void start_Default() throws Exception { | ||
// When | ||
testee.start(); | ||
|
||
// Then | ||
verify(fotoapparatA).start(); | ||
} | ||
|
||
@Test | ||
public void stop_Default() throws Exception { | ||
// When | ||
testee.stop(); | ||
|
||
// Then | ||
verify(fotoapparatA).stop(); | ||
} | ||
|
||
@Test | ||
public void getCurrentFotoapparat() throws Exception { | ||
// When | ||
Fotoapparat result = testee.getCurrentFotoapparat(); | ||
|
||
// Then | ||
assertSame(fotoapparatA, result); | ||
} | ||
|
||
@Test | ||
public void switchTo() throws Exception { | ||
// Given | ||
testee.switchTo(fotoapparatB); | ||
|
||
// When | ||
Fotoapparat result = testee.getCurrentFotoapparat(); | ||
|
||
// Then | ||
assertSame(fotoapparatB, result); | ||
|
||
verifyZeroInteractions(fotoapparatA); | ||
verifyZeroInteractions(fotoapparatB); | ||
} | ||
|
||
@Test | ||
public void switchTo_AlreadyStarted() throws Exception { | ||
// Given | ||
testee.start(); | ||
|
||
// When | ||
testee.switchTo(fotoapparatB); | ||
|
||
// Then | ||
InOrder inOrder = inOrder(fotoapparatA, fotoapparatB); | ||
|
||
inOrder.verify(fotoapparatA).stop(); | ||
inOrder.verify(fotoapparatB).start(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.