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_ = sptr<SubSession>::MakeSptr(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 = sptr<WindowSessionProperty>::MakeSptr();
93 ASSERT_NE(nullptr, property);
94 sptr<SessionStageMocker> mockSessionStage = sptr<SessionStageMocker>::MakeSptr();
95 EXPECT_NE(nullptr, mockSessionStage);
96 sptr<TestWindowEventChannel> testWindowEventChannel = sptr<TestWindowEventChannel>::MakeSptr();
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 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 */
169 HWTEST_F(SessionStubLifecycleTest, Hide02, Function | SmallTest | Level1)
170 {
171 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
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: Hide04
182 * @tc.desc: check func Hide
183 * @tc.type: FUNC
184 */
185 HWTEST_F(SessionStubLifecycleTest, Hide04, Function | SmallTest | Level1)
186 {
187 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
188 ASSERT_NE(nullptr, property);
189 sptr<WindowProperty> winPropSrc = sptr<WindowProperty>::MakeSptr();
190 ASSERT_NE(nullptr, winPropSrc);
191 uint32_t animationFlag = 1;
192 winPropSrc->SetAnimationFlag(animationFlag);
193 uint32_t res = winPropSrc->GetAnimationFlag();
194 ASSERT_NE(res, static_cast<uint32_t>(WindowAnimation::CUSTOM));
195 ASSERT_TRUE(subSession_ != nullptr);
196 auto result = subSession_->Hide();
197 ASSERT_EQ(result, WSError::WS_OK);
198
199 animationFlag = 3;
200 winPropSrc->SetAnimationFlag(animationFlag);
201 res = winPropSrc->GetAnimationFlag();
202 ASSERT_EQ(res, static_cast<uint32_t>(WindowAnimation::CUSTOM));
203 ASSERT_TRUE(subSession_ != nullptr);
204 result = subSession_->Hide();
205 ASSERT_EQ(result, WSError::WS_OK);
206 }
207
208 /**
209 * @tc.name: Show02
210 * @tc.desc: check func Show
211 * @tc.type: FUNC
212 */
213 HWTEST_F(SessionStubLifecycleTest, Show02, Function | SmallTest | Level1)
214 {
215 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
216 ASSERT_NE(nullptr, property);
217
218 sptr<WindowProperty> winPropSrc = sptr<WindowProperty>::MakeSptr();
219 ASSERT_NE(nullptr, winPropSrc);
220 uint32_t animationFlag = 1;
221 winPropSrc->SetAnimationFlag(animationFlag);
222 uint32_t res = winPropSrc->GetAnimationFlag();
223 ASSERT_NE(res, static_cast<uint32_t>(WindowAnimation::CUSTOM));
224
225 ASSERT_TRUE(subSession_ != nullptr);
226 auto result = subSession_->Show(property);
227 ASSERT_EQ(result, WSError::WS_OK);
228 }
229
230 /**
231 * @tc.name: Show03
232 * @tc.desc: check func Show
233 * @tc.type: FUNC
234 */
235 HWTEST_F(SessionStubLifecycleTest, Show03, Function | SmallTest | Level1)
236 {
237 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
238 ASSERT_NE(nullptr, property);
239
240 sptr<WindowProperty> winPropSrc = sptr<WindowProperty>::MakeSptr();
241 ASSERT_NE(nullptr, winPropSrc);
242 uint32_t animationFlag = 3;
243 winPropSrc->SetAnimationFlag(animationFlag);
244 uint32_t res = winPropSrc->GetAnimationFlag();
245 ASSERT_EQ(res, static_cast<uint32_t>(WindowAnimation::CUSTOM));
246
247 ASSERT_TRUE(subSession_ != nullptr);
248 auto result = subSession_->Show(property);
249 ASSERT_EQ(result, WSError::WS_OK);
250 }
251
252 /**
253 * @tc.name: Show04
254 * @tc.desc: check func Show
255 * @tc.type: FUNC
256 */
257 HWTEST_F(SessionStubLifecycleTest, Show04, Function | SmallTest | Level1)
258 {
259 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
260 ASSERT_NE(property, nullptr);
261 property->SetAnimationFlag(static_cast<uint32_t>(WindowAnimation::DEFAULT));
262 ASSERT_EQ(subSession_->Show(property), WSError::WS_OK);
263
264 property->SetAnimationFlag(static_cast<uint32_t>(WindowAnimation::CUSTOM));
265 ASSERT_EQ(subSession_->Show(property), WSError::WS_OK);
266
267 subSession_->SetSessionProperty(property);
268 ASSERT_EQ(subSession_->Show(property), WSError::WS_OK);
269 }
270
271 /**
272 * @tc.name: ProcessPointDownSession01
273 * @tc.desc: check func ProcessPointDownSession
274 * @tc.type: FUNC
275 */
276 HWTEST_F(SessionStubLifecycleTest, ProcessPointDownSession01, Function | SmallTest | Level1)
277 {
278 subSession_->Hide();
279 subSession_->SetParentSession(subSession_);
280 ASSERT_TRUE(subSession_->GetParentSession() != nullptr);
281
282 WSRect rect;
283 subSession_->UpdatePointerArea(rect);
284 subSession_->RectCheck(50, 100);
285 ASSERT_EQ(subSession_->ProcessPointDownSession(50, 100), WSError::WS_OK);
286 }
287
288 /**
289 * @tc.name: ProcessPointDownSession02
290 * @tc.desc: check func ProcessPointDownSession
291 * @tc.type: FUNC
292 */
293 HWTEST_F(SessionStubLifecycleTest, ProcessPointDownSession02, Function | SmallTest | Level1)
294 {
295 subSession_->Hide();
296 WSRect rect;
297 subSession_->UpdatePointerArea(rect);
298 subSession_->RectCheck(50, 100);
299
300 auto property = subSession_->GetSessionProperty();
301 ASSERT_NE(property, nullptr);
302 property->SetRaiseEnabled(false);
303 ASSERT_FALSE(subSession_->GetSessionProperty()->GetRaiseEnabled());
304 ASSERT_EQ(subSession_->ProcessPointDownSession(50, 100), WSError::WS_OK);
305 }
306 }
307 }
308 }