1 /*
2  * Copyright 2021 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.integration.testing
18 
19 import android.view.Surface
20 import androidx.camera.camera2.pipe.AudioRestrictionMode
21 import androidx.camera.camera2.pipe.AudioRestrictionMode.Companion.AUDIO_RESTRICTION_NONE
22 import androidx.camera.camera2.pipe.CameraGraph
23 import androidx.camera.camera2.pipe.CameraGraphId
24 import androidx.camera.camera2.pipe.GraphState
25 import androidx.camera.camera2.pipe.StreamGraph
26 import androidx.camera.camera2.pipe.StreamId
27 import kotlinx.coroutines.CancellationException
28 import kotlinx.coroutines.CoroutineScope
29 import kotlinx.coroutines.Deferred
30 import kotlinx.coroutines.async
31 import kotlinx.coroutines.coroutineScope
32 import kotlinx.coroutines.flow.StateFlow
33 
34 class FakeCameraGraph(
35     val fakeCameraGraphSession: FakeCameraGraphSession = FakeCameraGraphSession()
36 ) : CameraGraph {
37 
38     val setSurfaceResults = mutableMapOf<StreamId, Surface?>()
39     private var isClosed = false
40     override val id: CameraGraphId
41         get() = throw NotImplementedError("Not used in testing")
42 
43     override val streams: StreamGraph
44         get() = throw NotImplementedError("Not used in testing")
45 
46     override val graphState: StateFlow<GraphState>
47         get() = throw NotImplementedError("Not used in testing")
48 
49     override var isForeground = true
50     private var audioRestrictionMode = AUDIO_RESTRICTION_NONE
51 
52     override val parameters: CameraGraph.Parameters
53         get() = throw NotImplementedError("Not used in testing")
54 
acquireSessionnull55     override suspend fun acquireSession(): CameraGraph.Session {
56         if (isClosed) {
57             throw CancellationException()
58         }
59         return fakeCameraGraphSession
60     }
61 
acquireSessionOrNullnull62     override fun acquireSessionOrNull() = if (isClosed) null else fakeCameraGraphSession
63 
64     override suspend fun <T> useSession(
65         action: suspend CoroutineScope.(CameraGraph.Session) -> T
66     ): T = fakeCameraGraphSession.use { coroutineScope { action(it) } }
67 
useSessionInnull68     override fun <T> useSessionIn(
69         scope: CoroutineScope,
70         action: suspend CoroutineScope.(CameraGraph.Session) -> T
71     ): Deferred<T> = scope.async { useSession(action) }
72 
closenull73     override fun close() {
74         isClosed = true
75     }
76 
setSurfacenull77     override fun setSurface(stream: StreamId, surface: Surface?) {
78         setSurfaceResults[stream] = surface
79     }
80 
updateAudioRestrictionModenull81     override fun updateAudioRestrictionMode(mode: AudioRestrictionMode) {
82         audioRestrictionMode = mode
83     }
84 
startnull85     override fun start() {
86         throw NotImplementedError("Not used in testing")
87     }
88 
stopnull89     override fun stop() {
90         throw NotImplementedError("Not used in testing")
91     }
92 }
93