1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.camera.camera2.pipe.testing
18 
19 import androidx.camera.camera2.pipe.CameraTimestamp
20 import androidx.camera.camera2.pipe.FrameInfo
21 import androidx.camera.camera2.pipe.FrameMetadata
22 import androidx.camera.camera2.pipe.FrameNumber
23 import androidx.camera.camera2.pipe.Request
24 import androidx.camera.camera2.pipe.RequestFailure
25 import androidx.camera.camera2.pipe.RequestMetadata
26 import androidx.camera.camera2.pipe.StreamId
27 import kotlinx.coroutines.flow.MutableSharedFlow
28 import kotlinx.coroutines.flow.SharedFlow
29 import kotlinx.coroutines.flow.asSharedFlow
30 
31 /**
32  * Fake implementation of a [Request.Listener] for tests.
33  *
34  * Events are exposed as SharedFlows of events to allow a test to block and wait for events to to be
35  * sent.
36  */
37 @Suppress("ListenerInterface")
38 public class FakeRequestListener(private val replayBuffer: Int = 10) : Request.Listener {
39 
40     private val _onStartedFlow = MutableSharedFlow<OnStarted>(replay = replayBuffer)
41     public val onStartedFlow: SharedFlow<OnStarted> = _onStartedFlow.asSharedFlow()
42 
43     private val _onPartialCaptureResultFlow =
44         MutableSharedFlow<OnPartialCaptureResult>(replay = replayBuffer)
45     public val onPartialCaptureResultFlow: SharedFlow<OnPartialCaptureResult> =
46         _onPartialCaptureResultFlow.asSharedFlow()
47 
48     private val _onTotalCaptureResultFlow =
49         MutableSharedFlow<OnTotalCaptureResult>(replay = replayBuffer)
50     public val onTotalCaptureResultFlow: SharedFlow<OnTotalCaptureResult> =
51         _onTotalCaptureResultFlow.asSharedFlow()
52 
53     private val _onCompleteFlow = MutableSharedFlow<OnComplete>(replay = replayBuffer)
54     public val onCompleteFlow: SharedFlow<OnComplete> = _onCompleteFlow.asSharedFlow()
55 
56     private val _onBufferLostFlow = MutableSharedFlow<OnBufferLost>(replay = replayBuffer)
57     public val onBufferLostFlow: SharedFlow<OnBufferLost> = _onBufferLostFlow.asSharedFlow()
58 
59     private val _onAbortedFlow = MutableSharedFlow<OnAborted>(replay = replayBuffer)
60     public val onAbortedFlow: SharedFlow<OnAborted> = _onAbortedFlow.asSharedFlow()
61 
62     private val _onFailedFlow = MutableSharedFlow<OnFailed>(replay = replayBuffer)
63     public val onFailedFlow: SharedFlow<OnFailed> = _onFailedFlow.asSharedFlow()
64 
onStartednull65     override fun onStarted(
66         requestMetadata: RequestMetadata,
67         frameNumber: FrameNumber,
68         timestamp: CameraTimestamp
69     ): Unit =
70         check(_onStartedFlow.tryEmit(OnStarted(requestMetadata, frameNumber, timestamp))) {
71             "Failed to emit onStarted event! The size of the replay buffer" +
72                 "($replayBuffer) may need to be increased."
73         }
74 
onPartialCaptureResultnull75     override fun onPartialCaptureResult(
76         requestMetadata: RequestMetadata,
77         frameNumber: FrameNumber,
78         captureResult: FrameMetadata
79     ): Unit =
80         check(
81             _onPartialCaptureResultFlow.tryEmit(
82                 OnPartialCaptureResult(requestMetadata, frameNumber, captureResult)
83             )
84         ) {
85             "Failed to emit OnPartialCaptureResult event! The size of the replay buffer" +
86                 "($replayBuffer) may need to be increased."
87         }
88 
onTotalCaptureResultnull89     override fun onTotalCaptureResult(
90         requestMetadata: RequestMetadata,
91         frameNumber: FrameNumber,
92         totalCaptureResult: FrameInfo
93     ): Unit =
94         check(
95             _onTotalCaptureResultFlow.tryEmit(
96                 OnTotalCaptureResult(requestMetadata, frameNumber, totalCaptureResult)
97             )
98         ) {
99             "Failed to emit OnTotalCaptureResult event! The size of the replay buffer" +
100                 "($replayBuffer) may need to be increased."
101         }
102 
onCompletenull103     override fun onComplete(
104         requestMetadata: RequestMetadata,
105         frameNumber: FrameNumber,
106         result: FrameInfo
107     ): Unit =
108         check(_onCompleteFlow.tryEmit(OnComplete(requestMetadata, frameNumber, result))) {
109             "Failed to emit onComplete event! The size of the replay buffer" +
110                 "($replayBuffer) may need to be increased."
111         }
112 
onAbortednull113     override fun onAborted(request: Request): Unit =
114         check(_onAbortedFlow.tryEmit(OnAborted(request))) {
115             "Failed to emit OnAborted event! The size of the replay buffer" +
116                 "($replayBuffer) may need to be increased."
117         }
118 
onBufferLostnull119     override fun onBufferLost(
120         requestMetadata: RequestMetadata,
121         frameNumber: FrameNumber,
122         stream: StreamId
123     ): Unit =
124         check(_onBufferLostFlow.tryEmit(OnBufferLost(requestMetadata, frameNumber, stream))) {
125             "Failed to emit OnBufferLost event! The size of the replay buffer" +
126                 "($replayBuffer) may need to be increased."
127         }
128 
onFailednull129     override fun onFailed(
130         requestMetadata: RequestMetadata,
131         frameNumber: FrameNumber,
132         requestFailure: RequestFailure
133     ): Unit =
134         check(_onFailedFlow.tryEmit(OnFailed(requestMetadata, frameNumber, requestFailure))) {
135             "Failed to emit OnFailed event! The size of the replay buffer" +
136                 "($replayBuffer) may need to be increased."
137         }
138 }
139 
140 public sealed class RequestListenerEvent
141 
142 public class OnStarted(
143     public val requestMetadata: RequestMetadata,
144     public val frameNumber: FrameNumber,
145     public val timestamp: CameraTimestamp
146 ) : RequestListenerEvent()
147 
148 public class OnPartialCaptureResult(
149     public val requestMetadata: RequestMetadata,
150     public val frameNumber: FrameNumber,
151     public val frameMetadata: FrameMetadata
152 ) : RequestListenerEvent()
153 
154 public class OnTotalCaptureResult(
155     public val requestMetadata: RequestMetadata,
156     public val frameNumber: FrameNumber,
157     public val frameInfo: FrameInfo
158 ) : RequestListenerEvent()
159 
160 public class OnComplete(
161     public val requestMetadata: RequestMetadata,
162     public val frameNumber: FrameNumber,
163     public val frameInfo: FrameInfo
164 ) : RequestListenerEvent()
165 
166 public class OnAborted(public val request: Request) : RequestListenerEvent()
167 
168 public class OnBufferLost(
169     public val requestMetadata: RequestMetadata,
170     public val frameNumber: FrameNumber,
171     public val streamId: StreamId
172 ) : RequestListenerEvent()
173 
174 public class OnFailed(
175     public val requestMetadata: RequestMetadata,
176     public val frameNumber: FrameNumber,
177     public val requestFailure: RequestFailure
178 ) : RequestListenerEvent()
179