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