• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <gmock/gmock.h>
18 #include "picture_in_picture_controller.h"
19 #include "picture_in_picture_manager.h"
20 #include "window.h"
21 
22 using namespace testing;
23 using namespace testing::ext;
24 
25 namespace OHOS {
26 namespace Rosen {
27 class MockWindow : public Window {
28 public:
MockWindow()29     MockWindow() {};
~MockWindow()30     ~MockWindow() {};
31     MOCK_METHOD0(Destroy, WMError());
32 };
33 
34 class PictureInPictureControllerTest : public testing::Test {
35 public:
36     static void SetUpTestCase();
37     static void TearDownTestCase();
38     void SetUp() override;
39     void TearDown() override;
40 };
41 
SetUpTestCase()42 void PictureInPictureControllerTest::SetUpTestCase()
43 {
44 }
45 
TearDownTestCase()46 void PictureInPictureControllerTest::TearDownTestCase()
47 {
48 }
49 
SetUp()50 void PictureInPictureControllerTest::SetUp()
51 {
52 }
53 
TearDown()54 void PictureInPictureControllerTest::TearDown()
55 {
56 }
57 
58 namespace {
59 
60 /**
61  * @tc.name: ShowPictureInPictureWindow01
62  * @tc.desc: ShowPictureInPictureWindow
63  * @tc.type: FUNC
64  */
65 HWTEST_F(PictureInPictureControllerTest, ShowPictureInPictureWindow01, Function | SmallTest | Level2)
66 {
67     sptr<MockWindow> mw = new MockWindow();
68     ASSERT_NE(nullptr, mw);
69     sptr<PipOption> option = new PipOption();
70     PictureInPictureController* pipControl = new PictureInPictureController(option, mw, 100, nullptr);
71     ASSERT_EQ(WMError::WM_ERROR_PIP_STATE_ABNORMALLY, pipControl->ShowPictureInPictureWindow(StartPipType::NULL_START));
72     pipControl->window_ = mw;
73     ASSERT_EQ(WMError::WM_OK, pipControl->ShowPictureInPictureWindow(StartPipType::NULL_START));
74 }
75 
76 /**
77  * @tc.name: StopPictureInPicture01
78  * @tc.desc: StopPictureInPicture
79  * @tc.type: FUNC
80  */
81 HWTEST_F(PictureInPictureControllerTest, StopPictureInPicture01, Function | SmallTest | Level2)
82 {
83     sptr<MockWindow> mw = new MockWindow();
84     ASSERT_NE(nullptr, mw);
85     sptr<PipOption> option = new PipOption();
86     sptr<PictureInPictureController> pipControl = new PictureInPictureController(option, mw, 100, nullptr);
87     ASSERT_EQ(PipWindowState::STATE_UNDEFINED, pipControl->GetControllerState());
88     ASSERT_EQ(WMError::WM_ERROR_PIP_STATE_ABNORMALLY,
89         pipControl->StopPictureInPicture(true, false, StopPipType::NULL_STOP));
90     pipControl->window_ = mw;
91     EXPECT_CALL(*(mw), Destroy()).Times(1).WillOnce(Return(WMError::WM_DO_NOTHING));
92     ASSERT_EQ(WMError::WM_ERROR_PIP_DESTROY_FAILED,
93         pipControl->StopPictureInPicture(true, false, StopPipType::NULL_STOP));
94     ASSERT_EQ(PipWindowState::STATE_UNDEFINED, pipControl->GetControllerState());
95     EXPECT_CALL(*(mw), Destroy()).Times(1).WillOnce(Return(WMError::WM_OK));
96     ASSERT_EQ(WMError::WM_OK, pipControl->StopPictureInPicture(true, false, StopPipType::NULL_STOP));
97     ASSERT_EQ(PipWindowState::STATE_STOPPED, pipControl->GetControllerState());
98 }
99 
100 /**
101  * @tc.name: CreatePictureInPictureWindow
102  * @tc.desc: CreatePictureInPictureWindow
103  * @tc.type: FUNC
104  */
105 HWTEST_F(PictureInPictureControllerTest, CreatePictureInPictureWindow, Function | SmallTest | Level2)
106 {
107     sptr<MockWindow> mw = new MockWindow();
108     sptr<PipOption> option = new PipOption();
109     sptr<PictureInPictureController> pipControl = new PictureInPictureController(option, mw, 100, nullptr);
110     option = nullptr;
111     sptr<WindowOption> windowOption = nullptr;
112 
113     EXPECT_EQ(nullptr, windowOption);
114     EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, pipControl->CreatePictureInPictureWindow());
115     ASSERT_NE(WMError::WM_OK, pipControl->CreatePictureInPictureWindow());
116 }
117 
118 /**
119  * @tc.name: StartPictureInPicture
120  * @tc.desc: StartPictureInPicture
121  * @tc.type: FUNC
122  */
123 HWTEST_F(PictureInPictureControllerTest, StartPictureInPicture, Function | SmallTest | Level2)
124 {
125     StartPipType startType = StartPipType::AUTO_START;
126     sptr<PipOption> pipOption_;
127     sptr<MockWindow> mw = new MockWindow();
128     sptr<PipOption> option = new PipOption();
129     sptr<PictureInPictureController> pipControl = new PictureInPictureController(option, mw, 100, nullptr);
130     pipOption_ = nullptr;
131     sptr<Window> mainWindow_ = nullptr;
132 
133     EXPECT_EQ(true, pipControl->IsPullPiPAndHandleNavigation());
134     ASSERT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, pipControl->StartPictureInPicture(startType));
135 }
136 
137 /**
138  * @tc.name: StartPictureInPictureInner
139  * @tc.desc: StartPictureInPictureInner
140  * @tc.type: FUNC
141  */
142 HWTEST_F(PictureInPictureControllerTest, StartPictureInPictureInner, Function | SmallTest | Level2)
143 {
144     StartPipType startType = StartPipType::AUTO_START;
145     sptr<MockWindow> mw = new MockWindow();
146     sptr<PipOption> option = new PipOption();
147     sptr<PictureInPictureController> pipControl = new PictureInPictureController(option, mw, 100, nullptr);
148 
149     ASSERT_NE(WMError::WM_OK, pipControl->StartPictureInPictureInner(startType));
150 }
151 
152 /**
153  * @tc.name: GetPipWindow
154  * @tc.desc: GetPipWindow
155  * @tc.type: FUNC
156  */
157 HWTEST_F(PictureInPictureControllerTest, GetPipWindow, Function | SmallTest | Level2)
158 {
159     sptr<MockWindow> mw = new MockWindow();
160     sptr<PipOption> option = new PipOption();
161     sptr<Window> window_;
162     uint32_t mainWindowId_ = 0;
163     sptr<Window> window;
164     sptr<PictureInPictureController> pipControl = new PictureInPictureController(option, mw, 100, nullptr);
165 
166     pipControl->SetPipWindow(window);
167     auto ret = pipControl->GetPipWindow();
168     ASSERT_EQ(window_, ret);
169     auto ret1 = pipControl->GetMainWindowId();
170     ASSERT_NE(mainWindowId_, ret1);
171 }
172 
173 /**
174  * @tc.name: SetAutoStartEnabled
175  * @tc.desc: SetAutoStartEnabled
176  * @tc.type: FUNC
177  */
178 HWTEST_F(PictureInPictureControllerTest, SetAutoStartEnabled, Function | SmallTest | Level2)
179 {
180     bool enable = true;
181     sptr<MockWindow> mw = new MockWindow();
182     sptr<PipOption> option = new PipOption();
183     sptr<PictureInPictureController> pipControl = new PictureInPictureController(option, mw, 100, nullptr);
184 
185     pipControl->SetAutoStartEnabled(enable);
186     ASSERT_NE(WMError::WM_OK, pipControl->CreatePictureInPictureWindow());
187 }
188 
189 /**
190  * @tc.name: IsAutoStartEnabled
191  * @tc.desc: IsAutoStartEnabled
192  * @tc.type: FUNC
193  */
194 HWTEST_F(PictureInPictureControllerTest, IsAutoStartEnabled, Function | SmallTest | Level2)
195 {
196     bool enable = true;
197     sptr<MockWindow> mw = new MockWindow();
198     sptr<PipOption> option = new PipOption();
199     sptr<PictureInPictureController> pipControl = new PictureInPictureController(option, mw, 100, nullptr);
200 
201     pipControl->IsAutoStartEnabled(enable);
202     auto ret = pipControl->GetControllerState();
203     ASSERT_EQ(PipWindowState::STATE_UNDEFINED, ret);
204 }
205 
206 /**
207  * @tc.name: UpdateContentSize
208  * @tc.desc: UpdateContentSize
209  * @tc.type: FUNC
210  */
211 HWTEST_F(PictureInPictureControllerTest, UpdateContentSize, Function | SmallTest | Level2)
212 {
213     int32_t width = 0;
214     int32_t height = 0;
215     sptr<MockWindow> mw = new MockWindow();
216     sptr<PipOption> option = new PipOption();
217     sptr<PictureInPictureController> pipControl = new PictureInPictureController(option, mw, 100, nullptr);
218 
219     pipControl->UpdateContentSize(width, height);
220     ASSERT_NE(WMError::WM_OK, pipControl->CreatePictureInPictureWindow());
221 }
222 
223 /**
224  * @tc.name: StartMove
225  * @tc.desc: StartMove
226  * @tc.type: FUNC
227  */
228 HWTEST_F(PictureInPictureControllerTest, StartMove, Function | SmallTest | Level2)
229 {
230     sptr<MockWindow> mw = new MockWindow();
231     sptr<PipOption> option = new PipOption();
232     int32_t type = 0;
233     std::string navigationId = " ";
234     sptr<PictureInPictureController> pipControl = new PictureInPictureController(option, mw, 100, nullptr);
235     GTEST_LOG_(INFO) << "TearDownCasecccccc";
236 
237     pipControl->StartMove();
238     pipControl->DoScale();
239 
240     sptr<PictureInPictureController::PipMainWindowLifeCycleImpl> pipMainWindowLifeCycleImpl =
241         new PictureInPictureController::PipMainWindowLifeCycleImpl(navigationId);
242     GTEST_LOG_(INFO) << "TearDownCasecccccc3";
243 
244     pipMainWindowLifeCycleImpl->AfterBackground();
245     pipMainWindowLifeCycleImpl->BackgroundFailed(type);
246 
247     ASSERT_NE(WMError::WM_OK, pipControl->CreatePictureInPictureWindow());
248 }
249 
250 /**
251  * @tc.name: PipDisplayListener::OnCreate
252  * @tc.desc: PipDisplayListener::OnCreate
253  * @tc.type: FUNC
254  */
255 HWTEST_F(PictureInPictureControllerTest, OnCreate, Function | SmallTest | Level2)
256 {
257     sptr<MockWindow> mw = new MockWindow();
258     sptr<PipOption> option = new PipOption();
259     DisplayId displayId = 0;
260     wptr<PictureInPictureController> pipController = nullptr;
261     sptr<PictureInPictureController> pipControl = new PictureInPictureController(option, mw, 100, nullptr);
262 
263     sptr<PictureInPictureController::PipDisplayListener> pipDisplayListener =
264         new PictureInPictureController::PipDisplayListener(pipController);
265     pipDisplayListener->OnCreate(displayId);
266     pipDisplayListener->OnDestroy(displayId);
267     pipDisplayListener->OnChange(displayId);
268     ASSERT_NE(WMError::WM_OK, pipControl->CreatePictureInPictureWindow());
269 }
270 
271 /**
272  * @tc.name: DoActionEvent
273  * @tc.desc: DoActionEvent
274  * @tc.type: FUNC
275  */
276 HWTEST_F(PictureInPictureControllerTest, DoActionEvent, Function | SmallTest | Level2)
277 {
278     std::string actionName = " ";
279     sptr<MockWindow> mw = new MockWindow();
280     sptr<PipOption> option = new PipOption();
281     sptr<PictureInPictureController> pipControl = new PictureInPictureController(option, mw, 100, nullptr);
282 
283     pipControl->DoActionEvent(actionName);
284     pipControl->RestorePictureInPictureWindow();
285     GTEST_LOG_(INFO) << "TearDownCasecccccc5";
286     pipControl->ResetExtController();
287     ASSERT_NE(WMError::WM_OK, pipControl->CreatePictureInPictureWindow());
288 }
289 
290 /**
291  * @tc.name: SetXComponentController
292  * @tc.desc: SetXComponentController
293  * @tc.type: FUNC
294  */
295 HWTEST_F(PictureInPictureControllerTest, SetXComponentController, Function | SmallTest | Level2)
296 {
297     sptr<IPiPLifeCycle> listener = nullptr;
298     sptr<IPiPActionObserver> listener1 = nullptr;
299     std::shared_ptr<XComponentController> xComponentController = nullptr;
300     sptr<MockWindow> mw = new MockWindow();
301     sptr<PipOption> option = new PipOption();
302     sptr<PictureInPictureController> pipControl = new PictureInPictureController(option, mw, 100, nullptr);
303 
304     auto ret = pipControl->SetXComponentController(xComponentController);
305     pipControl->SetPictureInPictureLifecycle(listener);
306     pipControl->SetPictureInPictureActionObserver(listener1);
307     pipControl->GetPictureInPictureLifecycle();
308     pipControl->GetPictureInPictureActionObserver();
309     pipControl->GetPiPNavigationId();
310     EXPECT_EQ(WMError::WM_ERROR_PIP_STATE_ABNORMALLY, ret);
311     auto ret1 = pipControl->IsPullPiPAndHandleNavigation();
312     ASSERT_EQ(true, ret1);
313 }
314 }
315 }
316 }