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
18 #include "mock/mock_accesstoken_kit.h"
19 #include "mock/mock_session_stage.h"
20 #include "iremote_object_mocker.h"
21 #include "interfaces/include/ws_common.h"
22 #include "session_manager/include/scene_session_manager.h"
23 #include "session_manager/include/zidl/scene_session_manager_proxy.h"
24 #include "session_info.h"
25 #include "session/host/include/scene_session.h"
26
27 using namespace testing;
28 using namespace testing::ext;
29
30 namespace OHOS {
31 namespace Rosen {
32 class WindowManagerServiceDumpTest : public testing::Test {
33 public:
34 static void SetUpTestCase();
35 static void TearDownTestCase();
36 void SetUp() override;
37 void TearDown() override;
38 sptr<SceneSessionManager> ssm_;
39
40 private:
41 static constexpr uint32_t WAIT_SYNC_IN_NS = 200000;
42 };
43
DumpRootSceneElementInfoFuncTest(const sptr<SceneSession> & session,const std::vector<std::string> & params,std::vector<std::string> & infos)44 void DumpRootSceneElementInfoFuncTest(const sptr<SceneSession>& session,
45 const std::vector<std::string>& params, std::vector<std::string>& infos) {}
46
SetUpTestCase()47 void WindowManagerServiceDumpTest::SetUpTestCase() {}
48
TearDownTestCase()49 void WindowManagerServiceDumpTest::TearDownTestCase() {}
50
SetUp()51 void WindowManagerServiceDumpTest::SetUp()
52 {
53 ssm_ = &SceneSessionManager::GetInstance();
54 }
55
TearDown()56 void WindowManagerServiceDumpTest::TearDown()
57 {
58 usleep(WAIT_SYNC_IN_NS);
59 ssm_ = nullptr;
60 }
61
62 namespace {
63
64 /**
65 * @tc.name: GetSessionDumpInfo
66 * @tc.desc: GetSessionDumpInfo
67 * @tc.type: FUNC
68 */
69 HWTEST_F(WindowManagerServiceDumpTest, GetSessionDumpInfo01, TestSize.Level1)
70 {
71 ASSERT_NE(ssm_, nullptr);
72 std::string dumpInfo = "testDumpInfo";
73 std::vector<std::string> params = { "testDumpInfo" };
74 MockAccesstokenKit::MockIsSACalling(false);
75 WSError result = ssm_->GetSessionDumpInfo(params, dumpInfo);
76 EXPECT_EQ(result, WSError::WS_ERROR_INVALID_PERMISSION);
77 MockAccesstokenKit::MockIsSACalling(true);
78 result = ssm_->GetSessionDumpInfo(params, dumpInfo);
79 EXPECT_EQ(result, WSError::WS_ERROR_INVALID_OPERATION);
80
81 params.clear();
82 params.push_back("-w");
83 params.push_back("a");
84 result = ssm_->GetSessionDumpInfo(params, dumpInfo);
85 EXPECT_EQ(result, WSError::WS_ERROR_INVALID_OPERATION);
86
87 params.clear();
88 params.push_back("-b");
89 params.push_back("a");
90 result = ssm_->GetSessionDumpInfo(params, dumpInfo);
91 EXPECT_EQ(result, WSError::WS_OK);
92
93 params.clear();
94 params.push_back("-p");
95 params.push_back("1");
96 result = ssm_->GetSessionDumpInfo(params, dumpInfo);
97 EXPECT_EQ(result, WSError::WS_OK);
98
99 params.clear();
100 params.push_back("-b");
101 params.push_back("a");
102 result = ssm_->GetSessionDumpInfo(params, dumpInfo);
103 EXPECT_EQ(result, WSError::WS_OK);
104
105 params.clear();
106 params.push_back("testDumpInfo");
107 params.push_back("a");
108 result = ssm_->GetSessionDumpInfo(params, dumpInfo);
109 EXPECT_EQ(result, WSError::WS_ERROR_INVALID_OPERATION);
110 }
111
112 /**
113 * @tc.name: GetSessionDumpInfo
114 * @tc.desc: normal function
115 * @tc.type: FUNC
116 */
117 HWTEST_F(WindowManagerServiceDumpTest, GetSessionDumpInfo02, TestSize.Level1)
118 {
119 std::vector<std::string> params;
120 std::string info = "info";
121 sptr<IRemoteObject> iRemoteObjectMocker = sptr<IRemoteObjectMocker>::MakeSptr();
122 auto sceneSessionManagerProxy = sptr<SceneSessionManagerProxy>::MakeSptr(iRemoteObjectMocker);
123
124 ASSERT_EQ(WSError::WS_OK, sceneSessionManagerProxy->GetSessionDumpInfo(params, info));
125 }
126
127 /**
128 * @tc.name: DumpSessionInfo
129 * @tc.desc: SceneSesionManager dump session info
130 * @tc.type: FUNC
131 */
132 HWTEST_F(WindowManagerServiceDumpTest, DumpSessionInfo, TestSize.Level1)
133 {
134 SessionInfo info;
135 std::ostringstream oss;
136 std::string dumpInfo;
137 info.abilityName_ = "DumpSessionInfo";
138 info.bundleName_ = "DumpSessionInfo";
139 info.isSystem_ = false;
140 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
141 ASSERT_NE(nullptr, sceneSession);
142 ssm_->DumpSessionInfo(sceneSession, oss);
143 EXPECT_FALSE(sceneSession->IsVisible());
144
145 sptr<SceneSession::SpecificSessionCallback> specific = sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
146 EXPECT_NE(nullptr, specific);
147 sceneSession = sptr<SceneSession>::MakeSptr(info, specific);
148 ASSERT_NE(nullptr, sceneSession);
149 ssm_->DumpSessionInfo(sceneSession, oss);
150 EXPECT_FALSE(sceneSession->IsVisible());
151 sceneSession = nullptr;
152 info.isSystem_ = true;
153 sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
154 ssm_->DumpSessionInfo(sceneSession, oss);
155 }
156
157 /**
158 * @tc.name: DumpSessionAll
159 * @tc.desc: ScreenSesionManager dump all session info
160 * @tc.type: FUNC
161 */
162 HWTEST_F(WindowManagerServiceDumpTest, DumpSessionAll, TestSize.Level1)
163 {
164 SessionInfo sessionInfo;
165 sessionInfo.bundleName_ = "WindowManagerServiceDumpTest";
166 sessionInfo.abilityName_ = "DumpSessionAll";
167 sptr<WindowSessionProperty> windowSessionProperty = sptr<WindowSessionProperty>::MakeSptr();
168 sptr<SceneSession> sceneSession = ssm_->RequestSceneSession(sessionInfo, windowSessionProperty);
169 ASSERT_EQ(nullptr, sceneSession);
170 std::vector<std::string> infos;
171 WSError result = ssm_->DumpSessionAll(infos);
172 ASSERT_EQ(WSError::WS_OK, result);
173 ASSERT_FALSE(infos.empty());
174 }
175
176 /**
177 * @tc.name: DumpSesGetFloatWidthsionAll
178 * @tc.desc: Get Float Width
179 * @tc.type: FUNC
180 */
181 HWTEST_F(WindowManagerServiceDumpTest, GetFloatWidth, TestSize.Level1)
182 {
183 float value = 0.1234;
184 std::string strValue = ssm_->GetFloatWidth(5, value);
185 ASSERT_EQ("0.123", strValue);
186 }
187
188 /**
189 * @tc.name: DumpSessionWithId
190 * @tc.desc: ScreenSesionManager dump session with id
191 * @tc.type: FUNC
192 */
193 HWTEST_F(WindowManagerServiceDumpTest, DumpSessionWithId, TestSize.Level1)
194 {
195 SessionInfo sessionInfo;
196 sessionInfo.bundleName_ = "WindowManagerServiceDumpTest";
197 sessionInfo.abilityName_ = "DumpSessionWithId";
198 sptr<WindowSessionProperty> windowSessionProperty = sptr<WindowSessionProperty>::MakeSptr();
199 sptr<SceneSession> sceneSession = ssm_->RequestSceneSession(sessionInfo, windowSessionProperty);
200 ASSERT_EQ(nullptr, sceneSession);
201 std::vector<std::string> infos;
202 WSError result = ssm_->DumpSessionWithId(windowSessionProperty->GetPersistentId(), infos);
203 ASSERT_EQ(WSError::WS_OK, result);
204 ASSERT_FALSE(infos.empty());
205 }
206
207 /**
208 * @tc.name: DumpSessionElementInfo
209 * @tc.desc: SceneSesionManager dump session element info
210 * @tc.type: FUNC
211 */
212 HWTEST_F(WindowManagerServiceDumpTest, DumpSessionElementInfo, TestSize.Level1)
213 {
214 DumpRootSceneElementInfoFunc func_ = DumpRootSceneElementInfoFuncTest;
215 ssm_->SetDumpRootSceneElementInfoListener(func_);
216 SessionInfo info;
217 info.abilityName_ = "DumpSessionElementInfo";
218 info.bundleName_ = "DumpSessionElementInfo";
219 info.isSystem_ = false;
220 std::string strId = "10086";
221 sptr<SceneSession> sceneSession = nullptr;
222 sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
223 ASSERT_NE(nullptr, sceneSession);
224 std::vector<std::string> params_(5, "");
225 std::string dumpInfo;
226 ssm_->DumpSessionElementInfo(sceneSession, params_, dumpInfo);
227 sceneSession = nullptr;
228 info.isSystem_ = true;
229 sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
230 ASSERT_NE(nullptr, sceneSession);
231 ssm_->DumpSessionElementInfo(sceneSession, params_, dumpInfo);
232 WSError result01 = ssm_->GetSpecifiedSessionDumpInfo(dumpInfo, params_, strId);
233 EXPECT_EQ(result01, WSError::WS_ERROR_INVALID_PARAM);
234 }
235
236 /**
237 * @tc.name: NotifyDumpInfoResult
238 * @tc.desc: SceneSesionManager notify dump info result
239 * @tc.type: FUNC
240 */
241 HWTEST_F(WindowManagerServiceDumpTest, NotifyDumpInfoResult, TestSize.Level1)
242 {
243 MockAccesstokenKit::MockIsSACalling(true);
244 std::vector<std::string> info = { "std::", "vector", "<std::string>" };
245 ssm_->NotifyDumpInfoResult(info);
246 std::vector<std::string> params = { "-a" };
247 std::string dumpInfo = "";
248 WSError result01 = ssm_->GetSessionDumpInfo(params, dumpInfo);
249 EXPECT_EQ(result01, WSError::WS_OK);
250 params.clear();
251 params.push_back("-w");
252 params.push_back("23456");
253 WSError result02 = ssm_->GetSessionDumpInfo(params, dumpInfo);
254 EXPECT_NE(result02, WSError::WS_OK);
255 params.clear();
256 WSError result03 = ssm_->GetSessionDumpInfo(params, dumpInfo);
257 EXPECT_NE(result03, WSError::WS_OK);
258 }
259
260 /**
261 * @tc.name: GetAllSessionDumpInfo
262 * @tc.desc: SceneSesionManager test GetAllSessionDumpInfo
263 * @tc.type: FUNC
264 */
265 HWTEST_F(WindowManagerServiceDumpTest, GetAllSessionDumpInfo01, TestSize.Level1)
266 {
267 SessionInfo info1;
268 info1.abilityName_ = "GetAllSessionDumpInfo1";
269 info1.bundleName_ = "GetAllSessionDumpInfo1";
270 info1.persistentId_ = 1;
271 sptr<SceneSession> sceneSession1 = sptr<SceneSession>::MakeSptr(info1, nullptr);
272 ASSERT_NE(sceneSession1, nullptr);
273 sceneSession1->UpdateNativeVisibility(true);
274
275 SessionInfo info2;
276 info2.abilityName_ = "GetAllSessionDumpInfo2";
277 info2.bundleName_ = "GetAllSessionDumpInfo2";
278 info2.persistentId_ = 2;
279 sptr<SceneSession> sceneSession2 = sptr<SceneSession>::MakeSptr(info2, nullptr);
280 ASSERT_NE(sceneSession2, nullptr);
281 sceneSession2->UpdateNativeVisibility(false);
282
283 ssm_->sceneSessionMap_.insert({ 1, sceneSession1 });
284 ssm_->sceneSessionMap_.insert({ 2, sceneSession2 });
285 std::string dumpInfo;
286 ASSERT_EQ(ssm_->GetAllSessionDumpInfo(dumpInfo), WSError::WS_OK);
287 }
288
289 /**
290 * @tc.name: GetAllSessionDumpInfo
291 * @tc.desc: GetAllSessionDumpInfo
292 * @tc.type: FUNC
293 */
294 HWTEST_F(WindowManagerServiceDumpTest, GetAllSessionDumpInfo02, TestSize.Level1)
295 {
296 SessionInfo sessionInfo;
297 sessionInfo.bundleName_ = "SceneSessionManagerTest7";
298 sessionInfo.abilityName_ = "GetAllSessionDumpInfo";
299 sessionInfo.isSystem_ = false;
300 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(sessionInfo, nullptr);
301 ASSERT_NE(nullptr, sceneSession);
302 sceneSession->state_ = SessionState::STATE_DISCONNECT;
303 ASSERT_NE(nullptr, ssm_);
304 ssm_->sceneSessionMap_.insert(std::make_pair(1, sceneSession));
305 std::string dumpInfo = "";
306 auto ret = ssm_->GetAllSessionDumpInfo(dumpInfo);
307 EXPECT_EQ(ret, WSError::WS_OK);
308 sceneSession->state_ = SessionState::STATE_END;
309 ret = ssm_->GetAllSessionDumpInfo(dumpInfo);
310 EXPECT_EQ(ret, WSError::WS_OK);
311 sceneSession->state_ = SessionState::STATE_ACTIVE;
312 ret = ssm_->GetAllSessionDumpInfo(dumpInfo);
313 EXPECT_EQ(ret, WSError::WS_OK);
314 sessionInfo.isSystem_ = true;
315 ret = ssm_->GetAllSessionDumpInfo(dumpInfo);
316 EXPECT_EQ(ret, WSError::WS_OK);
317 sceneSession = nullptr;
318 ret = ssm_->GetAllSessionDumpInfo(dumpInfo);
319 EXPECT_EQ(ret, WSError::WS_OK);
320 }
321
322 /**
323 * @tc.name: GetAllSessionDumpInfo01
324 * @tc.desc: GetAllSessionDumpInfo
325 * @tc.type: FUNC
326 */
327 HWTEST_F(WindowManagerServiceDumpTest, GetAllSessionDumpInfo03, TestSize.Level1)
328 {
329 SessionInfo sessionInfo;
330 sessionInfo.bundleName_ = "SceneSessionManagerTest7";
331 sessionInfo.abilityName_ = "GetAllSessionDumpInfo01";
332 sessionInfo.isSystem_ = true;
333 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(sessionInfo, nullptr);
334 ASSERT_NE(nullptr, sceneSession);
335 sceneSession->isVisible_ = true;
336 ASSERT_NE(nullptr, ssm_);
337 ssm_->sceneSessionMap_.insert(std::make_pair(1, sceneSession));
338 std::string dumpInfo = "";
339 auto ret = ssm_->GetAllSessionDumpInfo(dumpInfo);
340 EXPECT_EQ(ret, WSError::WS_OK);
341 sceneSession->isVisible_ = false;
342 ret = ssm_->GetAllSessionDumpInfo(dumpInfo);
343 EXPECT_EQ(ret, WSError::WS_OK);
344 }
345
346 /**
347 * @tc.name: GetAllSessionDumpDetailInfo
348 * @tc.desc: SceneSesionManager test GetAllSessionDumpDetailInfo
349 * @tc.type: FUNC
350 */
351 HWTEST_F(WindowManagerServiceDumpTest, GetAllSessionDumpDetailInfo, TestSize.Level1)
352 {
353 SessionInfo info1;
354 info1.abilityName_ = "GetAllSessionDumpDetailInfo1";
355 info1.bundleName_ = "GetAllSessionDumpDetailInfo1";
356 info1.persistentId_ = 1;
357 sptr<SceneSession> sceneSession1 = sptr<SceneSession>::MakeSptr(info1, nullptr);
358 ASSERT_NE(sceneSession1, nullptr);
359 sceneSession1->UpdateNativeVisibility(true);
360
361 SessionInfo info2;
362 info2.abilityName_ = "GetAllSessionDumpDetailInfo2";
363 info2.bundleName_ = "GetAllSessionDumpDetailInfo2";
364 info2.persistentId_ = 2;
365 sptr<SceneSession> sceneSession2 = sptr<SceneSession>::MakeSptr(info2, nullptr);
366 ASSERT_NE(sceneSession2, nullptr);
367 sceneSession2->UpdateNativeVisibility(false);
368
369 ssm_->sceneSessionMap_.insert({ 0, nullptr });
370 ssm_->sceneSessionMap_.insert({ 1, sceneSession1 });
371 ssm_->sceneSessionMap_.insert({ 2, sceneSession2 });
372 std::string dumpInfo;
373 ASSERT_EQ(ssm_->GetAllSessionDumpDetailInfo(dumpInfo), WSError::WS_OK);
374 }
375
376 /**
377 * @tc.name: GetSpecifiedSessionDumpInfo
378 * @tc.desc: SceneSesionManager test GetSpecifiedSessionDumpInfo
379 * @tc.type: FUNC
380 */
381 HWTEST_F(WindowManagerServiceDumpTest, GetSpecifiedSessionDumpInfo, TestSize.Level1)
382 {
383 SessionInfo info;
384 info.abilityName_ = "GetSpecifiedSessionDumpInfo";
385 info.bundleName_ = "GetSpecifiedSessionDumpInfo";
386 info.persistentId_ = 1234;
387 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
388 ASSERT_NE(sceneSession, nullptr);
389 ssm_->sceneSessionMap_.insert({ 1234, sceneSession });
390 std::string dumpInfo;
391 std::string strId = "1234";
392 std::vector<std::string> params_(5, "");
393 ASSERT_EQ(ssm_->GetSpecifiedSessionDumpInfo(dumpInfo, params_, strId), WSError::WS_OK);
394 }
395 /**
396 * @tc.name: GetTotalUITreeInfo
397 * @tc.desc: GetTotalUITreeInfo set gesture navigation enabled
398 * @tc.type: FUNC
399 */
400 HWTEST_F(WindowManagerServiceDumpTest, GetTotalUITreeInfo, TestSize.Level1)
401 {
402 std::string dumpInfo = "dumpInfo";
403 ssm_->SetDumpUITreeFunc(nullptr);
404 EXPECT_EQ(WSError::WS_OK, ssm_->GetTotalUITreeInfo(dumpInfo));
__anon1ffbbe920202(std::string& dumpInfo) 405 DumpUITreeFunc func = [](std::string& dumpInfo) { return; };
406 ssm_->SetDumpUITreeFunc(func);
407 EXPECT_EQ(WSError::WS_OK, ssm_->GetTotalUITreeInfo(dumpInfo));
408 }
409
410 /**
411 * @tc.name: DumpSessionInfo
412 * @tc.desc: DumpSessionInfo
413 * @tc.type: FUNC
414 */
415 HWTEST_F(WindowManagerServiceDumpTest, SceneSessionDumpSessionInfo, TestSize.Level1)
416 {
417 SessionInfo info;
418 info.bundleName_ = "SceneSessionTest";
419 info.abilityName_ = "DumpSessionInfo";
420 info.windowType_ = 1;
421 sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
422 EXPECT_NE(sceneSession, nullptr);
423 std::vector<std::string> infos;
424 sceneSession->DumpSessionInfo(infos);
425 ASSERT_FALSE(infos.empty());
426 }
427
428 /**
429 * @tc.name: DumpSessionElementInfo01
430 * @tc.desc: DumpSessionElementInfo
431 * @tc.type: FUNC
432 */
433 HWTEST_F(WindowManagerServiceDumpTest, SceneSessionDumpSessionElementInfo, TestSize.Level1)
434 {
435 SessionInfo info;
436 info.abilityName_ = "Background01";
437 info.bundleName_ = "IsFloatingWindowAppType";
438 info.windowType_ = 1;
439 sptr<Rosen::ISession> session_;
440 sptr<SceneSession::SpecificSessionCallback> specificCallback_ =
441 sptr<SceneSession::SpecificSessionCallback>::MakeSptr();
442 EXPECT_NE(specificCallback_, nullptr);
443 sptr<SceneSession> sceneSession;
444 sceneSession = sptr<SceneSession>::MakeSptr(info, nullptr);
445 EXPECT_NE(sceneSession, nullptr);
446 sptr<SessionStageMocker> mockSessionStage = sptr<SessionStageMocker>::MakeSptr();
447 ASSERT_NE(mockSessionStage, nullptr);
448 std::vector<std::string> params;
449 sceneSession->DumpSessionElementInfo(params);
450 int ret = 1;
451 sceneSession->sessionStage_ = mockSessionStage;
452 sceneSession->DumpSessionElementInfo(params);
453 ASSERT_EQ(ret, 1);
454 }
455
456 } // namespace
457 } // namespace Rosen
458 } // namespace OHOS