• 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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 
19 #include "input_event_handler.h"
20 #include "libinput_mock.h"
21 #include "libinput_wrapper.h"
22 #include "x_key_event_processor.h"
23 #include "mmi_log.h"
24 
25 #undef MMI_LOG_TAG
26 #define MMI_LOG_TAG "XKeyEventProcessorTest"
27 
28 namespace OHOS {
29 namespace MMI {
30 namespace {
31 using namespace testing;
32 using namespace testing::ext;
33 }
34 class XKeyEventProcessorTest : public testing::Test {
35 public:
SetUpTestCase(void)36     static void SetUpTestCase(void){};
TearDownTestCase(void)37     static void TearDownTestCase(void){};
SetUp()38     void SetUp(){};
TearDown()39     void TearDown(){};
40     libinput_event *GetEvent();
41 
42 private:
43     static LibinputWrapper libinput_;
44 };
45 
46 LibinputWrapper XKeyEventProcessorTest::libinput_;
47 
GetEvent()48 libinput_event *XKeyEventProcessorTest::GetEvent()
49 {
50     libinput_event *event = nullptr;
51     libinput_event *evt = libinput_.Dispatch();
52     while (evt != nullptr) {
53         auto type = libinput_event_get_type(evt);
54         if (type == LIBINPUT_EVENT_POINTER_AXIS) {
55             event = evt;
56         }
57         evt = libinput_.Dispatch();
58     }
59     return event;
60 }
61 
62 #ifdef OHOS_BUILD_ENABLE_X_KEY
63 /**
64  * @tc.name: XKeyEventProcessorTest_IsXKeyEvent_001
65  * @tc.desc: Test IsXKeyEvent
66  * @tc.type: FUNC
67  * @tc.require:
68  */
69 HWTEST_F(XKeyEventProcessorTest, XKeyEventProcessorTest_IsXKeyEvent_001, TestSize.Level1)
70 {
71     libinput_event *event = GetEvent();
72     bool ret = XKeyEventHdr->IsXKeyEvent(event);
73     EXPECT_FALSE(ret);
74 }
75 
76 /**
77  * @tc.name: XKeyEventProcessorTest_HandleXKeyEvent_001
78  * @tc.desc: Test HandleXKeyEvent
79  * @tc.type: FUNC
80  * @tc.require:
81  */
82 HWTEST_F(XKeyEventProcessorTest, XKeyEventProcessorTest_HandleXKeyEvent_001, TestSize.Level1)
83 {
84     libinput_event *event = GetEvent();
85     int32_t ret = XKeyEventHdr->HandleXKeyEvent(event);
86     EXPECT_EQ(ret, 65142786);
87 }
88 
89 /**
90  * @tc.name: XKeyEventProcessorTest_AnalyseKeyEvent_001
91  * @tc.desc: Test AnalyseKeyEvent
92  * @tc.type: FUNC
93  * @tc.require:
94  */
95 HWTEST_F(XKeyEventProcessorTest, XKeyEventProcessorTest_AnalyseKeyEvent_001, TestSize.Level1)
96 {
97     libinput_event *event = GetEvent();
98     int32_t ret = XKeyEventHdr->AnalyseKeyEvent(event);
99     EXPECT_EQ(ret, 65142786);
100 }
101 
102 /**
103  * @tc.name: XKeyEventProcessorTest_InterceptXKeyDown_001
104  * @tc.desc: Test InterceptXKeyDown
105  * @tc.type: FUNC
106  * @tc.require:
107  */
108 HWTEST_F(XKeyEventProcessorTest, XKeyEventProcessorTest_InterceptXKeyDown_001, TestSize.Level1)
109 {
110     XKeyEventHdr->pressCount_ = 0;
111     ASSERT_NO_FATAL_FAILURE(XKeyEventHdr->InterceptXKeyDown());
112     XKeyEventHdr->pressCount_ = 2;
113     ASSERT_NO_FATAL_FAILURE(XKeyEventHdr->InterceptXKeyDown());
114 }
115 
116 /**
117  * @tc.name: XKeyEventProcessorTest_StartLongPressTimer_001
118  * @tc.desc: Test StartLongPressTimer
119  * @tc.type: FUNC
120  * @tc.require:
121  */
122 HWTEST_F(XKeyEventProcessorTest, XKeyEventProcessorTest_StartLongPressTimer_001, TestSize.Level1)
123 {
124     XKeyEventHdr->pressCount_ = 1;
125     XKeyEventHdr->handledLongPress_ = true;
126     ASSERT_NO_FATAL_FAILURE(XKeyEventHdr->StartLongPressTimer());
127     XKeyEventHdr->pressCount_ = 5;
128     XKeyEventHdr->handledLongPress_ = true;
129     ASSERT_NO_FATAL_FAILURE(XKeyEventHdr->StartLongPressTimer());
130     XKeyEventHdr->pressCount_ = 1;
131     XKeyEventHdr->handledLongPress_ = false;
132     ASSERT_NO_FATAL_FAILURE(XKeyEventHdr->StartLongPressTimer());
133     XKeyEventHdr->pressCount_ = 5;
134     XKeyEventHdr->handledLongPress_ = false;
135     ASSERT_NO_FATAL_FAILURE(XKeyEventHdr->StartLongPressTimer());
136 }
137 
138 /**
139  * @tc.name: XKeyEventProcessorTest_InterceptXKeyUp_001
140  * @tc.desc: Test InterceptXKeyUp
141  * @tc.type: FUNC
142  * @tc.require:
143  */
144 HWTEST_F(XKeyEventProcessorTest, XKeyEventProcessorTest_InterceptXKeyUp_001, TestSize.Level1)
145 {
146     XKeyEventHdr->pressCount_ = 1;
147     ASSERT_NO_FATAL_FAILURE(XKeyEventHdr->InterceptXKeyUp());
148     XKeyEventHdr->pressCount_ = 2;
149     ASSERT_NO_FATAL_FAILURE(XKeyEventHdr->InterceptXKeyUp());
150     XKeyEventHdr->pressCount_ = 5;
151     ASSERT_NO_FATAL_FAILURE(XKeyEventHdr->InterceptXKeyUp());
152 }
153 
154 /**
155  * @tc.name: XKeyEventProcessorTest_StartSingleClickTimer_001
156  * @tc.desc: Test StartSingleClickTimer
157  * @tc.type: FUNC
158  * @tc.require:
159  */
160 HWTEST_F(XKeyEventProcessorTest, XKeyEventProcessorTest_StartSingleClickTimer_001, TestSize.Level1)
161 {
162     XKeyEventHdr->pressCount_ = 1;
163     ASSERT_NO_FATAL_FAILURE(XKeyEventHdr->StartSingleClickTimer());
164     XKeyEventHdr->pressCount_ = 5;
165     ASSERT_NO_FATAL_FAILURE(XKeyEventHdr->StartSingleClickTimer());
166 }
167 
168 /**
169  * @tc.name: XKeyEventProcessorTest_RemoveTimer_001
170  * @tc.desc: Test RemoveTimer
171  * @tc.type: FUNC
172  * @tc.require:
173  */
174 HWTEST_F(XKeyEventProcessorTest, XKeyEventProcessorTest_RemoveTimer_001, TestSize.Level1)
175 {
176     XKeyEventHdr->singleClickTimerId_ = 5;
177     ASSERT_NO_FATAL_FAILURE(XKeyEventHdr->RemoveTimer());
178     XKeyEventHdr->singleClickTimerId_ = -1;
179     ASSERT_NO_FATAL_FAILURE(XKeyEventHdr->RemoveTimer());
180     XKeyEventHdr->longPressTimerId_ = 5;
181     ASSERT_NO_FATAL_FAILURE(XKeyEventHdr->RemoveTimer());
182     XKeyEventHdr->longPressTimerId_ = -1;
183     ASSERT_NO_FATAL_FAILURE(XKeyEventHdr->RemoveTimer());
184 }
185 
186 /**
187  * @tc.name: XKeyEventProcessorTest_HandleQuickAccessMenu_001
188  * @tc.desc: Test HandleQuickAccessMenu
189  * @tc.type: FUNC
190  * @tc.require:
191  */
192 HWTEST_F(XKeyEventProcessorTest, XKeyEventProcessorTest_HandleQuickAccessMenu_001, TestSize.Level1)
193 {
194     int32_t xKeyEventType = 0;
195     int32_t ret = XKeyEventHdr->HandleQuickAccessMenu(xKeyEventType);
196     ASSERT_EQ(ret, RET_OK);
197     xKeyEventType = 1;
198     ret = XKeyEventHdr->HandleQuickAccessMenu(xKeyEventType);
199     ASSERT_EQ(ret, RET_OK);
200     xKeyEventType = 5;
201     ret = XKeyEventHdr->HandleQuickAccessMenu(xKeyEventType);
202     ASSERT_EQ(ret, RET_OK);
203 }
204 
205 #if (defined(OHOS_BUILD_ENABLE_POINTER) || defined(OHOS_BUILD_ENABLE_TOUCH)) && defined(OHOS_BUILD_ENABLE_MONITOR)
206 /**
207  * @tc.name: XKeyEventProcessorTest_HandleQuickAccessMenu_002
208  * @tc.desc: Test HandleQuickAccessMenu
209  * @tc.type: FUNC
210  * @tc.require:
211  */
212 HWTEST_F(XKeyEventProcessorTest, XKeyEventProcessorTest_HandleQuickAccessMenu_002, TestSize.Level1)
213 {
214     int32_t xKeyEventType = 5;
215     InputHandler->eventMonitorHandler_ = std::make_shared<EventMonitorHandler>();
216     int32_t ret = XKeyEventHdr->HandleQuickAccessMenu(xKeyEventType);
217     ASSERT_EQ(ret, RET_OK);
218     InputHandler->eventMonitorHandler_ = nullptr;
219     ret = XKeyEventHdr->HandleQuickAccessMenu(xKeyEventType);
220     ASSERT_EQ(ret, RET_OK);
221 }
222 #endif // (OHOS_BUILD_ENABLE_POINTER || OHOS_BUILD_ENABLE_TOUCH) && OHOS_BUILD_ENABLE_MONITOR
223 
224 /**
225  * @tc.name: XKeyEventProcessorTest_StartXKeyIfNeeded_001
226  * @tc.desc: Test StartXKeyIfNeeded
227  * @tc.type: FUNC
228  * @tc.require:
229  */
230 HWTEST_F(XKeyEventProcessorTest, XKeyEventProcessorTest_StartXKeyIfNeeded_001, TestSize.Level1)
231 {
232     int32_t xKeyEventType = 1;
233     XKeyEventHdr->isStartedXKey_ = false;
234     ASSERT_NO_FATAL_FAILURE(XKeyEventHdr->StartXKeyIfNeeded(xKeyEventType));
235     XKeyEventHdr->isStartedXKey_ = true;
236     ASSERT_NO_FATAL_FAILURE(XKeyEventHdr->StartXKeyIfNeeded(xKeyEventType));
237 }
238 #endif // OHOS_BUILD_ENABLE_X_KEY
239 } // namespace MMI
240 } // namespace OHOS