1 /*
2  * Copyright (c) 2024 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 "session/host/include/sub_session.h"
18 
19 #include "common/include/session_permission.h"
20 #include "key_event.h"
21 #include "mock/mock_session_stage.h"
22 #include "session/host/include/session.h"
23 #include "session/host/include/main_session.h"
24 #include "session/host/include/system_session.h"
25 #include <ui/rs_surface_node.h>
26 #include "window_event_channel_base.h"
27 #include "window_helper.h"
28 #include "window_manager_hilog.h"
29 #include "window_property.h"
30 #include "window_session_property.h"
31 
32 using namespace testing;
33 using namespace testing::ext;
34 
35 namespace OHOS {
36 namespace Rosen {
37 class SubSessionTest : public testing::Test {
38 public:
39     static void SetUpTestCase();
40     static void TearDownTestCase();
41     void SetUp() override;
42     void TearDown() override;
43     SessionInfo info;
44     sptr<SubSession::SpecificSessionCallback> specificCallback = nullptr;
45 private:
46     RSSurfaceNode::SharedPtr CreateRSSurfaceNode();
47     sptr<SubSession> subSession_;
48     SystemSessionConfig systemConfig_;
49 };
50 
SetUpTestCase()51 void SubSessionTest::SetUpTestCase()
52 {
53 }
54 
TearDownTestCase()55 void SubSessionTest::TearDownTestCase()
56 {
57 }
58 
SetUp()59 void SubSessionTest::SetUp()
60 {
61     SessionInfo info;
62     info.abilityName_ = "testMainSession1";
63     info.moduleName_ = "testMainSession2";
64     info.bundleName_ = "testMainSession3";
65     subSession_ = new SubSession(info, specificCallback);
66     EXPECT_NE(nullptr, subSession_);
67 }
68 
TearDown()69 void SubSessionTest::TearDown()
70 {
71     subSession_ = nullptr;
72 }
73 
CreateRSSurfaceNode()74 RSSurfaceNode::SharedPtr SubSessionTest::CreateRSSurfaceNode()
75 {
76     struct RSSurfaceNodeConfig rsSurfaceNodeConfig;
77     rsSurfaceNodeConfig.SurfaceNodeName = "WindowSessionTestSurfaceNode";
78     auto surfaceNode = RSSurfaceNode::Create(rsSurfaceNodeConfig);
79     return surfaceNode;
80 }
81 
82 namespace {
83 
84 /**
85  * @tc.name: TransferKeyEvent01
86  * @tc.desc: check func TransferKeyEvent
87  * @tc.type: FUNC
88  */
HWTEST_F(SubSessionTest, TransferKeyEvent01, Function | SmallTest | Level1)89 HWTEST_F(SubSessionTest, TransferKeyEvent01, Function | SmallTest | Level1)
90 {
91     subSession_->state_ = SessionState::STATE_END;
92 
93     ASSERT_EQ(WSError::WS_ERROR_INVALID_SESSION, subSession_->TransferKeyEvent(nullptr));
94 }
95 
96 /**
97  * @tc.name: TransferKeyEvent02
98  * @tc.desc: check func TransferKeyEvent
99  * @tc.type: FUNC
100  */
HWTEST_F(SubSessionTest, TransferKeyEvent02, Function | SmallTest | Level1)101 HWTEST_F(SubSessionTest, TransferKeyEvent02, Function | SmallTest | Level1)
102 {
103     subSession_->state_ = SessionState::STATE_CONNECT;
104     std::shared_ptr<MMI::KeyEvent> keyEvent = nullptr;
105 
106     ASSERT_EQ(WSError::WS_ERROR_NULLPTR, subSession_->TransferKeyEvent(keyEvent));
107 }
108 
109 /**
110  * @tc.name: TransferKeyEvent03
111  * @tc.desc: check func TransferKeyEvent
112  * @tc.type: FUNC
113  */
HWTEST_F(SubSessionTest, TransferKeyEvent03, Function | SmallTest | Level1)114 HWTEST_F(SubSessionTest, TransferKeyEvent03, Function | SmallTest | Level1)
115 {
116     ASSERT_NE(subSession_, nullptr);
117     subSession_->state_ = SessionState::STATE_CONNECT;
118     std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
119     ASSERT_NE(keyEvent, nullptr);
120     subSession_->SetParentSession(nullptr);
121     ASSERT_EQ(WSError::WS_ERROR_NULLPTR, subSession_->TransferKeyEvent(keyEvent));
122 }
123 
124 /**
125  * @tc.name: TransferKeyEvent04
126  * @tc.desc: check func TransferKeyEvent
127  * @tc.type: FUNC
128  */
HWTEST_F(SubSessionTest, TransferKeyEvent04, Function | SmallTest | Level1)129 HWTEST_F(SubSessionTest, TransferKeyEvent04, Function | SmallTest | Level1)
130 {
131     SessionInfo sessionInfo;
132     sessionInfo.abilityName_ = "TransferKeyEvent04";
133     sessionInfo.moduleName_ = "TransferKeyEvent04";
134     sessionInfo.bundleName_ = "TransferKeyEvent04";
135     sptr<SubSession> session = new SubSession(sessionInfo, specificCallback);
136     ASSERT_NE(session, nullptr);
137     std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
138     ASSERT_NE(keyEvent, nullptr);
139 
140     subSession_->SetParentSession(session);
141     subSession_->SetSessionState(SessionState::STATE_ACTIVE);
142     ASSERT_EQ(WSError::WS_ERROR_NULLPTR, subSession_->TransferKeyEvent(keyEvent));
143 }
144 
145 /**
146  * @tc.name: IsTopmost01
147  * @tc.desc: check func IsTopmost
148  * @tc.type: FUNC
149  */
HWTEST_F(SubSessionTest, IsTopmost01, Function | SmallTest | Level1)150 HWTEST_F(SubSessionTest, IsTopmost01, Function | SmallTest | Level1)
151 {
152     subSession_->GetSessionProperty()->SetTopmost(false);
153     ASSERT_EQ(false, subSession_->IsTopmost());
154 
155     subSession_->GetSessionProperty()->SetTopmost(true);
156     ASSERT_EQ(true, subSession_->IsTopmost());
157 }
158 
159 /**
160  * @tc.name: IsTopmost02
161  * @tc.desc: check func IsTopmost
162  * @tc.type: FUNC
163  */
HWTEST_F(SubSessionTest, IsTopmost02, Function | SmallTest | Level1)164 HWTEST_F(SubSessionTest, IsTopmost02, Function | SmallTest | Level1)
165 {
166     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
167     subSession_->SetSessionProperty(property);
168     ASSERT_TRUE(subSession_->GetSessionProperty() != nullptr);
169 
170     subSession_->GetSessionProperty()->SetTopmost(true);
171     ASSERT_EQ(true, subSession_->IsTopmost());
172 }
173 
174 /**
175  * @tc.name: CheckPointerEventDispatch01
176  * @tc.desc: check func CheckPointerEventDispatch
177  * @tc.type: FUNC
178  */
HWTEST_F(SubSessionTest, CheckPointerEventDispatch01, Function | SmallTest | Level1)179 HWTEST_F(SubSessionTest, CheckPointerEventDispatch01, Function | SmallTest | Level1)
180 {
181     std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
182     ASSERT_NE(nullptr, pointerEvent);
183     systemConfig_.windowUIType_ = WindowUIType::PHONE_WINDOW;
184 
185     ASSERT_TRUE(subSession_ != nullptr);
186     auto result = subSession_->CheckPointerEventDispatch(pointerEvent);
187     ASSERT_TRUE(result);
188 }
189 
190 /**
191  * @tc.name: CheckPointerEventDispatch02
192  * @tc.desc: check func CheckPointerEventDispatch
193  * @tc.type: FUNC
194  */
HWTEST_F(SubSessionTest, CheckPointerEventDispatch02, Function | SmallTest | Level1)195 HWTEST_F(SubSessionTest, CheckPointerEventDispatch02, Function | SmallTest | Level1)
196 {
197     std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
198     ASSERT_NE(nullptr, pointerEvent);
199     systemConfig_.windowUIType_ = WindowUIType::PC_WINDOW;
200 
201     ASSERT_TRUE(subSession_ != nullptr);
202     subSession_->SetSessionState(SessionState::STATE_FOREGROUND);
203     auto result = subSession_->CheckPointerEventDispatch(pointerEvent);
204     ASSERT_TRUE(result);
205 }
206 
207 /**
208  * @tc.name: CheckPointerEventDispatch03
209  * @tc.desc: check func CheckPointerEventDispatch
210  * @tc.type: FUNC
211  */
HWTEST_F(SubSessionTest, CheckPointerEventDispatch03, Function | SmallTest | Level1)212 HWTEST_F(SubSessionTest, CheckPointerEventDispatch03, Function | SmallTest | Level1)
213 {
214     std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
215     ASSERT_NE(nullptr, pointerEvent);
216     systemConfig_.windowUIType_ = WindowUIType::PC_WINDOW;
217 
218     ASSERT_TRUE(subSession_ != nullptr);
219     subSession_->SetSessionState(SessionState::STATE_BACKGROUND);
220     subSession_->UpdateSessionState(SessionState::STATE_ACTIVE);
221     auto result = subSession_->CheckPointerEventDispatch(pointerEvent);
222     ASSERT_TRUE(result);
223 }
224 
225 /**
226  * @tc.name: CheckPointerEventDispatch04
227  * @tc.desc: check func CheckPointerEventDispatch
228  * @tc.type: FUNC
229  */
HWTEST_F(SubSessionTest, CheckPointerEventDispatch04, Function | SmallTest | Level1)230 HWTEST_F(SubSessionTest, CheckPointerEventDispatch04, Function | SmallTest | Level1)
231 {
232     std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
233     ASSERT_NE(nullptr, pointerEvent);
234     systemConfig_.windowUIType_ = WindowUIType::PC_WINDOW;
235 
236     ASSERT_TRUE(subSession_ != nullptr);
237     subSession_->SetSessionState(SessionState::STATE_BACKGROUND);
238     subSession_->UpdateSessionState(SessionState::STATE_INACTIVE);
239     pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_LEAVE_WINDOW);
240     auto result = subSession_->CheckPointerEventDispatch(pointerEvent);
241     ASSERT_TRUE(result);
242 }
243 
244 /**
245  * @tc.name: CheckPointerEventDispatch05
246  * @tc.desc: check func CheckPointerEventDispatch
247  * @tc.type: FUNC
248  */
HWTEST_F(SubSessionTest, CheckPointerEventDispatch05, Function | SmallTest | Level1)249 HWTEST_F(SubSessionTest, CheckPointerEventDispatch05, Function | SmallTest | Level1)
250 {
251     std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
252     ASSERT_NE(nullptr, pointerEvent);
253     systemConfig_.windowUIType_ = WindowUIType::PC_WINDOW;
254 
255     ASSERT_TRUE(subSession_ != nullptr);
256     subSession_->SetSessionState(SessionState::STATE_BACKGROUND);
257     subSession_->UpdateSessionState(SessionState::STATE_INACTIVE);
258     pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_DOWN);
259     auto result = subSession_->CheckPointerEventDispatch(pointerEvent);
260     ASSERT_TRUE(result);
261 }
262 
263 /**
264  * @tc.name: IsModal01
265  * @tc.desc: check func IsModal
266  * @tc.type: FUNC
267  */
HWTEST_F(SubSessionTest, IsModal, Function | SmallTest | Level1)268 HWTEST_F(SubSessionTest, IsModal, Function | SmallTest | Level1)
269 {
270     ASSERT_FALSE(subSession_->IsModal());
271 
272     subSession_->SetSessionProperty(nullptr);
273     ASSERT_TRUE(subSession_->GetSessionProperty() == nullptr);
274 
275     ASSERT_FALSE(subSession_->IsModal());
276 }
277 
278 /**
279  * @tc.name: IsVisibleForeground01
280  * @tc.desc: check func IsVisibleForeground
281  * @tc.type: FUNC
282  */
HWTEST_F(SubSessionTest, IsVisibleForeground01, Function | SmallTest | Level1)283 HWTEST_F(SubSessionTest, IsVisibleForeground01, Function | SmallTest | Level1)
284 {
285     ASSERT_FALSE(subSession_->IsVisibleForeground());
286 
287     SessionInfo info;
288     info.abilityName_ = "testMainSession1";
289     info.moduleName_ = "testMainSession2";
290     info.bundleName_ = "testMainSession3";
291     auto parentSession = new SubSession(info, specificCallback);
292 
293     subSession_->SetParentSession(parentSession);
294     ASSERT_FALSE(subSession_->IsVisibleForeground());
295 }
296 
297 /**
298  * @tc.name: RectCheck
299  * @tc.desc: test function : RectCheck
300  * @tc.type: FUNC
301  */
HWTEST_F(SubSessionTest, RectCheck, Function | SmallTest | Level1)302 HWTEST_F(SubSessionTest, RectCheck, Function | SmallTest | Level1)
303 {
304     ASSERT_NE(subSession_, nullptr);
305     SessionInfo info;
306     info.abilityName_ = "testRectCheck";
307     info.moduleName_ = "testRectCheck";
308     info.bundleName_ = "testRectCheck";
309     sptr<Session> session = new (std::nothrow) Session(info);
310     EXPECT_NE(nullptr, session);
311     subSession_->parentSession_ = session;
312     uint32_t curWidth = 100;
313     uint32_t curHeight = 200;
314     subSession_->RectCheck(curWidth, curHeight);
315 
316     curWidth = 300;
317     curHeight = 200;
318     subSession_->RectCheck(curWidth, curHeight);
319 
320     curWidth = 1930;
321     curHeight = 200;
322     subSession_->RectCheck(curWidth, curHeight);
323 
324     curWidth = 330;
325     curHeight = 200;
326     subSession_->RectCheck(curWidth, curHeight);
327 
328     curWidth = 330;
329     curHeight = 1930;
330     subSession_->RectCheck(curWidth, curHeight);
331 }
332 }
333 }
334 }