• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 "dm_adapter_mock_test.h"
17 
18 #include "device/dm_adapter.h"
19 #include "pasteboard_error.h"
20 
21 using namespace testing;
22 using namespace testing::ext;
23 
24 namespace OHOS {
25 namespace MiscServices {
26 
SetUpTestCase(void)27 void DMAdapterMockTest::SetUpTestCase(void)
28 {
29     DistributedHardware::PasteDeviceManager::pasteDeviceManager = deviceManagerMock_;
30 }
31 
TearDownTestCase(void)32 void DMAdapterMockTest::TearDownTestCase(void)
33 {
34     DistributedHardware::PasteDeviceManager::pasteDeviceManager = nullptr;
35     deviceManagerMock_ = nullptr;
36 }
37 
SetUp(void)38 void DMAdapterMockTest::SetUp(void) { }
39 
TearDown(void)40 void DMAdapterMockTest::TearDown(void)
41 {
42     testing::Mock::VerifyAndClear(deviceManagerMock_.get());
43 }
44 
45 /**
46  * @tc.name: OnDeviceChanged001
47  * @tc.desc: OnDeviceChanged.
48  * @tc.type: FUNC
49  * @tc.require:
50  * @tc.author:
51  */
52 HWTEST_F(DMAdapterMockTest, OnDeviceChanged001, TestSize.Level0)
53 {
54 #ifdef PB_DEVICE_MANAGER_ENABLE
55     EXPECT_CALL(*deviceManagerMock_, IsSameAccount(testing::_)).Times(1).WillRepeatedly(testing::Return(true));
56     DmDeviceInfo info;
57     std::string networkId = "testNetworkId";
58     std::copy(networkId.begin(), networkId.end(), info.networkId);
59     auto stateObserver = std::make_shared<DmStateObserver>(
__anoncc0af8440102(const DmDeviceInfo &deviceInfo) 60         [](const DmDeviceInfo &deviceInfo) {
61             return;
62         },
__anoncc0af8440202(const DmDeviceInfo &deviceInfo) 63         [](const DmDeviceInfo &deviceInfo) {
64             return;
65         },
__anoncc0af8440302(const DmDeviceInfo &deviceInfo) 66         [](const DmDeviceInfo &deviceInfo) {
67             return;
68         });
69     stateObserver->OnDeviceChanged(info);
70     ASSERT_TRUE(true);
71 #else
72     ASSERT_TRUE(true);
73 #endif
74 }
75 
76 /**
77  * @tc.name: OnDeviceChanged002
78  * @tc.desc: OnDeviceChanged.
79  * @tc.type: FUNC
80  * @tc.require:
81  * @tc.author:
82  */
83 HWTEST_F(DMAdapterMockTest, OnDeviceChanged002, TestSize.Level0)
84 {
85 #ifdef PB_DEVICE_MANAGER_ENABLE
86     EXPECT_CALL(*deviceManagerMock_, IsSameAccount(testing::_)).Times(1).WillRepeatedly(testing::Return(false));
87     DmDeviceInfo info;
88     std::string networkId = "testNetworkId";
89     std::copy(networkId.begin(), networkId.end(), info.networkId);
90     auto stateObserver = std::make_shared<DmStateObserver>(
__anoncc0af8440402(const DmDeviceInfo &deviceInfo) 91         [](const DmDeviceInfo &deviceInfo) {
92             return;
93         },
__anoncc0af8440502(const DmDeviceInfo &deviceInfo) 94         [](const DmDeviceInfo &deviceInfo) {
95             return;
96         },
__anoncc0af8440602(const DmDeviceInfo &deviceInfo) 97         [](const DmDeviceInfo &deviceInfo) {
98             return;
99         });
100     stateObserver->OnDeviceChanged(info);
101     ASSERT_TRUE(true);
102 #else
103     ASSERT_TRUE(true);
104 #endif
105 }
106 
107 /**
108  * @tc.name: GetLocalDeviceUdid001
109  * @tc.desc: GetLocalDeviceUdid.
110  * @tc.type: FUNC
111  * @tc.require:
112  * @tc.author:
113  */
114 HWTEST_F(DMAdapterMockTest, GetLocalDeviceUdid001, TestSize.Level0)
115 {
116 #ifdef PB_DEVICE_MANAGER_ENABLE
117     EXPECT_CALL(*deviceManagerMock_, GetLocalDeviceInfo(testing::_, testing::_))
118         .Times(1)
119         .WillRepeatedly(testing::Return(1));
120     EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_)).Times(0);
121     DMAdapter::GetInstance().localDeviceUdid_ = "";
122     std::string udid = DMAdapter::GetInstance().GetLocalDeviceUdid();
123     ASSERT_TRUE(udid.empty());
124     DMAdapter::GetInstance().localDeviceUdid_ = "";
125 #else
126     ASSERT_TRUE(true);
127 #endif
128 }
129 
130 /**
131  * @tc.name: GetLocalDeviceUdid002
132  * @tc.desc: GetLocalDeviceUdid.
133  * @tc.type: FUNC
134  * @tc.require:
135  * @tc.author:
136  */
137 HWTEST_F(DMAdapterMockTest, GetLocalDeviceUdid002, TestSize.Level0)
138 {
139 #ifdef PB_DEVICE_MANAGER_ENABLE
140     EXPECT_CALL(*deviceManagerMock_, GetLocalDeviceInfo(testing::_, testing::_))
141         .Times(1)
142         .WillRepeatedly(testing::Return(0));
143     EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
144         .Times(1)
__anoncc0af8440702(auto, auto, std::string &udid) 145         .WillRepeatedly([](auto, auto, std::string &udid) {
146             udid = "";
147             return 0;
148         });
149     DMAdapter::GetInstance().localDeviceUdid_ = "";
150     std::string udid = DMAdapter::GetInstance().GetLocalDeviceUdid();
151     ASSERT_TRUE(udid.empty());
152     DMAdapter::GetInstance().localDeviceUdid_ = "";
153 #else
154     ASSERT_TRUE(true);
155 #endif
156 }
157 
158 /**
159  * @tc.name: GetLocalDeviceUdid003
160  * @tc.desc: GetLocalDeviceUdid.
161  * @tc.type: FUNC
162  * @tc.require:
163  * @tc.author:
164  */
165 HWTEST_F(DMAdapterMockTest, GetLocalDeviceUdid003, TestSize.Level0)
166 {
167 #ifdef PB_DEVICE_MANAGER_ENABLE
168     EXPECT_CALL(*deviceManagerMock_, GetLocalDeviceInfo(testing::_, testing::_))
169         .Times(1)
170         .WillRepeatedly(testing::Return(0));
171     EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
172         .Times(1)
__anoncc0af8440802(auto, auto, std::string &udid) 173         .WillRepeatedly([](auto, auto, std::string &udid) {
174             udid = "testUdid";
175             return 0;
176         });
177     DMAdapter::GetInstance().localDeviceUdid_ = "";
178     std::string udid = DMAdapter::GetInstance().GetLocalDeviceUdid();
179     ASSERT_FALSE(udid.empty());
180     DMAdapter::GetInstance().localDeviceUdid_ = "";
181 #else
182     ASSERT_TRUE(true);
183 #endif
184 }
185 
186 /**
187  * @tc.name: GetUdidByNetworkId001
188  * @tc.desc: GetUdidByNetworkId.
189  * @tc.type: FUNC
190  * @tc.require:
191  * @tc.author:
192  */
193 HWTEST_F(DMAdapterMockTest, GetUdidByNetworkId001, TestSize.Level0)
194 {
195 #ifdef PB_DEVICE_MANAGER_ENABLE
196     EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
197         .Times(1)
__anoncc0af8440902(auto, auto, std::string &udid) 198         .WillRepeatedly([](auto, auto, std::string &udid) {
199             udid = "testUdid";
200             return 1;
201         });
202     std::string networkId = "testNetworkId";
203     std::string udid = DMAdapter::GetInstance().GetUdidByNetworkId(networkId);
204     ASSERT_TRUE(udid.empty());
205 #else
206     ASSERT_TRUE(true);
207 #endif
208 }
209 
210 /**
211  * @tc.name: GetUdidByNetworkId002
212  * @tc.desc: GetUdidByNetworkId.
213  * @tc.type: FUNC
214  * @tc.require:
215  * @tc.author:
216  */
217 HWTEST_F(DMAdapterMockTest, GetUdidByNetworkId002, TestSize.Level0)
218 {
219 #ifdef PB_DEVICE_MANAGER_ENABLE
220     EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
221         .Times(1)
__anoncc0af8440a02(auto, auto, std::string &udid) 222         .WillRepeatedly([](auto, auto, std::string &udid) {
223             udid = "";
224             return 1;
225         });
226     std::string networkId = "testNetworkId";
227     std::string udid = DMAdapter::GetInstance().GetUdidByNetworkId(networkId);
228     ASSERT_TRUE(udid.empty());
229 #else
230     ASSERT_TRUE(true);
231 #endif
232 }
233 
234 /**
235  * @tc.name: GetUdidByNetworkId003
236  * @tc.desc: GetUdidByNetworkId.
237  * @tc.type: FUNC
238  * @tc.require:
239  * @tc.author:
240  */
241 HWTEST_F(DMAdapterMockTest, GetUdidByNetworkId003, TestSize.Level0)
242 {
243 #ifdef PB_DEVICE_MANAGER_ENABLE
244     EXPECT_CALL(*deviceManagerMock_, GetUdidByNetworkId(testing::_, testing::_, testing::_))
245         .Times(1)
__anoncc0af8440b02(auto, auto, std::string &udid) 246         .WillRepeatedly([](auto, auto, std::string &udid) {
247             udid = "";
248             return 0;
249         });
250     std::string networkId = "testNetworkId";
251     std::string udid = DMAdapter::GetInstance().GetUdidByNetworkId(networkId);
252     ASSERT_TRUE(udid.empty());
253 #else
254     ASSERT_TRUE(true);
255 #endif
256 }
257 
258 /**
259  * @tc.name: GetLocalDeviceType001
260  * @tc.desc: GetLocalDeviceType.
261  * @tc.type: FUNC
262  * @tc.require:
263  * @tc.author:
264  */
265 HWTEST_F(DMAdapterMockTest, GetLocalDeviceType001, TestSize.Level0)
266 {
267 #ifdef PB_DEVICE_MANAGER_ENABLE
268     EXPECT_CALL(*deviceManagerMock_, GetLocalDeviceType(testing::_, testing::_))
269         .Times(1)
270         .WillRepeatedly(testing::Return(1));
271     int32_t deviceType = DMAdapter::GetInstance().GetLocalDeviceType();
272     ASSERT_EQ(DmDeviceType::DEVICE_TYPE_UNKNOWN, deviceType);
273 #else
274     ASSERT_TRUE(true);
275 #endif
276 }
277 
278 /**
279  * @tc.name: SetDevices001
280  * @tc.desc: SetDevices.
281  * @tc.type: FUNC
282  * @tc.require:
283  * @tc.author:
284  */
285 HWTEST_F(DMAdapterMockTest, SetDevices001, TestSize.Level0)
286 {
287 #ifdef PB_DEVICE_MANAGER_ENABLE
288     EXPECT_CALL(*deviceManagerMock_, GetTrustedDeviceList(testing::_, testing::_, testing::_))
289         .Times(1)
__anoncc0af8440c02(auto, auto, std::vector<DmDeviceInfo> &deviceList) 290         .WillRepeatedly([](auto, auto, std::vector<DmDeviceInfo> &deviceList) {
291             DmDeviceInfo info;
292             std::string networkId = "testNetworkId";
293             std::copy(networkId.begin(), networkId.end(), info.networkId);
294             deviceList.emplace_back(info);
295             return 1;
296         });
297     EXPECT_CALL(*deviceManagerMock_, IsSameAccount(testing::_)).Times(0);
298     DMAdapter::GetInstance().SetDevices();
299     ASSERT_TRUE(true);
300     DMAdapter::GetInstance().devices_.clear();
301 #else
302     ASSERT_TRUE(true);
303 #endif
304 }
305 
306 /**
307  * @tc.name: SetDevices002
308  * @tc.desc: SetDevices.
309  * @tc.type: FUNC
310  * @tc.require:
311  * @tc.author:
312  */
313 HWTEST_F(DMAdapterMockTest, SetDevices002, TestSize.Level0)
314 {
315 #ifdef PB_DEVICE_MANAGER_ENABLE
316     EXPECT_CALL(*deviceManagerMock_, GetTrustedDeviceList(testing::_, testing::_, testing::_))
317         .Times(1)
__anoncc0af8440d02(auto, auto, std::vector<DmDeviceInfo> &deviceList) 318         .WillRepeatedly([](auto, auto, std::vector<DmDeviceInfo> &deviceList) {
319             DmDeviceInfo info;
320             std::string networkId = "testNetworkId";
321             std::copy(networkId.begin(), networkId.end(), info.networkId);
322             deviceList.emplace_back(info);
323             return 0;
324         });
325     EXPECT_CALL(*deviceManagerMock_, IsSameAccount(testing::_)).Times(1).WillRepeatedly(testing::Return(false));
326     DMAdapter::GetInstance().SetDevices();
327     ASSERT_TRUE(true);
328     DMAdapter::GetInstance().devices_.clear();
329 #else
330     ASSERT_TRUE(true);
331 #endif
332 }
333 
334 /**
335  * @tc.name: SetDevices003
336  * @tc.desc: SetDevices.
337  * @tc.type: FUNC
338  * @tc.require:
339  * @tc.author:
340  */
341 HWTEST_F(DMAdapterMockTest, SetDevices003, TestSize.Level0)
342 {
343 #ifdef PB_DEVICE_MANAGER_ENABLE
344     EXPECT_CALL(*deviceManagerMock_, GetTrustedDeviceList(testing::_, testing::_, testing::_))
345         .Times(1)
__anoncc0af8440e02(auto, auto, std::vector<DmDeviceInfo> &deviceList) 346         .WillRepeatedly([](auto, auto, std::vector<DmDeviceInfo> &deviceList) {
347             DmDeviceInfo info;
348             std::string networkId = "testNetworkId";
349             std::copy(networkId.begin(), networkId.end(), info.networkId);
350             deviceList.emplace_back(info);
351             return 0;
352         });
353     EXPECT_CALL(*deviceManagerMock_, IsSameAccount(testing::_)).Times(1).WillRepeatedly(testing::Return(true));
354     DMAdapter::GetInstance().SetDevices();
355     ASSERT_TRUE(true);
356     DMAdapter::GetInstance().devices_.clear();
357 #else
358     ASSERT_TRUE(true);
359 #endif
360 }
361 
362 } // namespace MiscServices
363 } // namespace OHOS