1 /*
2 * Copyright (c) 2022-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 "dinput_context_test.h"
17
18 #include "dinput_context.h"
19 #include "dinput_errcode.h"
20 #include "dinput_utils_tool.h"
21 #include "dinput_softbus_define.h"
22
23 using namespace testing::ext;
24 using namespace OHOS::DistributedHardware::DistributedInput;
25 using namespace std;
26
27 namespace OHOS {
28 namespace DistributedHardware {
29 namespace DistributedInput {
30 constexpr uint32_t SIZE_AFTER_GET = 1;
31 constexpr uint32_t HEIGHT = 1080;
32
SetUp()33 void DInputContextTest::SetUp()
34 {
35 }
36
TearDown()37 void DInputContextTest::TearDown()
38 {
39 }
40
SetUpTestCase()41 void DInputContextTest::SetUpTestCase()
42 {
43 }
44
TearDownTestCase()45 void DInputContextTest::TearDownTestCase()
46 {
47 }
48
49 HWTEST_F(DInputContextTest, GetSourceWindId001, testing::ext::TestSize.Level1)
50 {
51 std::string devId = "hello";
52 uint64_t sourceWinId = 1;
53 std::string ret = DInputContext::GetInstance().GetScreenInfoKey(devId, sourceWinId);
54 EXPECT_EQ("hello###1", ret);
55 }
56
57 HWTEST_F(DInputContextTest, RemoveSinkScreenInfo001, testing::ext::TestSize.Level1)
58 {
59 std::string sourceWinId = "hello";
60 int32_t ret = DInputContext::GetInstance().RemoveSinkScreenInfo(sourceWinId);
61 EXPECT_EQ(DH_SUCCESS, ret);
62 }
63
64 HWTEST_F(DInputContextTest, UpdateSinkScreenInfo001, testing::ext::TestSize.Level1)
65 {
66 std::string sourceWinId = "hello";
67 SinkScreenInfo sinkScreenInfo;
68 int32_t ret = DInputContext::GetInstance().UpdateSinkScreenInfo(sourceWinId, sinkScreenInfo);
69 EXPECT_EQ(ERR_DH_INPUT_CONTEXT_KEY_NOT_EXIST, ret);
70 }
71
72 HWTEST_F(DInputContextTest, UpdateSinkScreenInfo002, testing::ext::TestSize.Level1)
73 {
74 std::string sourceWinId = "hello";
75 SinkScreenInfo sinkScreenInfo = DInputContext::GetInstance().GetSinkScreenInfo(sourceWinId);
76 int32_t ret = DInputContext::GetInstance().UpdateSinkScreenInfo(sourceWinId, sinkScreenInfo);
77 EXPECT_EQ(DH_SUCCESS, ret);
78 DInputContext::GetInstance().RemoveSinkScreenInfo(sourceWinId);
79 }
80
81 HWTEST_F(DInputContextTest, GetSinkScreenInfo001, testing::ext::TestSize.Level1)
82 {
83 std::string sourceWinId = "hello";
84 SinkScreenInfo sinkScreenInfo = DInputContext::GetInstance().GetSinkScreenInfo(sourceWinId);
85 EXPECT_EQ(SIZE_AFTER_GET, DInputContext::GetInstance().sinkScreenInfoMap_.size());
86 DInputContext::GetInstance().RemoveSinkScreenInfo(sourceWinId);
87 }
88
89 HWTEST_F(DInputContextTest, CleanExceptionalInfo001, testing::ext::TestSize.Level1)
90 {
91 SrcScreenInfo srcScreenInfo;
92 srcScreenInfo.uuid = "uuid1";
93 srcScreenInfo.sessionId = 1;
94 EXPECT_TRUE(DInputContext::GetInstance().sinkScreenInfoMap_.empty());
95 DInputContext::GetInstance().CleanExceptionalInfo(srcScreenInfo);
96 }
97
98 HWTEST_F(DInputContextTest, CleanExceptionalInfo002, testing::ext::TestSize.Level1)
99 {
100 std::string sourceWinId = "hello";
101 SrcScreenInfo srcScreenInfo;
102 srcScreenInfo.uuid = "uuid1";
103 srcScreenInfo.sessionId = 1;
104
105 SinkScreenInfo sinkInfo;
106 sinkInfo.srcScreenInfo.uuid = "uuid1";
107 sinkInfo.srcScreenInfo.sessionId = 1;
108
109 DInputContext::GetInstance().sinkScreenInfoMap_[sourceWinId] = sinkInfo;
110 EXPECT_EQ(DInputContext::GetInstance().sinkScreenInfoMap_.size(), 1);
111 DInputContext::GetInstance().CleanExceptionalInfo(srcScreenInfo);
112
113 DInputContext::GetInstance().RemoveSinkScreenInfo(sourceWinId);
114 }
115
116 HWTEST_F(DInputContextTest, CleanExceptionalInfo003, testing::ext::TestSize.Level1)
117 {
118 std::string sourceWinId = "hello";
119 SrcScreenInfo srcScreenInfo;
120 srcScreenInfo.uuid = "uuid1";
121 srcScreenInfo.sessionId = 1;
122
123 SinkScreenInfo sinkInfo;
124 sinkInfo.srcScreenInfo.uuid = "uuid2";
125 sinkInfo.srcScreenInfo.sessionId = 1;
126
127 DInputContext::GetInstance().sinkScreenInfoMap_[sourceWinId] = sinkInfo;
128 EXPECT_EQ(DInputContext::GetInstance().sinkScreenInfoMap_.size(), 1);
129 DInputContext::GetInstance().CleanExceptionalInfo(srcScreenInfo);
130
131 DInputContext::GetInstance().RemoveSinkScreenInfo(sourceWinId);
132 }
133
134 HWTEST_F(DInputContextTest, CleanExceptionalInfo004, testing::ext::TestSize.Level1)
135 {
136 std::string sourceWinId = "hello";
137 SrcScreenInfo srcScreenInfo;
138 srcScreenInfo.uuid = "uuid1";
139 srcScreenInfo.sessionId = 1;
140
141 SinkScreenInfo sinkInfo;
142 sinkInfo.srcScreenInfo.uuid = "uuid1";
143 sinkInfo.srcScreenInfo.sessionId = 2;
144
145 DInputContext::GetInstance().sinkScreenInfoMap_[sourceWinId] = sinkInfo;
146 EXPECT_EQ(DInputContext::GetInstance().sinkScreenInfoMap_.size(), 1);
147 DInputContext::GetInstance().CleanExceptionalInfo(srcScreenInfo);
148
149 DInputContext::GetInstance().RemoveSinkScreenInfo(sourceWinId);
150 }
151
152 HWTEST_F(DInputContextTest, GetSinkScreenInfo002, testing::ext::TestSize.Level1)
153 {
154 std::string sourceWinId = "hello";
155 SinkScreenInfo sinkScreenInfo1;
156 sinkScreenInfo1.sinkPhyHeight = HEIGHT;
157 DInputContext::GetInstance().sinkScreenInfoMap_[sourceWinId] = sinkScreenInfo1;
158 SinkScreenInfo sinkScreenInfo = DInputContext::GetInstance().GetSinkScreenInfo(sourceWinId);
159 EXPECT_EQ(HEIGHT, sinkScreenInfo.sinkPhyHeight);
160 DInputContext::GetInstance().RemoveSinkScreenInfo(sourceWinId);
161 }
162
163 HWTEST_F(DInputContextTest, RemoveSrcScreenInfo001, testing::ext::TestSize.Level1)
164 {
165 std::string sourceWinId = "hello";
166 int32_t ret = DInputContext::GetInstance().RemoveSrcScreenInfo(sourceWinId);
167 EXPECT_EQ(DH_SUCCESS, ret);
168 }
169
170 HWTEST_F(DInputContextTest, UpdateSrcScreenInfo001, testing::ext::TestSize.Level1)
171 {
172 std::string sourceWinId = "hello";
173 SrcScreenInfo srcScreenInfo;
174 int32_t ret = DInputContext::GetInstance().UpdateSrcScreenInfo(sourceWinId, srcScreenInfo);
175 EXPECT_EQ(ERR_DH_INPUT_CONTEXT_KEY_NOT_EXIST, ret);
176 }
177
178 HWTEST_F(DInputContextTest, UpdateSrcScreenInfo002, testing::ext::TestSize.Level1)
179 {
180 std::string sourceWinId = "hello";
181 SrcScreenInfo srcScreenInfo = DInputContext::GetInstance().GetSrcScreenInfo(sourceWinId);
182 int32_t ret = DInputContext::GetInstance().UpdateSrcScreenInfo(sourceWinId, srcScreenInfo);
183 EXPECT_EQ(DH_SUCCESS, ret);
184 DInputContext::GetInstance().RemoveSrcScreenInfo(sourceWinId);
185 }
186
187 HWTEST_F(DInputContextTest, GetSrcScreenInfo001, testing::ext::TestSize.Level1)
188 {
189 std::string sourceWinId = "hello";
190 SrcScreenInfo srcScreenInfo = DInputContext::GetInstance().GetSrcScreenInfo(sourceWinId);
191 EXPECT_EQ(SIZE_AFTER_GET, DInputContext::GetInstance().srcScreenInfoMap_.size());
192 DInputContext::GetInstance().RemoveSrcScreenInfo(sourceWinId);
193 }
194
195 HWTEST_F(DInputContextTest, GetSrcScreenInfo002, testing::ext::TestSize.Level1)
196 {
197 std::string sourceWinId = "hello";
198 SrcScreenInfo srcScreenInfo1;
199 srcScreenInfo1.sourcePhyHeight = HEIGHT;
200 DInputContext::GetInstance().srcScreenInfoMap_[sourceWinId] = srcScreenInfo1;
201 SrcScreenInfo srcScreenInfo = DInputContext::GetInstance().GetSrcScreenInfo(sourceWinId);
202 EXPECT_EQ(HEIGHT, srcScreenInfo.sourcePhyHeight);
203 DInputContext::GetInstance().RemoveSrcScreenInfo(sourceWinId);
204 }
205
206 HWTEST_F(DInputContextTest, SetGetLocalTouchScreenInfo001, testing::ext::TestSize.Level1)
207 {
208 LocalTouchScreenInfo localTouchScreenInfo;
209 localTouchScreenInfo.sinkShowWidth = HEIGHT;
210 DInputContext::GetInstance().SetLocalTouchScreenInfo(localTouchScreenInfo);
211 EXPECT_EQ(HEIGHT, DInputContext::GetInstance().GetLocalTouchScreenInfo().sinkShowWidth);
212 }
213
214 HWTEST_F(DInputContextTest, CalculateTransformInfo001, testing::ext::TestSize.Level1)
215 {
216 SinkScreenInfo sinkScreenInfo;
217 int32_t ret = DInputContext::GetInstance().CalculateTransformInfo(sinkScreenInfo);
218 EXPECT_EQ(ERR_DH_INPUT_CONTEXT_CALCULATE_FAIL, ret);
219 }
220
221 HWTEST_F(DInputContextTest, CalculateTransformInfo002, testing::ext::TestSize.Level1)
222 {
223 SinkScreenInfo sinkScreenInfo;
224 sinkScreenInfo.sinkPhyHeight = 1080;
225 sinkScreenInfo.sinkPhyWidth = 960;
226 sinkScreenInfo.sinkShowHeight = 1080;
227 sinkScreenInfo.sinkShowWidth = 960;
228 int32_t ret = DInputContext::GetInstance().CalculateTransformInfo(sinkScreenInfo);
229 EXPECT_EQ(ERR_DH_INPUT_CONTEXT_CALCULATE_FAIL, ret);
230 }
231
232 HWTEST_F(DInputContextTest, CalculateTransformInfo003, testing::ext::TestSize.Level1)
233 {
234 SinkScreenInfo sinkScreenInfo;
235 sinkScreenInfo.sinkPhyHeight = 1080;
236 sinkScreenInfo.sinkPhyWidth = 960;
237 sinkScreenInfo.sinkShowHeight = 1080;
238 sinkScreenInfo.sinkShowWidth = 960;
239 sinkScreenInfo.sinkProjShowHeight = 640;
240 sinkScreenInfo.sinkProjShowWidth = 480;
241 int32_t ret = DInputContext::GetInstance().CalculateTransformInfo(sinkScreenInfo);
242 EXPECT_EQ(DH_SUCCESS, ret);
243 }
244
245 HWTEST_F(DInputContextTest, GetLocalDeviceInfo_001, testing::ext::TestSize.Level1)
246 {
247 DevInfo devInfo = GetLocalDeviceInfo();
248 EXPECT_NE(0, devInfo.networkId.size());
249 }
250
251 HWTEST_F(DInputContextTest, GetLocalNetworkId_001, testing::ext::TestSize.Level1)
252 {
253 std::string ret = GetLocalNetworkId();
254 EXPECT_NE(0, ret.size());
255 }
256
257 HWTEST_F(DInputContextTest, GetUUIDBySoftBus_001, testing::ext::TestSize.Level1)
258 {
259 std::string networkId = "";
260 std::string ret = GetUUIDBySoftBus(networkId);
261 EXPECT_EQ(0, ret.size());
262
263 networkId = "networkidc08647073e02e7a78f09473aa122ff57fc81c00";
264 ret = GetUUIDBySoftBus(networkId);
265 EXPECT_EQ(0, ret.size());
266 }
267
268 HWTEST_F(DInputContextTest, GetCurrentTime_001, testing::ext::TestSize.Level1)
269 {
270 uint64_t ret = GetCurrentTimeUs();
271 EXPECT_NE(0, ret);
272 }
273
274 HWTEST_F(DInputContextTest, SetAnonyId_001, testing::ext::TestSize.Level1)
275 {
276 std::string message = "";
277 std::string ret = SetAnonyId(message);
278 EXPECT_EQ(0, ret.size());
279 }
280
281 HWTEST_F(DInputContextTest, SetAnonyId_002, testing::ext::TestSize.Level1)
282 {
283 nlohmann::json jsonObj;
284 std::string deviceId = "deviceId_test";
285 std::string inputData = "inputData_data";
286 std::string vecDhId = "dhId_123.dhId_456.dhId_789";
287 std::string srcDevId = "srcDevId_test";
288 std::string sinkDevId = "sinkDevId_test";
289 jsonObj[DINPUT_SOFTBUS_KEY_DEVICE_ID] = deviceId;
290 jsonObj[DINPUT_SOFTBUS_KEY_INPUT_DATA] = inputData;
291 jsonObj[DINPUT_SOFTBUS_KEY_VECTOR_DHID] = vecDhId;
292 jsonObj[DINPUT_SOFTBUS_KEY_SRC_DEV_ID] = srcDevId;
293 jsonObj[DINPUT_SOFTBUS_KEY_SINK_DEV_ID] = sinkDevId;
294 std::string ret = SetAnonyId(jsonObj.dump());
295 EXPECT_NE(0, ret.size());
296 }
297
298 HWTEST_F(DInputContextTest, GetNodeDesc_001, testing::ext::TestSize.Level1)
299 {
300 std::string parameters = "";
301 std::string ret = GetNodeDesc(parameters);
302 EXPECT_EQ(0, ret.size());
303 }
304
305 HWTEST_F(DInputContextTest, JointDhIds_001, testing::ext::TestSize.Level1)
306 {
307 std::vector<std::string> dhids;
308 std::string ret = JointDhIds(dhids);
309 EXPECT_EQ("", ret);
310 }
311
312 } // namespace DistributedInput
313 } // namespace DistributedHardware
314 } // namespace OHOS
315