1 /*
2 * Copyright (c) 2022-2024 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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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, GetSinkScreenInfo002, testing::ext::TestSize.Level0)
90 {
91 std::string sourceWinId = "hello";
92 SinkScreenInfo sinkScreenInfo1;
93 sinkScreenInfo1.sinkPhyHeight = HEIGHT;
94 DInputContext::GetInstance().sinkScreenInfoMap_[sourceWinId] = sinkScreenInfo1;
95 SinkScreenInfo sinkScreenInfo = DInputContext::GetInstance().GetSinkScreenInfo(sourceWinId);
96 EXPECT_EQ(HEIGHT, sinkScreenInfo.sinkPhyHeight);
97 DInputContext::GetInstance().RemoveSinkScreenInfo(sourceWinId);
98 }
99
100 HWTEST_F(DInputContextTest, RemoveSrcScreenInfo001, testing::ext::TestSize.Level0)
101 {
102 std::string sourceWinId = "hello";
103 int32_t ret = DInputContext::GetInstance().RemoveSrcScreenInfo(sourceWinId);
104 EXPECT_EQ(DH_SUCCESS, ret);
105 }
106
107 HWTEST_F(DInputContextTest, UpdateSrcScreenInfo001, testing::ext::TestSize.Level0)
108 {
109 std::string sourceWinId = "hello";
110 SrcScreenInfo srcScreenInfo;
111 int32_t ret = DInputContext::GetInstance().UpdateSrcScreenInfo(sourceWinId, srcScreenInfo);
112 EXPECT_EQ(ERR_DH_INPUT_CONTEXT_KEY_NOT_EXIST, ret);
113 }
114
115 HWTEST_F(DInputContextTest, UpdateSrcScreenInfo002, testing::ext::TestSize.Level0)
116 {
117 std::string sourceWinId = "hello";
118 SrcScreenInfo srcScreenInfo = DInputContext::GetInstance().GetSrcScreenInfo(sourceWinId);
119 int32_t ret = DInputContext::GetInstance().UpdateSrcScreenInfo(sourceWinId, srcScreenInfo);
120 EXPECT_EQ(DH_SUCCESS, ret);
121 DInputContext::GetInstance().RemoveSrcScreenInfo(sourceWinId);
122 }
123
124 HWTEST_F(DInputContextTest, GetSrcScreenInfo001, testing::ext::TestSize.Level0)
125 {
126 std::string sourceWinId = "hello";
127 SrcScreenInfo srcScreenInfo = DInputContext::GetInstance().GetSrcScreenInfo(sourceWinId);
128 EXPECT_EQ(SIZE_AFTER_GET, DInputContext::GetInstance().srcScreenInfoMap_.size());
129 DInputContext::GetInstance().RemoveSrcScreenInfo(sourceWinId);
130 }
131
132 HWTEST_F(DInputContextTest, GetSrcScreenInfo002, testing::ext::TestSize.Level0)
133 {
134 std::string sourceWinId = "hello";
135 SrcScreenInfo srcScreenInfo1;
136 srcScreenInfo1.sourcePhyHeight = HEIGHT;
137 DInputContext::GetInstance().srcScreenInfoMap_[sourceWinId] = srcScreenInfo1;
138 SrcScreenInfo srcScreenInfo = DInputContext::GetInstance().GetSrcScreenInfo(sourceWinId);
139 EXPECT_EQ(HEIGHT, srcScreenInfo.sourcePhyHeight);
140 DInputContext::GetInstance().RemoveSrcScreenInfo(sourceWinId);
141 }
142
143 HWTEST_F(DInputContextTest, SetGetLocalTouchScreenInfo001, testing::ext::TestSize.Level0)
144 {
145 LocalTouchScreenInfo localTouchScreenInfo;
146 localTouchScreenInfo.sinkShowWidth = HEIGHT;
147 DInputContext::GetInstance().SetLocalTouchScreenInfo(localTouchScreenInfo);
148 EXPECT_EQ(HEIGHT, DInputContext::GetInstance().GetLocalTouchScreenInfo().sinkShowWidth);
149 }
150
151 HWTEST_F(DInputContextTest, CalculateTransformInfo001, testing::ext::TestSize.Level0)
152 {
153 SinkScreenInfo sinkScreenInfo;
154 int32_t ret = DInputContext::GetInstance().CalculateTransformInfo(sinkScreenInfo);
155 EXPECT_EQ(ERR_DH_INPUT_CONTEXT_CALCULATE_FAIL, ret);
156 }
157
158 HWTEST_F(DInputContextTest, CalculateTransformInfo002, testing::ext::TestSize.Level0)
159 {
160 SinkScreenInfo sinkScreenInfo;
161 sinkScreenInfo.sinkPhyHeight = 1080;
162 sinkScreenInfo.sinkPhyWidth = 960;
163 sinkScreenInfo.sinkShowHeight = 1080;
164 sinkScreenInfo.sinkShowWidth = 960;
165 int32_t ret = DInputContext::GetInstance().CalculateTransformInfo(sinkScreenInfo);
166 EXPECT_EQ(ERR_DH_INPUT_CONTEXT_CALCULATE_FAIL, ret);
167 }
168
169 HWTEST_F(DInputContextTest, CalculateTransformInfo003, testing::ext::TestSize.Level0)
170 {
171 SinkScreenInfo sinkScreenInfo;
172 sinkScreenInfo.sinkPhyHeight = 1080;
173 sinkScreenInfo.sinkPhyWidth = 960;
174 sinkScreenInfo.sinkShowHeight = 1080;
175 sinkScreenInfo.sinkShowWidth = 960;
176 sinkScreenInfo.sinkProjShowHeight = 640;
177 sinkScreenInfo.sinkProjShowWidth = 480;
178 int32_t ret = DInputContext::GetInstance().CalculateTransformInfo(sinkScreenInfo);
179 EXPECT_EQ(DH_SUCCESS, ret);
180 }
181
182 HWTEST_F(DInputContextTest, GetLocalDeviceInfo_001, testing::ext::TestSize.Level1)
183 {
184 DevInfo devInfo = GetLocalDeviceInfo();
185 EXPECT_NE(0, devInfo.networkId.size());
186 }
187
188 HWTEST_F(DInputContextTest, GetLocalNetworkId_001, testing::ext::TestSize.Level1)
189 {
190 std::string ret = GetLocalNetworkId();
191 EXPECT_NE(0, ret.size());
192 }
193
194 HWTEST_F(DInputContextTest, GetUUIDBySoftBus_001, testing::ext::TestSize.Level1)
195 {
196 std::string networkId = "";
197 std::string ret = GetUUIDBySoftBus(networkId);
198 EXPECT_EQ(0, ret.size());
199
200 networkId = "networkidc08647073e02e7a78f09473aa122ff57fc81c00";
201 ret = GetUUIDBySoftBus(networkId);
202 EXPECT_EQ(0, ret.size());
203 }
204
205 HWTEST_F(DInputContextTest, GetCurrentTime_001, testing::ext::TestSize.Level1)
206 {
207 uint64_t ret = GetCurrentTimeUs();
208 EXPECT_NE(0, ret);
209 }
210
211 HWTEST_F(DInputContextTest, SetAnonyId_001, testing::ext::TestSize.Level1)
212 {
213 std::string message = "";
214 std::string ret = SetAnonyId(message);
215 EXPECT_EQ(0, ret.size());
216 }
217
218 HWTEST_F(DInputContextTest, SetAnonyId_002, testing::ext::TestSize.Level1)
219 {
220 nlohmann::json jsonObj;
221 std::string deviceId = "deviceId_test";
222 std::string inputData = "inputData_data";
223 std::string vecDhId = "dhId_123.dhId_456.dhId_789";
224 std::string srcDevId = "srcDevId_test";
225 std::string sinkDevId = "sinkDevId_test";
226 jsonObj[DINPUT_SOFTBUS_KEY_DEVICE_ID] = deviceId;
227 jsonObj[DINPUT_SOFTBUS_KEY_INPUT_DATA] = inputData;
228 jsonObj[DINPUT_SOFTBUS_KEY_VECTOR_DHID] = vecDhId;
229 jsonObj[DINPUT_SOFTBUS_KEY_SRC_DEV_ID] = srcDevId;
230 jsonObj[DINPUT_SOFTBUS_KEY_SINK_DEV_ID] = sinkDevId;
231 std::string ret = SetAnonyId(jsonObj.dump());
232 EXPECT_NE(0, ret.size());
233 }
234
235 HWTEST_F(DInputContextTest, GetNodeDesc_001, testing::ext::TestSize.Level1)
236 {
237 std::string parameters = "";
238 std::string ret = GetNodeDesc(parameters);
239 EXPECT_EQ(0, ret.size());
240 }
241
242 HWTEST_F(DInputContextTest, JointDhIds_001, testing::ext::TestSize.Level1)
243 {
244 std::vector<std::string> dhids;
245 std::string ret = JointDhIds(dhids);
246 EXPECT_EQ("", ret);
247 }
248
249 } // namespace DistributedInput
250 } // namespace DistributedHardware
251 } // namespace OHOS
252