1 /*
2 * Copyright (c) 2023-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
18 #include <gmock/gmock.h>
19 #include "ability_context_impl.h"
20 #include "mock_session.h"
21 #include "modifier_render_thread/rs_modifiers_draw_thread.h"
22 #include "parameters.h"
23 #include "picture_in_picture_controller.h"
24 #include "picture_in_picture_manager.h"
25 #include "window.h"
26 #include "window_session_impl.h"
27 #include "wm_common.h"
28 #include "xcomponent_controller.h"
29
30 using namespace testing;
31 using namespace testing::ext;
32
33 namespace OHOS {
34 namespace Rosen {
35 class MockWindow : public Window {
36 public:
MockWindow()37 MockWindow() {};
~MockWindow()38 ~MockWindow() {};
39 MOCK_METHOD3(Show, WMError(uint32_t reason, bool withAnimation, bool withFocus));
40 MOCK_METHOD1(Destroy, WMError(uint32_t reason));
41 MOCK_METHOD0(NotifyPrepareClosePiPWindow, WMError());
42 MOCK_METHOD4(SetAutoStartPiP, void(bool isAutoStart, uint32_t priority, uint32_t width, uint32_t height));
43 MOCK_CONST_METHOD0(GetWindowState, WindowState());
44 };
45
46 class MockXComponentController : public XComponentController {
47 public:
MockXComponentController()48 MockXComponentController() {}
~MockXComponentController()49 ~MockXComponentController() {}
50 MOCK_METHOD2(GetGlobalPosition, XComponentControllerErrorCode(float& offsetX, float& offsetY));
51 MOCK_METHOD2(GetSize, XComponentControllerErrorCode(float& width, float& height));
52 MOCK_METHOD1(SetExtController,
53 XComponentControllerErrorCode(std::shared_ptr<XComponentController> xComponentController));
54 MOCK_METHOD1(ResetExtController,
55 XComponentControllerErrorCode(std::shared_ptr<XComponentController> xComponentController));
56 };
57
58 class PictureInPictureControllerTest : public testing::Test {
59 public:
60 static void SetUpTestCase();
61 static void TearDownTestCase();
62 void SetUp() override;
63 void TearDown() override;
64 private:
65 static constexpr uint32_t WAIT_SYNC_IN_NS = 200000;
66 };
67
SetUpTestCase()68 void PictureInPictureControllerTest::SetUpTestCase() {}
69
TearDownTestCase()70 void PictureInPictureControllerTest::TearDownTestCase()
71 {
72 #ifdef RS_ENABLE_VK
73 RSModifiersDrawThread::Destroy();
74 #endif
75 }
76
SetUp()77 void PictureInPictureControllerTest::SetUp() {}
78
TearDown()79 void PictureInPictureControllerTest::TearDown() {}
80
81 namespace {
82 /**
83 * @tc.name: ShowPictureInPictureWindow01
84 * @tc.desc: ShowPictureInPictureWindow
85 * @tc.type: FUNC
86 */
87 HWTEST_F(PictureInPictureControllerTest, ShowPictureInPictureWindow01, TestSize.Level1)
88 {
89 StartPipType startType = StartPipType::NULL_START;
90 sptr<MockWindow> mw = new (std::nothrow) MockWindow();
91 ASSERT_NE(nullptr, mw);
92 sptr<MockWindow> mw1 = new (std::nothrow) MockWindow();
93 ASSERT_NE(nullptr, mw1);
94 sptr<PipOption> option = new (std::nothrow) PipOption();
95 ASSERT_NE(nullptr, option);
96 sptr<PictureInPictureController> pipControl =
97 new (std::nothrow) PictureInPictureController(option, mw, 100, nullptr);
98
99 pipControl->pipOption_ = nullptr;
100 ASSERT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, pipControl->ShowPictureInPictureWindow(startType));
101 pipControl->pipOption_ = option;
102
103 pipControl->window_ = nullptr;
104 ASSERT_EQ(WMError::WM_ERROR_PIP_STATE_ABNORMALLY, pipControl->ShowPictureInPictureWindow(startType));
105 pipControl->window_ = mw;
106
107 auto listener = sptr<IPiPLifeCycle>::MakeSptr();
108 ASSERT_NE(nullptr, listener);
109 pipControl->RegisterPiPLifecycle(listener);
110 EXPECT_CALL(*(mw), Show(_, _, _)).Times(1).WillOnce(Return(WMError::WM_DO_NOTHING));
111 ASSERT_EQ(WMError::WM_ERROR_PIP_INTERNAL_ERROR, pipControl->ShowPictureInPictureWindow(startType));
112 EXPECT_CALL(*(mw), Show(_, _, _)).Times(1).WillOnce(Return(WMError::WM_OK));
113 ASSERT_EQ(WMError::WM_OK, pipControl->ShowPictureInPictureWindow(startType));
114 EXPECT_CALL(*(mw), Show(_, _, _)).Times(1).WillOnce(Return(WMError::WM_OK));
115 startType = StartPipType::AUTO_START;
116 ASSERT_EQ(WMError::WM_OK, pipControl->ShowPictureInPictureWindow(startType));
117 startType = StartPipType::NULL_START;
118 pipControl->pipOption_->SetContentSize(10, 10);
119 EXPECT_CALL(*(mw), Show(_, _, _)).Times(1).WillOnce(Return(WMError::WM_OK));
120 ASSERT_EQ(WMError::WM_OK, pipControl->ShowPictureInPictureWindow(startType));
121 pipControl->pipOption_->SetContentSize(0, 10);
122 EXPECT_CALL(*(mw), Show(_, _, _)).Times(1).WillOnce(Return(WMError::WM_OK));
123 ASSERT_EQ(WMError::WM_OK, pipControl->ShowPictureInPictureWindow(startType));
124 pipControl->pipOption_->SetContentSize(10, 0);
125 EXPECT_CALL(*(mw), Show(_, _, _)).Times(1).WillOnce(Return(WMError::WM_OK));
126 ASSERT_EQ(WMError::WM_OK, pipControl->ShowPictureInPictureWindow(startType));
127 pipControl->pipOption_->SetContentSize(0, 0);
128 EXPECT_CALL(*(mw), Show(_, _, _)).Times(1).WillOnce(Return(WMError::WM_OK));
129 ASSERT_EQ(WMError::WM_OK, pipControl->ShowPictureInPictureWindow(startType));
130 }
131
132 /**
133 * @tc.name: StopPictureInPicture01
134 * @tc.desc: StopPictureInPicture
135 * @tc.type: FUNC
136 */
137 HWTEST_F(PictureInPictureControllerTest, StopPictureInPicture01, TestSize.Level1)
138 {
139 sptr<MockWindow> mw = new (std::nothrow) MockWindow();
140 ASSERT_NE(nullptr, mw);
141 sptr<PipOption> option = new (std::nothrow) PipOption();
142 ASSERT_NE(nullptr, option);
143 sptr<PictureInPictureController> pipControl =
144 new (std::nothrow) PictureInPictureController(option, mw, 100, nullptr);
145
146 pipControl->curState_ = PiPWindowState::STATE_STOPPING;
147 pipControl->isStoppedFromClient_ = true;
148 ASSERT_EQ(WMError::WM_ERROR_PIP_STATE_ABNORMALLY, pipControl->StopPictureInPicture(true, StopPipType::NULL_STOP));
149
150 pipControl->isStoppedFromClient_ = false;
151 ASSERT_EQ(WMError::WM_ERROR_PIP_REPEAT_OPERATION, pipControl->StopPictureInPicture(true, StopPipType::NULL_STOP));
152
153 pipControl->curState_ = PiPWindowState::STATE_STOPPED;
154 ASSERT_EQ(WMError::WM_ERROR_PIP_REPEAT_OPERATION, pipControl->StopPictureInPicture(true, StopPipType::NULL_STOP));
155 pipControl->curState_ = PiPWindowState::STATE_UNDEFINED;
156 ASSERT_EQ(WMError::WM_ERROR_PIP_STATE_ABNORMALLY, pipControl->StopPictureInPicture(false, StopPipType::NULL_STOP));
157
158 pipControl->window_ = nullptr;
159 ASSERT_EQ(WMError::WM_ERROR_PIP_STATE_ABNORMALLY, pipControl->StopPictureInPicture(true, StopPipType::NULL_STOP));
160
161 pipControl->window_ = mw;
162 pipControl->isStoppedFromClient_ = true;
163 pipControl->curState_ = PiPWindowState::STATE_STOPPING;
164 ASSERT_EQ(WMError::WM_OK, pipControl->StopPictureInPicture(false, StopPipType::NULL_STOP));
165
166 pipControl->window_ = mw;
167 pipControl->curState_ = PiPWindowState::STATE_STARTED;
168 ASSERT_EQ(PiPWindowState::STATE_STARTED, pipControl->GetControllerState());
169 ASSERT_EQ(WMError::WM_OK, pipControl->StopPictureInPicture(true, StopPipType::NULL_STOP));
170 ASSERT_NE(PiPWindowState::STATE_STARTED, pipControl->GetControllerState());
171 }
172
173 /**
174 * @tc.name: CreatePictureInPictureWindow
175 * @tc.desc: CreatePictureInPictureWindow
176 * @tc.type: FUNC
177 */
178 HWTEST_F(PictureInPictureControllerTest, CreatePictureInPictureWindow01, TestSize.Level1)
179 {
180 auto mw = sptr<MockWindow>::MakeSptr();
181 ASSERT_NE(nullptr, mw);
182 auto option = sptr<PipOption>::MakeSptr();
183 ASSERT_NE(nullptr, option);
184 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
185
186 pipControl->pipOption_ = nullptr;
187 StartPipType startType = StartPipType::AUTO_START;
188 EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, pipControl->CreatePictureInPictureWindow(startType));
189 pipControl->pipOption_ = option;
190 option->SetContext(nullptr);
191 ASSERT_EQ(nullptr, option->GetContext());
192 EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, pipControl->CreatePictureInPictureWindow(startType));
193 AbilityRuntime::AbilityContextImpl* contextPtr = new AbilityRuntime::AbilityContextImpl();
194 option->SetContext(contextPtr);
195 pipControl->mainWindow_ = nullptr;
196 EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, pipControl->CreatePictureInPictureWindow(startType));
197 delete contextPtr;
198 }
199
200 /**
201 * @tc.name: CreatePictureInPictureWindow
202 * @tc.desc: CreatePictureInPictureWindow
203 * @tc.type: FUNC
204 */
205 HWTEST_F(PictureInPictureControllerTest, CreatePictureInPictureWindow02, TestSize.Level1)
206 {
207 auto mw = sptr<MockWindow>::MakeSptr();
208 ASSERT_NE(nullptr, mw);
209 auto option = sptr<PipOption>::MakeSptr();
210 ASSERT_NE(nullptr, option);
211 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
212 pipControl->pipOption_ = option;
213 StartPipType startType = StartPipType::AUTO_START;
214 AbilityRuntime::AbilityContextImpl* contextPtr = new AbilityRuntime::AbilityContextImpl();
215 option->SetContext(contextPtr);
216
217 std::shared_ptr<MockXComponentController> xComponentController = std::make_shared<MockXComponentController>();
218 ASSERT_NE(nullptr, xComponentController);
219 pipControl->pipOption_->SetXComponentController(nullptr);
220 pipControl->pipOption_->SetTypeNodeEnabled(false);
221 pipControl->mainWindow_ = nullptr;
222 EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, pipControl->CreatePictureInPictureWindow(startType));
223 pipControl->pipOption_->SetXComponentController(xComponentController);
224 ASSERT_EQ(pipControl->pipOption_->GetXComponentController(), xComponentController);
225 EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, pipControl->CreatePictureInPictureWindow(startType));
226 pipControl->pipOption_->SetXComponentController(nullptr);
227 pipControl->mainWindow_ = mw;
228 EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, pipControl->CreatePictureInPictureWindow(startType));
229 delete contextPtr;
230 }
231
232 /**
233 * @tc.name: CreatePictureInPictureWindow
234 * @tc.desc: CreatePictureInPictureWindow
235 * @tc.type: FUNC
236 */
237 HWTEST_F(PictureInPictureControllerTest, CreatePictureInPictureWindow03, TestSize.Level1)
238 {
239 auto mw = sptr<MockWindow>::MakeSptr();
240 ASSERT_NE(nullptr, mw);
241 auto option = sptr<PipOption>::MakeSptr();
242 ASSERT_NE(nullptr, option);
243 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
244 pipControl->pipOption_ = option;
245 option->SetDefaultWindowSizeType(1);
246 AbilityRuntime::AbilityContextImpl* contextPtr = new AbilityRuntime::AbilityContextImpl();
247 option->SetContext(contextPtr);
248 std::shared_ptr<MockXComponentController> xComponentController = std::make_shared<MockXComponentController>();
249 pipControl->pipOption_->SetXComponentController(xComponentController);
250 ASSERT_EQ(pipControl->pipOption_->GetXComponentController(), xComponentController);
251 pipControl->pipOption_->SetTypeNodeEnabled(true);
252 pipControl->mainWindow_ = mw;
253 StartPipType startType = StartPipType::NULL_START;
254 EXPECT_CALL(*(mw), GetWindowState()).Times(2).WillOnce(Return(WindowState::STATE_CREATED));
255 EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, pipControl->CreatePictureInPictureWindow(startType));
256 EXPECT_EQ(1, option->GetDefaultWindowSizeType());
257 startType = StartPipType::AUTO_START;
258 delete contextPtr;
259 }
260
261 /**
262 * @tc.name: StartPictureInPicture
263 * @tc.desc: StartPictureInPicture
264 * @tc.type: FUNC
265 */
266 HWTEST_F(PictureInPictureControllerTest, StartPictureInPicture01, TestSize.Level1)
267 {
268 StartPipType startType = StartPipType::AUTO_START;
269 auto mw = sptr<MockWindow>::MakeSptr();
270 ASSERT_NE(nullptr, mw);
271 auto option = sptr<PipOption>::MakeSptr();
272 ASSERT_NE(nullptr, option);
273 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
274
275 pipControl->pipOption_ = nullptr;
276 EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, pipControl->StartPictureInPicture(startType));
277 pipControl->pipOption_ = option;
278 option->SetContext(nullptr);
279 ASSERT_EQ(nullptr, option->GetContext());
280 EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, pipControl->StartPictureInPicture(startType));
281 AbilityRuntime::AbilityContextImpl* contextPtr = new AbilityRuntime::AbilityContextImpl();
282 option->SetContext(contextPtr);
283 pipControl->curState_ = PiPWindowState::STATE_STARTING;
284 EXPECT_EQ(WMError::WM_ERROR_PIP_REPEAT_OPERATION, pipControl->StartPictureInPicture(startType));
285 pipControl->curState_ = PiPWindowState::STATE_STARTED;
286 EXPECT_EQ(WMError::WM_ERROR_PIP_REPEAT_OPERATION, pipControl->StartPictureInPicture(startType));
287 delete contextPtr;
288 }
289
290 /**
291 * @tc.name: StartPictureInPicture
292 * @tc.desc: StartPictureInPicture
293 * @tc.type: FUNC
294 */
295 HWTEST_F(PictureInPictureControllerTest, StartPictureInPicture02, TestSize.Level1)
296 {
297 StartPipType startType = StartPipType::AUTO_START;
298 auto mw = sptr<MockWindow>::MakeSptr();
299 ASSERT_NE(nullptr, mw);
300 auto option = sptr<PipOption>::MakeSptr();
301 ASSERT_NE(nullptr, option);
302 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
303 pipControl->pipOption_ = option;
304 AbilityRuntime::AbilityContextImpl* contextPtr = new AbilityRuntime::AbilityContextImpl();
305 option->SetContext(contextPtr);
306 pipControl->curState_ = PiPWindowState::STATE_UNDEFINED;
307
308 pipControl->pipOption_->SetNavigationId("navId");
309 pipControl->mainWindow_ = nullptr;
310 EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, pipControl->StartPictureInPicture(startType));
311 pipControl->pipOption_->SetNavigationId("");
312 auto pipControl1 = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
313 auto pipControl2 = sptr<PictureInPictureController>::MakeSptr(option, mw, 200, nullptr);
314 PictureInPictureManager::SetActiveController(pipControl1);
315 PictureInPictureManager::IsActiveController(pipControl2);
316 pipControl->mainWindowId_ = 100;
317 PictureInPictureManager::IsAttachedToSameWindow(100);
318 pipControl->window_ = nullptr;
319 EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, pipControl->StartPictureInPicture(startType));
320 pipControl->window_ = mw;
321 pipControl->pipOption_ = nullptr;
322 EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, pipControl->StartPictureInPicture(startType));
323 pipControl->pipOption_ = option;
324 PictureInPictureManager::RemoveActiveController(pipControl1);
325 PictureInPictureManager::IsActiveController(pipControl);
326 pipControl->StartPictureInPicture(startType);
327 delete contextPtr;
328 }
329
330 /**
331 * @tc.name: StartPictureInPictureInner
332 * @tc.desc: StartPictureInPictureInner
333 * @tc.type: FUNC
334 */
335 HWTEST_F(PictureInPictureControllerTest, StartPictureInPictureInner, TestSize.Level1)
336 {
337 StartPipType startType = StartPipType::USER_START;
338 auto mw = sptr<MockWindow>::MakeSptr();
339 ASSERT_NE(nullptr, mw);
340 auto option = sptr<PipOption>::MakeSptr();
341 ASSERT_NE(nullptr, option);
342 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
343 pipControl->pipOption_->SetTypeNodeEnabled(true);
344 ASSERT_NE(WMError::WM_OK, pipControl->StartPictureInPictureInner(startType));
345 }
346
347 /**
348 * @tc.name: StopPictureInPictureFromClient
349 * @tc.desc: StopPictureInPictureFromClient
350 * @tc.type: FUNC
351 */
352 HWTEST_F(PictureInPictureControllerTest, StopPictureInPictureFromClient, TestSize.Level1)
353 {
354 sptr<MockWindow> mw = new (std::nothrow) MockWindow();
355 ASSERT_NE(nullptr, mw);
356 sptr<MockWindow> mw1 = new (std::nothrow) MockWindow();
357 ASSERT_NE(nullptr, mw1);
358 sptr<PipOption> option = new (std::nothrow) PipOption();
359 ASSERT_NE(nullptr, option);
360 sptr<PictureInPictureController> pipControl =
361 new (std::nothrow) PictureInPictureController(option, mw, 100, nullptr);
362
363 pipControl->window_ = mw1;
364 ASSERT_NE(WMError::WM_ERROR_PIP_STATE_ABNORMALLY, pipControl->StopPictureInPictureFromClient());
365 pipControl->window_ = nullptr;
366 ASSERT_NE(WMError::WM_OK, pipControl->StopPictureInPictureFromClient());
367 pipControl->window_ = mw1;
368 pipControl->curState_ = PiPWindowState::STATE_STOPPING;
369 EXPECT_EQ(WMError::WM_ERROR_PIP_REPEAT_OPERATION, pipControl->StopPictureInPictureFromClient());
370 pipControl->curState_ = PiPWindowState::STATE_STOPPED;
371 EXPECT_EQ(WMError::WM_ERROR_PIP_REPEAT_OPERATION, pipControl->StopPictureInPictureFromClient());
372 pipControl->curState_ = PiPWindowState::STATE_RESTORING;
373 EXPECT_EQ(WMError::WM_ERROR_PIP_REPEAT_OPERATION, pipControl->StopPictureInPictureFromClient());
374 pipControl->curState_ = PiPWindowState::STATE_UNDEFINED;
375 EXPECT_CALL(*(mw1), NotifyPrepareClosePiPWindow()).Times(1).WillOnce(Return(WMError::WM_DO_NOTHING));
376 EXPECT_EQ(WMError::WM_ERROR_PIP_DESTROY_FAILED, pipControl->StopPictureInPictureFromClient());
377 EXPECT_CALL(*(mw1), NotifyPrepareClosePiPWindow()).Times(1).WillOnce(Return(WMError::WM_OK));
378 EXPECT_EQ(WMError::WM_OK, pipControl->StopPictureInPictureFromClient());
379 }
380
381 /**
382 * @tc.name: GetPipWindow
383 * @tc.desc: GetPipWindow/SetPipWindow
384 * @tc.type: FUNC
385 */
386 HWTEST_F(PictureInPictureControllerTest, GetPipWindow, TestSize.Level1)
387 {
388 sptr<MockWindow> mw = new MockWindow();
389 sptr<PipOption> option = new PipOption();
390 sptr<Window> window_;
391 sptr<Window> window;
392 sptr<PictureInPictureController> pipControl = new PictureInPictureController(option, mw, 100, nullptr);
393
394 pipControl->SetPipWindow(window);
395 auto ret = pipControl->GetPipWindow();
396 ASSERT_EQ(pipControl->window_, ret);
397 }
398
399 /**
400 * @tc.name: SetAutoStartEnabled
401 * @tc.desc: SetAutoStartEnabled
402 * @tc.type: FUNC
403 */
404 HWTEST_F(PictureInPictureControllerTest, SetAutoStartEnabled, TestSize.Level1)
405 {
406 bool enable = true;
407 sptr<MockWindow> mw = new (std::nothrow) MockWindow();
408 ASSERT_NE(nullptr, mw);
409 sptr<PipOption> option = new (std::nothrow) PipOption();
410 ASSERT_NE(nullptr, option);
411 sptr<PictureInPictureController> pipControl =
412 new (std::nothrow) PictureInPictureController(option, mw, 100, nullptr);
413
414 pipControl->mainWindow_ = nullptr;
415 pipControl->SetAutoStartEnabled(enable);
416 pipControl->mainWindow_ = mw;
417 pipControl->pipOption_ = nullptr;
418 pipControl->SetAutoStartEnabled(enable);
419 pipControl->pipOption_ = option;
420
421 pipControl->isAutoStartEnabled_ = enable;
422 ASSERT_EQ(true, pipControl->isAutoStartEnabled_);
423 pipControl->pipOption_->SetTypeNodeEnabled(true);
424 ASSERT_EQ(true, pipControl->IsTypeNodeEnabled());
425 EXPECT_CALL(*(mw), SetAutoStartPiP(_, _, _, _)).WillRepeatedly(Return());
426 pipControl->SetAutoStartEnabled(enable);
427 enable = false;
428 pipControl->isAutoStartEnabled_ = enable;
429 ASSERT_EQ(false, pipControl->isAutoStartEnabled_);
430 pipControl->pipOption_->SetTypeNodeEnabled(true);
431 ASSERT_EQ(true, pipControl->IsTypeNodeEnabled());
432 EXPECT_CALL(*(mw), SetAutoStartPiP(_, _, _, _)).WillRepeatedly(Return());
433 pipControl->SetAutoStartEnabled(enable);
434 pipControl->pipOption_->SetTypeNodeEnabled(false);
435 ASSERT_EQ(false, pipControl->IsTypeNodeEnabled());
436 EXPECT_CALL(*(mw), SetAutoStartPiP(_, _, _, _)).WillRepeatedly(Return());
437 pipControl->SetAutoStartEnabled(enable);
438 pipControl->pipOption_->SetNavigationId("");
439 ASSERT_EQ("", pipControl->pipOption_->GetNavigationId());
440 pipControl->SetAutoStartEnabled(enable);
441 }
442
443 /**
444 * @tc.name: IsAutoStartEnabled
445 * @tc.desc: IsAutoStartEnabled
446 * @tc.type: FUNC
447 */
448 HWTEST_F(PictureInPictureControllerTest, IsAutoStartEnabled, TestSize.Level1)
449 {
450 bool enable = true;
451 sptr<MockWindow> mw = new MockWindow();
452 sptr<PipOption> option = new PipOption();
453 sptr<PictureInPictureController> pipControl = new PictureInPictureController(option, mw, 100, nullptr);
454
455 pipControl->IsAutoStartEnabled(enable);
456 auto ret = pipControl->GetControllerState();
457 ASSERT_EQ(PiPWindowState::STATE_UNDEFINED, ret);
458 }
459
460 /**
461 * @tc.name: UpdateContentSize01
462 * @tc.desc: UpdateContentSize
463 * @tc.type: FUNC
464 */
465 HWTEST_F(PictureInPictureControllerTest, UpdateContentSize01, TestSize.Level1)
466 {
467 sptr<MockWindow> mw = new (std::nothrow) MockWindow();
468 ASSERT_NE(nullptr, mw);
469 sptr<PipOption> option = new (std::nothrow) PipOption();
470 ASSERT_NE(nullptr, option);
471 sptr<PictureInPictureController> pipControl =
472 new (std::nothrow) PictureInPictureController(option, mw, 100, nullptr);
473
474 int32_t width = 0;
475 int32_t height = 0;
476 pipControl->UpdateContentSize(width, height);
477 height = 150;
478 pipControl->UpdateContentSize(width, height);
479 height = 0;
480 width = 100;
481 pipControl->UpdateContentSize(width, height);
482 height = 150;
483 pipControl->UpdateContentSize(width, height);
484
485 pipControl->curState_ = PiPWindowState::STATE_UNDEFINED;
486 pipControl->UpdateContentSize(width, height);
487 pipControl->curState_ = PiPWindowState::STATE_STARTED;
488 pipControl->UpdateContentSize(width, height);
489 }
490
491 /**
492 * @tc.name: UpdateContentSize02
493 * @tc.desc: UpdateContentSize
494 * @tc.type: FUNC
495 */
496 HWTEST_F(PictureInPictureControllerTest, UpdateContentSize02, TestSize.Level1)
497 {
498 auto mw = sptr<MockWindow>::MakeSptr();
499 ASSERT_NE(nullptr, mw);
500 auto option = sptr<PipOption>::MakeSptr();
501 ASSERT_NE(nullptr, option);
502 std::shared_ptr<MockXComponentController> xComponentController = std::make_shared<MockXComponentController>();
503 ASSERT_NE(nullptr, xComponentController);
504 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
505
506 pipControl->curState_ = PiPWindowState::STATE_STARTED;
507 pipControl->window_ = nullptr;
508 int32_t width = 10;
509 int32_t height = 20;
510 pipControl->UpdateContentSize(width, height);
511 pipControl->window_ = mw;
512
513 pipControl->pipOption_->SetTypeNodeEnabled(true);
514 ASSERT_EQ(true, pipControl->IsTypeNodeEnabled());
515 pipControl->mainWindowXComponentController_ = xComponentController;
516 pipControl->UpdateContentSize(width, height);
517 pipControl->pipOption_->SetTypeNodeEnabled(false);
518 pipControl->windowRect_ = { 0, 0, 0, 0 };
519 pipControl->IsContentSizeChanged(0, 0, 0, 0);
520 pipControl->UpdateContentSize(width, height);
521 pipControl->IsContentSizeChanged(10, 10, 10, 10);
522 pipControl->UpdateContentSize(width, height);
523 pipControl->mainWindowXComponentController_ = nullptr;
524 pipControl->UpdateContentSize(width, height);
525 }
526
527 /**
528 * @tc.name: UpdatePiPControlStatus
529 * @tc.desc: UpdatePiPControlStatus
530 * @tc.type: FUNC
531 */
532 HWTEST_F(PictureInPictureControllerTest, UpdatePiPControlStatus, TestSize.Level1)
533 {
534 auto mw = sptr<MockWindow>::MakeSptr();
535 ASSERT_NE(nullptr, mw);
536 auto option = sptr<PipOption>::MakeSptr();
537 ASSERT_NE(nullptr, option);
538 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
539 auto controlType = PiPControlType::VIDEO_PLAY_PAUSE;
540 auto status = PiPControlStatus::ENABLED;
541 pipControl->UpdatePiPControlStatus(controlType, status);
542 ASSERT_EQ(1, pipControl->pipOption_->GetControlEnable().size());
543 status = PiPControlStatus::PLAY;
544 pipControl->UpdatePiPControlStatus(controlType, status);
545 ASSERT_EQ(1, pipControl->pipOption_->GetControlStatus().size());
546 pipControl->window_ = nullptr;
547 pipControl->UpdatePiPControlStatus(controlType, status);
548 pipControl->window_ = mw;
549 pipControl->UpdatePiPControlStatus(controlType, status);
550 }
551
552 /**
553 * @tc.name: IsContentSizeChanged
554 * @tc.desc: IsContentSizeChanged
555 * @tc.type: FUNC
556 */
557 HWTEST_F(PictureInPictureControllerTest, IsContentSizeChanged, TestSize.Level1)
558 {
559 sptr<MockWindow> mw = new (std::nothrow) MockWindow();
560 ASSERT_NE(nullptr, mw);
561 sptr<PipOption> option = new (std::nothrow) PipOption();
562 ASSERT_NE(nullptr, option);
563 sptr<PictureInPictureController> pipControl =
564 new (std::nothrow) PictureInPictureController(option, mw, 100, nullptr);
565 pipControl->windowRect_ = { 0, 0, 0, 0 };
566 ASSERT_EQ(true, pipControl->IsContentSizeChanged(10.5, 0, 0, 0));
567 ASSERT_EQ(true, pipControl->IsContentSizeChanged(0, 10.5, 0, 0));
568 ASSERT_EQ(true, pipControl->IsContentSizeChanged(0, 0, 10.5, 0));
569 ASSERT_EQ(true, pipControl->IsContentSizeChanged(0, 0, 0, 10.5));
570 ASSERT_EQ(true, pipControl->IsContentSizeChanged(10.5, 10.5, 0, 0));
571 ASSERT_EQ(true, pipControl->IsContentSizeChanged(10.5, 0, 10.5, 0));
572 ASSERT_EQ(true, pipControl->IsContentSizeChanged(10.5, 0, 0, 10.5));
573 ASSERT_EQ(true, pipControl->IsContentSizeChanged(0, 10.5, 10.5, 0));
574 ASSERT_EQ(true, pipControl->IsContentSizeChanged(0, 10.5, 0, 10.5));
575 ASSERT_EQ(true, pipControl->IsContentSizeChanged(0, 0, 10.5, 10.5));
576 ASSERT_EQ(true, pipControl->IsContentSizeChanged(10.5, 10.5, 10.5, 0));
577 ASSERT_EQ(true, pipControl->IsContentSizeChanged(10.5, 10.5, 0, 10.5));
578 ASSERT_EQ(true, pipControl->IsContentSizeChanged(10.5, 0, 10.5, 10.5));
579 ASSERT_EQ(true, pipControl->IsContentSizeChanged(0, 10.5, 10.5, 10.5));
580 ASSERT_EQ(true, pipControl->IsContentSizeChanged(10.5, 10.5, 10.5, 10.5));
581 ASSERT_EQ(false, pipControl->IsContentSizeChanged(0, 0, 0, 0));
582 }
583
584 /**
585 * @tc.name: DoActionEvent
586 * @tc.desc: DoActionEvent
587 * @tc.type: FUNC
588 */
589 HWTEST_F(PictureInPictureControllerTest, DoActionEvent, TestSize.Level1)
590 {
591 std::string actionName = "";
592 int32_t status = 0;
593 auto mw = sptr<MockWindow>::MakeSptr();
594 ASSERT_NE(nullptr, mw);
595 auto option = sptr<PipOption>::MakeSptr();
596 ASSERT_NE(nullptr, option);
597 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
598 sptr<IPiPActionObserver> listener = nullptr;
599
600 pipControl->DoActionEvent(actionName, status);
601 ASSERT_EQ(0, pipControl->pipOption_->GetControlStatus().size());
602 actionName = "nextVideo";
603 pipControl->DoActionEvent(actionName, status);
604 ASSERT_EQ(1, pipControl->pipOption_->GetControlStatus().size());
605 }
606
607 /**
608 * @tc.name: DoControlEvent
609 * @tc.desc: DoControlEvent
610 * @tc.type: FUNC
611 */
612 HWTEST_F(PictureInPictureControllerTest, DoControlEvent, TestSize.Level1)
613 {
614 auto controlType = PiPControlType::VIDEO_PLAY_PAUSE;
615 auto status = PiPControlStatus::PLAY;
616 auto mw = sptr<MockWindow>::MakeSptr();
617 ASSERT_NE(nullptr, mw);
618 auto option = sptr<PipOption>::MakeSptr();
619 ASSERT_NE(nullptr, option);
620 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
621 sptr<IPiPControlObserver> listener = nullptr;
622
623 pipControl->pipOption_ = nullptr;
624 pipControl->DoControlEvent(controlType, status);
625 pipControl->pipOption_ = option;
626 pipControl->DoControlEvent(controlType, status);
627 pipControl->RegisterPiPControlObserver(listener);
628 pipControl->DoControlEvent(controlType, status);
629 ASSERT_EQ(1, pipControl->pipOption_->GetControlStatus().size());
630 }
631
632 /**
633 * @tc.name: PreRestorePictureInPicture
634 * @tc.desc: PreRestorePictureInPicture
635 * @tc.type: FUNC
636 */
637 HWTEST_F(PictureInPictureControllerTest, PreRestorePictureInPicture, TestSize.Level1)
638 {
639 sptr<IPiPLifeCycle> listener = nullptr;
640 auto mw = sptr<MockWindow>::MakeSptr();
641 ASSERT_NE(nullptr, mw);
642 auto option = sptr<PipOption>::MakeSptr();
643 ASSERT_NE(nullptr, option);
644 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
645 pipControl->curState_ = PiPWindowState::STATE_STARTED;
646 pipControl->RegisterPiPLifecycle(listener);
647 pipControl->PreRestorePictureInPicture();
648 ASSERT_EQ(PiPWindowState::STATE_RESTORING, pipControl->curState_);
649 }
650
651 /**
652 * @tc.name: RestorePictureInPictureWindow
653 * @tc.desc: RestorePictureInPictureWindow
654 * @tc.type: FUNC
655 */
656 HWTEST_F(PictureInPictureControllerTest, RestorePictureInPictureWindow, TestSize.Level1)
657 {
658 auto mw = sptr<MockWindow>::MakeSptr();
659 ASSERT_NE(nullptr, mw);
660 auto option = sptr<PipOption>::MakeSptr();
661 ASSERT_NE(nullptr, option);
662 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
663
664 pipControl->curState_ = PiPWindowState::STATE_STARTED;
665 pipControl->window_ = mw;
666 pipControl->RestorePictureInPictureWindow();
667 ASSERT_EQ(PiPWindowState::STATE_STOPPING, pipControl->curState_);
668 }
669
670 /**
671 * @tc.name: UpdateWinRectByComponent
672 * @tc.desc: UpdateWinRectByComponent
673 * @tc.type: FUNC
674 */
675 HWTEST_F(PictureInPictureControllerTest, UpdateWinRectByComponent, TestSize.Level1)
676 {
677 auto mw = sptr<MockWindow>::MakeSptr();
678 ASSERT_NE(nullptr, mw);
679 auto option = sptr<PipOption>::MakeSptr();
680 ASSERT_NE(nullptr, option);
681 std::shared_ptr<MockXComponentController> xComponentController = std::make_shared<MockXComponentController>();
682 ASSERT_NE(nullptr, xComponentController);
683 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
684
685 pipControl->pipOption_->SetTypeNodeEnabled(true);
686 pipControl->UpdateWinRectByComponent();
687 ASSERT_EQ(pipControl->windowRect_.width_, 16);
688 ASSERT_EQ(pipControl->windowRect_.height_, 9);
689
690 pipControl->pipOption_->SetTypeNodeEnabled(false);
691 pipControl->mainWindowXComponentController_ = nullptr;
692 pipControl->UpdateWinRectByComponent();
693 pipControl->mainWindowXComponentController_ = xComponentController;
694
695 pipControl->windowRect_.width_ = 0;
696 pipControl->windowRect_.height_ = 10;
697 pipControl->UpdateWinRectByComponent();
698 ASSERT_EQ(pipControl->windowRect_.height_, 0);
699 pipControl->windowRect_.width_ = 10;
700 pipControl->windowRect_.height_ = 0;
701 pipControl->UpdateWinRectByComponent();
702 pipControl->windowRect_.width_ = 0;
703 pipControl->UpdateWinRectByComponent();
704 pipControl->windowRect_.width_ = 10;
705 pipControl->windowRect_.height_ = 10;
706 pipControl->UpdateWinRectByComponent();
707 ASSERT_EQ(pipControl->windowRect_.posX_, 0);
708 ASSERT_EQ(pipControl->windowRect_.posY_, 0);
709 }
710
711 /**
712 * @tc.name: RegisterPiPLifecycle
713 * @tc.desc: RegisterPiPLifecycle/UnregisterPiPLifecycle
714 * @tc.type: FUNC
715 */
716 HWTEST_F(PictureInPictureControllerTest, RegisterListener, TestSize.Level1)
717 {
718 auto mw = sptr<MockWindow>::MakeSptr();
719 ASSERT_NE(nullptr, mw);
720 auto option = sptr<PipOption>::MakeSptr();
721 ASSERT_NE(nullptr, option);
722 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
723
724 auto listener = sptr<IPiPLifeCycle>::MakeSptr();
725 ASSERT_NE(nullptr, listener);
726 auto listener1 = sptr<IPiPLifeCycle>::MakeSptr();
727 ASSERT_NE(nullptr, listener1);
728 pipControl->pipLifeCycleListeners_.push_back(listener);
729 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, pipControl->RegisterPiPLifecycle(nullptr));
730 ASSERT_EQ(WMError::WM_OK, pipControl->RegisterPiPLifecycle(listener));
731 ASSERT_EQ(WMError::WM_OK, pipControl->RegisterPiPLifecycle(listener1));
732 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, pipControl->UnregisterPiPLifecycle(nullptr));
733 ASSERT_EQ(WMError::WM_OK, pipControl->UnregisterPiPLifecycle(listener));
734 }
735
736 /**
737 * @tc.name: RegisterPiPActionObserver
738 * @tc.desc: RegisterPiPActionObserver/UnregisterPiPActionObserver
739 * @tc.type: FUNC
740 */
741 HWTEST_F(PictureInPictureControllerTest, RegisterPiPActionObserver, TestSize.Level1)
742 {
743 auto mw = sptr<MockWindow>::MakeSptr();
744 ASSERT_NE(nullptr, mw);
745 auto option = sptr<PipOption>::MakeSptr();
746 ASSERT_NE(nullptr, option);
747 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
748
749 auto listener = sptr<IPiPActionObserver>::MakeSptr();
750 ASSERT_NE(nullptr, listener);
751 auto listener1 = sptr<IPiPActionObserver>::MakeSptr();
752 ASSERT_NE(nullptr, listener1);
753 pipControl->pipActionObservers_.push_back(listener);
754 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, pipControl->RegisterPiPActionObserver(nullptr));
755 ASSERT_EQ(WMError::WM_OK, pipControl->RegisterPiPActionObserver(listener));
756 ASSERT_EQ(WMError::WM_OK, pipControl->RegisterPiPActionObserver(listener1));
757 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, pipControl->UnregisterPiPActionObserver(nullptr));
758 ASSERT_EQ(WMError::WM_OK, pipControl->UnregisterPiPActionObserver(listener));
759 }
760
761 /**
762 * @tc.name: RegisterPiPControlObserver
763 * @tc.desc: RegisterPiPControlObserver/UnregisterPiPControlObserver
764 * @tc.type: FUNC
765 */
766 HWTEST_F(PictureInPictureControllerTest, RegisterPiPControlObserver, TestSize.Level1)
767 {
768 auto mw = sptr<MockWindow>::MakeSptr();
769 ASSERT_NE(nullptr, mw);
770 auto option = sptr<PipOption>::MakeSptr();
771 ASSERT_NE(nullptr, option);
772 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
773
774 auto listener = sptr<IPiPControlObserver>::MakeSptr();
775 ASSERT_NE(nullptr, listener);
776 auto listener1 = sptr<IPiPControlObserver>::MakeSptr();
777 ASSERT_NE(nullptr, listener1);
778 pipControl->pipControlObservers_.push_back(listener);
779 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, pipControl->RegisterPiPControlObserver(nullptr));
780 ASSERT_EQ(WMError::WM_OK, pipControl->RegisterPiPControlObserver(listener));
781 ASSERT_EQ(WMError::WM_OK, pipControl->RegisterPiPControlObserver(listener1));
782 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, pipControl->UnregisterPiPControlObserver(nullptr));
783 ASSERT_EQ(WMError::WM_OK, pipControl->UnregisterPiPControlObserver(listener));
784 }
785
786 /**
787 * @tc.name: IsPullPiPAndHandleNavigation
788 * @tc.desc: IsPullPiPAndHandleNavigation
789 * @tc.type: FUNC
790 */
791 HWTEST_F(PictureInPictureControllerTest, IsPullPiPAndHandleNavigation, TestSize.Level1)
792 {
793 sptr<MockWindow> mw = new MockWindow();
794 sptr<PipOption> option = new PipOption();
795 sptr<PictureInPictureController> pipControl = new PictureInPictureController(option, mw, 100, nullptr);
796 pipControl->pipOption_->SetTypeNodeEnabled(true);
797 ASSERT_EQ(true, pipControl->IsPullPiPAndHandleNavigation());
798 pipControl->pipOption_->SetTypeNodeEnabled(false);
799 pipControl->pipOption_->SetNavigationId("");
800 ASSERT_EQ(true, pipControl->IsPullPiPAndHandleNavigation());
801 pipControl->pipOption_->SetNavigationId("navId");
802 pipControl->mainWindow_ = nullptr;
803 ASSERT_EQ(false, pipControl->IsPullPiPAndHandleNavigation());
804 pipControl->mainWindow_ = mw;
805 }
806
807 /**
808 * @tc.name: ResetExtController
809 * @tc.desc: ResetExtController
810 * @tc.type: FUNC
811 */
812 HWTEST_F(PictureInPictureControllerTest, ResetExtController, TestSize.Level1)
813 {
814 auto mw = sptr<MockWindow>::MakeSptr();
815 ASSERT_NE(nullptr, mw);
816 auto option = sptr<PipOption>::MakeSptr();
817 ASSERT_NE(nullptr, option);
818 std::shared_ptr<MockXComponentController> xComponentController = std::make_shared<MockXComponentController>();
819 ASSERT_NE(nullptr, xComponentController);
820 std::shared_ptr<MockXComponentController> xComponentController1 = std::make_shared<MockXComponentController>();
821 ASSERT_NE(nullptr, xComponentController1);
822 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
823
824 pipControl->pipOption_->SetTypeNodeEnabled(true);
825 pipControl->ResetExtController();
826 pipControl->pipOption_->SetTypeNodeEnabled(false);
827 pipControl->ResetExtController();
828
829 pipControl->pipXComponentController_ = nullptr;
830 pipControl->mainWindowXComponentController_ = nullptr;
831 pipControl->ResetExtController();
832 pipControl->mainWindowXComponentController_ = nullptr;
833 pipControl->pipXComponentController_ = xComponentController;
834 pipControl->ResetExtController();
835 pipControl->pipXComponentController_ = nullptr;
836 pipControl->mainWindowXComponentController_ = xComponentController1;
837 pipControl->ResetExtController();
838 pipControl->pipXComponentController_ = xComponentController;
839 EXPECT_CALL(*(xComponentController1), ResetExtController(_))
840 .Times(1)
841 .WillOnce(Return(XComponentControllerErrorCode::XCOMPONENT_CONTROLLER_TYPE_ERROR));
842 pipControl->ResetExtController();
843 EXPECT_CALL(*(xComponentController1), ResetExtController(_))
844 .Times(1)
845 .WillOnce(Return(XComponentControllerErrorCode::XCOMPONENT_CONTROLLER_NO_ERROR));
846 pipControl->ResetExtController();
847 }
848
849 /**
850 * @tc.name: OnPictureInPictureStart
851 * @tc.desc: OnPictureInPictureStart
852 * @tc.type: FUNC
853 */
854 HWTEST_F(PictureInPictureControllerTest, OnPictureInPictureStart, TestSize.Level1)
855 {
856 auto mw = sptr<MockWindow>::MakeSptr();
857 ASSERT_NE(nullptr, mw);
858 auto option = sptr<PipOption>::MakeSptr();
859 ASSERT_NE(nullptr, option);
860 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
861 pipControl->OnPictureInPictureStart();
862 }
863
864 /**
865 * @tc.name: IsTypeNodeEnabled
866 * @tc.desc: IsTypeNodeEnabled
867 * @tc.type: FUNC
868 */
869 HWTEST_F(PictureInPictureControllerTest, IsTypeNodeEnabled, TestSize.Level1)
870 {
871 auto mw = sptr<MockWindow>::MakeSptr();
872 ASSERT_NE(nullptr, mw);
873 auto option = sptr<PipOption>::MakeSptr();
874 ASSERT_NE(nullptr, option);
875 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
876 pipControl->pipOption_->SetTypeNodeEnabled(true);
877 ASSERT_TRUE(pipControl->IsTypeNodeEnabled());
878 pipControl->pipOption_->SetTypeNodeEnabled(false);
879 ASSERT_TRUE(!pipControl->IsTypeNodeEnabled());
880 pipControl->pipOption_ = nullptr;
881 ASSERT_TRUE(!pipControl->IsTypeNodeEnabled());
882 }
883
884 /**
885 * @tc.name: GetTypeNode
886 * @tc.desc: GetTypeNode
887 * @tc.type: FUNC
888 */
889 HWTEST_F(PictureInPictureControllerTest, GetTypeNode, TestSize.Level1)
890 {
891 auto mw = sptr<MockWindow>::MakeSptr();
892 ASSERT_NE(nullptr, mw);
893 auto option = sptr<PipOption>::MakeSptr();
894 ASSERT_NE(nullptr, option);
895 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
896 pipControl->pipOption_->SetTypeNodeRef(nullptr);
897 ASSERT_EQ(nullptr, pipControl->GetTypeNode());
898 pipControl->pipOption_ = nullptr;
899 ASSERT_EQ(nullptr, pipControl->GetTypeNode());
900 }
901
902 /**
903 * @tc.name: SetXComponentController
904 * @tc.desc: SetXComponentController
905 * @tc.type: FUNC
906 */
907 HWTEST_F(PictureInPictureControllerTest, SetXComponentController, TestSize.Level1)
908 {
909 sptr<IPiPLifeCycle> listener = nullptr;
910 std::shared_ptr<MockXComponentController> xComponentController = std::make_shared<MockXComponentController>();
911 ASSERT_NE(nullptr, xComponentController);
912 std::shared_ptr<MockXComponentController> xComponentController1 = std::make_shared<MockXComponentController>();
913 ASSERT_NE(nullptr, xComponentController1);
914 sptr<MockWindow> mw = new MockWindow();
915 sptr<PipOption> option = new PipOption();
916 sptr<PictureInPictureController> pipControl = new PictureInPictureController(option, mw, 100, nullptr);
917 pipControl->pipOption_->SetTypeNodeEnabled(true);
918 ASSERT_EQ(WMError::WM_OK, pipControl->SetXComponentController(xComponentController));
919 pipControl->pipOption_->SetTypeNodeEnabled(false);
920 pipControl->window_ = nullptr;
921 ASSERT_EQ(WMError::WM_ERROR_PIP_STATE_ABNORMALLY, pipControl->SetXComponentController(xComponentController));
922 pipControl->window_ = mw;
923
924 pipControl->pipXComponentController_ = nullptr;
925 pipControl->mainWindowXComponentController_ = nullptr;
926 ASSERT_EQ(WMError::WM_ERROR_PIP_STATE_ABNORMALLY, pipControl->SetXComponentController(xComponentController));
927 pipControl->mainWindowXComponentController_ = nullptr;
928 pipControl->pipXComponentController_ = xComponentController;
929 ASSERT_EQ(WMError::WM_ERROR_PIP_STATE_ABNORMALLY, pipControl->SetXComponentController(xComponentController));
930 pipControl->mainWindowXComponentController_ = xComponentController1;
931 EXPECT_CALL(*(xComponentController1), SetExtController(_))
932 .Times(1)
933 .WillOnce(Return(XComponentControllerErrorCode::XCOMPONENT_CONTROLLER_TYPE_ERROR));
934 ASSERT_EQ(WMError::WM_ERROR_PIP_INTERNAL_ERROR, pipControl->SetXComponentController(xComponentController));
935 EXPECT_CALL(*(xComponentController1), SetExtController(_))
936 .Times(1)
937 .WillOnce(Return(XComponentControllerErrorCode::XCOMPONENT_CONTROLLER_NO_ERROR));
938 ASSERT_EQ(WMError::WM_OK, pipControl->SetXComponentController(xComponentController));
939 }
940
941 /**
942 * @tc.name: RegisterPiPTypeNodeChange
943 * @tc.desc: RegisterPiPTypeNodeChange
944 * @tc.type: FUNC
945 */
946 HWTEST_F(PictureInPictureControllerTest, RegisterPiPTypeNodeChange, Function | SmallTest | Level2)
947 {
948 sptr<MockWindow> mw = new MockWindow();
949 sptr<PipOption> option = new PipOption();
950 sptr<PictureInPictureController> pipControl = new PictureInPictureController(option, mw, 100, nullptr);
951 auto listener = sptr<IPiPTypeNodeObserver>::MakeSptr();
952 ASSERT_NE(nullptr, listener);
953 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, pipControl->RegisterPiPTypeNodeChange(nullptr));
954 ASSERT_EQ(WMError::WM_OK, pipControl->RegisterPiPTypeNodeChange(listener));
955 }
956
957 /**
958 * @tc.name: UnRegisterPiPTypeNodeChange
959 * @tc.desc: UnRegisterPiPTypeNodeChange
960 * @tc.type: FUNC
961 */
962 HWTEST_F(PictureInPictureControllerTest, UnRegisterPiPTypeNodeChange, Function | SmallTest | Level2)
963 {
964 sptr<MockWindow> mw = new MockWindow();
965 sptr<PipOption> option = new PipOption();
966 sptr<PictureInPictureController> pipControl = new PictureInPictureController(option, mw, 100, nullptr);
967 auto listener = sptr<IPiPTypeNodeObserver>::MakeSptr();
968 ASSERT_NE(nullptr, listener);
969 ASSERT_EQ(WMError::WM_ERROR_NULLPTR, pipControl->UnRegisterPiPTypeNodeChange(nullptr));
970 ASSERT_EQ(WMError::WM_OK, pipControl->UnRegisterPiPTypeNodeChange(listener));
971 }
972
973 /**
974 * @tc.name: UpdateContentNodeRef
975 * @tc.desc: UpdateContentNodeRef
976 * @tc.type: FUNC
977 */
978 HWTEST_F(PictureInPictureControllerTest, UpdateContentNodeRef, TestSize.Level1)
979 {
980 sptr<MockWindow> mw = new MockWindow();
981 sptr<PipOption> option = new PipOption();
982 sptr<PictureInPictureController> pipControl = new PictureInPictureController(option, mw, 100, nullptr);
983 pipControl->pipOption_ = nullptr;
984 napi_ref nodeRef = nullptr;
985 pipControl->UpdateContentNodeRef(nodeRef);
986 pipControl->pipOption_ = option;
987 pipControl->pipOption_->SetTypeNodeEnabled(true);
988 pipControl->UpdateContentNodeRef(nodeRef);
989 pipControl->pipOption_->SetTypeNodeEnabled(false);
990 pipControl->UpdateContentNodeRef(nodeRef);
991 pipControl->isAutoStartEnabled_ = true;
992 pipControl->UpdateContentNodeRef(nodeRef);
993 pipControl->isAutoStartEnabled_ = false;
994 ASSERT_EQ(true, pipControl->IsTypeNodeEnabled());
995 }
996
997 /**
998 * @tc.name: UpdatePiPSourceRect
999 * @tc.desc: UpdatePiPSourceRect
1000 * @tc.type: FUNC
1001 */
1002 HWTEST_F(PictureInPictureControllerTest, UpdatePiPSourceRect, TestSize.Level1)
1003 {
1004 auto mw = sptr<MockWindow>::MakeSptr();
1005 ASSERT_NE(nullptr, mw);
1006 auto option = sptr<PipOption>::MakeSptr();
1007 ASSERT_NE(nullptr, option);
1008 std::shared_ptr<MockXComponentController> xComponentController = std::make_shared<MockXComponentController>();
1009 ASSERT_NE(nullptr, xComponentController);
1010 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
1011
1012 pipControl->pipOption_->SetTypeNodeEnabled(true);
1013 pipControl->window_ = mw;
1014 pipControl->UpdatePiPSourceRect();
1015 ASSERT_EQ(0, pipControl->windowRect_.posX_);
1016 pipControl->pipOption_->SetTypeNodeEnabled(false);
1017 pipControl->UpdatePiPSourceRect();
1018
1019 pipControl->mainWindowXComponentController_ = nullptr;
1020 pipControl->window_ = mw;
1021 pipControl->UpdatePiPSourceRect();
1022 pipControl->mainWindowXComponentController_ = xComponentController;
1023 pipControl->window_ = nullptr;
1024 pipControl->UpdatePiPSourceRect();
1025 pipControl->mainWindowXComponentController_ = nullptr;
1026 pipControl->UpdatePiPSourceRect();
1027 pipControl->mainWindowXComponentController_ = xComponentController;
1028 pipControl->window_ = mw;
1029 pipControl->UpdatePiPSourceRect();
1030 ASSERT_EQ(0, pipControl->windowRect_.posX_);
1031 ASSERT_EQ(0, pipControl->windowRect_.posY_);
1032 ASSERT_EQ(0, pipControl->windowRect_.width_);
1033 ASSERT_EQ(0, pipControl->windowRect_.height_);
1034 }
1035
1036 /**
1037 * @tc.name: DestroyPictureInPictureWindow
1038 * @tc.desc: DestroyPictureInPictureWindow
1039 * @tc.type: FUNC
1040 */
1041 HWTEST_F(PictureInPictureControllerTest, DestroyPictureInPictureWindow, TestSize.Level1)
1042 {
1043 sptr<MockWindow> mw = new (std::nothrow) MockWindow();
1044 ASSERT_NE(nullptr, mw);
1045 sptr<PipOption> option = new (std::nothrow) PipOption();
1046 ASSERT_NE(nullptr, option);
1047 sptr<PictureInPictureController> pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
1048
1049 pipControl->window_ = nullptr;
1050 ASSERT_EQ(WMError::WM_ERROR_PIP_INTERNAL_ERROR, pipControl->DestroyPictureInPictureWindow());
1051
1052 sptr<MockWindow> window = sptr<MockWindow>::MakeSptr();
1053 pipControl->window_ = window;
1054 EXPECT_CALL(*(window), Destroy(0)).Times(1).WillOnce(Return(WMError::WM_DO_NOTHING));
1055 ASSERT_EQ(WMError::WM_ERROR_PIP_DESTROY_FAILED, pipControl->DestroyPictureInPictureWindow());
1056
1057 EXPECT_CALL(*(window), Destroy(0)).Times(1).WillOnce(Return(WMError::WM_OK));
1058 pipControl->pipOption_ = nullptr;
1059 pipControl->mainWindow_ = nullptr;
1060 pipControl->window_ = window;
1061 ASSERT_EQ(WMError::WM_OK, pipControl->DestroyPictureInPictureWindow());
1062 pipControl->mainWindow_ = mw;
1063 pipControl->window_ = window;
1064 EXPECT_CALL(*(window), Destroy(0)).Times(1).WillOnce(Return(WMError::WM_OK));
1065 ASSERT_EQ(WMError::WM_OK, pipControl->DestroyPictureInPictureWindow());
1066 pipControl->pipOption_ = option;
1067 pipControl->pipOption_->SetNavigationId("navId");
1068 pipControl->pipOption_->SetTypeNodeEnabled(false);
1069 pipControl->mainWindow_ = nullptr;
1070 pipControl->window_ = window;
1071 EXPECT_CALL(*(window), Destroy(0)).Times(1).WillOnce(Return(WMError::WM_OK));
1072 ASSERT_EQ(WMError::WM_OK, pipControl->DestroyPictureInPictureWindow());
1073 pipControl->pipOption_->SetNavigationId("");
1074 pipControl->mainWindow_ = mw;
1075 pipControl->window_ = window;
1076 EXPECT_CALL(*(window), Destroy(0)).Times(1).WillOnce(Return(WMError::WM_OK));
1077 ASSERT_EQ(WMError::WM_OK, pipControl->DestroyPictureInPictureWindow());
1078 }
1079
1080 /**
1081 * @tc.name: PrepareSource
1082 * @tc.desc: PrepareSource
1083 * @tc.type: FUNC
1084 */
1085 HWTEST_F(PictureInPictureControllerTest, PrepareSource, TestSize.Level1)
1086 {
1087 auto mw = sptr<MockWindow>::MakeSptr();
1088 ASSERT_NE(nullptr, mw);
1089 auto option = sptr<PipOption>::MakeSptr();
1090 ASSERT_NE(nullptr, option);
1091 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
1092 ASSERT_NE(nullptr, pipControl);
1093 pipControl->pipOption_->SetTypeNodeEnabled(true);
1094 pipControl->PrepareSource();
1095 pipControl->pipOption_->SetTypeNodeEnabled(false);
1096 pipControl->mainWindow_ = nullptr;
1097 pipControl->PrepareSource();
1098 pipControl->mainWindow_ = mw;
1099 pipControl->pipOption_->SetNavigationId("");
1100 pipControl->PrepareSource();
1101 }
1102
1103 /**
1104 * @tc.name: LocateSource
1105 * @tc.desc: LocateSource
1106 * @tc.type: FUNC
1107 */
1108 HWTEST_F(PictureInPictureControllerTest, LocateSource, TestSize.Level1)
1109 {
1110 auto mw = sptr<MockWindow>::MakeSptr();
1111 ASSERT_NE(nullptr, mw);
1112 auto option = sptr<PipOption>::MakeSptr();
1113 ASSERT_NE(nullptr, option);
1114 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
1115 ASSERT_NE(nullptr, pipControl);
1116 pipControl->window_ = nullptr;
1117 pipControl->LocateSource();
1118
1119 pipControl->window_ = mw;
1120 pipControl->LocateSource();
1121 }
1122
1123 /**
1124 * @tc.name: StopPictureInPictureInner
1125 * @tc.desc: StopPictureInPictureInner
1126 * @tc.type: FUNC
1127 */
1128 HWTEST_F(PictureInPictureControllerTest, StopPictureInPictureInner, TestSize.Level1)
1129 {
1130 auto mw = sptr<MockWindow>::MakeSptr();
1131 ASSERT_NE(nullptr, mw);
1132 auto option = sptr<PipOption>::MakeSptr();
1133 ASSERT_NE(nullptr, option);
1134 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
1135
1136 pipControl->pipOption_ = nullptr;
1137 pipControl->StopPictureInPictureInner(StopPipType::NULL_STOP, true);
1138 pipControl->pipOption_ = option;
1139 pipControl->window_ = nullptr;
1140 ASSERT_EQ(WMError::WM_ERROR_PIP_INTERNAL_ERROR,
1141 pipControl->StopPictureInPictureInner(StopPipType::NULL_STOP, true));
1142 pipControl->mainWindow_ = mw;
1143 ASSERT_EQ(WMError::WM_ERROR_PIP_INTERNAL_ERROR,
1144 pipControl->StopPictureInPictureInner(StopPipType::NULL_STOP, true));
1145 auto window = sptr<MockWindow>::MakeSptr();
1146 pipControl->window_ = window;
1147 ASSERT_EQ(WMError::WM_OK, pipControl->StopPictureInPictureInner(StopPipType::NULL_STOP, true));
1148 ASSERT_EQ(WMError::WM_OK, pipControl->StopPictureInPictureInner(StopPipType::NULL_STOP, false));
1149 }
1150
1151 /**
1152 * @tc.name: GetPipPossible
1153 * @tc.desc: GetPipPossible
1154 * @tc.type: FUNC
1155 */
1156 HWTEST_F(PictureInPictureControllerTest, GetPipPossible, TestSize.Level1)
1157 {
1158 auto mw = sptr<MockWindow>::MakeSptr();
1159 ASSERT_NE(nullptr, mw);
1160 auto option = sptr<PipOption>::MakeSptr();
1161 ASSERT_NE(nullptr, option);
1162 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
1163
1164 const std::string multiWindowUIType = system::GetParameter("const.window.multiWindowUIType", "");
1165 bool isDeviceSupported = multiWindowUIType == "HandsetSmartWindow" || multiWindowUIType == "FreeFormMultiWindow" ||
1166 multiWindowUIType == "TabletSmartWindow";
1167
1168 bool pipSupported = false;
1169 pipControl->pipOption_ = option;
1170 pipControl->GetPipPossible(pipSupported);
1171 ASSERT_EQ(isDeviceSupported, pipSupported);
1172 }
1173
1174 /**
1175 * @tc.name: GetPipEnabled
1176 * @tc.desc: GetPipEnabled
1177 * @tc.type: FUNC
1178 */
1179 HWTEST_F(PictureInPictureControllerTest, GetPipEnabled, TestSize.Level1)
1180 {
1181 const std::string multiWindowUIType = system::GetParameter("const.window.multiWindowUIType", "");
1182 bool isDeviceSupported = multiWindowUIType == "HandsetSmartWindow" || multiWindowUIType == "FreeFormMultiWindow" ||
1183 multiWindowUIType == "TabletSmartWindow";
1184 bool pipSupported = PictureInPictureControllerBase::GetPipEnabled();
1185 ASSERT_EQ(isDeviceSupported, pipSupported);
1186 }
1187
1188 /**
1189 * @tc.name: GetPipSettingSwitchStatusEnabled
1190 * @tc.desc: GetPipSettingSwitchStatusEnabled
1191 * @tc.type: FUNC
1192 */
1193 HWTEST_F(PictureInPictureControllerTest, GetPipSettingSwitchStatusEnabled, TestSize.Level1)
1194 {
1195 auto mw = sptr<MockWindow>::MakeSptr();
1196 ASSERT_NE(nullptr, mw);
1197 auto option = sptr<PipOption>::MakeSptr();
1198 ASSERT_NE(nullptr, option);
1199 auto pipControl = sptr<PictureInPictureController>::MakeSptr(option, mw, 100, nullptr);
1200 const std::string multiWindowUIType = system::GetParameter("const.window.multiWindowUIType", "");
1201 bool isDeviceSupported = multiWindowUIType == "HandsetSmartWindow" || multiWindowUIType == "TabletSmartWindow";
1202 ASSERT_EQ(isDeviceSupported, pipControl->GetPipSettingSwitchStatusEnabled());
1203 }
1204
1205 /**
1206 * @tc.name: GetPiPSettingSwitchStatus
1207 * @tc.desc: GetPiPSettingSwitchStatus
1208 * @tc.type: FUNC
1209 */
1210 HWTEST_F(PictureInPictureControllerTest, GetPiPSettingSwitchStatus, TestSize.Level1)
1211 {
1212 auto mw = sptr<MockWindow>::MakeSptr();
1213 ASSERT_NE(nullptr, mw);
1214 auto pipOption = sptr<PipOption>::MakeSptr();
1215 ASSERT_NE(nullptr, pipOption);
1216 auto pipControl = sptr<PictureInPictureController>::MakeSptr(pipOption, mw, 1, nullptr);
1217 EXPECT_EQ(false, pipControl->GetPiPSettingSwitchStatus());
1218
1219 sptr<WindowOption> option = sptr<WindowOption>::MakeSptr();
1220 option->SetWindowName("GetPiPSettingSwitchStatus");
1221 sptr<WindowSessionImpl> window = sptr<WindowSessionImpl>::MakeSptr(option);
1222 window->property_->SetPersistentId(1);
1223 window->property_->SetWindowType(WindowType::WINDOW_TYPE_APP_MAIN_WINDOW);
1224 WindowSessionImpl::windowSessionMap_.clear();
1225 WindowSessionImpl::windowSessionMap_.insert(std::make_pair(window->GetWindowName(),
1226 std::pair<uint64_t, sptr<WindowSessionImpl>>(window->GetWindowId(), window)));
1227 EXPECT_EQ(false, pipControl->GetPiPSettingSwitchStatus());
1228 WindowSessionImpl::windowSessionMap_.clear();
1229 }
1230 } // namespace
1231 } // namespace Rosen
1232 } // namespace OHOS