diff --git a/window_scene/test/unittest/BUILD.gn b/window_scene/test/unittest/BUILD.gn index 2cfe58fd593cc42ba2439a3c1a5f0fbb8e883de0..2e4f0cacf1e149012de4576a6149d52e0ab61b9e 100644 --- a/window_scene/test/unittest/BUILD.gn +++ b/window_scene/test/unittest/BUILD.gn @@ -99,6 +99,7 @@ group("unittest") { ":ws_window_event_channel_test", ":ws_window_scene_config_test", ":ws_window_session_property_test", + "window_pattern:window_pattern_snapshot_test", ] if (window_manager_use_sceneboard) { @@ -1643,7 +1644,10 @@ config("ws_unittest_common_public_config") { } ohos_static_library("ws_unittest_common") { - visibility = [ ":*" ] + visibility = [ + ":*", + "window_pattern:*", + ] testonly = true public_configs = [ diff --git a/window_scene/test/unittest/window_pattern/BUILD.gn b/window_scene/test/unittest/window_pattern/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..c43b9539f15bcce30c5595fb626e006a4aa77fa9 --- /dev/null +++ b/window_scene/test/unittest/window_pattern/BUILD.gn @@ -0,0 +1,26 @@ +# Copyright (c) 2024 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") +import("../../../../windowmanager_aafwk.gni") + +module_out_path = "window_manager/window_scene/window_pattern" +ws_unittest_common = "../:ws_unittest_common" + +ohos_unittest("window_pattern_snapshot_test") { + module_out_path = module_out_path + sources = [ "window_pattern_snapshot_test.cpp" ] + include_dirs = [ "${window_base_path}/window_scene/session/host/include" ] + deps = [ ws_unittest_common ] + external_deps = [ "c_utils:utils" ] +} diff --git a/window_scene/test/unittest/window_pattern/window_pattern_snapshot_test.cpp b/window_scene/test/unittest/window_pattern/window_pattern_snapshot_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b5946e381db1b318e6cd5b6014ace54eea213e2 --- /dev/null +++ b/window_scene/test/unittest/window_pattern/window_pattern_snapshot_test.cpp @@ -0,0 +1,254 @@ +/* + * Copyright (c) 2023 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 "scene_persistence.h" +#include "session/host/include/scene_session.h" +#include "session.h" +#include +#include "session_info.h" +#include "ws_common.h" + +using namespace testing; +using namespace testing::ext; + +namespace OHOS { +namespace Rosen { +class WindowPatternSnapshotTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp() override; + void TearDown() override; +private: + std::shared_ptr mPixelMap = std::make_shared(); +}; + +constexpr const char* UNDERLINE_SEPARATOR = "_"; +constexpr const char* IMAGE_SUFFIX = ".jpg"; + +static sptr scenePersistence = sptr::MakeSptr("WindowPatternSnapshotTest", 1423); + +void WindowPatternSnapshotTest::SetUpTestCase() +{ +} + +void WindowPatternSnapshotTest::TearDownTestCase() +{ +} + +void WindowPatternSnapshotTest::SetUp() +{ +} + +void WindowPatternSnapshotTest::TearDown() +{ +} + +namespace { +/** + * @tc.name: CreateSnapshotDir + * @tc.desc: test function : CreateSnapshotDir + * @tc.type: FUNC + */ +HWTEST_F(WindowPatternSnapshotTest, CreateSnapshotDir, Function | SmallTest | Level1) +{ + std::string directory = "0/Storage"; + ASSERT_NE(nullptr, scenePersistence); + bool result = scenePersistence->CreateSnapshotDir(directory); + ASSERT_EQ(result, false); +} + +/** + * @tc.name: CreateUpdatedIconDir + * @tc.desc: test function : CreateUpdatedIconDir + * @tc.type: FUNC + */ +HWTEST_F(WindowPatternSnapshotTest, CreateUpdatedIconDir, Function | SmallTest | Level1) +{ + std::string directory = "0/Storage"; + ASSERT_NE(nullptr, scenePersistence); + bool result = scenePersistence->CreateUpdatedIconDir(directory); + ASSERT_EQ(result, false); +} + +/** + * @tc.name: SaveSnapshot + * @tc.desc: test function : SaveSnapshot + * @tc.type: FUNC + */ +HWTEST_F(WindowPatternSnapshotTest, SaveSnapshot, Function | SmallTest | Level1) +{ + std::shared_ptr pixelMap = nullptr; + std::string directory = "0/Storage"; + std::string bundleName = "testBundleName"; + + SessionInfo info; + info.abilityName_ = bundleName; + info.bundleName_ = bundleName; + sptr sceneSession = sptr::MakeSptr(info, nullptr); + + int32_t persistentId = 1423; + ASSERT_NE(nullptr, scenePersistence); + scenePersistence->SaveSnapshot(pixelMap); + + sptr session = sptr::MakeSptr(info); + ASSERT_NE(nullptr, session); + scenePersistence->snapshotPath_ = "/data/1.png"; + + scenePersistence->SaveSnapshot(mPixelMap); + uint32_t fileID = static_cast(persistentId) & 0x3fffffff; + std::string test = ScenePersistence::snapshotDirectory_ + + bundleName + UNDERLINE_SEPARATOR + std::to_string(fileID) + IMAGE_SUFFIX; + std::pair sizeResult = scenePersistence->GetSnapshotSize(); + EXPECT_EQ(sizeResult.first, 0); + EXPECT_EQ(sizeResult.second, 0); +} + +/** + * @tc.name: RenameSnapshotFromOldPersistentId + * @tc.desc: test function : RenameSnapshotFromOldPersistentId + * @tc.type: FUNC + */ +HWTEST_F(WindowPatternSnapshotTest, RenameSnapshotFromOldPersistentId, Function | SmallTest | Level3) +{ + int ret = 0; + int32_t persistentId = 1424; + std::string bundleName = "testBundleName"; + sptr scenePersistence2 = sptr::MakeSptr(bundleName, persistentId); + scenePersistence2->RenameSnapshotFromOldPersistentId(persistentId); + ASSERT_EQ(ret, 0); + + sptr scenePersistence3 = sptr::MakeSptr(bundleName, persistentId); + ASSERT_NE(nullptr, scenePersistence3); + scenePersistence3->snapshotPath_ = "/data/1.png"; + scenePersistence3->RenameSnapshotFromOldPersistentId(persistentId); +} + +/** + * @tc.name: IsSnapshotExisted + * @tc.desc: test function : IsSnapshotExisted + * @tc.type: FUNC + */ +HWTEST_F(WindowPatternSnapshotTest, IsSnapshotExisted, Function | SmallTest | Level1) +{ + std::string bundleName = "testBundleName"; + int32_t persistentId = 1423; + sptr scenePersistence = sptr::MakeSptr(bundleName, persistentId); + ASSERT_NE(nullptr, scenePersistence); + bool result = scenePersistence->IsSnapshotExisted(); + ASSERT_EQ(result, false); +} + +/** + * @tc.name: GetLocalSnapshotPixelMap + * @tc.desc: test function : get local snapshot pixelmap + * @tc.type: FUNC + */ +HWTEST_F(WindowPatternSnapshotTest, GetLocalSnapshotPixelMap, Function | SmallTest | Level1) +{ + SessionInfo info; + info.abilityName_ = "GetPixelMap"; + info.bundleName_ = "GetPixelMap1"; + sptr session = sptr::MakeSptr(info); + ASSERT_NE(nullptr, session); + auto abilityInfo = session->GetSessionInfo(); + auto persistentId = abilityInfo.persistentId_; + ScenePersistence::CreateSnapshotDir("storage"); + sptr scenePersistence = + sptr::MakeSptr(abilityInfo.bundleName_, persistentId); + ASSERT_NE(nullptr, scenePersistence); + auto result = scenePersistence->GetLocalSnapshotPixelMap(0.5, 0.5); + EXPECT_EQ(result, nullptr); + + bool result2 = scenePersistence->IsSnapshotExisted(); + EXPECT_EQ(result2, false); + + // create pixelMap + const uint32_t colors[1] = { 0x6f0000ff }; + constexpr uint32_t COMMON_SIZE = 1; + uint32_t colorsLength = sizeof(colors) / sizeof(colors[0]); + const int32_t offset = 0; + Media::InitializationOptions opts; + opts.size.width = COMMON_SIZE; + opts.size.height = COMMON_SIZE; + opts.pixelFormat = Media::PixelFormat::RGBA_8888; + opts.alphaType = Media::AlphaType::IMAGE_ALPHA_TYPE_OPAQUE; + int32_t stride = opts.size.width; + std::shared_ptr pixelMap1 = Media::PixelMap::Create(colors, colorsLength, offset, stride, opts); + + scenePersistence->SaveSnapshot(pixelMap1); + int maxScenePersistencePollNum = 100; + scenePersistence->snapshotPath_ = "/data/1.png"; + for (int i = 0; i < maxScenePersistencePollNum; i++) { + result = scenePersistence->GetLocalSnapshotPixelMap(0.8, 0.2); + result2 = scenePersistence->IsSnapshotExisted(); + } + EXPECT_NE(result, nullptr); + ASSERT_EQ(result2, true); + + result = scenePersistence->GetLocalSnapshotPixelMap(0.0, 0.2); + EXPECT_NE(result, nullptr); +} + +/** + * @tc.name: IsSavingSnapshot + * @tc.desc: test function : IsSavingSnapshot + * @tc.type: FUNC + */ +HWTEST_F(WindowPatternSnapshotTest, IsSavingSnapshot, Function | SmallTest | Level1) +{ + std::string bundleName = "testBundleName"; + int32_t persistentId = 1423; + sptr scenePersistence = sptr::MakeSptr(bundleName, persistentId); + ASSERT_NE(nullptr, scenePersistence); + bool result = scenePersistence->IsSavingSnapshot(); + ASSERT_EQ(result, false); +} + +/** + * @tc.name: GetSnapshotFilePath + * @tc.desc: test function : GetSnapshotFilePath + * @tc.type: FUNC + */ +HWTEST_F(WindowPatternSnapshotTest, GetSnapshotFilePath, Function | SmallTest | Level1) +{ + std::string bundleName = "testBundleName"; + int32_t persistentId = 1423; + sptr scenePersistence = sptr::MakeSptr(bundleName, persistentId); + ASSERT_NE(nullptr, scenePersistence); + scenePersistence->RenameSnapshotFromOldPersistentId(0); + auto result = scenePersistence->GetSnapshotFilePath(); + ASSERT_EQ(result, scenePersistence->snapshotPath_); +} + +/** + * @tc.name: HasSnapshot + * @tc.desc: test function: HasSnapshot + * @tc.type: FUNC + */ +HWTEST_F(WindowPatternSnapshotTest, HasSnapshot, Function | SmallTest | Level1) +{ + std::string bundleName = "testBundleName"; + int32_t persistentId = 1423; + sptr scenePersistence = sptr::MakeSptr(bundleName, persistentId); + ASSERT_NE(nullptr, scenePersistence); + scenePersistence->SetHasSnapshot(true); + ASSERT_EQ(scenePersistence->HasSnapshot(), true); + scenePersistence->SetHasSnapshot(false); + ASSERT_EQ(scenePersistence->HasSnapshot(), false); +} +} +} +} \ No newline at end of file