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 SessionStubLifecycleTest : 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 SessionStubLifecycleTest::SetUpTestCase()
52 {
53 }
54 
TearDownTestCase()55 void SessionStubLifecycleTest::TearDownTestCase()
56 {
57 }
58 
SetUp()59 void SessionStubLifecycleTest::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 SessionStubLifecycleTest::TearDown()
70 {
71     subSession_ = nullptr;
72 }
73 
CreateRSSurfaceNode()74 RSSurfaceNode::SharedPtr SessionStubLifecycleTest::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: Reconnect01
86  * @tc.desc: check func Reconnect
87  * @tc.type: FUNC
88  */
HWTEST_F(SessionStubLifecycleTest, Reconnect01, Function | SmallTest | Level1)89 HWTEST_F(SessionStubLifecycleTest, Reconnect01, Function | SmallTest | Level1)
90 {
91     auto surfaceNode = CreateRSSurfaceNode();
92     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
93     ASSERT_NE(nullptr, property);
94     sptr<SessionStageMocker> mockSessionStage = new (std::nothrow) SessionStageMocker();
95     EXPECT_NE(nullptr, mockSessionStage);
96     sptr<TestWindowEventChannel> testWindowEventChannel = new (std::nothrow) TestWindowEventChannel();
97     EXPECT_NE(nullptr, testWindowEventChannel);
98 
99     auto result = subSession_->Reconnect(nullptr, nullptr, nullptr, property);
100     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
101 
102     result = subSession_->Reconnect(nullptr, testWindowEventChannel, surfaceNode, property);
103     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
104 
105     result = subSession_->Reconnect(mockSessionStage, nullptr, surfaceNode, property);
106     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
107 
108     result = subSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, nullptr);
109     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
110 
111     result = subSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
112     ASSERT_EQ(result, WSError::WS_ERROR_INVALID_PARAM);
113 
114     property->SetWindowState(WindowState::STATE_INITIAL);
115     result = subSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
116     ASSERT_EQ(result, WSError::WS_ERROR_INVALID_PARAM);
117 
118     property->SetWindowState(WindowState::STATE_CREATED);
119     result = subSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
120     ASSERT_EQ(result, WSError::WS_OK);
121 
122     property->SetWindowState(WindowState::STATE_SHOWN);
123     result = subSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
124     ASSERT_EQ(result, WSError::WS_OK);
125 
126     property->SetWindowState(WindowState::STATE_HIDDEN);
127     result = subSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
128     ASSERT_EQ(result, WSError::WS_OK);
129 
130     property->SetWindowState(WindowState::STATE_FROZEN);
131     result = subSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
132     ASSERT_EQ(result, WSError::WS_ERROR_INVALID_PARAM);
133 }
134 
135 /**
136  * @tc.name: Hide01
137  * @tc.desc: check func Hide
138  * @tc.type: FUNC
139  */
HWTEST_F(SessionStubLifecycleTest, Hide01, Function | SmallTest | Level1)140 HWTEST_F(SessionStubLifecycleTest, Hide01, Function | SmallTest | Level1)
141 {
142     subSession_->Hide();
143     subSession_->GetMissionId();
144 
145     subSession_->isActive_ = true;
146     ASSERT_EQ(WSError::WS_OK, subSession_->Hide());
147     subSession_->isActive_ = false;
148 
149     subSession_->sessionInfo_.isSystem_ = true;
150     ASSERT_EQ(WSError::WS_OK, subSession_->Hide());
151     subSession_->sessionInfo_.isSystem_ = false;
152 
153     sptr<ISessionStage> tempStage_ = subSession_->sessionStage_;
154     subSession_->sessionStage_ = nullptr;
155     ASSERT_EQ(WSError::WS_OK, subSession_->Hide());
156     subSession_->sessionStage_ = tempStage_;
157 
158     WSRect rect;
159     subSession_->UpdatePointerArea(rect);
160     subSession_->RectCheck(50, 100);
161     ASSERT_EQ(WSError::WS_OK, subSession_->ProcessPointDownSession(50, 100));
162 }
163 
164 /**
165  * @tc.name: Hide02
166  * @tc.desc: check func Hide
167  * @tc.type: FUNC
168  */
HWTEST_F(SessionStubLifecycleTest, Hide02, Function | SmallTest | Level1)169 HWTEST_F(SessionStubLifecycleTest, Hide02, Function | SmallTest | Level1)
170 {
171     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
172     ASSERT_NE(nullptr, property);
173     property->SetWindowType(WindowType::APP_MAIN_WINDOW_BASE);
174     ASSERT_TRUE(subSession_ != nullptr);
175     subSession_->SetSessionProperty(property);
176     auto result = subSession_->Hide();
177     ASSERT_EQ(result, WSError::WS_OK);
178 }
179 
180 /**
181  * @tc.name: Hide03
182  * @tc.desc: check func Hide
183  * @tc.type: FUNC
184  */
HWTEST_F(SessionStubLifecycleTest, Hide03, Function | SmallTest | Level1)185 HWTEST_F(SessionStubLifecycleTest, Hide03, Function | SmallTest | Level1)
186 {
187     sptr<WindowSessionProperty> property = nullptr;
188     ASSERT_TRUE(subSession_ != nullptr);
189     subSession_->SetSessionProperty(property);
190     auto result = subSession_->Hide();
191     ASSERT_EQ(result, WSError::WS_OK);
192 }
193 
194 /**
195  * @tc.name: Hide04
196  * @tc.desc: check func Hide
197  * @tc.type: FUNC
198  */
HWTEST_F(SessionStubLifecycleTest, Hide04, Function | SmallTest | Level1)199 HWTEST_F(SessionStubLifecycleTest, Hide04, Function | SmallTest | Level1)
200 {
201     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
202     ASSERT_NE(nullptr, property);
203     sptr<WindowProperty> winPropSrc = new (std::nothrow) WindowProperty();
204     ASSERT_NE(nullptr, winPropSrc);
205     uint32_t animationFlag = 1;
206     winPropSrc->SetAnimationFlag(animationFlag);
207     uint32_t res = winPropSrc->GetAnimationFlag();
208     ASSERT_NE(res, static_cast<uint32_t>(WindowAnimation::CUSTOM));
209     ASSERT_TRUE(subSession_ != nullptr);
210     auto result = subSession_->Hide();
211     ASSERT_EQ(result, WSError::WS_OK);
212 
213     animationFlag = 3;
214     winPropSrc->SetAnimationFlag(animationFlag);
215     res = winPropSrc->GetAnimationFlag();
216     ASSERT_EQ(res, static_cast<uint32_t>(WindowAnimation::CUSTOM));
217     ASSERT_TRUE(subSession_ != nullptr);
218     result = subSession_->Hide();
219     ASSERT_EQ(result, WSError::WS_OK);
220 }
221 
222 /**
223  * @tc.name: Show01
224  * @tc.desc: check func Show
225  * @tc.type: FUNC
226  */
HWTEST_F(SessionStubLifecycleTest, Show01, Function | SmallTest | Level1)227 HWTEST_F(SessionStubLifecycleTest, Show01, Function | SmallTest | Level1)
228 {
229     sptr<WindowSessionProperty> property = nullptr;
230     ASSERT_EQ(nullptr, property);
231 
232     ASSERT_TRUE(subSession_ != nullptr);
233     ASSERT_EQ(WSError::WS_OK, subSession_->Show(property));
234 }
235 
236 /**
237  * @tc.name: Show02
238  * @tc.desc: check func Show
239  * @tc.type: FUNC
240  */
HWTEST_F(SessionStubLifecycleTest, Show02, Function | SmallTest | Level1)241 HWTEST_F(SessionStubLifecycleTest, Show02, Function | SmallTest | Level1)
242 {
243     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
244     ASSERT_NE(nullptr, property);
245 
246     sptr<WindowProperty> winPropSrc = new (std::nothrow) WindowProperty();
247     ASSERT_NE(nullptr, winPropSrc);
248     uint32_t animationFlag = 1;
249     winPropSrc->SetAnimationFlag(animationFlag);
250     uint32_t res = winPropSrc->GetAnimationFlag();
251     ASSERT_NE(res, static_cast<uint32_t>(WindowAnimation::CUSTOM));
252 
253     ASSERT_TRUE(subSession_ != nullptr);
254     auto result = subSession_->Show(property);
255     ASSERT_EQ(result, WSError::WS_OK);
256 }
257 
258 /**
259  * @tc.name: Show03
260  * @tc.desc: check func Show
261  * @tc.type: FUNC
262  */
HWTEST_F(SessionStubLifecycleTest, Show03, Function | SmallTest | Level1)263 HWTEST_F(SessionStubLifecycleTest, Show03, Function | SmallTest | Level1)
264 {
265     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
266     ASSERT_NE(nullptr, property);
267 
268     sptr<WindowProperty> winPropSrc = new (std::nothrow) WindowProperty();
269     ASSERT_NE(nullptr, winPropSrc);
270     uint32_t animationFlag = 3;
271     winPropSrc->SetAnimationFlag(animationFlag);
272     uint32_t res = winPropSrc->GetAnimationFlag();
273     ASSERT_EQ(res, static_cast<uint32_t>(WindowAnimation::CUSTOM));
274 
275     ASSERT_TRUE(subSession_ != nullptr);
276     auto result = subSession_->Show(property);
277     ASSERT_EQ(result, WSError::WS_OK);
278 }
279 /**
280  * @tc.name: Show04
281  * @tc.desc: check func Show
282  * @tc.type: FUNC
283  */
HWTEST_F(SessionStubLifecycleTest, Show04, Function | SmallTest | Level1)284 HWTEST_F(SessionStubLifecycleTest, Show04, Function | SmallTest | Level1)
285 {
286     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
287     ASSERT_NE(property, nullptr);
288     property->SetAnimationFlag(static_cast<uint32_t>(WindowAnimation::DEFAULT));
289     ASSERT_EQ(subSession_->Show(property), WSError::WS_OK);
290 
291     property->SetAnimationFlag(static_cast<uint32_t>(WindowAnimation::CUSTOM));
292     ASSERT_EQ(subSession_->Show(property), WSError::WS_OK);
293 
294     subSession_->SetSessionProperty(property);
295     ASSERT_EQ(subSession_->Show(property), WSError::WS_OK);
296 }
297 /**
298  * @tc.name: ProcessPointDownSession01
299  * @tc.desc: check func ProcessPointDownSession
300  * @tc.type: FUNC
301  */
HWTEST_F(SessionStubLifecycleTest, ProcessPointDownSession01, Function | SmallTest | Level1)302 HWTEST_F(SessionStubLifecycleTest, ProcessPointDownSession01, Function | SmallTest | Level1)
303 {
304     subSession_->Hide();
305     subSession_->SetParentSession(subSession_);
306     ASSERT_TRUE(subSession_->GetParentSession() != nullptr);
307 
308     WSRect rect;
309     subSession_->UpdatePointerArea(rect);
310     subSession_->RectCheck(50, 100);
311     ASSERT_EQ(subSession_->ProcessPointDownSession(50, 100), WSError::WS_OK);
312 }
313 /**
314  * @tc.name: ProcessPointDownSession02
315  * @tc.desc: check func ProcessPointDownSession
316  * @tc.type: FUNC
317  */
HWTEST_F(SessionStubLifecycleTest, ProcessPointDownSession02, Function | SmallTest | Level1)318 HWTEST_F(SessionStubLifecycleTest, ProcessPointDownSession02, Function | SmallTest | Level1)
319 {
320     subSession_->Hide();
321     WSRect rect;
322     subSession_->UpdatePointerArea(rect);
323     subSession_->RectCheck(50, 100);
324 
325     auto property = subSession_->GetSessionProperty();
326     ASSERT_NE(property, nullptr);
327     property->SetRaiseEnabled(false);
328     ASSERT_FALSE(subSession_->GetSessionProperty()->GetRaiseEnabled());
329     ASSERT_EQ(subSession_->ProcessPointDownSession(50, 100), WSError::WS_OK);
330 }
331 }
332 }
333 }