1 /*
2 * Copyright (c) 2022 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 <gtest/gtest.h>
17
18 #define private public
19 #define protected public
20 #include "distributed_camera_source_service.h"
21 #undef protected
22 #undef private
23
24 #include "dcamera_source_callback_proxy.h"
25 #include "distributed_camera_errno.h"
26 #include "distributed_hardware_log.h"
27 #include "if_system_ability_manager.h"
28 #include "iservice_registry.h"
29 #include "xcollie/watchdog.h"
30
31 using namespace testing::ext;
32
33 namespace OHOS {
34 namespace DistributedHardware {
35 class DistributedCameraSourceServiceTest : public testing::Test {
36 public:
37 static void SetUpTestCase(void);
38 static void TearDownTestCase(void);
39 void SetUp();
40 void TearDown();
41
42 std::shared_ptr<DistributedCameraSourceService> testSrcService_;
43 };
44
45 namespace {
46 const std::string TEST_DEVICE_ID = "bb536a637105409e904d4da83790a4a7";
47 const std::string TEST_CAMERA_DH_ID_0 = "camera_0";
48 const std::string TEST_REQID = "bb536a637105409e904d4da83790a4a7";
49 const int32_t TEST_SOURCE_SERVICE = 200000;
50 }
51
SetUpTestCase(void)52 void DistributedCameraSourceServiceTest::SetUpTestCase(void)
53 {
54 DHLOGI("DistributedCameraSourceServiceTest::SetUpTestCase");
55 }
56
TearDownTestCase(void)57 void DistributedCameraSourceServiceTest::TearDownTestCase(void)
58 {
59 DHLOGI("DistributedCameraSourceServiceTest::TearDownTestCase");
60 }
61
SetUp(void)62 void DistributedCameraSourceServiceTest::SetUp(void)
63 {
64 DHLOGI("DistributedCameraSourceServiceTest::SetUp");
65 testSrcService_ = std::make_shared<DistributedCameraSourceService>(DISTRIBUTED_HARDWARE_CAMERA_SOURCE_SA_ID, true);
66 }
67
TearDown(void)68 void DistributedCameraSourceServiceTest::TearDown(void)
69 {
70 DHLOGI("DistributedCameraSourceServiceTest::TearDown");
71 testSrcService_ = nullptr;
72 }
73
74 /**
75 * @tc.name: dcamera_source_service_test_001
76 * @tc.desc: Verify the RegisterDistributedHardware UnregisterDistributedHardware function.
77 * @tc.type: FUNC
78 * @tc.require: issue
79 */
80 HWTEST_F(DistributedCameraSourceServiceTest, dcamera_source_service_test_001, TestSize.Level1)
81 {
82 DHLOGI("DistributedCameraSourceServiceTest::dcamera_source_service_test_001");
83 EXPECT_EQ(false, testSrcService_ == nullptr);
84
85 EnableParam param;
86 int32_t ret = testSrcService_->RegisterDistributedHardware(TEST_DEVICE_ID, TEST_CAMERA_DH_ID_0, TEST_REQID, param);
87 EXPECT_EQ(DCAMERA_OK, ret);
88
89 ret = testSrcService_->UnregisterDistributedHardware(TEST_DEVICE_ID, TEST_CAMERA_DH_ID_0, TEST_REQID);
90 EXPECT_EQ(DCAMERA_OK, ret);
91 }
92
93 /**
94 * @tc.name: dcamera_source_service_test_002
95 * @tc.desc: Verify the DCameraNotify function.
96 * @tc.type: FUNC
97 * @tc.require: issue
98 */
99 HWTEST_F(DistributedCameraSourceServiceTest, dcamera_source_service_test_002, TestSize.Level1)
100 {
101 DHLOGI("DistributedCameraSourceServiceTest::dcamera_source_service_test_002");
102 EXPECT_EQ(false, testSrcService_ == nullptr);
103
104 std::string events;
105 int32_t ret = testSrcService_->DCameraNotify(TEST_DEVICE_ID, TEST_CAMERA_DH_ID_0, events);
106 EXPECT_EQ(DCAMERA_BAD_VALUE, ret);
107 }
108
109 /**
110 * @tc.name: dcamera_source_service_test_003
111 * @tc.desc: Verify the Dump function.
112 * @tc.type: FUNC
113 * @tc.require: issue
114 */
115 HWTEST_F(DistributedCameraSourceServiceTest, dcamera_source_service_test_003, TestSize.Level1)
116 {
117 DHLOGI("DistributedCameraSourceServiceTest::dcamera_source_service_test_003");
118 EXPECT_EQ(false, testSrcService_ == nullptr);
119
120 int32_t fd = 1;
121 std::vector<std::u16string> args;
122 std::u16string str(u"");
123 args.push_back(str);
124 int ret = testSrcService_->Dump(fd, args);
125 EXPECT_EQ(DCAMERA_OK, ret);
126 }
127
128 /**
129 * @tc.name: dcamera_source_service_test_004
130 * @tc.desc: Verify the OnStart function.
131 * @tc.type: FUNC
132 * @tc.require: issue
133 */
134 HWTEST_F(DistributedCameraSourceServiceTest, dcamera_source_service_test_004, TestSize.Level1)
135 {
136 DHLOGI("DistributedCameraSourceServiceTest::dcamera_source_service_test_004");
137 EXPECT_EQ(false, testSrcService_ == nullptr);
138
139 int32_t ret = 0;
140 testSrcService_->state_ = DCameraServiceState::DCAMERA_SRV_STATE_RUNNING;
141 testSrcService_->OnStart();
142 EXPECT_EQ(DCAMERA_OK, ret);
143 }
144
145 /**
146 * @tc.name: dcamera_source_service_test_005
147 * @tc.desc: Verify the Init function.
148 * @tc.type: FUNC
149 * @tc.require: issue
150 */
151 HWTEST_F(DistributedCameraSourceServiceTest, dcamera_source_service_test_005, TestSize.Level1)
152 {
153 DHLOGI("DistributedCameraSourceServiceTest::dcamera_source_service_test_005");
154 EXPECT_EQ(false, testSrcService_ == nullptr);
155
156 int32_t ret = 0;
157 testSrcService_->registerToService_ = true;
158 testSrcService_->Init();
159 testSrcService_->OnStop();
160 EXPECT_EQ(DCAMERA_OK, ret);
161 }
162
163 /**
164 * @tc.name: dcamera_source_service_test_006
165 * @tc.desc: Verify the InitSource function.
166 * @tc.type: FUNC
167 * @tc.require: issue
168 */
169 HWTEST_F(DistributedCameraSourceServiceTest, dcamera_source_service_test_006, TestSize.Level1)
170 {
171 DHLOGI("DistributedCameraSourceServiceTest::dcamera_source_service_test_006");
172 EXPECT_EQ(false, testSrcService_ == nullptr);
173
174 std::string params = "test006";
175 sptr<ISystemAbilityManager> samgr =
176 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
177 sptr<IRemoteObject> remoteObject = samgr->GetSystemAbility(DISTRIBUTED_HARDWARE_CAMERA_SOURCE_SA_ID);
178 sptr<DCameraSourceCallbackProxy> callbackProxy(new DCameraSourceCallbackProxy(remoteObject));
179 testSrcService_->listener_ = std::make_shared<DCameraServiceStateListener>();
180 int32_t ret = testSrcService_->InitSource(params, callbackProxy);
181 EXPECT_EQ(DCAMERA_OK, ret);
182 usleep(TEST_SOURCE_SERVICE);
183 }
184
185 /**
186 * @tc.name: dcamera_source_service_test_007
187 * @tc.desc: Verify the ReleaseSource function.
188 * @tc.type: FUNC
189 * @tc.require: issue
190 */
191 HWTEST_F(DistributedCameraSourceServiceTest, dcamera_source_service_test_007, TestSize.Level1)
192 {
193 DHLOGI("DistributedCameraSourceServiceTest::dcamera_source_service_test_007");
194 EXPECT_EQ(false, testSrcService_ == nullptr);
195
196 int32_t ret = testSrcService_->ReleaseSource();
197 EXPECT_EQ(DCAMERA_BAD_VALUE, ret);
198 usleep(TEST_SOURCE_SERVICE);
199 }
200
201 /**
202 * @tc.name: dcamera_source_service_test_008
203 * @tc.desc: Verify the LoadDCameraHDF function.
204 * @tc.type: FUNC
205 * @tc.require: issue
206 */
207 HWTEST_F(DistributedCameraSourceServiceTest, dcamera_source_service_test_008, TestSize.Level1)
208 {
209 DHLOGI("DistributedCameraSourceServiceTest::dcamera_source_service_test_008");
210 EXPECT_EQ(false, testSrcService_ == nullptr);
211
212 int32_t ret = testSrcService_->LoadDCameraHDF();
213 EXPECT_EQ(DCAMERA_OK, ret);
214 usleep(TEST_SOURCE_SERVICE);
215 }
216
217 /**
218 * @tc.name: dcamera_source_service_test_009
219 * @tc.desc: Verify the UnLoadCameraHDF function.
220 * @tc.type: FUNC
221 * @tc.require: issue
222 */
223 HWTEST_F(DistributedCameraSourceServiceTest, dcamera_source_service_test_009, TestSize.Level1)
224 {
225 DHLOGI("DistributedCameraSourceServiceTest::dcamera_source_service_test_009");
226 EXPECT_EQ(false, testSrcService_ == nullptr);
227
228 int32_t ret = testSrcService_->LoadDCameraHDF();
229 EXPECT_EQ(DCAMERA_OK, ret);
230 ret = testSrcService_->UnLoadCameraHDF();
231 EXPECT_EQ(DCAMERA_OK, ret);
232 usleep(TEST_SOURCE_SERVICE);
233 }
234 } // namespace DistributedHardware
235 } // namespace OHOS