-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creates Robolectric shadow for TotalCaptureResult.
PiperOrigin-RevId: 241630963
- Loading branch information
1 parent
78d27a9
commit 8e1e585
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
robolectric/src/test/java/org/robolectric/shadows/ShadowCaptureResultTest.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,44 @@ | ||
package org.robolectric.shadows; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.junit.Assert.fail; | ||
import static org.robolectric.Shadows.shadowOf; | ||
|
||
import android.hardware.camera2.CaptureResult; | ||
import android.hardware.camera2.CaptureResult.Key; | ||
import android.os.Build.VERSION_CODES; | ||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.robolectric.annotation.Config; | ||
|
||
/** Tests for {@link ShadowCaptureResult}. */ | ||
@Config(minSdk = VERSION_CODES.LOLLIPOP) | ||
@RunWith(AndroidJUnit4.class) | ||
public class ShadowCaptureResultTest { | ||
|
||
private final Key<Long> timestampKey = CaptureResult.SENSOR_TIMESTAMP; | ||
private final CaptureResult captureResult = ShadowCaptureResult.newCaptureResult(); | ||
|
||
@Test | ||
public void testSetExistingKey() { | ||
shadowOf(captureResult).set(timestampKey, 1L); | ||
try { | ||
shadowOf(captureResult).set(timestampKey, 2L); | ||
fail(); | ||
} catch (IllegalArgumentException exception) { | ||
// Pass. | ||
} | ||
} | ||
|
||
@Test | ||
public void testGetUnrecongizedKey() { | ||
assertThat(captureResult.get(timestampKey)).isNull(); | ||
} | ||
|
||
@Test | ||
public void testGetRecognizedKey() { | ||
shadowOf(captureResult).set(timestampKey, 1L); | ||
assertThat(captureResult.get(timestampKey)).isEqualTo(1L); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
shadows/framework/src/main/java/org/robolectric/shadows/ShadowCaptureResult.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,44 @@ | ||
package org.robolectric.shadows; | ||
|
||
import android.annotation.Nullable; | ||
import android.hardware.camera2.CaptureResult; | ||
import android.hardware.camera2.CaptureResult.Key; | ||
import android.os.Build.VERSION_CODES; | ||
import com.google.common.base.Preconditions; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.robolectric.annotation.Implementation; | ||
import org.robolectric.annotation.Implements; | ||
import org.robolectric.util.ReflectionHelpers; | ||
|
||
/** Shadow of {@link CaptureResult}. */ | ||
@Implements(value = CaptureResult.class, minSdk = VERSION_CODES.LOLLIPOP) | ||
public class ShadowCaptureResult { | ||
|
||
private final Map<Key<?>, Object> resultsKeyToValue = new HashMap<>(); | ||
|
||
/** Convenience method which returns a new instance of {@link CaptureResult}. */ | ||
public static CaptureResult newCaptureResult() { | ||
return ReflectionHelpers.callConstructor(CaptureResult.class); | ||
} | ||
|
||
/** | ||
* Obtain a property of the CaptureResult. | ||
*/ | ||
@Implementation | ||
@Nullable | ||
@SuppressWarnings("unchecked") | ||
protected <T> T get(Key<T> key) { | ||
return (T) resultsKeyToValue.get(key); | ||
} | ||
|
||
/** | ||
* Sets the value for a given key. | ||
* | ||
* @throws IllegalArgumentException if there's an existing value for the key. | ||
*/ | ||
public <T> void set(Key<T> key, T value) { | ||
Preconditions.checkArgument(!resultsKeyToValue.containsKey(key)); | ||
resultsKeyToValue.put(key, value); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
shadows/framework/src/main/java/org/robolectric/shadows/ShadowTotalCaptureResult.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,16 @@ | ||
package org.robolectric.shadows; | ||
|
||
import android.hardware.camera2.TotalCaptureResult; | ||
import android.os.Build.VERSION_CODES; | ||
import org.robolectric.annotation.Implements; | ||
import org.robolectric.util.ReflectionHelpers; | ||
|
||
/** Shadow of {@link TotalCaptureResult}. */ | ||
@Implements(value = TotalCaptureResult.class, minSdk = VERSION_CODES.LOLLIPOP) | ||
public class ShadowTotalCaptureResult extends ShadowCaptureResult { | ||
|
||
/** Convenience method which returns a new instance of {@link TotalCaptureResult}. */ | ||
public static TotalCaptureResult newTotalCaptureResult() { | ||
return ReflectionHelpers.callConstructor(TotalCaptureResult.class); | ||
} | ||
} |