1 /*
2 * Copyright (c) 2022 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 <fstream>
19
20 #include "input_display_bind_helper.h"
21 #include "mmi_log.h"
22
23 namespace OHOS {
24 namespace MMI {
25 namespace {
26 using namespace testing::ext;
27 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MMI_LOG_DOMAIN, "InputDisplayBindHelperTest" };
28 } // namespace
29 class InputDisplayBindHelperTest : public testing::Test {
30 public:
SetUpTestCase(void)31 static void SetUpTestCase(void) {}
TearDownTestCase(void)32 static void TearDownTestCase(void) {}
33 static bool WriteConfigFile(const std::string &content);
34 static inline const std::string bindCfgFile_ = "input_display_bind_helper.cfg";
GetCfgFileName()35 static std::string GetCfgFileName()
36 {
37 return bindCfgFile_;
38 }
39 };
40
WriteConfigFile(const std::string & content)41 bool InputDisplayBindHelperTest::WriteConfigFile(const std::string &content)
42 {
43 const std::string &fileName = InputDisplayBindHelperTest::bindCfgFile_;
44 std::ofstream ofs(fileName.c_str());
45 if (!ofs) {
46 MMI_HILOGE("Open file fail.%s\n", fileName.c_str());
47 return false;
48 }
49 ofs << content;
50 ofs.close();
51 return true;
52 }
53
54 /**
55 * @tc.name: InputDisplayBindHelperTest_001
56 * @tc.desc: No bind info in disk
57 * @tc.type: FUNC
58 * @tc.require:
59 */
60 HWTEST_F(InputDisplayBindHelperTest, InputDisplayBindHelperTest_001, TestSize.Level1)
61 {
62 InputDisplayBindHelperTest::WriteConfigFile("");
63 InputDisplayBindHelper bindInfo(InputDisplayBindHelperTest::GetCfgFileName());
64 // 多模初始化
65 bindInfo.Load();
66 // 检测到触摸板设备
67 bindInfo.AddInputDevice(1, "mouse");
68 bindInfo.AddInputDevice(2, "keyboard");
69 // 窗口同步信息
70 bindInfo.AddDisplay(0, "hp 223");
71 bindInfo.AddDisplay(2, "think 123");
72 ASSERT_EQ(bindInfo.Dumps(), std::string("mouse<=>hp 223\nkeyboard<=>think 123\n"));
73 }
74
75 /**
76 * @tc.name: InputDisplayBindHelperTest_002
77 * @tc.desc: Has info with adding order in disk
78 * @tc.type: FUNC
79 * @tc.require:
80 */
81 HWTEST_F(InputDisplayBindHelperTest, InputDisplayBindHelperTest_002, TestSize.Level1)
82 {
83 InputDisplayBindHelperTest::WriteConfigFile("mouse<=>hp 223\nkeyboard<=>think 123\n");
84 InputDisplayBindHelper bindInfo(InputDisplayBindHelperTest::GetCfgFileName());
85 // 多模初始化
86 bindInfo.Load();
87 // 检测到触摸板设备
88 bindInfo.AddInputDevice(1, "mouse");
89 bindInfo.AddInputDevice(2, "keyboard");
90 // 窗口同步信息
91 bindInfo.AddDisplay(0, "hp 223");
92 bindInfo.AddDisplay(2, "think 123");
93 ASSERT_EQ(bindInfo.Dumps(), std::string("mouse<=>hp 223\nkeyboard<=>think 123\n"));
94 }
95
96 /**
97 * @tc.name: InputDisplayBindHelperTest_003
98 * @tc.desc: Has info without adding order in disk
99 * @tc.type: FUNC
100 * @tc.require:
101 */
102 HWTEST_F(InputDisplayBindHelperTest, InputDisplayBindHelperTest_003, TestSize.Level1)
103 {
104 InputDisplayBindHelperTest::WriteConfigFile("mouse<=>think 123\nkeyboard<=>hp 223\n");
105 InputDisplayBindHelper bindInfo(InputDisplayBindHelperTest::GetCfgFileName());
106 // 多模初始化
107 bindInfo.Load();
108 // 检测到触摸板设备
109 bindInfo.AddInputDevice(1, "mouse");
110 bindInfo.AddInputDevice(2, "keyboard");
111 // 窗口同步信息
112 bindInfo.AddDisplay(0, "think 123");
113 bindInfo.AddDisplay(2, "hp 223");
114 ASSERT_EQ(bindInfo.Dumps(), std::string("mouse<=>think 123\nkeyboard<=>hp 223\n"));
115 }
116
117 /**
118 * @tc.name: InputDisplayBindHelperTest_004
119 * @tc.desc: Bind and remove test
120 * @tc.type: FUNC
121 * @tc.require:
122 */
123 HWTEST_F(InputDisplayBindHelperTest, InputDisplayBindHelperTest_004, TestSize.Level1)
124 {
125 InputDisplayBindHelperTest::WriteConfigFile("");
126 InputDisplayBindHelper bindInfo(InputDisplayBindHelperTest::GetCfgFileName());
127 // 多模初始化
128 bindInfo.Load();
129 // 检测到触摸板设备
130 bindInfo.AddInputDevice(1, "mouse");
131 bindInfo.AddInputDevice(2, "keyboard");
132 // 窗口同步信息
133 bindInfo.AddDisplay(0, "hp 223");
134 bindInfo.AddDisplay(2, "think 123");
135 // 显示屏移除
136 bindInfo.RemoveDisplay(2);
137 bindInfo.RemoveDisplay(0);
138 // 输入设备移除
139 bindInfo.RemoveInputDevice(1);
140 bindInfo.RemoveInputDevice(2);
141 bindInfo.RemoveInputDevice(3);
142 // 窗口同步信息
143 bindInfo.AddDisplay(0, "hp 223");
144 bindInfo.AddDisplay(2, "think 123");
145 // 检测到触摸板设备
146 bindInfo.AddInputDevice(1, "mouse");
147 bindInfo.AddInputDevice(2, "keyboard");
148 bindInfo.AddInputDevice(3, "keyboard88");
149
150 bindInfo.Store();
151 bindInfo.Load();
152 bindInfo.Dumps();
153 // 输入设备移除
154 bindInfo.RemoveInputDevice(1);
155 bindInfo.RemoveInputDevice(2);
156 // 触摸板设备移除
157 bindInfo.RemoveDisplay(2);
158 bindInfo.RemoveDisplay(0);
159 ASSERT_EQ(bindInfo.Dumps(), std::string(""));
160 }
161
162 /**
163 * @tc.name: InputDisplayBindHelperTest_005
164 * @tc.desc: Test GetBindDisplayNameByInputDevice
165 * @tc.type: FUNC
166 * @tc.require:
167 */
168 HWTEST_F(InputDisplayBindHelperTest, InputDisplayBindHelperTest_GetBindDisplayNameByInputDevice_005, TestSize.Level1)
169 {
170 InputDisplayBindHelperTest::WriteConfigFile("mouse<=>think 123\nkeyboard<=>hp 223\n");
171 InputDisplayBindHelper bindInfo(InputDisplayBindHelperTest::GetCfgFileName());
172 // 多模初始化
173 bindInfo.Load();
174 // 检测到触摸板设备
175 bindInfo.AddInputDevice(1, "mouse");
176 bindInfo.AddInputDevice(2, "keyboard");
177 // 窗口同步信息
178 bindInfo.AddDisplay(0, "think 123");
179 bindInfo.AddDisplay(2, "hp 223");
180 ASSERT_EQ(bindInfo.Dumps(), std::string("mouse<=>think 123\nkeyboard<=>hp 223\n"));
181 // 获取
182 ASSERT_EQ(bindInfo.GetBindDisplayNameByInputDevice(1), std::string("think 123"));
183 ASSERT_EQ(bindInfo.GetBindDisplayNameByInputDevice(2), std::string("hp 223"));
184 ASSERT_EQ(bindInfo.GetBindDisplayNameByInputDevice(3), std::string());
185 // 删除display
186 bindInfo.RemoveDisplay(0);
187 ASSERT_EQ(bindInfo.GetBindDisplayNameByInputDevice(1), std::string());
188 ASSERT_EQ(bindInfo.GetBindDisplayNameByInputDevice(2), std::string("hp 223"));
189 bindInfo.RemoveDisplay(2);
190 ASSERT_EQ(bindInfo.GetBindDisplayNameByInputDevice(1), std::string());
191 ASSERT_EQ(bindInfo.GetBindDisplayNameByInputDevice(2), std::string());
192 }
193
194 /**
195 * @tc.name: InputDisplayBindHelperTest_IsDisplayAdd_006
196 * @tc.desc: Test GetBindDisplayNameByInputDevice
197 * @tc.type: FUNC
198 * @tc.require:
199 */
200 HWTEST_F(InputDisplayBindHelperTest, InputDisplayBindHelperTest_IsDisplayAdd_006, TestSize.Level1)
201 {
202 InputDisplayBindHelperTest::WriteConfigFile("mouse<=>think 123\nkeyboard<=>hp 223\n");
203 InputDisplayBindHelper bindInfo(InputDisplayBindHelperTest::GetCfgFileName());
204 // 多模初始化
205 bindInfo.Load();
206 ASSERT_FALSE(bindInfo.IsDisplayAdd(0, "hp 223"));
207 ASSERT_FALSE(bindInfo.IsDisplayAdd(2, "think 123"));
208 ASSERT_FALSE(bindInfo.IsDisplayAdd(1, "think 123"));
209 ASSERT_EQ(bindInfo.Dumps(), std::string());
210 // 检测到触摸板设备
211 bindInfo.AddInputDevice(1, "mouse");
212 bindInfo.AddInputDevice(2, "keyboard");
213 // 窗口同步信息
214 bindInfo.AddDisplay(0, "think 123");
215 bindInfo.AddDisplay(2, "hp 223");
216 ASSERT_TRUE(bindInfo.IsDisplayAdd(0, "think 123"));
217 ASSERT_TRUE(bindInfo.IsDisplayAdd(2, "hp 223"));
218 ASSERT_FALSE(bindInfo.IsDisplayAdd(1, "think 123"));
219
220 ASSERT_EQ(bindInfo.Dumps(), std::string("mouse<=>think 123\nkeyboard<=>hp 223\n"));
221 }
222
223 /**
224 * @tc.name: InputDisplayBindHelperTest_GetDisplayIdNames_007
225 * @tc.desc: Test GetBindDisplayNameByInputDevice
226 * @tc.type: FUNC
227 * @tc.require:
228 */
229 HWTEST_F(InputDisplayBindHelperTest, InputDisplayBindHelperTest_GetDisplayIdNames_007, TestSize.Level1)
230 {
231 using IdNames = std::set<std::pair<int32_t, std::string>>;
232 InputDisplayBindHelperTest::WriteConfigFile("mouse<=>think 123\nkeyboard<=>hp 223\n");
233 InputDisplayBindHelper bindInfo(InputDisplayBindHelperTest::GetCfgFileName());
234 // 多模初始化
235 bindInfo.Load();
236 IdNames idNames;
237 ASSERT_EQ(bindInfo.GetDisplayIdNames(), idNames);
238 bindInfo.AddDisplay(2, "hp 223");
239 idNames.insert(std::make_pair(2, "hp 223"));
240 ASSERT_EQ(bindInfo.GetDisplayIdNames(), idNames);
241
242 // 检测到触摸板设备
243 bindInfo.AddInputDevice(1, "mouse");
244 bindInfo.AddInputDevice(2, "keyboard");
245
246 // 窗口同步信息
247 bindInfo.AddDisplay(0, "think 123");
248 idNames.insert(std::make_pair(0, "think 123"));
249 ASSERT_EQ(bindInfo.GetDisplayIdNames(), idNames);
250 bindInfo.AddDisplay(2, "hp 223");
251 idNames.insert(std::make_pair(2, "hp 223"));
252 ASSERT_EQ(bindInfo.GetDisplayIdNames(), idNames);
253 ASSERT_EQ(bindInfo.Dumps(), std::string("mouse<=>think 123\nkeyboard<=>hp 223\n"));
254 }
255 } // namespace MMI
256 } // namespace OHOS