FakeCaptureRequest


public final class FakeCaptureRequest implements CaptureRequestWrapper


A fake implementation of CaptureRequestWrapper designed for unit testing.

This class allows tests to mock camera settings by configuring mock values for both native CaptureRequest.Keys and custom Metadata.Keys.

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 unwrapAs

Because FakeCaptureRequest does not wrap a real, native CaptureRequest object, calling unwrapAs(CaptureRequest::class.java) will return null. It only supports unwrapping to FakeCaptureRequest itself or its supertypes.

Summary

Nested types

public static class FakeCaptureRequest.Companion

Public methods

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 FakeCaptureRequest instance for Java interoperability.

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

Retrieves the mock value configured for the specified CaptureRequest.Key.

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

Retrieves the mock value configured for the specified Metadata.Key.

@NonNull List<@NonNull CaptureRequest.Key<@NonNull ?>>

The list of CaptureRequest.Keys that have configured mock values in this fake request.

@NonNull Set<@NonNull Metadata.Key<@NonNull ?>>

The set of custom Metadata.Keys that have configured mock values in this fake request.

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

Unwraps this object to the specified type.

Inherited methods

From androidx.camera.common.CaptureRequestMetadata
@NonNull T
<T extends Object> getOrDefault(
    @NonNull CaptureRequest.Key<@NonNull T> key,
    @NonNull T default
)

Retrieves the value of the specified CaptureRequest.Key, or returns default if the key is not present or its value is null.

From androidx.camera.common.Metadata
@NonNull T
<T extends Object> getOrDefault(
    @NonNull Metadata.Key<@NonNull T> key,
    @NonNull T default
)

Retrieves the value associated with the specified Metadata.Key, or returns default 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 FakeCaptureRequest instance for Java interoperability.

Parameters
@NonNull Map<@NonNull CaptureRequest.Key<@NonNull ?>, Object> requestParameters

The map of capture request keys to their mock values. Defaults to an empty map.

@NonNull Map<@NonNull Metadata.Key<@NonNull ?>, Object> requestMetadata

The map of custom metadata keys to their mock values. Defaults to an empty map.

Returns
@NonNull FakeCaptureRequest

A configured FakeCaptureRequest instance.

get

Added in 1.7.0-alpha03
public T <T extends Object> get(@NonNull CaptureRequest.Key<@NonNull T> key)

Retrieves the mock value configured for the specified CaptureRequest.Key.

Parameters
@NonNull CaptureRequest.Key<@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-alpha03
public T <T extends Object> get(@NonNull Metadata.Key<@NonNull T> key)

Retrieves the mock value configured for the specified Metadata.Key.

Parameters
@NonNull Metadata.Key<@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-alpha03
public @NonNull List<@NonNull CaptureRequest.Key<@NonNull ?>> getKeys()

The list of CaptureRequest.Keys that have configured mock values in this fake request.

getMetadataKeys

Added in 1.7.0-alpha03
public @NonNull Set<@NonNull Metadata.Key<@NonNull ?>> getMetadataKeys()

The set of custom Metadata.Keys that have configured mock values in this fake request.

unwrapAs

Added in 1.7.0-alpha03
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 CaptureRequest object, this method will return null if type is CaptureRequest. It only returns this (cast to T) if type is compatible with FakeCaptureRequest.

Parameters
@NonNull Class<@NonNull T> type

The class representing the target type.

Returns
T

This instance if compatible with type, otherwise null.