1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file expected in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci#include "flashlight_test.h"
16094332d3Sopenharmony_ci
17094332d3Sopenharmony_ciusing namespace OHOS;
18094332d3Sopenharmony_ciusing namespace std;
19094332d3Sopenharmony_ciusing namespace testing::ext;
20094332d3Sopenharmony_ciusing namespace OHOS::Camera;
21094332d3Sopenharmony_ci
22094332d3Sopenharmony_civoid FlashlightTest::SetUpTestCase(void) {}
23094332d3Sopenharmony_civoid FlashlightTest::TearDownTestCase(void) {}
24094332d3Sopenharmony_civoid FlashlightTest::SetUp(void)
25094332d3Sopenharmony_ci{
26094332d3Sopenharmony_ci    Test_ = std::make_shared<OHOS::Camera::Test>();
27094332d3Sopenharmony_ci    Test_->Init();
28094332d3Sopenharmony_ci    Test_->GetCameraAbility();
29094332d3Sopenharmony_ci}
30094332d3Sopenharmony_civoid FlashlightTest::TearDown(void)
31094332d3Sopenharmony_ci{
32094332d3Sopenharmony_ci    Test_->Close();
33094332d3Sopenharmony_ci}
34094332d3Sopenharmony_ci
35094332d3Sopenharmony_ci/**
36094332d3Sopenharmony_ci  * @tc.name: flashlight
37094332d3Sopenharmony_ci  * @tc.desc: Turn on the flashlight, not the camera, success.
38094332d3Sopenharmony_ci  * @tc.size: MediumTest
39094332d3Sopenharmony_ci  * @tc.type: Function
40094332d3Sopenharmony_ci  */
41094332d3Sopenharmony_ciHWTEST_F(FlashlightTest, Camera_Flashlight_0001, TestSize.Level0)
42094332d3Sopenharmony_ci{
43094332d3Sopenharmony_ci    std::cout << "==========[test log]Turn on the flashlight, not the camera, success." << std::endl;
44094332d3Sopenharmony_ci    Test_->status = true;
45094332d3Sopenharmony_ci    Test_->rc = Test_->service->SetFlashlight(Test_->cameraIds.front(), Test_->status);
46094332d3Sopenharmony_ci    EXPECT_EQ(Test_->rc, Camera::NO_ERROR);
47094332d3Sopenharmony_ci    sleep(3); // sleep for 3 seconds
48094332d3Sopenharmony_ci}
49094332d3Sopenharmony_ci
50094332d3Sopenharmony_ci/**
51094332d3Sopenharmony_ci  * @tc.name: flashlight
52094332d3Sopenharmony_ci  * @tc.desc: Turn off the flashlight, not the camera, success.
53094332d3Sopenharmony_ci  * @tc.size: MediumTest
54094332d3Sopenharmony_ci  * @tc.type: Function
55094332d3Sopenharmony_ci  */
56094332d3Sopenharmony_ciHWTEST_F(FlashlightTest, Camera_Flashlight_0002, TestSize.Level0)
57094332d3Sopenharmony_ci{
58094332d3Sopenharmony_ci    std::cout << "==========[test log]Flashlight turn off, not the camera, success." << std::endl;
59094332d3Sopenharmony_ci    std::cout << "==========[test log]1. turn on flashlight for 5s." << std::endl;
60094332d3Sopenharmony_ci    Test_->status = true;
61094332d3Sopenharmony_ci    Test_->rc = Test_->service->SetFlashlight(Test_->cameraIds.front(), Test_->status);
62094332d3Sopenharmony_ci    EXPECT_EQ(Test_->rc, Camera::NO_ERROR);
63094332d3Sopenharmony_ci    sleep(3); // sleep for 3 seconds
64094332d3Sopenharmony_ci    std::cout << "==========[test log]2. turn off flashlight." << std::endl;
65094332d3Sopenharmony_ci    Test_->status = false;
66094332d3Sopenharmony_ci    Test_->rc = Test_->service->SetFlashlight(Test_->cameraIds.front(), Test_->status);
67094332d3Sopenharmony_ci    EXPECT_EQ(Test_->rc, Camera::NO_ERROR);
68094332d3Sopenharmony_ci}
69094332d3Sopenharmony_ci
70094332d3Sopenharmony_ci/**
71094332d3Sopenharmony_ci  * @tc.name: flashlight
72094332d3Sopenharmony_ci  * @tc.desc: Turn on/off the flashlight for 10 times, not the camera, success.
73094332d3Sopenharmony_ci  * @tc.size: MediumTest
74094332d3Sopenharmony_ci  * @tc.type: Function
75094332d3Sopenharmony_ci  */
76094332d3Sopenharmony_ciHWTEST_F(FlashlightTest, Camera_Flashlight_0003, TestSize.Level3)
77094332d3Sopenharmony_ci{
78094332d3Sopenharmony_ci    std::cout << "==========[test log]Turn on/off the flashlight for 10 times,";
79094332d3Sopenharmony_ci    std::cout <<" not the camera, success." << std::endl;
80094332d3Sopenharmony_ci    for (int i = 0; i < 10; i++) {
81094332d3Sopenharmony_ci        if (i%2 == 0) {
82094332d3Sopenharmony_ci            Test_->status = true;
83094332d3Sopenharmony_ci        } else {
84094332d3Sopenharmony_ci            Test_->status = false;
85094332d3Sopenharmony_ci        }
86094332d3Sopenharmony_ci        Test_->rc = Test_->service->SetFlashlight(Test_->cameraIds.front(), Test_->status);
87094332d3Sopenharmony_ci        if (Test_->rc != Camera::NO_ERROR) {
88094332d3Sopenharmony_ci            std::cout << "==========[test log]Flashlight turn on fail, at the ";
89094332d3Sopenharmony_ci            std::cout << (i+1) <<"times, RetCode is " << Test_->rc << std::endl;
90094332d3Sopenharmony_ci        }
91094332d3Sopenharmony_ci        EXPECT_EQ(Test_->rc, Camera::NO_ERROR);
92094332d3Sopenharmony_ci    }
93094332d3Sopenharmony_ci}
94094332d3Sopenharmony_ci
95094332d3Sopenharmony_ci/**
96094332d3Sopenharmony_ci  * @tc.name: flashlight
97094332d3Sopenharmony_ci  * @tc.desc: Open the camera, preview, then turn on the flashlight, expected return errorcode.
98094332d3Sopenharmony_ci  * @tc.size: MediumTest
99094332d3Sopenharmony_ci  * @tc.type: Function
100094332d3Sopenharmony_ci  */
101094332d3Sopenharmony_ciHWTEST_F(FlashlightTest, Camera_Flashlight_0010, TestSize.Level1)
102094332d3Sopenharmony_ci{
103094332d3Sopenharmony_ci    std::cout << "==========[test log]Open the camera, preview, then turn on the flashlight,";
104094332d3Sopenharmony_ci    std::cout <<" expected return errorcode." << std::endl;
105094332d3Sopenharmony_ci    Test_->Open();
106094332d3Sopenharmony_ci    // Start stream
107094332d3Sopenharmony_ci    Test_->intents = {Camera::PREVIEW};
108094332d3Sopenharmony_ci    Test_->StartStream(Test_->intents);
109094332d3Sopenharmony_ci    // Get preview
110094332d3Sopenharmony_ci    Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
111094332d3Sopenharmony_ci    Test_->status = true;
112094332d3Sopenharmony_ci    Test_->rc = Test_->service->SetFlashlight(Test_->cameraIds.front(), Test_->status);
113094332d3Sopenharmony_ci    EXPECT_EQ(Test_->rc, Camera::METHOD_NOT_SUPPORTED);
114094332d3Sopenharmony_ci    if (Test_->rc == Camera::NO_ERROR) {
115094332d3Sopenharmony_ci        std::cout << "==========[test log]turn on the flashlight success." << std::endl;
116094332d3Sopenharmony_ci    } else {
117094332d3Sopenharmony_ci        std::cout << "==========[test log]turn on the flashlight fail, Test_->rc = ";
118094332d3Sopenharmony_ci        std::cout << Test_->rc << std::endl;
119094332d3Sopenharmony_ci    }
120094332d3Sopenharmony_ci    std::cout << "==========[test log]turn off/on flashlight fail, preview go on, for 5s." << std::endl;
121094332d3Sopenharmony_ci    sleep(3); // sleep for 3 seconds
122094332d3Sopenharmony_ci    // release stream
123094332d3Sopenharmony_ci    Test_->captureIds = {Test_->captureId_preview};
124094332d3Sopenharmony_ci    Test_->streamIds = {Test_->streamId_preview};
125094332d3Sopenharmony_ci    Test_->StopStream(Test_->captureIds, Test_->streamIds);
126094332d3Sopenharmony_ci    Test_->StopConsumer(Test_->intents);
127094332d3Sopenharmony_ci}
128094332d3Sopenharmony_ci
129094332d3Sopenharmony_ci/**
130094332d3Sopenharmony_ci  * @tc.name: flashlight
131094332d3Sopenharmony_ci  * @tc.desc: Open the camera, preview, then turn off the flashlight, expected return errorcode.
132094332d3Sopenharmony_ci  * @tc.size: MediumTest
133094332d3Sopenharmony_ci  * @tc.type: Function
134094332d3Sopenharmony_ci  */
135094332d3Sopenharmony_ciHWTEST_F(FlashlightTest, Camera_Flashlight_0011, TestSize.Level1)
136094332d3Sopenharmony_ci{
137094332d3Sopenharmony_ci    std::cout << "==========[test log]Open the camera, preview, then turn on the flashlight,";
138094332d3Sopenharmony_ci    std::cout <<" expected return errorcode." << std::endl;
139094332d3Sopenharmony_ci    Test_->Open();
140094332d3Sopenharmony_ci    // Start stream
141094332d3Sopenharmony_ci    Test_->intents = {Camera::PREVIEW};
142094332d3Sopenharmony_ci    Test_->StartStream(Test_->intents);
143094332d3Sopenharmony_ci    // Get preview
144094332d3Sopenharmony_ci    Test_->StartCapture(Test_->streamId_preview, Test_->captureId_preview, false, true);
145094332d3Sopenharmony_ci    Test_->status = false;
146094332d3Sopenharmony_ci    Test_->rc = Test_->service->SetFlashlight(Test_->cameraIds.front(), Test_->status);
147094332d3Sopenharmony_ci    EXPECT_EQ(Test_->rc, Camera::METHOD_NOT_SUPPORTED);
148094332d3Sopenharmony_ci    if (Test_->rc == Camera::NO_ERROR) {
149094332d3Sopenharmony_ci        std::cout << "==========[test log]turn off the flashlight success." << std::endl;
150094332d3Sopenharmony_ci    } else {
151094332d3Sopenharmony_ci        std::cout << "==========[test log]turn off the flashlight fail,rc = " << Test_->rc << std::endl;
152094332d3Sopenharmony_ci    }
153094332d3Sopenharmony_ci    std::cout << "==========[test log]turn off the flashlight fail, preview go on, for 5s." << std::endl;
154094332d3Sopenharmony_ci    sleep(3); // sleep for 3 seconds
155094332d3Sopenharmony_ci    // release stream
156094332d3Sopenharmony_ci    Test_->captureIds = {Test_->captureId_preview};
157094332d3Sopenharmony_ci    Test_->streamIds = {Test_->streamId_preview};
158094332d3Sopenharmony_ci    Test_->StopStream(Test_->captureIds, Test_->streamIds);
159094332d3Sopenharmony_ci    Test_->StopConsumer(Test_->intents);
160094332d3Sopenharmony_ci}
161094332d3Sopenharmony_ci
162094332d3Sopenharmony_ci/**
163094332d3Sopenharmony_ci  * @tc.name: flashlight
164094332d3Sopenharmony_ci  * @tc.desc: Turn on the flashlight ,then open the camera, the light will turn off.
165094332d3Sopenharmony_ci  * @tc.size: MediumTest
166094332d3Sopenharmony_ci  * @tc.type: Function
167094332d3Sopenharmony_ci  */
168094332d3Sopenharmony_ciHWTEST_F(FlashlightTest, Camera_Flashlight_0020, TestSize.Level1)
169094332d3Sopenharmony_ci{
170094332d3Sopenharmony_ci    std::cout << "==========[test log]Turn on the flashlight for 5s,then open the camera,";
171094332d3Sopenharmony_ci    std::cout <<" the light will turn off." << std::endl;
172094332d3Sopenharmony_ci    Test_->status = true;
173094332d3Sopenharmony_ci    Test_->rc = Test_->service->SetFlashlight(Test_->cameraIds.front(), Test_->status);
174094332d3Sopenharmony_ci    sleep(3); // sleep for 3 seconds
175094332d3Sopenharmony_ci    // Open the camera, preview
176094332d3Sopenharmony_ci    Test_->Open();
177094332d3Sopenharmony_ci    // query the status of the flashlight, currently only supports callback reporting
178094332d3Sopenharmony_ci    std::cout << "==========[test log]check the flashlight status is off." << std::endl;
179094332d3Sopenharmony_ci    sleep(3); // sleep for 3 seconds
180094332d3Sopenharmony_ci}
181