1 /*
2 * Copyright (c) 2025 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 "interfaces/include/ws_common.h"
19 #include "session_manager/include/scene_session_manager.h"
20 #include "session_info.h"
21 #include "session/host/include/scene_session.h"
22 #include "session_manager.h"
23 #include "test/mock/mock_session_stage.h"
24 #include <ability_manager_client.h>
25 #include <mock_uiext_session_permission.h>
26 #include <mock_ability_manager_client.h>
27 #include <mock_window_event_channel.h>
28
29 using namespace testing;
30 using namespace testing::ext;
31
32 namespace OHOS {
33 namespace Rosen {
34 namespace {
35 const std::string BUNDLE_NAME = "uiextension";
36 const std::string BUNDLE_NAME_TEST = "test";
37 }
38 class SceneSessionManagerExtensionTest : public testing::Test {
39 public:
40 static void SetUpTestCase();
41 static void TearDownTestCase();
42 void SetUp() override;
43 void TearDown() override;
44
45 static sptr<SceneSessionManager> ssm_;
46 private:
47 static constexpr uint32_t WAIT_SYNC_IN_NS = 200000;
48 };
49
50 sptr<SceneSessionManager> SceneSessionManagerExtensionTest::ssm_ = nullptr;
51
SetUpTestCase()52 void SceneSessionManagerExtensionTest::SetUpTestCase()
53 {
54 ssm_ = &SceneSessionManager::GetInstance();
55 }
56
TearDownTestCase()57 void SceneSessionManagerExtensionTest::TearDownTestCase()
58 {
59 ssm_ = nullptr;
60 }
61
SetUp()62 void SceneSessionManagerExtensionTest::SetUp()
63 {
64 ssm_->systemConfig_.windowUIType_ = WindowUIType::PHONE_WINDOW;
65 }
66
TearDown()67 void SceneSessionManagerExtensionTest::TearDown()
68 {
69 ssm_->sceneSessionMap_.clear();
70 usleep(WAIT_SYNC_IN_NS);
71 }
72
73 namespace {
74 /**
75 * @tc.name: CheckSubSessionStartedByExtensionAndSetDisplayId01
76 * @tc.desc: normal test
77 * @tc.type: FUNC
78 */
79 HWTEST_F(SceneSessionManagerExtensionTest, CheckSubSessionStartedByExtensionAndSetDisplayId01, TestSize.Level1)
80 {
81 ASSERT_NE(ssm_, nullptr);
82 SessionInfo info;
83 sptr<SceneSession::SpecificSessionCallback> callback = ssm_->CreateSpecificSessionCallback();
84 sptr<SceneSession> parentSession = sptr<SceneSession>::MakeSptr(info, callback);
85 ssm_->sceneSessionMap_.insert({ parentSession->GetPersistentId(), parentSession });
86 sptr<IRemoteObject> token;
87 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
88 sptr<ISessionStage> sessionStage = sptr<SessionStageMocker>::MakeSptr();
89 EXPECT_EQ(ssm_->CheckSubSessionStartedByExtensionAndSetDisplayId(token, property, sessionStage),
90 WSError::WS_ERROR_NULLPTR);
91 property->SetParentPersistentId(parentSession->GetPersistentId());
92 constexpr DisplayId displayId = 0;
93 parentSession->GetSessionProperty()->SetDisplayId(displayId);
94 EXPECT_EQ(ssm_->CheckSubSessionStartedByExtensionAndSetDisplayId(token, property, nullptr), WSError::WS_OK);
95 EXPECT_EQ(ssm_->CheckSubSessionStartedByExtensionAndSetDisplayId(token, property, sessionStage), WSError::WS_OK);
96 property->SetIsUIExtFirstSubWindow(true);
97 EXPECT_EQ(ssm_->CheckSubSessionStartedByExtensionAndSetDisplayId(token, property, sessionStage), WSError::WS_OK);
98 EXPECT_EQ(property->GetDisplayId(), displayId);
99 }
100
101 /**
102 * @tc.name: CheckSubSessionStartedByExtensionAndSetDisplayId02
103 * @tc.desc: test compare host bundleName and parent bundleName
104 * @tc.type: FUNC
105 */
106 HWTEST_F(SceneSessionManagerExtensionTest, CheckSubSessionStartedByExtensionAndSetDisplayId02, TestSize.Level1)
107 {
108 ASSERT_NE(ssm_, nullptr);
109 SessionInfo info;
110 info.bundleName_ = BUNDLE_NAME;
111 sptr<SceneSession::SpecificSessionCallback> callback = ssm_->CreateSpecificSessionCallback();
112 sptr<SceneSession> parentSession = sptr<SceneSession>::MakeSptr(info, callback);
113 ssm_->sceneSessionMap_.insert({ parentSession->GetPersistentId(), parentSession });
114 sptr<IRemoteObject> token;
115 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
116 sptr<ISessionStage> sessionStage = sptr<SessionStageMocker>::MakeSptr();
117 property->SetParentPersistentId(parentSession->GetPersistentId());
118 property->SetIsUIExtensionAbilityProcess(true);
119 EXPECT_EQ(ssm_->CheckSubSessionStartedByExtensionAndSetDisplayId(token, property, sessionStage), WSError::WS_OK);
120
121 MockUIExtSessionPermission::SetIsStartedByUIExtensionFlag(true);
122 AAFwk::UIExtensionHostInfo hostInfo;
123 hostInfo.elementName_.SetBundleName(BUNDLE_NAME);
124 AAFwk::MockAbilityManagerClient::SetUIExtensionRootHostInfo(hostInfo);
125 AAFwk::UIExtensionSessionInfo extensionSessionInfo;
126 extensionSessionInfo.hostElementName.SetBundleName(BUNDLE_NAME);
127 AAFwk::MockAbilityManagerClient::SetUIExtensionSessionInfo(extensionSessionInfo);
128 EXPECT_EQ(ssm_->CheckSubSessionStartedByExtensionAndSetDisplayId(token, property, sessionStage), WSError::WS_OK);
129
130 hostInfo.elementName_.SetBundleName(BUNDLE_NAME_TEST);
131 AAFwk::MockAbilityManagerClient::SetUIExtensionRootHostInfo(hostInfo);
132 EXPECT_EQ(ssm_->CheckSubSessionStartedByExtensionAndSetDisplayId(token, property, sessionStage), WSError::WS_OK);
133
134 extensionSessionInfo.hostElementName.SetBundleName(BUNDLE_NAME_TEST);
135 AAFwk::MockAbilityManagerClient::SetUIExtensionSessionInfo(extensionSessionInfo);
136 EXPECT_EQ(ssm_->CheckSubSessionStartedByExtensionAndSetDisplayId(token, property, sessionStage),
137 WSError::WS_ERROR_INVALID_WINDOW);
138 MockUIExtSessionPermission::ClearAllFlag();
139 AAFwk::MockAbilityManagerClient::ClearAll();
140 }
141
142 /**
143 * @tc.name: CreateAndConnectSpecificSession04
144 * @tc.desc: CreateAndConnectSpecificSession test uiextension hideNonSecureFloatWindows
145 * @tc.type: FUNC
146 */
147 HWTEST_F(SceneSessionManagerExtensionTest, CreateAndConnectSpecificSession_TestHideFloatWindow, TestSize.Level1)
148 {
149 ASSERT_NE(ssm_, nullptr);
150 sptr<ISessionStage> sessionStage = sptr<SessionStageMocker>::MakeSptr();
151 sptr<IWindowEventChannel> eventChannel = sptr<WindowEventChannelMocker>::MakeSptr(sessionStage);
152 std::shared_ptr<RSSurfaceNode> node = nullptr;
153 sptr<ISession> session;
154 SystemSessionConfig systemConfig;
155 sptr<IRemoteObject> token;
156 int32_t id = 0;
157 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
158
159 MockUIExtSessionPermission::SetIsSystemCallingFlag(false);
160 MockUIExtSessionPermission::SetVerifyCallingPermissionFlag(true);
161 MockUIExtSessionPermission::SetIsStartByHdcdFlag(true);
162 property->SetWindowFlags(123);
163 property->SetParentPersistentId(11);
164 property->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
165 property->SetFloatingWindowAppType(true);
166 ssm_->shouldHideNonSecureFloatingWindows_ = true;
167 WSError res = ssm_->CreateAndConnectSpecificSession(sessionStage, eventChannel, node, property, id, session,
168 systemConfig, token);
169 ASSERT_EQ(WSError::WS_ERROR_INVALID_OPERATION, res);
170
171 ssm_->systemConfig_.windowUIType_ = WindowUIType::PC_WINDOW;
172 res = ssm_->CreateAndConnectSpecificSession(sessionStage, eventChannel, node, property, id, session,
173 systemConfig, token);
174 ASSERT_EQ(WSError::WS_OK, res);
175 MockUIExtSessionPermission::ClearAllFlag();
176 }
177
178 /**
179 * @tc.name: CreateAndConnectSpecificSession05
180 * @tc.desc: CreateAndConnectSpecificSession test uiextension hideNonSecureSubWindows
181 * @tc.type: FUNC
182 */
183 HWTEST_F(SceneSessionManagerExtensionTest, CreateAndConnectSpecificSession_TestHideSubWindow, TestSize.Level1)
184 {
185 ASSERT_NE(ssm_, nullptr);
186 sptr<ISessionStage> sessionStage = sptr<SessionStageMocker>::MakeSptr();
187 sptr<IWindowEventChannel> eventChannel = sptr<WindowEventChannelMocker>::MakeSptr(sessionStage);
188 std::shared_ptr<RSSurfaceNode> node = nullptr;
189 sptr<ISession> session;
190 SystemSessionConfig systemConfig;
191 sptr<IRemoteObject> token;
192 int32_t id = 0;
193 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
194
195 MockUIExtSessionPermission::SetIsSystemCallingFlag(false);
196 property->SetWindowFlags(123);
197 property->SetParentPersistentId(11);
198 property->SetWindowType(WindowType::WINDOW_TYPE_APP_SUB_WINDOW);
199 SessionInfo sessionInfo;
200 sptr<SceneSession> parentSession = sptr<SceneSession>::MakeSptr(sessionInfo, nullptr);
201 parentSession->persistentId_ = 11;
202 ExtensionWindowFlags extWindowFlags(0);
203 extWindowFlags.hideNonSecureWindowsFlag = true;
204 parentSession->UpdateExtWindowFlags(parentSession->GetPersistentId(), extWindowFlags, extWindowFlags);
205 parentSession->SetSessionState(SessionState::STATE_FOREGROUND);
206 ssm_->sceneSessionMap_.insert({parentSession->GetPersistentId(), parentSession});
207 WSError res = ssm_->CreateAndConnectSpecificSession(sessionStage, eventChannel, node, property, id, session,
208 systemConfig, token);
209 ASSERT_EQ(WSError::WS_ERROR_INVALID_OPERATION, res);
210
211 ssm_->sceneSessionMap_.erase(parentSession->GetPersistentId());
212 res = ssm_->CreateAndConnectSpecificSession(sessionStage, eventChannel, node, property, id, session,
213 systemConfig, token);
214 ASSERT_EQ(WSError::WS_OK, res);
215 MockUIExtSessionPermission::ClearAllFlag();
216 }
217 } // namespace
218 } // namespace Rosen
219 } // namespace OHOS