• 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 "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 
46 private:
47     RSSurfaceNode::SharedPtr CreateRSSurfaceNode();
48     sptr<SubSession> subSession_;
49     SystemSessionConfig systemConfig_;
50 };
51 
SetUpTestCase()52 void SessionStubLifecycleTest::SetUpTestCase() {}
53 
TearDownTestCase()54 void SessionStubLifecycleTest::TearDownTestCase() {}
55 
SetUp()56 void SessionStubLifecycleTest::SetUp()
57 {
58     SessionInfo info;
59     info.abilityName_ = "testMainSession1";
60     info.moduleName_ = "testMainSession2";
61     info.bundleName_ = "testMainSession3";
62     subSession_ = sptr<SubSession>::MakeSptr(info, specificCallback);
63     EXPECT_NE(nullptr, subSession_);
64 }
65 
TearDown()66 void SessionStubLifecycleTest::TearDown()
67 {
68     subSession_ = nullptr;
69 }
70 
CreateRSSurfaceNode()71 RSSurfaceNode::SharedPtr SessionStubLifecycleTest::CreateRSSurfaceNode()
72 {
73     struct RSSurfaceNodeConfig rsSurfaceNodeConfig;
74     rsSurfaceNodeConfig.SurfaceNodeName = "WindowSessionTestSurfaceNode";
75     auto surfaceNode = RSSurfaceNode::Create(rsSurfaceNodeConfig);
76     return surfaceNode;
77 }
78 
79 namespace {
80 
81 /**
82  * @tc.name: Reconnect01
83  * @tc.desc: check func Reconnect
84  * @tc.type: FUNC
85  */
86 HWTEST_F(SessionStubLifecycleTest, Reconnect01, TestSize.Level1)
87 {
88     auto surfaceNode = CreateRSSurfaceNode();
89     sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
90     ASSERT_NE(nullptr, property);
91     sptr<SessionStageMocker> mockSessionStage = sptr<SessionStageMocker>::MakeSptr();
92     EXPECT_NE(nullptr, mockSessionStage);
93     sptr<TestWindowEventChannel> testWindowEventChannel = sptr<TestWindowEventChannel>::MakeSptr();
94     EXPECT_NE(nullptr, testWindowEventChannel);
95 
96     auto result = subSession_->Reconnect(nullptr, nullptr, nullptr, property);
97     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
98 
99     result = subSession_->Reconnect(nullptr, testWindowEventChannel, surfaceNode, property);
100     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
101 
102     result = subSession_->Reconnect(mockSessionStage, nullptr, surfaceNode, property);
103     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
104 
105     result = subSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, nullptr);
106     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
107 
108     result = subSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
109     ASSERT_EQ(result, WSError::WS_ERROR_INVALID_PARAM);
110 
111     property->SetWindowState(WindowState::STATE_INITIAL);
112     result = subSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
113     ASSERT_EQ(result, WSError::WS_ERROR_INVALID_PARAM);
114 
115     property->SetWindowState(WindowState::STATE_CREATED);
116     result = subSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
117     ASSERT_EQ(result, WSError::WS_OK);
118 
119     property->SetWindowState(WindowState::STATE_SHOWN);
120     result = subSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
121     ASSERT_EQ(result, WSError::WS_OK);
122 
123     property->SetWindowState(WindowState::STATE_HIDDEN);
124     result = subSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
125     ASSERT_EQ(result, WSError::WS_OK);
126 
127     property->SetWindowState(WindowState::STATE_FROZEN);
128     result = subSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
129     ASSERT_EQ(result, WSError::WS_ERROR_INVALID_PARAM);
130 }
131 
132 /**
133  * @tc.name: Hide01
134  * @tc.desc: check func Hide
135  * @tc.type: FUNC
136  */
137 HWTEST_F(SessionStubLifecycleTest, Hide01, TestSize.Level1)
138 {
139     subSession_->Hide();
140     subSession_->GetMissionId();
141 
142     subSession_->isActive_ = true;
143     ASSERT_EQ(WSError::WS_OK, subSession_->Hide());
144     subSession_->isActive_ = false;
145 
146     subSession_->sessionInfo_.isSystem_ = true;
147     ASSERT_EQ(WSError::WS_OK, subSession_->Hide());
148     subSession_->sessionInfo_.isSystem_ = false;
149 
150     sptr<ISessionStage> tempStage_ = subSession_->sessionStage_;
151     subSession_->sessionStage_ = nullptr;
152     ASSERT_EQ(WSError::WS_OK, subSession_->Hide());
153     subSession_->sessionStage_ = tempStage_;
154 
155     WSRect rect;
156     subSession_->UpdatePointerArea(rect);
157     subSession_->RectCheck(50, 100);
158     ASSERT_EQ(WSError::WS_OK, subSession_->ProcessPointDownSession(50, 100));
159 }
160 
161 /**
162  * @tc.name: Hide02
163  * @tc.desc: check func Hide
164  * @tc.type: FUNC
165  */
166 HWTEST_F(SessionStubLifecycleTest, Hide02, TestSize.Level1)
167 {
168     sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
169     ASSERT_NE(nullptr, property);
170     property->SetWindowType(WindowType::APP_MAIN_WINDOW_BASE);
171     ASSERT_TRUE(subSession_ != nullptr);
172     subSession_->SetSessionProperty(property);
173     auto result = subSession_->Hide();
174     ASSERT_EQ(result, WSError::WS_OK);
175 }
176 
177 /**
178  * @tc.name: Hide04
179  * @tc.desc: check func Hide
180  * @tc.type: FUNC
181  */
182 HWTEST_F(SessionStubLifecycleTest, Hide04, TestSize.Level1)
183 {
184     sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
185     ASSERT_NE(nullptr, property);
186     sptr<WindowProperty> winPropSrc = sptr<WindowProperty>::MakeSptr();
187     ASSERT_NE(nullptr, winPropSrc);
188     uint32_t animationFlag = 1;
189     winPropSrc->SetAnimationFlag(animationFlag);
190     uint32_t res = winPropSrc->GetAnimationFlag();
191     ASSERT_NE(res, static_cast<uint32_t>(WindowAnimation::CUSTOM));
192     ASSERT_TRUE(subSession_ != nullptr);
193     auto result = subSession_->Hide();
194     ASSERT_EQ(result, WSError::WS_OK);
195 
196     animationFlag = 3;
197     winPropSrc->SetAnimationFlag(animationFlag);
198     res = winPropSrc->GetAnimationFlag();
199     ASSERT_EQ(res, static_cast<uint32_t>(WindowAnimation::CUSTOM));
200     ASSERT_TRUE(subSession_ != nullptr);
201     result = subSession_->Hide();
202     ASSERT_EQ(result, WSError::WS_OK);
203 }
204 
205 /**
206  * @tc.name: Show02
207  * @tc.desc: check func Show
208  * @tc.type: FUNC
209  */
210 HWTEST_F(SessionStubLifecycleTest, Show02, TestSize.Level1)
211 {
212     sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
213     ASSERT_NE(nullptr, property);
214 
215     sptr<WindowProperty> winPropSrc = sptr<WindowProperty>::MakeSptr();
216     ASSERT_NE(nullptr, winPropSrc);
217     uint32_t animationFlag = 1;
218     winPropSrc->SetAnimationFlag(animationFlag);
219     uint32_t res = winPropSrc->GetAnimationFlag();
220     ASSERT_NE(res, static_cast<uint32_t>(WindowAnimation::CUSTOM));
221 
222     ASSERT_TRUE(subSession_ != nullptr);
223     auto result = subSession_->Show(property);
224     ASSERT_EQ(result, WSError::WS_OK);
225 }
226 
227 /**
228  * @tc.name: Show03
229  * @tc.desc: check func Show
230  * @tc.type: FUNC
231  */
232 HWTEST_F(SessionStubLifecycleTest, Show03, TestSize.Level1)
233 {
234     sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
235     ASSERT_NE(nullptr, property);
236 
237     sptr<WindowProperty> winPropSrc = sptr<WindowProperty>::MakeSptr();
238     ASSERT_NE(nullptr, winPropSrc);
239     uint32_t animationFlag = 3;
240     winPropSrc->SetAnimationFlag(animationFlag);
241     uint32_t res = winPropSrc->GetAnimationFlag();
242     ASSERT_EQ(res, static_cast<uint32_t>(WindowAnimation::CUSTOM));
243 
244     ASSERT_TRUE(subSession_ != nullptr);
245     auto result = subSession_->Show(property);
246     ASSERT_EQ(result, WSError::WS_OK);
247 }
248 
249 /**
250  * @tc.name: Show04
251  * @tc.desc: check func Show
252  * @tc.type: FUNC
253  */
254 HWTEST_F(SessionStubLifecycleTest, Show04, TestSize.Level1)
255 {
256     sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
257     ASSERT_NE(property, nullptr);
258     property->SetAnimationFlag(static_cast<uint32_t>(WindowAnimation::DEFAULT));
259     ASSERT_EQ(subSession_->Show(property), WSError::WS_OK);
260 
261     property->SetAnimationFlag(static_cast<uint32_t>(WindowAnimation::CUSTOM));
262     ASSERT_EQ(subSession_->Show(property), WSError::WS_OK);
263 
264     subSession_->SetSessionProperty(property);
265     ASSERT_EQ(subSession_->Show(property), WSError::WS_OK);
266 }
267 
268 /**
269  * @tc.name: ProcessPointDownSession01
270  * @tc.desc: check func ProcessPointDownSession
271  * @tc.type: FUNC
272  */
273 HWTEST_F(SessionStubLifecycleTest, ProcessPointDownSession01, TestSize.Level1)
274 {
275     subSession_->Hide();
276     subSession_->SetParentSession(subSession_);
277     ASSERT_TRUE(subSession_->GetParentSession() != nullptr);
278 
279     WSRect rect;
280     subSession_->UpdatePointerArea(rect);
281     subSession_->RectCheck(50, 100);
282     ASSERT_EQ(subSession_->ProcessPointDownSession(50, 100), WSError::WS_OK);
283 }
284 
285 /**
286  * @tc.name: ProcessPointDownSession02
287  * @tc.desc: check func ProcessPointDownSession
288  * @tc.type: FUNC
289  */
290 HWTEST_F(SessionStubLifecycleTest, ProcessPointDownSession02, TestSize.Level1)
291 {
292     subSession_->Hide();
293     WSRect rect;
294     subSession_->UpdatePointerArea(rect);
295     subSession_->RectCheck(50, 100);
296 
297     auto property = subSession_->GetSessionProperty();
298     ASSERT_NE(property, nullptr);
299     property->SetRaiseEnabled(false);
300     ASSERT_FALSE(subSession_->GetSessionProperty()->GetRaiseEnabled());
301     ASSERT_EQ(subSession_->ProcessPointDownSession(50, 100), WSError::WS_OK);
302 }
303 } // namespace
304 } // namespace Rosen
305 } // namespace OHOS