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 "session/host/include/system_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 <ui/rs_surface_node.h>
24 #include "window_event_channel_base.h"
25 #include "window_helper.h"
26 #include "window_manager_hilog.h"
27 #include "pointer_event.h"
28 
29 using namespace testing;
30 using namespace testing::ext;
31 
32 namespace OHOS {
33 namespace Rosen {
34 class SystemSessionLifecycleTest : public testing::Test {
35 public:
36     static void SetUpTestCase();
37     static void TearDownTestCase();
38     void SetUp() override;
39     void TearDown() override;
40     SessionInfo info;
41     sptr <SystemSession::SpecificSessionCallback> specificCallback = nullptr;
42     sptr <SystemSession> systemSession_;
43 private:
44     RSSurfaceNode::SharedPtr CreateRSSurfaceNode();
45 };
46 
SetUpTestCase()47 void SystemSessionLifecycleTest::SetUpTestCase()
48 {
49 }
50 
TearDownTestCase()51 void SystemSessionLifecycleTest::TearDownTestCase()
52 {
53 }
54 
SetUp()55 void SystemSessionLifecycleTest::SetUp()
56 {
57     SessionInfo info;
58     info.abilityName_ = "testSystemSession1";
59     info.moduleName_ = "testSystemSession2";
60     info.bundleName_ = "testSystemSession3";
61     systemSession_ = new (std::nothrow) SystemSession(info, specificCallback);
62     EXPECT_NE(nullptr, systemSession_);
63 }
64 
TearDown()65 void SystemSessionLifecycleTest::TearDown()
66 {
67     systemSession_ = nullptr;
68 }
69 
CreateRSSurfaceNode()70 RSSurfaceNode::SharedPtr SystemSessionLifecycleTest::CreateRSSurfaceNode()
71 {
72     struct RSSurfaceNodeConfig rsSurfaceNodeConfig;
73     rsSurfaceNodeConfig.SurfaceNodeName = "WindowSessionTestSurfaceNode";
74     auto surfaceNode = RSSurfaceNode::Create(rsSurfaceNodeConfig);
75     return surfaceNode;
76 }
77 
78 namespace {
79 
80 /**
81  * @tc.name: Show
82  * @tc.desc: test function : Show
83  * @tc.type: FUNC
84  */
85 HWTEST_F(SystemSessionLifecycleTest, Show01, Function | SmallTest | Level1)
86 {
87     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
88 
89     ASSERT_TRUE((systemSession_ != nullptr));
90     ASSERT_EQ(WSError::WS_OK, systemSession_->Show(property));
91 }
92 
93 /**
94  * @tc.name: Show
95  * @tc.desc: test function : Show
96  * @tc.type: FUNC
97  */
98 HWTEST_F(SystemSessionLifecycleTest, Show02, Function | SmallTest | Level1)
99 {
100     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
101     ASSERT_TRUE((property != nullptr));
102     property->SetWindowType(WindowType::WINDOW_TYPE_TOAST);
103     ASSERT_TRUE((systemSession_ != nullptr));
104     systemSession_->SetSessionProperty(property);
105     ASSERT_EQ(WSError::WS_OK, systemSession_->Show(property));
106 }
107 
108 /**
109  * @tc.name: Show
110  * @tc.desc: test function : Show
111  * @tc.type: FUNC
112  */
113 HWTEST_F(SystemSessionLifecycleTest, Show03, Function | SmallTest | Level1)
114 {
115     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
116     ASSERT_TRUE((property != nullptr));
117     property->SetWindowType(WindowType::WINDOW_TYPE_FLOAT);
118     ASSERT_TRUE((systemSession_ != nullptr));
119     systemSession_->SetSessionProperty(property);
120     ASSERT_EQ(WSError::WS_OK, systemSession_->Show(property));
121 }
122 
123 /**
124  * @tc.name: Reconnect01
125  * @tc.desc: check func Reconnect
126  * @tc.type: FUNC
127  */
128 HWTEST_F(SystemSessionLifecycleTest, Reconnect01, Function | SmallTest | Level1)
129 {
130     auto surfaceNode = CreateRSSurfaceNode();
131     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
132     ASSERT_NE(nullptr, property);
133     sptr<SessionStageMocker> mockSessionStage = new (std::nothrow) SessionStageMocker();
134     EXPECT_NE(nullptr, mockSessionStage);
135     sptr<TestWindowEventChannel> testWindowEventChannel = new (std::nothrow) TestWindowEventChannel();
136     EXPECT_NE(nullptr, testWindowEventChannel);
137 
138     auto result = systemSession_->Reconnect(nullptr, nullptr, nullptr, property);
139     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
140 
141     result = systemSession_->Reconnect(nullptr, testWindowEventChannel, surfaceNode, property);
142     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
143 
144     result = systemSession_->Reconnect(mockSessionStage, nullptr, surfaceNode, property);
145     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
146 
147     result = systemSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, nullptr);
148     ASSERT_EQ(result, WSError::WS_ERROR_NULLPTR);
149 
150     property->windowState_ = WindowState::STATE_INITIAL;
151     result = systemSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
152     ASSERT_EQ(result, WSError::WS_ERROR_INVALID_PARAM);
153 
154     property->windowState_ = WindowState::STATE_CREATED;
155     result = systemSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
156     ASSERT_EQ(result, WSError::WS_OK);
157 
158     property->windowState_ = WindowState::STATE_SHOWN;
159     result = systemSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
160     ASSERT_EQ(result, WSError::WS_OK);
161 
162     property->windowState_ = WindowState::STATE_HIDDEN;
163     result = systemSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
164     ASSERT_EQ(result, WSError::WS_OK);
165 
166     property->windowState_ = WindowState::STATE_DESTROYED;
167     result = systemSession_->Reconnect(mockSessionStage, testWindowEventChannel, surfaceNode, property);
168     ASSERT_EQ(result, WSError::WS_ERROR_INVALID_PARAM);
169 }
170 
171 /**
172  * @tc.name: Hide
173  * @tc.desc: test function : Hide
174  * @tc.type: FUNC
175  */
176 HWTEST_F(SystemSessionLifecycleTest, Hide, Function | SmallTest | Level1)
177 {
178     ASSERT_TRUE(systemSession_ != nullptr);
179 
180     auto ret = systemSession_->Hide();
181     ASSERT_EQ(WSError::WS_OK, ret);
182 }
183 
184 /**
185  * @tc.name: Disconnect
186  * @tc.desc: test function : Disconnect
187  * @tc.type: FUNC
188  */
189 HWTEST_F(SystemSessionLifecycleTest, Disconnect, Function | SmallTest | Level1)
190 {
191     ASSERT_TRUE(systemSession_ != nullptr);
192 
193     bool isFromClient = true;
194     auto ret = systemSession_->Disconnect(isFromClient);
195     ASSERT_EQ(WSError::WS_OK, ret);
196 }
197 
198 /**
199  * @tc.name: Disconnect02
200  * @tc.desc: test function : Disconnect
201  * @tc.type: FUNC
202  */
203 HWTEST_F(SystemSessionLifecycleTest, Disconnect02, Function | SmallTest | Level1)
204 {
205     SessionInfo info;
206     info.abilityName_ = "Disconnect02";
207     info.bundleName_ = "Disconnect02Func";
208     info.windowType_ = 2122;
209     sptr<SceneSession::SpecificSessionCallback> specificCallback =
210         new (std::nothrow) SceneSession::SpecificSessionCallback();
211     ASSERT_NE(specificCallback, nullptr);
212     sptr<SystemSession> sysSession =
213         new (std::nothrow) SystemSession(info, specificCallback);
214     ASSERT_NE(sysSession, nullptr);
215 
216     bool isFromClient = true;
217     auto ret = sysSession->Disconnect(isFromClient);
218     ASSERT_EQ(WSError::WS_OK, ret);
219 }
220 
221 /**
222  * @tc.name: Disconnect03
223  * @tc.desc: test function : Disconnect
224  * @tc.type: FUNC
225  */
226 HWTEST_F(SystemSessionLifecycleTest, Disconnect03, Function | SmallTest | Level1)
227 {
228     SessionInfo info;
229     info.abilityName_ = "Disconnect03";
230     info.bundleName_ = "Disconnect03Func";
231     info.windowType_ = 2122;
232     sptr<SceneSession::SpecificSessionCallback> specificCallback =
233         new (std::nothrow) SceneSession::SpecificSessionCallback();
234     ASSERT_NE(specificCallback, nullptr);
235     sptr<SystemSession> sysSession =
236         new (std::nothrow) SystemSession(info, specificCallback);
237     ASSERT_NE(sysSession, nullptr);
238 
239     sptr<WindowSessionProperty> property = new (std::nothrow) WindowSessionProperty();
240     WindowType type = WindowType::WINDOW_TYPE_FLOAT_CAMERA;
241     property->SetWindowType(type);
242     sysSession->property_ = property;
243 
244     bool isFromClient = true;
245     auto ret = sysSession->Disconnect(isFromClient);
246     ASSERT_EQ(WSError::WS_OK, ret);
247 }
248 }
249 }
250 }