• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     for (int i = 0; i < 10242; i++) {
128         args.push_back(u"");
129     }
130     ret = testSrcService_->Dump(fd, args);
131     EXPECT_EQ(DCAMERA_BAD_VALUE, ret);
132 }
133 
134 /**
135  * @tc.name: dcamera_source_service_test_004
136  * @tc.desc: Verify the OnStart function.
137  * @tc.type: FUNC
138  * @tc.require: issue
139  */
140 HWTEST_F(DistributedCameraSourceServiceTest, dcamera_source_service_test_004, TestSize.Level1)
141 {
142     DHLOGI("DistributedCameraSourceServiceTest::dcamera_source_service_test_004");
143     EXPECT_EQ(false, testSrcService_ == nullptr);
144 
145     int32_t ret = 0;
146     testSrcService_->state_ = DCameraServiceState::DCAMERA_SRV_STATE_RUNNING;
147     testSrcService_->OnStart();
148     EXPECT_EQ(DCAMERA_OK, ret);
149 }
150 
151 /**
152  * @tc.name: dcamera_source_service_test_005
153  * @tc.desc: Verify the Init function.
154  * @tc.type: FUNC
155  * @tc.require: issue
156  */
157 HWTEST_F(DistributedCameraSourceServiceTest, dcamera_source_service_test_005, TestSize.Level1)
158 {
159     DHLOGI("DistributedCameraSourceServiceTest::dcamera_source_service_test_005");
160     EXPECT_EQ(false, testSrcService_ == nullptr);
161 
162     int32_t ret = 0;
163     testSrcService_->registerToService_ = true;
164     testSrcService_->Init();
165     testSrcService_->OnStop();
166     EXPECT_EQ(DCAMERA_OK, ret);
167 }
168 
169 /**
170  * @tc.name: dcamera_source_service_test_006
171  * @tc.desc: Verify the InitSource function.
172  * @tc.type: FUNC
173  * @tc.require: issue
174  */
175 HWTEST_F(DistributedCameraSourceServiceTest, dcamera_source_service_test_006, TestSize.Level1)
176 {
177     DHLOGI("DistributedCameraSourceServiceTest::dcamera_source_service_test_006");
178     EXPECT_EQ(false, testSrcService_ == nullptr);
179 
180     std::string params = "test006";
181     sptr<ISystemAbilityManager> samgr =
182         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
183     sptr<IRemoteObject> remoteObject = samgr->GetSystemAbility(DISTRIBUTED_HARDWARE_CAMERA_SOURCE_SA_ID);
184     sptr<DCameraSourceCallbackProxy> callbackProxy(new DCameraSourceCallbackProxy(remoteObject));
185     testSrcService_->listener_ = std::make_shared<DCameraServiceStateListener>();
186     int32_t ret = testSrcService_->InitSource(params, callbackProxy);
187     EXPECT_EQ(DCAMERA_OK, ret);
188     usleep(TEST_SOURCE_SERVICE);
189 }
190 
191 /**
192  * @tc.name: dcamera_source_service_test_007
193  * @tc.desc: Verify the ReleaseSource function.
194  * @tc.type: FUNC
195  * @tc.require: issue
196  */
197 HWTEST_F(DistributedCameraSourceServiceTest, dcamera_source_service_test_007, TestSize.Level1)
198 {
199     DHLOGI("DistributedCameraSourceServiceTest::dcamera_source_service_test_007");
200     EXPECT_EQ(false, testSrcService_ == nullptr);
201 
202     int32_t ret = testSrcService_->ReleaseSource();
203     EXPECT_EQ(DCAMERA_BAD_VALUE, ret);
204     usleep(TEST_SOURCE_SERVICE);
205 }
206 
207 /**
208  * @tc.name: dcamera_source_service_test_008
209  * @tc.desc: Verify the LoadDCameraHDF function.
210  * @tc.type: FUNC
211  * @tc.require: issue
212  */
213 HWTEST_F(DistributedCameraSourceServiceTest, dcamera_source_service_test_008, TestSize.Level1)
214 {
215     DHLOGI("DistributedCameraSourceServiceTest::dcamera_source_service_test_008");
216     EXPECT_EQ(false, testSrcService_ == nullptr);
217 
218     int32_t ret = testSrcService_->LoadDCameraHDF();
219     EXPECT_EQ(DCAMERA_OK, ret);
220     usleep(TEST_SOURCE_SERVICE);
221 }
222 
223 /**
224  * @tc.name: dcamera_source_service_test_009
225  * @tc.desc: Verify the UnLoadCameraHDF function.
226  * @tc.type: FUNC
227  * @tc.require: issue
228  */
229 HWTEST_F(DistributedCameraSourceServiceTest, dcamera_source_service_test_009, TestSize.Level1)
230 {
231     DHLOGI("DistributedCameraSourceServiceTest::dcamera_source_service_test_009");
232     EXPECT_EQ(false, testSrcService_ == nullptr);
233 
234     int32_t ret = testSrcService_->UnLoadCameraHDF();
235     EXPECT_EQ(DCAMERA_OK, ret);
236 
237     ret = testSrcService_->LoadDCameraHDF();
238     EXPECT_EQ(DCAMERA_OK, ret);
239     ret = testSrcService_->UnLoadCameraHDF();
240     EXPECT_EQ(DCAMERA_OK, ret);
241     usleep(TEST_SOURCE_SERVICE);
242 }
243 } // namespace DistributedHardware
244 } // namespace OHOS