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  */
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  */
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  */
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  */
129 HWTEST_F(SubSessionTest, TransferKeyEvent04, Function | SmallTest | Level1)
130 {
131     std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
132     ASSERT_NE(keyEvent, nullptr);
133 
134     subSession_->SetParentSession(subSession_);
135     subSession_->SetSessionState(SessionState::STATE_CONNECT);
136     ASSERT_EQ(WSError::WS_ERROR_NULLPTR, subSession_->TransferKeyEvent(keyEvent));
137 }
138 
139 /**
140  * @tc.name: IsTopmost01
141  * @tc.desc: check func IsTopmost
142  * @tc.type: FUNC
143  */
144 HWTEST_F(SubSessionTest, IsTopmost01, Function | SmallTest | Level1)
145 {
146     subSession_->GetSessionProperty()->SetTopmost(false);
147     ASSERT_EQ(false, subSession_->IsTopmost());
148 
149     subSession_->GetSessionProperty()->SetTopmost(true);
150     ASSERT_EQ(true, subSession_->IsTopmost());
151 }
152 
153 /**
154  * @tc.name: IsTopmost02
155  * @tc.desc: check func IsTopmost
156  * @tc.type: FUNC
157  */
158 HWTEST_F(SubSessionTest, IsTopmost02, Function | SmallTest | Level1)
159 {
160     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
161     subSession_->SetSessionProperty(property);
162     ASSERT_TRUE(subSession_->GetSessionProperty() != nullptr);
163 
164     subSession_->GetSessionProperty()->SetTopmost(true);
165     ASSERT_EQ(true, subSession_->IsTopmost());
166 }
167 
168 /**
169  * @tc.name: CheckPointerEventDispatch01
170  * @tc.desc: check func CheckPointerEventDispatch
171  * @tc.type: FUNC
172  */
173 HWTEST_F(SubSessionTest, CheckPointerEventDispatch01, Function | SmallTest | Level1)
174 {
175     std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
176     ASSERT_NE(nullptr, pointerEvent);
177     systemConfig_.uiType_ = "phone";
178 
179     ASSERT_TRUE(subSession_ != nullptr);
180     auto result = subSession_->CheckPointerEventDispatch(pointerEvent);
181     ASSERT_TRUE(result);
182 }
183 
184 /**
185  * @tc.name: CheckPointerEventDispatch02
186  * @tc.desc: check func CheckPointerEventDispatch
187  * @tc.type: FUNC
188  */
189 HWTEST_F(SubSessionTest, CheckPointerEventDispatch02, Function | SmallTest | Level1)
190 {
191     std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
192     ASSERT_NE(nullptr, pointerEvent);
193     systemConfig_.uiType_ = "pc";
194 
195     ASSERT_TRUE(subSession_ != nullptr);
196     subSession_->SetSessionState(SessionState::STATE_FOREGROUND);
197     auto result = subSession_->CheckPointerEventDispatch(pointerEvent);
198     ASSERT_TRUE(result);
199 }
200 
201 /**
202  * @tc.name: CheckPointerEventDispatch03
203  * @tc.desc: check func CheckPointerEventDispatch
204  * @tc.type: FUNC
205  */
206 HWTEST_F(SubSessionTest, CheckPointerEventDispatch03, Function | SmallTest | Level1)
207 {
208     std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
209     ASSERT_NE(nullptr, pointerEvent);
210     systemConfig_.uiType_ = "pc";
211 
212     ASSERT_TRUE(subSession_ != nullptr);
213     subSession_->SetSessionState(SessionState::STATE_BACKGROUND);
214     subSession_->UpdateSessionState(SessionState::STATE_ACTIVE);
215     auto result = subSession_->CheckPointerEventDispatch(pointerEvent);
216     ASSERT_TRUE(result);
217 }
218 
219 /**
220  * @tc.name: CheckPointerEventDispatch04
221  * @tc.desc: check func CheckPointerEventDispatch
222  * @tc.type: FUNC
223  */
224 HWTEST_F(SubSessionTest, CheckPointerEventDispatch04, Function | SmallTest | Level1)
225 {
226     std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
227     ASSERT_NE(nullptr, pointerEvent);
228     systemConfig_.uiType_ = "pc";
229 
230     ASSERT_TRUE(subSession_ != nullptr);
231     subSession_->SetSessionState(SessionState::STATE_BACKGROUND);
232     subSession_->UpdateSessionState(SessionState::STATE_INACTIVE);
233     pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_LEAVE_WINDOW);
234     auto result = subSession_->CheckPointerEventDispatch(pointerEvent);
235     ASSERT_TRUE(result);
236 }
237 
238 /**
239  * @tc.name: CheckPointerEventDispatch05
240  * @tc.desc: check func CheckPointerEventDispatch
241  * @tc.type: FUNC
242  */
243 HWTEST_F(SubSessionTest, CheckPointerEventDispatch05, Function | SmallTest | Level1)
244 {
245     std::shared_ptr<MMI::PointerEvent> pointerEvent = MMI::PointerEvent::Create();
246     ASSERT_NE(nullptr, pointerEvent);
247     systemConfig_.uiType_ = "pc";
248 
249     ASSERT_TRUE(subSession_ != nullptr);
250     subSession_->SetSessionState(SessionState::STATE_BACKGROUND);
251     subSession_->UpdateSessionState(SessionState::STATE_INACTIVE);
252     pointerEvent->SetPointerAction(MMI::PointerEvent::POINTER_ACTION_DOWN);
253     auto result = subSession_->CheckPointerEventDispatch(pointerEvent);
254     ASSERT_TRUE(result);
255 }
256 
257 /**
258  * @tc.name: IsModal01
259  * @tc.desc: check func IsModal
260  * @tc.type: FUNC
261  */
262 HWTEST_F(SubSessionTest, IsModal, Function | SmallTest | Level1)
263 {
264     ASSERT_FALSE(subSession_->IsModal());
265 }
266 
267 /**
268  * @tc.name: RectCheck
269  * @tc.desc: test function : RectCheck
270  * @tc.type: FUNC
271  */
272 HWTEST_F(SubSessionTest, RectCheck, Function | SmallTest | Level1)
273 {
274     ASSERT_NE(subSession_, nullptr);
275     SessionInfo info;
276     info.abilityName_ = "testRectCheck";
277     info.moduleName_ = "testRectCheck";
278     info.bundleName_ = "testRectCheck";
279     sptr<Session> session = new (std::nothrow) Session(info);
280     EXPECT_NE(nullptr, session);
281     subSession_->parentSession_ = session;
282     uint32_t curWidth = 100;
283     uint32_t curHeight = 200;
284     subSession_->RectCheck(curWidth, curHeight);
285 
286     curWidth = 300;
287     curHeight = 200;
288     subSession_->RectCheck(curWidth, curHeight);
289 
290     curWidth = 1930;
291     curHeight = 200;
292     subSession_->RectCheck(curWidth, curHeight);
293 
294     curWidth = 330;
295     curHeight = 200;
296     subSession_->RectCheck(curWidth, curHeight);
297 
298     curWidth = 330;
299     curHeight = 1930;
300     subSession_->RectCheck(curWidth, curHeight);
301 }
302 
303 /**
304  * @tc.name: IsModal
305  * @tc.desc: IsModal function01
306  * @tc.type: FUNC
307  */
308 HWTEST_F(SubSessionTest, IsModal01, Function | SmallTest | Level2)
309 {
310     sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
311     property->SetWindowType(WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
312     property->AddWindowFlag(WindowFlag::WINDOW_FLAG_IS_MODAL);
313     EXPECT_EQ(subSession_->IsModal(), false);
314     subSession_->SetSessionProperty(property);
315     EXPECT_EQ(subSession_->IsModal(), true);
316 }
317 
318 /**
319  * @tc.name: IsApplicationModal
320  * @tc.desc: IsApplicationModal function01
321  * @tc.type: FUNC
322  */
323 HWTEST_F(SubSessionTest, IsApplicationModal, Function | SmallTest | Level2)
324 {
325     sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
326     property->SetWindowType(WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
327     property->AddWindowFlag(WindowFlag::WINDOW_FLAG_IS_MODAL);
328     property->AddWindowFlag(WindowFlag::WINDOW_FLAG_IS_APPLICATION_MODAL);
329     EXPECT_EQ(subSession_->IsApplicationModal(), false);
330     subSession_->SetSessionProperty(property);
331     EXPECT_EQ(subSession_->IsApplicationModal(), true);
332 }
333 }
334 }
335 }