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 "modifier_render_thread/rs_modifiers_draw_thread.h"
21 #include "singleton_mocker.h"
22 #include "window_impl.h"
23 #include "window_manager_hilog.h"
24
25 using namespace testing;
26 using namespace testing::ext;
27
28 namespace OHOS {
29 namespace Rosen {
30 namespace {
31 std::string g_errLog;
MyLogCallback(const LogType type,const LogLevel level,const unsigned int domain,const char * tag,const char * msg)32 void MyLogCallback(const LogType type, const LogLevel level, const unsigned int domain, const char *tag,
33 const char *msg)
34 {
35 g_errLog = msg;
36 }
37 using WindowMocker = SingletonMocker<WindowAdapter, MockWindowAdapter>;
38 class InputTransferStationTest : 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 std::shared_ptr<InputEventListener> listener;
46 };
SetUpTestCase()47 void InputTransferStationTest::SetUpTestCase() {}
48
TearDownTestCase()49 void InputTransferStationTest::TearDownTestCase()
50 {
51 #ifdef RS_ENABLE_VK
52 RSModifiersDrawThread::Destroy();
53 #endif
54 }
55
SetUp()56 void InputTransferStationTest::SetUp()
57 {
58 sptr<WindowOption> option = new WindowOption();
59 option->SetWindowName("inputwindow");
60 window_ = new WindowImpl(option);
61 window_->Create(INVALID_WINDOW_ID);
62 listener = std::make_shared<InputEventListener>(InputEventListener());
63 }
64
TearDown()65 void InputTransferStationTest::TearDown()
66 {
67 window_->Destroy();
68 window_ = nullptr;
69 }
70
71 namespace {
72 /**
73 * @tc.name: AddInputWindow
74 * @tc.desc: add input window in station.
75 * @tc.type: FUNC
76 * @tc.require: issueI5I5L4
77 */
78 HWTEST_F(InputTransferStationTest, AddInputWindow, TestSize.Level0)
79 {
80 InputTransferStation::GetInstance().isRegisteredMMI_ = true;
81 InputTransferStation::GetInstance().AddInputWindow(window_);
82 InputTransferStation::GetInstance().isRegisteredMMI_ = false;
83 window_->GetWindowProperty()->SetWindowType(WindowType::APP_SUB_WINDOW_BASE);
84 InputTransferStation::GetInstance().destroyed_ = true;
85 InputTransferStation::GetInstance().AddInputWindow(window_);
86 InputTransferStation::GetInstance().destroyed_ = false;
87 InputTransferStation::GetInstance().AddInputWindow(window_);
88 InputTransferStation::GetInstance().inputListener_ = listener;
89 InputTransferStation::GetInstance().AddInputWindow(window_);
90 InputTransferStation::GetInstance().isGameControllerLoaded_ = true;
91 InputTransferStation::GetInstance().AddInputWindow(window_);
92 InputTransferStation::GetInstance().isGameControllerLoaded_ = false;
93 InputTransferStation::GetInstance().AddInputWindow(window_);
94 ASSERT_EQ(true, InputTransferStation::GetInstance().isGameControllerLoaded_);
95 }
96
97 /**
98 * @tc.name: RemoveInputWindow
99 * @tc.desc: remove input window in station.
100 * @tc.type: FUNC
101 * @tc.require: issueI5I5L4
102 */
103 HWTEST_F(InputTransferStationTest, RemoveInputWindow, TestSize.Level0)
104 {
105 InputTransferStation::GetInstance().destroyed_ = true;
106 InputTransferStation::GetInstance().RemoveInputWindow(window_->GetWindowId());
107
108 InputTransferStation::GetInstance().destroyed_ = false;
109 sptr<WindowInputChannel> inputChannel = sptr<WindowInputChannel>::MakeSptr(window_);
110 InputTransferStation::GetInstance().windowInputChannels_.insert({ window_->GetWindowId(), inputChannel });
111 InputTransferStation::GetInstance().RemoveInputWindow(window_->GetWindowId());
112 auto iter = InputTransferStation::GetInstance().windowInputChannels_.find(window_->GetWindowId());
113 ASSERT_EQ(iter, InputTransferStation::GetInstance().windowInputChannels_.end());
114 }
115
116 /**
117 * @tc.name: OnInputEvent
118 * @tc.desc: OnInputEvent keyEvent
119 * @tc.type: FUNC
120 */
121 HWTEST_F(InputTransferStationTest, OnInputEvent1, TestSize.Level1)
122 {
123 auto keyEvent = MMI::KeyEvent::Create();
124 auto tempKeyEvent = keyEvent;
125 keyEvent = nullptr;
126 listener->OnInputEvent(keyEvent);
127 keyEvent = tempKeyEvent;
128 InputTransferStation::GetInstance().destroyed_ = true;
129 auto channel = InputTransferStation::GetInstance().GetInputChannel(0);
130 listener->OnInputEvent(keyEvent);
131 ASSERT_EQ(channel, nullptr);
132 }
133
134 /**
135 * @tc.name: OnInputEvent
136 * @tc.desc: OnInputEvent axisEvent
137 * @tc.type: FUNC
138 */
139 HWTEST_F(InputTransferStationTest, OnInputEvent2, TestSize.Level1)
140 {
141 g_errLog.clear();
142 LOG_SetCallback(MyLogCallback);
143 auto axisEvent = MMI::AxisEvent::Create();
144 auto tempAxisEvent = axisEvent;
145 axisEvent = nullptr;
146 listener->OnInputEvent(axisEvent);
147 EXPECT_TRUE(g_errLog.find("AxisEvent is nullptr") != std::string::npos);
148 axisEvent = tempAxisEvent;
149 listener->OnInputEvent(axisEvent);
150 EXPECT_FALSE(g_errLog.find("Receive axisEvent, windowId: %{public}d") != std::string::npos);
151 LOG_SetCallback(nullptr);
152 }
153
154 /**
155 * @tc.name: OnInputEvent
156 * @tc.desc: OnInputEvent pointerEvent
157 * @tc.type: FUNC
158 */
159 HWTEST_F(InputTransferStationTest, OnInputEvent3, TestSize.Level1)
160 {
161 auto pointerEvent = MMI::PointerEvent::Create();
162 auto tempPointerEvent = pointerEvent;
163 pointerEvent = nullptr;
164 listener->OnInputEvent(pointerEvent);
165 pointerEvent = tempPointerEvent;
166 pointerEvent->SetPointerAction(static_cast<int32_t>(MMI::PointerEvent::POINTER_ACTION_DOWN));
167 listener->OnInputEvent(pointerEvent);
168 pointerEvent->SetPointerAction(static_cast<int32_t>(MMI::PointerEvent::POINTER_ACTION_MOVE));
169 pointerEvent->SetAgentWindowId(0);
170 listener->OnInputEvent(pointerEvent);
171 pointerEvent->SetSourceType(MMI::PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
172 pointerEvent->SetPointerAction(static_cast<int32_t>(MMI::PointerEvent::POINTER_ACTION_DOWN));
173 pointerEvent->AddFlag(MMI::InputEvent::EVENT_FLAG_GESTURE_SUPPLEMENT);
174 listener->OnInputEvent(pointerEvent);
175 EXPECT_EQ(pointerEvent->GetSourceType(), MMI::PointerEvent::SOURCE_TYPE_MOUSE);
176 pointerEvent->SetAgentWindowId(static_cast<uint32_t>(-1));
177 InputTransferStation::GetInstance().destroyed_ = true;
178 auto channel = InputTransferStation::GetInstance().GetInputChannel(0);
179 listener->OnInputEvent(pointerEvent);
180 ASSERT_EQ(channel, nullptr);
181 }
182
183 /**
184 * @tc.name: GetInputChannel
185 * @tc.desc: GetInputChannel
186 * @tc.type: FUNC
187 */
188 HWTEST_F(InputTransferStationTest, GetInputChannel, TestSize.Level0)
189 {
190 InputTransferStation::GetInstance().destroyed_ = true;
191 auto channel = InputTransferStation::GetInstance().GetInputChannel(0);
192 ASSERT_EQ(channel, nullptr);
193 InputTransferStation::GetInstance().destroyed_ = false;
194 ASSERT_EQ(channel, nullptr);
195 InputTransferStation::GetInstance().AddInputWindow(window_);
196 InputTransferStation::GetInstance().GetInputChannel(0);
197 }
198 } // namespace
199 }
200 } // namespace Rosen
201 } // namespace OHOS
202