From 1d4c169de5126a6bb2ce96812e7154b264dd00f5 Mon Sep 17 00:00:00 2001 From: shiyueeee Date: Tue, 18 Jan 2022 21:46:01 +0800 Subject: [PATCH] Add screen manager unittest Signed-off-by: shiyueeee Change-Id: Ic3feffc4752a61214f0c91e26cc91fce404beb8a --- dm/test/unittest/BUILD.gn | 17 +++++ dm/test/unittest/screen_manager_test.cpp | 93 +++++++++++++++++++++++ dm/test/unittest/screen_manager_test.h | 42 ++++++++++ dm/test/unittest/screen_manager_utils.cpp | 41 ++++++++++ dm/test/unittest/screen_manager_utils.h | 46 +++++++++++ 5 files changed, 239 insertions(+) create mode 100644 dm/test/unittest/screen_manager_test.cpp create mode 100644 dm/test/unittest/screen_manager_test.h create mode 100644 dm/test/unittest/screen_manager_utils.cpp create mode 100644 dm/test/unittest/screen_manager_utils.h diff --git a/dm/test/unittest/BUILD.gn b/dm/test/unittest/BUILD.gn index ce638248..55505d89 100644 --- a/dm/test/unittest/BUILD.gn +++ b/dm/test/unittest/BUILD.gn @@ -20,6 +20,7 @@ group("unittest") { deps = [ ":dm_display_power_unit_test", + ":dm_screen_manager_test", ":dm_screenshot_test", ":dm_snapshot_utils_test", ] @@ -61,6 +62,20 @@ ohos_unittest("dm_screenshot_test") { ## UnitTest dm_screenshot_test }}} +## UnitTest dm_screen_manager_test {{{ +ohos_unittest("dm_screen_manager_test") { + module_out_path = module_out_path + + sources = [ + "screen_manager_test.cpp", + "screen_manager_utils.cpp", + ] + + deps = [ ":dm_unittest_common" ] +} + +## UnitTest dm_screen_manager_test }}} + ## Build dm_unittest_common.a {{{ config("dm_unittest_common_public_config") { include_dirs = [ @@ -69,6 +84,7 @@ config("dm_unittest_common_public_config") { "//foundation/windowmanager/snapshot", "//foundation/windowmanager/interfaces/innerkits/dm", "//foundation/windowmanager/utils/include", + "//foundation/graphic/standard/rosen/modules/render_service_client", # RSSurface ] } @@ -82,6 +98,7 @@ ohos_static_library("dm_unittest_common") { "//foundation/multimedia/image_standard/interfaces/innerkits:image_native", # PixelMap "//foundation/multimodalinput/input/frameworks/proxy:libmmi-client", "//foundation/windowmanager/dm:libdm", + "//foundation/windowmanager/dmserver:libdms", "//foundation/windowmanager/snapshot:snapshot_display", "//foundation/windowmanager/utils:libwmutil", "//foundation/windowmanager/wm:libwm", diff --git a/dm/test/unittest/screen_manager_test.cpp b/dm/test/unittest/screen_manager_test.cpp new file mode 100644 index 00000000..3ddd5c65 --- /dev/null +++ b/dm/test/unittest/screen_manager_test.cpp @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2022 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 "screen_manager_test.h" +#include "mock_display_manager_adapter.h" +#include "singleton_mocker.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +namespace Rosen { +using Mocker = SingletonMocker; + +sptr ScreenManagerTest::defaultDisplay_ = nullptr; +DisplayId ScreenManagerTest::defaultDisplayId_ = DISPLAY_ID_INVALD; +uint32_t ScreenManagerTest::defaultWidth_ = 480; +uint32_t ScreenManagerTest::defaultHeight_ = 320; + +void ScreenManagerTest::SetUpTestCase() +{ + defaultDisplay_ = DisplayManager::GetInstance().GetDefaultDisplay(); + defaultDisplayId_ = defaultDisplay_->GetId(); + defaultWidth_ = defaultDisplay_->GetWidth(); + defaultHeight_ = defaultDisplay_->GetHeight(); +} + +void ScreenManagerTest::TearDownTestCase() +{ +} + +void ScreenManagerTest::SetUp() +{ +} + +void ScreenManagerTest::TearDown() +{ +} + +namespace { +/** + * @tc.name: CreateAndDestory01 + * @tc.desc: CreateVirtualScreen with invalid option and return invalid screen id + * @tc.type: FUNC + */ +HWTEST_F(ScreenManagerTest, CreateAndDestory01, Function | SmallTest | Level1) +{ + VirtualScreenOption wrongOption = {defaultName_, defaultWidth_, defaultHeight_, + defaultDensity_, nullptr, defaultFlags_}; + std::unique_ptr m = std::make_unique(); + EXPECT_CALL(m->Mock(), CreateVirtualScreen(_)).Times(1).WillOnce(Return(SCREEN_ID_INVALD)); + EXPECT_CALL(m->Mock(), DestroyVirtualScreen(_)).Times(1).WillOnce(Return(DMError::DM_ERROR_INVALID_PARAM)); + ScreenId id = ScreenManager::GetInstance().CreateVirtualScreen(wrongOption); + DMError ret = ScreenManager::GetInstance().DestroyVirtualScreen(id); + ASSERT_EQ(SCREEN_ID_INVALD, id); + ASSERT_EQ(DMError::DM_ERROR_INVALID_PARAM, ret); +} + +/** + * @tc.name: CreateAndDestory02 + * @tc.desc: CreateVirtualScreen with valid option and return valid screen id + * @tc.type: FUNC + */ +HWTEST_F(ScreenManagerTest, CreateAndDestory02, Function | SmallTest | Level1) +{ + ScreenManagerUtils utils; + ASSERT_TRUE(utils.CreateSurface()); + VirtualScreenOption defaultOption = {defaultName_, defaultWidth_, defaultHeight_, + defaultDensity_, utils.psurface_, defaultFlags_}; + ScreenId validId = 0; + std::unique_ptr m = std::make_unique(); + EXPECT_CALL(m->Mock(), CreateVirtualScreen(_)).Times(1).WillOnce(Return(validId)); + EXPECT_CALL(m->Mock(), DestroyVirtualScreen(_)).Times(1).WillOnce(Return(DMError::DM_OK)); + ScreenId id = ScreenManager::GetInstance().CreateVirtualScreen(defaultOption); + DMError ret = ScreenManager::GetInstance().DestroyVirtualScreen(id); + ASSERT_EQ(validId, id); + ASSERT_EQ(DMError::DM_OK, ret); +} +} +} // namespace Rosen +} // namespace OHOS \ No newline at end of file diff --git a/dm/test/unittest/screen_manager_test.h b/dm/test/unittest/screen_manager_test.h new file mode 100644 index 00000000..32d4ad04 --- /dev/null +++ b/dm/test/unittest/screen_manager_test.h @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022 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. + */ + +#ifndef FRAMEWORKS_DM_TEST_UT_SCREEN_MANAGER_TEST_H +#define FRAMEWORKS_DM_TEST_UT_SCREEN_MANAGER_TEST_H + +#include +#include "screen_manager_utils.h" + +namespace OHOS { +namespace Rosen { +class ScreenManagerTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + virtual void SetUp() override; + virtual void TearDown() override; + + const std::string defaultName_ = "virtualScreen01"; + const float defaultDensity_ = 2.0; + const int32_t defaultFlags_ = 0; + static sptr defaultDisplay_; + static DisplayId defaultDisplayId_; + static uint32_t defaultWidth_; + static uint32_t defaultHeight_; +}; +} // namespace Rosen +} // namespace OHOS + +#endif // FRAMEWORKS_DM_TEST_UT_SCREEN_MANAGER_TEST_H \ No newline at end of file diff --git a/dm/test/unittest/screen_manager_utils.cpp b/dm/test/unittest/screen_manager_utils.cpp new file mode 100644 index 00000000..23095232 --- /dev/null +++ b/dm/test/unittest/screen_manager_utils.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2022 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 "screen_manager_utils.h" + +namespace OHOS { +namespace Rosen { +namespace { + constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0, "ScreenManagerUtils"}; +} + +bool ScreenManagerUtils::CreateSurface() +{ + csurface_ = Surface::CreateSurfaceAsConsumer(); + if (csurface_ == nullptr) { + WLOGFE("csurface create failed"); + return false; + } + + auto producer = csurface_->GetProducer(); + psurface_ = Surface::CreateSurfaceAsProducer(producer); + if (psurface_ == nullptr) { + WLOGFE("csurface create failed"); + return false; + } + return true; +} +} // namespace ROSEN +} // namespace OHOS \ No newline at end of file diff --git a/dm/test/unittest/screen_manager_utils.h b/dm/test/unittest/screen_manager_utils.h new file mode 100644 index 00000000..8621f090 --- /dev/null +++ b/dm/test/unittest/screen_manager_utils.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022 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. + */ + +#ifndef FRAMEWORKS_DM_TEST_UT_SCREEN_MANAGER_UTILS_H +#define FRAMEWORKS_DM_TEST_UT_SCREEN_MANAGER_UTILS_H + +#include + +#include "display.h" +#include "display_manager.h" +#include "dm_common.h" +#include "screen.h" +#include "screen_manager.h" +#include "window_manager_hilog.h" +#include "unique_fd.h" +#include "core/ui/rs_surface_node.h" +#include "core/ui/rs_display_node.h" + +namespace OHOS { +namespace Rosen { +class ScreenManagerUtils { +public: + bool CreateSurface(); + + const std::string defaultName_ = "virtualScreen01"; + sptr csurface_ = nullptr; // cosumer surface + sptr psurface_ = nullptr; // producer surface + const float defaultDensity_ = 2.0; + const int32_t defaultFlags_ = 0; +}; +} // namespace Rosen +} // namespace OHOS + +#endif // FRAMEWORKS_DM_TEST_UT_SCREEN_MANAGER_UTILS_H \ No newline at end of file