From 33b023dab0290d4cec0f8b5b37257e5887b40c32 Mon Sep 17 00:00:00 2001 From: liuyanzhi Date: Mon, 26 Dec 2022 07:07:17 +0000 Subject: [PATCH] add ans UT test Signed-off-by: liuyanzhi Change-Id: I2917fbd490b2aa124450b5b00049e53bfc2fda26 --- services/distributed/test/unittest/BUILD.gn | 167 ++++++++++++++ .../distributed_database_callback_test.cpp | 157 +++++++++++++ .../distributed_device_callback_test.cpp | 84 +++++++ .../distributed_flow_control_test.cpp | 74 ++++++ .../distributed_preferences_branch_test.cpp | 216 ++++++++++++++++++ .../mock_distributed_preferences_database.cpp | 80 +++++++ 6 files changed, 778 insertions(+) create mode 100755 services/distributed/test/unittest/distributed_database_callback_test.cpp create mode 100755 services/distributed/test/unittest/distributed_device_callback_test.cpp create mode 100755 services/distributed/test/unittest/distributed_flow_control_test.cpp create mode 100755 services/distributed/test/unittest/distributed_preferences_branch_test/distributed_preferences_branch_test.cpp create mode 100755 services/distributed/test/unittest/distributed_preferences_branch_test/mock_distributed_preferences_database.cpp diff --git a/services/distributed/test/unittest/BUILD.gn b/services/distributed/test/unittest/BUILD.gn index d7286306e..dc5efc2d9 100644 --- a/services/distributed/test/unittest/BUILD.gn +++ b/services/distributed/test/unittest/BUILD.gn @@ -23,6 +23,10 @@ group("ans_unit_test") { deps += [ ":ans_distributed_unit_test", ":distributed_database_branch_test", + ":distributed_database_callback_test", + ":distributed_device_callback_test", + ":distributed_flow_control_test", + ":distributed_preferences_branch_test", ":distributed_preferences_database_test", ":distributed_screen_status_manager_branch_test", ] @@ -224,3 +228,166 @@ ohos_unittest("distributed_database_branch_test") { subsystem_name = "${subsystem_name}" part_name = "${component_name}" } + +ohos_unittest("distributed_preferences_branch_test") { + module_out_path = "${component_name}/unittest" + include_dirs = [ + "/${services_path}/distributed/include", + "//commonlibrary/c_utils/base/include", + ] + + sources = [ + "distributed_preferences_branch_test/distributed_preferences_branch_test.cpp", + "distributed_preferences_branch_test/mock_distributed_preferences_database.cpp", + ] + + configs = [ + "${services_path}/distributed/:ans_distributed_config", + "${core_path}:public_ans_core_config", + "${frameworks_module_ans_path}:ans_innerkits_public_config", + ] + + deps = [ + "${core_path}:ans_core", + "${services_path}/distributed:libans_distributed", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", + "bundle_framework:appexecfwk_base", + "c_utils:utils", + "eventhandler:libeventhandler", + "hitrace_native:hitrace_meter", + "hitrace_native:libhitracechain", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "kv_store:distributeddata_inner", + "multimedia_image_framework:image_native", + ] + + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" +} + +ohos_unittest("distributed_device_callback_test") { + module_out_path = "${component_name}/unittest" + include_dirs = [ + "/${services_path}/distributed/include", + "//commonlibrary/c_utils/base/include", + ] + + sources = [ "distributed_device_callback_test.cpp" ] + + configs = [ + "${services_path}/distributed/:ans_distributed_config", + "${core_path}:public_ans_core_config", + "${frameworks_module_ans_path}:ans_innerkits_public_config", + ] + + deps = [ + "${core_path}:ans_core", + "${services_path}/distributed:libans_distributed", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", + "bundle_framework:appexecfwk_base", + "c_utils:utils", + "eventhandler:libeventhandler", + "hitrace_native:hitrace_meter", + "hitrace_native:libhitracechain", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "kv_store:distributeddata_inner", + "multimedia_image_framework:image_native", + ] + + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" +} + +ohos_unittest("distributed_database_callback_test") { + module_out_path = "${component_name}/unittest" + include_dirs = [ + "/${services_path}/distributed/include", + "//commonlibrary/c_utils/base/include", + ] + + sources = [ "distributed_database_callback_test.cpp" ] + + configs = [ + "${services_path}/distributed/:ans_distributed_config", + "${core_path}:public_ans_core_config", + "${frameworks_module_ans_path}:ans_innerkits_public_config", + ] + + deps = [ + "${core_path}:ans_core", + "${services_path}/distributed:libans_distributed", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", + "bundle_framework:appexecfwk_base", + "c_utils:utils", + "eventhandler:libeventhandler", + "hitrace_native:hitrace_meter", + "hitrace_native:libhitracechain", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "kv_store:distributeddata_inner", + "multimedia_image_framework:image_native", + ] + + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" +} + +ohos_unittest("distributed_flow_control_test") { + module_out_path = "${component_name}/unittest" + include_dirs = [ + "/${services_path}/distributed/include", + "//commonlibrary/c_utils/base/include", + ] + + sources = [ "distributed_flow_control_test.cpp" ] + + configs = [ + "${services_path}/distributed/:ans_distributed_config", + "${core_path}:public_ans_core_config", + "${frameworks_module_ans_path}:ans_innerkits_public_config", + ] + + deps = [ + "${core_path}:ans_core", + "${services_path}/distributed:libans_distributed", + "//third_party/googletest:gtest_main", + ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", + "bundle_framework:appexecfwk_base", + "c_utils:utils", + "eventhandler:libeventhandler", + "hitrace_native:hitrace_meter", + "hitrace_native:libhitracechain", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "kv_store:distributeddata_inner", + "multimedia_image_framework:image_native", + ] + + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" +} diff --git a/services/distributed/test/unittest/distributed_database_callback_test.cpp b/services/distributed/test/unittest/distributed_database_callback_test.cpp new file mode 100755 index 000000000..cedce847a --- /dev/null +++ b/services/distributed/test/unittest/distributed_database_callback_test.cpp @@ -0,0 +1,157 @@ +/* + * 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 + +#include "gtest/gtest.h" + +#define private public +#include "distributed_database_callback.h" + +using namespace testing::ext; +namespace OHOS { +namespace Notification { +class DistributedDatabaseCallbackTest : public testing::Test { +public: + void SetUp() override; + void TearDown() override; + +public: + virtual void OnInsert(const std::string &deviceId, const std::string &key, const std::string &value); + virtual void OnUpdate(const std::string &deviceId, const std::string &key, const std::string &value); + virtual void OnDelete(const std::string &deviceId, const std::string &key, const std::string &value); +}; + +void DistributedDatabaseCallbackTest::SetUp() +{} + +void DistributedDatabaseCallbackTest::TearDown() +{} + +void DistributedDatabaseCallbackTest::OnInsert(const std::string &deviceId, const std::string &key, const std::string &value) +{} + +void DistributedDatabaseCallbackTest::OnUpdate(const std::string &deviceId, const std::string &key, const std::string &value) +{} + +void DistributedDatabaseCallbackTest::OnDelete(const std::string &deviceId, const std::string &key, const std::string &value) +{} + +/** + * @tc.name : DistributedDatabaseCallback_00100 + * @tc.number : DistributedDatabaseCallback_00100 + * @tc.desc : test OnChange function and callback_.OnInsert is nullptr . + */ +HWTEST_F(DistributedDatabaseCallbackTest, DistributedDatabaseCallback_00100, Function | SmallTest | Level1) +{ + DistributedDatabaseCallback::IDatabaseChange databaseCallback = { + .OnInsert = std::bind(&DistributedDatabaseCallbackTest::OnInsert, + this, + std::placeholders::_1, + std::placeholders::_2, + std::placeholders::_3), + .OnUpdate = std::bind(&DistributedDatabaseCallbackTest::OnUpdate, + this, + std::placeholders::_1, + std::placeholders::_2, + std::placeholders::_3), + .OnDelete = std::bind(&DistributedDatabaseCallbackTest::OnDelete, + this, + std::placeholders::_1, + std::placeholders::_2, + std::placeholders::_3), + }; + std::shared_ptr databaseCallback_ = + std::make_shared(databaseCallback); + databaseCallback_->callback_.OnInsert = nullptr; + std::vector insertEntries; + std::vector updateEntries; + std::vector deleteEntries; + DistributedKv::ChangeNotification changeNotification( + std::move(insertEntries), std::move(updateEntries), std::move(deleteEntries), "", false); + databaseCallback_->OnChange(changeNotification); +} + +/** + * @tc.name : DistributedDatabaseCallback_00200 + * @tc.number : DistributedDatabaseCallback_00200 + * @tc.desc : test OnChange function and callback_.OnUpdate is nullptr . + */ +HWTEST_F(DistributedDatabaseCallbackTest, DistributedDatabaseCallback_00200, Function | SmallTest | Level1) +{ + DistributedDatabaseCallback::IDatabaseChange databaseCallback = { + .OnInsert = std::bind(&DistributedDatabaseCallbackTest::OnInsert, + this, + std::placeholders::_1, + std::placeholders::_2, + std::placeholders::_3), + .OnUpdate = std::bind(&DistributedDatabaseCallbackTest::OnUpdate, + this, + std::placeholders::_1, + std::placeholders::_2, + std::placeholders::_3), + .OnDelete = std::bind(&DistributedDatabaseCallbackTest::OnDelete, + this, + std::placeholders::_1, + std::placeholders::_2, + std::placeholders::_3), + }; + std::shared_ptr databaseCallback_ = + std::make_shared(databaseCallback); + databaseCallback_->callback_.OnUpdate = nullptr; + std::vector insertEntries; + std::vector updateEntries; + std::vector deleteEntries; + DistributedKv::ChangeNotification changeNotification( + std::move(insertEntries), std::move(updateEntries), std::move(deleteEntries), "", false); + databaseCallback_->OnChange(changeNotification); +} + +/** + * @tc.name : DistributedDatabaseCallback_00300 + * @tc.number : DistributedDatabaseCallback_00300 + * @tc.desc : test OnChange function and callback_.OnDelete is nullptr . + */ +HWTEST_F(DistributedDatabaseCallbackTest, DistributedDatabaseCallback_00300, Function | SmallTest | Level1) +{ + DistributedDatabaseCallback::IDatabaseChange databaseCallback = { + .OnInsert = std::bind(&DistributedDatabaseCallbackTest::OnInsert, + this, + std::placeholders::_1, + std::placeholders::_2, + std::placeholders::_3), + .OnUpdate = std::bind(&DistributedDatabaseCallbackTest::OnUpdate, + this, + std::placeholders::_1, + std::placeholders::_2, + std::placeholders::_3), + .OnDelete = std::bind(&DistributedDatabaseCallbackTest::OnDelete, + this, + std::placeholders::_1, + std::placeholders::_2, + std::placeholders::_3), + }; + std::shared_ptr databaseCallback_ = + std::make_shared(databaseCallback); + databaseCallback_->callback_.OnDelete = nullptr; + std::vector insertEntries; + std::vector updateEntries; + std::vector deleteEntries; + DistributedKv::ChangeNotification changeNotification( + std::move(insertEntries), std::move(updateEntries), std::move(deleteEntries), "", false); + databaseCallback_->OnChange(changeNotification); +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/services/distributed/test/unittest/distributed_device_callback_test.cpp b/services/distributed/test/unittest/distributed_device_callback_test.cpp new file mode 100755 index 000000000..8603e1cd6 --- /dev/null +++ b/services/distributed/test/unittest/distributed_device_callback_test.cpp @@ -0,0 +1,84 @@ +/* + * 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 + +#include "gtest/gtest.h" + +#define private public +#include "distributed_device_callback.h" + +using namespace testing::ext; +namespace OHOS { +namespace Notification { +class DistributedDeviceCallbackTest : public testing::Test { +public: + void SetUp() override; + void TearDown() override; + +public: + virtual void OnConnected(const std::string &deviceId); + virtual void OnDisconnected(const std::string &deviceId); +}; + +void DistributedDeviceCallbackTest::SetUp() +{} + +void DistributedDeviceCallbackTest::TearDown() +{} + +void DistributedDeviceCallbackTest::OnConnected(const std::string &deviceId) +{} + +void DistributedDeviceCallbackTest::OnDisconnected(const std::string &deviceId) +{} + +/** + * @tc.name : DistributedDeviceCallback_00100 + * @tc.number : DistributedDeviceCallback_00100 + * @tc.desc : test OnDeviceChanged function and callback_.OnConnected is nullptr . + */ +HWTEST_F(DistributedDeviceCallbackTest, DistributedDeviceCallback_00100, Function | SmallTest | Level1) +{ + DistributedDeviceCallback::IDeviceChange deviceCallback = { + .OnConnected = std::bind(&DistributedDeviceCallbackTest::OnConnected, this, std::placeholders::_1), + .OnDisconnected = std::bind(&DistributedDeviceCallbackTest::OnDisconnected, this, std::placeholders::_1), + }; + std::shared_ptr deviceCallback_ = + std::make_shared(deviceCallback); + deviceCallback_->callback_.OnConnected = nullptr; + DistributedKv::DeviceInfo info; + deviceCallback_->OnDeviceChanged(info, DistributedKv::DeviceChangeType::DEVICE_ONLINE); +} + +/** + * @tc.name : DistributedDeviceCallback_00200 + * @tc.number : DistributedDeviceCallback_00200 + * @tc.desc : test OnDeviceChanged function and callback_.OnDisconnected is nullptr . + */ +HWTEST_F(DistributedDeviceCallbackTest, DistributedDeviceCallback_00200, Function | SmallTest | Level1) +{ + DistributedDeviceCallback::IDeviceChange deviceCallback = { + .OnConnected = std::bind(&DistributedDeviceCallbackTest::OnConnected, this, std::placeholders::_1), + .OnDisconnected = std::bind(&DistributedDeviceCallbackTest::OnDisconnected, this, std::placeholders::_1), + }; + std::shared_ptr deviceCallback_ = + std::make_shared(deviceCallback); + deviceCallback_->callback_.OnDisconnected = nullptr; + DistributedKv::DeviceInfo info; + deviceCallback_->OnDeviceChanged(info, DistributedKv::DeviceChangeType::DEVICE_OFFLINE); +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/services/distributed/test/unittest/distributed_flow_control_test.cpp b/services/distributed/test/unittest/distributed_flow_control_test.cpp new file mode 100755 index 000000000..494792801 --- /dev/null +++ b/services/distributed/test/unittest/distributed_flow_control_test.cpp @@ -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. + */ + +#include + +#include "gtest/gtest.h" + +#define private public +#include "distributed_flow_control.h" + +using namespace testing::ext; +namespace OHOS { +namespace Notification { +class DistributedFlowControlTest : public testing::Test { +public: + void SetUp() override; + void TearDown() override; +}; + +void DistributedFlowControlTest::SetUp() +{} + +void DistributedFlowControlTest::TearDown() +{} + +/** + * @tc.name : DistributedFlowControl_00100 + * @tc.number : DistributedFlowControl_00100 + * @tc.desc : test KvManagerFlowControl function and listSize >= kvManagerMinuteMaxinum_ . + */ +HWTEST_F(DistributedFlowControlTest, DistributedFlowControl_00100, Function | SmallTest | Level1) +{ + size_t kvManagerSecondMaxinum = 0; + size_t kvManagerMinuteMaxinum = 0; + size_t kvStoreSecondMaxinum = 0; + size_t kvStoreMinuteMaxinum = 0; + DistributedFlowControl distributedFlowControl( + kvManagerSecondMaxinum, kvManagerMinuteMaxinum, kvStoreSecondMaxinum, kvStoreMinuteMaxinum); + std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); + distributedFlowControl.kvStoreTimestampList_.push_front(now); + EXPECT_EQ(false, distributedFlowControl.KvManagerFlowControl()); +} + +/** + * @tc.name : DistributedFlowControl_00200 + * @tc.number : DistributedFlowControl_00200 + * @tc.desc : test KvStoreFlowControl function and listSize >= kvStoreMinuteMaxinum_ . + */ +HWTEST_F(DistributedFlowControlTest, DistributedFlowControl_00200, Function | SmallTest | Level1) +{ + size_t kvManagerSecondMaxinum = 0; + size_t kvManagerMinuteMaxinum = 0; + size_t kvStoreSecondMaxinum = 0; + size_t kvStoreMinuteMaxinum = 0; + DistributedFlowControl distributedFlowControl( + kvManagerSecondMaxinum, kvManagerMinuteMaxinum, kvStoreSecondMaxinum, kvStoreMinuteMaxinum); + std::chrono::system_clock::time_point now = std::chrono::system_clock::now(); + distributedFlowControl.kvStoreTimestampList_.push_front(now); + EXPECT_EQ(false, distributedFlowControl.KvStoreFlowControl()); +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/services/distributed/test/unittest/distributed_preferences_branch_test/distributed_preferences_branch_test.cpp b/services/distributed/test/unittest/distributed_preferences_branch_test/distributed_preferences_branch_test.cpp new file mode 100755 index 000000000..99e2eb238 --- /dev/null +++ b/services/distributed/test/unittest/distributed_preferences_branch_test/distributed_preferences_branch_test.cpp @@ -0,0 +1,216 @@ +/* + * 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 + +#include "gtest/gtest.h" + +#include "ans_inner_errors.h" +#define private public +#include "distributed_preferences.h" +#include "distributed_preferences_info.h" + +extern void mockGetEntriesFromDistributedDB(bool mockRet); +extern void mockPutToDistributedDB(bool mockRet); +extern void mockDeleteToDistributedDB(bool mockRet); +extern void mockClearDatabase(bool mockRet); + +using namespace testing::ext; +namespace OHOS { +namespace Notification { +namespace { +const std::string DISTRIBUTED_LABEL = "distributed"; +const std::string DELIMITER = "|"; +const std::string MAIN_LABEL = "ans_main"; +const std::string BUNDLE_LABEL = "bundle"; +const std::string WITHOUT_APP = "without_app"; +} +class DistributedPreferencesBranchTest : public testing::Test { +public: + void SetUp() override; + void TearDown() override; + +protected: + std::shared_ptr distributedPreferences_; +}; + +void DistributedPreferencesBranchTest::SetUp() +{ + distributedPreferences_ = DistributedPreferences::GetInstance(); +} + +void DistributedPreferencesBranchTest::TearDown() +{ + distributedPreferences_ = nullptr; + DistributedPreferences::DestroyInstance(); +} + +/** + * @tc.name : DistributedPreferencesBranchTest_00100 + * @tc.number : DistributedPreferencesBranchTest_00100 + * @tc.desc : test ResolveDistributedKey function and distributedLabelEndPosition == std::string::npos. + */ +HWTEST_F(DistributedPreferencesBranchTest, DistributedPreferencesBranchTest_00100, Function | SmallTest | Level1) +{ + DistributedKv::Entry entry; + entry.key = "DistributedPreferencesBranchTest_00100"; + EXPECT_EQ(distributedPreferences_->ResolveDistributedKey(entry), false); +} + +/** + * @tc.name : DistributedPreferencesBranchTest_00200 + * @tc.number : DistributedPreferencesBranchTest_00200 + * @tc.desc : test ResolveDistributedKey function and typeLabelPosition != std::string::npos. + */ +HWTEST_F(DistributedPreferencesBranchTest, DistributedPreferencesBranchTest_00200, Function | SmallTest | Level1) +{ + DistributedKv::Entry entry; + entry.key = DISTRIBUTED_LABEL + DELIMITER + MAIN_LABEL + DELIMITER + BUNDLE_LABEL + DELIMITER + WITHOUT_APP; + EXPECT_EQ(distributedPreferences_->ResolveDistributedKey(entry), true); +} + +/** + * @tc.name : DistributedPreferencesBranchTest_00300 + * @tc.number : DistributedPreferencesBranchTest_00300 + * @tc.desc : test InitDistributedAllInfo function and GetEntriesFromDistributedDB is false. + */ +HWTEST_F(DistributedPreferencesBranchTest, DistributedPreferencesBranchTest_00300, Function | SmallTest | Level1) +{ + mockGetEntriesFromDistributedDB(false); + EXPECT_EQ(distributedPreferences_->InitDistributedAllInfo(), false); +} + +/** + * @tc.name : DistributedPreferencesBranchTest_00400 + * @tc.number : DistributedPreferencesBranchTest_00400 + * @tc.desc : test InitDistributedAllInfo function and ResolveDistributedKey is false. + */ +HWTEST_F(DistributedPreferencesBranchTest, DistributedPreferencesBranchTest_00400, Function | SmallTest | Level1) +{ + mockGetEntriesFromDistributedDB(true); + EXPECT_EQ(distributedPreferences_->InitDistributedAllInfo(), true); +} + +/** + * @tc.name : DistributedPreferencesBranchTest_00500 + * @tc.number : DistributedPreferencesBranchTest_00500 + * @tc.desc : test GetDistributedBundleKey function and bundleOption is nullptr. + */ +HWTEST_F(DistributedPreferencesBranchTest, DistributedPreferencesBranchTest_00500, Function | SmallTest | Level1) +{ + sptr bundleOption = nullptr; + std::string key = "DistributedPreferencesBranchTest"; + distributedPreferences_->GetDistributedBundleKey(bundleOption, key); +} + +/** + * @tc.name : DistributedPreferencesBranchTest_00600 + * @tc.number : DistributedPreferencesBranchTest_00600 + * @tc.desc : test SetDistributedEnable function and PutToDistributedDB is false. + */ +HWTEST_F(DistributedPreferencesBranchTest, DistributedPreferencesBranchTest_00600, Function | SmallTest | Level1) +{ + bool isEnable = true; + mockPutToDistributedDB(false); + EXPECT_EQ(distributedPreferences_->SetDistributedEnable(isEnable), ERR_ANS_DISTRIBUTED_OPERATION_FAILED); +} + +/** + * @tc.name : DistributedPreferencesBranchTest_00700 + * @tc.number : DistributedPreferencesBranchTest_00700 + * @tc.desc : test SetDistributedBundleEnable function and bundleOption is nullptr. + */ +HWTEST_F(DistributedPreferencesBranchTest, DistributedPreferencesBranchTest_00700, Function | SmallTest | Level1) +{ + sptr bundleOption = nullptr; + bool isEnable = true; + EXPECT_EQ(distributedPreferences_->SetDistributedBundleEnable(bundleOption, isEnable), ERR_ANS_INVALID_PARAM); +} + +/** + * @tc.name : DistributedPreferencesBranchTest_00800 + * @tc.number : DistributedPreferencesBranchTest_00800 + * @tc.desc : test SetDistributedBundleEnable function and PutToDistributedDB is false. + */ +HWTEST_F(DistributedPreferencesBranchTest, DistributedPreferencesBranchTest_00800, Function | SmallTest | Level1) +{ + sptr bundleOption = new NotificationBundleOption("", 783); + bool isEnable = true; + mockPutToDistributedDB(false); + EXPECT_EQ(distributedPreferences_->SetDistributedBundleEnable(bundleOption, isEnable), + ERR_ANS_DISTRIBUTED_OPERATION_FAILED); +} + +/** + * @tc.name : DistributedPreferencesBranchTest_00900 + * @tc.number : DistributedPreferencesBranchTest_00900 + * @tc.desc : test GetDistributedBundleEnable function and bundleOption is nullptr. + */ +HWTEST_F(DistributedPreferencesBranchTest, DistributedPreferencesBranchTest_00900, Function | SmallTest | Level1) +{ + sptr bundleOption = nullptr; + bool isEnable = true; + EXPECT_EQ(distributedPreferences_->GetDistributedBundleEnable(bundleOption, isEnable), ERR_ANS_INVALID_PARAM); +} + +/** + * @tc.name : DistributedPreferencesBranchTest_01000 + * @tc.number : DistributedPreferencesBranchTest_01000 + * @tc.desc : test DeleteDistributedBundleInfo function and bundleOption is nullptr. + */ +HWTEST_F(DistributedPreferencesBranchTest, DistributedPreferencesBranchTest_01000, Function | SmallTest | Level1) +{ + sptr bundleOption = nullptr; + EXPECT_EQ(distributedPreferences_->DeleteDistributedBundleInfo(bundleOption), ERR_ANS_INVALID_PARAM); +} + +/** + * @tc.name : DistributedPreferencesBranchTest_01100 + * @tc.number : DistributedPreferencesBranchTest_01100 + * @tc.desc : test DeleteDistributedBundleInfo function and DeleteToDistributedDB is false. + */ +HWTEST_F(DistributedPreferencesBranchTest, DistributedPreferencesBranchTest_01100, Function | SmallTest | Level1) +{ + sptr bundleOption = new NotificationBundleOption("", 783); + mockDeleteToDistributedDB(false); + EXPECT_EQ( + distributedPreferences_->DeleteDistributedBundleInfo(bundleOption), ERR_ANS_DISTRIBUTED_OPERATION_FAILED); +} + +/** + * @tc.name : DistributedPreferencesBranchTest_01200 + * @tc.number : DistributedPreferencesBranchTest_01200 + * @tc.desc : test ClearDataInRestoreFactorySettings function and ClearDatabase is false. + */ +HWTEST_F(DistributedPreferencesBranchTest, DistributedPreferencesBranchTest_01200, Function | SmallTest | Level1) +{ + mockClearDatabase(false); + EXPECT_EQ(distributedPreferences_->ClearDataInRestoreFactorySettings(), ERR_ANS_DISTRIBUTED_OPERATION_FAILED); +} + +/** + * @tc.name : DistributedPreferencesBranchTest_01300 + * @tc.number : DistributedPreferencesBranchTest_01300 + * @tc.desc : test SetSyncEnabledWithoutApp function and PutToDistributedDB is false. + */ +HWTEST_F(DistributedPreferencesBranchTest, DistributedPreferencesBranchTest_01300, Function | SmallTest | Level1) +{ + mockPutToDistributedDB(false); + int32_t userId = 1; + bool enabled = true; + EXPECT_EQ(distributedPreferences_->SetSyncEnabledWithoutApp(userId, enabled), ERR_ANS_DISTRIBUTED_OPERATION_FAILED); +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/services/distributed/test/unittest/distributed_preferences_branch_test/mock_distributed_preferences_database.cpp b/services/distributed/test/unittest/distributed_preferences_branch_test/mock_distributed_preferences_database.cpp new file mode 100755 index 000000000..eeb8e0472 --- /dev/null +++ b/services/distributed/test/unittest/distributed_preferences_branch_test/mock_distributed_preferences_database.cpp @@ -0,0 +1,80 @@ +/* + * 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 "distributed_preferences_database.h" + +namespace { + bool g_mockGetEntriesFromDistributedDBRet = true; + bool g_mockPutToDistributedDBRet = true; + bool g_mockDeleteToDistributedDBRet = true; + bool g_mockClearDatabaseRet = true; +} + +void mockGetEntriesFromDistributedDB(bool mockRet) +{ + g_mockGetEntriesFromDistributedDBRet = mockRet; +} + +void mockPutToDistributedDB(bool mockRet) +{ + g_mockPutToDistributedDBRet = mockRet; +} + +void mockDeleteToDistributedDB(bool mockRet) +{ + g_mockDeleteToDistributedDBRet = mockRet; +} + +void mockClearDatabase(bool mockRet) +{ + g_mockClearDatabaseRet = mockRet; +} + +namespace OHOS { +namespace Notification { + +DistributedPreferencesDatabase::DistributedPreferencesDatabase() : DistributedFlowControl() +{ + GetKvDataManager(); +} + +DistributedPreferencesDatabase::~DistributedPreferencesDatabase() +{} + +bool DistributedPreferencesDatabase::GetEntriesFromDistributedDB( + const std::string &prefixKey, std::vector &entries) +{ + Entry entry; + entry.key = "GetEntriesFromDistributedDB"; + entries.emplace_back(entry); + return g_mockGetEntriesFromDistributedDBRet; +} + +bool DistributedPreferencesDatabase::PutToDistributedDB(const std::string &key, const std::string &value) +{ + return g_mockPutToDistributedDBRet; +} + +bool DistributedPreferencesDatabase::DeleteToDistributedDB(const std::string &key) +{ + return g_mockDeleteToDistributedDBRet; +} + +bool DistributedPreferencesDatabase::ClearDatabase() +{ + return g_mockClearDatabaseRet; +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file -- Gitee