• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
16 
17 #include <gmock/gmock.h>
18 #include "parameters.h"
19 #include "picture_in_picture_controller.h"
20 #include "picture_in_picture_manager.h"
21 #include "window.h"
22 #include "wm_common.h"
23 #include "xcomponent_controller.h"
24 #include "ability_context_impl.h"
25 #include "web_picture_in_picture_controller.h"
26 
27 using namespace testing;
28 using namespace testing::ext;
29 
30 namespace OHOS {
31 namespace Rosen {
32 class MockWindow : public Window {
33 public:
MockWindow()34     MockWindow() {};
~MockWindow()35     ~MockWindow() {};
36     MOCK_METHOD3(Show, WMError(uint32_t reason, bool withAnimation, bool withFocus));
37     MOCK_METHOD1(Destroy, WMError(uint32_t reason));
38     MOCK_METHOD0(Destroy, WMError());
39     MOCK_METHOD0(NotifyPrepareClosePiPWindow, WMError());
40     MOCK_METHOD4(SetAutoStartPiP, void(bool isAutoStart, uint32_t priority, uint32_t width, uint32_t height));
41     MOCK_CONST_METHOD0(GetWindowState, WindowState());
42 };
43 
44 class MockXComponentController : public XComponentController {
45 public:
MockXComponentController()46     MockXComponentController() {}
~MockXComponentController()47     ~MockXComponentController() {}
48     MOCK_METHOD2(GetGlobalPosition, XComponentControllerErrorCode(float& offsetX, float& offsetY));
49     MOCK_METHOD2(GetSize, XComponentControllerErrorCode(float& width, float& height));
50     MOCK_METHOD1(SetExtController,
51         XComponentControllerErrorCode(std::shared_ptr<XComponentController> xComponentController));
52     MOCK_METHOD1(ResetExtController,
53         XComponentControllerErrorCode(std::shared_ptr<XComponentController> xComponentController));
54 };
55 
56 class WebPictureInPictureControllerTest : public testing::Test {
57 public:
58     static void SetUpTestCase();
59     static void TearDownTestCase();
60     void SetUp() override;
61     void TearDown() override;
62     PiPConfig config;
63 };
64 
SetUpTestCase()65 void WebPictureInPictureControllerTest::SetUpTestCase()
66 {
67 }
68 
TearDownTestCase()69 void WebPictureInPictureControllerTest::TearDownTestCase()
70 {
71 }
72 
SetUp()73 void WebPictureInPictureControllerTest::SetUp()
74 {
75     uint32_t mainWindowId = 100;
76     uint32_t pipTemplateType = 0;
77     uint32_t width = 100;
78     uint32_t height = 150;
79     std::vector<uint32_t> controlGroup = {101};
80     int num = 0;
81     napi_env env = reinterpret_cast<napi_env>(&num);
82     config = {mainWindowId, pipTemplateType, width, height, controlGroup, env};
83 }
84 
TearDown()85 void WebPictureInPictureControllerTest::TearDown()
86 {
87 }
88 
89 namespace {
90 /**
91  * @tc.name: CreatePictureInPictureWindow
92  * @tc.desc: CreatePictureInPictureWindow
93  * @tc.type: FUNC
94  */
95 HWTEST_F(WebPictureInPictureControllerTest, CreatePictureInPictureWindow, TestSize.Level1)
96 {
97     auto mw = sptr<MockWindow>::MakeSptr();
98     ASSERT_NE(nullptr, mw);
99     auto webPipControl = sptr<WebPictureInPictureController>::MakeSptr(config);
100     auto option = webPipControl->pipOption_;
101     webPipControl->pipOption_ = nullptr;
102     StartPipType startType = StartPipType::NATIVE_START;
103     EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, webPipControl->CreatePictureInPictureWindow(startType));
104 
105     webPipControl->pipOption_ = option;
106     webPipControl->mainWindow_ = nullptr;
107     EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, webPipControl->CreatePictureInPictureWindow(startType));
108 
109     webPipControl->mainWindow_ = mw;
110     EXPECT_CALL(*(mw), GetWindowState()).Times(2).WillOnce(Return(WindowState::STATE_CREATED));
111     EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, webPipControl->CreatePictureInPictureWindow(startType));
112 
113     EXPECT_CALL(*(mw), GetWindowState()).Times(2).WillOnce(Return(WindowState::STATE_SHOWN));
114     EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, webPipControl->CreatePictureInPictureWindow(startType));
115 }
116 
117 /**
118  * @tc.name: StartPictureInPicture
119  * @tc.desc: StartPictureInPicture
120  * @tc.type: FUNC
121  */
122 HWTEST_F(WebPictureInPictureControllerTest, StartPictureInPicture, TestSize.Level1)
123 {
124     auto webPipControl = sptr<WebPictureInPictureController>::MakeSptr(config);
125     auto option = webPipControl->pipOption_;
126     webPipControl->pipOption_ = nullptr;
127     StartPipType startType = StartPipType::NATIVE_START;
128     EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, webPipControl->StartPictureInPicture(startType));
129 
130     webPipControl->pipOption_ = option;
131     webPipControl->curState_ = PiPWindowState::STATE_STARTING;
132     EXPECT_EQ(WMError::WM_ERROR_PIP_REPEAT_OPERATION, webPipControl->StartPictureInPicture(startType));
133 
134     webPipControl->curState_ = PiPWindowState::STATE_STARTED;
135     EXPECT_EQ(WMError::WM_ERROR_PIP_REPEAT_OPERATION, webPipControl->StartPictureInPicture(startType));
136 
137     webPipControl->curState_ = PiPWindowState::STATE_UNDEFINED;
138     EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, webPipControl->StartPictureInPicture(startType));
139 }
140 
141 /**
142  * @tc.name: UpdateContentSize
143  * @tc.desc: UpdateContentSize
144  * @tc.type: FUNC
145  */
146 HWTEST_F(WebPictureInPictureControllerTest, UpdateContentSize, TestSize.Level1)
147 {
148     auto mw = sptr<MockWindow>::MakeSptr();
149     ASSERT_NE(nullptr, mw);
150     auto webPipControl = sptr<WebPictureInPictureController>::MakeSptr(config);
151     int32_t width = 0;
152     int32_t height = 0;
153     webPipControl->UpdateContentSize(width, height);
154     height = 150;
155     webPipControl->UpdateContentSize(width, height);
156     height = 0;
157     width = 100;
158     webPipControl->UpdateContentSize(width, height);
159     height = 150;
160     webPipControl->UpdateContentSize(width, height);
161 
162     webPipControl->curState_ = PiPWindowState::STATE_UNDEFINED;
163     webPipControl->UpdateContentSize(width, height);
164     webPipControl->curState_ = PiPWindowState::STATE_STARTED;
165     webPipControl->UpdateContentSize(width, height);
166 
167     webPipControl->window_ = nullptr;
168     webPipControl->UpdateContentSize(width, height);
169     webPipControl->window_ = mw;
170     webPipControl->UpdateContentSize(width, height);
171     EXPECT_EQ(webPipControl->pipOption_->contentWidth_, 100);
172     EXPECT_EQ(webPipControl->pipOption_->contentHeight_, 150);
173 }
174 
175 /**
176  * @tc.name: RestorePictureInPictureWindow
177  * @tc.desc: RestorePictureInPictureWindow
178  * @tc.type: FUNC
179  */
180 HWTEST_F(WebPictureInPictureControllerTest, RestorePictureInPictureWindow, TestSize.Level1)
181 {
182     auto mw = sptr<MockWindow>::MakeSptr();
183     ASSERT_NE(nullptr, mw);
184     auto webPipControl = sptr<WebPictureInPictureController>::MakeSptr(config);
185     webPipControl->window_ = mw;
186     webPipControl->RestorePictureInPictureWindow();
187     EXPECT_EQ(webPipControl->curState_, PiPWindowState::STATE_STOPPED);
188 }
189 
190 /**
191  * @tc.name: SetXComponentController
192  * @tc.desc: SetXComponentController
193  * @tc.type: FUNC
194  */
195 HWTEST_F(WebPictureInPictureControllerTest, SetXComponentController, TestSize.Level1)
196 {
197     std::shared_ptr<MockXComponentController> xComponentController = std::make_shared<MockXComponentController>();
198     ASSERT_NE(nullptr, xComponentController);
199     sptr<MockWindow> mw = new MockWindow();
200     auto webPipControl = sptr<WebPictureInPictureController>::MakeSptr(config);
201     webPipControl->window_ = nullptr;
202     EXPECT_EQ(WMError::WM_ERROR_PIP_STATE_ABNORMALLY, webPipControl->SetXComponentController(xComponentController));
203     webPipControl->window_ = mw;
204     EXPECT_EQ(WMError::WM_OK, webPipControl->SetXComponentController(xComponentController));
205 }
206 
207 /**
208  * @tc.name: GetWebRequestId
209  * @tc.desc: GetWebRequestId
210  * @tc.type: FUNC
211  */
212 HWTEST_F(WebPictureInPictureControllerTest, GetWebRequestId, TestSize.Level1)
213 {
214     auto webPipControl = sptr<WebPictureInPictureController>::MakeSptr(config);
215     EXPECT_EQ(webPipControl->GetWebRequestId(), 0);
216     EXPECT_EQ(webPipControl->GetWebRequestId(), 1);
217     webPipControl->webRequestId_ = UINT8_MAX - 1;
218     EXPECT_EQ(webPipControl->GetWebRequestId(), UINT8_MAX - 1);
219     EXPECT_EQ(webPipControl->GetWebRequestId(), UINT8_MAX);
220     EXPECT_EQ(webPipControl->GetWebRequestId(), 0);
221 }
222 
223 /**
224  * @tc.name: UpdateWinRectByComponent
225  * @tc.desc: UpdateWinRectByComponent
226  * @tc.type: FUNC
227  */
228 HWTEST_F(WebPictureInPictureControllerTest, UpdateWinRectByComponent, TestSize.Level1)
229 {
230     auto webPipControl = sptr<WebPictureInPictureController>::MakeSptr(config);
231     webPipControl->UpdateWinRectByComponent();
232     EXPECT_EQ(webPipControl->windowRect_.width_, 100);
233     EXPECT_EQ(webPipControl->windowRect_.height_, 150);
234 
235     webPipControl->pipOption_->contentWidth_ = 0;
236     webPipControl->pipOption_->contentHeight_ = 0;
237     webPipControl->UpdateWinRectByComponent();
238     EXPECT_EQ(webPipControl->windowRect_.width_, 0);
239     EXPECT_EQ(webPipControl->windowRect_.height_, 0);
240     webPipControl->pipOption_->contentWidth_ = 10;
241     webPipControl->pipOption_->contentHeight_ = 0;
242     webPipControl->UpdateWinRectByComponent();
243     EXPECT_EQ(webPipControl->windowRect_.width_, 10);
244     EXPECT_EQ(webPipControl->windowRect_.height_, 0);
245     webPipControl->UpdateWinRectByComponent();
246     webPipControl->pipOption_->contentWidth_ = 0;
247     webPipControl->UpdateWinRectByComponent();
248     EXPECT_EQ(webPipControl->windowRect_.width_, 0);
249     EXPECT_EQ(webPipControl->windowRect_.height_, 0);
250     webPipControl->pipOption_->contentWidth_ = 10;
251     webPipControl->pipOption_->contentHeight_ = 10;
252     webPipControl->UpdateWinRectByComponent();
253     EXPECT_EQ(webPipControl->windowRect_.posX_, 0);
254     EXPECT_EQ(webPipControl->windowRect_.posY_, 0);
255 }
256 }
257 }
258 }