• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <regex>
18 #include <bundle_mgr_interface.h>
19 #include <bundlemgr/launcher_service.h>
20 
21 #include "common_test_utils.h"
22 #include "context.h"
23 #include "interfaces/include/ws_common.h"
24 #include "iremote_object_mocker.h"
25 #include "mock/mock_session_stage.h"
26 #include "mock/mock_window_event_channel.h"
27 #include "session_info.h"
28 #include "session_manager.h"
29 #include "session_manager/include/scene_session_manager.h"
30 #include "session/host/include/scene_session.h"
31 #include "session/host/include/main_session.h"
32 #include "window_manager_agent.h"
33 #include "zidl/window_manager_agent_interface.h"
34 
35 using namespace testing;
36 using namespace testing::ext;
37 
38 namespace OHOS {
39 namespace Rosen {
40 class SceneSessionManagerTest12 : public testing::Test {
41 public:
42     static void SetUpTestCase();
43     static void TearDownTestCase();
44     void SetUp() override;
45     void TearDown() override;
46 
47     static sptr<SceneSessionManager> ssm_;
48 private:
49     static constexpr uint32_t WAIT_SYNC_IN_NS = 200000;
50 };
51 
52 sptr<SceneSessionManager> SceneSessionManagerTest12::ssm_ = nullptr;
53 
SetUpTestCase()54 void SceneSessionManagerTest12::SetUpTestCase()
55 {
56     ssm_ = &SceneSessionManager::GetInstance();
57 }
58 
TearDownTestCase()59 void SceneSessionManagerTest12::TearDownTestCase()
60 {
61     ssm_ = nullptr;
62 }
63 
SetUp()64 void SceneSessionManagerTest12::SetUp()
65 {
66 }
67 
TearDown()68 void SceneSessionManagerTest12::TearDown()
69 {
70     usleep(WAIT_SYNC_IN_NS);
71 }
72 
73 namespace {
74 /**
75  * @tc.name: HasFloatingWindowForeground01
76  * @tc.desc: test HasFloatingWindowForeground with null abilityToken
77  * @tc.type: FUNC
78  */
79 HWTEST_F(SceneSessionManagerTest12, HasFloatingWindowForeground01, Function | SmallTest | Level3)
80 {
81     bool hasFloatWindowForeground = false;
82     WMError result = ssm_->HasFloatingWindowForeground(nullptr, hasFloatWindowForeground);
83     EXPECT_EQ(result, WMError::WM_ERROR_NULLPTR);
84     EXPECT_EQ(hasFloatWindowForeground, false);
85 }
86 
87 /**
88  * @tc.name: HasFloatingWindowForeground02
89  * @tc.desc: test HasFloatingWindowForeground with not existed abilityToken
90  * @tc.type: FUNC
91  */
92 HWTEST_F(SceneSessionManagerTest12, HasFloatingWindowForeground02, Function | SmallTest | Level3)
93 {
94     SessionInfo sessionInfo;
95     sessionInfo.sessionState_ = SessionState::STATE_ACTIVE;
96     sessionInfo.persistentId_ = 1;
97     sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(sessionInfo, nullptr);
98 
99     sceneSession->GetSessionProperty()->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
100     sptr<IRemoteObject> token1 = sptr<MockIRemoteObject>::MakeSptr();
101     sceneSession->SetAbilityToken(token1);
102     sceneSession->SetSessionState(SessionState::STATE_ACTIVE);
103     ssm_->sceneSessionMap_.insert({ sceneSession->GetPersistentId(), sceneSession });
104 
105     bool hasFloatWindowForeground = false;
106     sptr<IRemoteObject> token2 = sptr<MockIRemoteObject>::MakeSptr();
107     WMError result = ssm_->HasFloatingWindowForeground(token2, hasFloatWindowForeground);
108     EXPECT_EQ(result, WMError::WM_OK);
109     EXPECT_EQ(hasFloatWindowForeground, false);
110 }
111 
112 /**
113  * @tc.name: HasFloatingWindowForeground03
114  * @tc.desc: test HasFloatingWindowForeground with existed foreground float window
115  * @tc.type: FUNC
116  */
117 HWTEST_F(SceneSessionManagerTest12, HasFloatingWindowForeground03, Function | SmallTest | Level3)
118 {
119     // create first test sceneSession
120     SessionInfo sessionInfo1;
121     sessionInfo1.sessionState_ = SessionState::STATE_ACTIVE;
122     sessionInfo1.persistentId_ = 1;
123 
124     sptr<SceneSession> sceneSession1 = sptr<SceneSession>::MakeSptr(sessionInfo1, nullptr);
125     sptr<IRemoteObject> token1 = sptr<MockIRemoteObject>::MakeSptr();
126     sceneSession1->GetSessionProperty()->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
127     sceneSession1->SetAbilityToken(token1);
128     sceneSession1->SetSessionState(SessionState::STATE_ACTIVE);
129 
130     // create second test sceneSession
131     SessionInfo sessionInfo2;
132     sessionInfo2.sessionState_ = SessionState::STATE_FOREGROUND;
133     sessionInfo2.persistentId_ = 2;
134 
135     sptr<SceneSession> sceneSession2 = sptr<SceneSession>::MakeSptr(sessionInfo2, nullptr);
136     sptr<IRemoteObject> token2 = sptr<MockIRemoteObject>::MakeSptr();
137     sceneSession2->GetSessionProperty()->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
138     sceneSession2->SetAbilityToken(token2);
139     sceneSession2->SetSessionState(SessionState::STATE_FOREGROUND);
140 
141     ssm_->sceneSessionMap_.insert({ sceneSession1->GetPersistentId(), sceneSession1 });
142     ssm_->sceneSessionMap_.insert({ sceneSession2->GetPersistentId(), sceneSession2 });
143 
144     bool hasFloatWindowForeground = false;
145     WMError result = ssm_->HasFloatingWindowForeground(token1, hasFloatWindowForeground);
146     EXPECT_EQ(result, WMError::WM_OK);
147     EXPECT_EQ(hasFloatWindowForeground, true);
148 
149     hasFloatWindowForeground = false;
150     result = ssm_->HasFloatingWindowForeground(token2, hasFloatWindowForeground);
151     EXPECT_EQ(result, WMError::WM_OK);
152     EXPECT_EQ(hasFloatWindowForeground, true);
153 }
154 
155 /**
156  * @tc.name: HasFloatingWindowForeground04
157  * @tc.desc: test HasFloatingWindowForeground with existed background float window
158  * @tc.type: FUNC
159  */
160 HWTEST_F(SceneSessionManagerTest12, HasFloatingWindowForeground04, Function | SmallTest | Level3)
161 {
162     // create first test sceneSession
163     SessionInfo sessionInfo1;
164     sessionInfo1.sessionState_ = SessionState::STATE_INACTIVE;
165     sessionInfo1.persistentId_ = 1;
166 
167     sptr<SceneSession> sceneSession1 = sptr<SceneSession>::MakeSptr(sessionInfo1, nullptr);
168     sptr<IRemoteObject> token1 = sptr<MockIRemoteObject>::MakeSptr();
169     sceneSession1->GetSessionProperty()->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
170     sceneSession1->SetAbilityToken(token1);
171     sceneSession1->SetSessionState(SessionState::STATE_INACTIVE);
172 
173     // create second test sceneSession
174     SessionInfo sessionInfo2;
175     sessionInfo2.sessionState_ = SessionState::STATE_BACKGROUND;
176     sessionInfo2.persistentId_ = 2;
177 
178     sptr<SceneSession> sceneSession2 = sptr<SceneSession>::MakeSptr(sessionInfo2, nullptr);
179     sptr<IRemoteObject> token2 = sptr<MockIRemoteObject>::MakeSptr();
180     sceneSession2->GetSessionProperty()->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
181     sceneSession2->SetAbilityToken(token2);
182     sceneSession2->SetSessionState(SessionState::STATE_BACKGROUND);
183 
184     ssm_->sceneSessionMap_.insert({ sceneSession1->GetPersistentId(), sceneSession1 });
185     ssm_->sceneSessionMap_.insert({ sceneSession2->GetPersistentId(), sceneSession2 });
186 
187     bool hasFloatWindowForeground = false;
188     WMError result = ssm_->HasFloatingWindowForeground(token1, hasFloatWindowForeground);
189     EXPECT_EQ(result, WMError::WM_OK);
190     EXPECT_EQ(hasFloatWindowForeground, false);
191 
192     hasFloatWindowForeground = false;
193     result = ssm_->HasFloatingWindowForeground(token2, hasFloatWindowForeground);
194     EXPECT_EQ(result, WMError::WM_OK);
195     EXPECT_EQ(hasFloatWindowForeground, false);
196 }
197 
198 /**
199  * @tc.name: HasFloatingWindowForeground05
200  * @tc.desc: test HasFloatingWindowForeground with existed forground toast window
201  * @tc.type: FUNC
202  */
203 HWTEST_F(SceneSessionManagerTest12, HasFloatingWindowForeground05, Function | SmallTest | Level3)
204 {
205     // create first test sceneSession
206     SessionInfo sessionInfo;
207     sessionInfo.sessionState_ = SessionState::STATE_INACTIVE;
208     sessionInfo.persistentId_ = 1;
209 
210     sptr<SceneSession> sceneSession = sptr<SceneSession>::MakeSptr(sessionInfo, nullptr);
211     sptr<IRemoteObject> token = sptr<MockIRemoteObject>::MakeSptr();
212     sceneSession->GetSessionProperty()->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
213     sceneSession->SetAbilityToken(token);
214     sceneSession->SetSessionState(SessionState::STATE_INACTIVE);
215 
216     ssm_->sceneSessionMap_.insert({ sceneSession->GetPersistentId(), sceneSession });
217 
218     bool hasFloatWindowForeground = false;
219     WMError result = ssm_->HasFloatingWindowForeground(token, hasFloatWindowForeground);
220     EXPECT_EQ(result, WMError::WM_OK);
221     EXPECT_EQ(hasFloatWindowForeground, false);
222 }
223 
224 /**
225  * @tc.name: HasFloatingWindowForeground06
226  * @tc.desc: test HasFloatingWindowForeground with other foreground float window
227  * @tc.type: FUNC
228  */
229 HWTEST_F(SceneSessionManagerTest12, HasFloatingWindowForeground06, Function | SmallTest | Level3)
230 {
231     // create first test sceneSession
232     SessionInfo sessionInfo1;
233     sessionInfo1.sessionState_ = SessionState::STATE_ACTIVE;
234     sessionInfo1.persistentId_ = 1;
235 
236     sptr<SceneSession> sceneSession1 = sptr<SceneSession>::MakeSptr(sessionInfo1, nullptr);
237     sptr<IRemoteObject> token1 = sptr<MockIRemoteObject>::MakeSptr();
238     sceneSession1->GetSessionProperty()->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
239     sceneSession1->SetAbilityToken(token1);
240     sceneSession1->SetSessionState(SessionState::STATE_ACTIVE);
241 
242     // create second test sceneSession
243     SessionInfo sessionInfo2;
244     sessionInfo2.sessionState_ = SessionState::STATE_BACKGROUND;
245     sessionInfo2.persistentId_ = 2;
246 
247     sptr<SceneSession> sceneSession2 = sptr<SceneSession>::MakeSptr(sessionInfo2, nullptr);
248     sptr<IRemoteObject> token2 = sptr<MockIRemoteObject>::MakeSptr();
249     sceneSession2->GetSessionProperty()->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
250     sceneSession2->SetAbilityToken(token2);
251     sceneSession2->SetSessionState(SessionState::STATE_BACKGROUND);
252 
253     ssm_->sceneSessionMap_.insert({ sceneSession1->GetPersistentId(), sceneSession1 });
254     ssm_->sceneSessionMap_.insert({ sceneSession2->GetPersistentId(), sceneSession2 });
255 
256     bool hasFloatWindowForeground = false;
257     WMError result = ssm_->HasFloatingWindowForeground(token2, hasFloatWindowForeground);
258     EXPECT_EQ(result, WMError::WM_OK);
259     EXPECT_EQ(hasFloatWindowForeground, false);
260 }
261 }
262 } // namespace Rosen
263 } // namespace OHOS