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 <gtest/gtest.h>
17
18 #include "device/dm_adapter.h"
19 #include "device_manager.h"
20 #include "distributed_clip.h"
21 #include "pasteboard_error.h"
22
23 namespace OHOS::MiscServices {
24 using namespace testing::ext;
25 class DMAdapterTest : public testing::Test {
26 public:
27 static void SetUpTestCase(void);
28 static void TearDownTestCase(void);
29 void SetUp();
30 void TearDown();
31 };
32
SetUpTestCase(void)33 void DMAdapterTest::SetUpTestCase(void) { }
34
TearDownTestCase(void)35 void DMAdapterTest::TearDownTestCase(void) { }
36
SetUp(void)37 void DMAdapterTest::SetUp(void) { }
38
TearDown(void)39 void DMAdapterTest::TearDown(void) { }
40
41 /**
42 * @tc.name: GetLocalDeviceUdid001
43 * @tc.desc: Get the local device udid.
44 * @tc.type: FUNC
45 * @tc.require:
46 * @tc.author:
47 */
48 HWTEST_F(DMAdapterTest, GetLocalDeviceUdid001, TestSize.Level0)
49 {
50 std::string bundleName = "com.example.myapplication";
51 bool res = DMAdapter::GetInstance().Initialize(bundleName);
52 ASSERT_FALSE(res);
53 std::string device = "deviceTestName";
54 auto fromDevice = DMAdapter::GetInstance().GetDeviceName(device);
55 ASSERT_FALSE(fromDevice.empty());
56 auto &udid = DMAdapter::GetInstance().GetLocalDeviceUdid();
57 ASSERT_FALSE(udid.empty());
58 }
59
60 /**
61 * @tc.name: GetLocalDeviceUdid002
62 * @tc.desc: Get the local device udid.
63 * @tc.type: FUNC
64 * @tc.require:
65 * @tc.author:
66 */
67 HWTEST_F(DMAdapterTest, GetLocalDeviceUdid002, TestSize.Level0)
68 {
69 std::string bundleName = "com.example.myapplication";
70 bool res = DMAdapter::GetInstance().Initialize(bundleName);
71 std::string device = "deviceTestName";
72 auto fromDevice = DMAdapter::GetInstance().GetDeviceName(device);
73 DmDeviceInfo info;
74 std::string pkgName_ = "pkgName";
75 std::string localDeviceUdid_ = "localDeviceUdid";
76 DeviceManager::GetInstance().GetUdidByNetworkId(pkgName_, info.networkId, localDeviceUdid_);
77 auto &udid = DMAdapter::GetInstance().GetLocalDeviceUdid();
78 ASSERT_FALSE(udid.empty());
79 }
80
81 /**
82 * @tc.name: GetLocalNetworkId
83 * @tc.desc: Get the local network id.
84 * @tc.type: FUNC
85 * @tc.require:
86 * @tc.author:
87 */
88 HWTEST_F(DMAdapterTest, GetLocalNetworkId, TestSize.Level0)
89 {
90 std::string bundleName = "com.example.myapplication";
91 bool res = DMAdapter::GetInstance().Initialize(bundleName);
92 ASSERT_FALSE(res);
93 auto networkId = DMAdapter::GetInstance().GetLocalNetworkId();
94 ASSERT_FALSE(networkId.empty());
95 }
96
97 /**
98 * @tc.name: DistributedClipRegister
99 * @tc.desc: DistributedClip Register and Unregister.
100 * @tc.type: FUNC
101 * @tc.require:
102 * @tc.author:
103 */
104 HWTEST_F(DMAdapterTest, DistributedClipRegister, TestSize.Level0)
105 {
106 DistributedClip *observer = new DistributedClip();
107 DMAdapter::GetInstance().Register(observer);
108 DMAdapter::GetInstance().Unregister(observer);
109 ASSERT_TRUE(true);
110 }
111
112 /**
113 * @tc.name: GetRemoteDeviceInfo
114 * @tc.desc: Get the remote device info.
115 * @tc.type: FUNC
116 * @tc.require:
117 * @tc.author:
118 */
119 HWTEST_F(DMAdapterTest, GetRemoteDeviceInfo, TestSize.Level0)
120 {
121 #ifdef PB_DEVICE_MANAGER_ENABLE
122 DmDeviceInfo remoteDevice;
123 auto ret = DMAdapter::GetInstance().GetRemoteDeviceInfo("", remoteDevice);
124 ASSERT_TRUE(ret == static_cast<int32_t>(PasteboardError::NO_TRUST_DEVICE_ERROR));
125 #else
126 ASSERT_TRUE(true);
127 #endif
128 }
129
130 /**
131 * @tc.name: GetUdidByNetworkId
132 * @tc.desc: Get Udid By NetworkId.
133 * @tc.type: FUNC
134 * @tc.require:
135 * @tc.author:
136 */
137 HWTEST_F(DMAdapterTest, GetUdidByNetworkId, TestSize.Level0)
138 {
139 auto udid = DMAdapter::GetInstance().GetUdidByNetworkId("");
140 ASSERT_TRUE(udid.empty());
141 }
142
143 /**
144 * @tc.name: IsSameAccount
145 * @tc.desc: is same account.
146 * @tc.type: FUNC
147 * @tc.require:
148 * @tc.author:
149 */
150 HWTEST_F(DMAdapterTest, IsSameAccount, TestSize.Level0)
151 {
152 std::string networkId = DMAdapter::GetInstance().GetLocalNetworkId();
153 bool ret = DMAdapter::GetInstance().IsSameAccount(networkId);
154 ASSERT_FALSE(ret);
155 }
156
157 /**
158 * @tc.name: GetDevices
159 * @tc.desc: Get Devices.
160 * @tc.type: FUNC
161 * @tc.require:
162 * @tc.author:
163 */
164 HWTEST_F(DMAdapterTest, GetDevices, TestSize.Level0)
165 {
166 DMAdapter::GetInstance().SetDevices();
167 std::vector<DmDeviceInfo> devices = DMAdapter::GetInstance().GetDevices();
168 ASSERT_TRUE(devices.empty());
169 }
170
171 /**
172 * @tc.name: GetDeviceName001
173 * @tc.desc: Get Local Device Type
174 * @tc.type: FUNC
175 * @tc.require:
176 * @tc.author:
177 */
178 HWTEST_F(DMAdapterTest, GetDeviceName001, TestSize.Level0)
179 {
180 std::string networkId = "invalidnetworkId";
181 std::string expectedDeviceName = "unknown";
182 (void)DMAdapter::GetInstance().GetLocalDeviceType();
183 std::string actualDeviceName = DMAdapter::GetInstance().GetDeviceName(networkId);
184 EXPECT_EQ(expectedDeviceName, actualDeviceName);
185 }
186
187 /**
188 * @tc.name: GetDeviceName002
189 * @tc.desc: Get Local Device Type
190 * @tc.type: FUNC
191 * @tc.require:
192 * @tc.author:
193 */
194 HWTEST_F(DMAdapterTest, GetDeviceName002, TestSize.Level0)
195 {
196 std::string networkId = DMAdapter::GetInstance().GetLocalNetworkId();
197 std::string expectedDeviceName = "unknown";
198 std::string actualDeviceName = DMAdapter::GetInstance().GetDeviceName(networkId);
199 EXPECT_EQ(expectedDeviceName, actualDeviceName);
200 }
201
202 /**
203 * @tc.name: DeInitialize
204 * @tc.desc: De Initi alize
205 * @tc.type: FUNC
206 * @tc.require:
207 * @tc.author:
208 */
209 HWTEST_F(DMAdapterTest, DeInitialize, TestSize.Level0)
210 {
211 DMAdapter::GetInstance().DeInitialize();
212 ASSERT_TRUE(true);
213 }
214 } // namespace OHOS::MiscServices
215