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_window_adapter.h"
18 #include "singleton_mocker.h"
19 #include "window_impl.h"
20 #include "window_input_channel.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace Rosen {
27 using WindowMocker = SingletonMocker<WindowAdapter, MockWindowAdapter>;
28 class WindowInputChannelTest : public testing::Test {
29 public:
30 static void SetUpTestCase();
31 static void TearDownTestCase();
32 virtual void SetUp() override;
33 virtual void TearDown() override;
34 sptr<WindowImpl> window_;
35
36 private:
37 static constexpr uint32_t WAIT_SYNC_IN_NS = 300000;
38 };
SetUpTestCase()39 void WindowInputChannelTest::SetUpTestCase() {}
40
TearDownTestCase()41 void WindowInputChannelTest::TearDownTestCase() {}
42
SetUp()43 void WindowInputChannelTest::SetUp()
44 {
45 sptr<WindowOption> option = new WindowOption();
46 option->SetWindowName("window");
47 window_ = new WindowImpl(option);
48 window_->Create(INVALID_WINDOW_ID);
49 }
50
TearDown()51 void WindowInputChannelTest::TearDown()
52 {
53 usleep(WAIT_SYNC_IN_NS);
54 window_->Destroy();
55 window_ = nullptr;
56 }
57
58 namespace {
59 /**
60 * @tc.name: HandlePointerEvent
61 * @tc.desc: consume pointer event when receive callback from input
62 * @tc.type: FUNC
63 */
64 HWTEST_F(WindowInputChannelTest, HandlePointerEvent, Function | SmallTest | Level2)
65 {
66 auto pointerEvent = MMI::PointerEvent::Create();
67 sptr<WindowInputChannel> inputChannel = new WindowInputChannel(window_);
68 ASSERT_NE(window_, nullptr);
69 ASSERT_NE(pointerEvent, nullptr);
70 ASSERT_NE(inputChannel, nullptr);
71 window_->ConsumePointerEvent(pointerEvent);
72 auto tempPointer = pointerEvent;
73 pointerEvent = nullptr;
74 inputChannel->HandlePointerEvent(pointerEvent);
75 pointerEvent = tempPointer;
76 inputChannel->window_ = nullptr;
77 inputChannel->HandlePointerEvent(pointerEvent);
78 inputChannel->window_ = window_;
79 window_->GetWindowProperty()->SetWindowType(WindowType::WINDOW_TYPE_DIALOG);
80 pointerEvent->SetAgentWindowId(0);
81 pointerEvent->SetTargetWindowId(1);
82 pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_DOWN);
83 inputChannel->HandlePointerEvent(pointerEvent);
84 pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_BUTTON_DOWN);
85 inputChannel->HandlePointerEvent(pointerEvent);
86 pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_PULL_MOVE);
87 inputChannel->HandlePointerEvent(pointerEvent);
88 pointerEvent->SetTargetWindowId(0);
89 inputChannel->HandlePointerEvent(pointerEvent);
90 window_->GetWindowProperty()->SetWindowType(WindowType::APP_SUB_WINDOW_BASE);
91 window_->SetWindowFlags(static_cast<uint32_t>(WindowFlag::WINDOW_FLAG_WATER_MARK));
92 inputChannel->HandlePointerEvent(pointerEvent);
93 window_->SetWindowFlags(static_cast<uint32_t>(WindowFlag::WINDOW_FLAG_IS_MODAL));
94 inputChannel->HandlePointerEvent(pointerEvent);
95
96 pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_DOWN);
97 MMI::PointerEvent::PointerItem item;
98 pointerEvent->AddPointerItem(item);
99 inputChannel->HandlePointerEvent(pointerEvent);
100
101 pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_MOVE);
102 inputChannel->HandlePointerEvent(pointerEvent);
103
104 pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_PULL_MOVE);
105 inputChannel->HandlePointerEvent(pointerEvent);
106
107 window_->GetWindowProperty()->SetWindowRect({ 0, 0, 8, 8 });
108 inputChannel->HandlePointerEvent(pointerEvent);
109 inputChannel->Destroy();
110 }
111
112 /**
113 * @tc.name: HandleKeyEvent
114 * @tc.desc: consume key event when receive callback from input
115 * @tc.type: FUNC
116 */
117 HWTEST_F(WindowInputChannelTest, HandleKeyEvent, Function | SmallTest | Level2)
118 {
119 auto keyEvent = MMI::KeyEvent::Create();
120 sptr<WindowInputChannel> inputChannel = new WindowInputChannel(window_);
121 ASSERT_NE(window_, nullptr);
122 ASSERT_NE(keyEvent, nullptr);
123 ASSERT_NE(inputChannel, nullptr);
124 window_->ConsumeKeyEvent(keyEvent);
125 auto tempKeyEvent = keyEvent;
126 keyEvent = nullptr;
127 inputChannel->HandleKeyEvent(keyEvent);
128 keyEvent = tempKeyEvent;
129 window_->GetWindowProperty()->SetWindowType(WindowType::WINDOW_TYPE_DIALOG);
130 keyEvent->SetAgentWindowId(0);
131 keyEvent->SetTargetWindowId(1);
132 inputChannel->HandleKeyEvent(keyEvent);
133 keyEvent->SetTargetWindowId(0);
134 inputChannel->HandleKeyEvent(keyEvent);
135 keyEvent->SetKeyCode(MMI::KeyEvent::KEYCODE_BACK);
136 inputChannel->HandleKeyEvent(keyEvent);
137 window_->GetWindowProperty()->SetWindowType(WindowType::WINDOW_TYPE_GLOBAL_SEARCH);
138 inputChannel->HandleKeyEvent(keyEvent);
139 keyEvent->SetKeyCode(MMI::KeyEvent::KEYCODE_FN);
140 inputChannel->HandleKeyEvent(keyEvent);
141 }
142
143 /**
144 * @tc.name: DispatchKeyEventCallback
145 * @tc.desc: DispatchKeyEventCallback
146 * @tc.type: FUNC
147 */
148 HWTEST_F(WindowInputChannelTest, DispatchKeyEventCallback, Function | SmallTest | Level2)
149 {
150 sptr<WindowInputChannel> inputChannel = new WindowInputChannel(window_);
151 auto keyEvent = MMI::KeyEvent::Create();
152 ASSERT_NE(inputChannel, nullptr);
153 ASSERT_NE(keyEvent, nullptr);
154 auto tempKeyEvent = keyEvent;
155 keyEvent = nullptr;
156 inputChannel->DispatchKeyEventCallback(keyEvent, false);
157 keyEvent = tempKeyEvent;
158 inputChannel->DispatchKeyEventCallback(keyEvent, true);
159 inputChannel->DispatchKeyEventCallback(keyEvent, false);
160 inputChannel->window_ = nullptr;
161 inputChannel->DispatchKeyEventCallback(keyEvent, false);
162 inputChannel->window_ = window_;
163 inputChannel->DispatchKeyEventCallback(keyEvent, false);
164 }
165
166 /**
167 * @tc.name: GetWindowRect
168 * @tc.desc: GetWindowRect
169 * @tc.type: FUNC
170 */
171 HWTEST_F(WindowInputChannelTest, GetWindowRect, Function | SmallTest | Level2)
172 {
173 sptr<WindowInputChannel> inputChannel = new WindowInputChannel(window_);
174 ASSERT_NE(inputChannel, nullptr);
175 inputChannel->window_ = nullptr;
176 auto rect = inputChannel->GetWindowRect();
177 Rect tempTect;
178 ASSERT_EQ(tempTect, rect);
179 inputChannel->window_ = window_;
180 auto rect2 = inputChannel->GetWindowRect();
181 ASSERT_EQ(tempTect, rect2);
182 }
183 } // namespace
184 } // namespace Rosen
185 } // namespace OHOS
186