1 /*
2 * Copyright (c) 2025 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
16 #include "camera_utils_utest.h"
17 #include "camera_host_selfkiller.h"
18 #include "string"
19 #include "parameters.h"
20 using namespace OHOS;
21 using namespace OHOS::CameraUtest;
22 using namespace testing::ext;
23 constexpr const char *DEBUG_SELF_KILL_PARAM_NAME = "debug.camera.setting.selfkill.enable";
SetUpTestCase(void)24 void CameraUtilsUTest::SetUpTestCase(void)
25 {
26 CAMERA_LOGD("Camera::CameraUtilsUTest SetUpTestCase");
27 }
28
TearDownTestCase(void)29 void CameraUtilsUTest::TearDownTestCase(void)
30 {
31 CAMERA_LOGD("Camera::CameraUtilsUTest TearDownTestCase");
32 }
33
SetUp(void)34 void CameraUtilsUTest::SetUp(void)
35 {
36 CAMERA_LOGD("Camera::CameraUtilsUTest SetUp");
37 }
38
TearDown(void)39 void CameraUtilsUTest::TearDown(void)
40 {
41 CAMERA_LOGD("Camera::CameraUtilsUTest TearDown..");
42 }
43
44 /**
45 * @tc.name: CameraUtilsUTest
46 * @tc.desc: Test camera host selfkill, the system param is enabled, the camera devices count is 0 and the timing
47 * conditions are also met.
48 * @tc.level: Level0
49 * @tc.size: MediumTest
50 * @tc.type: Function
51 */
52 HWTEST_F(CameraUtilsUTest, TestSelfKiller001, TestSize.Level0)
53 {
54 CAMERA_LOGI("Camera::CameraUtilsUTest TestSelfKiller001");
55 OHOS::system::SetParameter(DEBUG_SELF_KILL_PARAM_NAME, "true");
56 std::unique_ptr<CameraHostSelfkiller> usbCameraSelfKiller = std::make_unique<CameraHostSelfkiller>(1, 5);
57 uint8_t cameraCount = 0;
58 bool selfkillNotifyFlag = false;
59 EXPECT_EQ(selfkillNotifyFlag, false);
__anona1f776310102() 60 usbCameraSelfKiller->Init([&cameraCount]() { return cameraCount == 0; },
__anona1f776310202() 61 [&selfkillNotifyFlag]() {
62 CAMERA_LOGI("TestSelfKiller001 selfkill notify.");
63 selfkillNotifyFlag = true;
64 },
65 "debug.camera.setting.selfkill.enable",
66 "invlid_service_name");
67 cameraCount = 1;
68 sleep(3);
69 EXPECT_EQ(selfkillNotifyFlag, false);
70 sleep(3);
71 EXPECT_EQ(selfkillNotifyFlag, false);
72 sleep(3);
73 EXPECT_EQ(selfkillNotifyFlag, false);
74 cameraCount = 0;
75 sleep(3);
76 EXPECT_EQ(selfkillNotifyFlag, false);
77 sleep(4);
78 EXPECT_EQ(selfkillNotifyFlag, true);
79 usbCameraSelfKiller->DeInit();
80 }
81
82 /**
83 * @tc.name: CameraUtilsUTest
84 * @tc.desc: Test camera host selfkill, the param is disabled, so it will not self kill
85 * @tc.level: Level0
86 * @tc.size: MediumTest
87 * @tc.type: Function
88 */
89 HWTEST_F(CameraUtilsUTest, TestSelfKiller002, TestSize.Level0)
90 {
91 CAMERA_LOGI("Camera::CameraUtilsUTest TestSelfKiller002");
92 OHOS::system::SetParameter(DEBUG_SELF_KILL_PARAM_NAME, "false");
93 std::unique_ptr<CameraHostSelfkiller> usbCameraSelfKiller = std::make_unique<CameraHostSelfkiller>(1, 3);
94 uint8_t cameraCount = 0;
95 bool selfkillNotifyFlag = false;
96 EXPECT_EQ(selfkillNotifyFlag, false);
__anona1f776310302() 97 usbCameraSelfKiller->Init([&cameraCount]() { return cameraCount == 0; },
__anona1f776310402() 98 [&selfkillNotifyFlag]() {
99 CAMERA_LOGI("TestSelfKiller002 selfkill notify.");
100 selfkillNotifyFlag = true;
101 },
102 DEBUG_SELF_KILL_PARAM_NAME,
103 "invlid_service_name");
104 sleep(10);
105 EXPECT_EQ(selfkillNotifyFlag, false);
106 usbCameraSelfKiller->DeInit();
107 }
108
109 /**
110 * @tc.name: CameraUtilsUTest
111 * @tc.desc: Test camera host selfkill, the CameraHostSelfkiller Deinited, so it will not self kill
112 * @tc.level: Level0
113 * @tc.size: MediumTest
114 * @tc.type: Function
115 */
116 HWTEST_F(CameraUtilsUTest, TestSelfKiller003, TestSize.Level0)
117 {
118 CAMERA_LOGI("Camera::CameraUtilsUTest TestSelfKiller003");
119 OHOS::system::SetParameter(DEBUG_SELF_KILL_PARAM_NAME, "true");
120 std::unique_ptr<CameraHostSelfkiller> usbCameraSelfKiller = std::make_unique<CameraHostSelfkiller>(1, 5);
121 uint8_t cameraCount = 0;
122 bool selfkillNotifyFlag = false;
123 EXPECT_EQ(selfkillNotifyFlag, false);
__anona1f776310502() 124 usbCameraSelfKiller->Init([&cameraCount]() { return cameraCount == 0; },
__anona1f776310602() 125 [&selfkillNotifyFlag]() {
126 CAMERA_LOGI("TestSelfKiller003 selfkill notify.");
127 selfkillNotifyFlag = true;
128 },
129 DEBUG_SELF_KILL_PARAM_NAME,
130 "invlid_service_name");
131 sleep(3);
132 usbCameraSelfKiller->DeInit();
133 usbCameraSelfKiller = nullptr;
134 sleep(10);
135 EXPECT_EQ(selfkillNotifyFlag, false);
136 }