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 "input_manager.h"
18 #include "input_transfer_station.h"
19 #include "mock_window_adapter.h"
20 #include "singleton_mocker.h"
21 #include "window_impl.h"
22
23 using namespace testing;
24 using namespace testing::ext;
25
26 namespace OHOS {
27 namespace Rosen {
28 using WindowMocker = SingletonMocker<WindowAdapter, MockWindowAdapter>;
29 class InputTransferStationTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 virtual void SetUp() override;
34 virtual void TearDown() override;
35 sptr<WindowImpl> window_;
36 std::shared_ptr<InputEventListener> listener;
37 };
SetUpTestCase()38 void InputTransferStationTest::SetUpTestCase() {}
39
TearDownTestCase()40 void InputTransferStationTest::TearDownTestCase() {}
41
SetUp()42 void InputTransferStationTest::SetUp()
43 {
44 sptr<WindowOption> option = new WindowOption();
45 option->SetWindowName("inputwindow");
46 window_ = new WindowImpl(option);
47 window_->Create(INVALID_WINDOW_ID);
48 listener = std::make_shared<InputEventListener>(InputEventListener());
49 }
50
TearDown()51 void InputTransferStationTest::TearDown()
52 {
53 window_->Destroy();
54 window_ = nullptr;
55 }
56
57 namespace {
58 /**
59 * @tc.name: AddInputWindow
60 * @tc.desc: add input window in station.
61 * @tc.type: FUNC
62 * @tc.require: issueI5I5L4
63 */
64 HWTEST_F(InputTransferStationTest, AddInputWindow, Function | SmallTest | Level2)
65 {
66 if (!window_) {
67 GTEST_LOG_(INFO) << "Null Pointer";
68 return;
69 }
70 InputTransferStation::GetInstance().isRegisteredMMI_ = true;
71 InputTransferStation::GetInstance().AddInputWindow(window_);
72 InputTransferStation::GetInstance().isRegisteredMMI_ = false;
73 window_->GetWindowProperty()->SetWindowType(WindowType::APP_SUB_WINDOW_BASE);
74 InputTransferStation::GetInstance().destroyed_ = true;
75 InputTransferStation::GetInstance().AddInputWindow(window_);
76 InputTransferStation::GetInstance().destroyed_ = false;
77 InputTransferStation::GetInstance().AddInputWindow(window_);
78 InputTransferStation::GetInstance().inputListener_ = listener;
79 InputTransferStation::GetInstance().AddInputWindow(window_);
80 }
81
82 /**
83 * @tc.name: RemoveInputWindow
84 * @tc.desc: remove input window in station.
85 * @tc.type: FUNC
86 * @tc.require: issueI5I5L4
87 */
88 HWTEST_F(InputTransferStationTest, RemoveInputWindow, Function | SmallTest | Level2)
89 {
90 InputTransferStation::GetInstance().destroyed_ = true;
91 InputTransferStation::GetInstance().RemoveInputWindow(window_->GetWindowId());
92
93 InputTransferStation::GetInstance().destroyed_ = false;
94 sptr<WindowInputChannel> inputChannel = sptr<WindowInputChannel>::MakeSptr(window_);
95 InputTransferStation::GetInstance().windowInputChannels_.insert({ window_->GetWindowId(), inputChannel });
96 InputTransferStation::GetInstance().RemoveInputWindow(window_->GetWindowId());
97 auto iter = InputTransferStation::GetInstance().windowInputChannels_.find(window_->GetWindowId());
98 ASSERT_EQ(iter, InputTransferStation::GetInstance().windowInputChannels_.end());
99 }
100
101 /**
102 * @tc.name: OnInputEvent
103 * @tc.desc: OnInputEvent keyEvent
104 * @tc.type: FUNC
105 */
106 HWTEST_F(InputTransferStationTest, OnInputEvent1, Function | SmallTest | Level2)
107 {
108 auto keyEvent = MMI::KeyEvent::Create();
109 if (!keyEvent || !listener) {
110 GTEST_LOG_(INFO) << "Null Pointer";
111 return;
112 }
113 auto tempKeyEvent = keyEvent;
114 keyEvent = nullptr;
115 listener->OnInputEvent(keyEvent);
116 keyEvent = tempKeyEvent;
117 InputTransferStation::GetInstance().destroyed_ = true;
118 auto channel = InputTransferStation::GetInstance().GetInputChannel(0);
119 listener->OnInputEvent(keyEvent);
120 ASSERT_EQ(channel, nullptr);
121 }
122
123 /**
124 * @tc.name: OnInputEvent
125 * @tc.desc: OnInputEvent axisEvent
126 * @tc.type: FUNC
127 */
128 HWTEST_F(InputTransferStationTest, OnInputEvent2, Function | SmallTest | Level2)
129 {
130 auto axisEvent = MMI::AxisEvent::Create();
131 if (!axisEvent || !listener) {
132 GTEST_LOG_(INFO) << "Null Pointer";
133 return;
134 }
135 auto tempAxisEvent = axisEvent;
136 axisEvent = nullptr;
137 listener->OnInputEvent(axisEvent);
138 axisEvent = tempAxisEvent;
139 listener->OnInputEvent(axisEvent);
140 }
141
142 /**
143 * @tc.name: OnInputEvent
144 * @tc.desc: OnInputEvent pointerEvent
145 * @tc.type: FUNC
146 */
147 HWTEST_F(InputTransferStationTest, OnInputEvent3, Function | SmallTest | Level2)
148 {
149 auto pointerEvent = MMI::PointerEvent::Create();
150 if (!pointerEvent || !listener) {
151 GTEST_LOG_(INFO) << "Null Pointer";
152 return;
153 }
154 auto tempPointerEvent = pointerEvent;
155 pointerEvent = nullptr;
156 listener->OnInputEvent(pointerEvent);
157 pointerEvent = tempPointerEvent;
158 pointerEvent->SetPointerAction(static_cast<int32_t>(MMI::PointerEvent::POINTER_ACTION_DOWN));
159 listener->OnInputEvent(pointerEvent);
160 pointerEvent->SetPointerAction(static_cast<int32_t>(MMI::PointerEvent::POINTER_ACTION_MOVE));
161 pointerEvent->SetAgentWindowId(0);
162 listener->OnInputEvent(pointerEvent);
163 pointerEvent->SetAgentWindowId(static_cast<uint32_t>(-1));
164 InputTransferStation::GetInstance().destroyed_ = true;
165 auto channel = InputTransferStation::GetInstance().GetInputChannel(0);
166 listener->OnInputEvent(pointerEvent);
167 ASSERT_EQ(channel, nullptr);
168 }
169
170 /**
171 * @tc.name: GetInputChannel
172 * @tc.desc: GetInputChannel
173 * @tc.type: FUNC
174 */
175 HWTEST_F(InputTransferStationTest, GetInputChannel, Function | SmallTest | Level2)
176 {
177 InputTransferStation::GetInstance().destroyed_ = true;
178 auto channel = InputTransferStation::GetInstance().GetInputChannel(0);
179 ASSERT_EQ(channel, nullptr);
180 InputTransferStation::GetInstance().destroyed_ = false;
181 ASSERT_EQ(channel, nullptr);
182 InputTransferStation::GetInstance().AddInputWindow(window_);
183 InputTransferStation::GetInstance().GetInputChannel(0);
184 }
185 } // namespace
186 } // namespace Rosen
187 } // namespace OHOS
188