1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "preview_test.h"
16
17 using namespace OHOS;
18 using namespace std;
19 using namespace testing::ext;
20 using namespace OHOS::Camera;
21
SetUpTestCase(void)22 void PreviewTest::SetUpTestCase(void) {}
TearDownTestCase(void)23 void PreviewTest::TearDownTestCase(void) {}
SetUp(void)24 void PreviewTest::SetUp(void)
25 {
26 Test_ = std::make_shared<OHOS::Camera::Test>();
27 Test_->Init();
28 Test_->Open();
29 }
TearDown(void)30 void PreviewTest::TearDown(void)
31 {
32 Test_->Close();
33 }
34
35 /**
36 * @tc.name: Preview
37 * @tc.desc: Preview stream, 640*480, expected success.
38 * @tc.size: MediumTest
39 * @tc.type: Function
40 */
41 HWTEST_F(PreviewTest, Camera_Preview_0001, TestSize.Level0)
42 {
43 std::cout << "==========[test log]Preview stream, 640*480, expected success." << std::endl;
44 // Start stream
45 Test_->intents = {Camera::PREVIEW};
46 Test_->StartStream(Test_->intents);
47 // Get preview
48 Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
49 // Release stream
50 Test_->captureIds = {Test_->captureId_preview};
51 Test_->streamIds = {Test_->streamId_preview};
52 Test_->StopStream(Test_->captureIds, Test_->streamIds);
53 Test_->StopConsumer(Test_->intents);
54 }
55
56 /**
57 * @tc.name: Preview
58 * @tc.desc: Preview stream, 1280*960, expected success.
59 * @tc.size: MediumTest
60 * @tc.type: Function
61 */
62 HWTEST_F(PreviewTest, Camera_Preview_0004, TestSize.Level2)
63 {
64 std::cout << "==========[test log]Preview stream, 1280*960, expected success." << std::endl;
65
66 // Create and get streamOperator information
67 Test_->CreateStreamOperatorCallback();
68 Test_->rc = Test_->cameraDevice->GetStreamOperator(Test_->streamOperatorCallback, Test_->streamOperator);
69 EXPECT_EQ(Test_->rc, Camera::NO_ERROR);
70 // Create data flow
71 Test_->streamInfo = std::make_shared<StreamInfo>();
72 Test_->streamInfo->streamId_ = Test_->streamId_preview;
73 Test_->streamInfo->width_ = 1280;
74 Test_->streamInfo->height_ = 960;
75 Test_->streamInfo->dataspace_ = 8;
76 Test_->StreamInfoFormat();
77 Test_->streamInfo->intent_ = Camera::PREVIEW;
78 std::shared_ptr<OHOS::Camera::Test::StreamConsumer> preview_consumer =
79 std::make_shared<OHOS::Camera::Test::StreamConsumer>();
80 #ifdef CAMERA_BUILT_ON_OHOS_LITE
__anon98d3f5760102(OHOS::SurfaceBuffer* buffer) 81 Test_->streamInfo->bufferQueue_ = preview_consumer->CreateProducer([this](OHOS::SurfaceBuffer* buffer) {
82 Test_->SaveYUV("preview", buffer->GetVirAddr(), buffer->GetSize());
83 });
84 #else
__anon98d3f5760202(void* addr, uint32_t size) 85 Test_->streamInfo->bufferQueue_ = preview_consumer->CreateProducer([this](void* addr, uint32_t size) {
86 Test_->SaveYUV("preview", addr, size);
87 });
88 #endif
89 Test_->streamInfo->bufferQueue_->SetQueueSize(8);
90 Test_->consumerMap_[Camera::PREVIEW] = preview_consumer;
91 Test_->streamInfo->tunneledMode_ = 5;
92 std::vector<std::shared_ptr<StreamInfo>>().swap(Test_->streamInfos);
93 Test_->streamInfos.push_back(Test_->streamInfo);
94 Test_->rc = Test_->streamOperator->CreateStreams(Test_->streamInfos);
95 EXPECT_EQ(Test_->rc, Camera::NO_ERROR);
96 // Flow distribution
97 Test_->rc = Test_->streamOperator->CommitStreams(Camera::NORMAL, Test_->ability);
98 EXPECT_EQ(Test_->rc, Camera::NO_ERROR);
99 // Get preview
100 Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
101 // Release stream
102 Test_->captureIds = {Test_->captureId_preview};
103 Test_->streamIds = {Test_->streamId_preview};
104 Test_->StopStream(Test_->captureIds, Test_->streamIds);
105 Test_->StopConsumer(Test_->intents);
106 }
107
108 /**
109 * @tc.name: Preview
110 * @tc.desc: GetStreamOperator success.
111 * @tc.size: MediumTest
112 * @tc.type: Function
113 */
114 HWTEST_F(PreviewTest, Camera_Preview_0010, TestSize.Level0)
115 {
116 std::cout << "==========[test log]GetStreamOperator success." << std::endl;
117 // Create and get streamOperator information
118 Test_->CreateStreamOperatorCallback();
119 Test_->rc = Test_->cameraDevice->GetStreamOperator(Test_->streamOperatorCallback, Test_->streamOperator);
120 EXPECT_EQ(false, Test_->rc != Camera::NO_ERROR || Test_->streamOperator == nullptr);
121 }
122
123 /**
124 * @tc.name: Preview
125 * @tc.desc: GetStreamOperator, input nullptr.
126 * @tc.size: MediumTest
127 * @tc.type: Function
128 */
129 HWTEST_F(PreviewTest, Camera_Preview_0011, TestSize.Level2)
130 {
131 std::cout << "==========[test log]GetStreamOperator, input nullptr." << std::endl;
132 // Create and get streamOperator information
133 Test_->rc = Test_->cameraDevice->GetStreamOperator(nullptr, Test_->streamOperator);
134 std::cout << "GetStreamOperator's rc " << Test_->rc << std::endl;
135 EXPECT_EQ(Camera::CamRetCode::INVALID_ARGUMENT, Test_->rc);
136 }
137
138 /**
139 * @tc.name: Preview
140 * @tc.desc: Preview, CommitStreams Metadata = nullptr.
141 * @tc.size: MediumTest
142 * @tc.type: Function
143 */
144 HWTEST_F(PreviewTest, Camera_Preview_0040, TestSize.Level2)
145 {
146 std::cout << "==========[test log]Preview, CommitStreams Metadata = nullptr." << std::endl;
147 // Create and get streamOperator information
148 Test_->CreateStreamOperatorCallback();
149 Test_->rc = Test_->cameraDevice->GetStreamOperator(Test_->streamOperatorCallback, Test_->streamOperator);
150 EXPECT_EQ(Test_->rc, Camera::NO_ERROR);
151 // Flow distribution
152 Test_->rc = Test_->streamOperator->CommitStreams(Camera::NORMAL, nullptr);
153 EXPECT_EQ(true, Test_->rc == INVALID_ARGUMENT);
154 }
155
156 /**
157 * @tc.name: Preview
158 * @tc.desc: Preview, CommitStreams without CreateStreams, expected fail.
159 * @tc.size: MediumTest
160 * @tc.type: Function
161 */
162 HWTEST_F(PreviewTest, Camera_Preview_0050, TestSize.Level2)
163 {
164 std::cout << "==========[test log]Preview, CommitStreams no CreateStreams, expected fail." << std::endl;
165 // Create and get streamOperator information
166 Test_->CreateStreamOperatorCallback();
167 Test_->rc = Test_->cameraDevice->GetStreamOperator(Test_->streamOperatorCallback, Test_->streamOperator);
168 EXPECT_EQ(Test_->rc, Camera::NO_ERROR);
169 // Flow distribution
170 Test_->rc = Test_->streamOperator->CommitStreams(Camera::NORMAL, Test_->ability);
171 std::cout << "streamOperator->CommitStreams's rc " << Test_->rc << std::endl;
172 EXPECT_EQ(true, Test_->rc == INVALID_ARGUMENT);
173 }
174
175 /**
176 * @tc.name: Preview
177 * @tc.desc: Preview and release streams, success.
178 * @tc.size: MediumTest
179 * @tc.type: Function
180 */
181 HWTEST_F(PreviewTest, Camera_Preview_0060, TestSize.Level1)
182 {
183 std::cout << "==========[test log]Preview and release streams, success." << std::endl;
184 // Start stream
185 Test_->intents = {Camera::PREVIEW};
186 Test_->StartStream(Test_->intents);
187 // Get preview
188 Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
189 // Release stream
190 Test_->captureIds = {Test_->captureId_preview};
191 Test_->streamIds = {Test_->streamId_preview};
192 Test_->StopStream(Test_->captureIds, Test_->streamIds);
193 Test_->StopConsumer(Test_->intents);
194 }
195
196 /**
197 * @tc.name: Preview
198 * @tc.desc: ReleaseStreams no exist streamID, expect success.
199 * @tc.size: MediumTest
200 * @tc.type: Function
201 */
202 HWTEST_F(PreviewTest, Camera_Preview_0062, TestSize.Level2)
203 {
204 std::cout << "==========[test log]ReleaseStreams no exist streamID, expect success." << std::endl;
205 // Create and get streamOperator information
206 Test_->CreateStreamOperatorCallback();
207 Test_->rc = Test_->cameraDevice->GetStreamOperator(Test_->streamOperatorCallback, Test_->streamOperator);
208 EXPECT_EQ(false, Test_->rc != Camera::NO_ERROR || Test_->streamOperator == nullptr);
209 Test_->streamInfo = std::make_shared<StreamInfo>();
210 Test_->rc = Test_->streamOperator->ReleaseStreams({9999});
211 std::cout << "streamOperator->ReleaseStreams's rc " << Test_->rc << std::endl;
212 EXPECT_EQ(true, Test_->rc == Camera::NO_ERROR);
213 }
214
215 /**
216 * @tc.name: Preview
217 * @tc.desc: CancelCapture without Create capture.
218 * @tc.size: MediumTest
219 * @tc.type: Function
220 */
221 HWTEST_F(PreviewTest, Camera_Preview_0090, TestSize.Level2)
222 {
223 std::cout << "==========[test log]CancelCapture without Create capture ." << std::endl;
224 // Start stream
225 Test_->intents = {Camera::PREVIEW};
226 Test_->StartStream(Test_->intents);
227 // Do not capture the preview image, directly cancel the capture
228 int captureId = 100;
229 Test_->rc = Test_->streamOperator->CancelCapture(captureId);
230 EXPECT_EQ(true, Test_->rc == Camera::INVALID_ARGUMENT);
231 if (Test_->rc == Camera::NO_ERROR) {
232 std::cout << "==========[test log]CancelCapture success." << std::endl;
233 } else {
234 std::cout << "==========[test log]CancelCapture fail, rc = " << Test_->rc << std::endl;
235 }
236 // Release stream
237 Test_->rc = Test_->streamOperator->ReleaseStreams({Test_->streamId_preview});
238 EXPECT_EQ(Test_->rc, Camera::NO_ERROR);
239 if (Test_->rc == Camera::NO_ERROR) {
240 std::cout << "==========[test log]ReleaseStreams success." << std::endl;
241 } else {
242 std::cout << "==========[test log]ReleaseStreams fail, rc = " << Test_->rc << std::endl;
243 }
244 }
245
246 /**
247 * @tc.name: Preview
248 * @tc.desc: Release streams, then creatCapture.
249 * @tc.size: MediumTest
250 * @tc.type: Function
251 */
252 HWTEST_F(PreviewTest, Camera_Preview_0091, TestSize.Level2)
253 {
254 std::cout << "==========[test log]Create capture, then release streams." << std::endl;
255 // Start stream
256 Test_->intents = {Camera::PREVIEW};
257 Test_->StartStream(Test_->intents);
258 // Release stream
259 Test_->rc = Test_->streamOperator->ReleaseStreams({Test_->streamId_preview});
260 EXPECT_EQ(Test_->rc, Camera::NO_ERROR);
261 if (Test_->rc == Camera::NO_ERROR) {
262 std::cout << "==========[test log]ReleaseStreams success." << std::endl;
263 } else {
264 std::cout << "==========[test log]ReleaseStreams fail, rc = " << Test_->rc << std::endl;
265 }
266
267 // Get preview
268 int captureId = 100;
269 Test_->captureInfo = std::make_shared<CaptureInfo>();
270 Test_->captureInfo->streamIds_ = {Test_->streamId_preview};
271 Test_->captureInfo->captureSetting_ = Test_->ability;
272 Test_->captureInfo->enableShutterCallback_ = false;
273 bool isStreaming = true;
274 Test_->rc = Test_->streamOperator->Capture(captureId, Test_->captureInfo, isStreaming);
275 EXPECT_EQ(Test_->rc, Camera::INVALID_ARGUMENT);
276 // Turn off the device
277 Test_->cameraDevice->Close();
278 std::cout << "cameraDevice->Close" << std::endl;
279 }
280
281 /**
282 * @tc.name: Preview
283 * @tc.desc: The same CaptureID, Create capture twice, expected failed.
284 * @tc.size: MediumTest
285 * @tc.type: Function
286 */
287 HWTEST_F(PreviewTest, Camera_Preview_0092, TestSize.Level2)
288 {
289 std::cout << "==========[test log]The same CaptureID, Create capture twice, expected failed." << std::endl;
290 // Start stream
291 Test_->intents = {Camera::PREVIEW};
292 Test_->StartStream(Test_->intents);
293 // Get preview
294 Test_->captureInfo = std::make_shared<Camera::CaptureInfo>();
295 Test_->captureInfo->streamIds_ = {Test_->streamId_preview};
296 Test_->captureInfo->captureSetting_ = Test_->ability;
297 Test_->captureInfo->enableShutterCallback_ = true;
298 // The same captureId, the first time to take a photo
299 Test_->rc = Test_->streamOperator->Capture(Test_->captureId_preview, Test_->captureInfo, true);
300 EXPECT_EQ(true, Test_->rc == Camera::NO_ERROR);
301 if (Test_->rc == Camera::NO_ERROR) {
302 std::cout << "==========[test log]check Capture: Capture success, " << Test_->captureId_preview << std::endl;
303 } else {
304 std::cout << "==========[test log]check Capture: Capture fail, rc = " << Test_->rc << std::endl;
305 }
306 sleep(1);
307 // The same captureId, take the second photo
308 Test_->rc = Test_->streamOperator->Capture(Test_->captureId_preview, Test_->captureInfo, true);
309 EXPECT_EQ(false, Test_->rc == Camera::NO_ERROR);
310 if (Test_->rc == Camera::NO_ERROR) {
311 std::cout << "==========[test log]check Capture: Capture success, " << Test_->captureId_preview << std::endl;
312 } else {
313 std::cout << "==========[test log]check Capture: Capture fail, rc = " << Test_->rc << std::endl;
314 }
315 sleep(1);
316 Test_->rc = Test_->streamOperator->CancelCapture(Test_->captureId_preview);
317 EXPECT_EQ(true, Test_->rc == Camera::NO_ERROR);
318 if (Test_->rc == Camera::NO_ERROR) {
319 std::cout << "======[test log]check Capture: CancelCapture success," << Test_->captureId_preview << std::endl;
320 } else {
321 std::cout << "==========[test log]check Capture: CancelCapture fail, rc = " << Test_->rc << std::endl;
322 std::cout << "captureId = " << Test_->captureId_preview << std::endl;
323 }
324 // Release stream
325 Test_->streamIds = {Test_->streamId_preview};
326 std::cout << "Test_->streamIds size = " << Test_->streamIds.size() << std::endl;
327 Test_->rc = Test_->streamOperator->ReleaseStreams(Test_->streamIds);
328 if (Test_->rc == Camera::NO_ERROR) {
329 std::cout << "==========[test log]check Capture: ReleaseStreams success." << std::endl;
330 } else {
331 std::cout << "==========[test log]check Capture: ReleaseStreams fail, rc = " << Test_->rc << std::endl;
332 }
333 EXPECT_EQ(true, Test_->rc == Camera::NO_ERROR);
334 }
335
336 /**
337 * @tc.name: Preview
338 * @tc.desc: Different captureIDs, Create capture,expected success.
339 * @tc.size: MediumTest
340 * @tc.type: Function
341 */
342 HWTEST_F(PreviewTest, Camera_Preview_0093, TestSize.Level2)
343 {
344 std::cout << "==========[test log]Different captureIDs, Create capture,expected success." << std::endl;
345 // Start stream
346 Test_->intents = {Camera::PREVIEW};
347 Test_->StartStream(Test_->intents);
348 // Get preview
349 Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, false);
350 Test_->StartCapture(Test_->streamId_preview, Test_->captureId_capture, false, false);
351 // Release stream
352 Test_->captureIds = {};
353 Test_->streamIds = {Test_->streamId_preview};
354 Test_->StopStream(Test_->captureIds, Test_->streamIds);
355 Test_->StopConsumer(Test_->intents);
356 }
357