1 /*
2 * Copyright (c) 2023 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 <parameters.h>
18 #include "display_info.h"
19 #include "ability_context_impl.h"
20 #include "mock_session.h"
21 #include "window_session_impl.h"
22 #include "mock_uicontent.h"
23 #include "window_scene_session_impl.h"
24 #include "mock_window_adapter.h"
25 #include "singleton_mocker.h"
26 #include "session/host/include/scene_session.h"
27
28 using namespace testing;
29 using namespace testing::ext;
30
31 namespace OHOS {
32 namespace Rosen {
33 using Mocker = SingletonMocker<WindowAdapter, MockWindowAdapter>;
34
35 class MockWindowChangeListener : public IWindowChangeListener {
36 public:
37 MOCK_METHOD3(OnSizeChange,
38 void(Rect rect, WindowSizeChangeReason reason, const std::shared_ptr<RSTransaction>& rsTransaction));
39 };
40
41 class MockWindowLifeCycleListener : public IWindowLifeCycle {
42 public:
43 MOCK_METHOD0(AfterForeground, void(void));
44 MOCK_METHOD0(AfterBackground, void(void));
45 MOCK_METHOD0(AfterFocused, void(void));
46 MOCK_METHOD0(AfterUnfocused, void(void));
47 MOCK_METHOD1(ForegroundFailed, void(int32_t));
48 MOCK_METHOD0(AfterActive, void(void));
49 MOCK_METHOD0(AfterInactive, void(void));
50 MOCK_METHOD0(AfterResumed, void(void));
51 MOCK_METHOD0(AfterPaused, void(void));
52 };
53
54 class WindowSceneSessionImplSpecialWindowTest : public testing::Test {
55 public:
56 static void SetUpTestCase();
57 static void TearDownTestCase();
58 void SetUp() override;
59 void TearDown() override;
60
61 std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext_;
62 std::unique_ptr<Mocker> m = std::make_unique<Mocker>();
63
64 private:
65 RSSurfaceNode::SharedPtr CreateRSSurfaceNode();
66 static constexpr uint32_t WAIT_SYNC_IN_NS = 200000;
67 };
68
SetUpTestCase()69 void WindowSceneSessionImplSpecialWindowTest::SetUpTestCase() {}
70
TearDownTestCase()71 void WindowSceneSessionImplSpecialWindowTest::TearDownTestCase() {}
72
SetUp()73 void WindowSceneSessionImplSpecialWindowTest::SetUp()
74 {
75 abilityContext_ = std::make_shared<AbilityRuntime::AbilityContextImpl>();
76 }
77
TearDown()78 void WindowSceneSessionImplSpecialWindowTest::TearDown()
79 {
80 usleep(WAIT_SYNC_IN_NS);
81 abilityContext_ = nullptr;
82 }
83
CreateRSSurfaceNode()84 RSSurfaceNode::SharedPtr WindowSceneSessionImplSpecialWindowTest::CreateRSSurfaceNode()
85 {
86 struct RSSurfaceNodeConfig rsSurfaceNodeConfig;
87 rsSurfaceNodeConfig.SurfaceNodeName = "startingWindowTestSurfaceNode";
88 auto surfaceNode = RSSurfaceNode::Create(rsSurfaceNodeConfig, RSSurfaceNodeType::DEFAULT);
89 return surfaceNode;
90 }
91
92 namespace {
93 /**
94 * @tc.name: CreateAndConnectSpecificSession01
95 * @tc.desc: CreateAndConnectSpecificSession
96 * @tc.type: FUNC
97 */
98 HWTEST_F(WindowSceneSessionImplSpecialWindowTest, CreateAndConnectSpecificSession01, TestSize.Level1)
99 {
100 sptr<WindowOption> option = new (std::nothrow) WindowOption();
101 ASSERT_NE(nullptr, option);
102 option->SetWindowName("CreateAndConnectSpecificSession01");
103 sptr<WindowSceneSessionImpl> windowSceneSession = new (std::nothrow) WindowSceneSessionImpl(option);
104 ASSERT_NE(nullptr, windowSceneSession);
105
106 windowSceneSession->SetWindowType(WindowType::APP_MAIN_WINDOW_BASE);
107 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, windowSceneSession->CreateAndConnectSpecificSession());
108 windowSceneSession->property_->SetPersistentId(102);
109 windowSceneSession->property_->SetParentPersistentId(100);
110 windowSceneSession->property_->SetParentId(100);
111 SessionInfo sessionInfo = {"CreateTestBundle", "CreateTestModule", "CreateTestAbility"};
112 sptr<SessionMocker> session = new (std::nothrow) SessionMocker(sessionInfo);
113 ASSERT_NE(nullptr, session);
114
115 ASSERT_EQ(WMError::WM_OK, windowSceneSession->Create(abilityContext_, session));
116 ASSERT_EQ(WMError::WM_OK, windowSceneSession->Destroy(true));
117 }
118
119 /**
120 * @tc.name: CreateAndConnectSpecificSession02
121 * @tc.desc: CreateAndConnectSpecificSession
122 * @tc.type: FUNC
123 */
124 HWTEST_F(WindowSceneSessionImplSpecialWindowTest, CreateAndConnectSpecificSession02, TestSize.Level1)
125 {
126 sptr<WindowOption> option = new (std::nothrow) WindowOption();
127 ASSERT_NE(nullptr, option);
128 option->SetWindowTag(WindowTag::SUB_WINDOW);
129 option->SetWindowName("CreateAndConnectSpecificSession02");
130 sptr<WindowSceneSessionImpl> windowSceneSession = new (std::nothrow) WindowSceneSessionImpl(option);
131 ASSERT_NE(nullptr, windowSceneSession);
132
133 SessionInfo sessionInfo = { "CreateTestBundle0", "CreateTestModule0", "CreateTestAbility0" };
134 sptr<SessionMocker> session = new (std::nothrow) SessionMocker(sessionInfo);
135 ASSERT_NE(nullptr, session);
136 ASSERT_EQ(WMError::WM_OK, windowSceneSession->Create(abilityContext_, session));
137 windowSceneSession->property_->SetPersistentId(103);
138 windowSceneSession->property_->SetParentPersistentId(102);
139 windowSceneSession->property_->SetParentId(102);
140 windowSceneSession->hostSession_ = session;
141
142 windowSceneSession->property_->type_ = WindowType::APP_SUB_WINDOW_BASE;
143 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, windowSceneSession->CreateAndConnectSpecificSession());
144 ASSERT_EQ(WMError::WM_OK, windowSceneSession->Destroy(true));
145 }
146
147 /**
148 * @tc.name: CreateAndConnectSpecificSession03
149 * @tc.desc: CreateAndConnectSpecificSession
150 * @tc.type: FUNC
151 */
152 HWTEST_F(WindowSceneSessionImplSpecialWindowTest, CreateAndConnectSpecificSession03, TestSize.Level1)
153 {
154 sptr<WindowOption> option = new (std::nothrow) WindowOption();
155 ASSERT_NE(nullptr, option);
156 option->SetWindowTag(WindowTag::SUB_WINDOW);
157 option->SetWindowName("CreateAndConnectSpecificSession03");
158 option->SetIsUIExtFirstSubWindow(true);
159 sptr<WindowSceneSessionImpl> windowSceneSession = new (std::nothrow) WindowSceneSessionImpl(option);
160 ASSERT_NE(nullptr, windowSceneSession);
161
162 SessionInfo sessionInfo = { "CreateTestBundle0", "CreateTestModule0", "CreateTestAbility0" };
163 sptr<SessionMocker> session = new (std::nothrow) SessionMocker(sessionInfo);
164 ASSERT_NE(nullptr, session);
165 ASSERT_EQ(WMError::WM_OK, windowSceneSession->Create(abilityContext_, session));
166
167 windowSceneSession->property_->SetParentPersistentId(102);
168 windowSceneSession->property_->SetParentId(102);
169 windowSceneSession->hostSession_ = session;
170
171 windowSceneSession->property_->type_ = WindowType::APP_SUB_WINDOW_BASE;
172 ASSERT_EQ(WMError::WM_OK, windowSceneSession->CreateAndConnectSpecificSession());
173 ASSERT_EQ(WMError::WM_OK, windowSceneSession->Destroy(true));
174 }
175
176 /**
177 * @tc.name: CreateAndConnectSpecificSession04
178 * @tc.desc: CreateAndConnectSpecificSession
179 * @tc.type: FUNC
180 */
181 HWTEST_F(WindowSceneSessionImplSpecialWindowTest, CreateAndConnectSpecificSession04, TestSize.Level1)
182 {
183 sptr<WindowOption> option = new (std::nothrow) WindowOption();
184 ASSERT_NE(nullptr, option);
185 option->SetWindowTag(WindowTag::SUB_WINDOW);
186 option->SetWindowName("CreateAndConnectSpecificSession04");
187 sptr<WindowSceneSessionImpl> windowSceneSession = new (std::nothrow) WindowSceneSessionImpl(option);
188 ASSERT_NE(nullptr, windowSceneSession);
189
190 SessionInfo sessionInfo = { "CreateTestBundle4", "CreateTestModule4", "CreateTestAbility4" };
191 sptr<SessionMocker> session = new (std::nothrow) SessionMocker(sessionInfo);
192 ASSERT_NE(nullptr, session);
193 ASSERT_EQ(WMError::WM_OK, windowSceneSession->Create(abilityContext_, session));
194
195 windowSceneSession->property_->SetPersistentId(104);
196 windowSceneSession->property_->SetParentPersistentId(103);
197 windowSceneSession->property_->SetParentId(103);
198 windowSceneSession->property_->type_ = WindowType::APP_MAIN_WINDOW_BASE;
199 windowSceneSession->hostSession_ = session;
200
201 windowSceneSession->property_->type_ = WindowType::APP_SUB_WINDOW_BASE;
202 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, windowSceneSession->CreateAndConnectSpecificSession());
203 ASSERT_EQ(WMError::WM_OK, windowSceneSession->Destroy(true));
204 }
205
206 /**
207 * @tc.name: CreateAndConnectSpecificSession05
208 * @tc.desc: CreateAndConnectSpecificSession
209 * @tc.type: FUNC
210 */
211 HWTEST_F(WindowSceneSessionImplSpecialWindowTest, CreateAndConnectSpecificSession05, TestSize.Level1)
212 {
213 sptr<WindowOption> option = new (std::nothrow) WindowOption();
214 ASSERT_NE(nullptr, option);
215 option->SetWindowTag(WindowTag::SUB_WINDOW);
216 option->SetWindowName("CreateAndConnectSpecificSession05");
217 sptr<WindowSceneSessionImpl> windowSceneSession = new (std::nothrow) WindowSceneSessionImpl(option);
218 ASSERT_NE(nullptr, windowSceneSession);
219
220 SessionInfo sessionInfo = { "CreateTestBundle5", "CreateTestModule5", "CreateTestAbility5" };
221 sptr<SessionMocker> session = new (std::nothrow) SessionMocker(sessionInfo);
222 ASSERT_NE(nullptr, session);
223 ASSERT_EQ(WMError::WM_OK, windowSceneSession->Create(abilityContext_, session));
224
225 windowSceneSession->property_->SetParentPersistentId(104);
226 windowSceneSession->property_->SetParentId(104);
227 windowSceneSession->hostSession_ = session;
228
229 windowSceneSession->property_->type_ = WindowType::APP_SUB_WINDOW_BASE;
230 ASSERT_NE(WMError::WM_ERROR_INVALID_TYPE, windowSceneSession->CreateAndConnectSpecificSession());
231 ASSERT_NE(WMError::WM_OK, windowSceneSession->Destroy(true));
232 }
233
234 /**
235 * @tc.name: CreateAndConnectSpecificSession06
236 * @tc.desc: CreateAndConnectSpecificSession
237 * @tc.type: FUNC
238 */
239 HWTEST_F(WindowSceneSessionImplSpecialWindowTest, CreateAndConnectSpecificSession06, TestSize.Level1)
240 {
241 sptr<WindowOption> option = new (std::nothrow) WindowOption();
242 ASSERT_NE(nullptr, option);
243 option->SetWindowTag(WindowTag::SYSTEM_WINDOW);
244 option->SetWindowName("CreateAndConnectSpecificSession06");
245 sptr<WindowSceneSessionImpl> windowSceneSession = new (std::nothrow) WindowSceneSessionImpl(option);
246 ASSERT_NE(nullptr, windowSceneSession);
247
248 SessionInfo sessionInfo = { "CreateTestBundle6", "CreateTestModule6", "CreateTestAbility6" };
249 sptr<SessionMocker> session = new (std::nothrow) SessionMocker(sessionInfo);
250 ASSERT_NE(nullptr, session);
251 ASSERT_EQ(WMError::WM_OK, windowSceneSession->Create(abilityContext_, session));
252
253 windowSceneSession->property_->SetPersistentId(105);
254 windowSceneSession->property_->SetParentPersistentId(102);
255 windowSceneSession->property_->SetParentId(102);
256 windowSceneSession->hostSession_ = session;
257
258 windowSceneSession->property_->type_ = WindowType::SYSTEM_WINDOW_BASE;
259 ASSERT_EQ(WMError::WM_OK, windowSceneSession->CreateAndConnectSpecificSession());
260 ASSERT_EQ(WMError::WM_OK, windowSceneSession->Destroy(true));
261 }
262
263 /**
264 * @tc.name: CreateAndConnectSpecificSession07
265 * @tc.desc: CreateAndConnectSpecificSession
266 * @tc.type: FUNC
267 */
268 HWTEST_F(WindowSceneSessionImplSpecialWindowTest, CreateAndConnectSpecificSession07, TestSize.Level1)
269 {
270 sptr<WindowOption> option = new (std::nothrow) WindowOption();
271 ASSERT_NE(nullptr, option);
272 option->SetWindowTag(WindowTag::SYSTEM_WINDOW);
273 option->SetWindowName("CreateAndConnectSpecificSession07");
274 sptr<WindowSceneSessionImpl> windowSceneSession = new (std::nothrow) WindowSceneSessionImpl(option);
275 ASSERT_NE(nullptr, windowSceneSession);
276
277 SessionInfo sessionInfo = { "CreateTestBundle7", "CreateTestModule7", "CreateTestAbility7" };
278 sptr<SessionMocker> session = new (std::nothrow) SessionMocker(sessionInfo);
279 ASSERT_NE(nullptr, session);
280 ASSERT_EQ(WMError::WM_OK, windowSceneSession->Create(abilityContext_, session));
281
282 windowSceneSession->property_->SetPersistentId(106);
283 windowSceneSession->property_->SetParentPersistentId(105);
284 windowSceneSession->property_->SetParentId(105);
285 windowSceneSession->hostSession_ = session;
286
287 windowSceneSession->property_->type_ = WindowType::SYSTEM_SUB_WINDOW_BASE;
288 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, windowSceneSession->CreateAndConnectSpecificSession());
289 ASSERT_EQ(WMError::WM_OK, windowSceneSession->Destroy(true));
290 }
291
292 /**
293 * @tc.name: CreateAndConnectSpecificSession08
294 * @tc.desc: CreateAndConnectSpecificSession
295 * @tc.type: FUNC
296 */
297 HWTEST_F(WindowSceneSessionImplSpecialWindowTest, CreateAndConnectSpecificSession08, TestSize.Level1)
298 {
299 constexpr int parentId = 10000;
300 constexpr int displayId = 100;
301 sptr<WindowOption> option = new (std::nothrow) WindowOption();
302 ASSERT_NE(nullptr, option);
303 option->SetWindowType(WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
304 option->SetWindowName("CreateAndConnectSpecificSession08");
305 option->SetParentId(parentId);
306
307 // scene session manager processing
308 option->SetIsUIExtFirstSubWindow(true);
309 sptr<WindowSceneSessionImpl> windowSceneSession = new (std::nothrow) WindowSceneSessionImpl(option);
310 ASSERT_NE(nullptr, windowSceneSession);
311 windowSceneSession->Create(abilityContext_, nullptr);
312 ASSERT_EQ(windowSceneSession->property_->GetParentPersistentId(), option->GetParentId());
313 // scene session manager processing
314 option->SetIsUIExtFirstSubWindow(false);
315 option->SetIsUIExtAnySubWindow(true);
316 option->AddWindowFlag(WindowFlag::WINDOW_FLAG_IS_TOAST);
317 windowSceneSession = new (std::nothrow) WindowSceneSessionImpl(option);
318 ASSERT_NE(nullptr, windowSceneSession);
319 windowSceneSession->Create(abilityContext_, nullptr);
320 ASSERT_EQ(windowSceneSession->property_->GetParentPersistentId(), option->GetParentId());
321
322 sptr<WindowOption> mainOption = new WindowOption();
323 mainOption->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
324 mainOption->SetDisplayId(displayId);
325 sptr<WindowSceneSessionImpl> mainWindow = new (std::nothrow) WindowSceneSessionImpl(option);
326 ASSERT_NE(nullptr, windowSceneSession);
327 windowSceneSession->property_->SetPersistentId(parentId);
328 // window processing
329 option->SetIsUIExtFirstSubWindow(false);
330 option->SetIsUIExtAnySubWindow(true);
331 option->RemoveWindowFlag(WindowFlag::WINDOW_FLAG_IS_TOAST);
332 windowSceneSession = new (std::nothrow) WindowSceneSessionImpl(option);
333 ASSERT_NE(nullptr, windowSceneSession);
334 windowSceneSession->windowSessionMap_["mainWindow"] = {parentId, mainWindow};
335 windowSceneSession->Create(abilityContext_, nullptr);
336 ASSERT_EQ(windowSceneSession->property_->GetDisplayId(), mainWindow->property_->GetDisplayId());
337 // window processing
338 option->SetIsUIExtAnySubWindow(false);
339 windowSceneSession = new (std::nothrow) WindowSceneSessionImpl(option);
340 ASSERT_NE(nullptr, windowSceneSession);
341 windowSceneSession->windowSessionMap_["mainWindow"] = {parentId, mainWindow};
342 windowSceneSession->Create(abilityContext_, nullptr);
343 ASSERT_EQ(windowSceneSession->property_->GetDisplayId(), mainWindow->property_->GetDisplayId());
344 windowSceneSession->Destroy(true);
345 }
346
347 /**
348 * @tc.name: FindParentSessionByParentId01
349 * @tc.desc: FindParentSessionByParentId
350 * @tc.type: FUNC
351 */
352 HWTEST_F(WindowSceneSessionImplSpecialWindowTest, FindParentSessionByParentId01, TestSize.Level1)
353 {
354 sptr<WindowOption> option = new (std::nothrow) WindowOption();
355 option->SetWindowTag(WindowTag::MAIN_WINDOW);
356 option->SetWindowName("FindParentSessionByParentId01");
357 sptr<WindowSceneSessionImpl> windowSceneSession = new (std::nothrow) WindowSceneSessionImpl(option);
358 ASSERT_NE(nullptr, windowSceneSession);
359
360 windowSceneSession->SetWindowType(WindowType::WINDOW_TYPE_DIALOG);
361 ASSERT_TRUE(windowSceneSession->FindMainWindowWithContext() == nullptr);
362 windowSceneSession->SetWindowType(WindowType::ABOVE_APP_SYSTEM_WINDOW_END);
363 ASSERT_TRUE(windowSceneSession->FindMainWindowWithContext() == nullptr);
364
365 windowSceneSession->property_->SetPersistentId(1112);
366 windowSceneSession->property_->SetParentId(1000);
367 windowSceneSession->property_->SetParentPersistentId(1000);
368 windowSceneSession->property_->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
369 SessionInfo sessionInfo = { "CreateTestBundle", "CreateTestModule", "CreateTestAbility" };
370 sptr<SessionMocker> session = new (std::nothrow) SessionMocker(sessionInfo);
371 ASSERT_NE(nullptr, session);
372
373 ASSERT_EQ(WMError::WM_OK, windowSceneSession->Create(abilityContext_, session));
374 windowSceneSession->hostSession_ = session;
375 ASSERT_TRUE(nullptr != windowSceneSession->FindParentSessionByParentId(1112));
376 windowSceneSession->Destroy(true);
377 }
378 }
379 } // namespace Rosen
380 } // namespace OHOS
381