From 15271476e2e2e64e2eb3d344e8a9649524cf058b Mon Sep 17 00:00:00 2001 From: william Date: Thu, 24 Nov 2022 20:09:58 +0800 Subject: [PATCH] add ut Signed-off-by: william --- bundle.json | 3 +- services/common/test/unittest/utils/BUILD.gn | 3 + .../unittest/utils/dscreen_fwkkit_test.cpp | 47 ++++++++++++++ .../test/unittest/utils/dscreen_fwkkit_test.h | 32 ++++++++++ .../utils/dscreen_maprelation_test.cpp | 50 +++++++++++++++ .../test/unittest/utils/video_param_test.cpp | 63 +++++++++++++++++++ services/screenclient/include/screen_client.h | 3 +- .../include/screen_client_window_adapter.h | 3 +- services/screenclient/src/screen_client.cpp | 34 +++++----- .../src/screen_client_window_adapter.cpp | 42 +++++++------ .../screenregionmgr/include/screenregion.h | 2 +- .../dscreenmgr/include/dscreen.h | 2 + .../include/dscreen_source_service.h | 2 +- .../src/dscreen_source_service.cpp | 5 ++ .../sinkservice/screenregionmgr/BUILD.gn | 4 +- .../src/screenregionmgr_test.cpp | 21 +++++++ 16 files changed, 272 insertions(+), 44 deletions(-) create mode 100644 services/common/test/unittest/utils/dscreen_fwkkit_test.cpp create mode 100644 services/common/test/unittest/utils/dscreen_fwkkit_test.h diff --git a/bundle.json b/bundle.json index 21213006..6d721823 100644 --- a/bundle.json +++ b/bundle.json @@ -84,12 +84,11 @@ "test":[ "//foundation/distributedhardware/distributed_screen/services/screendemo:distributedScreenTest", "//foundation/distributedhardware/distributed_screen/screenhandler/test/unittest/screenhandler:DscreenHandlerTest", - "//foundation/distributedhardware/distributed_screen/services/screenservice/test/unittest/sinkservice/screenregionmgr:DscreenSinkServiceTest", + "//foundation/distributedhardware/distributed_screen/services/screenservice/test/unittest/sinkservice/screenregionmgr:DscreenRegionMgrTest", "//foundation/distributedhardware/distributed_screen/services/screentransport/test/unittest:screen_transport_test", "//foundation/distributedhardware/distributed_screen/services/softbusadapter/test/unittest:SoftBusAdapterTest", "//foundation/distributedhardware/distributed_screen/services/common/test/unittest:service_common_test", "//foundation/distributedhardware/distributed_screen/interfaces/innerkits/native_cpp/test/unittest:screen_interface_test", - "//foundation/distributedhardware/distributed_screen/services/screenclient/test/unittest:ScreenClientTest", "//foundation/distributedhardware/distributed_screen/screenhandler/test/fuzztest:fuzztest", "//foundation/distributedhardware/distributed_screen/interfaces/innerkits/native_cpp/test/fuzztest:fuzztest" ] diff --git a/services/common/test/unittest/utils/BUILD.gn b/services/common/test/unittest/utils/BUILD.gn index 559366ad..73c301ee 100644 --- a/services/common/test/unittest/utils/BUILD.gn +++ b/services/common/test/unittest/utils/BUILD.gn @@ -25,6 +25,7 @@ config("module_private_config") { "${fwk_common_path}/utils/include", "${fwk_common_path}/log/include", "${fwk_utils_path}/include/log", + "//foundation/distributedhardware/distributed_hardware_fwk/interfaces/inner_kits/include", ] include_dirs += [ @@ -40,6 +41,7 @@ ohos_unittest("UtilsTest") { module_out_path = module_out_path sources = [ + "${services_path}/common/test/unittest/utils/dscreen_fwkkit_test.cpp", "${services_path}/common/test/unittest/utils/dscreen_maprelation_test.cpp", "${services_path}/common/test/unittest/utils/video_param_test.cpp", ] @@ -47,6 +49,7 @@ ohos_unittest("UtilsTest") { configs = [ ":module_private_config" ] deps = [ + "${distributedhardwarefwk_path}/interfaces/inner_kits:libdhfwk_sdk", "${services_path}/screenservice/sinkservice:distributed_screen_sink", "//third_party/googletest:gmock", "//third_party/googletest:gtest_main", diff --git a/services/common/test/unittest/utils/dscreen_fwkkit_test.cpp b/services/common/test/unittest/utils/dscreen_fwkkit_test.cpp new file mode 100644 index 00000000..bd6c3558 --- /dev/null +++ b/services/common/test/unittest/utils/dscreen_fwkkit_test.cpp @@ -0,0 +1,47 @@ +/* + * 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. + */ + +#define private public +#include "dscreen_fwkkit_test.h" +#include "distributed_hardware_fwk_kit.h" +#include "dscreen_fwkkit.h" +#undef private + +using namespace testing::ext; + +namespace OHOS { +namespace DistributedHardware { +void DScreenFwkKitTest::SetUpTestCase(void) {} + +void DScreenFwkKitTest::TearDownTestCase(void) {} + +void DScreenFwkKitTest::SetUp(){} + +void DScreenFwkKitTest::TearDown() {} + +/** + * @tc.name: GetDHFwkKit_001 + * @tc.desc: Verify the GetDHFwkKit function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(DScreenFwkKitTest, GetDHFwkKit_001, TestSize.Level1) +{ + auto ret = DScreenFwkKit::GetInstance().GetDHFwkKit(); + + EXPECT_NE(nullptr, DScreenFwkKit::GetInstance().dhfwkKit_); +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/common/test/unittest/utils/dscreen_fwkkit_test.h b/services/common/test/unittest/utils/dscreen_fwkkit_test.h new file mode 100644 index 00000000..42cd54bf --- /dev/null +++ b/services/common/test/unittest/utils/dscreen_fwkkit_test.h @@ -0,0 +1,32 @@ +/* + * 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 OHOS_DSCREEN_FWK_KIT_TEST_H +#define OHOS_DSCREEN_FWK_KIT_TEST_H + +#include + +namespace OHOS { +namespace DistributedHardware { +class DScreenFwkKitTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/services/common/test/unittest/utils/dscreen_maprelation_test.cpp b/services/common/test/unittest/utils/dscreen_maprelation_test.cpp index f78fa27e..d18a89cd 100644 --- a/services/common/test/unittest/utils/dscreen_maprelation_test.cpp +++ b/services/common/test/unittest/utils/dscreen_maprelation_test.cpp @@ -93,5 +93,55 @@ HWTEST_F(DScreenMapRelationTest, GetScreenRect_001, TestSize.Level1) ScreenRect actual = dscreenMapRelation->GetScreenRect(); EXPECT_EQ(startX, actual.startX); } + +/** + * @tc.name: to_json_001 + * @tc.desc: Verify the to_json function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(DScreenMapRelationTest, to_json_001, TestSize.Level1) +{ + json j; + uint64_t displayId = 1; + uint64_t screenId = 2; + DisplayRect displayRect = {0, 0, 100, 100}; + ScreenRect screenRect = {0, 0, 200, 200}; + DScreenMapRelation dScreenMapRelation; + dScreenMapRelation.SetDisplayId(displayId); + dScreenMapRelation.SetScreenId(screenId); + dScreenMapRelation.SetDisplayRect(displayRect); + dScreenMapRelation.SetScreenRect(screenRect); + to_json(j, dScreenMapRelation); + + uint64_t jsonDisplayId = 0; + j.at(KEY_DISPLAY_ID).get_to(jsonDisplayId); + EXPECT_EQ(displayId, jsonDisplayId); +} + +/** + * @tc.name: from_json_001 + * @tc.desc: Verify the from_json function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(DScreenMapRelationTest, from_json_001, TestSize.Level1) +{ + json j; + uint64_t displayId = 1; + uint64_t screenId = 2; + DisplayRect displayRect = {0, 0, 100, 100}; + ScreenRect screenRect = {0, 0, 200, 200}; + DScreenMapRelation dScreenMapRelation; + dScreenMapRelation.SetDisplayId(displayId); + dScreenMapRelation.SetScreenId(screenId); + dScreenMapRelation.SetDisplayRect(displayRect); + dScreenMapRelation.SetScreenRect(screenRect); + to_json(j, dScreenMapRelation); + + DScreenMapRelation jsonDScreenMapRelation; + from_json(j, jsonDScreenMapRelation); + EXPECT_EQ(displayId, jsonDScreenMapRelation.GetDisplayId()); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/common/test/unittest/utils/video_param_test.cpp b/services/common/test/unittest/utils/video_param_test.cpp index 23bd1357..2d15f458 100644 --- a/services/common/test/unittest/utils/video_param_test.cpp +++ b/services/common/test/unittest/utils/video_param_test.cpp @@ -131,5 +131,68 @@ HWTEST_F(VideoParamTest, GetVideoFormat_001, TestSize.Level1) uint8_t actual = videoParam_->GetVideoFormat(); EXPECT_EQ(videoFormat, actual); } + +/** + * @tc.name: to_json_001 + * @tc.desc: Verify the to_json function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(VideoParamTest, to_json_001, TestSize.Level1) +{ + json j; + uint32_t screenWidth = 100; + uint32_t screenHeight = 100; + uint32_t videoWidth = 100; + uint32_t videoHeight = 100; + uint32_t fps = 30; + uint8_t codecType = DEFAULT_CODECTYPE; + uint8_t videoFormat = DEFAULT_VIDEO_FORMAT; + + VideoParam videoParam; + videoParam.SetScreenWidth(screenWidth); + videoParam.SetScreenHeight(screenHeight); + videoParam.SetVideoWidth(videoWidth); + videoParam.SetVideoHeight(videoHeight); + videoParam.SetFps(fps); + videoParam.SetCodecType(codecType); + videoParam.SetVideoFormat(videoFormat); + to_json(j, videoParam); + + uint32_t jsonVideoWidth = 0; + j.at(KEY_VIDEO_WIDTH).get_to(jsonVideoWidth); + EXPECT_EQ(videoWidth, jsonVideoWidth); +} + +/** + * @tc.name: from_json_001 + * @tc.desc: Verify the from_json function. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(VideoParamTest, from_json_001, TestSize.Level1) +{ + json j; + uint32_t screenWidth = 100; + uint32_t screenHeight = 100; + uint32_t videoWidth = 100; + uint32_t videoHeight = 100; + uint32_t fps = 30; + uint8_t codecType = DEFAULT_CODECTYPE; + uint8_t videoFormat = DEFAULT_VIDEO_FORMAT; + + VideoParam videoParam; + videoParam.SetScreenWidth(screenWidth); + videoParam.SetScreenHeight(screenHeight); + videoParam.SetVideoWidth(videoWidth); + videoParam.SetVideoHeight(videoHeight); + videoParam.SetFps(fps); + videoParam.SetCodecType(codecType); + videoParam.SetVideoFormat(videoFormat); + to_json(j, videoParam); + VideoParam jsonVideoParam; + from_json(j, jsonVideoParam); + EXPECT_EQ(videoWidth, jsonVideoParam.GetVideoWidth()); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/screenclient/include/screen_client.h b/services/screenclient/include/screen_client.h index 3f0d9fe6..be3e8f99 100644 --- a/services/screenclient/include/screen_client.h +++ b/services/screenclient/include/screen_client.h @@ -41,10 +41,9 @@ public: int32_t MoveWindow(int32_t windowId, int32_t startX, int32_t startY); int32_t RemoveWindow(int32_t windowId); int32_t HideWindow(int32_t windowId); - int32_t DestroyAllWindow(); private: ScreenClient() = default; - ~ScreenClient() = default; + ~ScreenClient(); std::map> surfaceMap_; std::atomic windowId_ = INVALID_WINDOW_ID; std::mutex surfaceMapMutex_; diff --git a/services/screenclient/include/screen_client_window_adapter.h b/services/screenclient/include/screen_client_window_adapter.h index 267a4264..5b90b54d 100644 --- a/services/screenclient/include/screen_client_window_adapter.h +++ b/services/screenclient/include/screen_client_window_adapter.h @@ -42,10 +42,9 @@ public: int32_t HideWindow(int32_t windowId); int32_t MoveWindow(int32_t windowId, int32_t startX, int32_t startY); int32_t RemoveWindow(int32_t windowId); - int32_t DestroyAllWindow(); private: ScreenClientWindowAdapter() = default; - ~ScreenClientWindowAdapter() = default; + ~ScreenClientWindowAdapter(); std::map> windowIdMap_; std::mutex windowIdMapMutex_; }; diff --git a/services/screenclient/src/screen_client.cpp b/services/screenclient/src/screen_client.cpp index d738fd9d..52d68230 100644 --- a/services/screenclient/src/screen_client.cpp +++ b/services/screenclient/src/screen_client.cpp @@ -22,6 +22,26 @@ namespace OHOS { namespace DistributedHardware { IMPLEMENT_SINGLE_INSTANCE(ScreenClient); + +ScreenClient::~ScreenClient() +{ + DHLOGD("~ScreenClient"); + { + std::lock_guard dataLock(surfaceMapMutex_); + for (const auto &item : surfaceMap_) { + int32_t ret = ScreenClientWindowAdapter::GetInstance().RemoveWindow(item.first); + if (ret != DH_SUCCESS) { + DHLOGE("windowId (ID = %d) remove failed.", item.first); + return; + } + } + surfaceMap_.clear(); + windowId_ = INVALID_WINDOW_ID; + } + DHLOGD("ScreenClient Destory."); + return; +} + int32_t ScreenClient::AddWindow(std::shared_ptr windowProperty) { if (windowProperty == nullptr) { @@ -134,19 +154,5 @@ int32_t ScreenClient::RemoveWindow(int32_t windowId) DHLOGD("windowId (ID = %d) remove success.", windowId); return ret; } - -int32_t ScreenClient::DestroyAllWindow() -{ - DHLOGD("DestroyAllWindow"); - int32_t ret = ScreenClientWindowAdapter::GetInstance().DestroyAllWindow(); - if (ret != DH_SUCCESS) { - DHLOGE("DestroyAllWindow failed."); - return ret; - } - std::lock_guard dataLock(surfaceMapMutex_); - surfaceMap_.clear(); - windowId_ = INVALID_WINDOW_ID; - return DH_SUCCESS; -} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/screenclient/src/screen_client_window_adapter.cpp b/services/screenclient/src/screen_client_window_adapter.cpp index 79123f63..f1509ac6 100644 --- a/services/screenclient/src/screen_client_window_adapter.cpp +++ b/services/screenclient/src/screen_client_window_adapter.cpp @@ -28,6 +28,28 @@ namespace OHOS { namespace DistributedHardware { IMPLEMENT_SINGLE_INSTANCE(ScreenClientWindowAdapter); +ScreenClientWindowAdapter::~ScreenClientWindowAdapter() +{ + DHLOGD("~ScreenClientWindowAdapter"); + { + std::lock_guard dataLock(windowIdMapMutex_); + for (const auto &item : windowIdMap_) { + auto window = item.second; + if (window == nullptr) { + DHLOGE("window is nullptr(windowId = %d).", item.first); + return; + } + if (window->Destroy() != OHOS::Rosen::WMError::WM_OK) { + DHLOGE("screenclientSurface is nullptr(windowId = %d).", item.first); + return; + } + } + windowIdMap_.clear(); + } + DHLOGD("ScreenClientWindowAdapter Destory."); + return; +} + sptr ScreenClientWindowAdapter::CreateWindow(std::shared_ptr windowProperty, int32_t windowId) { @@ -179,26 +201,6 @@ int32_t ScreenClientWindowAdapter::RemoveWindow(int32_t windowId) return DH_SUCCESS; } -int32_t ScreenClientWindowAdapter::DestroyAllWindow() -{ - DHLOGD("DestroyAllWindow"); - std::lock_guard dataLock(windowIdMapMutex_); - for (const auto &item : windowIdMap_) { - auto window = item.second; - if (window == nullptr) { - DHLOGE("window is nullptr(windowId = %d).", item.first); - continue; - } - if (OHOS::Rosen::WMError::WM_OK != window->Destroy()) { - DHLOGE("Destroy window(windowId = %d) failed .", item.first); - continue; - } - } - windowIdMap_.clear(); - return DH_SUCCESS; -} - - bool ScreenClientInputEventListener::OnInputEvent(const std::shared_ptr& pointerEvent) const { if (pointerEvent == nullptr) { diff --git a/services/screenservice/sinkservice/screenregionmgr/include/screenregion.h b/services/screenservice/sinkservice/screenregionmgr/include/screenregion.h index bab1ee68..39b817a8 100644 --- a/services/screenservice/sinkservice/screenregionmgr/include/screenregion.h +++ b/services/screenservice/sinkservice/screenregionmgr/include/screenregion.h @@ -41,7 +41,7 @@ public: class ScreenRegion : public ScreenSinkTransCallback, public std::enable_shared_from_this { public: ScreenRegion(const std::string &remoteDevId, uint64_t screenId, uint64_t displayId); - ~ScreenRegion() override; + ~ScreenRegion(); void OnTransError(int32_t err, const std::string &content) override; void SetVideoParam(std::shared_ptr &videoParam); void SetMapRelation(std::shared_ptr &mapRelation); diff --git a/services/screenservice/sourceservice/dscreenmgr/include/dscreen.h b/services/screenservice/sourceservice/dscreenmgr/include/dscreen.h index 76f0b3b8..612f2428 100644 --- a/services/screenservice/sourceservice/dscreenmgr/include/dscreen.h +++ b/services/screenservice/sourceservice/dscreenmgr/include/dscreen.h @@ -103,6 +103,8 @@ private: int32_t Start(); int32_t Stop(); + static constexpr uint8_t TASK_WAIT_SECONDS = 1; + std::string devId_; std::string dhId_; uint64_t screenId_ = SCREEN_ID_INVALID; diff --git a/services/screenservice/sourceservice/dscreenservice/include/dscreen_source_service.h b/services/screenservice/sourceservice/dscreenservice/include/dscreen_source_service.h index 7e0cd747..8f97cff9 100644 --- a/services/screenservice/sourceservice/dscreenservice/include/dscreen_source_service.h +++ b/services/screenservice/sourceservice/dscreenservice/include/dscreen_source_service.h @@ -30,7 +30,7 @@ class DScreenSourceService : public SystemAbility, public DScreenSourceStub, DECLARE_SYSTEM_ABILITY(DScreenSourceService); public: DScreenSourceService(int32_t saId, bool runOnCreate); - ~DScreenSourceService() override = default; + ~DScreenSourceService(); int32_t InitSource(const std::string ¶ms, const sptr &callback) override; int32_t ReleaseSource() override; int32_t RegisterDistributedHardware(const std::string &devId, const std::string &dhId, diff --git a/services/screenservice/sourceservice/dscreenservice/src/dscreen_source_service.cpp b/services/screenservice/sourceservice/dscreenservice/src/dscreen_source_service.cpp index 052689e9..db9b47de 100644 --- a/services/screenservice/sourceservice/dscreenservice/src/dscreen_source_service.cpp +++ b/services/screenservice/sourceservice/dscreenservice/src/dscreen_source_service.cpp @@ -38,6 +38,11 @@ DScreenSourceService::DScreenSourceService(int32_t saId, bool runOnCreate) : Sys DHLOGI("dscreen source service create."); } +DScreenSourceService::~DScreenSourceService() +{ + DHLOGI("~DScreenSourceService."); +} + void DScreenSourceService::OnStart() { DHLOGI("dscreen source service start."); diff --git a/services/screenservice/test/unittest/sinkservice/screenregionmgr/BUILD.gn b/services/screenservice/test/unittest/sinkservice/screenregionmgr/BUILD.gn index 2abb51cb..58b72f96 100644 --- a/services/screenservice/test/unittest/sinkservice/screenregionmgr/BUILD.gn +++ b/services/screenservice/test/unittest/sinkservice/screenregionmgr/BUILD.gn @@ -53,8 +53,8 @@ config("module_private_config") { ] } -## UnitTest DscreenSinkServiceTest -ohos_unittest("DscreenSinkServiceTest") { +## UnitTest DscreenRegionMgrTest +ohos_unittest("DscreenRegionMgrTest") { module_out_path = module_out_path sources = [ diff --git a/services/screenservice/test/unittest/sinkservice/screenregionmgr/src/screenregionmgr_test.cpp b/services/screenservice/test/unittest/sinkservice/screenregionmgr/src/screenregionmgr_test.cpp index af269be8..5fd622c9 100644 --- a/services/screenservice/test/unittest/sinkservice/screenregionmgr/src/screenregionmgr_test.cpp +++ b/services/screenservice/test/unittest/sinkservice/screenregionmgr/src/screenregionmgr_test.cpp @@ -67,6 +67,27 @@ HWTEST_F(ScreenRegionManagerTest, NotifyRemoteScreenService_001, TestSize.Level1 EXPECT_EQ(ERR_DH_SCREEN_SA_GET_REMOTE_SOURCE_SERVICE_FAIL, ret); } +/** + * @tc.name: HandleNotifySetUp_001 + * @tc.desc: Verify the HandleNotifySetUp function failed. + * @tc.type: FUNC + * @tc.require: Issue Number + */ +HWTEST_F(ScreenRegionManagerTest, HandleNotifySetUp_001, TestSize.Level1) +{ + ScreenRegionManager::GetInstance().screenRegions_.clear(); + const std::string remoteDevId = "sourceDevId"; + const std::string eventContent = "{\"dhId\":\"SCREEN#0\",\"mapRelation\":{\"displayId\":0,\ + \"displayRect\":{\"height\":1280,\"startX\":0,\"startY\":0,\"width\":720},\"screenId\":2,\ + \"screenRect\":{\"height\":1280,\"startX\":0,\"startY\":0,\"width\":720}},\"screenId\":2,\ + \"videoParam\":{\"codecType\":2,\"colorFormat\":3,\"fps\":30,\"screenHeight\":1280,\ + \"screenWidth\":720,\"videoHeight\":1280,\"videoWidth\":720}}"; + ScreenRegionManager::GetInstance().HandleNotifySetUp(remoteDevId, eventContent); + + EXPECT_NE(0, ScreenRegionManager::GetInstance().screenRegions_.size()); + ScreenRegionManager::GetInstance().screenRegions_.clear(); +} + /** * @tc.name: GetDScreenSourceSA_001 * @tc.desc: Verify the GetDScreenSourceSA function failed. -- Gitee