• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <memory>
17 
18 #include "dscreen_source_service_test.h"
19 #include "1.0/include/dscreen_manager.h"
20 #include "2.0/include/dscreen_manager.h"
21 #include "accesstoken_kit.h"
22 #include "dscreen_constants.h"
23 #include "dscreen_source_callback.h"
24 #include "dscreen_source_service.h"
25 #include "idscreen_source.h"
26 #include "system_ability.h"
27 
28 using namespace testing;
29 using namespace testing::ext;
30 
31 static int32_t g_mockEnableDistributedScreenReturnInt32Value = -1;
32 static int32_t g_mockUnregisterDistributedHardwareV1ReturnInt32Value = -1;
33 static int32_t g_mockUnregisterDistributedHardwareV2ReturnInt32Value = -1;
34 static int32_t g_mockUnInitV1ReturnInt32Value = -1;
35 static int32_t g_mockReleaseV2ReturnInt32Value = -1;
36 static int32_t g_mockInitV1ReturnInt32Value = -1;
37 static int32_t g_mockInitializeV2ReturnInt32Value = -1;
38 
39 namespace OHOS {
40 namespace DistributedHardware {
EnableDistributedScreen(const std::string & devId,const std::string & dhId,const EnableParam & param,const std::string & reqId)41 int32_t V1_0::DScreenManager::EnableDistributedScreen(const std::string &devId, const std::string &dhId,
42     const EnableParam &param, const std::string &reqId)
43 {
44     return g_mockEnableDistributedScreenReturnInt32Value;
45 }
46 
EnableDistributedScreen(const std::string & devId,const std::string & dhId,const EnableParam & param,const std::string & reqId)47 int32_t V2_0::DScreenManager::EnableDistributedScreen(const std::string &devId, const std::string &dhId,
48     const EnableParam &param, const std::string &reqId)
49 {
50     return g_mockEnableDistributedScreenReturnInt32Value;
51 }
52 
DisableDistributedScreen(const std::string & devId,const std::string & dhId,const std::string & reqId)53 int32_t V1_0::DScreenManager::DisableDistributedScreen(const std::string &devId,
54     const std::string &dhId, const std::string &reqId)
55 {
56     return g_mockUnregisterDistributedHardwareV1ReturnInt32Value;
57 }
58 
DisableDistributedScreen(const std::string & devId,const std::string & dhId,const std::string & reqId)59 int32_t V2_0::DScreenManager::DisableDistributedScreen(const std::string &devId,
60     const std::string &dhId, const std::string &reqId)
61 {
62     return g_mockUnregisterDistributedHardwareV2ReturnInt32Value;
63 }
64 
Init()65 int32_t V1_0::DScreenManager::Init()
66 {
67     return g_mockInitV1ReturnInt32Value;
68 }
69 
Initialize()70 int32_t V2_0::DScreenManager::Initialize()
71 {
72     return g_mockInitializeV2ReturnInt32Value;
73 }
74 
UnInit()75 int32_t V1_0::DScreenManager::UnInit()
76 {
77     return g_mockUnInitV1ReturnInt32Value;
78 }
79 
Release()80 int32_t V2_0::DScreenManager::Release()
81 {
82     return g_mockReleaseV2ReturnInt32Value;
83 }
84 
RegisterDScreenCallback(const sptr<IDScreenSourceCallback> & callback)85 void V1_0::DScreenManager::RegisterDScreenCallback(const sptr<IDScreenSourceCallback> &callback) {}
86 
RegisterDScreenCallback(const sptr<IDScreenSourceCallback> & callback)87 void V2_0::DScreenManager::RegisterDScreenCallback(const sptr<IDScreenSourceCallback> &callback) {}
88 
SetUpTestCase(void)89 void DScreenSourceServiceTest::SetUpTestCase(void) {}
90 
TearDownTestCase(void)91 void DScreenSourceServiceTest::TearDownTestCase(void) {}
92 
SetUp(void)93 void DScreenSourceServiceTest::SetUp(void)
94 {
95     g_mockEnableDistributedScreenReturnInt32Value = -1;
96     g_mockUnregisterDistributedHardwareV1ReturnInt32Value = -1;
97     g_mockUnregisterDistributedHardwareV2ReturnInt32Value = -1;
98     g_mockUnInitV1ReturnInt32Value = -1;
99     g_mockReleaseV2ReturnInt32Value = -1;
100     g_mockInitV1ReturnInt32Value = -1;
101     g_mockInitializeV2ReturnInt32Value = -1;
102 }
103 
TearDown(void)104 void DScreenSourceServiceTest::TearDown(void) {}
105 
106 /* *
107  * @tc.name: OnStart_001
108  * @tc.desc: Verify the OnStart function.
109  * @tc.type: FUNC
110  * @tc.require: Issue Number
111  */
112 HWTEST_F(DScreenSourceServiceTest, OnStart_001, TestSize.Level1)
113 {
114     std::shared_ptr<DScreenSourceService> sourceService =
115         std::make_shared<DScreenSourceService>(DISTRIBUTED_HARDWARE_SCREEN_SOURCE_SA_ID, false);
116     sourceService->registerToService_ = true;
117     sourceService->OnStart();
118     sourceService->OnStop();
119     EXPECT_FALSE(sourceService->registerToService_);
120 }
121 
122 /* *
123  * @tc.name: OnStop_001
124  * @tc.desc: Verify the OnStop function.
125  * @tc.type: FUNC
126  * @tc.require: Issue Number
127  */
128 HWTEST_F(DScreenSourceServiceTest, OnStop_001, TestSize.Level1)
129 {
130     std::shared_ptr<DScreenSourceService> sourceService =
131         std::make_shared<DScreenSourceService>(DISTRIBUTED_HARDWARE_SCREEN_SOURCE_SA_ID, false);
132     sourceService->registerToService_ = true;
133     g_mockReleaseV2ReturnInt32Value = 0;
134     g_mockUnInitV1ReturnInt32Value = 0;
135     sourceService->OnStop();
136     EXPECT_FALSE(sourceService->registerToService_);
137 }
138 
139 /* *
140  * @tc.name: InitSource_001
141  * @tc.desc: Verify the InitSource function.
142  * @tc.type: FUNC
143  * @tc.require: Issue Number
144  */
145 HWTEST_F(DScreenSourceServiceTest, InitSource_001, TestSize.Level1)
146 {
147     std::shared_ptr<DScreenSourceService> sourceService =
148         std::make_shared<DScreenSourceService>(DISTRIBUTED_HARDWARE_SCREEN_SOURCE_SA_ID, false);
149     sptr<IDScreenSourceCallback> callback = nullptr;
150     std::string params;
151     int32_t ret = sourceService->InitSource(params, callback);
152     EXPECT_EQ(ret, ERR_DH_SCREEN_SA_INIT_SOURCE_FAIL);
153     sourceService->ReleaseSource();
154 }
155 
156 /* *
157  * @tc.name: InitSource_002
158  * @tc.desc: Verify the InitSource function.
159  * @tc.type: FUNC
160  * @tc.require: Issue Number
161  */
162 HWTEST_F(DScreenSourceServiceTest, InitSource_002, TestSize.Level1)
163 {
164     std::shared_ptr<DScreenSourceService> sourceService =
165         std::make_shared<DScreenSourceService>(DISTRIBUTED_HARDWARE_SCREEN_SOURCE_SA_ID, false);
166     std::string params;
167     g_mockReleaseV2ReturnInt32Value = 0;
168     g_mockUnInitV1ReturnInt32Value = 0;
169     sptr<IDScreenSourceCallback> callback(new DScreenSourceCallback());
170     int32_t ret = sourceService->InitSource(params, callback);
171     EXPECT_EQ(ret, ERR_DH_SCREEN_SA_INIT_SOURCE_FAIL);
172     sourceService->ReleaseSource();
173 }
174 
175 /* *
176  * @tc.name: InitSource_003
177  * @tc.desc: Verify the InitSource function.
178  * @tc.type: FUNC
179  * @tc.require: Issue Number
180  */
181 HWTEST_F(DScreenSourceServiceTest, InitSource_003, TestSize.Level1)
182 {
183     std::shared_ptr<DScreenSourceService> sourceService =
184         std::make_shared<DScreenSourceService>(DISTRIBUTED_HARDWARE_SCREEN_SOURCE_SA_ID, false);
185     std::string params;
186     g_mockInitializeV2ReturnInt32Value = 0;
187     g_mockInitV1ReturnInt32Value = -1;
188     sptr<IDScreenSourceCallback> callback(new DScreenSourceCallback());
189     int32_t ret = sourceService->InitSource(params, callback);
190     EXPECT_EQ(ret, ERR_DH_SCREEN_SA_INIT_SOURCE_FAIL);
191 
192     g_mockInitV1ReturnInt32Value = 0;
193     ret = sourceService->InitSource(params, callback);
194     EXPECT_EQ(ret, DH_SUCCESS);
195     sourceService->ReleaseSource();
196 }
197 
198 /* *
199  * @tc.name: RegisterDistributedHardware_001
200  * @tc.desc: Verify the RegisterDistributedHardware function.
201  * @tc.type: FUNC
202  * @tc.require: Issue Number
203  */
204 HWTEST_F(DScreenSourceServiceTest, RegisterDistributedHardware_001, TestSize.Level1)
205 {
206     std::shared_ptr<DScreenSourceService> sourceService =
207         std::make_shared<DScreenSourceService>(DISTRIBUTED_HARDWARE_SCREEN_SOURCE_SA_ID, false);
208     g_mockEnableDistributedScreenReturnInt32Value = DH_SUCCESS;
209     std::string devId;
210     std::string dhId;
211     EnableParam param = { "", "", "1.0", "attrs000" };
212     std::string reqId;
213     int32_t ret = sourceService->RegisterDistributedHardware(devId, dhId, param, reqId);
214     EXPECT_EQ(ret, DH_SUCCESS);
215 }
216 
217 /* *
218  * @tc.name: RegisterDistributedHardware_002
219  * @tc.desc: Verify the RegisterDistributedHardware function.
220  * @tc.type: FUNC
221  * @tc.require: Issue Number
222  */
223 HWTEST_F(DScreenSourceServiceTest, RegisterDistributedHardware_002, TestSize.Level1)
224 {
225     std::shared_ptr<DScreenSourceService> sourceService =
226         std::make_shared<DScreenSourceService>(DISTRIBUTED_HARDWARE_SCREEN_SOURCE_SA_ID, false);
227     std::string devId;
228     std::string dhId;
229     EnableParam param = { "", "", "3.0", "attrs000" };
230     std::string reqId;
231     int32_t ret = sourceService->RegisterDistributedHardware(devId, dhId, param, reqId);
232     EXPECT_EQ(ret, ERR_DH_SCREEN_SA_ENABLE_FAILED);
233 }
234 
235 /* *
236  * @tc.name: UnregisterDistributedHardware_001
237  * @tc.desc: Verify the UnregisterDistributedHardware function.
238  * @tc.type: FUNC
239  * @tc.require: Issue Number
240  */
241 HWTEST_F(DScreenSourceServiceTest, UnregisterDistributedHardware_001, TestSize.Level1)
242 {
243     std::shared_ptr<DScreenSourceService> sourceService =
244         std::make_shared<DScreenSourceService>(DISTRIBUTED_HARDWARE_SCREEN_SOURCE_SA_ID, false);
245     std::string devId;
246     std::string dhId;
247     std::string reqId;
248     g_mockUnregisterDistributedHardwareV1ReturnInt32Value = -1;
249     int32_t ret = sourceService->UnregisterDistributedHardware(devId, dhId, reqId);
250     EXPECT_EQ(ret, ERR_DH_SCREEN_SA_DISABLE_FAILED);
251 }
252 
253 /* *
254  * @tc.name: UnregisterDistributedHardware_002
255  * @tc.desc: Verify the UnregisterDistributedHardware function.
256  * @tc.type: FUNC
257  * @tc.require: Issue Number
258  */
259 HWTEST_F(DScreenSourceServiceTest, UnregisterDistributedHardware_002, TestSize.Level1)
260 {
261     std::shared_ptr<DScreenSourceService> sourceService =
262         std::make_shared<DScreenSourceService>(DISTRIBUTED_HARDWARE_SCREEN_SOURCE_SA_ID, false);
263     std::string devId;
264     std::string dhId;
265     std::string reqId;
266     g_mockUnregisterDistributedHardwareV1ReturnInt32Value = 0;
267     g_mockUnregisterDistributedHardwareV2ReturnInt32Value = -1;
268     int32_t ret = sourceService->UnregisterDistributedHardware(devId, dhId, reqId);
269     EXPECT_EQ(ret, ERR_DH_SCREEN_SA_DISABLE_FAILED);
270 }
271 
272 /* *
273  * @tc.name: UnregisterDistributedHardware_003
274  * @tc.desc: Verify the UnregisterDistributedHardware function.
275  * @tc.type: FUNC
276  * @tc.require: Issue Number
277  */
278 HWTEST_F(DScreenSourceServiceTest, UnregisterDistributedHardware_003, TestSize.Level1)
279 {
280     std::shared_ptr<DScreenSourceService> sourceService =
281         std::make_shared<DScreenSourceService>(DISTRIBUTED_HARDWARE_SCREEN_SOURCE_SA_ID, false);
282     std::string devId;
283     std::string dhId;
284     std::string reqId;
285     g_mockUnregisterDistributedHardwareV1ReturnInt32Value = 0;
286     g_mockUnregisterDistributedHardwareV2ReturnInt32Value = 0;
287     int32_t ret = sourceService->UnregisterDistributedHardware(devId, dhId, reqId);
288     EXPECT_EQ(ret, DH_SUCCESS);
289 }
290 } // namespace DistributedHardware
291 } // namespace OHOS
292