Skip to content

Commit

Permalink
Creates Robolectric shadow for TotalCaptureResult.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 241630963
  • Loading branch information
Googler authored and copybara-robolectric committed Apr 2, 2019
1 parent 78d27a9 commit 8e1e585
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
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);
}
}
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);
}
}
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);
}
}

0 comments on commit 8e1e585

Please sign in to comment.