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  */
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  */
140 HWTEST_F(SessionStubLifecycleTest, Hide01, Function | SmallTest | Level1)
141 {
142     subSession_->Hide();
143     subSession_->GetMissionId();
144 
145     WSRect rect;
146     subSession_->UpdatePointerArea(rect);
147     subSession_->RectCheck(50, 100);
148     ASSERT_EQ(WSError::WS_OK, subSession_->ProcessPointDownSession(50, 100));
149 }
150 
151 /**
152  * @tc.name: Hide02
153  * @tc.desc: check func Hide
154  * @tc.type: FUNC
155  */
156 HWTEST_F(SessionStubLifecycleTest, Hide02, Function | SmallTest | Level1)
157 {
158     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
159     ASSERT_NE(nullptr, property);
160     property->SetWindowType(WindowType::APP_MAIN_WINDOW_BASE);
161     ASSERT_TRUE(subSession_ != nullptr);
162     subSession_->SetSessionProperty(property);
163     auto result = subSession_->Hide();
164     ASSERT_EQ(result, WSError::WS_OK);
165 }
166 
167 /**
168  * @tc.name: Hide03
169  * @tc.desc: check func Hide
170  * @tc.type: FUNC
171  */
172 HWTEST_F(SessionStubLifecycleTest, Hide03, Function | SmallTest | Level1)
173 {
174     sptr<WindowSessionProperty> property = nullptr;
175     ASSERT_TRUE(subSession_ != nullptr);
176     subSession_->SetSessionProperty(property);
177     auto result = subSession_->Hide();
178     ASSERT_EQ(result, WSError::WS_OK);
179 }
180 
181 /**
182  * @tc.name: Hide04
183  * @tc.desc: check func Hide
184  * @tc.type: FUNC
185  */
186 HWTEST_F(SessionStubLifecycleTest, Hide04, Function | SmallTest | Level1)
187 {
188     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
189     ASSERT_NE(nullptr, property);
190     sptr<WindowProperty> winPropSrc = new (std::nothrow) WindowProperty();
191     ASSERT_NE(nullptr, winPropSrc);
192     uint32_t animationFlag = 1;
193     winPropSrc->SetAnimationFlag(animationFlag);
194     uint32_t res = winPropSrc->GetAnimationFlag();
195     ASSERT_NE(res, static_cast<uint32_t>(WindowAnimation::CUSTOM));
196     ASSERT_TRUE(subSession_ != nullptr);
197     auto result = subSession_->Hide();
198     ASSERT_EQ(result, WSError::WS_OK);
199 
200     animationFlag = 3;
201     winPropSrc->SetAnimationFlag(animationFlag);
202     res = winPropSrc->GetAnimationFlag();
203     ASSERT_EQ(res, static_cast<uint32_t>(WindowAnimation::CUSTOM));
204     ASSERT_TRUE(subSession_ != nullptr);
205     result = subSession_->Hide();
206     ASSERT_EQ(result, WSError::WS_OK);
207 }
208 
209 /**
210  * @tc.name: Show01
211  * @tc.desc: check func Show
212  * @tc.type: FUNC
213  */
214 HWTEST_F(SessionStubLifecycleTest, Show01, Function | SmallTest | Level1)
215 {
216     sptr<WindowSessionProperty> property = nullptr;
217     ASSERT_EQ(nullptr, property);
218 
219     ASSERT_TRUE(subSession_ != nullptr);
220     ASSERT_EQ(WSError::WS_OK, subSession_->Show(property));
221 }
222 
223 /**
224  * @tc.name: Show02
225  * @tc.desc: check func Show
226  * @tc.type: FUNC
227  */
228 HWTEST_F(SessionStubLifecycleTest, Show02, Function | SmallTest | Level1)
229 {
230     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
231     ASSERT_NE(nullptr, property);
232 
233     sptr<WindowProperty> winPropSrc = new (std::nothrow) WindowProperty();
234     ASSERT_NE(nullptr, winPropSrc);
235     uint32_t animationFlag = 1;
236     winPropSrc->SetAnimationFlag(animationFlag);
237     uint32_t res = winPropSrc->GetAnimationFlag();
238     ASSERT_NE(res, static_cast<uint32_t>(WindowAnimation::CUSTOM));
239 
240     ASSERT_TRUE(subSession_ != nullptr);
241     auto result = subSession_->Show(property);
242     ASSERT_EQ(result, WSError::WS_OK);
243 }
244 
245 /**
246  * @tc.name: Show03
247  * @tc.desc: check func Show
248  * @tc.type: FUNC
249  */
250 HWTEST_F(SessionStubLifecycleTest, Show03, Function | SmallTest | Level1)
251 {
252     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
253     ASSERT_NE(nullptr, property);
254 
255     sptr<WindowProperty> winPropSrc = new (std::nothrow) WindowProperty();
256     ASSERT_NE(nullptr, winPropSrc);
257     uint32_t animationFlag = 3;
258     winPropSrc->SetAnimationFlag(animationFlag);
259     uint32_t res = winPropSrc->GetAnimationFlag();
260     ASSERT_EQ(res, static_cast<uint32_t>(WindowAnimation::CUSTOM));
261 
262     ASSERT_TRUE(subSession_ != nullptr);
263     auto result = subSession_->Show(property);
264     ASSERT_EQ(result, WSError::WS_OK);
265 }
266 /**
267  * @tc.name: Show04
268  * @tc.desc: check func Show
269  * @tc.type: FUNC
270  */
271 HWTEST_F(SessionStubLifecycleTest, Show04, Function | SmallTest | Level1)
272 {
273     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
274     ASSERT_NE(property, nullptr);
275     property->SetAnimationFlag(static_cast<uint32_t>(WindowAnimation::DEFAULT));
276     ASSERT_EQ(subSession_->Show(property), WSError::WS_OK);
277 
278     property->SetAnimationFlag(static_cast<uint32_t>(WindowAnimation::CUSTOM));
279     ASSERT_EQ(subSession_->Show(property), WSError::WS_OK);
280 
281     subSession_->SetSessionProperty(property);
282     ASSERT_EQ(subSession_->Show(property), WSError::WS_OK);
283 }
284 /**
285  * @tc.name: ProcessPointDownSession01
286  * @tc.desc: check func ProcessPointDownSession
287  * @tc.type: FUNC
288  */
289 HWTEST_F(SessionStubLifecycleTest, ProcessPointDownSession01, Function | SmallTest | Level1)
290 {
291     subSession_->Hide();
292     subSession_->SetParentSession(subSession_);
293     ASSERT_TRUE(subSession_->GetParentSession() != nullptr);
294 
295     WSRect rect;
296     subSession_->UpdatePointerArea(rect);
297     subSession_->RectCheck(50, 100);
298     ASSERT_EQ(subSession_->ProcessPointDownSession(50, 100), WSError::WS_OK);
299 }
300 /**
301  * @tc.name: ProcessPointDownSession02
302  * @tc.desc: check func ProcessPointDownSession
303  * @tc.type: FUNC
304  */
305 HWTEST_F(SessionStubLifecycleTest, ProcessPointDownSession02, Function | SmallTest | Level1)
306 {
307     subSession_->Hide();
308     WSRect rect;
309     subSession_->UpdatePointerArea(rect);
310     subSession_->RectCheck(50, 100);
311 
312     auto property = subSession_->GetSessionProperty();
313     ASSERT_NE(property, nullptr);
314     property->SetRaiseEnabled(false);
315     ASSERT_FALSE(subSession_->GetSessionProperty()->GetRaiseEnabled());
316     ASSERT_EQ(subSession_->ProcessPointDownSession(50, 100), WSError::WS_OK);
317 }
318 }
319 }
320 }