[go: nahoru, domu]

blob: 29f0f9c7246289b09f233b288ea0f49a3d6ce6e9 [file] [log] [blame]
Paul Rohdec9719092020-05-09 14:52:48 -07001/*
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
17package androidx.camera.camera2.pipe.impl
18
19import android.content.Context
20import android.os.Build
21import androidx.camera.camera2.pipe.CameraGraph
Paul Rohde281f68b2020-05-21 10:32:03 -070022import androidx.camera.camera2.pipe.CameraId
Paul Rohde4ae91792020-06-24 12:40:57 -070023import androidx.camera.camera2.pipe.Request
24import androidx.camera.camera2.pipe.RequestTemplate
Paul Rohdea5f5b072020-05-13 09:56:40 -070025import androidx.camera.camera2.pipe.testing.CameraPipeRobolectricTestRunner
Paul Rohde4ae91792020-06-24 12:40:57 -070026import androidx.camera.camera2.pipe.testing.FakeGraphProcessor
Paul Rohdec9719092020-05-09 14:52:48 -070027import androidx.test.core.app.ApplicationProvider
28import androidx.test.filters.SmallTest
29import com.google.common.truth.Truth.assertThat
Sushil Nath8aa21992020-05-20 15:04:10 -070030import kotlinx.coroutines.runBlocking
31import org.junit.Before
Paul Rohdec9719092020-05-09 14:52:48 -070032import org.junit.Test
33import org.junit.runner.RunWith
Paul Rohdec9719092020-05-09 14:52:48 -070034import org.robolectric.annotation.Config
35import org.robolectric.annotation.internal.DoNotInstrument
36
37@SmallTest
Paul Rohde0f732922020-05-11 10:45:17 -070038@RunWith(CameraPipeRobolectricTestRunner::class)
Paul Rohdec9719092020-05-09 14:52:48 -070039@DoNotInstrument
40@Config(minSdk = Build.VERSION_CODES.LOLLIPOP)
41class CameraGraphImplTest {
Paul Rohde4ae91792020-06-24 12:40:57 -070042 private val graphProcessor = FakeGraphProcessor()
Sushil Nath8aa21992020-05-20 15:04:10 -070043 private lateinit var impl: CameraGraphImpl
44
45 @Before
46 fun setUp() {
Paul Rohdeed2fc712020-06-18 10:48:08 -070047 val config = CameraGraph.Config(
48 camera = CameraId("0"),
49 streams = listOf(),
Paul Rohde4ae91792020-06-24 12:40:57 -070050 defaultTemplate = RequestTemplate(0)
Paul Rohdeed2fc712020-06-18 10:48:08 -070051 )
Paul Rohdec9719092020-05-09 14:52:48 -070052 val context = ApplicationProvider.getApplicationContext() as Context
Paul Rohde4ae91792020-06-24 12:40:57 -070053 impl = CameraGraphImpl(context, config, graphProcessor, StreamMap())
Sushil Nath8aa21992020-05-20 15:04:10 -070054 }
Paul Rohdec9719092020-05-09 14:52:48 -070055
Sushil Nath8aa21992020-05-20 15:04:10 -070056 @Test
57 fun createCameraGraphImpl() {
Paul Rohdec9719092020-05-09 14:52:48 -070058 assertThat(impl).isNotNull()
59 }
Sushil Nath8aa21992020-05-20 15:04:10 -070060
61 @Test
Paul Rohde4ae91792020-06-24 12:40:57 -070062 fun testAcquireSession() = runBlocking {
Sushil Nath8aa21992020-05-20 15:04:10 -070063 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 Rohde4ae91792020-06-24 12:40:57 -070074 fun testAcquireSessionOrNullAfterAcquireSession() = runBlocking {
Sushil Nath8aa21992020-05-20 15:04:10 -070075 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 Rohde4ae91792020-06-24 12:40:57 -070088
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 Rohdec9719092020-05-09 14:52:48 -0700144}