Paul Rohde | c971909 | 2020-05-09 14:52:48 -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.impl |
| 18 | |
| 19 | import android.content.Context |
| 20 | import android.os.Build |
| 21 | import androidx.camera.camera2.pipe.CameraGraph |
Paul Rohde | 281f68b | 2020-05-21 10:32:03 -0700 | [diff] [blame] | 22 | import androidx.camera.camera2.pipe.CameraId |
Paul Rohde | 4ae9179 | 2020-06-24 12:40:57 -0700 | [diff] [blame^] | 23 | import androidx.camera.camera2.pipe.Request |
| 24 | import androidx.camera.camera2.pipe.RequestTemplate |
Paul Rohde | a5f5b07 | 2020-05-13 09:56:40 -0700 | [diff] [blame] | 25 | import androidx.camera.camera2.pipe.testing.CameraPipeRobolectricTestRunner |
Paul Rohde | 4ae9179 | 2020-06-24 12:40:57 -0700 | [diff] [blame^] | 26 | import androidx.camera.camera2.pipe.testing.FakeGraphProcessor |
Paul Rohde | c971909 | 2020-05-09 14:52:48 -0700 | [diff] [blame] | 27 | import androidx.test.core.app.ApplicationProvider |
| 28 | import androidx.test.filters.SmallTest |
| 29 | import com.google.common.truth.Truth.assertThat |
Sushil Nath | 8aa2199 | 2020-05-20 15:04:10 -0700 | [diff] [blame] | 30 | import kotlinx.coroutines.runBlocking |
| 31 | import org.junit.Before |
Paul Rohde | c971909 | 2020-05-09 14:52:48 -0700 | [diff] [blame] | 32 | import org.junit.Test |
| 33 | import org.junit.runner.RunWith |
Paul Rohde | c971909 | 2020-05-09 14:52:48 -0700 | [diff] [blame] | 34 | import org.robolectric.annotation.Config |
| 35 | import org.robolectric.annotation.internal.DoNotInstrument |
| 36 | |
| 37 | @SmallTest |
Paul Rohde | 0f73292 | 2020-05-11 10:45:17 -0700 | [diff] [blame] | 38 | @RunWith(CameraPipeRobolectricTestRunner::class) |
Paul Rohde | c971909 | 2020-05-09 14:52:48 -0700 | [diff] [blame] | 39 | @DoNotInstrument |
| 40 | @Config(minSdk = Build.VERSION_CODES.LOLLIPOP) |
| 41 | class CameraGraphImplTest { |
Paul Rohde | 4ae9179 | 2020-06-24 12:40:57 -0700 | [diff] [blame^] | 42 | private val graphProcessor = FakeGraphProcessor() |
Sushil Nath | 8aa2199 | 2020-05-20 15:04:10 -0700 | [diff] [blame] | 43 | private lateinit var impl: CameraGraphImpl |
| 44 | |
| 45 | @Before |
| 46 | fun setUp() { |
Paul Rohde | ed2fc71 | 2020-06-18 10:48:08 -0700 | [diff] [blame] | 47 | val config = CameraGraph.Config( |
| 48 | camera = CameraId("0"), |
| 49 | streams = listOf(), |
Paul Rohde | 4ae9179 | 2020-06-24 12:40:57 -0700 | [diff] [blame^] | 50 | defaultTemplate = RequestTemplate(0) |
Paul Rohde | ed2fc71 | 2020-06-18 10:48:08 -0700 | [diff] [blame] | 51 | ) |
Paul Rohde | c971909 | 2020-05-09 14:52:48 -0700 | [diff] [blame] | 52 | val context = ApplicationProvider.getApplicationContext() as Context |
Paul Rohde | 4ae9179 | 2020-06-24 12:40:57 -0700 | [diff] [blame^] | 53 | impl = CameraGraphImpl(context, config, graphProcessor, StreamMap()) |
Sushil Nath | 8aa2199 | 2020-05-20 15:04:10 -0700 | [diff] [blame] | 54 | } |
Paul Rohde | c971909 | 2020-05-09 14:52:48 -0700 | [diff] [blame] | 55 | |
Sushil Nath | 8aa2199 | 2020-05-20 15:04:10 -0700 | [diff] [blame] | 56 | @Test |
| 57 | fun createCameraGraphImpl() { |
Paul Rohde | c971909 | 2020-05-09 14:52:48 -0700 | [diff] [blame] | 58 | assertThat(impl).isNotNull() |
| 59 | } |
Sushil Nath | 8aa2199 | 2020-05-20 15:04:10 -0700 | [diff] [blame] | 60 | |
| 61 | @Test |
Paul Rohde | 4ae9179 | 2020-06-24 12:40:57 -0700 | [diff] [blame^] | 62 | fun testAcquireSession() = runBlocking { |
Sushil Nath | 8aa2199 | 2020-05-20 15:04:10 -0700 | [diff] [blame] | 63 | val session = impl.acquireSession() |
| 64 | assertThat(session).isNotNull() |
| 65 | } |
| 66 | |
| 67 | @Test |
| 68 | fun testAcquireSessionOrNull() { |
| 69 | val session = impl.acquireSessionOrNull() |
| 70 | assertThat(session).isNotNull() |
| 71 | } |
| 72 | |
| 73 | @Test |
Paul Rohde | 4ae9179 | 2020-06-24 12:40:57 -0700 | [diff] [blame^] | 74 | fun testAcquireSessionOrNullAfterAcquireSession() = runBlocking { |
Sushil Nath | 8aa2199 | 2020-05-20 15:04:10 -0700 | [diff] [blame] | 75 | val session = impl.acquireSession() |
| 76 | assertThat(session).isNotNull() |
| 77 | |
| 78 | // Since a session is already active, an attempt to acquire another session will fail. |
| 79 | val session1 = impl.acquireSessionOrNull() |
| 80 | assertThat(session1).isNull() |
| 81 | |
| 82 | // Closing an active session should allow a new session instance to be created. |
| 83 | session.close() |
| 84 | |
| 85 | val session2 = impl.acquireSessionOrNull() |
| 86 | assertThat(session2).isNotNull() |
| 87 | } |
Paul Rohde | 4ae9179 | 2020-06-24 12:40:57 -0700 | [diff] [blame^] | 88 | |
| 89 | @Test |
| 90 | fun sessionSubmitsRequestsToGraphProcessor() { |
| 91 | val session = checkNotNull(impl.acquireSessionOrNull()) |
| 92 | val request = Request(listOf()) |
| 93 | session.submit(request) |
| 94 | |
| 95 | assertThat(graphProcessor.requestQueue).contains(listOf(request)) |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | fun sessionSetsRepeatingRequestOnGraphProcessor() { |
| 100 | val session = checkNotNull(impl.acquireSessionOrNull()) |
| 101 | val request = Request(listOf()) |
| 102 | session.setRepeating(request) |
| 103 | |
| 104 | assertThat(graphProcessor.repeatingRequest).isSameInstanceAs(request) |
| 105 | } |
| 106 | |
| 107 | @Test |
| 108 | fun sessionAbortsRequestOnGraphProcessor() { |
| 109 | val session = checkNotNull(impl.acquireSessionOrNull()) |
| 110 | val request = Request(listOf()) |
| 111 | session.submit(request) |
| 112 | session.abort() |
| 113 | |
| 114 | assertThat(graphProcessor.requestQueue).isEmpty() |
| 115 | } |
| 116 | |
| 117 | @Test |
| 118 | fun closingSessionDoesNotCloseGraphProcessor() { |
| 119 | val session = impl.acquireSessionOrNull() |
| 120 | checkNotNull(session).close() |
| 121 | |
| 122 | assertThat(graphProcessor.closed).isFalse() |
| 123 | } |
| 124 | |
| 125 | @Test |
| 126 | fun closingCameraGraphClosesGraphProcessor() { |
| 127 | impl.close() |
| 128 | assertThat(graphProcessor.closed).isTrue() |
| 129 | } |
| 130 | |
| 131 | @Test |
| 132 | fun stoppingCameraGraphStopsGraphProcessor() { |
| 133 | assertThat(graphProcessor.active).isFalse() |
| 134 | impl.start() |
| 135 | assertThat(graphProcessor.active).isTrue() |
| 136 | impl.stop() |
| 137 | assertThat(graphProcessor.active).isFalse() |
| 138 | impl.start() |
| 139 | assertThat(graphProcessor.active).isTrue() |
| 140 | impl.close() |
| 141 | assertThat(graphProcessor.closed).isTrue() |
| 142 | assertThat(graphProcessor.active).isFalse() |
| 143 | } |
Paul Rohde | c971909 | 2020-05-09 14:52:48 -0700 | [diff] [blame] | 144 | } |