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 "distributed_module_config_mock_test.h"
17
18 #include "device/dev_profile.h"
19 #include "device/distributed_module_config.h"
20 #include "device/dm_adapter.h"
21 #include "distributed_device_profile_errors.h"
22 #include "pasteboard_error.h"
23
24 using namespace testing;
25 using namespace testing::ext;
26
27 namespace OHOS {
28 namespace MiscServices {
29
SetUpTestCase(void)30 void DistributedModuleConfigMockTest::SetUpTestCase(void)
31 {
32 DistributedHardware::PasteDeviceManager::pasteDeviceManager = deviceManagerMock_;
33 }
34
TearDownTestCase(void)35 void DistributedModuleConfigMockTest::TearDownTestCase(void)
36 {
37 DevProfile::GetInstance().ClearDeviceProfileService();
38 DistributedHardware::PasteDeviceManager::pasteDeviceManager = nullptr;
39 deviceManagerMock_ = nullptr;
40 }
41
SetUp(void)42 void DistributedModuleConfigMockTest::SetUp(void) { }
43
TearDown(void)44 void DistributedModuleConfigMockTest::TearDown(void)
45 {
46 testing::Mock::VerifyAndClear(deviceManagerMock_.get());
47 }
48
49 /**
50 * @tc.name: GetEnabledStatusTest001
51 * @tc.desc: GetEnabledStatus should return LOCAL_SWITCH_NOT_TURNED_ON when local switch off.
52 * @tc.type: FUNC
53 * @tc.require:
54 * @tc.author:
55 */
56 HWTEST_F(DistributedModuleConfigMockTest, GetEnabledStatusTest001, TestSize.Level0)
57 {
58 NiceMock<DistributedDeviceProfile::DeviceProfileClientMock> dpMock;
59 EXPECT_CALL(dpMock, GetCharacteristicProfile)
60 .WillRepeatedly(testing::Return(static_cast<int32_t>(PasteboardError::NO_TRUST_DEVICE_ERROR)));
61 DMAdapter::GetInstance().devices_.clear();
62 DevProfile::GetInstance().enabledStatusCache_.Clear();
63 EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
__anonb286ead00102(auto, auto, std::string &udid) 64 .WillRepeatedly([](auto, auto, std::string &udid) {
65 udid = "testUdid";
66 return 0;
67 });
68 DMAdapter::GetInstance().pkgName_ = "com.exapmle.myApplicationdm_adaper";
69 DistributedModuleConfig config;
70 int32_t ret = config.GetEnabledStatus();
71 ASSERT_EQ(static_cast<int32_t>(PasteboardError::LOCAL_SWITCH_NOT_TURNED_ON), ret);
72 }
73
74 /**
75 * @tc.name: GetEnabledStatusTest002
76 * @tc.desc: GetEnabledStatus should return NO_TRUST_DEVICE_ERROR when remote device is invalid.
77 * @tc.type: FUNC
78 * @tc.require:
79 * @tc.author:
80 */
81 HWTEST_F(DistributedModuleConfigMockTest, GetEnabledStatusTest002, TestSize.Level0)
82 {
83 NiceMock<DistributedDeviceProfile::DeviceProfileClientMock> dpMock;
84 EXPECT_CALL(dpMock, GetCharacteristicProfile)
85 .Times(2)
__anonb286ead00202(auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) 86 .WillOnce([](auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) {
87 characteristicProfile.characteristicValue_ = "1";
88 return DistributedDeviceProfile::DP_SUCCESS;
89 })
__anonb286ead00302(auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) 90 .WillRepeatedly([](auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) {
91 characteristicProfile.characteristicValue_ = "0";
92 return DistributedDeviceProfile::DP_SUCCESS;
93 });
94 DMAdapter::GetInstance().devices_.clear();
95 DevProfile::GetInstance().enabledStatusCache_.Clear();
96 EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
97 .Times(2)
__anonb286ead00402(auto, auto, std::string &udid) 98 .WillOnce([](auto, auto, std::string &udid) {
99 udid = "testUdid";
100 return 0;
101 })
__anonb286ead00502(auto, auto, std::string &udid) 102 .WillOnce([](auto, auto, std::string &udid) {
103 udid = "invalidUdid";
104 return 0;
105 });
106 DMAdapter::GetInstance().pkgName_ = "com.exapmle.myApplicationdm_adaper";
107 std::string networkId = "invalidNetworkId";
108 std::string testName = "testDeviceName";
109 DmDeviceInfo info;
110 info.authForm = IDENTICAL_ACCOUNT;
111 std::copy(networkId.begin(), networkId.end(), info.networkId);
112 std::copy(testName.begin(), testName.end(), info.deviceName);
113 DMAdapter::GetInstance().devices_.emplace_back(info);
114 DistributedModuleConfig config;
115 int32_t ret = config.GetEnabledStatus();
116 ASSERT_EQ(static_cast<int32_t>(PasteboardError::NO_TRUST_DEVICE_ERROR), ret);
117 DMAdapter::GetInstance().devices_.clear();
118 }
119
120 /**
121 * @tc.name: GetEnabledStatusTest003
122 * @tc.desc: GetEnabledStatus should return E_OK when query valid network.
123 * @tc.type: FUNC
124 * @tc.require:
125 * @tc.author:
126 */
127 HWTEST_F(DistributedModuleConfigMockTest, GetEnabledStatusTest003, TestSize.Level0)
128 {
129 NiceMock<DistributedDeviceProfile::DeviceProfileClientMock> dpMock;
130 EXPECT_CALL(dpMock, GetCharacteristicProfile)
__anonb286ead00602(auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) 131 .WillRepeatedly([](auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) {
132 characteristicProfile.characteristicValue_ = "1";
133 return DistributedDeviceProfile::DP_SUCCESS;
134 });
135 DMAdapter::GetInstance().devices_.clear();
136 DevProfile::GetInstance().enabledStatusCache_.Clear();
137 EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
138 .Times(2)
__anonb286ead00702(auto, auto, std::string &udid) 139 .WillRepeatedly([](auto, auto, std::string &udid) {
140 udid = "testUdid";
141 return 0;
142 });
143 DMAdapter::GetInstance().pkgName_ = "com.exapmle.myApplicationdm_adaper";
144 std::string networkId = DMAdapter::GetInstance().GetLocalNetworkId();
145 std::string testName = "testDeviceName";
146 DmDeviceInfo info;
147 info.authForm = IDENTICAL_ACCOUNT;
148 std::copy(networkId.begin(), networkId.end(), info.networkId);
149 std::copy(testName.begin(), testName.end(), info.deviceName);
150 DMAdapter::GetInstance().devices_.emplace_back(info);
151 DistributedModuleConfig config;
152 int32_t ret = config.GetEnabledStatus();
153 ASSERT_EQ(static_cast<int32_t>(PasteboardError::E_OK), ret);
154 DMAdapter::GetInstance().devices_.clear();
155 }
156
157 /**
158 * @tc.name: Notify001
159 * @tc.desc: Notify.
160 * @tc.type: FUNC
161 * @tc.require:
162 * @tc.author:
163 */
164 HWTEST_F(DistributedModuleConfigMockTest, Notify001, TestSize.Level0)
165 {
166 NiceMock<DistributedDeviceProfile::DeviceProfileClientMock> dpMock;
167 EXPECT_CALL(dpMock, GetCharacteristicProfile)
__anonb286ead00802(auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) 168 .WillRepeatedly([](auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) {
169 characteristicProfile.characteristicValue_ = "1";
170 return DistributedDeviceProfile::DP_SUCCESS;
171 });
172 DMAdapter::GetInstance().devices_.clear();
173 DevProfile::GetInstance().enabledStatusCache_.Clear();
174 EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
__anonb286ead00902(auto, auto, std::string &udid) 175 .WillRepeatedly([](auto, auto, std::string &udid) {
176 udid = "testUdid";
177 return 0;
178 });
179 DMAdapter::GetInstance().pkgName_ = "com.exapmle.myApplicationdm_adaper";
180 std::string networkId = DMAdapter::GetInstance().GetLocalNetworkId();
181 std::string testName = "testDeviceName";
182 DmDeviceInfo info;
183 info.authForm = IDENTICAL_ACCOUNT;
184 std::copy(networkId.begin(), networkId.end(), info.networkId);
185 std::copy(testName.begin(), testName.end(), info.deviceName);
186 DMAdapter::GetInstance().devices_.emplace_back(info);
187 DistributedModuleConfig config;
188 config.status_ = false;
189 config.Notify();
190 ASSERT_TRUE(true);
191 DMAdapter::GetInstance().devices_.clear();
192 }
193
194 /**
195 * @tc.name: Notify002
196 * @tc.desc: Notify.
197 * @tc.type: FUNC
198 * @tc.require:
199 * @tc.author:
200 */
201 HWTEST_F(DistributedModuleConfigMockTest, Notify002, TestSize.Level0)
202 {
203 NiceMock<DistributedDeviceProfile::DeviceProfileClientMock> dpMock;
204 EXPECT_CALL(dpMock, GetCharacteristicProfile)
__anonb286ead00a02(auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) 205 .WillRepeatedly([](auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) {
206 characteristicProfile.characteristicValue_ = "1";
207 return DistributedDeviceProfile::DP_SUCCESS;
208 });
209 DMAdapter::GetInstance().devices_.clear();
210 DevProfile::GetInstance().enabledStatusCache_.Clear();
211 EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
__anonb286ead00b02(auto, auto, std::string &udid) 212 .WillRepeatedly([](auto, auto, std::string &udid) {
213 udid = "testUdid";
214 return 0;
215 });
216 DMAdapter::GetInstance().pkgName_ = "com.exapmle.myApplicationdm_adaper";
217 std::string networkId = DMAdapter::GetInstance().GetLocalNetworkId();
218 std::string testName = "testDeviceName";
219 DmDeviceInfo info;
220 info.authForm = IDENTICAL_ACCOUNT;
221 std::copy(networkId.begin(), networkId.end(), info.networkId);
222 std::copy(testName.begin(), testName.end(), info.deviceName);
223 DMAdapter::GetInstance().devices_.emplace_back(info);
224 DistributedModuleConfig config;
225 config.status_ = false;
__anonb286ead00c02(bool isOn) 226 std::function<void(bool isOn)> func = [](bool isOn) {
227 return;
228 };
229 config.observer_ = func;
230 config.Notify();
231 ASSERT_TRUE(true);
232 DMAdapter::GetInstance().devices_.clear();
233 }
234
235 /**
236 * @tc.name: GetRemoteDeviceMinVersion001
237 * @tc.desc: GetRemoteDeviceMinVersion.
238 * @tc.type: FUNC
239 * @tc.require:
240 * @tc.author:
241 */
242 HWTEST_F(DistributedModuleConfigMockTest, GetRemoteDeviceMinVersion001, TestSize.Level0)
243 {
244 NiceMock<DistributedDeviceProfile::DeviceProfileClientMock> dpMock;
245 EXPECT_CALL(dpMock, GetCharacteristicProfile)
__anonb286ead00d02(auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) 246 .WillRepeatedly([](auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) {
247 characteristicProfile.characteristicValue_ = "0";
248 return static_cast<int32_t>(PasteboardError::NO_TRUST_DEVICE_ERROR);
249 });
250 DMAdapter::GetInstance().devices_.clear();
251 DevProfile::GetInstance().enabledStatusCache_.Clear();
252 EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
__anonb286ead00e02(auto, auto, std::string &udid) 253 .WillRepeatedly([](auto, auto, std::string &udid) {
254 udid = "testUdid";
255 return 0;
256 });
257 DMAdapter::GetInstance().pkgName_ = "com.exapmle.myApplicationdm_adaper";
258 std::string networkId = DMAdapter::GetInstance().GetLocalNetworkId();
259 std::string testName = "testDeviceName";
260 DmDeviceInfo info;
261 info.authForm = IDENTICAL_ACCOUNT;
262 std::copy(networkId.begin(), networkId.end(), info.networkId);
263 std::copy(testName.begin(), testName.end(), info.deviceName);
264 DMAdapter::GetInstance().devices_.emplace_back(info);
265 DistributedModuleConfig config;
266 uint32_t minVersion = config.GetRemoteDeviceMinVersion();
267 ASSERT_EQ(UINT_MAX, minVersion);
268 DMAdapter::GetInstance().devices_.clear();
269 }
270
271 /**
272 * @tc.name: GetRemoteDeviceMinVersion002
273 * @tc.desc: GetRemoteDeviceMinVersion.
274 * @tc.type: FUNC
275 * @tc.require:
276 * @tc.author:
277 */
278 HWTEST_F(DistributedModuleConfigMockTest, GetRemoteDeviceMinVersion002, TestSize.Level0)
279 {
280 NiceMock<DistributedDeviceProfile::DeviceProfileClientMock> dpMock;
281 EXPECT_CALL(dpMock, GetCharacteristicProfile)
__anonb286ead00f02(auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) 282 .WillRepeatedly([](auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) {
283 characteristicProfile.characteristicValue_ = "0";
284 return static_cast<int32_t>(DistributedDeviceProfile::DP_SUCCESS);
285 });
286 DMAdapter::GetInstance().devices_.clear();
287 DevProfile::GetInstance().enabledStatusCache_.Clear();
288 EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
__anonb286ead01002(auto, auto, std::string &udid) 289 .WillRepeatedly([](auto, auto, std::string &udid) {
290 udid = "testUdid";
291 return 0;
292 });
293 DMAdapter::GetInstance().pkgName_ = "com.exapmle.myApplicationdm_adaper";
294 std::string networkId = DMAdapter::GetInstance().GetLocalNetworkId();
295 std::string testName = "testDeviceName";
296 DmDeviceInfo info;
297 info.authForm = IDENTICAL_ACCOUNT;
298 std::copy(networkId.begin(), networkId.end(), info.networkId);
299 std::copy(testName.begin(), testName.end(), info.deviceName);
300 DMAdapter::GetInstance().devices_.emplace_back(info);
301 DistributedModuleConfig config;
302 uint32_t minVersion = config.GetRemoteDeviceMinVersion();
303 ASSERT_EQ(UINT_MAX, minVersion);
304 DMAdapter::GetInstance().devices_.clear();
305 }
306
307 /**
308 * @tc.name: GetRemoteDeviceMinVersion003
309 * @tc.desc: GetRemoteDeviceMinVersion.
310 * @tc.type: FUNC
311 * @tc.require:
312 * @tc.author:
313 */
314 HWTEST_F(DistributedModuleConfigMockTest, GetRemoteDeviceMinVersion003, TestSize.Level0)
315 {
316 NiceMock<DistributedDeviceProfile::DeviceProfileClientMock> dpMock;
317 EXPECT_CALL(dpMock, GetCharacteristicProfile)
__anonb286ead01102(auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) 318 .WillRepeatedly([](auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) {
319 characteristicProfile.characteristicValue_ = "1";
320 return static_cast<int32_t>(PasteboardError::NO_TRUST_DEVICE_ERROR);
321 });
322 DMAdapter::GetInstance().devices_.clear();
323 DevProfile::GetInstance().enabledStatusCache_.Clear();
324 EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
__anonb286ead01202(auto, auto, std::string &udid) 325 .WillRepeatedly([](auto, auto, std::string &udid) {
326 udid = "testUdid";
327 return 0;
328 });
329 DMAdapter::GetInstance().pkgName_ = "com.exapmle.myApplicationdm_adaper";
330 std::string networkId = DMAdapter::GetInstance().GetLocalNetworkId();
331 std::string testName = "testDeviceName";
332 DmDeviceInfo info;
333 info.authForm = IDENTICAL_ACCOUNT;
334 std::copy(networkId.begin(), networkId.end(), info.networkId);
335 std::copy(testName.begin(), testName.end(), info.deviceName);
336 DMAdapter::GetInstance().devices_.emplace_back(info);
337 DistributedModuleConfig config;
338 uint32_t minVersion = config.GetRemoteDeviceMinVersion();
339 ASSERT_EQ(UINT_MAX, minVersion);
340 DMAdapter::GetInstance().devices_.clear();
341 }
342
343 /**
344 * @tc.name: GetRemoteDeviceMinVersion004
345 * @tc.desc: GetRemoteDeviceMinVersion.
346 * @tc.type: FUNC
347 * @tc.require:
348 * @tc.author:
349 */
350 HWTEST_F(DistributedModuleConfigMockTest, GetRemoteDeviceMinVersion004, TestSize.Level0)
351 {
352 NiceMock<DistributedDeviceProfile::DeviceProfileClientMock> dpMock;
353 EXPECT_CALL(dpMock, GetCharacteristicProfile)
__anonb286ead01302(auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) 354 .WillRepeatedly([](auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) {
355 characteristicProfile.characteristicValue_ = "1";
356 return static_cast<int32_t>(DistributedDeviceProfile::DP_SUCCESS);
357 });
358 DMAdapter::GetInstance().devices_.clear();
359 DevProfile::GetInstance().enabledStatusCache_.Clear();
360 EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
__anonb286ead01402(auto, auto, std::string &udid) 361 .WillRepeatedly([](auto, auto, std::string &udid) {
362 udid = "testUdid";
363 return 0;
364 });
365 DMAdapter::GetInstance().pkgName_ = "com.exapmle.myApplicationdm_adaper";
366 std::string networkId = DMAdapter::GetInstance().GetLocalNetworkId();
367 std::string testName = "testDeviceName";
368 DmDeviceInfo info;
369 info.authForm = IDENTICAL_ACCOUNT;
370 std::copy(networkId.begin(), networkId.end(), info.networkId);
371 std::copy(testName.begin(), testName.end(), info.deviceName);
372 DMAdapter::GetInstance().devices_.emplace_back(info);
373 DistributedModuleConfig config;
374 uint32_t minVersion = config.GetRemoteDeviceMinVersion();
375 ASSERT_EQ(UINT_MAX, minVersion);
376 DMAdapter::GetInstance().devices_.clear();
377 }
378
379 } // namespace MiscServices
380 } // namespace OHOS