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 expected 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
16#include "stability_test.h"
17
18using namespace OHOS;
19using namespace std;
20using namespace testing::ext;
21using namespace OHOS::Camera;
22
23void StabilityTest::SetUpTestCase(void) {}
24void StabilityTest::TearDownTestCase(void) {}
25void StabilityTest::SetUp(void)
26{
27    Test_ = std::make_shared<OHOS::Camera::Test>();
28    Test_->Init();
29}
30void StabilityTest::TearDown(void)
31{
32    Test_->Close();
33}
34
35/**
36  * @tc.name: OpenCamera
37  * @tc.desc: OpenCamera, 100 times.
38  * @tc.size: MediumTest
39  * @tc.type: Function
40  */
41HWTEST_F(StabilityTest, Camera_Stability_Open_0001, TestSize.Level3)
42{
43    if (Test_->cameraDevice == nullptr) {
44        std::cout << "==========[test log] Check Performance: OpenCamera, 100 times."<< std::endl;
45        std::vector<int> FailTimes;
46        Test_->service->GetCameraIds(Test_->cameraIds);
47        Test_->CreateDeviceCallback();
48        for (int i = 0; i < 100; i++) {
49            std::cout << "Running " << i << " time" << std::endl;
50            Test_->rc =
51                Test_->service->OpenCamera(Test_->cameraIds.front(), Test_->deviceCallback, Test_->cameraDevice);
52            EXPECT_EQ(Test_->rc, Camera::NO_ERROR);
53            EXPECT_EQ(true, Test_->cameraDevice != nullptr);
54            if (Test_->rc != Camera::NO_ERROR) {
55                FailTimes.push_back(i);
56            }
57        }
58        std::cout << "Total fail times: "<< FailTimes.size() << ", at :" << std::endl;
59        for (auto it = FailTimes.begin(); it != FailTimes.end(); ++it) {
60            std::cout << *it << std::endl;
61        }
62    }
63}
64
65/**
66  * @tc.name: Preview
67  * @tc.desc: Preview for 100 times.
68  * @tc.size: MediumTest
69  * @tc.type: Function
70  */
71HWTEST_F(StabilityTest, Camera_Stability_Preview_0001, TestSize.Level3)
72{
73    std::cout << "==========[test log] Check Performance: Preview for 100 times." << std::endl;
74    for (int i = 1; i < 101; i++) {
75        std::cout << "Running " << i << " time" << std::endl;
76        std::cout << "==========[test log] Check Performance: Preview: " << i << " times. " << std::endl;
77        // Turn on the camera
78        Test_->Open();
79        // Configure two streams of information
80        Test_->intents = {Camera::PREVIEW};
81        Test_->StartStream(Test_->intents);
82        // Capture preview stream
83        Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
84        // post-processing
85        Test_->captureIds = {Test_->captureId_preview};
86        Test_->streamIds = {Test_->streamId_preview};
87        Test_->StopStream(Test_->captureIds, Test_->streamIds);
88        Test_->StopConsumer(Test_->intents);
89    }
90    Test_->Close();
91}
92
93/**
94  * @tc.name: preview and capture stream, for 100 times.
95  * @tc.desc: Commit 2 streams together, Preview and still_capture streams, for 100 times.
96  * @tc.size: MediumTest
97  * @tc.type: Function
98  */
99HWTEST_F(StabilityTest, Camera_Stability_Capture_0001, TestSize.Level3)
100{
101    std::cout << "==========[test log] Commit 2 streams together, Preview and still_capture streams,";
102    std::cout << " for 100 times." << std::endl;
103    // Turn on the camera
104    Test_->Open();
105    for (int i = 1; i < 101; i++) {
106        std::cout << "Running " << i << " time" << std::endl;
107        // Configure two streams of information
108        Test_->intents = {Camera::PREVIEW, Camera::STILL_CAPTURE};
109        Test_->StartStream(Test_->intents);
110        // Capture preview stream
111        Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
112        // Circulate the camera stream to capture 100 times
113        Test_->StartCapture(Test_->streamId_capture, Test_->captureId_capture, false, false);
114        // post-processing
115        Test_->captureIds = {Test_->captureId_preview};
116        Test_->streamIds.push_back(Test_->streamId_preview);
117        Test_->streamIds.push_back(Test_->streamId_capture);
118        Test_->StopStream(Test_->captureIds, Test_->streamIds);
119        Test_->StopConsumer(Test_->intents);
120    }
121    Test_->Close();
122}
123
124/**
125  * @tc.name: preview and capture
126  * @tc.desc: Dynamic capture start&stop, Preview and still_capture streams.
127  * @tc.size: MediumTest
128  * @tc.type: Function
129  */
130HWTEST_F(StabilityTest, Camera_Stability_Capture_0002, TestSize.Level3)
131{
132    std::cout << "==========[test log] Dynamic capture start&stop, ";
133    std::cout << "Preview and still_capture streams for 100 times." << std::endl;
134    // Turn on the camera
135    Test_->Open();
136    for (int i = 1; i < 101; i++) {
137        std::cout << "Running " << i << " time" << std::endl;
138        // start preview stream
139        Test_->intents = {Camera::PREVIEW};
140        Test_->StartStream(Test_->intents);
141        // Get preview
142        Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
143        // start stream
144        Test_->intents = {Camera::STILL_CAPTURE};
145        Test_->StartStream(Test_->intents);
146        // Start capture
147        Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
148        Test_->StartCapture(Test_->streamId_capture, Test_->captureId_capture, false, true);
149        // release stream
150        Test_->captureIds = {Test_->captureId_preview, Test_->captureId_capture};
151        Test_->streamIds.push_back(Test_->streamId_preview);
152        Test_->streamIds.push_back(Test_->streamId_capture);
153        Test_->StopStream(Test_->captureIds, Test_->streamIds);
154        Test_->StopConsumer({Camera::PREVIEW, Camera::STILL_CAPTURE});
155    }
156    Test_->Close();
157}
158
159/**
160  * @tc.name: Preview + Video stream, for 100 times.
161  * @tc.desc: Preview + video, commit together, for 100 times.
162  * @tc.size: MediumTest
163  * @tc.type: Function
164  */
165HWTEST_F(StabilityTest, Camera_Stability_Video_0001, TestSize.Level3)
166{
167    std::cout << "==========[test log] Performance: Preview + video, commit together, 100 times." << std::endl;
168    // Turn on the camera
169    Test_->Open();
170    for (int i = 1; i < 101; i++) {
171        std::cout << "Running " << i << " time" << std::endl;
172        std::cout << "==========[test log] Performance: Preview + video, commit together, success." << std::endl;
173        // Configure two streams of information
174        Test_->intents = {Camera::PREVIEW, Camera::VIDEO};
175        Test_->StartStream(Test_->intents);
176        // Capture preview stream
177        Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
178        // Capture video stream
179        Test_->StartCapture(Test_->streamId_video, Test_->captureId_video, false, true);
180        // post-processing
181        Test_->captureIds = {Test_->captureId_preview, Test_->captureId_video};
182        Test_->streamIds.push_back(Test_->streamId_preview);
183        Test_->streamIds.push_back(Test_->streamId_video);
184        Test_->StopStream(Test_->captureIds, Test_->streamIds);
185        Test_->StopConsumer(Test_->intents);
186    }
187    Test_->Close();
188}
189
190/**
191  * @tc.name: Video
192  * @tc.desc: Dynamic Video start&stop, for 100 times, success.
193  * @tc.size: MediumTest
194  * @tc.type: Function
195  */
196HWTEST_F(StabilityTest, Camera_Stability_Video_0002, TestSize.Level3)
197{
198    std::cout << "==========[test log]Check video: Dynamic Video start&stop, for 100 times, success." << std::endl;
199    for (int i = 0; i < 100; i++) {
200        // Turn on the camera
201        Test_->Open();
202        std::cout << "Running " << i << " time" << std::endl;
203        // start preview stream
204        Test_->intents = {Camera::PREVIEW};
205        Test_->StartStream(Test_->intents);
206        // Get preview
207        Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
208        // start stream
209        Test_->intents = {Camera::VIDEO};
210        Test_->StartStream(Test_->intents);
211        Test_->StopConsumer(Test_->intents);
212        // Start capture
213        Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
214        Test_->StartCapture(Test_->streamId_video, Test_->captureId_video, false, true);
215        // release stream
216        Test_->captureIds = {Test_->captureId_preview, Test_->captureId_video};
217        Test_->streamIds.push_back(Test_->streamId_preview);
218        Test_->streamIds.push_back(Test_->streamId_video);
219        Test_->intents = {Camera::PREVIEW};
220        Test_->StopStream(Test_->captureIds, Test_->streamIds);
221        Test_->StopConsumer(Test_->intents);
222        Test_->Close();
223    }
224}
225
226/**
227  * @tc.name: set 3A 100 times
228  * @tc.desc: set 3A 100 times, check result.
229  * @tc.size: MediumTest
230  * @tc.type: Function
231  */
232HWTEST_F(StabilityTest, Camera_Stability_3a_0001, TestSize.Level3)
233{
234    std::cout << "==========[test log] Check Performance: Set 3A 100 times, check result." << std::endl;
235    Test_->Open();
236    // Issue 3A parameters
237    std::shared_ptr<Camera::CameraSetting> meta = std::make_shared<Camera::CameraSetting>(100, 2000);
238    std::vector<uint8_t> awbMode = {
239        OHOS_CAMERA_AWB_MODE_OFF,
240        OHOS_CAMERA_AWB_MODE_AUTO,
241        OHOS_CAMERA_AWB_MODE_INCANDESCENT,
242        OHOS_CAMERA_AWB_MODE_FLUORESCENT,
243        OHOS_CAMERA_AWB_MODE_WARM_FLUORESCENT,
244        OHOS_CAMERA_AWB_MODE_DAYLIGHT,
245        OHOS_CAMERA_AWB_MODE_CLOUDY_DAYLIGHT,
246        OHOS_CAMERA_AWB_MODE_TWILIGHT,
247        OHOS_CAMERA_AWB_MODE_SHADE
248    };
249    for (int round = 0; round < 10; round++) {
250        for (int i = 0; i < awbMode.size(); i++) {
251            int times = (round * 10) + i + 1;
252            std::cout << "==========[test log] Check Performance: Set 3A Times: " << times << std::endl;
253            meta->addEntry(OHOS_CONTROL_AWB_MODE, &awbMode.at(i), 1);
254            std::cout << "==========[test log] UpdateSettings, awb mode :" << awbMode.at(i) << std::endl;
255            Test_->rc = Test_->cameraDevice->UpdateSettings(meta);
256            if (Test_->rc == Camera::NO_ERROR) {
257                std::cout << "==========[test log] Check Performance: UpdateSettings success." << std::endl;
258            } else {
259                std::cout << "==========[test log] Check Performance: UpdateSettings fail, , at the " << (i+1);
260                std::cout <<"times, RetCode is " << Test_->rc << std::endl;
261            }
262            sleep(1);
263        }
264    }
265    Test_->Close();
266}
267
268/**
269  * @tc.name: flashlight
270  * @tc.desc: Turn on and off the flashlight, for 1000 times.
271  * @tc.size: MediumTest
272  * @tc.type: Function
273  */
274HWTEST_F(StabilityTest, Camera_Stability_Flashlight_0001, TestSize.Level3)
275{
276    std::cout << "==========[test log]Performance: Turn on and off the flashlight, 1000 times." << std::endl;
277    Test_->GetCameraAbility();
278    // Cycle to turn on and off the flashlight
279    for (int i = 0; i < 1000; i++) {
280        std::cout <<"times: " << i << std::endl;
281        bool status = true;
282        Test_->rc = Test_->service->SetFlashlight(Test_->cameraIds.front(), status);
283        if (Test_->rc != Camera::NO_ERROR) {
284            std::cout << "==========[test log] Check Performance: Flashlight turn on fail, at the " << (i+1);
285            std::cout <<"times, RetCode is " << Test_->rc << std::endl;
286        }
287        status = false;
288        Test_->rc = Test_->service->SetFlashlight(Test_->cameraIds.front(), status);
289        if (Test_->rc != Camera::NO_ERROR) {
290            std::cout << "==========[test log] Check Performance: Flashlight turn off fail, at the " <<(i+1);
291            std::cout<<"times, RetCode is " << Test_->rc << std::endl;
292        }
293        sleep(1);
294    }
295    Test_->Close();
296}
297