1 /*
2  * Copyright (c) 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 <gtest/gtest.h>
17 #include <libinput.h>
18 
19 #include "key_auto_repeat.h"
20 #include "mmi_log.h"
21 #include "i_preference_manager.h"
22 #include "timer_manager.h"
23 
24 #undef MMI_LOG_TAG
25 #define MMI_LOG_TAG "KeyAutoRepeatTest"
26 namespace OHOS {
27 namespace MMI {
28 namespace {
29 using namespace testing::ext;
30 constexpr int32_t DEFAULT_KEY_REPEAT_DELAY = 500;
31 constexpr int32_t DEFAULT_KEY_REPEAT_RATE = 50;
32 constexpr int32_t MIN_KEY_REPEAT_RATE = 36;
33 const std::string KEYBOARD_FILE_NAME = "keyboard_settings.xml";
34 } // namespace
35 
36 class KeyAutoRepeatTest : public testing::Test {
37 public:
SetUpTestCase(void)38     static void SetUpTestCase(void) {}
TearDownTestCase(void)39     static void TearDownTestCase(void) {}
40 };
41 
42 /**
43  * @tc.name: KeyAutoRepeatTest_AddDeviceConfig_001
44  * @tc.desc: Test the funcation AddDeviceConfig
45  * @tc.type: FUNC
46  * @tc.require:
47  */
48 HWTEST_F(KeyAutoRepeatTest, KeyAutoRepeatTest_AddDeviceConfig_001, TestSize.Level1)
49 {
50     CALL_DEBUG_ENTER;
51     KeyAutoRepeat keyAutoRepeat;
52     struct libinput_device *device = nullptr;
53     int32_t result = keyAutoRepeat.AddDeviceConfig(device);
54     EXPECT_NE(result, RET_OK);
55 }
56 
57 /**
58  * @tc.name: KeyAutoRepeatTest_SelectAutoRepeat_001
59  * @tc.desc: Test the funcation SelectAutoRepeat
60  * @tc.type: FUNC
61  * @tc.require:
62  */
63 HWTEST_F(KeyAutoRepeatTest, KeyAutoRepeatTest_SelectAutoRepeat_001, TestSize.Level1)
64 {
65     CALL_DEBUG_ENTER;
66     KeyAutoRepeat keyAutoRepeat;
67     auto keyEvent = KeyEvent::Create();
68     EXPECT_NE(keyEvent, nullptr);
69     int32_t timerId_ = 2;
70     keyEvent->SetKeyCode(1);
71     keyEvent->SetAction(KeyEvent::KEY_ACTION_DOWN);
72     keyAutoRepeat.SelectAutoRepeat(keyEvent);
73     EXPECT_EQ(timerId_, 2);
74     timerId_ = TimerMgr->AddTimer(1, 1, nullptr);
75     keyAutoRepeat.SelectAutoRepeat(keyEvent);
76     EXPECT_EQ(timerId_, -1);
77     keyEvent->SetAction(KeyEvent::KEY_ACTION_UP);
78     keyAutoRepeat.SelectAutoRepeat(keyEvent);
79     EXPECT_EQ(timerId_, -1);
80 }
81 
82 /**
83  * @tc.name: KeyAutoRepeatTest_GetTomlFilePath_001
84  * @tc.desc: Test the funcation GetTomlFilePath
85  * @tc.type: FUNC
86  * @tc.require:
87  */
88 HWTEST_F(KeyAutoRepeatTest, KeyAutoRepeatTest_GetTomlFilePath_001, TestSize.Level1)
89 {
90     CALL_DEBUG_ENTER;
91     KeyAutoRepeat keyAutoRepeat;
92     std::string fileName = "test";
93     std::string expectedPath = "/vendor/etc/keymap/test.TOML";
94     EXPECT_EQ(keyAutoRepeat.GetTomlFilePath(fileName), expectedPath);
95     fileName = "";
96     expectedPath = "/vendor/etc/keymap/.TOML";
97     EXPECT_EQ(keyAutoRepeat.GetTomlFilePath(fileName), expectedPath);
98 }
99 
100 /**
101  * @tc.name: KeyAutoRepeatTest_GetIntervalTime_001
102  * @tc.desc: Test the funcation GetIntervalTime
103  * @tc.type: FUNC
104  * @tc.require:
105  */
106 HWTEST_F(KeyAutoRepeatTest, KeyAutoRepeatTest_GetIntervalTime_001, TestSize.Level1)
107 {
108     CALL_DEBUG_ENTER;
109     KeyAutoRepeat keyAutoRepeat;
110     int32_t deviceId = 1;
111     int32_t expected = DEFAULT_KEY_REPEAT_RATE;
112     EXPECT_EQ(keyAutoRepeat.GetIntervalTime(deviceId), expected);
113     int32_t unexpected = 0;
114     EXPECT_NE(keyAutoRepeat.GetIntervalTime(deviceId), unexpected);
115 }
116 
117 /**
118  * @tc.name: KeyAutoRepeatTest_GetDelayTime_001
119  * @tc.desc: Test the funcation GetDelayTime
120  * @tc.type: FUNC
121  * @tc.require:
122  */
123 HWTEST_F(KeyAutoRepeatTest, KeyAutoRepeatTest_GetDelayTime_001, TestSize.Level1)
124 {
125     CALL_DEBUG_ENTER;
126     KeyAutoRepeat keyAutoRepeat;
127     int32_t delayTime = keyAutoRepeat.GetDelayTime();
128     EXPECT_EQ(delayTime, DEFAULT_KEY_REPEAT_DELAY);
129 }
130 
131 /**
132  * @tc.name: KeyAutoRepeatTest_GetKeyboardRepeatTime_001
133  * @tc.desc: Test the funcation GetKeyboardRepeatTime
134  * @tc.type: FUNC
135  * @tc.require:
136  */
137 HWTEST_F(KeyAutoRepeatTest, KeyAutoRepeatTest_GetKeyboardRepeatTime_001, TestSize.Level1)
138 {
139     CALL_DEBUG_ENTER;
140     KeyAutoRepeat keyAutoRepeat;
141     int32_t deviceId = 1;
142     bool isDelay = true;
143     int32_t expectedRepeatTime = DEFAULT_KEY_REPEAT_DELAY;
144     int32_t actualRepeatTime = keyAutoRepeat.GetKeyboardRepeatTime(deviceId, isDelay);
145     EXPECT_EQ(expectedRepeatTime, actualRepeatTime);
146     isDelay = false;
147     expectedRepeatTime = DEFAULT_KEY_REPEAT_RATE;
148     actualRepeatTime = keyAutoRepeat.GetKeyboardRepeatTime(deviceId, isDelay);
149     EXPECT_EQ(expectedRepeatTime, actualRepeatTime);
150 }
151 
152 /**
153  * @tc.name: KeyAutoRepeatTest_GetAutoSwitch_001
154  * @tc.desc: Test the funcation GetAutoSwitch
155  * @tc.type: FUNC
156  * @tc.require:
157  */
158 HWTEST_F(KeyAutoRepeatTest, KeyAutoRepeatTest_GetAutoSwitch_001, TestSize.Level1)
159 {
160     CALL_DEBUG_ENTER;
161     KeyAutoRepeat keyAutoRepeat;
162     std::map<int32_t, DeviceConfig> deviceConfig_;
163     int32_t existingDeviceId = 1;
164     DeviceConfig expectedConfig;
165     deviceConfig_[existingDeviceId] = expectedConfig;
166     ASSERT_NO_FATAL_FAILURE(keyAutoRepeat.GetAutoSwitch(existingDeviceId));
167 }
168 
169 /**
170  * @tc.name: KeyAutoRepeatTest_SetKeyboardRepeatDelay_001
171  * @tc.desc: Test the funcation SetKeyboardRepeatDelay
172  * @tc.type: FUNC
173  * @tc.require:
174  */
175 HWTEST_F(KeyAutoRepeatTest, KeyAutoRepeatTest_SetKeyboardRepeatDelay_001, TestSize.Level1)
176 {
177     CALL_DEBUG_ENTER;
178     KeyAutoRepeat keyAutoRepeat;
179     int32_t delay = 500;
180     int32_t expectedResult = RET_OK;
181     int32_t result = keyAutoRepeat.SetKeyboardRepeatDelay(delay);
182     EXPECT_EQ(result, expectedResult);
183     delay = 100;
184     result = keyAutoRepeat.SetKeyboardRepeatDelay(delay);
185     EXPECT_EQ(result, expectedResult);
186     delay = 2000;
187     result = keyAutoRepeat.SetKeyboardRepeatDelay(delay);
188     EXPECT_EQ(result, expectedResult);
189 }
190 
191 /**
192  * @tc.name: KeyAutoRepeatTest_SetKeyboardRepeatRate_001
193  * @tc.desc: Test the funcation SetKeyboardRepeatRate
194  * @tc.type: FUNC
195  * @tc.require:
196  */
197 HWTEST_F(KeyAutoRepeatTest, KeyAutoRepeatTest_SetKeyboardRepeatRate_001, TestSize.Level1)
198 {
199     CALL_DEBUG_ENTER;
200     KeyAutoRepeat keyAutoRepeat;
201     int32_t rate = 500;
202     int32_t expectedResult = RET_OK;
203     int32_t result = keyAutoRepeat.SetKeyboardRepeatRate(rate);
204     EXPECT_EQ(result, expectedResult);
205     rate = 30;
206     result = keyAutoRepeat.SetKeyboardRepeatRate(rate);
207     EXPECT_EQ(result, expectedResult);
208     rate = 101;
209     result = keyAutoRepeat.SetKeyboardRepeatRate(rate);
210     EXPECT_EQ(result, expectedResult);
211     rate = -1;
212     result = keyAutoRepeat.SetKeyboardRepeatRate(rate);
213     EXPECT_EQ(result, expectedResult);
214 }
215 
216 /**
217  * @tc.name: KeyAutoRepeatTest_GetKeyboardRepeatDelay_001
218  * @tc.desc: Test the funcation GetKeyboardRepeatDelay
219  * @tc.type: FUNC
220  * @tc.require:
221  */
222 HWTEST_F(KeyAutoRepeatTest, KeyAutoRepeatTest_GetKeyboardRepeatDelay_001, TestSize.Level1)
223 {
224     CALL_DEBUG_ENTER;
225     KeyAutoRepeat keyAutoRepeat;
226     int32_t delay = 0;
227     int32_t expectedDelay = 1000;
228     EXPECT_EQ(keyAutoRepeat.GetKeyboardRepeatDelay(delay), RET_OK);
229     EXPECT_EQ(delay, expectedDelay);
230     delay = 100;
231     EXPECT_EQ(keyAutoRepeat.GetKeyboardRepeatDelay(delay), RET_OK);
232     EXPECT_EQ(delay, expectedDelay);
233 }
234 
235 /**
236  * @tc.name: KeyAutoRepeatTest_GetKeyboardRepeatRate_001
237  * @tc.desc: Test the funcation GetKeyboardRepeatRate
238  * @tc.type: FUNC
239  * @tc.require:
240  */
241 HWTEST_F(KeyAutoRepeatTest, KeyAutoRepeatTest_GetKeyboardRepeatRate_001, TestSize.Level1)
242 {
243     CALL_DEBUG_ENTER;
244     KeyAutoRepeat keyAutoRepeat;
245     int32_t rate = 0;
246     EXPECT_EQ(keyAutoRepeat.GetKeyboardRepeatRate(rate), RET_OK);
247     int32_t expectedRate = MIN_KEY_REPEAT_RATE;
248     EXPECT_EQ(keyAutoRepeat.GetKeyboardRepeatRate(rate), RET_OK);
249     EXPECT_EQ(rate, expectedRate);
250     EXPECT_EQ(keyAutoRepeat.GetKeyboardRepeatRate(rate), RET_OK);
251     EXPECT_EQ(rate, expectedRate);
252     rate = 500;
253     EXPECT_EQ(keyAutoRepeat.GetKeyboardRepeatRate(rate), RET_OK);
254     EXPECT_EQ(rate, expectedRate);
255 }
256 
257 /**
258  * @tc.name: KeyAutoRepeatTest_PutConfigDataToDatabase_001
259  * @tc.desc: Test the funcation PutConfigDataToDatabase
260  * @tc.type: FUNC
261  * @tc.require:
262  */
263 HWTEST_F(KeyAutoRepeatTest, KeyAutoRepeatTest_PutConfigDataToDatabase_001, TestSize.Level1)
264 {
265     CALL_DEBUG_ENTER;
266     KeyAutoRepeat keyAutoRepeat;
267     std::string testKey = "testKey";
268     int32_t testValue = 123;
269     int32_t result = keyAutoRepeat.PutConfigDataToDatabase(testKey, testValue);
270     ASSERT_NE(result, -1);
271     ASSERT_TRUE(PREFERENCES_MGR->GetIntValue(testKey, testValue));
272 }
273 
274 /**
275  * @tc.name: KeyAutoRepeatTest_GetConfigDataFromDatabase_001
276  * @tc.desc: Test the funcation GetConfigDataFromDatabase
277  * @tc.type: FUNC
278  * @tc.require:
279  */
280 HWTEST_F(KeyAutoRepeatTest, KeyAutoRepeatTest_GetConfigDataFromDatabase_001, TestSize.Level1)
281 {
282     CALL_DEBUG_ENTER;
283     KeyAutoRepeat keyAutoRepeat;
284     std::string key = "test_key";
285     int32_t value = 0;
286     PREFERENCES_MGR->SetIntValue(key, KEYBOARD_FILE_NAME, 42);
287     int32_t ret = keyAutoRepeat.GetConfigDataFromDatabase(key, value);
288     EXPECT_EQ(ret, RET_OK);
289     EXPECT_EQ(value, 42);
290     key = "nonexistent_key";
291     value = 0;
292     ret = keyAutoRepeat.GetConfigDataFromDatabase(key, value);
293     EXPECT_EQ(ret, RET_OK);
294     EXPECT_EQ(value, 0);
295 }
296 } // namespace MMI
297 } // namespace OHOS