FakeCaptureRequest


class FakeCaptureRequest : 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

Public companion functions

FakeCaptureRequest
create(
    requestParameters: Map<CaptureRequest.Key<*>, Any?>,
    requestMetadata: Map<Metadata.Key<*>, Any?>
)

Factory method to create a FakeCaptureRequest instance for Java interoperability.

operator FakeCaptureRequest
invoke(
    requestParameters: Map<CaptureRequest.Key<*>, Any?>,
    requestMetadata: Map<Metadata.Key<*>, Any?>
)

Creates a FakeCaptureRequest instance.

Public functions

open operator T?
<T : Any> get(key: CaptureRequest.Key<T>)

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

open operator T?
<T : Any> get(key: Metadata.Key<T>)

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

open T?
<T : Any> unwrapAs(type: Class<T>)

Unwraps this object to the specified type.

Public properties

open List<CaptureRequest.Key<*>>

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

open Set<Metadata.Key<*>>

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

Inherited functions

From androidx.camera.common.CaptureRequestMetadata
open T
<T : Any> getOrDefault(key: CaptureRequest.Key<T>, default: T)

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
open T
<T : Any> getOrDefault(key: Metadata.Key<T>, default: T)

Retrieves the value associated with the specified Metadata.Key, or returns default if the key is not present.

Public companion functions

create

fun create(
    requestParameters: Map<CaptureRequest.Key<*>, Any?> = emptyMap(),
    requestMetadata: Map<Metadata.Key<*>, Any?> = emptyMap()
): FakeCaptureRequest

Factory method to create a FakeCaptureRequest instance for Java interoperability.

Parameters
requestParameters: Map<CaptureRequest.Key<*>, Any?> = emptyMap()

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

requestMetadata: Map<Metadata.Key<*>, Any?> = emptyMap()

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

Returns
FakeCaptureRequest

A configured FakeCaptureRequest instance.

invoke

Added in 1.7.0-alpha03
operator fun invoke(
    requestParameters: Map<CaptureRequest.Key<*>, Any?> = emptyMap(),
    requestMetadata: Map<Metadata.Key<*>, Any?> = emptyMap()
): FakeCaptureRequest

Creates a FakeCaptureRequest instance.

This allows constructor-like syntax in Kotlin: FakeCaptureRequest(...).

Parameters
requestParameters: Map<CaptureRequest.Key<*>, Any?> = emptyMap()

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

requestMetadata: Map<Metadata.Key<*>, Any?> = emptyMap()

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

Returns
FakeCaptureRequest

A configured FakeCaptureRequest instance.

Public functions

get

Added in 1.7.0-alpha03
open operator fun <T : Any> get(key: CaptureRequest.Key<T>): T?

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

Parameters
key: CaptureRequest.Key<T>

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
open operator fun <T : Any> get(key: Metadata.Key<T>): T?

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

Parameters
key: Metadata.Key<T>

The custom metadata key to query.

Returns
T?

The configured mock value, or null if no value was configured for the key.

unwrapAs

Added in 1.7.0-alpha03
open fun <T : Any> unwrapAs(type: Class<T>): T?

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
type: Class<T>

The class representing the target type.

Returns
T?

This instance if compatible with type, otherwise null.

Public properties

keys

Added in 1.7.0-alpha03
open val keysList<CaptureRequest.Key<*>>

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

metadataKeys

Added in 1.7.0-alpha03
open val metadataKeysSet<Metadata.Key<*>>

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