From 8ecb7c822544069eceb8cde7f5f917527b60351f Mon Sep 17 00:00:00 2001 From: shuangshuangliu Date: Fri, 22 Aug 2025 14:20:34 +0800 Subject: [PATCH] =?UTF-8?q?TDD=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= =?UTF-8?q?=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: shuangshuangliu Change-Id: I7ea1873ef616fa47bec09a1a5b3ea9e0fb25373d --- wm/test/unittest/BUILD.gn | 16 + ...picture_in_picture_controller_new_test.cpp | 317 ++++++++++++++++++ 2 files changed, 333 insertions(+) create mode 100644 wm/test/unittest/web_picture_in_picture_controller_new_test.cpp diff --git a/wm/test/unittest/BUILD.gn b/wm/test/unittest/BUILD.gn index 6a9fca35b8..45e99601b6 100644 --- a/wm/test/unittest/BUILD.gn +++ b/wm/test/unittest/BUILD.gn @@ -34,6 +34,7 @@ group("unittest") { ":wm_root_scene_test", ":wm_screen_scene_test", ":wm_vsync_station_test", + ":wm_web_picture_in_picture_controller_new_test", ":wm_web_picture_in_picture_controller_test", ":wm_window_agent_test", ":wm_window_display_change_adapter_test", @@ -621,6 +622,21 @@ ohos_unittest("wm_web_picture_in_picture_controller_test") { ] } +ohos_unittest("wm_web_picture_in_picture_controller_new_test") { + module_out_path = module_out_path + + sources = [ "web_picture_in_picture_controller_new_test.cpp" ] + + deps = [ ":wm_unittest_common" ] + + external_deps = [ + "ability_runtime:runtime", + "ace_engine:ace_xcomponent_controller", + "c_utils:utils", + "hilog:libhilog", + ] +} + ohos_unittest("wm_oh_window_pip_test") { module_out_path = module_out_path diff --git a/wm/test/unittest/web_picture_in_picture_controller_new_test.cpp b/wm/test/unittest/web_picture_in_picture_controller_new_test.cpp new file mode 100644 index 0000000000..53700829cc --- /dev/null +++ b/wm/test/unittest/web_picture_in_picture_controller_new_test.cpp @@ -0,0 +1,317 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include + +#include +#include "parameters.h" +#include "picture_in_picture_controller.h" +#include "picture_in_picture_manager.h" +#include "window.h" +#include "window_manager_hilog.h" +#include "wm_common.h" +#include "xcomponent_controller.h" +#include "ability_context_impl.h" +#include "web_picture_in_picture_controller.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +namespace Rosen { +namespace { + std::string g_errLog; + void MyLogCallback(const LogType type, const LogLevel level, const unsigned int domain, const char *tag, + const char *msg) + { + g_errLog = msg; + } +} +class MockNewWindow : public Window { +public: + MockNewWindow() {}; + ~MockNewWindow() {}; + MOCK_METHOD3(Show, WMError(uint32_t reason, bool withAnimation, bool withFocus)); + MOCK_METHOD1(Destroy, WMError(uint32_t reason)); + MOCK_METHOD0(Destroy, WMError()); + MOCK_METHOD0(NotifyPrepareClosePiPWindow, WMError()); + MOCK_METHOD4(SetAutoStartPiP, void(bool isAutoStart, uint32_t priority, uint32_t width, uint32_t height)); + MOCK_CONST_METHOD0(GetWindowState, WindowState()); +}; + +class MockXComponentNewController : public XComponentController { +public: + MockXComponentNewController() {} + ~MockXComponentNewController() {} + MOCK_METHOD2(GetGlobalPosition, XComponentControllerErrorCode(float& offsetX, float& offsetY)); + MOCK_METHOD2(GetSize, XComponentControllerErrorCode(float& width, float& height)); + MOCK_METHOD1(SetExtController, + XComponentControllerErrorCode(std::shared_ptr xComponentController)); + MOCK_METHOD1(ResetExtController, + XComponentControllerErrorCode(std::shared_ptr xComponentController)); +}; + +class WebPictureInPictureControllerNewTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp() override; + void TearDown() override; + PiPConfig config; +}; + +void WebPictureInPictureControllerNewTest::SetUpTestCase() +{ +} + +void WebPictureInPictureControllerNewTest::TearDownTestCase() +{ +} + +void WebPictureInPictureControllerNewTest::SetUp() +{ + uint32_t mainWindowId = 100; + uint32_t pipTemplateType = 0; + uint32_t width = 100; + uint32_t height = 150; + std::vector controlGroup = {101}; + int num = 0; + napi_env env = reinterpret_cast(&num); + config = {mainWindowId, pipTemplateType, width, height, controlGroup, env}; +} + +void WebPictureInPictureControllerNewTest::TearDown() +{ +} + +namespace { +/** + * @tc.name: CreatePictureInPictureWindow + * @tc.desc: CreatePictureInPictureWindow + * @tc.type: FUNC + */ +HWTEST_F(WebPictureInPictureControllerNewTest, CreatePictureInPictureWindow, TestSize.Level1) +{ + auto mw = sptr::MakeSptr(); + ASSERT_NE(nullptr, mw); + auto webPipControl = sptr::MakeSptr(config); + auto option = webPipControl->pipOption_; + webPipControl->pipOption_ = nullptr; + StartPipType startType = StartPipType::NATIVE_START; + EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, webPipControl->CreatePictureInPictureWindow(startType)); + + webPipControl->pipOption_ = option; + webPipControl->mainWindow_ = nullptr; + EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, webPipControl->CreatePictureInPictureWindow(startType)); + + webPipControl->mainWindow_ = mw; + EXPECT_CALL(*(mw), GetWindowState()).Times(2).WillOnce(Return(WindowState::STATE_CREATED)); + EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, webPipControl->CreatePictureInPictureWindow(startType)); + + EXPECT_CALL(*(mw), GetWindowState()).Times(2).WillOnce(Return(WindowState::STATE_SHOWN)); + EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, webPipControl->CreatePictureInPictureWindow(startType)); +} + +/** + * @tc.name: StartPictureInPicture + * @tc.desc: StartPictureInPicture + * @tc.type: FUNC + */ +HWTEST_F(WebPictureInPictureControllerNewTest, StartPictureInPicture001, TestSize.Level1) +{ + auto webPipControl = sptr::MakeSptr(config); + auto option = webPipControl->pipOption_; + webPipControl->pipOption_ = nullptr; + StartPipType startType = StartPipType::NATIVE_START; + EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, webPipControl->StartPictureInPicture(startType)); +} + +/** + * @tc.name: StartPictureInPicture + * @tc.desc: StartPictureInPicture + * @tc.type: FUNC + */ +HWTEST_F(WebPictureInPictureControllerNewTest, StartPictureInPicture002, TestSize.Level1) +{ + auto webPipControl = sptr::MakeSptr(config); + StartPipType startType = StartPipType::NATIVE_START; + webPipControl->curState_ = PiPWindowState::STATE_STARTING; + EXPECT_EQ(WMError::WM_ERROR_PIP_REPEAT_OPERATION, webPipControl->StartPictureInPicture(startType)); +} + +/** + * @tc.name: StartPictureInPicture + * @tc.desc: StartPictureInPicture + * @tc.type: FUNC + */ +HWTEST_F(WebPictureInPictureControllerNewTest, StartPictureInPicture003, TestSize.Level1) +{ + auto webPipControl = sptr::MakeSptr(config); + StartPipType startType = StartPipType::NATIVE_START; + webPipControl->curState_ = PiPWindowState::STATE_STARTED; + EXPECT_EQ(WMError::WM_ERROR_PIP_REPEAT_OPERATION, webPipControl->StartPictureInPicture(startType)); +} + +/** + * @tc.name: StartPictureInPicture + * @tc.desc: StartPictureInPicture + * @tc.type: FUNC + */ +HWTEST_F(WebPictureInPictureControllerNewTest, StartPictureInPicture004, TestSize.Level1) +{ + auto webPipControl = sptr::MakeSptr(config); + StartPipType startType = StartPipType::NATIVE_START; + webPipControl->curState_ = PiPWindowState::STATE_UNDEFINED; + EXPECT_EQ(WMError::WM_ERROR_PIP_CREATE_FAILED, webPipControl->StartPictureInPicture(startType)); +} + +/** + * @tc.name: UpdateContentSize + * @tc.desc: UpdateContentSize + * @tc.type: FUNC + */ +HWTEST_F(WebPictureInPictureControllerNewTest, UpdateContentSize, TestSize.Level1) +{ + g_errLog.clear(); + LOG_SetCallback(MyLogCallback); + auto mw = sptr::MakeSptr(); + ASSERT_NE(nullptr, mw); + auto webPipControl = sptr::MakeSptr(config); + int32_t width = 0; + int32_t height = 0; + webPipControl->UpdateContentSize(width, height); + EXPECT_TRUE(g_errLog.find("invalid size") != std::string::npos); + height = 150; + webPipControl->UpdateContentSize(width, height); + height = 0; + width = 100; + webPipControl->UpdateContentSize(width, height); + height = 150; + webPipControl->UpdateContentSize(width, height); + + webPipControl->curState_ = PiPWindowState::STATE_UNDEFINED; + webPipControl->UpdateContentSize(width, height); + webPipControl->curState_ = PiPWindowState::STATE_STARTED; + webPipControl->UpdateContentSize(width, height); + + webPipControl->window_ = nullptr; + webPipControl->UpdateContentSize(width, height); + webPipControl->window_ = mw; + webPipControl->UpdateContentSize(width, height); + EXPECT_EQ(webPipControl->pipOption_->contentWidth_, 100); + EXPECT_EQ(webPipControl->pipOption_->contentHeight_, 150); + LOG_SetCallback(nullptr); +} + +/** + * @tc.name: RestorePictureInPictureWindow + * @tc.desc: RestorePictureInPictureWindow + * @tc.type: FUNC + */ +HWTEST_F(WebPictureInPictureControllerNewTest, RestorePictureInPictureWindow, TestSize.Level1) +{ + g_errLog.clear(); + LOG_SetCallback(MyLogCallback); + auto mw = sptr::MakeSptr(); + ASSERT_NE(nullptr, mw); + auto webPipControl = sptr::MakeSptr(config); + webPipControl->window_ = mw; + webPipControl->RestorePictureInPictureWindow(); + EXPECT_EQ(webPipControl->curState_, PiPWindowState::STATE_STOPPED); + EXPECT_TRUE(g_errLog.find("restore pip main window finished") != std::string::npos); + LOG_SetCallback(nullptr); +} + +/** + * @tc.name: SetXComponentController + * @tc.desc: SetXComponentController + * @tc.type: FUNC + */ +HWTEST_F(WebPictureInPictureControllerNewTest, SetXComponentController001, TestSize.Level1) +{ + std::shared_ptr xComponentController = std::make_shared(); + ASSERT_NE(nullptr, xComponentController); + sptr mw = new MockNewWindow(); + auto webPipControl = sptr::MakeSptr(config); + webPipControl->window_ = nullptr; + EXPECT_EQ(WMError::WM_ERROR_PIP_STATE_ABNORMALLY, webPipControl->SetXComponentController(xComponentController)); +} + +/** + * @tc.name: SetXComponentController + * @tc.desc: SetXComponentController + * @tc.type: FUNC + */ +HWTEST_F(WebPictureInPictureControllerNewTest, SetXComponentController002, TestSize.Level1) +{ + std::shared_ptr xComponentController = std::make_shared(); + ASSERT_NE(nullptr, xComponentController); + sptr mw = new MockNewWindow(); + auto webPipControl = sptr::MakeSptr(config); + webPipControl->window_ = mw; + EXPECT_EQ(WMError::WM_OK, webPipControl->SetXComponentController(xComponentController)); +} + +/** + * @tc.name: GetWebRequestId + * @tc.desc: GetWebRequestId + * @tc.type: FUNC + */ +HWTEST_F(WebPictureInPictureControllerNewTest, GetWebRequestId, TestSize.Level1) +{ + auto webPipControl = sptr::MakeSptr(config); + EXPECT_EQ(webPipControl->GetWebRequestId(), 0); + EXPECT_EQ(webPipControl->GetWebRequestId(), 1); + webPipControl->webRequestId_ = UINT8_MAX - 1; + EXPECT_EQ(webPipControl->GetWebRequestId(), UINT8_MAX - 1); + EXPECT_EQ(webPipControl->GetWebRequestId(), UINT8_MAX); + EXPECT_EQ(webPipControl->GetWebRequestId(), 0); +} + +/** + * @tc.name: UpdateWinRectByComponent + * @tc.desc: UpdateWinRectByComponent + * @tc.type: FUNC + */ +HWTEST_F(WebPictureInPictureControllerNewTest, UpdateWinRectByComponent, TestSize.Level1) +{ + auto webPipControl = sptr::MakeSptr(config); + webPipControl->UpdateWinRectByComponent(); + EXPECT_EQ(webPipControl->windowRect_.width_, 100); + EXPECT_EQ(webPipControl->windowRect_.height_, 150); + + webPipControl->pipOption_->contentWidth_ = 0; + webPipControl->pipOption_->contentHeight_ = 0; + webPipControl->UpdateWinRectByComponent(); + EXPECT_EQ(webPipControl->windowRect_.width_, 0); + EXPECT_EQ(webPipControl->windowRect_.height_, 0); + webPipControl->pipOption_->contentWidth_ = 10; + webPipControl->pipOption_->contentHeight_ = 0; + webPipControl->UpdateWinRectByComponent(); + EXPECT_EQ(webPipControl->windowRect_.width_, 10); + EXPECT_EQ(webPipControl->windowRect_.height_, 0); + webPipControl->UpdateWinRectByComponent(); + webPipControl->pipOption_->contentWidth_ = 0; + webPipControl->UpdateWinRectByComponent(); + EXPECT_EQ(webPipControl->windowRect_.width_, 0); + EXPECT_EQ(webPipControl->windowRect_.height_, 0); + webPipControl->pipOption_->contentWidth_ = 10; + webPipControl->pipOption_->contentHeight_ = 10; + webPipControl->UpdateWinRectByComponent(); + EXPECT_EQ(webPipControl->windowRect_.posX_, 0); + EXPECT_EQ(webPipControl->windowRect_.posY_, 0); +} +} +} +} \ No newline at end of file -- Gitee