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