Paul Rohde | 4ae9179 | 2020-06-24 12:40:57 -0700 | [diff] [blame^] | 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 android.hardware.camera2.CaptureRequest |
| 20 | import androidx.camera.camera2.pipe.Request |
| 21 | import androidx.camera.camera2.pipe.impl.RequestProcessor |
| 22 | import androidx.camera.camera2.pipe.impl.TokenLock |
| 23 | import androidx.camera.camera2.pipe.impl.TokenLockImpl |
| 24 | import kotlinx.coroutines.channels.Channel |
| 25 | import kotlinx.coroutines.withTimeout |
| 26 | |
| 27 | /** |
| 28 | * Fake implementation of a [RequestProcessor] for tests. |
| 29 | */ |
| 30 | class FakeRequestProcessor : RequestProcessor { |
| 31 | private val eventChannel = Channel<Event>(Channel.UNLIMITED) |
| 32 | |
| 33 | val requestQueue: MutableList<FakeRequest> = mutableListOf() |
| 34 | var repeatingRequest: FakeRequest? = null |
| 35 | |
| 36 | var rejectRequests = false |
| 37 | var abortInvoked = false |
| 38 | private set |
| 39 | var disconnectInvoked = false |
| 40 | private set |
| 41 | var stopInvoked = false |
| 42 | private set |
| 43 | |
| 44 | private val tokenLock = TokenLockImpl(1) |
| 45 | private var token: TokenLock.Token? = null |
| 46 | |
| 47 | data class FakeRequest( |
| 48 | val burst: List<Request>, |
| 49 | val extraRequestParameters: Map<CaptureRequest.Key<*>, Any> = emptyMap(), |
| 50 | val requireStreams: Boolean = false |
| 51 | ) |
| 52 | |
| 53 | override fun submit( |
| 54 | request: Request, |
| 55 | extraRequestParameters: Map<CaptureRequest.Key<*>, Any>, |
| 56 | requireSurfacesForAllStreams: Boolean |
| 57 | ): Boolean { |
| 58 | val fakeRequest = |
| 59 | FakeRequest(listOf(request), extraRequestParameters, requireSurfacesForAllStreams) |
| 60 | |
| 61 | if (rejectRequests || disconnectInvoked || stopInvoked) { |
| 62 | eventChannel.offer(Event(request = fakeRequest, rejected = true)) |
| 63 | return false |
| 64 | } |
| 65 | |
| 66 | requestQueue.add(fakeRequest) |
| 67 | eventChannel.offer(Event(request = fakeRequest, submit = true)) |
| 68 | |
| 69 | return true |
| 70 | } |
| 71 | |
| 72 | override fun submit( |
| 73 | requests: List<Request>, |
| 74 | extraRequestParameters: Map<CaptureRequest.Key<*>, Any>, |
| 75 | requireSurfacesForAllStreams: Boolean |
| 76 | ): Boolean { |
| 77 | val fakeRequest = |
| 78 | FakeRequest(requests, extraRequestParameters, requireSurfacesForAllStreams) |
| 79 | if (rejectRequests || disconnectInvoked || stopInvoked) { |
| 80 | eventChannel.offer(Event(request = fakeRequest, rejected = true)) |
| 81 | return false |
| 82 | } |
| 83 | |
| 84 | requestQueue.add(fakeRequest) |
| 85 | eventChannel.offer(Event(request = fakeRequest, submit = true)) |
| 86 | |
| 87 | return true |
| 88 | } |
| 89 | |
| 90 | override fun setRepeating( |
| 91 | request: Request, |
| 92 | extraRequestParameters: Map<CaptureRequest.Key<*>, Any>, |
| 93 | requireSurfacesForAllStreams: Boolean |
| 94 | ): Boolean { |
| 95 | val fakeRequest = |
| 96 | FakeRequest(listOf(request), extraRequestParameters, requireSurfacesForAllStreams) |
| 97 | if (rejectRequests || disconnectInvoked || stopInvoked) { |
| 98 | eventChannel.offer(Event(request = fakeRequest, rejected = true)) |
| 99 | return false |
| 100 | } |
| 101 | |
| 102 | repeatingRequest = fakeRequest |
| 103 | eventChannel.offer(Event(request = fakeRequest, setRepeating = true)) |
| 104 | return true |
| 105 | } |
| 106 | |
| 107 | override fun abort() { |
| 108 | abortInvoked = true |
| 109 | eventChannel.offer(Event(abort = true)) |
| 110 | } |
| 111 | |
| 112 | override fun disconnect() { |
| 113 | disconnectInvoked = true |
| 114 | eventChannel.offer(Event(disconnect = true)) |
| 115 | } |
| 116 | |
| 117 | override fun stop() { |
| 118 | stopInvoked = true |
| 119 | eventChannel.offer(Event(stop = true)) |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Get the next event from queue with an option to specify a timeout for tests. |
| 124 | */ |
| 125 | suspend fun nextEvent(timeMillis: Long = 25): Event = withTimeout(timeMillis) { |
| 126 | eventChannel.receive() |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | data class Event( |
| 131 | val request: FakeRequestProcessor.FakeRequest? = null, |
| 132 | val rejected: Boolean = false, |
| 133 | val abort: Boolean = false, |
| 134 | val disconnect: Boolean = false, |
| 135 | val stop: Boolean = false, |
| 136 | val submit: Boolean = false, |
| 137 | val setRepeating: Boolean = false |
| 138 | ) |