• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "i_preference_manager.h"
20 #include "key_auto_repeat.h"
21 #include "mmi_log.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     ASSERT_NO_FATAL_FAILURE(keyAutoRepeat.SelectAutoRepeat(keyEvent));
73     timerId_ = TimerMgr->AddTimer(1, 1, nullptr);
74     keyAutoRepeat.SelectAutoRepeat(keyEvent);
75     EXPECT_EQ(timerId_, -1);
76     keyEvent->SetAction(KeyEvent::KEY_ACTION_UP);
77     ASSERT_NO_FATAL_FAILURE(keyAutoRepeat.SelectAutoRepeat(keyEvent));
78 }
79 
80 /**
81  * @tc.name: KeyAutoRepeatTest_GetTomlFilePath_001
82  * @tc.desc: Test the funcation GetTomlFilePath
83  * @tc.type: FUNC
84  * @tc.require:
85  */
86 HWTEST_F(KeyAutoRepeatTest, KeyAutoRepeatTest_GetTomlFilePath_001, TestSize.Level1)
87 {
88     CALL_DEBUG_ENTER;
89     KeyAutoRepeat keyAutoRepeat;
90     std::string fileName = "test";
91     std::string expectedPath = "/vendor/etc/keymap/test.TOML";
92     EXPECT_EQ(keyAutoRepeat.GetTomlFilePath(fileName), expectedPath);
93     fileName = "";
94     expectedPath = "/vendor/etc/keymap/.TOML";
95     EXPECT_EQ(keyAutoRepeat.GetTomlFilePath(fileName), expectedPath);
96 }
97 
98 /**
99  * @tc.name: KeyAutoRepeatTest_GetIntervalTime_001
100  * @tc.desc: Test the funcation GetIntervalTime
101  * @tc.type: FUNC
102  * @tc.require:
103  */
104 HWTEST_F(KeyAutoRepeatTest, KeyAutoRepeatTest_GetIntervalTime_001, TestSize.Level1)
105 {
106     CALL_DEBUG_ENTER;
107     KeyAutoRepeat keyAutoRepeat;
108     int32_t deviceId = 1;
109     int32_t expected = DEFAULT_KEY_REPEAT_RATE;
110     EXPECT_EQ(keyAutoRepeat.GetIntervalTime(deviceId), expected);
111     int32_t unexpected = 0;
112     EXPECT_NE(keyAutoRepeat.GetIntervalTime(deviceId), unexpected);
113 }
114 
115 /**
116  * @tc.name: KeyAutoRepeatTest_GetDelayTime_001
117  * @tc.desc: Test the funcation GetDelayTime
118  * @tc.type: FUNC
119  * @tc.require:
120  */
121 HWTEST_F(KeyAutoRepeatTest, KeyAutoRepeatTest_GetDelayTime_001, TestSize.Level1)
122 {
123     CALL_DEBUG_ENTER;
124     KeyAutoRepeat keyAutoRepeat;
125     int32_t delayTime = keyAutoRepeat.GetDelayTime();
126     EXPECT_EQ(delayTime, DEFAULT_KEY_REPEAT_DELAY);
127 }
128 
129 /**
130  * @tc.name: KeyAutoRepeatTest_GetKeyboardRepeatTime_001
131  * @tc.desc: Test the funcation GetKeyboardRepeatTime
132  * @tc.type: FUNC
133  * @tc.require:
134  */
135 HWTEST_F(KeyAutoRepeatTest, KeyAutoRepeatTest_GetKeyboardRepeatTime_001, TestSize.Level1)
136 {
137     CALL_DEBUG_ENTER;
138     KeyAutoRepeat keyAutoRepeat;
139     int32_t deviceId = 1;
140     bool isDelay = true;
141     int32_t expectedRepeatTime = DEFAULT_KEY_REPEAT_DELAY;
142     int32_t actualRepeatTime = keyAutoRepeat.GetKeyboardRepeatTime(deviceId, isDelay);
143     EXPECT_EQ(expectedRepeatTime, actualRepeatTime);
144     isDelay = false;
145     expectedRepeatTime = DEFAULT_KEY_REPEAT_RATE;
146     actualRepeatTime = keyAutoRepeat.GetKeyboardRepeatTime(deviceId, isDelay);
147     EXPECT_EQ(expectedRepeatTime, actualRepeatTime);
148 }
149 
150 /**
151  * @tc.name: KeyAutoRepeatTest_GetAutoSwitch_001
152  * @tc.desc: Test the funcation GetAutoSwitch
153  * @tc.type: FUNC
154  * @tc.require:
155  */
156 HWTEST_F(KeyAutoRepeatTest, KeyAutoRepeatTest_GetAutoSwitch_001, TestSize.Level1)
157 {
158     CALL_DEBUG_ENTER;
159     KeyAutoRepeat keyAutoRepeat;
160     std::map<int32_t, DeviceConfig> deviceConfig_;
161     int32_t existingDeviceId = 1;
162     DeviceConfig expectedConfig;
163     deviceConfig_[existingDeviceId] = expectedConfig;
164     ASSERT_NO_FATAL_FAILURE(keyAutoRepeat.GetAutoSwitch(existingDeviceId));
165 }
166 
167 /**
168  * @tc.name: KeyAutoRepeatTest_SetKeyboardRepeatDelay_001
169  * @tc.desc: Test the funcation SetKeyboardRepeatDelay
170  * @tc.type: FUNC
171  * @tc.require:
172  */
173 HWTEST_F(KeyAutoRepeatTest, KeyAutoRepeatTest_SetKeyboardRepeatDelay_001, TestSize.Level1)
174 {
175     CALL_DEBUG_ENTER;
176     KeyAutoRepeat keyAutoRepeat;
177     int32_t delay = 500;
178     int32_t expectedResult = RET_OK;
179     int32_t result = keyAutoRepeat.SetKeyboardRepeatDelay(delay);
180     EXPECT_EQ(result, expectedResult);
181     delay = 100;
182     result = keyAutoRepeat.SetKeyboardRepeatDelay(delay);
183     EXPECT_EQ(result, expectedResult);
184     delay = 2000;
185     result = keyAutoRepeat.SetKeyboardRepeatDelay(delay);
186     EXPECT_EQ(result, expectedResult);
187 }
188 
189 /**
190  * @tc.name: KeyAutoRepeatTest_SetKeyboardRepeatRate_001
191  * @tc.desc: Test the funcation SetKeyboardRepeatRate
192  * @tc.type: FUNC
193  * @tc.require:
194  */
195 HWTEST_F(KeyAutoRepeatTest, KeyAutoRepeatTest_SetKeyboardRepeatRate_001, TestSize.Level1)
196 {
197     CALL_DEBUG_ENTER;
198     KeyAutoRepeat keyAutoRepeat;
199     int32_t rate = 500;
200     int32_t expectedResult = RET_OK;
201     int32_t result = keyAutoRepeat.SetKeyboardRepeatRate(rate);
202     EXPECT_EQ(result, expectedResult);
203     rate = 30;
204     result = keyAutoRepeat.SetKeyboardRepeatRate(rate);
205     EXPECT_EQ(result, expectedResult);
206     rate = 101;
207     result = keyAutoRepeat.SetKeyboardRepeatRate(rate);
208     EXPECT_EQ(result, expectedResult);
209     rate = -1;
210     result = keyAutoRepeat.SetKeyboardRepeatRate(rate);
211     EXPECT_EQ(result, expectedResult);
212 }
213 
214 /**
215  * @tc.name: KeyAutoRepeatTest_GetKeyboardRepeatDelay_001
216  * @tc.desc: Test the funcation GetKeyboardRepeatDelay
217  * @tc.type: FUNC
218  * @tc.require:
219  */
220 HWTEST_F(KeyAutoRepeatTest, KeyAutoRepeatTest_GetKeyboardRepeatDelay_001, TestSize.Level1)
221 {
222     CALL_DEBUG_ENTER;
223     KeyAutoRepeat keyAutoRepeat;
224     int32_t delay = 0;
225     int32_t expectedDelay = 1000;
226     EXPECT_EQ(keyAutoRepeat.GetKeyboardRepeatDelay(delay), RET_OK);
227     EXPECT_EQ(delay, expectedDelay);
228     delay = 100;
229     EXPECT_EQ(keyAutoRepeat.GetKeyboardRepeatDelay(delay), RET_OK);
230     EXPECT_EQ(delay, expectedDelay);
231 }
232 
233 /**
234  * @tc.name: KeyAutoRepeatTest_GetKeyboardRepeatRate_001
235  * @tc.desc: Test the funcation GetKeyboardRepeatRate
236  * @tc.type: FUNC
237  * @tc.require:
238  */
239 HWTEST_F(KeyAutoRepeatTest, KeyAutoRepeatTest_GetKeyboardRepeatRate_001, TestSize.Level1)
240 {
241     CALL_DEBUG_ENTER;
242     KeyAutoRepeat keyAutoRepeat;
243     int32_t rate = 0;
244     EXPECT_EQ(keyAutoRepeat.GetKeyboardRepeatRate(rate), RET_OK);
245     int32_t expectedRate = MIN_KEY_REPEAT_RATE;
246     EXPECT_EQ(keyAutoRepeat.GetKeyboardRepeatRate(rate), RET_OK);
247     EXPECT_EQ(rate, expectedRate);
248     EXPECT_EQ(keyAutoRepeat.GetKeyboardRepeatRate(rate), RET_OK);
249     EXPECT_EQ(rate, expectedRate);
250     rate = 500;
251     EXPECT_EQ(keyAutoRepeat.GetKeyboardRepeatRate(rate), RET_OK);
252     EXPECT_EQ(rate, expectedRate);
253 }
254 
255 /**
256  * @tc.name: KeyAutoRepeatTest_PutConfigDataToDatabase_001
257  * @tc.desc: Test the funcation PutConfigDataToDatabase
258  * @tc.type: FUNC
259  * @tc.require:
260  */
261 HWTEST_F(KeyAutoRepeatTest, KeyAutoRepeatTest_PutConfigDataToDatabase_001, TestSize.Level1)
262 {
263     CALL_DEBUG_ENTER;
264     KeyAutoRepeat keyAutoRepeat;
265     std::string testKey = "testKey";
266     int32_t testValue = 123;
267     int32_t result = keyAutoRepeat.PutConfigDataToDatabase(testKey, testValue);
268     EXPECT_NE(result, -1);
269     ASSERT_TRUE(PREFERENCES_MGR->GetIntValue(testKey, testValue));
270 }
271 
272 /**
273  * @tc.name: KeyAutoRepeatTest_GetConfigDataFromDatabase_001
274  * @tc.desc: Test the funcation GetConfigDataFromDatabase
275  * @tc.type: FUNC
276  * @tc.require:
277  */
278 HWTEST_F(KeyAutoRepeatTest, KeyAutoRepeatTest_GetConfigDataFromDatabase_001, TestSize.Level1)
279 {
280     CALL_DEBUG_ENTER;
281     KeyAutoRepeat keyAutoRepeat;
282     std::string key = "test_key";
283     int32_t value = 0;
284     PREFERENCES_MGR->SetIntValue(key, KEYBOARD_FILE_NAME, 42);
285     int32_t ret = keyAutoRepeat.GetConfigDataFromDatabase(key, value);
286     EXPECT_EQ(ret, RET_OK);
287     EXPECT_EQ(value, 42);
288     key = "nonexistent_key";
289     value = 0;
290     ret = keyAutoRepeat.GetConfigDataFromDatabase(key, value);
291     EXPECT_EQ(ret, RET_OK);
292     EXPECT_EQ(value, 0);
293 }
294 } // namespace MMI
295 } // namespace OHOS