• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "mock_session.h"
18 #include "mock_window_adapter.h"
19 #include "singleton_mocker.h"
20 #include "window_scene_session_impl.h"
21 #include "window_impl.h"
22 #include "window_input_channel.h"
23 
24 using namespace testing;
25 using namespace testing::ext;
26 
27 namespace OHOS {
28 namespace Rosen {
29 class MockWindow : public Window {
30 public:
MockWindow()31     MockWindow() {};
~MockWindow()32     ~MockWindow() {};
33     MOCK_METHOD(bool, IsAnco, (), (const, override));
34     MOCK_METHOD(bool, OnPointDown, (int32_t eventId, int32_t posX, int32_t posY), (override));
35 };
36 
37 using WindowMocker = SingletonMocker<WindowAdapter, MockWindowAdapter>;
38 class WindowInputChannelTest : public testing::Test {
39 public:
40     static void SetUpTestCase();
41     static void TearDownTestCase();
42     virtual void SetUp() override;
43     virtual void TearDown() override;
44     sptr<WindowImpl> window_;
45 
46 private:
47     static constexpr uint32_t WAIT_SYNC_IN_NS = 300000;
48 };
SetUpTestCase()49 void WindowInputChannelTest::SetUpTestCase() {}
50 
TearDownTestCase()51 void WindowInputChannelTest::TearDownTestCase() {}
52 
SetUp()53 void WindowInputChannelTest::SetUp()
54 {
55     sptr<WindowOption> option = new WindowOption();
56     option->SetWindowName("window");
57     window_ = new WindowImpl(option);
58     window_->Create(INVALID_WINDOW_ID);
59 }
60 
TearDown()61 void WindowInputChannelTest::TearDown()
62 {
63     usleep(WAIT_SYNC_IN_NS);
64     window_->Destroy();
65     window_ = nullptr;
66 }
67 
68 namespace {
69     std::string g_logMsg;
MyLogCallback(const LogType type,const LogLevel level,const unsigned int domain,const char * tag,const char * msg)70     void MyLogCallback(const LogType type, const LogLevel level, const unsigned int domain, const char* tag,
71         const char* msg)
72     {
73         g_logMsg += msg;
74     }
75 }
76 
77 namespace {
78 /**
79  * @tc.name: HandlePointerEvent
80  * @tc.desc: consume pointer event when receive callback from input
81  * @tc.type: FUNC
82  */
83 HWTEST_F(WindowInputChannelTest, HandlePointerEvent, TestSize.Level1)
84 {
85     auto pointerEvent = MMI::PointerEvent::Create();
86     sptr<WindowInputChannel> inputChannel = new WindowInputChannel(window_);
87     ASSERT_NE(window_, nullptr);
88     ASSERT_NE(pointerEvent, nullptr);
89     ASSERT_NE(inputChannel, nullptr);
90     window_->ConsumePointerEvent(pointerEvent);
91     auto tempPointer = pointerEvent;
92     pointerEvent = nullptr;
93     inputChannel->HandlePointerEvent(pointerEvent);
94     pointerEvent = tempPointer;
95     inputChannel->window_ = nullptr;
96     inputChannel->HandlePointerEvent(pointerEvent);
97     inputChannel->window_ = window_;
98     window_->GetWindowProperty()->SetWindowType(WindowType::WINDOW_TYPE_DIALOG);
99     pointerEvent->SetAgentWindowId(0);
100     pointerEvent->SetTargetWindowId(1);
101     pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_DOWN);
102     inputChannel->HandlePointerEvent(pointerEvent);
103     pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_BUTTON_DOWN);
104     inputChannel->HandlePointerEvent(pointerEvent);
105     pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_PULL_MOVE);
106     inputChannel->HandlePointerEvent(pointerEvent);
107     pointerEvent->SetTargetWindowId(0);
108     inputChannel->HandlePointerEvent(pointerEvent);
109     window_->GetWindowProperty()->SetWindowType(WindowType::APP_SUB_WINDOW_BASE);
110     window_->SetWindowFlags(static_cast<uint32_t>(WindowFlag::WINDOW_FLAG_WATER_MARK));
111     inputChannel->HandlePointerEvent(pointerEvent);
112     window_->SetWindowFlags(static_cast<uint32_t>(WindowFlag::WINDOW_FLAG_IS_MODAL));
113     inputChannel->HandlePointerEvent(pointerEvent);
114 
115     pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_DOWN);
116     MMI::PointerEvent::PointerItem item;
117     pointerEvent->AddPointerItem(item);
118     inputChannel->HandlePointerEvent(pointerEvent);
119 
120     pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_MOVE);
121     inputChannel->HandlePointerEvent(pointerEvent);
122 
123     pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_PULL_MOVE);
124     inputChannel->HandlePointerEvent(pointerEvent);
125 
126     window_->GetWindowProperty()->SetWindowRect({ 0, 0, 8, 8 });
127     inputChannel->HandlePointerEvent(pointerEvent);
128     inputChannel->Destroy();
129 }
130 
131 /**
132  * @tc.name: HandlePointEvent
133  * @tc.desc: consume key event when receive callback from input
134  * @tc.type: FUNC
135  */
136 HWTEST_F(WindowInputChannelTest, HandlePointEvent01, TestSize.Level1)
137 {
138     auto pointerEvent = MMI::PointerEvent::Create();
139     sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
140     option->SetWindowName("window");
141     auto window = sptr<MockWindow>::MakeSptr();
142     sptr<WindowInputChannel> inputChannel = sptr<WindowInputChannel>::MakeSptr(window);
143 
144     EXPECT_CALL(*(window), IsAnco()).Times(1).WillOnce(Return(true));
145     EXPECT_CALL(*(window), OnPointDown(_, _, _)).Times(0);
146     inputChannel->HandlePointerEvent(pointerEvent);
147     testing::Mock::VerifyAndClearExpectations(window);
148 
149     MMI::PointerEvent::PointerItem item;
150     inputChannel->HandlePointerEvent(pointerEvent);
151 
152     pointerEvent->AddPointerItem(item);
153     pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_DOWN);
154     EXPECT_CALL(*(window), IsAnco()).Times(1).WillOnce(Return(true));
155     EXPECT_CALL(*(window), OnPointDown(_, _, _)).Times(1);
156     inputChannel->HandlePointerEvent(pointerEvent);
157     testing::Mock::VerifyAndClearExpectations(window);
158 }
159 
160 /**
161  * @tc.name: HandleKeyEvent
162  * @tc.desc: consume key event when receive callback from input
163  * @tc.type: FUNC
164  */
165 HWTEST_F(WindowInputChannelTest, HandleKeyEvent, TestSize.Level1)
166 {
167     auto keyEvent = MMI::KeyEvent::Create();
168     sptr<WindowInputChannel> inputChannel = new WindowInputChannel(window_);
169     ASSERT_NE(window_, nullptr);
170     ASSERT_NE(keyEvent, nullptr);
171     ASSERT_NE(inputChannel, nullptr);
172     window_->ConsumeKeyEvent(keyEvent);
173     auto tempKeyEvent = keyEvent;
174     keyEvent = nullptr;
175     inputChannel->HandleKeyEvent(keyEvent);
176     keyEvent = tempKeyEvent;
177     window_->GetWindowProperty()->SetWindowType(WindowType::WINDOW_TYPE_DIALOG);
178     keyEvent->SetAgentWindowId(0);
179     keyEvent->SetTargetWindowId(1);
180     inputChannel->HandleKeyEvent(keyEvent);
181     keyEvent->SetTargetWindowId(0);
182     inputChannel->HandleKeyEvent(keyEvent);
183     keyEvent->SetKeyCode(MMI::KeyEvent::KEYCODE_BACK);
184     inputChannel->HandleKeyEvent(keyEvent);
185     window_->GetWindowProperty()->SetWindowType(WindowType::WINDOW_TYPE_GLOBAL_SEARCH);
186     inputChannel->HandleKeyEvent(keyEvent);
187     keyEvent->SetKeyCode(MMI::KeyEvent::KEYCODE_FN);
188     inputChannel->HandleKeyEvent(keyEvent);
189 }
190 
191 /**
192  * @tc.name: DispatchKeyEventCallback
193  * @tc.desc: DispatchKeyEventCallback
194  * @tc.type: FUNC
195  */
196 HWTEST_F(WindowInputChannelTest, DispatchKeyEventCallback, TestSize.Level1)
197 {
198     sptr<WindowInputChannel> inputChannel = new WindowInputChannel(window_);
199     auto keyEvent = MMI::KeyEvent::Create();
200     ASSERT_NE(inputChannel, nullptr);
201     ASSERT_NE(keyEvent, nullptr);
202     auto tempKeyEvent = keyEvent;
203     keyEvent = nullptr;
204     inputChannel->DispatchKeyEventCallback(keyEvent, false);
205     keyEvent = tempKeyEvent;
206     inputChannel->DispatchKeyEventCallback(keyEvent, true);
207     inputChannel->DispatchKeyEventCallback(keyEvent, false);
208     inputChannel->window_ = nullptr;
209     inputChannel->DispatchKeyEventCallback(keyEvent, false);
210     inputChannel->window_ = window_;
211     inputChannel->DispatchKeyEventCallback(keyEvent, false);
212 }
213 
214 /**
215  * @tc.name: GetWindowRect
216  * @tc.desc: GetWindowRect
217  * @tc.type: FUNC
218  */
219 HWTEST_F(WindowInputChannelTest, GetWindowRect, TestSize.Level1)
220 {
221     sptr<WindowInputChannel> inputChannel = new WindowInputChannel(window_);
222     ASSERT_NE(inputChannel, nullptr);
223     inputChannel->window_ = nullptr;
224     auto rect = inputChannel->GetWindowRect();
225     Rect tempTect;
226     ASSERT_EQ(tempTect, rect);
227     inputChannel->window_ = window_;
228     auto rect2 = inputChannel->GetWindowRect();
229     ASSERT_EQ(tempTect, rect2);
230 }
231 
232 /**
233  * @tc.name: HandleKeyEventTest
234  * @tc.desc: consume key event when receive callback from input
235  * @tc.type: FUNC
236  */
237 HWTEST_F(WindowInputChannelTest, HandleKeyEventTest, TestSize.Level1)
238 {
239     g_logMsg.clear();
240     LOG_SetCallback(MyLogCallback);
241 
242     auto keyEvent = MMI::KeyEvent::Create();
243     ASSERT_NE(keyEvent, nullptr);
244     keyEvent->SetKeyCode(MMI::KeyEvent::KEYCODE_BACK);
245     keyEvent->SetAgentWindowId(0);
246     keyEvent->SetTargetWindowId(0);
247     keyEvent->SetKeyAction(MMI::KeyEvent::KEY_ACTION_UP);
248 
249     sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
250     option->SetWindowName("HandleKeyEventTest");
251     option->SetWindowType(WindowType::WINDOW_TYPE_DIALOG);
252     sptr<WindowSceneSessionImpl> sceneSession = sptr<WindowSceneSessionImpl>::MakeSptr(option);
253     SessionInfo sessionInfo = { "CreateTestBundle", "CreateTestModule", "CreateTestAbility" };
254     sptr<SessionMocker> session = sptr<SessionMocker>::MakeSptr(sessionInfo);
255     sceneSession->hostSession_ = session;
256     sceneSession->property_->SetPersistentId(1001);
257 
258     sptr<WindowInputChannel> inputChannel = sptr<WindowInputChannel>::MakeSptr(sceneSession);
259     ASSERT_NE(inputChannel, nullptr);
260 
261     auto ret = sceneSession->SetDialogBackGestureEnabled(true);
262     EXPECT_EQ(ret, WMError::WM_OK);
263     inputChannel->HandleKeyEvent(keyEvent);
264     EXPECT_TRUE(g_logMsg.find("keyEvent is nullptr") == std::string::npos);
265     EXPECT_TRUE(g_logMsg.find("ConsumeBackEvent") != std::string::npos);
266 
267     g_logMsg.clear();
268     ret = sceneSession->SetDialogBackGestureEnabled(false);
269     EXPECT_EQ(ret, WMError::WM_OK);
270     inputChannel->HandleKeyEvent(keyEvent);
271     EXPECT_TRUE(g_logMsg.find("ConsumeBackEvent") == std::string::npos);
272 
273     g_logMsg.clear();
274     keyEvent->SetKeyAction(MMI::KeyEvent::KEY_ACTION_UNKNOWN);
275     ret = sceneSession->SetDialogBackGestureEnabled(true);
276     EXPECT_EQ(ret, WMError::WM_OK);
277     inputChannel->HandleKeyEvent(keyEvent);
278     EXPECT_TRUE(g_logMsg.find("ConsumeBackEvent") == std::string::npos);
279 
280     g_logMsg.clear();
281     ret = sceneSession->SetDialogBackGestureEnabled(false);
282     EXPECT_EQ(ret, WMError::WM_OK);
283     inputChannel->HandleKeyEvent(keyEvent);
284     EXPECT_TRUE(g_logMsg.find("ConsumeBackEvent") == std::string::npos);
285 
286     g_logMsg.clear();
287     inputChannel->Destroy();
288     LOG_SetCallback(nullptr);
289 }
290 } // namespace
291 } // namespace Rosen
292 } // namespace OHOS
293