1 /*
2 * Copyright (c) 2025 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 #include <configuration.h>
16 #include <gtest/gtest.h>
17
18 #include "ability_context_impl.h"
19 #include "mock_static_call.h"
20 #include "mock_session.h"
21 #include "web_picture_in_picture_controller_interface.h"
22
23 #include "native_pip_window_listener.h"
24 #include "window_manager_hilog.h"
25
26 using namespace testing;
27 using namespace testing::ext;
28
29 namespace OHOS {
30 namespace Rosen {
31 class WebPictureInPictureControllerInterfaceTest : public testing::Test {
32 public:
33 static void SetUpTestCase();
34 static void TearDownTestCase();
35 void SetUp() override;
36 void TearDown() override;
37 int num = 0;
38 sptr<WebPictureInPictureControllerInterface> controller = nullptr;
39 PiPConfig pipConfig{};
40 };
41
SetUpTestCase()42 void WebPictureInPictureControllerInterfaceTest::SetUpTestCase()
43 {
44 }
45
TearDownTestCase()46 void WebPictureInPictureControllerInterfaceTest::TearDownTestCase()
47 {
48 }
49
SetUp()50 void WebPictureInPictureControllerInterfaceTest::SetUp()
51 {
52 pipConfig.mainWindowId = 100;
53 pipConfig.pipTemplateType = 0;
54 pipConfig.width = 100;
55 pipConfig.height = 150;
56 pipConfig.controlGroup = {101};
57 pipConfig.env = reinterpret_cast<napi_env>(&num);
58 controller = sptr<WebPictureInPictureControllerInterface>::MakeSptr();
59 }
60
TearDown()61 void WebPictureInPictureControllerInterfaceTest::TearDown()
62 {
63 pipConfig = {};
64 controller = nullptr;
65 }
66
PipStartPipCallback(uint32_t controllerId,uint8_t requestId,uint64_t surfaceId)67 void PipStartPipCallback(uint32_t controllerId, uint8_t requestId, uint64_t surfaceId)
68 {
69 }
70
PipLifeCycleCallback(uint32_t controllerId,PiPState state,int32_t errorCode)71 void PipLifeCycleCallback(uint32_t controllerId, PiPState state, int32_t errorCode)
72 {
73 }
74
PipControlEventCallback(uint32_t controllerId,PiPControlType controlType,PiPControlStatus status)75 void PipControlEventCallback(uint32_t controllerId, PiPControlType controlType, PiPControlStatus status)
76 {
77 }
78
PipResizeCallback(uint32_t controllerId,uint32_t width,uint32_t height,double scale)79 void PipResizeCallback(uint32_t controllerId, uint32_t width, uint32_t height, double scale)
80 {
81 }
82 namespace {
83 /**
84 * @tc.name: Create
85 * @tc.desc: Create
86 * @tc.type: FUNC
87 */
88 HWTEST_F(WebPictureInPictureControllerInterfaceTest, Create, TestSize.Level1)
89 {
90 pipConfig.mainWindowId = 0;
91 WMError ret = controller->Create(pipConfig);
92 EXPECT_EQ(ret, WMError::WM_ERROR_INVALID_PARAM);
93 pipConfig.mainWindowId = 100;
94 pipConfig.width = 0;
95 ret = controller->Create(pipConfig);
96 EXPECT_EQ(ret, WMError::WM_ERROR_INVALID_PARAM);
97 pipConfig.height = 0;
98 ret = controller->Create(pipConfig);
99 EXPECT_EQ(ret, WMError::WM_ERROR_INVALID_PARAM);
100 pipConfig.width = 100;
101 ret = controller->Create(pipConfig);
102 EXPECT_EQ(ret, WMError::WM_ERROR_INVALID_PARAM);
103 pipConfig.height = 150;
104 pipConfig.env = nullptr;
105 ret = controller->Create(pipConfig);
106 EXPECT_EQ(ret, WMError::WM_ERROR_INVALID_PARAM);
107 int num = 0;
108 pipConfig.env = reinterpret_cast<napi_env>(&num);
109 pipConfig.pipTemplateType = 5;
110 ret = controller->Create(pipConfig);
111 EXPECT_EQ(ret, WMError::WM_ERROR_INVALID_PARAM);
112 pipConfig.pipTemplateType = 0;
113 pipConfig.controlGroup = {101, 102, 201, 202};
114 ret = controller->Create(pipConfig);
115 EXPECT_EQ(ret, WMError::WM_ERROR_INVALID_PARAM);
116 pipConfig.controlGroup = {201};
117 ret = controller->Create(pipConfig);
118 EXPECT_EQ(ret, WMError::WM_ERROR_INVALID_PARAM);
119 pipConfig.pipTemplateType = 1;
120 ret = controller->Create(pipConfig);
121 EXPECT_EQ(ret, WMError::WM_OK);
122 pipConfig.pipTemplateType = 0;
123 pipConfig.controlGroup = {101, 102};
124 ret = controller->Create(pipConfig);
125 EXPECT_EQ(ret, WMError::WM_ERROR_INVALID_PARAM);
126 }
127
128 /**
129 * @tc.name: StartPip
130 * @tc.desc: StartPip
131 * @tc.type: FUNC
132 */
133 HWTEST_F(WebPictureInPictureControllerInterfaceTest, StartPip, TestSize.Level1)
134 {
135 WMError ret = controller->StartPip(0);
136 EXPECT_EQ(ret, WMError::WM_ERROR_PIP_INTERNAL_ERROR);
137 controller->Create(pipConfig);
138 ret = controller->StartPip(0);
139 EXPECT_EQ(ret, WMError::WM_ERROR_PIP_CREATE_FAILED);
140 }
141
142 /**
143 * @tc.name: StopPip
144 * @tc.desc: StopPip
145 * @tc.type: FUNC
146 */
147 HWTEST_F(WebPictureInPictureControllerInterfaceTest, StopPip, TestSize.Level1)
148 {
149 WMError ret = controller->StopPip();
150 EXPECT_EQ(ret, WMError::WM_ERROR_PIP_INTERNAL_ERROR);
151 controller->Create(pipConfig);
152 ret = controller->StopPip();
153 EXPECT_EQ(ret, WMError::WM_ERROR_PIP_STATE_ABNORMALLY);
154 }
155
156 /**
157 * @tc.name: UpdateContentSize
158 * @tc.desc: UpdateContentSize
159 * @tc.type: FUNC
160 */
161 HWTEST_F(WebPictureInPictureControllerInterfaceTest, UpdateContentSize, TestSize.Level1)
162 {
163 WMError ret =controller->UpdateContentSize(0, 0);
164 EXPECT_EQ(ret, WMError::WM_ERROR_INVALID_PARAM);
165 ret =controller->UpdateContentSize(0, 10);
166 EXPECT_EQ(ret, WMError::WM_ERROR_INVALID_PARAM);
167 ret =controller->UpdateContentSize(10, 0);
168 EXPECT_EQ(ret, WMError::WM_ERROR_INVALID_PARAM);
169 ret =controller->UpdateContentSize(10, 10);
170 EXPECT_EQ(ret, WMError::WM_ERROR_PIP_INTERNAL_ERROR);
171 controller->Create(pipConfig);
172 ret =controller->UpdateContentSize(10, 10);
173 EXPECT_EQ(ret, WMError::WM_OK);
174 }
175
176 /**
177 * @tc.name: UpdatePiPControlStatus
178 * @tc.desc: UpdatePiPControlStatus
179 * @tc.type: FUNC
180 */
181 HWTEST_F(WebPictureInPictureControllerInterfaceTest, UpdatePiPControlStatus, TestSize.Level1)
182 {
183 auto controlType = PiPControlType::VIDEO_PLAY_PAUSE;
184 auto status = PiPControlStatus::PAUSE;
185 WMError ret =controller->UpdatePiPControlStatus(controlType, status);
186 EXPECT_EQ(ret, WMError::WM_ERROR_PIP_INTERNAL_ERROR);
187 controller->Create(pipConfig);
188 ret =controller->UpdatePiPControlStatus(controlType, status);
189 EXPECT_EQ(ret, WMError::WM_OK);
190 }
191
192 /**
193 * @tc.name: setPiPControlEnabled
194 * @tc.desc: setPiPControlEnabled
195 * @tc.type: FUNC
196 */
197 HWTEST_F(WebPictureInPictureControllerInterfaceTest, setPiPControlEnabled, TestSize.Level1)
198 {
199 auto controlType = PiPControlType::VIDEO_PREVIOUS;
200 WMError ret =controller->setPiPControlEnabled(controlType, true);
201 EXPECT_EQ(ret, WMError::WM_ERROR_PIP_INTERNAL_ERROR);
202 controller->Create(pipConfig);
203 ret = controller->setPiPControlEnabled(controlType, false);
204 EXPECT_EQ(ret, WMError::WM_OK);
205 }
206
207 /**
208 * @tc.name: RegisterStartPipListener
209 * @tc.desc: RegisterStartPipListener
210 * @tc.type: FUNC
211 */
212 HWTEST_F(WebPictureInPictureControllerInterfaceTest, RegisterStartPipListener, TestSize.Level1)
213 {
214 controller->Create(pipConfig);
215 WMError ret = controller->RegisterStartPipListener(PipStartPipCallback);
216 EXPECT_EQ(WMError::WM_OK, ret);
217 ret = controller->RegisterStartPipListener(nullptr);
218 EXPECT_EQ(WMError::WM_ERROR_INVALID_PARAM, ret);
219 ret = controller->RegisterStartPipListener(PipStartPipCallback);
220 EXPECT_EQ(WMError::WM_ERROR_INVALID_PARAM, ret);
221 }
222
223 /**
224 * @tc.name: UnregisterStartPipListener
225 * @tc.desc: UnregisterStartPipListener
226 * @tc.type: FUNC
227 */
228 HWTEST_F(WebPictureInPictureControllerInterfaceTest, UnregisterStartPipListener, TestSize.Level1)
229 {
230 controller->Create(pipConfig);
231 WMError ret = controller->RegisterStartPipListener(PipStartPipCallback);
232 EXPECT_EQ(WMError::WM_OK, ret);
233 ret = controller->UnregisterStartPipListener(PipStartPipCallback);
234 EXPECT_EQ(WMError::WM_OK, ret);
235
236 ret = controller->UnregisterStartPipListener(nullptr);
237 EXPECT_EQ(WMError::WM_ERROR_INVALID_PARAM, ret);
238
239 ret = controller->UnregisterStartPipListener(PipStartPipCallback);
240 EXPECT_EQ(WMError::WM_ERROR_INVALID_PARAM, ret);
241 }
242
243 /**
244 * @tc.name: RegisterLifeCycleListener
245 * @tc.desc: RegisterLifeCycleListener
246 * @tc.type: FUNC
247 */
248 HWTEST_F(WebPictureInPictureControllerInterfaceTest, RegisterLifeCycleListener, TestSize.Level1)
249 {
250 controller->Create(pipConfig);
251 WMError ret = controller->RegisterLifeCycleListener(PipLifeCycleCallback);
252 EXPECT_EQ(WMError::WM_OK, ret);
253 ret = controller->RegisterLifeCycleListener(nullptr);
254 EXPECT_EQ(WMError::WM_ERROR_INVALID_PARAM, ret);
255 ret = controller->RegisterLifeCycleListener(PipLifeCycleCallback);
256 EXPECT_EQ(WMError::WM_ERROR_INVALID_PARAM, ret);
257 }
258
259 /**
260 * @tc.name: UnregisterLifeCycleListener
261 * @tc.desc: UnregisterLifeCycleListener
262 * @tc.type: FUNC
263 */
264 HWTEST_F(WebPictureInPictureControllerInterfaceTest, UnregisterLifeCycleListener, TestSize.Level1)
265 {
266 controller->Create(pipConfig);
267 WMError ret = controller->RegisterLifeCycleListener(PipLifeCycleCallback);
268 EXPECT_EQ(WMError::WM_OK, ret);
269 ret = controller->UnregisterLifeCycleListener(PipLifeCycleCallback);
270 EXPECT_EQ(WMError::WM_OK, ret);
271
272 ret = controller->UnregisterLifeCycleListener(nullptr);
273 EXPECT_EQ(WMError::WM_ERROR_INVALID_PARAM, ret);
274
275 ret = controller->UnregisterLifeCycleListener(PipLifeCycleCallback);
276 EXPECT_EQ(WMError::WM_ERROR_INVALID_PARAM, ret);
277
278 controller->sptrWebPipController_ = nullptr;
279 ret = controller->UnregisterLifeCycleListener(PipLifeCycleCallback);
280 EXPECT_EQ(WMError::WM_ERROR_PIP_INTERNAL_ERROR, ret);
281 }
282
283 /**
284 * @tc.name: RegisterControlEventListener
285 * @tc.desc: RegisterControlEventListener
286 * @tc.type: FUNC
287 */
288 HWTEST_F(WebPictureInPictureControllerInterfaceTest, RegisterControlEventListener, TestSize.Level1)
289 {
290 controller->Create(pipConfig);
291 WMError ret = controller->RegisterControlEventListener(PipControlEventCallback);
292 EXPECT_EQ(WMError::WM_OK, ret);
293 ret = controller->RegisterControlEventListener(nullptr);
294 EXPECT_EQ(WMError::WM_ERROR_INVALID_PARAM, ret);
295 ret = controller->RegisterControlEventListener(PipControlEventCallback);
296 EXPECT_EQ(WMError::WM_ERROR_INVALID_PARAM, ret);
297 }
298
299 /**
300 * @tc.name: UnregisterControlEventListener
301 * @tc.desc: UnregisterControlEventListener
302 * @tc.type: FUNC
303 */
304 HWTEST_F(WebPictureInPictureControllerInterfaceTest, UnregisterControlEventListener, TestSize.Level1)
305 {
306 controller->Create(pipConfig);
307 WMError ret = controller->RegisterControlEventListener(PipControlEventCallback);
308 EXPECT_EQ(WMError::WM_OK, ret);
309 ret = controller->UnregisterControlEventListener(PipControlEventCallback);
310 EXPECT_EQ(WMError::WM_OK, ret);
311
312 ret = controller->UnregisterControlEventListener(nullptr);
313 EXPECT_EQ(WMError::WM_ERROR_INVALID_PARAM, ret);
314
315 ret = controller->UnregisterControlEventListener(PipControlEventCallback);
316 EXPECT_EQ(WMError::WM_ERROR_INVALID_PARAM, ret);
317
318 controller->sptrWebPipController_ = nullptr;
319 ret = controller->UnregisterControlEventListener(PipControlEventCallback);
320 EXPECT_EQ(WMError::WM_ERROR_PIP_INTERNAL_ERROR, ret);
321 }
322
323 /**
324 * @tc.name: RegisterResizeListener
325 * @tc.desc: RegisterResizeListener
326 * @tc.type: FUNC
327 */
328 HWTEST_F(WebPictureInPictureControllerInterfaceTest, RegisterResizeListener, TestSize.Level1)
329 {
330 controller->Create(pipConfig);
331 WMError ret = controller->RegisterResizeListener(PipResizeCallback);
332 EXPECT_EQ(WMError::WM_OK, ret);
333 ret = controller->RegisterResizeListener(nullptr);
334 EXPECT_EQ(WMError::WM_ERROR_INVALID_PARAM, ret);
335 ret = controller->RegisterResizeListener(PipResizeCallback);
336 EXPECT_EQ(WMError::WM_ERROR_INVALID_PARAM, ret);
337 }
338
339 /**
340 * @tc.name: UnregisterResizeListener
341 * @tc.desc: UnregisterResizeListener
342 * @tc.type: FUNC
343 */
344 HWTEST_F(WebPictureInPictureControllerInterfaceTest, UnregisterResizeListener, TestSize.Level1)
345 {
346 controller->Create(pipConfig);
347 WMError ret = controller->RegisterResizeListener(PipResizeCallback);
348 EXPECT_EQ(WMError::WM_OK, ret);
349 ret = controller->UnregisterResizeListener(PipResizeCallback);
350 EXPECT_EQ(WMError::WM_OK, ret);
351
352 ret = controller->UnregisterResizeListener(nullptr);
353 EXPECT_EQ(WMError::WM_ERROR_INVALID_PARAM, ret);
354
355 ret = controller->UnregisterResizeListener(PipResizeCallback);
356 EXPECT_EQ(WMError::WM_ERROR_INVALID_PARAM, ret);
357
358 controller->sptrWebPipController_ = nullptr;
359 ret = controller->UnregisterResizeListener(PipResizeCallback);
360 EXPECT_EQ(WMError::WM_ERROR_PIP_INTERNAL_ERROR, ret);
361 }
362
363 /**
364 * @tc.name: UnregisterAllPiPStart
365 * @tc.desc: UnregisterAllPiPStart
366 * @tc.type: FUNC
367 */
368 HWTEST_F(WebPictureInPictureControllerInterfaceTest, UnregisterAllPiPStart, TestSize.Level1)
369 {
370 controller->Create(pipConfig);
371 WMError ret = controller->RegisterStartPipListener(PipStartPipCallback);
372 EXPECT_EQ(WMError::WM_OK, ret);
373 ret = controller->UnregisterAllPiPStart();
374 EXPECT_EQ(WMError::WM_OK, ret);
375 EXPECT_EQ(controller->startPipCallbackSet_.size(), 0);
376
377 controller->sptrWebPipController_ = nullptr;
378 ret = controller->UnregisterAllPiPStart();
379 EXPECT_EQ(WMError::WM_ERROR_PIP_INTERNAL_ERROR, ret);
380 }
381
382 /**
383 * @tc.name: UnregisterAllPiPLifecycle
384 * @tc.desc: UnregisterAllPiPLifecycle
385 * @tc.type: FUNC
386 */
387 HWTEST_F(WebPictureInPictureControllerInterfaceTest, UnregisterAllPiPLifecycle, TestSize.Level1)
388 {
389 controller->Create(pipConfig);
390 WMError ret = controller->RegisterLifeCycleListener(PipLifeCycleCallback);
391 EXPECT_EQ(WMError::WM_OK, ret);
392 ret = controller->UnregisterAllPiPLifecycle();
393 EXPECT_EQ(WMError::WM_OK, ret);
394 EXPECT_EQ(controller->lifeCycleCallbackSet_.size(), 0);
395
396 controller->sptrWebPipController_ = nullptr;
397 ret = controller->UnregisterAllPiPLifecycle();
398 EXPECT_EQ(WMError::WM_ERROR_PIP_INTERNAL_ERROR, ret);
399 }
400
401 /**
402 * @tc.name: UnregisterAllPiPControlObserver
403 * @tc.desc: UnregisterAllPiPControlObserver
404 * @tc.type: FUNC
405 */
406 HWTEST_F(WebPictureInPictureControllerInterfaceTest, UnregisterAllPiPControlObserver, TestSize.Level1)
407 {
408 controller->Create(pipConfig);
409 WMError ret = controller->RegisterControlEventListener(PipControlEventCallback);
410 EXPECT_EQ(WMError::WM_OK, ret);
411 ret = controller->UnregisterAllPiPControlObserver();
412 EXPECT_EQ(WMError::WM_OK, ret);
413 EXPECT_EQ(controller->controlEventCallbackSet_.size(), 0);
414
415 controller->sptrWebPipController_ = nullptr;
416 ret = controller->UnregisterAllPiPControlObserver();
417 EXPECT_EQ(WMError::WM_ERROR_PIP_INTERNAL_ERROR, ret);
418 }
419
420 /**
421 * @tc.name: UnregisterAllPiPWindowSize
422 * @tc.desc: UnregisterAllPiPWindowSize
423 * @tc.type: FUNC
424 */
425 HWTEST_F(WebPictureInPictureControllerInterfaceTest, UnregisterAllPiPWindowSize, TestSize.Level1)
426 {
427 controller->Create(pipConfig);
428 WMError ret = controller->RegisterResizeListener(PipResizeCallback);
429 EXPECT_EQ(WMError::WM_OK, ret);
430 ret = controller->UnregisterAllPiPWindowSize();
431 EXPECT_EQ(WMError::WM_OK, ret);
432 EXPECT_EQ(controller->resizeCallbackSet_.size(), 0);
433
434 controller->sptrWebPipController_ = nullptr;
435 ret = controller->UnregisterAllPiPWindowSize();
436 EXPECT_EQ(WMError::WM_ERROR_PIP_INTERNAL_ERROR, ret);
437 }
438
439 /**
440 * @tc.name: SetPipInitialSurfaceRect
441 * @tc.desc: SetPipInitialSurfaceRect
442 * @tc.type: FUNC
443 */
444 HWTEST_F(WebPictureInPictureControllerInterfaceTest, SetPipInitialSurfaceRect, TestSize.Level1)
445 {
446 controller->Create(pipConfig);
447 WMError ret = controller->SetPipInitialSurfaceRect(10, 10, 0, 20);
448 EXPECT_EQ(WMError::WM_ERROR_INVALID_PARAM, ret);
449 ret = controller->SetPipInitialSurfaceRect(10, 10, 20, 0);
450 EXPECT_EQ(WMError::WM_ERROR_INVALID_PARAM, ret);
451 ret = controller->SetPipInitialSurfaceRect(10, 10, 0, 0);
452 EXPECT_EQ(WMError::WM_ERROR_INVALID_PARAM, ret);
453 ret = controller->SetPipInitialSurfaceRect(10, 10, 20, 20);
454 EXPECT_EQ(WMError::WM_OK, ret);
455 controller->sptrWebPipController_ = nullptr;
456 ret = controller->SetPipInitialSurfaceRect(10, 10, 20, 20);
457 EXPECT_EQ(WMError::WM_ERROR_PIP_INTERNAL_ERROR, ret);
458 }
459
460 /**
461 * @tc.name: UnsetPipInitialSurfaceRect
462 * @tc.desc: UnsetPipInitialSurfaceRect
463 * @tc.type: FUNC
464 */
465 HWTEST_F(WebPictureInPictureControllerInterfaceTest, UnsetPipInitialSurfaceRect, TestSize.Level1)
466 {
467 controller->Create(pipConfig);
468 WMError ret = controller->SetPipInitialSurfaceRect(10, 10, 20, 20);
469 EXPECT_EQ(WMError::WM_ERROR_PIP_INTERNAL_ERROR, ret);
470 ret = controller->UnsetPipInitialSurfaceRect();
471 EXPECT_EQ(WMError::WM_OK, ret);
472 controller->sptrWebPipController_ = nullptr;
473 ret = controller->UnsetPipInitialSurfaceRect();
474 EXPECT_EQ(WMError::WM_ERROR_PIP_INTERNAL_ERROR, ret);
475 }
476 }
477 }
478 }