• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 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 "component_manager_test.h"
17 
18 #include <chrono>
19 #include <thread>
20 #include <vector>
21 
22 #include <gmock/gmock.h>
23 
24 #include "component_disable.h"
25 #include "component_enable.h"
26 #define private public
27 #include "component_manager.h"
28 #undef private
29 #include "constants.h"
30 #include "distributed_hardware_errno.h"
31 #include "distributed_hardware_log.h"
32 #include "mock_idistributed_hardware_sink.h"
33 #include "mock_idistributed_hardware_source.h"
34 
35 using namespace testing::ext;
36 
37 namespace OHOS {
38 namespace DistributedHardware {
39 #undef DH_LOG_TAG
40 #define DH_LOG_TAG "ComponentManagerTest"
41 
42 constexpr int32_t EXECUTE_TIME_TEST = 1000;
43 const std::string DEV_ID_TEST = "123456";
44 const std::string DH_ID_TEST = "Camera_0";
45 
SetUpTestCase(void)46 void ComponentManagerTest::SetUpTestCase(void) {}
47 
TearDownTestCase(void)48 void ComponentManagerTest::TearDownTestCase(void) {}
49 
SetUp()50 void ComponentManagerTest::SetUp()
51 {
52     ComponentManager::GetInstance().compSource_.clear();
53     ComponentManager::GetInstance().compSink_.clear();
54 }
55 
TearDown()56 void ComponentManagerTest::TearDown() {}
57 
Enable(int32_t timeout,int32_t status)58 int32_t ComponentManagerTest::Enable(int32_t timeout, int32_t status)
59 {
60     MockIDistributedHardwareSource stub;
61     auto compEnable = std::make_shared<ComponentEnable>();
62     EnableParam parameters;
63     std::future<int32_t> future;
64     auto handler = [&future, timeout, status, compEnable](std::string uuid, std::string dhId,
65         const EnableParam &parameters, std::shared_ptr<RegisterCallback> callback) {
66         future = std::async(std::launch::async, [timeout, compEnable, uuid, dhId, status]() {
67             std::this_thread::sleep_for(std::chrono::milliseconds(timeout));
68             return compEnable->OnRegisterResult(uuid, dhId, status, "");
69         });
70         return DH_FWK_SUCCESS;
71     };
72 
73     EXPECT_CALL(stub, RegisterDistributedHardware(DEV_ID_TEST, DH_ID_TEST, testing::_, testing::_))
74         .Times(1)
75         .WillOnce(testing::Invoke(handler));
76 
77     auto start = std::chrono::system_clock::now();
78     auto ret = compEnable->Enable(DEV_ID_TEST, DH_ID_TEST, parameters, &stub);
79     auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - start).count();
80     DHLOGI("enable callback use time: %d (ms)", diff);
81     return ret;
82 }
83 
Disable(int32_t timeout,int32_t status)84 int32_t ComponentManagerTest::Disable(int32_t timeout, int32_t status)
85 {
86     MockIDistributedHardwareSource stub;
87     auto compDisable = std::make_shared<ComponentDisable>();
88 
89     std::future<int32_t> future;
90     auto handler = [&future, timeout, status, compDisable](std::string uuid, std::string dhId,
91         std::shared_ptr<UnregisterCallback> callback) {
92         future = std::async(std::launch::async, [timeout, compDisable, uuid, dhId, status]() {
93             std::this_thread::sleep_for(std::chrono::milliseconds(timeout));
94             return compDisable->OnUnregisterResult(uuid, dhId, status, "");
95         });
96         return DH_FWK_SUCCESS;
97     };
98 
99     EXPECT_CALL(stub, UnregisterDistributedHardware(DEV_ID_TEST, DH_ID_TEST, testing::_))
100         .Times(1)
101         .WillOnce(testing::Invoke(handler));
102 
103     auto start = std::chrono::system_clock::now();
104 
105     auto ret = compDisable->Disable(DEV_ID_TEST, DH_ID_TEST, &stub);
106     auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - start).count();
107     DHLOGI("disable callback use time: %d (ms)", diff);
108     return ret;
109 }
110 
111 /**
112  * @tc.name: init_test_001
113  * @tc.desc: Verify the Init function
114  * @tc.type: FUNC
115  * @tc.require: AR000GHSK5
116  */
117 HWTEST_F(ComponentManagerTest, init_test_001, TestSize.Level0)
118 {
119     auto ret = ComponentManager::GetInstance().Init();
120     EXPECT_EQ(ERR_DH_FWK_COMPONENT_INIT_SOURCE_FAILED, ret);
121 }
122 
123 /**
124  * @tc.name: init_test_002
125  * @tc.desc: Verify the Init function
126  * @tc.type: FUNC
127  * @tc.require: AR000GHSK5
128  */
129 HWTEST_F(ComponentManagerTest, init_test_002, TestSize.Level0)
130 {
131     MockIDistributedHardwareSource cameraSource;
132     ComponentManager::GetInstance().compSource_.insert(std::make_pair(DHType::CAMERA, &cameraSource));
133 
134     auto ret = ComponentManager::GetInstance().Init();
135     EXPECT_EQ(ERR_DH_FWK_COMPONENT_INIT_SINK_FAILED, ret);
136 }
137 
138 /**
139  * @tc.name: init_test_003
140  * @tc.desc: Verify the Init function
141  * @tc.type: FUNC
142  * @tc.require: AR000GHSK5
143  */
144 HWTEST_F(ComponentManagerTest, init_test_003, TestSize.Level0)
145 {
__anon83cbaae10502(std::string param) 146     auto handler = [](std::string param) {
147         std::this_thread::sleep_for(std::chrono::milliseconds(EXECUTE_TIME_TEST));
148         return DH_FWK_SUCCESS;
149     };
150 
151     MockIDistributedHardwareSource cameraSource;
152     ComponentManager::GetInstance().compSource_.insert(std::make_pair(DHType::CAMERA, &cameraSource));
153     EXPECT_CALL(cameraSource, InitSource(testing::_)).Times(1).WillOnce(testing::Invoke(handler));
154 
155     MockIDistributedHardwareSource speakerSource;
156     ComponentManager::GetInstance().compSource_.insert(std::make_pair(DHType::SPEAKER, &speakerSource));
157     EXPECT_CALL(speakerSource, InitSource(testing::_)).Times(1).WillOnce(testing::Invoke(handler));
158 
159     MockIDistributedHardwareSink micSink;
160     ComponentManager::GetInstance().compSink_.insert(std::make_pair(DHType::MIC, &micSink));
161     EXPECT_CALL(micSink, InitSink(testing::_)).Times(1).WillOnce(testing::Invoke(handler));
162 
163     auto start = std::chrono::system_clock::now();
164 
165     auto ret = ComponentManager::GetInstance().Init();
166     EXPECT_EQ(DH_FWK_SUCCESS, ret);
167 
168     auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - start).count();
169     DHLOGI("Init use time: %d (ms)", diff);
170     ASSERT_TRUE(diff <= EXECUTE_TIME_TEST * 1.1);
171 }
172 
173 /**
174  * @tc.name: unInit_test_001
175  * @tc.desc: Verify the UnInit function
176  * @tc.type: FUNC
177  * @tc.require: AR000GHSK5
178  */
179 HWTEST_F(ComponentManagerTest, unInit_test_001, TestSize.Level0)
180 {
__anon83cbaae10602() 181     auto handler = []() {
182         std::this_thread::sleep_for(std::chrono::milliseconds(EXECUTE_TIME_TEST));
183         return DH_FWK_SUCCESS;
184     };
185 
186     MockIDistributedHardwareSource cameraSource;
187     ComponentManager::GetInstance().compSource_.insert(std::make_pair(DHType::CAMERA, &cameraSource));
188     EXPECT_CALL(cameraSource, ReleaseSource()).Times(1).WillOnce(testing::Invoke(handler));
189 
190     MockIDistributedHardwareSink speakerSink;
191     ComponentManager::GetInstance().compSink_.insert(std::make_pair(DHType::SPEAKER, &speakerSink));
192     EXPECT_CALL(speakerSink, ReleaseSink()).Times(1).WillOnce(testing::Invoke(handler));
193 
194     MockIDistributedHardwareSink micSink;
195     ComponentManager::GetInstance().compSink_.insert(std::make_pair(DHType::MIC, &micSink));
196     EXPECT_CALL(micSink, ReleaseSink()).Times(1).WillOnce(testing::Invoke(handler));
197 
198     auto start = std::chrono::system_clock::now();
199 
200     auto ret = ComponentManager::GetInstance().UnInit();
201     EXPECT_EQ(DH_FWK_SUCCESS, ret);
202 
203     auto diff = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - start).count();
204     DHLOGI("UnInit use time : %d (ms)", diff);
205     ASSERT_TRUE(diff <= EXECUTE_TIME_TEST * 1.1);
206 }
207 
208 /**
209  * @tc.name: enable_test_001
210  * @tc.desc: Verify the Enable success
211  * @tc.type: FUNC
212  * @tc.require: AR000GHSK7
213  */
214 HWTEST_F(ComponentManagerTest, enable_test_001, TestSize.Level0)
215 {
216     auto result = Enable(ENABLE_TIMEOUT_MS / 2, DH_FWK_SUCCESS);
217     EXPECT_EQ(DH_FWK_SUCCESS, result);
218 }
219 
220 /**
221  * @tc.name: enable_test_002
222  * @tc.desc: Verify the Enable failed for register hardware failed
223  * @tc.type: FUNC
224  * @tc.require: AR000GHSK7
225  */
226 
227 HWTEST_F(ComponentManagerTest, enable_test_002, TestSize.Level0)
228 {
229     auto result = Enable(ENABLE_TIMEOUT_MS / 2, ERR_DH_FWK_COMPONENT_ENABLE_FAILED);
230     EXPECT_EQ(ERR_DH_FWK_COMPONENT_ENABLE_FAILED, result);
231 }
232 
233 /**
234  * @tc.name: enable_test_003
235  * @tc.desc: Verify the Enable timeout
236  * @tc.type: FUNC
237  * @tc.require: AR000GHSK7
238  */
239 HWTEST_F(ComponentManagerTest, enable_test_003, TestSize.Level0)
240 {
241     auto result = Enable(ENABLE_TIMEOUT_MS * 2, ERR_DH_FWK_COMPONENT_ENABLE_FAILED);
242     EXPECT_EQ(ERR_DH_FWK_COMPONENT_ENABLE_TIMEOUT, result);
243 }
244 
245 /**
246  * @tc.name: enable_test_004
247  * @tc.desc: Verify the Enable for Multi-thread
248  * @tc.type: FUNC
249  * @tc.require: AR000GHSK7
250  */
251 HWTEST_F(ComponentManagerTest, enable_test_004, TestSize.Level0)
252 {
__anon83cbaae10702(int32_t time, int32_t status, int32_t expect) 253     auto handler = [this](int32_t time, int32_t status, int32_t expect) {
254         auto ret = this->Enable(time, status);
255         EXPECT_EQ(expect, ret);
256     };
257 
258     std::thread thread1(handler, ENABLE_TIMEOUT_MS / 2, DH_FWK_SUCCESS, DH_FWK_SUCCESS);
259     std::thread thread2(handler, ENABLE_TIMEOUT_MS / 2, ERR_DH_FWK_COMPONENT_ENABLE_FAILED,
260         ERR_DH_FWK_COMPONENT_ENABLE_FAILED);
261     std::thread thread3(handler, ENABLE_TIMEOUT_MS * 3, DH_FWK_SUCCESS, ERR_DH_FWK_COMPONENT_ENABLE_TIMEOUT);
262 
263     std::thread thread6(handler, ENABLE_TIMEOUT_MS / 10, DH_FWK_SUCCESS, DH_FWK_SUCCESS);
264     std::thread thread4(handler, ENABLE_TIMEOUT_MS / 10, ERR_DH_FWK_COMPONENT_ENABLE_FAILED,
265         ERR_DH_FWK_COMPONENT_ENABLE_FAILED);
266     std::thread thread5(handler, ENABLE_TIMEOUT_MS * 2, DH_FWK_SUCCESS, ERR_DH_FWK_COMPONENT_ENABLE_TIMEOUT);
267 
268     thread1.join();
269     thread2.join();
270     thread3.join();
271     thread4.join();
272     thread5.join();
273     thread6.join();
274 }
275 
276 /**
277  * @tc.name: disable_test_001
278  * @tc.desc: Verify the Disable success
279  * @tc.type: FUNC
280  * @tc.require: AR000GHSK9
281  */
282 HWTEST_F(ComponentManagerTest, disable_test_001, TestSize.Level0)
283 {
284     auto result = Disable(DISABLE_TIMEOUT_MS / 2, DH_FWK_SUCCESS);
285     EXPECT_EQ(DH_FWK_SUCCESS, result);
286 }
287 
288 /**
289  * @tc.name: disable_test_002
290  * @tc.desc: Verify the Disable failed
291  * @tc.type: FUNC
292  * @tc.require: AR000GHSK9
293  */
294 HWTEST_F(ComponentManagerTest, disable_test_002, TestSize.Level0)
295 {
296     auto result = Disable(DISABLE_TIMEOUT_MS / 2, ERR_DH_FWK_COMPONENT_DISABLE_FAILED);
297     EXPECT_EQ(ERR_DH_FWK_COMPONENT_DISABLE_FAILED, result);
298 }
299 
300 /**
301  * @tc.name: disable_test_003
302  * @tc.desc: Verify the Disable timeout
303  * @tc.type: FUNC
304  * @tc.require: AR000GHSK9
305  */
306 HWTEST_F(ComponentManagerTest, disable_test_003, TestSize.Level0)
307 {
308     auto result = Disable(DISABLE_TIMEOUT_MS * 2, ERR_DH_FWK_COMPONENT_DISABLE_TIMEOUT);
309     EXPECT_EQ(ERR_DH_FWK_COMPONENT_DISABLE_TIMEOUT, result);
310 }
311 
312 /**
313  * @tc.name: disable_test_004
314  * @tc.desc: Verify the Disable for Multi-thread
315  * @tc.type: FUNC
316  * @tc.require: AR000GHSK9
317  */
318 HWTEST_F(ComponentManagerTest, disable_test_004, TestSize.Level0)
319 {
__anon83cbaae10802(int32_t timeout, int32_t status, int32_t expect) 320     auto handler = [this](int32_t timeout, int32_t status, int32_t expect) {
321         auto result = this->Disable(timeout, status);
322         EXPECT_EQ(expect, result);
323     };
324 
325     std::thread thread1(handler, DISABLE_TIMEOUT_MS / 2, DH_FWK_SUCCESS, DH_FWK_SUCCESS);
326     std::thread thread2(handler, DISABLE_TIMEOUT_MS / 2, ERR_DH_FWK_COMPONENT_DISABLE_FAILED,
327         ERR_DH_FWK_COMPONENT_DISABLE_FAILED);
328     std::thread thread3(handler, DISABLE_TIMEOUT_MS * 3, DH_FWK_SUCCESS, ERR_DH_FWK_COMPONENT_DISABLE_TIMEOUT);
329 
330     std::thread thread4(handler, DISABLE_TIMEOUT_MS / 10, ERR_DH_FWK_COMPONENT_DISABLE_FAILED,
331         ERR_DH_FWK_COMPONENT_DISABLE_FAILED);
332     std::thread thread6(handler, DISABLE_TIMEOUT_MS / 10, DH_FWK_SUCCESS, DH_FWK_SUCCESS);
333     std::thread thread5(handler, DISABLE_TIMEOUT_MS * 2, DH_FWK_SUCCESS, ERR_DH_FWK_COMPONENT_DISABLE_TIMEOUT);
334 
335     thread1.join();
336     thread2.join();
337     thread3.join();
338     thread4.join();
339     thread5.join();
340     thread6.join();
341 }
342 }
343 }
344