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 <message_option.h>
18 #include <message_parcel.h>
19
20 #include "iremote_object_mocker.h"
21 #include "session_manager/include/scene_session_manager.h"
22 #include "session_manager/include/zidl/scene_session_manager_interface.h"
23 #include "session/container/include/window_event_channel.h"
24 #include "window_manager_agent.h"
25 #include "zidl/scene_session_manager_stub.h"
26 #include "zidl/window_manager_agent_interface.h"
27 #include "pattern_detach_callback.h"
28 #include "test/mock/mock_session_stage.h"
29
30 using namespace testing;
31 using namespace testing::ext;
32 namespace OHOS {
33 namespace Rosen {
34 class SceneSessionManagerStubLifecycleTest : public testing::Test {
35 public:
36 static void SetUpTestCase();
37 static void TearDownTestCase();
38 void SetUp() override;
39 void TearDown() override;
40 sptr<SceneSessionManagerStub> stub_;
41
42 private:
43 static constexpr uint32_t WAIT_SYNC_IN_NS = 200000;
44 };
45
SetUpTestCase()46 void SceneSessionManagerStubLifecycleTest::SetUpTestCase() {}
47
TearDownTestCase()48 void SceneSessionManagerStubLifecycleTest::TearDownTestCase() {}
49
SetUp()50 void SceneSessionManagerStubLifecycleTest::SetUp()
51 {
52 stub_ = sptr<SceneSessionManager>::MakeSptr();
53 }
54
TearDown()55 void SceneSessionManagerStubLifecycleTest::TearDown()
56 {
57 usleep(WAIT_SYNC_IN_NS);
58 }
59
60 namespace {
61 /**
62 * @tc.name: HandleRecoverAndReconnectSceneSession
63 * @tc.desc: test HandleRecoverAndReconnectSceneSession
64 * @tc.type: FUNC
65 */
66 HWTEST_F(SceneSessionManagerStubLifecycleTest, HandleRecoverAndReconnectSceneSession, TestSize.Level1)
67 {
68 ASSERT_NE(nullptr, stub_);
69 MessageParcel data;
70 MessageParcel reply;
71
72 sptr<ISessionStage> sessionStage = sptr<SessionStageMocker>::MakeSptr();
73 ASSERT_NE(nullptr, sessionStage);
74 int res = stub_->HandleRecoverAndReconnectSceneSession(data, reply);
75 ASSERT_EQ(res, ERR_INVALID_DATA);
76
77 data.WriteRemoteObject(sessionStage->AsObject());
78 sptr<IWindowEventChannel> eventChannel = sptr<WindowEventChannel>::MakeSptr(sessionStage);
79 ASSERT_NE(nullptr, eventChannel);
80 data.WriteRemoteObject(eventChannel->AsObject());
81 struct RSSurfaceNodeConfig surfaceNodeConfig;
82 surfaceNodeConfig.SurfaceNodeName = "SurfaceNode";
83 std::shared_ptr<RSSurfaceNode> surfaceNode = RSSurfaceNode::Create(surfaceNodeConfig, RSSurfaceNodeType::DEFAULT);
84 surfaceNode->Marshalling(data);
85 data.WriteBool(false);
86 res = stub_->HandleRecoverAndReconnectSceneSession(data, reply);
87 ASSERT_EQ(res, ERR_INVALID_STATE);
88
89 data.WriteRemoteObject(sessionStage->AsObject());
90 data.WriteRemoteObject(eventChannel->AsObject());
91 surfaceNode->Marshalling(data);
92 data.WriteBool(true);
93 sptr<WindowSessionProperty> property = sptr<WindowSessionProperty>::MakeSptr();
94 ASSERT_NE(nullptr, property);
95 property->SetTokenState(true);
96 data.WriteStrongParcelable(property);
97 sptr<IWindowManagerAgent> windowManagerAgent = sptr<WindowManagerAgent>::MakeSptr();
98 ASSERT_NE(nullptr, windowManagerAgent);
99 data.WriteRemoteObject(windowManagerAgent->AsObject());
100 res = stub_->HandleRecoverAndReconnectSceneSession(data, reply);
101 EXPECT_EQ(res, ERR_INVALID_STATE);
102 }
103
104 /**
105 * @tc.name: HandlePendingSessionToForeground
106 * @tc.desc: test HandlePendingSessionToForeground
107 * @tc.type: FUNC
108 */
109 HWTEST_F(SceneSessionManagerStubLifecycleTest, HandlePendingSessionToForeground, TestSize.Level1)
110 {
111 MessageParcel data;
112 MessageParcel reply;
113
114 sptr<IWindowManagerAgent> windowManagerAgent = sptr<WindowManagerAgent>::MakeSptr();
115 data.WriteRemoteObject(windowManagerAgent->AsObject());
116
117 int res = stub_->HandlePendingSessionToForeground(data, reply);
118 EXPECT_EQ(res, ERR_NONE);
119 }
120
121 /**
122 * @tc.name: HandlePendingSessionToBackgroundForDelegator
123 * @tc.desc: test HandlePendingSessionToBackgroundForDelegator
124 * @tc.type: FUNC
125 */
126 HWTEST_F(SceneSessionManagerStubLifecycleTest, HandlePendingSessionToBackgroundForDelegator, TestSize.Level1)
127 {
128 MessageParcel data;
129 MessageParcel reply;
130
131 sptr<IWindowManagerAgent> windowManagerAgent = sptr<WindowManagerAgent>::MakeSptr();
132 data.WriteRemoteObject(windowManagerAgent->AsObject());
133
134 int res = stub_->HandlePendingSessionToBackgroundForDelegator(data, reply);
135 EXPECT_EQ(res, ERR_INVALID_DATA);
136 }
137
138 /**
139 * @tc.name: HandleTerminateSessionNew
140 * @tc.desc: test HandleTerminateSessionNew
141 * @tc.type: FUNC
142 */
143 HWTEST_F(SceneSessionManagerStubLifecycleTest, HandleTerminateSessionNew, TestSize.Level1)
144 {
145 MessageParcel data;
146 MessageParcel reply;
147
148 data.WriteBool(false);
149
150 int res = stub_->HandleTerminateSessionNew(data, reply);
151 EXPECT_EQ(res, ERR_INVALID_DATA);
152 }
153
154 /**
155 * @tc.name: HandleSetSessionContinueState
156 * @tc.desc: test HandleSetSessionContinueState
157 * @tc.type: FUNC
158 */
159 HWTEST_F(SceneSessionManagerStubLifecycleTest, HandleSetSessionContinueState, TestSize.Level1)
160 {
161 MessageParcel data;
162 MessageParcel reply;
163
164 int32_t x = 1;
165 sptr<IWindowManagerAgent> windowManagerAgent = sptr<WindowManagerAgent>::MakeSptr();
166 data.WriteRemoteObject(windowManagerAgent->AsObject());
167 data.WriteInt32(x);
168
169 int res = stub_->HandleSetSessionContinueState(data, reply);
170 EXPECT_EQ(res, ERR_NONE);
171 }
172
173 /**
174 * @tc.name: HandleClearSession
175 * @tc.desc: test HandleClearSession
176 * @tc.type: FUNC
177 */
178 HWTEST_F(SceneSessionManagerStubLifecycleTest, HandleClearSession, TestSize.Level1)
179 {
180 ASSERT_NE(nullptr, stub_);
181 MessageParcel data;
182 MessageParcel reply;
183
184 auto res = stub_->HandleClearSession(data, reply);
185 EXPECT_EQ(res, ERR_INVALID_DATA);
186
187 int32_t persistentId = 65535;
188 data.WriteInt32(persistentId);
189
190 res = stub_->HandleClearSession(data, reply);
191 EXPECT_EQ(res, ERR_NONE);
192 }
193
194 /**
195 * @tc.name: HandleClearAllSessions
196 * @tc.desc: test HandleClearAllSessions
197 * @tc.type: FUNC
198 */
199 HWTEST_F(SceneSessionManagerStubLifecycleTest, HandleClearAllSessions, TestSize.Level1)
200 {
201 ASSERT_NE(nullptr, stub_);
202 MessageParcel data;
203 MessageParcel reply;
204
205 int res = stub_->HandleClearAllSessions(data, reply);
206 EXPECT_EQ(res, ERR_NONE);
207 }
208
209 /**
210 * @tc.name: HandleLockSession
211 * @tc.desc: test HandleLockSession
212 * @tc.type: FUNC
213 */
214 HWTEST_F(SceneSessionManagerStubLifecycleTest, HandleLockSession, TestSize.Level1)
215 {
216 ASSERT_NE(nullptr, stub_);
217 MessageParcel data;
218 MessageParcel reply;
219
220 auto res = stub_->HandleLockSession(data, reply);
221 EXPECT_EQ(res, ERR_INVALID_DATA);
222 int32_t sessionId = 65535;
223 data.WriteInt32(sessionId);
224 res = stub_->HandleLockSession(data, reply);
225 EXPECT_EQ(res, ERR_NONE);
226 }
227
228 /**
229 * @tc.name: HandleUnlockSession
230 * @tc.desc: test HandleUnlockSession
231 * @tc.type: FUNC
232 */
233 HWTEST_F(SceneSessionManagerStubLifecycleTest, HandleUnlockSession, TestSize.Level1)
234 {
235 ASSERT_NE(nullptr, stub_);
236 MessageParcel data;
237 MessageParcel reply;
238
239 auto res = stub_->HandleUnlockSession(data, reply);
240 EXPECT_EQ(res, ERR_INVALID_DATA);
241 int32_t sessionId = 65535;
242 data.WriteInt32(sessionId);
243 res = stub_->HandleUnlockSession(data, reply);
244 EXPECT_EQ(res, ERR_NONE);
245 }
246
247 /**
248 * @tc.name: HandleMoveSessionsToForeground
249 * @tc.desc: test HandleMoveSessionsToForeground
250 * @tc.type: FUNC
251 */
252 HWTEST_F(SceneSessionManagerStubLifecycleTest, HandleMoveSessionsToForeground, TestSize.Level1)
253 {
254 ASSERT_NE(nullptr, stub_);
255 MessageParcel data;
256 MessageParcel reply;
257
258 auto res = stub_->HandleMoveSessionsToForeground(data, reply);
259 EXPECT_EQ(res, ERR_INVALID_DATA);
260
261 std::vector<int32_t> sessionIds = { 1, 2, 3, 15, 1423 };
262 data.WriteInt32Vector(sessionIds);
263 int32_t topSessionId = 1;
264 data.WriteInt32(topSessionId);
265 res = stub_->HandleMoveSessionsToForeground(data, reply);
266 EXPECT_EQ(res, ERR_NONE);
267 }
268
269 /**
270 * @tc.name: HandleMoveSessionsToBackground
271 * @tc.desc: test HandleMoveSessionsToBackground
272 * @tc.type: FUNC
273 */
274 HWTEST_F(SceneSessionManagerStubLifecycleTest, HandleMoveSessionsToBackground, TestSize.Level1)
275 {
276 ASSERT_NE(nullptr, stub_);
277 MessageParcel data;
278 MessageParcel reply;
279
280 std::vector<int32_t> sessionIds = { 1, 2, 3, 15, 1423 };
281 data.WriteInt32Vector(sessionIds);
282 std::vector<int32_t> result = { 1, 2, 3, 15, 1423 };
283 data.WriteInt32Vector(result);
284
285 int res = stub_->HandleMoveSessionsToBackground(data, reply);
286 EXPECT_EQ(res, ERR_NONE);
287 }
288
289 /**
290 * @tc.name: HandleUpdateSessionWindowVisibilityListener
291 * @tc.desc: test HandleUpdateSessionWindowVisibilityListener
292 * @tc.type: FUNC
293 */
294 HWTEST_F(SceneSessionManagerStubLifecycleTest, HandleUpdateSessionWindowVisibilityListener, TestSize.Level1)
295 {
296 MessageParcel data;
297 MessageParcel reply;
298
299 data.WriteInt32(0);
300 data.WriteBool(true);
301
302 int res = stub_->HandleUpdateSessionWindowVisibilityListener(data, reply);
303 EXPECT_EQ(res, ERR_NONE);
304 }
305
306 /**
307 * @tc.name: HandleGetVisibilityWindowInfo
308 * @tc.desc: test HandleGetVisibilityWindowInfo
309 * @tc.type: FUNC
310 */
311 HWTEST_F(SceneSessionManagerStubLifecycleTest, HandleGetVisibilityWindowInfo, TestSize.Level1)
312 {
313 ASSERT_NE(nullptr, stub_);
314 MessageParcel data;
315 MessageParcel reply;
316
317 int res = stub_->HandleGetVisibilityWindowInfo(data, reply);
318 EXPECT_EQ(res, ERR_NONE);
319 }
320 } // namespace
321 } // namespace Rosen
322 } // namespace OHOS