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 
18 #include "input_scene_board_judgement.h"
19 #include "input_manager_impl.h"
20 #include "multimodal_event_handler.h"
21 
22 #undef MMI_LOG_TAG
23 #define MMI_LOG_TAG "InputManagerImplTest"
24 
25 namespace OHOS {
26 namespace MMI {
27 namespace {
28 using namespace testing::ext;
29 } // namespace
30 
31 class InputManagerImplTest : public testing::Test {
32 public:
SetUpTestCase(void)33     static void SetUpTestCase(void) {}
TearDownTestCase(void)34     static void TearDownTestCase(void) {}
35 };
36 
37 /**
38  * @tc.name: InputManagerImplTest_IsValiadWindowAreas
39  * @tc.desc: Test IsValiadWindowAreas
40  * @tc.type: FUNC
41  * @tc.require:
42  */
43 HWTEST_F(InputManagerImplTest, InputManagerImplTest_IsValiadWindowAreas, TestSize.Level1)
44 {
45     CALL_TEST_DEBUG;
46     WindowInfo windowInfo;
47     windowInfo.action = WINDOW_UPDATE_ACTION::UNKNOWN;
48     Rect rect;
49     rect.x = 100;
50     windowInfo.defaultHotAreas.push_back(rect);
51     windowInfo.pointerHotAreas.push_back(rect);
52     windowInfo.pointerChangeAreas.push_back(100);
53     windowInfo.transform.push_back(100.5);
54     std::vector<WindowInfo> windows;
55     windows.push_back(windowInfo);
56     EXPECT_FALSE(InputMgrImpl.IsValiadWindowAreas(windows));
57 
58     windows[0].pointerChangeAreas.clear();
59     EXPECT_FALSE(InputMgrImpl.IsValiadWindowAreas(windows));
60 }
61 
62 /**
63  * @tc.name: InputManagerImplTest_PrintWindowInfo
64  * @tc.desc: Test PrintWindowInfo
65  * @tc.type: FUNC
66  * @tc.require:
67  */
68 HWTEST_F(InputManagerImplTest, InputManagerImplTest_PrintWindowInfo, TestSize.Level1)
69 {
70     CALL_TEST_DEBUG;
71     WindowInfo windowInfo;
72     windowInfo.action = WINDOW_UPDATE_ACTION::UNKNOWN;
73     Rect rect {
74         .x = 100,
75         .y = 100,
76         .width = 300,
77         .height = 300,
78     };
79     windowInfo.id = 10;
80     windowInfo.pid = 1000;
81     windowInfo.uid = 100;
82     windowInfo.area.x = 100;
83     windowInfo.area.y = 100;
84     windowInfo.area.height = 200;
85     windowInfo.area.width = 200;
86     windowInfo.agentWindowId = 50;
87     windowInfo.flags = 0;
88     windowInfo.displayId = 30;
89     windowInfo.zOrder = 60;
90     windowInfo.defaultHotAreas.push_back(rect);
91     windowInfo.pointerHotAreas.push_back(rect);
92     windowInfo.pointerChangeAreas.push_back(100);
93     windowInfo.transform.push_back(100.5);
94     std::vector<WindowInfo> windows;
95     windows.push_back(windowInfo);
96     EXPECT_NO_FATAL_FAILURE(InputMgrImpl.PrintWindowInfo(windows));
97 }
98 
99 /**
100  * @tc.name: InputManagerImplTest_RecoverPointerEvent
101  * @tc.desc: Test RecoverPointerEvent
102  * @tc.type: FUNC
103  * @tc.require:
104  */
105 HWTEST_F(InputManagerImplTest, InputManagerImplTest_RecoverPointerEvent, TestSize.Level1)
106 {
107     CALL_TEST_DEBUG;
108     InputMgrImpl.lastPointerEvent_ = PointerEvent::Create();
109     ASSERT_NE(InputMgrImpl.lastPointerEvent_, nullptr);
110     std::initializer_list<int32_t> pointerActionPullEvents { PointerEvent::POINTER_ACTION_MOVE,
111         PointerEvent::POINTER_ACTION_UP };
112     InputMgrImpl.lastPointerEvent_->SetPointerAction(PointerEvent::POINTER_ACTION_UP);
113     EXPECT_FALSE(InputMgrImpl.RecoverPointerEvent(pointerActionPullEvents, PointerEvent::POINTER_ACTION_UP));
114 
115     PointerEvent::PointerItem item;
116     item.SetPointerId(1);
117     InputMgrImpl.lastPointerEvent_->SetPointerId(1);
118     InputMgrImpl.lastPointerEvent_->AddPointerItem(item);
119     EXPECT_TRUE(InputMgrImpl.RecoverPointerEvent(pointerActionPullEvents, PointerEvent::POINTER_ACTION_UP));
120 
121     InputMgrImpl.lastPointerEvent_->SetPointerId(2);
122     EXPECT_FALSE(InputMgrImpl.RecoverPointerEvent(pointerActionPullEvents, PointerEvent::POINTER_ACTION_UP));
123 }
124 
125 /**
126  * @tc.name: InputManagerImplTest_OnDisconnected_01
127  * @tc.desc: Test OnDisconnected
128  * @tc.type: FUNC
129  * @tc.require:
130  */
131 HWTEST_F(InputManagerImplTest, InputManagerImplTest_OnDisconnected_01, TestSize.Level1)
132 {
133     CALL_TEST_DEBUG;
134     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
135     ASSERT_NE(pointerEvent, nullptr);
136     pointerEvent->pointerAction_ = PointerEvent::POINTER_ACTION_UP;
137 
138     EXPECT_NO_FATAL_FAILURE(InputMgrImpl.OnDisconnected());
139 }
140 
141 /**
142  * @tc.name: InputManagerImplTest_OnDisconnected_02
143  * @tc.desc: Test OnDisconnected
144  * @tc.type: FUNC
145  * @tc.require:
146  */
147 HWTEST_F(InputManagerImplTest, InputManagerImplTest_OnDisconnected_02, TestSize.Level1)
148 {
149     CALL_TEST_DEBUG;
150     std::shared_ptr<PointerEvent> pointerEvent = PointerEvent::Create();
151     ASSERT_NE(pointerEvent, nullptr);
152     pointerEvent->pointerAction_ = PointerEvent::POINTER_ACTION_PULL_UP;
153 
154     EXPECT_NO_FATAL_FAILURE(InputMgrImpl.OnDisconnected());
155 }
156 
157 /**
158  * @tc.name: InputManagerImplTest_OnKeyEvent_01
159  * @tc.desc: Test OnKeyEvent
160  * @tc.type: FUNC
161  * @tc.require:
162  */
163 HWTEST_F(InputManagerImplTest, InputManagerImplTest_OnKeyEvent_01, TestSize.Level1)
164 {
165     CALL_TEST_DEBUG;
166     std::shared_ptr<KeyEvent> keyEvent = KeyEvent::Create();
167     ASSERT_NE(keyEvent, nullptr);
168 
169     MMIClientPtr client = MMIEventHdl.GetMMIClient();
170     EXPECT_NO_FATAL_FAILURE(InputMgrImpl.OnKeyEvent(keyEvent));
171 }
172 
173 /**
174  * @tc.name: InputManagerImplTest_IsValiadWindowAreas_01
175  * @tc.desc: Test IsValiadWindowAreas
176  * @tc.type: FUNC
177  * @tc.require:
178  */
179 HWTEST_F(InputManagerImplTest, InputManagerImplTest_IsValiadWindowAreas_01, TestSize.Level1)
180 {
181     CALL_TEST_DEBUG;
182     std::vector<WindowInfo> windows;
183     WindowInfo window;
184     window.action = WINDOW_UPDATE_ACTION::DEL;
185 
186     bool ret = InputMgrImpl.IsValiadWindowAreas(windows);
187     EXPECT_TRUE(ret);
188 }
189 
190 /**
191  * @tc.name: InputManagerImplTest_IsValiadWindowAreas_02
192  * @tc.desc: Test IsValiadWindowAreas
193  * @tc.type: FUNC
194  * @tc.require:
195  */
196 HWTEST_F(InputManagerImplTest, InputManagerImplTest_IsValiadWindowAreas_02, TestSize.Level1)
197 {
198     CALL_TEST_DEBUG;
199     std::vector<WindowInfo> windows;
200     WindowInfo window;
201     window.action = WINDOW_UPDATE_ACTION::CHANGE;
202 
203     bool ret = InputMgrImpl.IsValiadWindowAreas(windows);
204     EXPECT_TRUE(ret);
205 }
206 } // namespace MMI
207 } // namespace OHOS