Modify dmadaptor methods as virtual type for gmock. Add unit test of snapshot

Signed-off-by: shiyueeee <nieshiyue@huawei.com>
Change-Id: I16a629ff803f74a647542bdc7193c8a4e0da14d1
This commit is contained in:
shiyueeee 2022-01-17 22:16:46 +08:00
parent b17ad52871
commit 119d4e06c2
7 changed files with 344 additions and 21 deletions

View File

@ -32,33 +32,31 @@ public:
};
class DisplayManagerAdapter {
WM_DECLARE_SINGLE_INSTANCE_BASE(DisplayManagerAdapter);
WM_DECLARE_SINGLE_INSTANCE(DisplayManagerAdapter);
public:
DisplayId GetDefaultDisplayId();
sptr<Display> GetDisplayById(DisplayId displayId);
virtual DisplayId GetDefaultDisplayId();
virtual sptr<Display> GetDisplayById(DisplayId displayId);
ScreenId CreateVirtualScreen(VirtualScreenOption option);
DMError DestroyVirtualScreen(ScreenId screenId);
std::shared_ptr<Media::PixelMap> GetDisplaySnapshot(DisplayId displayId);
virtual ScreenId CreateVirtualScreen(VirtualScreenOption option);
virtual DMError DestroyVirtualScreen(ScreenId screenId);
virtual std::shared_ptr<Media::PixelMap> GetDisplaySnapshot(DisplayId displayId);
void RegisterDisplayManagerAgent(const sptr<IDisplayManagerAgent>& displayManagerAgent,
virtual void RegisterDisplayManagerAgent(const sptr<IDisplayManagerAgent>& displayManagerAgent,
DisplayManagerAgentType type);
void UnregisterDisplayManagerAgent(const sptr<IDisplayManagerAgent>& displayManagerAgent,
virtual void UnregisterDisplayManagerAgent(const sptr<IDisplayManagerAgent>& displayManagerAgent,
DisplayManagerAgentType type);
bool WakeUpBegin(PowerStateChangeReason reason);
bool WakeUpEnd();
bool SuspendBegin(PowerStateChangeReason reason);
bool SuspendEnd();
bool SetScreenPowerForAll(DisplayPowerState state, PowerStateChangeReason reason);
bool SetDisplayState(DisplayState state);
DisplayState GetDisplayState(uint64_t displayId);
void NotifyDisplayEvent(DisplayEvent event);
DMError AddMirror(ScreenId mainScreenId, ScreenId mirrorScreenId);
void Clear();
virtual bool WakeUpBegin(PowerStateChangeReason reason);
virtual bool WakeUpEnd();
virtual bool SuspendBegin(PowerStateChangeReason reason);
virtual bool SuspendEnd();
virtual bool SetScreenPowerForAll(DisplayPowerState state, PowerStateChangeReason reason);
virtual bool SetDisplayState(DisplayState state);
virtual DisplayState GetDisplayState(uint64_t displayId);
virtual void NotifyDisplayEvent(DisplayEvent event);
virtual DMError AddMirror(ScreenId mainScreenId, ScreenId mirrorScreenId);
virtual void Clear();
private:
DisplayManagerAdapter() = default;
~DisplayManagerAdapter() = default;
bool InitDMSProxyLocked();
static inline SingletonDelegator<DisplayManagerAdapter> delegator;

View File

@ -13,5 +13,8 @@
group("test") {
testonly = true
deps = [ "systemtest:systemtest" ]
deps = [
"systemtest:systemtest",
"unittest:unittest",
]
}

74
dm/test/unittest/BUILD.gn Normal file
View File

@ -0,0 +1,74 @@
# 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.
import("//build/test.gni")
module_out_path = "window_manager/dm"
group("unittest") {
testonly = true
deps = [ ":dm_snapshot_utils_test" ]
}
## UnitTest dm_snapshot_utils_test {{{
ohos_unittest("dm_snapshot_utils_test") {
module_out_path = module_out_path
sources = [
"//foundation/windowmanager/snapshot/snapshot_utils.cpp",
"snapshot_utils_test.cpp",
]
deps = [ ":dm_unittest_common" ]
}
## UnitTest dm_snapshot_utils_test }}}
## Build dm_unittest_common.a {{{
config("dm_unittest_common_public_config") {
include_dirs = [
"//foundation/windowmanager/dm/include",
"//foundation/windowmanager/dmserver/include",
"//foundation/windowmanager/snapshot",
"//foundation/windowmanager/interfaces/innerkits/dm",
"//foundation/windowmanager/utils/include",
]
}
ohos_static_library("dm_unittest_common") {
visibility = [ ":*" ]
testonly = true
public_configs = [ ":dm_unittest_common_public_config" ]
public_deps = [
"//foundation/multimedia/image_standard/interfaces/innerkits:image_native", # PixelMap
"//foundation/multimodalinput/input/frameworks/proxy:libmmi-client",
"//foundation/windowmanager/dm:libdm",
"//foundation/windowmanager/snapshot:snapshot_display",
"//foundation/windowmanager/utils:libwmutil",
"//foundation/windowmanager/wm:libwm",
"//foundation/windowmanager/wmserver:libwms",
"//third_party/googletest:gmock",
"//third_party/googletest:gtest_main",
"//third_party/libpng:libpng", # png
"//utils/native/base:utils",
]
external_deps = [
"hilog_native:libhilog",
"ipc:ipc_core",
]
}
## Build wm_unittest_common.a }}}

View File

@ -0,0 +1,36 @@
/*
* 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_WM_TEST_UT_MOCK_DISPLAY_MANAGER_ADAPTER_H
#define FRAMEWORKS_WM_TEST_UT_MOCK_DISPLAY_MANAGER_ADAPTER_H
#include <gmock/gmock.h>
#include "display_manager_adapter.h"
namespace OHOS {
namespace Rosen {
class MockDisplayManagerAdapter : public DisplayManagerAdapter {
public:
MOCK_METHOD0(GetDefaultDisplayId, DisplayId());
MOCK_METHOD1(GetDisplayById, sptr<Display>(DisplayId displayId));
MOCK_METHOD1(CreateVirtualScreen, ScreenId(VirtualScreenOption option));
MOCK_METHOD1(DestroyVirtualScreen, DMError(ScreenId screenId));
MOCK_METHOD1(GetDisplaySnapshot, std::shared_ptr<Media::PixelMap>(DisplayId displayId));
MOCK_METHOD0(Clear, void());
};
}
} // namespace OHOS
#endif // FRAMEWORKS_WM_TEST_UT_MOCK_DISPLAY_MANAGER_ADAPTER_H

View File

@ -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_SINGLETON_MOCKER_H
#define FRAMEWORKS_DM_TEST_UT_SINGLETON_MOCKER_H
#include "singleton_container.h"
namespace OHOS {
namespace Rosen {
template<class T, class MockT>
class SingletonMocker {
public:
SingletonMocker()
{
SingletonContainer::Set<T>(mock);
}
~SingletonMocker()
{
SingletonContainer::Set<T>(T::GetInstance());
}
MockT& Mock()
{
return mock;
}
private:
MockT mock;
};
} // namespace Rosen
} // namespace OHOS
#endif // FRAMEWORKS_DM_TEST_UT_SINGLETON_MOCKER_H

View File

@ -0,0 +1,130 @@
/*
* 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 "snapshot_utils_test.h"
#include "mock_display_manager_adapter.h"
#include "singleton_mocker.h"
#include "snapshot_utils.h"
using namespace testing;
using namespace testing::ext;
namespace OHOS {
namespace Rosen {
using Mocker = SingletonMocker<DisplayManagerAdapter, MockDisplayManagerAdapter>;
void SnapshotUtilsTest::SetUpTestCase()
{
}
void SnapshotUtilsTest::TearDownTestCase()
{
}
void SnapshotUtilsTest::SetUp()
{
}
void SnapshotUtilsTest::TearDown()
{
}
namespace {
/**
* @tc.name: Check01
* @tc.desc: Check if default png is valid file names
* @tc.type: FUNC
*/
HWTEST_F(SnapshotUtilsTest, Check01, Function | SmallTest | Level3)
{
ASSERT_EQ(true, SnapShotUtils::CheckFileNameValid(defaultFile_));
}
/**
* @tc.name: Check02
* @tc.desc: Check custom png is valid file names
* @tc.type: FUNC
*/
HWTEST_F(SnapshotUtilsTest, Check02, Function | SmallTest | Level3)
{
std::string fileName = "/data/test.png";
ASSERT_EQ(true, SnapShotUtils::CheckFileNameValid(fileName));
}
/**
* @tc.name: Check03
* @tc.desc: Check random path is invalid file names
* @tc.type: FUNC
*/
HWTEST_F(SnapshotUtilsTest, Check03, Function | SmallTest | Level3)
{
std::string fileName = "/path/to/test/1.png";
ASSERT_EQ(false, SnapShotUtils::CheckFileNameValid(fileName));
}
/**
* @tc.name: Write01
* @tc.desc: Write default png using valid file names and valid PixelMap
* @tc.type: FUNC
*/
HWTEST_F(SnapshotUtilsTest, Write01, Function | SmallTest | Level3)
{
DisplayId id = DisplayManager::GetInstance().GetDefaultDisplayId();
std::shared_ptr<Media::PixelMap> pixelMap = DisplayManager::GetInstance().GetScreenshot(id);
ASSERT_NE(nullptr, pixelMap);
ASSERT_EQ(true, SnapShotUtils::WriteToPngWithPixelMap(defaultFile_, *pixelMap));
}
/**
* @tc.name: Write02
* @tc.desc: Write default png using valid file names and valid WriteToPngParam
* @tc.type: FUNC
*/
HWTEST_F(SnapshotUtilsTest, Write02, Function | SmallTest | Level3)
{
DisplayId id = DisplayManager::GetInstance().GetDefaultDisplayId();
std::shared_ptr<Media::PixelMap> pixelMap = DisplayManager::GetInstance().GetScreenshot(id);
ASSERT_NE(nullptr, pixelMap);
WriteToPngParam param = {
.width = pixelMap->GetWidth(),
.height = pixelMap->GetHeight(),
.data = pixelMap->GetPixels(),
.stride = pixelMap->GetRowBytes(),
.bitDepth = defaultBitDepth_
};
ASSERT_EQ(true, SnapShotUtils::WriteToPng(defaultFile_, param));
}
/**
* @tc.name: Write03
* @tc.desc: Write custom png using valid file names and valid WriteToPngParam
* @tc.type: FUNC
*/
HWTEST_F(SnapshotUtilsTest, Write03, Function | SmallTest | Level3)
{
DisplayId id = DisplayManager::GetInstance().GetDefaultDisplayId();
std::shared_ptr<Media::PixelMap> pixelMap = DisplayManager::GetInstance().GetScreenshot(id);
ASSERT_NE(nullptr, pixelMap);
WriteToPngParam param = {
.width = (pixelMap->GetWidth() / 2),
.height = (pixelMap->GetWidth() / 2),
.data = pixelMap->GetPixels(),
.stride = pixelMap->GetRowBytes(),
.bitDepth = defaultBitDepth_
};
ASSERT_EQ(true, SnapShotUtils::WriteToPng(defaultFile_, param));
}
}
} // namespace Rosen
} // namespace OHOS

View File

@ -0,0 +1,36 @@
/*
* 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_SNAPSHOT_UTILS_TEST_H
#define FRAMEWORKS_DM_TEST_UT_SNAPSHOT_UTILS_TEST_H
#include <gtest/gtest.h>
#include "display.h"
namespace OHOS {
namespace Rosen {
class SnapshotUtilsTest : public testing::Test {
public:
static void SetUpTestCase();
static void TearDownTestCase();
virtual void SetUp() override;
virtual void TearDown() override;
const std::string defaultFile_ = "/data/snapshot_display_1.png";
const int defaultBitDepth_ = 8;
};
} // namespace Rosen
} // namespace OHOS
#endif // FRAMEWORKS_DM_TEST_UT_SNAPSHOT_UTILS_TEST_H