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