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 "dev_profile_mock_test.h"
17
18 #include "device/dev_profile.h"
19 #include "device/dm_adapter.h"
20 #include "distributed_device_profile_errors.h"
21 #include "pasteboard_error.h"
22
23 using namespace testing;
24 using namespace testing::ext;
25
26 namespace OHOS {
27 namespace MiscServices {
28
SetUpTestCase(void)29 void DevProfileMockTest::SetUpTestCase(void)
30 {
31 DistributedHardware::PasteDeviceManager::pasteDeviceManager = deviceManagerMock_;
32 }
33
TearDownTestCase(void)34 void DevProfileMockTest::TearDownTestCase(void)
35 {
36 DevProfile::GetInstance().ClearDeviceProfileService();
37 DistributedHardware::PasteDeviceManager::pasteDeviceManager = nullptr;
38 deviceManagerMock_ = nullptr;
39 }
40
SetUp(void)41 void DevProfileMockTest::SetUp(void) { }
42
TearDown(void)43 void DevProfileMockTest::TearDown(void)
44 {
45 testing::Mock::VerifyAndClear(deviceManagerMock_.get());
46 }
47 /**
48 * @tc.name: GetDeviceStatusTest001
49 * @tc.desc: GetDeviceStatus should return E_OK when query valid networkId
50 * @tc.type: FUNC
51 * @tc.require:
52 * @tc.author:
53 */
54 HWTEST_F(DevProfileMockTest, GetDeviceStatusTest001, TestSize.Level0)
55 {
56 NiceMock<DistributedDeviceProfile::DeviceProfileClientMock> dpMock;
57 EXPECT_CALL(dpMock, GetCharacteristicProfile)
58 .WillRepeatedly(testing::Return(static_cast<int32_t>(PasteboardError::NO_TRUST_DEVICE_ERROR)));
59 std::string bundleName = "com.example.myApplication";
60 bool res = DMAdapter::GetInstance().Initialize(bundleName);
61 EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
62 .Times(1)
__anonf3c92d020102(auto, auto, std::string &udid) 63 .WillRepeatedly([](auto, auto, std::string &udid) {
64 udid = "testUdid";
65 return 0;
66 });
67 DMAdapter::GetInstance().pkgName_ = "com.exapmle.myApplicationdm_adaper";
68 bool enabledStatus = false;
69 auto networkId = DMAdapter::GetInstance().GetLocalNetworkId();
70 int32_t ret = DevProfile::GetInstance().GetDeviceStatus(networkId, enabledStatus);
71 ASSERT_EQ(static_cast<int32_t>(PasteboardError::NO_TRUST_DEVICE_ERROR), ret);
72 }
73
74 /**
75 * @tc.name: GetDeviceStatusTest002
76 * @tc.desc: GetDeviceStatus should return E_OK when query valid networkId
77 * @tc.type: FUNC
78 * @tc.require:
79 * @tc.author:
80 */
81 HWTEST_F(DevProfileMockTest, GetDeviceStatusTest002, TestSize.Level0)
82 {
83 NiceMock<DistributedDeviceProfile::DeviceProfileClientMock> dpMock;
84 EXPECT_CALL(dpMock, GetCharacteristicProfile)
85 .WillRepeatedly(testing::Return(DistributedDeviceProfile::DP_SUCCESS));
86 EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
__anonf3c92d020202(auto, auto, std::string &udid) 87 .WillRepeatedly([](auto, auto, std::string &udid) {
88 udid = "testUdid";
89 return 0;
90 });
91 DMAdapter::GetInstance().pkgName_ = "com.exapmle.myApplicationdm_adaper";
92 bool enabledStatus = false;
93 auto networkId = DMAdapter::GetInstance().GetLocalNetworkId();
94 int32_t ret = DevProfile::GetInstance().GetDeviceStatus(networkId, enabledStatus);
95 ASSERT_EQ(static_cast<int32_t>(PasteboardError::E_OK), ret);
96 ret = DevProfile::GetInstance().GetDeviceStatus(networkId, enabledStatus);
97 ASSERT_EQ(static_cast<int32_t>(PasteboardError::E_OK), ret);
98 }
99
100 /**
101 * @tc.name: PutDeviceStatus001
102 * @tc.desc: PutDeviceStatus
103 * @tc.type: FUNC
104 * @tc.require:
105 * @tc.author:
106 */
107 HWTEST_F(DevProfileMockTest, PutDeviceStatus001, TestSize.Level0)
108 {
109 NiceMock<DistributedDeviceProfile::DeviceProfileClientMock> dpMock;
110 EXPECT_CALL(dpMock, PutCharacteristicProfile)
111 .WillRepeatedly(testing::Return(DistributedDeviceProfile::DP_SUCCESS));
112 EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
__anonf3c92d020302(auto, auto, std::string &udid) 113 .WillRepeatedly([](auto, auto, std::string &udid) {
114 udid = "testUdid";
115 return 0;
116 });
117 DMAdapter::GetInstance().pkgName_ = "com.exapmle.myApplicationdm_adaper";
118 bool enabledStatus = true;
119 DevProfile::GetInstance().PutDeviceStatus(enabledStatus);
120 EXPECT_TRUE(true);
121 }
122
123 /**
124 * @tc.name: PutDeviceStatus002
125 * @tc.desc: PutDeviceStatus
126 * @tc.type: FUNC
127 * @tc.require:
128 * @tc.author:
129 */
130 HWTEST_F(DevProfileMockTest, PutDeviceStatus002, TestSize.Level0)
131 {
132 NiceMock<DistributedDeviceProfile::DeviceProfileClientMock> dpMock;
133 EXPECT_CALL(dpMock, PutCharacteristicProfile)
134 .WillRepeatedly(testing::Return(DistributedDeviceProfile::DP_CACHE_EXIST));
135 EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
__anonf3c92d020402(auto, auto, std::string &udid) 136 .WillRepeatedly([](auto, auto, std::string &udid) {
137 udid = "testUdid";
138 return 0;
139 });
140 DMAdapter::GetInstance().pkgName_ = "com.exapmle.myApplicationdm_adaper";
141 bool enabledStatus = true;
142 DevProfile::GetInstance().PutDeviceStatus(enabledStatus);
143 EXPECT_TRUE(true);
144 }
145
146 /**
147 * @tc.name: PutDeviceStatus003
148 * @tc.desc: PutDeviceStatus
149 * @tc.type: FUNC
150 * @tc.require:
151 * @tc.author:
152 */
153 HWTEST_F(DevProfileMockTest, PutDeviceStatus003, TestSize.Level0)
154 {
155 NiceMock<DistributedDeviceProfile::DeviceProfileClientMock> dpMock;
156 EXPECT_CALL(dpMock, PutCharacteristicProfile)
157 .WillRepeatedly(testing::Return(DistributedDeviceProfile::DP_INVALID_PARAMS));
158 EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
__anonf3c92d020502(auto, auto, std::string &udid) 159 .WillRepeatedly([](auto, auto, std::string &udid) {
160 udid = "testUdid";
161 return 0;
162 });
163 DMAdapter::GetInstance().pkgName_ = "com.exapmle.myApplicationdm_adaper";
164 bool enabledStatus = true;
165 DevProfile::GetInstance().PutDeviceStatus(enabledStatus);
166 EXPECT_TRUE(true);
167 }
168
169 /**
170 * @tc.name: GetDeviceVersionTest001
171 * @tc.desc: GetDeviceVersionTest
172 * @tc.type: FUNC
173 * @tc.require:
174 * @tc.author:
175 */
176 HWTEST_F(DevProfileMockTest, GetDeviceVersionTest001, TestSize.Level0)
177 {
178 NiceMock<DistributedDeviceProfile::DeviceProfileClientMock> dpMock;
179 EXPECT_CALL(dpMock, GetCharacteristicProfile)
180 .WillRepeatedly(testing::Return(DistributedDeviceProfile::DP_SUCCESS));
181 EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
__anonf3c92d020602(auto, auto, std::string &udid) 182 .WillRepeatedly([](auto, auto, std::string &udid) {
183 udid = "testUdid";
184 return 0;
185 });
186 DMAdapter::GetInstance().pkgName_ = "com.exapmle.myApplicationdm_adaper";
187 uint32_t versionId;
188 std::string bundleName = "com.dev.profile";
189 DevProfile::GetInstance().proxy_ = nullptr;
190 DevProfile::GetInstance().subscribeUdidList_.clear();
191 DevProfile::GetInstance().GetDeviceVersion(bundleName, versionId);
192 EXPECT_TRUE(true);
193 }
194
195 /**
196 * @tc.name: GetDeviceVersionTest002
197 * @tc.desc: GetDeviceVersionTest
198 * @tc.type: FUNC
199 * @tc.require:
200 * @tc.author:
201 */
202 HWTEST_F(DevProfileMockTest, GetDeviceVersionTest002, TestSize.Level0)
203 {
204 NiceMock<DistributedDeviceProfile::DeviceProfileClientMock> dpMock;
205 EXPECT_CALL(dpMock, GetCharacteristicProfile)
206 .WillRepeatedly(testing::Return(DistributedDeviceProfile::DP_INVALID_PARAMS));
207 EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
__anonf3c92d020702(auto, auto, std::string &udid) 208 .WillRepeatedly([](auto, auto, std::string &udid) {
209 udid = "testUdid";
210 return 0;
211 });
212 DMAdapter::GetInstance().pkgName_ = "com.exapmle.myApplicationdm_adaper";
213 uint32_t versionId;
214 std::string bundleName = "com.dev.profile";
215 DevProfile::GetInstance().GetDeviceVersion(bundleName, versionId);
216 EXPECT_TRUE(true);
217 }
218
219 /**
220 * @tc.name: GetDeviceVersionTest003
221 * @tc.desc: GetDeviceVersionTest
222 * @tc.type: FUNC
223 * @tc.require:
224 * @tc.author:
225 */
226 HWTEST_F(DevProfileMockTest, GetDeviceVersionTest003, TestSize.Level0)
227 {
228 NiceMock<DistributedDeviceProfile::DeviceProfileClientMock> dpMock;
229 EXPECT_CALL(dpMock, GetCharacteristicProfile)
__anonf3c92d020802(auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) 230 .WillRepeatedly([](auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) {
231 characteristicProfile.characteristicValue_ = "{}";
232 return DistributedDeviceProfile::DP_SUCCESS;
233 });
234 EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
__anonf3c92d020902(auto, auto, std::string &udid) 235 .WillRepeatedly([](auto, auto, std::string &udid) {
236 udid = "testUdid";
237 return 0;
238 });
239 DMAdapter::GetInstance().pkgName_ = "com.exapmle.myApplicationdm_adaper";
240 uint32_t versionId;
241 std::string bundleName = "com.dev.profile";
242 DevProfile::GetInstance().GetDeviceVersion(bundleName, versionId);
243 EXPECT_TRUE(true);
244 }
245
246 /**
247 * @tc.name: GetDeviceVersionTest004
248 * @tc.desc: GetDeviceVersionTest
249 * @tc.type: FUNC
250 * @tc.require:
251 * @tc.author:
252 */
253 HWTEST_F(DevProfileMockTest, GetDeviceVersionTest004, TestSize.Level0)
254 {
255 NiceMock<DistributedDeviceProfile::DeviceProfileClientMock> dpMock;
256 EXPECT_CALL(dpMock, GetCharacteristicProfile)
__anonf3c92d020a02(auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) 257 .WillRepeatedly([](auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) {
258 characteristicProfile.characteristicValue_ = R"({"PasteboardVersionId": "invalid"})";
259 return DistributedDeviceProfile::DP_SUCCESS;
260 });
261 EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
__anonf3c92d020b02(auto, auto, std::string &udid) 262 .WillRepeatedly([](auto, auto, std::string &udid) {
263 udid = "testUdid";
264 return 0;
265 });
266 DMAdapter::GetInstance().pkgName_ = "com.exapmle.myApplicationdm_adaper";
267 uint32_t versionId;
268 std::string bundleName = "com.dev.profile";
269 DevProfile::GetInstance().GetDeviceVersion(bundleName, versionId);
270 EXPECT_TRUE(true);
271 }
272
273 /**
274 * @tc.name: GetDeviceVersionTest005
275 * @tc.desc: GetDeviceVersionTest
276 * @tc.type: FUNC
277 * @tc.require:
278 * @tc.author:
279 */
280 HWTEST_F(DevProfileMockTest, GetDeviceVersionTest005, TestSize.Level0)
281 {
282 NiceMock<DistributedDeviceProfile::DeviceProfileClientMock> dpMock;
283 EXPECT_CALL(dpMock, GetCharacteristicProfile)
__anonf3c92d020c02(auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) 284 .WillRepeatedly([](auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) {
285 characteristicProfile.characteristicValue_ = R"({"PasteboardVersionId": 10})";
286 return DistributedDeviceProfile::DP_SUCCESS;
287 });
288 EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
__anonf3c92d020d02(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 uint32_t versionId;
295 std::string bundleName = "com.dev.profile";
296 DevProfile::GetInstance().GetDeviceVersion(bundleName, versionId);
297 EXPECT_TRUE(true);
298 }
299
300 /**
301 * @tc.name: GetDeviceVersionTest006
302 * @tc.desc: GetDeviceVersionTest
303 * @tc.type: FUNC
304 * @tc.require:
305 * @tc.author:
306 */
307 HWTEST_F(DevProfileMockTest, GetDeviceVersionTest006, TestSize.Level0)
308 {
309 NiceMock<DistributedDeviceProfile::DeviceProfileClientMock> dpMock;
310 EXPECT_CALL(dpMock, GetCharacteristicProfile)
__anonf3c92d020e02(auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) 311 .WillRepeatedly([](auto, auto, auto, DistributedDeviceProfile::CharacteristicProfile &characteristicProfile) {
312 characteristicProfile.characteristicValue_ = R"({"PasteboardVersionId": -1})";
313 return DistributedDeviceProfile::DP_SUCCESS;
314 });
315 EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
__anonf3c92d020f02(auto, auto, std::string &udid) 316 .WillRepeatedly([](auto, auto, std::string &udid) {
317 udid = "testUdid";
318 return 0;
319 });
320 DMAdapter::GetInstance().pkgName_ = "com.exapmle.myApplicationdm_adaper";
321 uint32_t versionId;
322 std::string bundleName = "com.dev.profile";
323 DevProfile::GetInstance().GetDeviceVersion(bundleName, versionId);
324 EXPECT_TRUE(true);
325 }
326
327 /**
328 * @tc.name: SubscribeProfileEvent001
329 * @tc.desc: Sub scribe Profile Event
330 * @tc.type: FUNC
331 * @tc.require:
332 * @tc.author:
333 */
334 HWTEST_F(DevProfileMockTest, SubscribeProfileEventTest001, TestSize.Level0)
335 {
336 NiceMock<DistributedDeviceProfile::DeviceProfileClientMock> dpMock;
337 EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
__anonf3c92d021002(auto, auto, std::string &udid) 338 .WillRepeatedly([](auto, auto, std::string &udid) {
339 udid = "SubscribeProfileEventTest001";
340 return 0;
341 });
342 DevProfile::GetInstance().proxy_ = nullptr;
343 DevProfile::GetInstance().subscribeUdidList_.clear();
344 std::string bundleName = "com.pro.proEvent";
345 DevProfile::GetInstance().SendSubscribeInfos();
346 DevProfile::GetInstance().SubscribeProfileEvent(bundleName);
347 DevProfile::GetInstance().SendSubscribeInfos();
348 DevProfile::GetInstance().UnSubscribeProfileEvent(bundleName);
349 EXPECT_TRUE(true);
350 }
351 } // namespace MiscServices
352 } // namespace OHOS