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#include "flashlight_test.h"
16
17using namespace OHOS;
18using namespace std;
19using namespace testing::ext;
20using namespace OHOS::Camera;
21
22void FlashlightTest::SetUpTestCase(void) {}
23void FlashlightTest::TearDownTestCase(void) {}
24void FlashlightTest::SetUp(void)
25{
26    Test_ = std::make_shared<OHOS::Camera::Test>();
27    Test_->Init();
28    Test_->GetCameraAbility();
29}
30void FlashlightTest::TearDown(void)
31{
32    Test_->Close();
33}
34
35/**
36  * @tc.name: flashlight
37  * @tc.desc: Turn on the flashlight, not the camera, success.
38  * @tc.size: MediumTest
39  * @tc.type: Function
40  */
41HWTEST_F(FlashlightTest, Camera_Flashlight_0001, TestSize.Level0)
42{
43    std::cout << "==========[test log]Turn on the flashlight, not the camera, success." << std::endl;
44    Test_->status = true;
45    Test_->rc = Test_->service->SetFlashlight(Test_->cameraIds.front(), Test_->status);
46    EXPECT_EQ(Test_->rc, Camera::NO_ERROR);
47    sleep(3); // sleep for 3 seconds
48}
49
50/**
51  * @tc.name: flashlight
52  * @tc.desc: Turn off the flashlight, not the camera, success.
53  * @tc.size: MediumTest
54  * @tc.type: Function
55  */
56HWTEST_F(FlashlightTest, Camera_Flashlight_0002, TestSize.Level0)
57{
58    std::cout << "==========[test log]Flashlight turn off, not the camera, success." << std::endl;
59    std::cout << "==========[test log]1. turn on flashlight for 5s." << std::endl;
60    Test_->status = true;
61    Test_->rc = Test_->service->SetFlashlight(Test_->cameraIds.front(), Test_->status);
62    EXPECT_EQ(Test_->rc, Camera::NO_ERROR);
63    sleep(3); // sleep for 3 seconds
64    std::cout << "==========[test log]2. turn off flashlight." << std::endl;
65    Test_->status = false;
66    Test_->rc = Test_->service->SetFlashlight(Test_->cameraIds.front(), Test_->status);
67    EXPECT_EQ(Test_->rc, Camera::NO_ERROR);
68}
69
70/**
71  * @tc.name: flashlight
72  * @tc.desc: Turn on/off the flashlight for 10 times, not the camera, success.
73  * @tc.size: MediumTest
74  * @tc.type: Function
75  */
76HWTEST_F(FlashlightTest, Camera_Flashlight_0003, TestSize.Level3)
77{
78    std::cout << "==========[test log]Turn on/off the flashlight for 10 times,";
79    std::cout <<" not the camera, success." << std::endl;
80    for (int i = 0; i < 10; i++) {
81        if (i%2 == 0) {
82            Test_->status = true;
83        } else {
84            Test_->status = false;
85        }
86        Test_->rc = Test_->service->SetFlashlight(Test_->cameraIds.front(), Test_->status);
87        if (Test_->rc != Camera::NO_ERROR) {
88            std::cout << "==========[test log]Flashlight turn on fail, at the ";
89            std::cout << (i+1) <<"times, RetCode is " << Test_->rc << std::endl;
90        }
91        EXPECT_EQ(Test_->rc, Camera::NO_ERROR);
92    }
93}
94
95/**
96  * @tc.name: flashlight
97  * @tc.desc: Open the camera, preview, then turn on the flashlight, expected return errorcode.
98  * @tc.size: MediumTest
99  * @tc.type: Function
100  */
101HWTEST_F(FlashlightTest, Camera_Flashlight_0010, TestSize.Level1)
102{
103    std::cout << "==========[test log]Open the camera, preview, then turn on the flashlight,";
104    std::cout <<" expected return errorcode." << std::endl;
105    Test_->Open();
106    // Start stream
107    Test_->intents = {Camera::PREVIEW};
108    Test_->StartStream(Test_->intents);
109    // Get preview
110    Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
111    Test_->status = true;
112    Test_->rc = Test_->service->SetFlashlight(Test_->cameraIds.front(), Test_->status);
113    EXPECT_EQ(Test_->rc, Camera::METHOD_NOT_SUPPORTED);
114    if (Test_->rc == Camera::NO_ERROR) {
115        std::cout << "==========[test log]turn on the flashlight success." << std::endl;
116    } else {
117        std::cout << "==========[test log]turn on the flashlight fail, Test_->rc = ";
118        std::cout << Test_->rc << std::endl;
119    }
120    std::cout << "==========[test log]turn off/on flashlight fail, preview go on, for 5s." << std::endl;
121    sleep(3); // sleep for 3 seconds
122    // release stream
123    Test_->captureIds = {Test_->captureId_preview};
124    Test_->streamIds = {Test_->streamId_preview};
125    Test_->StopStream(Test_->captureIds, Test_->streamIds);
126    Test_->StopConsumer(Test_->intents);
127}
128
129/**
130  * @tc.name: flashlight
131  * @tc.desc: Open the camera, preview, then turn off the flashlight, expected return errorcode.
132  * @tc.size: MediumTest
133  * @tc.type: Function
134  */
135HWTEST_F(FlashlightTest, Camera_Flashlight_0011, TestSize.Level1)
136{
137    std::cout << "==========[test log]Open the camera, preview, then turn on the flashlight,";
138    std::cout <<" expected return errorcode." << std::endl;
139    Test_->Open();
140    // Start stream
141    Test_->intents = {Camera::PREVIEW};
142    Test_->StartStream(Test_->intents);
143    // Get preview
144    Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
145    Test_->status = false;
146    Test_->rc = Test_->service->SetFlashlight(Test_->cameraIds.front(), Test_->status);
147    EXPECT_EQ(Test_->rc, Camera::METHOD_NOT_SUPPORTED);
148    if (Test_->rc == Camera::NO_ERROR) {
149        std::cout << "==========[test log]turn off the flashlight success." << std::endl;
150    } else {
151        std::cout << "==========[test log]turn off the flashlight fail,rc = " << Test_->rc << std::endl;
152    }
153    std::cout << "==========[test log]turn off the flashlight fail, preview go on, for 5s." << std::endl;
154    sleep(3); // sleep for 3 seconds
155    // release stream
156    Test_->captureIds = {Test_->captureId_preview};
157    Test_->streamIds = {Test_->streamId_preview};
158    Test_->StopStream(Test_->captureIds, Test_->streamIds);
159    Test_->StopConsumer(Test_->intents);
160}
161
162/**
163  * @tc.name: flashlight
164  * @tc.desc: Turn on the flashlight ,then open the camera, the light will turn off.
165  * @tc.size: MediumTest
166  * @tc.type: Function
167  */
168HWTEST_F(FlashlightTest, Camera_Flashlight_0020, TestSize.Level1)
169{
170    std::cout << "==========[test log]Turn on the flashlight for 5s,then open the camera,";
171    std::cout <<" the light will turn off." << std::endl;
172    Test_->status = true;
173    Test_->rc = Test_->service->SetFlashlight(Test_->cameraIds.front(), Test_->status);
174    sleep(3); // sleep for 3 seconds
175    // Open the camera, preview
176    Test_->Open();
177    // query the status of the flashlight, currently only supports callback reporting
178    std::cout << "==========[test log]check the flashlight status is off." << std::endl;
179    sleep(3); // sleep for 3 seconds
180}
181