# FakeCaptureRequest

Artifact: [androidx.camera:camera-common-testing](https://developer.android.com/jetpack/androidx/releases/camera) [View Source](https://cs.android.com/search?q=file:androidx/camera/common/testing/FakeCaptureRequest.kt+class:androidx.camera.common.testing.FakeCaptureRequest) Added in [1.7.0-rc02](https://developer.android.com/jetpack/androidx/releases/camera#1.7.0-rc02)

*** ** * ** ***


```
public final class FakeCaptureRequest implements CaptureRequestWrapper
```

<br />

*** ** * ** ***

A fake implementation of `https://developer.android.com/reference/androidx/camera/common/CaptureRequestWrapper` designed for unit testing.

This class allows tests to mock camera settings by configuring mock values for both native `https://developer.android.com/reference/android/hardware/camera2/CaptureRequest.Key.html`s and custom `https://developer.android.com/reference/androidx/camera/common/Metadata.Key`s.

### Kotlin Example

```
val fakeRequest = FakeCaptureRequest(
    requestParameters = mapOf(CaptureRequest.CONTROL_AE_MODE to CaptureRequest.CONTROL_AE_MODE_ON),
    requestMetadata = mapOf(customMetadataKey to "custom_value")
)

// Querying values
val aeMode = fakeRequest[CaptureRequest.CONTROL_AE_MODE] // Returns CONTROL_AE_MODE_ON
```

### Java Example

```
Map<CaptureRequest.Key<?>, Object> parameters = new HashMap<>();
parameters.put(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);

FakeCaptureRequest fakeRequest = FakeCaptureRequest.create(parameters, Collections.emptyMap());
```

### Behavior of `https://developer.android.com/reference/androidx/camera/common/testing/FakeCaptureRequest#unwrapAs(java.lang.Class)`

Because `FakeCaptureRequest` does not wrap a real, native `https://developer.android.com/reference/android/hardware/camera2/CaptureRequest.html` object, calling `unwrapAs(CaptureRequest::class.java)` will return `null`. It only supports unwrapping to `https://developer.android.com/reference/androidx/camera/common/testing/FakeCaptureRequest` itself or its supertypes.

## Summary

| ### Nested types |
|---|
| `public static class https://developer.android.com/reference/androidx/camera/common/testing/FakeCaptureRequest.Companion` |

| ### Public methods |
|---|---|
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/camera/common/testing/FakeCaptureRequest` | `https://developer.android.com/reference/androidx/camera/common/testing/FakeCaptureRequest#create(kotlin.collections.Map,kotlin.collections.Map)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Map.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/hardware/camera2/CaptureRequest.Key.html<@https://developer.android.com/reference/androidx/annotation/NonNull ?>, https://developer.android.com/reference/java/lang/Object.html> requestParameters, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Map.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/camera/common/Metadata.Key<@https://developer.android.com/reference/androidx/annotation/NonNull ?>, https://developer.android.com/reference/java/lang/Object.html> requestMetadata )` Factory method to create a `https://developer.android.com/reference/androidx/camera/common/testing/FakeCaptureRequest` instance for Java interoperability. |
| `T` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/camera/common/testing/FakeCaptureRequest#get(android.hardware.camera2.CaptureRequest.Key)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/hardware/camera2/CaptureRequest.Key.html<@https://developer.android.com/reference/androidx/annotation/NonNull T> key)` Retrieves the mock value configured for the specified `https://developer.android.com/reference/android/hardware/camera2/CaptureRequest.Key.html`. |
| `T` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/camera/common/testing/FakeCaptureRequest#get(androidx.camera.common.Metadata.Key)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/camera/common/Metadata.Key<@https://developer.android.com/reference/androidx/annotation/NonNull T> key)` Retrieves the mock value configured for the specified `https://developer.android.com/reference/androidx/camera/common/Metadata.Key`. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/hardware/camera2/CaptureRequest.Key.html<@https://developer.android.com/reference/androidx/annotation/NonNull ?>>` | `https://developer.android.com/reference/androidx/camera/common/testing/FakeCaptureRequest#getKeys()()` The list of `https://developer.android.com/reference/android/hardware/camera2/CaptureRequest.Key.html`s that have configured mock values in this fake request. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Set.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/camera/common/Metadata.Key<@https://developer.android.com/reference/androidx/annotation/NonNull ?>>` | `https://developer.android.com/reference/androidx/camera/common/testing/FakeCaptureRequest#getMetadataKeys()()` The set of custom `https://developer.android.com/reference/androidx/camera/common/Metadata.Key`s that have configured mock values in this fake request. |
| `T` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/camera/common/testing/FakeCaptureRequest#unwrapAs(java.lang.Class)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Class.html<@https://developer.android.com/reference/androidx/annotation/NonNull T> type)` Unwraps this object to the specified type. |

### Inherited methods

From [androidx.camera.common.CaptureRequestMetadata](https://developer.android.com/reference/androidx/camera/common/CaptureRequestMetadata) `@https://developer.android.com/reference/androidx/annotation/NonNull T` `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/camera/common/CaptureRequestMetadata#getOrDefault(android.hardware.camera2.CaptureRequest.Key,kotlin.Any)(
@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/hardware/camera2/CaptureRequest.Key.html<@https://developer.android.com/reference/androidx/annotation/NonNull T> key,
@https://developer.android.com/reference/androidx/annotation/NonNull T default
)`

Retrieves the value of the specified `https://developer.android.com/reference/android/hardware/camera2/CaptureRequest.Key.html`, or returns `https://developer.android.com/reference/androidx/camera/common/CaptureRequestMetadata#getOrDefault(android.hardware.camera2.CaptureRequest.Key,kotlin.Any)` if the key is not present or its value is `null`.
From [androidx.camera.common.Metadata](https://developer.android.com/reference/androidx/camera/common/Metadata) `@https://developer.android.com/reference/androidx/annotation/NonNull T` `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/camera/common/Metadata#getOrDefault(androidx.camera.common.Metadata.Key,kotlin.Any)(
@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/camera/common/Metadata.Key<@https://developer.android.com/reference/androidx/annotation/NonNull T> key,
@https://developer.android.com/reference/androidx/annotation/NonNull T default
)`

Retrieves the value associated with the specified `https://developer.android.com/reference/androidx/camera/common/Metadata.Key`, or returns `https://developer.android.com/reference/androidx/camera/common/Metadata#getOrDefault(androidx.camera.common.Metadata.Key,kotlin.Any)` if the key is not present.

## Public methods

### create

```
public static final @NonNull FakeCaptureRequest create(
    @NonNull Map<@NonNull CaptureRequest.Key<@NonNull ?>, Object> requestParameters,
    @NonNull Map<@NonNull Metadata.Key<@NonNull ?>, Object> requestMetadata
)
```

Factory method to create a `https://developer.android.com/reference/androidx/camera/common/testing/FakeCaptureRequest` instance for Java interoperability.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Map.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/hardware/camera2/CaptureRequest.Key.html<@https://developer.android.com/reference/androidx/annotation/NonNull ?>, https://developer.android.com/reference/java/lang/Object.html> requestParameters` | The map of capture request keys to their mock values. Defaults to an empty map. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Map.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/camera/common/Metadata.Key<@https://developer.android.com/reference/androidx/annotation/NonNull ?>, https://developer.android.com/reference/java/lang/Object.html> requestMetadata` | The map of custom metadata keys to their mock values. Defaults to an empty map. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/camera/common/testing/FakeCaptureRequest` | A configured `https://developer.android.com/reference/androidx/camera/common/testing/FakeCaptureRequest` instance. |

### get

Added in [1.7.0-rc02](https://developer.android.com/jetpack/androidx/releases/camera#1.7.0-rc02)

```
public T <T extends Object> get(@NonNull CaptureRequest.Key<@NonNull T> key)
```

Retrieves the mock value configured for the specified `https://developer.android.com/reference/android/hardware/camera2/CaptureRequest.Key.html`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/hardware/camera2/CaptureRequest.Key.html<@https://developer.android.com/reference/androidx/annotation/NonNull T> key` | The native capture request key to query. |

| Returns |
|---|---|
| `T` | The configured mock value, or `null` if no value was configured for the key. |

### get

Added in [1.7.0-rc02](https://developer.android.com/jetpack/androidx/releases/camera#1.7.0-rc02)

```
public T <T extends Object> get(@NonNull Metadata.Key<@NonNull T> key)
```

Retrieves the mock value configured for the specified `https://developer.android.com/reference/androidx/camera/common/Metadata.Key`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/camera/common/Metadata.Key<@https://developer.android.com/reference/androidx/annotation/NonNull T> key` | The custom metadata key to query. |

| Returns |
|---|---|
| `T` | The configured mock value, or `null` if no value was configured for the key. |

### getKeys

Added in [1.7.0-rc02](https://developer.android.com/jetpack/androidx/releases/camera#1.7.0-rc02)

```
public @NonNull List<@NonNull CaptureRequest.Key<@NonNull ?>> getKeys()
```

The list of `https://developer.android.com/reference/android/hardware/camera2/CaptureRequest.Key.html`s that have configured mock values in this fake request.

### getMetadataKeys

Added in [1.7.0-rc02](https://developer.android.com/jetpack/androidx/releases/camera#1.7.0-rc02)

```
public @NonNull Set<@NonNull Metadata.Key<@NonNull ?>> getMetadataKeys()
```

The set of custom `https://developer.android.com/reference/androidx/camera/common/Metadata.Key`s that have configured mock values in this fake request.

### unwrapAs

Added in [1.7.0-rc02](https://developer.android.com/jetpack/androidx/releases/camera#1.7.0-rc02)

```
public T <T extends Object> unwrapAs(@NonNull Class<@NonNull T> type)
```

Unwraps this object to the specified type.

Since this is a fake implementation and does not wrap a native Android `https://developer.android.com/reference/android/hardware/camera2/CaptureRequest.html` object, this method will return `null` if `https://developer.android.com/reference/androidx/camera/common/testing/FakeCaptureRequest#unwrapAs(java.lang.Class)` is `https://developer.android.com/reference/android/hardware/camera2/CaptureRequest.html`. It only returns `this` (cast to `https://developer.android.com/reference/androidx/camera/common/testing/FakeCaptureRequest#unwrapAs(java.lang.Class)`) if `https://developer.android.com/reference/androidx/camera/common/testing/FakeCaptureRequest#unwrapAs(java.lang.Class)` is compatible with `https://developer.android.com/reference/androidx/camera/common/testing/FakeCaptureRequest`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/Class.html<@https://developer.android.com/reference/androidx/annotation/NonNull T> type` | The class representing the target type. |

| Returns |
|---|---|
| `T` | This instance if compatible with `https://developer.android.com/reference/androidx/camera/common/testing/FakeCaptureRequest#unwrapAs(java.lang.Class)`, otherwise `null`. |