diff --git a/services/distributed/test/unittest/BUILD.gn b/services/distributed/test/unittest/BUILD.gn index df46a8bfba8abad3ce43f79816d2a14cea44c007..6e816c21b59a977b02bd619e3a7b425a9a96239c 100644 --- a/services/distributed/test/unittest/BUILD.gn +++ b/services/distributed/test/unittest/BUILD.gn @@ -20,7 +20,11 @@ group("ans_unit_test") { deps = [] if (distributed_notification_supported) { - deps += [ ":ans_distributed_unit_test" ] + deps += [ + ":ans_distributed_unit_test", + ":distributed_preferences_database_test", + ":distributed_screen_status_manager_branch_test", + ] } } @@ -81,3 +85,95 @@ ohos_unittest("ans_distributed_unit_test") { subsystem_name = "${subsystem_name}" part_name = "${component_name}" } + +ohos_unittest("distributed_preferences_database_test") { + module_out_path = "${component_name}/unittest" + include_dirs = [ + "/${services_path}/distributed/include", + "${services_path}/distributed/test/unittest/mock", + "//commonlibrary/c_utils/base/include", + ] + + sources = [ + "distributed_preferences_database_test/distributed_preferences_database_test.cpp", + "distributed_preferences_database_test/mock_distributed_flow_control.cpp", + "distributed_preferences_database_test/mock_distributed_kv_data_manager.cpp", + "distributed_preferences_database_test/mock_single_kv_store.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_screen_status_manager_branch_test") { + module_out_path = "${component_name}/unittest" + include_dirs = [ + "/${services_path}/distributed/include", + "//commonlibrary/c_utils/base/include", + "${services_path}/distributed/test/unittest/mock", + ] + + sources = [ + "distributed_screen_status_manager_branch_test/distributed_screen_status_manager_branch_test.cpp", + "distributed_screen_status_manager_branch_test/mock_distributed_flow_control.cpp", + "distributed_screen_status_manager_branch_test/mock_distributed_kv_data_manager.cpp", + "distributed_screen_status_manager_branch_test/mock_single_kv_store.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_preferences_database_test/distributed_preferences_database_test.cpp b/services/distributed/test/unittest/distributed_preferences_database_test/distributed_preferences_database_test.cpp new file mode 100755 index 0000000000000000000000000000000000000000..dac6f71e6954399889c22db784ca39060d00bdc5 --- /dev/null +++ b/services/distributed/test/unittest/distributed_preferences_database_test/distributed_preferences_database_test.cpp @@ -0,0 +1,318 @@ +/* + * 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_preferences_database.h" + +#include "mock_single_kv_store.h" + +extern void mockKvStoreFlowControl(bool mockRet); +extern void mockGet(bool mockRet); +extern void mockKvManagerFlowControl(bool mockRet); +extern void mockCloseKvStore(bool mockRet); + +using namespace testing::ext; +using namespace OHOS::DistributedKv; +namespace OHOS { +namespace Notification { +class DistributedPreferencesDatabaseTest : public testing::Test { +public: + void SetUp() override; + void TearDown() override; +}; + +void DistributedPreferencesDatabaseTest::SetUp() +{} + +void DistributedPreferencesDatabaseTest::TearDown() +{} + +/** + * @tc.name : DistributedPreferencesDatabase_00100 + * @tc.number : DistributedPreferencesDatabase_00100 + * @tc.desc : test CheckKvDataManager and kvDataManager_ == nullptr. + */ +HWTEST_F(DistributedPreferencesDatabaseTest, DistributedPreferencesDatabase_00100, Function | SmallTest | Level1) +{ + DistributedPreferencesDatabase distributedPreferencesDatabase; + distributedPreferencesDatabase.kvDataManager_ = nullptr; + EXPECT_EQ(true, distributedPreferencesDatabase.CheckKvDataManager()); +} + +/** + * @tc.name : DistributedPreferencesDatabase_00200 + * @tc.number : DistributedPreferencesDatabase_00200 + * @tc.desc : test GetKvStore and status != DistributedKv::Status::SUCCESS. + */ +HWTEST_F(DistributedPreferencesDatabaseTest, DistributedPreferencesDatabase_00200, Function | SmallTest | Level1) +{ + DistributedPreferencesDatabase distributedPreferencesDatabase; + distributedPreferencesDatabase.GetKvStore(); +} + +/** + * @tc.name : DistributedPreferencesDatabase_00300 + * @tc.number : DistributedPreferencesDatabase_00300 + * @tc.desc : test CheckKvStore and kvStore_ == nullptr. + */ +HWTEST_F(DistributedPreferencesDatabaseTest, DistributedPreferencesDatabase_00300, Function | SmallTest | Level1) +{ + DistributedPreferencesDatabase distributedPreferencesDatabase; + distributedPreferencesDatabase.kvStore_ = nullptr; + EXPECT_EQ(false, distributedPreferencesDatabase.CheckKvStore()); +} + +/** + * @tc.name : DistributedPreferencesDatabase_00400 + * @tc.number : DistributedPreferencesDatabase_00400 + * @tc.desc : test PutToDistributedDB and CheckKvStore is false. + */ +HWTEST_F(DistributedPreferencesDatabaseTest, DistributedPreferencesDatabase_00400, Function | SmallTest | Level1) +{ + DistributedPreferencesDatabase distributedPreferencesDatabase; + distributedPreferencesDatabase.kvStore_ = nullptr; + std::string key = "aa"; + std::string value = "bb"; + EXPECT_EQ(false, distributedPreferencesDatabase.PutToDistributedDB(key, value)); +} + +/** + * @tc.name : DistributedPreferencesDatabase_00500 + * @tc.number : DistributedPreferencesDatabase_00500 + * @tc.desc : test PutToDistributedDB and KvStoreFlowControl is false. + */ +HWTEST_F(DistributedPreferencesDatabaseTest, DistributedPreferencesDatabase_00500, Function | SmallTest | Level1) +{ + DistributedPreferencesDatabase distributedPreferencesDatabase; + std::shared_ptr kvStore = std::make_shared(); + distributedPreferencesDatabase.kvStore_ = std::static_pointer_cast(kvStore); + mockKvStoreFlowControl(false); + std::string key = "aa"; + std::string value = "bb"; + EXPECT_EQ(false, distributedPreferencesDatabase.PutToDistributedDB(key, value)); +} + +/** + * @tc.name : DistributedPreferencesDatabase_00600 + * @tc.number : DistributedPreferencesDatabase_00600 + * @tc.desc : test PutToDistributedDB and status != DistributedKv::Status::SUCCESS. + */ +HWTEST_F(DistributedPreferencesDatabaseTest, DistributedPreferencesDatabase_00600, Function | SmallTest | Level1) +{ + DistributedPreferencesDatabase distributedPreferencesDatabase; + std::shared_ptr kvStore = std::make_shared(); + distributedPreferencesDatabase.kvStore_ = std::static_pointer_cast(kvStore); + mockKvStoreFlowControl(true); + std::string key = "aa"; + std::string value = "bb"; + EXPECT_EQ(false, distributedPreferencesDatabase.PutToDistributedDB(key, value)); +} + +/** + * @tc.name : DistributedPreferencesDatabase_00700 + * @tc.number : DistributedPreferencesDatabase_00700 + * @tc.desc : test GetFromDistributedDB and CheckKvStore is false. + */ +HWTEST_F(DistributedPreferencesDatabaseTest, DistributedPreferencesDatabase_00700, Function | SmallTest | Level1) +{ + DistributedPreferencesDatabase distributedPreferencesDatabase; + distributedPreferencesDatabase.kvStore_ = nullptr; + std::string key = "aa"; + std::string value = "bb"; + EXPECT_EQ(false, distributedPreferencesDatabase.GetFromDistributedDB(key, value)); +} + +/** + * @tc.name : DistributedPreferencesDatabase_00800 + * @tc.number : DistributedPreferencesDatabase_00800 + * @tc.desc : test GetFromDistributedDB and CheckKvStore is true and KvStoreFlowControl is false. + */ +HWTEST_F(DistributedPreferencesDatabaseTest, DistributedPreferencesDatabase_00800, Function | SmallTest | Level1) +{ + DistributedPreferencesDatabase distributedPreferencesDatabase; + std::shared_ptr kvStore = std::make_shared(); + distributedPreferencesDatabase.kvStore_ = std::static_pointer_cast(kvStore); + mockKvStoreFlowControl(false); + std::string key = "aa"; + std::string value = "bb"; + EXPECT_EQ(false, distributedPreferencesDatabase.GetFromDistributedDB(key, value)); +} + +/** + * @tc.name : DistributedPreferencesDatabase_00900 + * @tc.number : DistributedPreferencesDatabase_00900 + * @tc.desc : test GetFromDistributedDB KvStoreFlowControl is true and status != DistributedKv::Status::SUCCESS. + */ +HWTEST_F(DistributedPreferencesDatabaseTest, DistributedPreferencesDatabase_00900, Function | SmallTest | Level1) +{ + DistributedPreferencesDatabase distributedPreferencesDatabase; + std::shared_ptr kvStore = std::make_shared(); + distributedPreferencesDatabase.kvStore_ = std::static_pointer_cast(kvStore); + mockKvStoreFlowControl(true); + mockGet(true); + std::string key = "aa"; + std::string value = "bb"; + EXPECT_EQ(false, distributedPreferencesDatabase.GetFromDistributedDB(key, value)); +} + +/** + * @tc.name : DistributedPreferencesDatabase_01000 + * @tc.number : DistributedPreferencesDatabase_01000 + * @tc.desc : test GetFromDistributedDB KvStoreFlowControl is true and status == DistributedKv::Status::SUCCESS. + */ +HWTEST_F(DistributedPreferencesDatabaseTest, DistributedPreferencesDatabase_01000, Function | SmallTest | Level1) +{ + DistributedPreferencesDatabase distributedPreferencesDatabase; + std::shared_ptr kvStore = std::make_shared(); + distributedPreferencesDatabase.kvStore_ = std::static_pointer_cast(kvStore); + mockKvStoreFlowControl(true); + mockGet(false); + std::string key = "aa"; + std::string value = "bb"; + EXPECT_EQ(true, distributedPreferencesDatabase.GetFromDistributedDB(key, value)); +} + +/** + * @tc.name : DistributedPreferencesDatabase_01100 + * @tc.number : DistributedPreferencesDatabase_01100 + * @tc.desc : test GetEntriesFromDistributedDB and CheckKvStore is false. + */ +HWTEST_F(DistributedPreferencesDatabaseTest, DistributedPreferencesDatabase_01100, Function | SmallTest | Level1) +{ + DistributedPreferencesDatabase distributedPreferencesDatabase; + distributedPreferencesDatabase.kvStore_ = nullptr; + std::string prefixKey = "aa"; + std::vector entries; + EXPECT_EQ(false, distributedPreferencesDatabase.GetEntriesFromDistributedDB(prefixKey, entries)); +} + +/** + * @tc.name : DistributedPreferencesDatabase_01200 + * @tc.number : DistributedPreferencesDatabase_01200 + * @tc.desc : test GetEntriesFromDistributedDB and KvStoreFlowControl is false. + */ +HWTEST_F(DistributedPreferencesDatabaseTest, DistributedPreferencesDatabase_01200, Function | SmallTest | Level1) +{ + DistributedPreferencesDatabase distributedPreferencesDatabase; + std::shared_ptr kvStore = std::make_shared(); + distributedPreferencesDatabase.kvStore_ = std::static_pointer_cast(kvStore); + mockKvStoreFlowControl(false); + std::string prefixKey = "aa"; + std::vector entries; + EXPECT_EQ(false, distributedPreferencesDatabase.GetEntriesFromDistributedDB(prefixKey, entries)); +} + +/** + * @tc.name : DistributedPreferencesDatabase_01300 + * @tc.number : DistributedPreferencesDatabase_01300 + * @tc.desc : test GetEntriesFromDistributedDB and status != DistributedKv::Status::SUCCESS. + */ +HWTEST_F(DistributedPreferencesDatabaseTest, DistributedPreferencesDatabase_01300, Function | SmallTest | Level1) +{ + DistributedPreferencesDatabase distributedPreferencesDatabase; + std::shared_ptr kvStore = std::make_shared(); + distributedPreferencesDatabase.kvStore_ = std::static_pointer_cast(kvStore); + mockKvStoreFlowControl(true); + std::string prefixKey = "aa"; + std::vector entries; + EXPECT_EQ(false, distributedPreferencesDatabase.GetEntriesFromDistributedDB(prefixKey, entries)); +} + +/** + * @tc.name : DistributedPreferencesDatabase_01400 + * @tc.number : DistributedPreferencesDatabase_01400 + * @tc.desc : test DeleteToDistributedDB and CheckKvStore is false. + */ +HWTEST_F(DistributedPreferencesDatabaseTest, DistributedPreferencesDatabase_01400, Function | SmallTest | Level1) +{ + DistributedPreferencesDatabase distributedPreferencesDatabase; + distributedPreferencesDatabase.kvStore_ = nullptr; + std::string key = "aa"; + EXPECT_EQ(false, distributedPreferencesDatabase.DeleteToDistributedDB(key)); +} + +/** + * @tc.name : DistributedPreferencesDatabase_01500 + * @tc.number : DistributedPreferencesDatabase_01500 + * @tc.desc : test DeleteToDistributedDB and KvStoreFlowControl is false. + */ +HWTEST_F(DistributedPreferencesDatabaseTest, DistributedPreferencesDatabase_01500, Function | SmallTest | Level1) +{ + DistributedPreferencesDatabase distributedPreferencesDatabase; + std::shared_ptr kvStore = std::make_shared(); + distributedPreferencesDatabase.kvStore_ = std::static_pointer_cast(kvStore); + mockKvStoreFlowControl(false); + std::string key = "aa"; + EXPECT_EQ(false, distributedPreferencesDatabase.DeleteToDistributedDB(key)); +} + +/** + * @tc.name : DistributedPreferencesDatabase_01600 + * @tc.number : DistributedPreferencesDatabase_01600 + * @tc.desc : test DeleteToDistributedDB and status != DistributedKv::Status::SUCCESS. + */ +HWTEST_F(DistributedPreferencesDatabaseTest, DistributedPreferencesDatabase_01600, Function | SmallTest | Level1) +{ + DistributedPreferencesDatabase distributedPreferencesDatabase; + std::shared_ptr kvStore = std::make_shared(); + distributedPreferencesDatabase.kvStore_ = std::static_pointer_cast(kvStore); + mockKvStoreFlowControl(true); + std::string key = "aa"; + EXPECT_EQ(false, distributedPreferencesDatabase.DeleteToDistributedDB(key)); +} + +/** + * @tc.name : DistributedPreferencesDatabase_01700 + * @tc.number : DistributedPreferencesDatabase_01700 + * @tc.desc : test ClearDatabase and KvManagerFlowControl is false. + */ +HWTEST_F(DistributedPreferencesDatabaseTest, DistributedPreferencesDatabase_01700, Function | SmallTest | Level1) +{ + DistributedPreferencesDatabase distributedPreferencesDatabase; + mockKvManagerFlowControl(false); + EXPECT_EQ(false, distributedPreferencesDatabase.ClearDatabase()); +} + +/** + * @tc.name : DistributedPreferencesDatabase_01800 + * @tc.number : DistributedPreferencesDatabase_01800 + * @tc.desc : test ClearDatabase and status != DistributedKv::Status::SUCCESS. + */ +HWTEST_F(DistributedPreferencesDatabaseTest, DistributedPreferencesDatabase_01800, Function | SmallTest | Level1) +{ + DistributedPreferencesDatabase distributedPreferencesDatabase; + mockKvManagerFlowControl(true); + mockCloseKvStore(false); + EXPECT_EQ(false, distributedPreferencesDatabase.ClearDatabase()); +} + +/** + * @tc.name : DistributedPreferencesDatabase_01900 + * @tc.number : DistributedPreferencesDatabase_01900 + * @tc.desc : test ClearDatabase and status != DistributedKv::Status::SUCCESS. + */ +HWTEST_F(DistributedPreferencesDatabaseTest, DistributedPreferencesDatabase_01900, Function | SmallTest | Level1) +{ + DistributedPreferencesDatabase distributedPreferencesDatabase; + mockKvManagerFlowControl(true); + mockCloseKvStore(true); + EXPECT_EQ(false, distributedPreferencesDatabase.ClearDatabase()); +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/services/distributed/test/unittest/distributed_preferences_database_test/mock_distributed_flow_control.cpp b/services/distributed/test/unittest/distributed_preferences_database_test/mock_distributed_flow_control.cpp new file mode 100755 index 0000000000000000000000000000000000000000..7594a0db5c6b3eec49b7c30f477807be7e7e8a4a --- /dev/null +++ b/services/distributed/test/unittest/distributed_preferences_database_test/mock_distributed_flow_control.cpp @@ -0,0 +1,54 @@ +/* + * 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_flow_control.h" + +namespace { + bool g_mockKvStoreFlowControlRet = true; + bool g_mockKvManagerFlowControlRet = true; +} + +void mockKvStoreFlowControl(bool mockRet) +{ + g_mockKvStoreFlowControlRet = mockRet; +} + +void mockKvManagerFlowControl(bool mockRet) +{ + g_mockKvManagerFlowControlRet = mockRet; +} + +namespace OHOS { +namespace Notification { +DistributedFlowControl::DistributedFlowControl( + size_t kvManagerSecondMaxinum, size_t kvManagerMinuteMaxinum, size_t kvStoreSecondMaxinum, + size_t kvStoreMinuteMaxinum) + : kvManagerSecondMaxinum_(kvManagerSecondMaxinum), + kvManagerMinuteMaxinum_(kvManagerMinuteMaxinum), + kvStoreSecondMaxinum_(kvStoreSecondMaxinum), + kvStoreMinuteMaxinum_(kvStoreMinuteMaxinum) +{} + +bool DistributedFlowControl::KvStoreFlowControl(void) +{ + return g_mockKvStoreFlowControlRet; +} + +bool DistributedFlowControl::KvManagerFlowControl(void) +{ + return g_mockKvManagerFlowControlRet; +} +} // namespace Notification +} // namespace OHOS diff --git a/services/distributed/test/unittest/distributed_preferences_database_test/mock_distributed_kv_data_manager.cpp b/services/distributed/test/unittest/distributed_preferences_database_test/mock_distributed_kv_data_manager.cpp new file mode 100755 index 0000000000000000000000000000000000000000..20ed102eef817bd572741c2fff3bc9e91ca51824 --- /dev/null +++ b/services/distributed/test/unittest/distributed_preferences_database_test/mock_distributed_kv_data_manager.cpp @@ -0,0 +1,53 @@ +/* + * 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_kv_data_manager.h" + +namespace { + bool g_mockCloseKvStoreRet = true; +} + +void mockCloseKvStore(bool mockRet) +{ + g_mockCloseKvStoreRet = mockRet; +} + +namespace OHOS { +namespace DistributedKv { +DistributedKvDataManager::DistributedKvDataManager() +{} + +DistributedKvDataManager::~DistributedKvDataManager() +{} +Status DistributedKvDataManager::GetSingleKvStore(const Options &options, const AppId &appId, const StoreId &storeId, + std::shared_ptr &singleKvStore) +{ + return Status::INVALID_ARGUMENT; +} + +Status DistributedKvDataManager::CloseKvStore(const AppId &appId, const StoreId &storeId) +{ + if (false == g_mockCloseKvStoreRet) { + return Status::INVALID_ARGUMENT; + } + return Status::SUCCESS; +} + +Status DistributedKvDataManager::DeleteKvStore(const AppId &appId, const StoreId &storeId, const std::string &path) +{ + return Status::INVALID_ARGUMENT; +} +} // namespace DistributedKv +} // namespace OHOS \ No newline at end of file diff --git a/services/distributed/test/unittest/distributed_preferences_database_test/mock_single_kv_store.cpp b/services/distributed/test/unittest/distributed_preferences_database_test/mock_single_kv_store.cpp new file mode 100755 index 0000000000000000000000000000000000000000..1ee38da68cd5acf743c002c74ae69a4608f01c3f --- /dev/null +++ b/services/distributed/test/unittest/distributed_preferences_database_test/mock_single_kv_store.cpp @@ -0,0 +1,200 @@ +/* + * 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 "mock_single_kv_store.h" +#include "types.h" + +namespace { + bool g_mockGetRet = true; +} + +void mockGet(bool mockRet) +{ + g_mockGetRet = mockRet; +} + +namespace OHOS { +namespace DistributedKv { +Status MockSingleKvStore::GetEntries(const Key &prefixKey, std::vector &entries) const +{ + return Status::INVALID_ARGUMENT; +} + +Status MockSingleKvStore::GetEntries(const DataQuery &query, std::vector &entries) const +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::GetResultSet(const Key &prefixKey, std::shared_ptr &resultSet) const +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::GetResultSet( + const DataQuery &query, std::shared_ptr &resultSet) const +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::CloseResultSet(std::shared_ptr &resultSet) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::GetCount(const DataQuery &query, int &result) const +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::Sync(const std::vector &deviceIds, SyncMode mode, uint32_t allowedDelayMs) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::RemoveDeviceData(const std::string &device) +{ + return Status::SUCCESS; +} + +StoreId MockSingleKvStore::GetStoreId() const +{ + StoreId storeId; + storeId.storeId = ""; + return storeId; +} + +Status MockSingleKvStore::Delete(const Key &key) +{ + return Status::INVALID_ARGUMENT; +} + +Status MockSingleKvStore::Put(const Key &key, const Value &value) +{ + return Status::INVALID_ARGUMENT; +} + +Status MockSingleKvStore::Get(const Key &key, Value &value) +{ + if (false == g_mockGetRet) { + return Status::SUCCESS; + } + return Status::INVALID_ARGUMENT; +} + +Status MockSingleKvStore::SubscribeKvStore(SubscribeType subscribeType, std::shared_ptr observer) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::UnSubscribeKvStore(SubscribeType subscribeType, std::shared_ptr observer) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::RegisterSyncCallback(std::shared_ptr callback) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::UnRegisterSyncCallback() +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::PutBatch(const std::vector &entries) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::DeleteBatch(const std::vector &keys) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::StartTransaction() +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::Commit() +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::Rollback() +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::SetSyncParam(const KvSyncParam &syncParam) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::GetSyncParam(KvSyncParam &syncParam) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::SetCapabilityEnabled(bool enabled) const +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::SetCapabilityRange( + const std::vector &localLabels, const std::vector &remoteSupportLabels) const +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::GetSecurityLevel(SecurityLevel &securityLevel) const +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::Sync(const std::vector &deviceIds, SyncMode mode, + const DataQuery &query, std::shared_ptr syncCallback) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::SubscribeWithQuery(const std::vector &deviceIds, const DataQuery &query) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::UnsubscribeWithQuery(const std::vector &deviceIds, const DataQuery &query) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::Backup(const std::string &file, const std::string &baseDir) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::Restore(const std::string &file, const std::string &baseDir) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::DeleteBackup( + const std::vector &files, const std::string &baseDir, + std::map &status) +{ + return Status::SUCCESS; +} +} // namespace DistributedKv +} // namespace OHOS diff --git a/services/distributed/test/unittest/distributed_screen_status_manager_branch_test/distributed_screen_status_manager_branch_test.cpp b/services/distributed/test/unittest/distributed_screen_status_manager_branch_test/distributed_screen_status_manager_branch_test.cpp new file mode 100755 index 0000000000000000000000000000000000000000..828dcf37f8a354c2d476012fda1517ed87e5adee --- /dev/null +++ b/services/distributed/test/unittest/distributed_screen_status_manager_branch_test/distributed_screen_status_manager_branch_test.cpp @@ -0,0 +1,376 @@ +/* + * 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_screen_status_manager.h" + +#include "ans_inner_errors.h" +#include "mock_single_kv_store.h" + +extern void mockGetDeviceList(bool mockRet); +extern void mockStartWatchDeviceChange(bool mockRet); +extern void mockKvManagerFlowControl(bool mockRet); +extern void mockKvStoreFlowControl(bool mockRet); +extern void mockGetEntries(bool mockRet); +extern void mockGetLocalDevice(bool mockRet); + +using namespace testing::ext; +using namespace OHOS::DistributedKv; +namespace OHOS { +namespace Notification { +class DistributedScreenStatusManagerBranchTest : public testing::Test { +public: + void SetUp() override; + void TearDown() override; +}; + +void DistributedScreenStatusManagerBranchTest::SetUp() +{} + +void DistributedScreenStatusManagerBranchTest::TearDown() +{} + +/** + * @tc.name : DistributedScreen_0100 + * @tc.number : DistributedScreen_0100 + * @tc.desc : Test CheckKvDataManager function and kvDataManager_ == nullptr. + */ +HWTEST_F(DistributedScreenStatusManagerBranchTest, DistributedScreen_0100, Function | SmallTest | Level1) +{ + DistributedScreenStatusManager distributedScreenStatusManager; + distributedScreenStatusManager.kvDataManager_ = nullptr; + mockStartWatchDeviceChange(false); + EXPECT_EQ(false, distributedScreenStatusManager.CheckKvDataManager()); +} + +/** + * @tc.name : DistributedScreen_0200 + * @tc.number : DistributedScreen_0200 + * @tc.desc : Test OnDeviceDisconnected function and status != DistributedKv::Status::SUCCESS. + */ +HWTEST_F(DistributedScreenStatusManagerBranchTest, DistributedScreen_0200, Function | SmallTest | Level1) +{ + DistributedScreenStatusManager distributedScreenStatusManager; + mockGetDeviceList(false); + mockStartWatchDeviceChange(true); + std::string deviceId = "aa"; + distributedScreenStatusManager.OnDeviceDisconnected(deviceId); +} + +/** + * @tc.name : DistributedScreen_0300 + * @tc.number : DistributedScreen_0300 + * @tc.desc : Test OnDeviceDisconnected function and devInfoList is empty. + */ +HWTEST_F(DistributedScreenStatusManagerBranchTest, DistributedScreen_0300, Function | SmallTest | Level1) +{ + DistributedScreenStatusManager distributedScreenStatusManager; + mockGetDeviceList(true); + mockStartWatchDeviceChange(true); + std::string deviceId = "aa"; + distributedScreenStatusManager.OnDeviceDisconnected(deviceId); +} + +/** + * @tc.name : DistributedScreen_0400 + * @tc.number : DistributedScreen_0400 + * @tc.desc : Test CheckKvStore function and kvStore_ == nullptr. + */ +HWTEST_F(DistributedScreenStatusManagerBranchTest, DistributedScreen_0400, Function | SmallTest | Level1) +{ + DistributedScreenStatusManager distributedScreenStatusManager; + distributedScreenStatusManager.kvStore_ = nullptr; + EXPECT_EQ(false, distributedScreenStatusManager.CheckKvStore()); +} + +/** + * @tc.name : DistributedScreen_0500 + * @tc.number : DistributedScreen_0500 + * @tc.desc : Test CheckKvStore function and kvStore_ != nullptr. + */ +HWTEST_F(DistributedScreenStatusManagerBranchTest, DistributedScreen_0500, Function | SmallTest | Level1) +{ + DistributedScreenStatusManager distributedScreenStatusManager; + std::shared_ptr kvStore = std::make_shared(); + distributedScreenStatusManager.kvStore_ = std::static_pointer_cast(kvStore); + EXPECT_EQ(true, distributedScreenStatusManager.CheckKvStore()); +} + +/** + * @tc.name : DistributedScreen_0600 + * @tc.number : DistributedScreen_0600 + * @tc.desc : Test OnDeviceDisconnected function and CheckKvStore is false. + */ +HWTEST_F(DistributedScreenStatusManagerBranchTest, DistributedScreen_0600, Function | SmallTest | Level1) +{ + DistributedScreenStatusManager distributedScreenStatusManager; + mockGetDeviceList(true); + mockStartWatchDeviceChange(true); + distributedScreenStatusManager.kvStore_ = nullptr; + std::string deviceId = "aa"; + distributedScreenStatusManager.OnDeviceDisconnected(deviceId); +} + +/** + * @tc.name : DistributedScreen_0700 + * @tc.number : DistributedScreen_0700 + * @tc.desc : Test GetKvDataManager function and status != DistributedKv::Status::SUCCESS. + */ +HWTEST_F(DistributedScreenStatusManagerBranchTest, DistributedScreen_0700, Function | SmallTest | Level1) +{ + DistributedScreenStatusManager distributedScreenStatusManager; + mockStartWatchDeviceChange(false); + distributedScreenStatusManager.GetKvDataManager(); +} + +/** + * @tc.name : DistributedScreen_0800 + * @tc.number : DistributedScreen_0800 + * @tc.desc : Test GetKvStore function and CheckKvDataManager is false. + */ +HWTEST_F(DistributedScreenStatusManagerBranchTest, DistributedScreen_0800, Function | SmallTest | Level1) +{ + DistributedScreenStatusManager distributedScreenStatusManager; + mockStartWatchDeviceChange(false); + distributedScreenStatusManager.GetKvStore(); +} + +/** + * @tc.name : DistributedScreen_0900 + * @tc.number : DistributedScreen_0900 + * @tc.desc : Test GetKvStore function and status != DistributedKv::Status::SUCCESS. + */ +HWTEST_F(DistributedScreenStatusManagerBranchTest, DistributedScreen_0900, Function | SmallTest | Level1) +{ + DistributedScreenStatusManager distributedScreenStatusManager; + mockStartWatchDeviceChange(true); + distributedScreenStatusManager.GetKvStore(); +} + +/** + * @tc.name : DistributedScreen_1000 + * @tc.number : DistributedScreen_1000 + * @tc.desc : Test OnDeviceDisconnected function and CheckKvDataManager is false. + */ +HWTEST_F(DistributedScreenStatusManagerBranchTest, DistributedScreen_1000, Function | SmallTest | Level1) +{ + DistributedScreenStatusManager distributedScreenStatusManager; + distributedScreenStatusManager.kvDataManager_ = nullptr; + mockStartWatchDeviceChange(false); + std::string deviceId = "aa"; + distributedScreenStatusManager.OnDeviceDisconnected(deviceId); +} + +/** + * @tc.name : DistributedScreen_1100 + * @tc.number : DistributedScreen_1100 + * @tc.desc : Test CheckRemoteDevicesIsUsing function and CheckKvDataManager is false and kvStore_ is nullptr. + */ +HWTEST_F(DistributedScreenStatusManagerBranchTest, DistributedScreen_1100, Function | SmallTest | Level1) +{ + DistributedScreenStatusManager distributedScreenStatusManager; + // set CheckKvDataManager is false + distributedScreenStatusManager.kvDataManager_ = nullptr; + mockStartWatchDeviceChange(false); + // set kvStore_ is nullptr + distributedScreenStatusManager.kvStore_ = nullptr; + bool isUsing = true; + EXPECT_EQ(ERR_ANS_DISTRIBUTED_OPERATION_FAILED, distributedScreenStatusManager.CheckRemoteDevicesIsUsing(isUsing)); +} + +/** + * @tc.name : DistributedScreen_1200 + * @tc.number : DistributedScreen_1200 + * @tc.desc : Test CheckRemoteDevicesIsUsing function and KvManagerFlowControl is false KvStoreFlowControl false. + */ +HWTEST_F(DistributedScreenStatusManagerBranchTest, DistributedScreen_1200, Function | SmallTest | Level1) +{ + DistributedScreenStatusManager distributedScreenStatusManager; + // set CheckKvDataManager is true + mockStartWatchDeviceChange(true); + // set kvStore_ is not nullptr + std::shared_ptr kvStore = std::make_shared(); + distributedScreenStatusManager.kvStore_ = std::static_pointer_cast(kvStore); + // set KvManagerFlowControl is false + mockKvManagerFlowControl(false); + // set KvStoreFlowControl is false + mockKvStoreFlowControl(false); + bool isUsing = true; + EXPECT_EQ(ERR_ANS_DISTRIBUTED_OPERATION_FAILED, distributedScreenStatusManager.CheckRemoteDevicesIsUsing(isUsing)); +} + +/** + * @tc.name : DistributedScreen_1300 + * @tc.number : DistributedScreen_1300 + * @tc.desc : Test CheckRemoteDevicesIsUsing function and KvManagerFlowControl is true KvStoreFlowControl true. + */ +HWTEST_F(DistributedScreenStatusManagerBranchTest, DistributedScreen_1300, Function | SmallTest | Level1) +{ + DistributedScreenStatusManager distributedScreenStatusManager; + // set CheckKvDataManager is true + mockStartWatchDeviceChange(true); + // set kvStore_ is not nullptr + std::shared_ptr kvStore = std::make_shared(); + distributedScreenStatusManager.kvStore_ = std::static_pointer_cast(kvStore); + // set KvManagerFlowControl is true + mockKvManagerFlowControl(true); + // set KvStoreFlowControl is true + mockKvStoreFlowControl(true); + // set status != DistributedKv::Status::SUCCESS + mockGetDeviceList(false); + bool isUsing = true; + EXPECT_EQ(ERR_ANS_DISTRIBUTED_GET_INFO_FAILED, distributedScreenStatusManager.CheckRemoteDevicesIsUsing(isUsing)); +} + +/** + * @tc.name : DistributedScreen_1400 + * @tc.number : DistributedScreen_1400 + * @tc.desc : Test CheckRemoteDevicesIsUsing function and status != DistributedKv::Status::SUCCESS. + */ +HWTEST_F(DistributedScreenStatusManagerBranchTest, DistributedScreen_1400, Function | SmallTest | Level1) +{ + DistributedScreenStatusManager distributedScreenStatusManager; + // set CheckKvDataManager is true + mockStartWatchDeviceChange(true); + // set kvStore_ is not nullptr + std::shared_ptr kvStore = std::make_shared(); + distributedScreenStatusManager.kvStore_ = std::static_pointer_cast(kvStore); + // set KvManagerFlowControl is true + mockKvManagerFlowControl(true); + // set KvStoreFlowControl is true + mockKvStoreFlowControl(true); + // set status == DistributedKv::Status::SUCCESS + mockGetDeviceList(true); + // set status != DistributedKv::Status::SUCCESS + mockGetEntries(false); + bool isUsing = true; + EXPECT_EQ(ERR_ANS_DISTRIBUTED_GET_INFO_FAILED, distributedScreenStatusManager.CheckRemoteDevicesIsUsing(isUsing)); +} + +/** + * @tc.name : DistributedScreen_1500 + * @tc.number : DistributedScreen_1500 + * @tc.desc : Test CheckRemoteDevicesIsUsing function and status == DistributedKv::Status::SUCCESS isUsing = true. + */ +HWTEST_F(DistributedScreenStatusManagerBranchTest, DistributedScreen_1500, Function | SmallTest | Level1) +{ + DistributedScreenStatusManager distributedScreenStatusManager; + // set CheckKvDataManager is true + mockStartWatchDeviceChange(true); + // set kvStore_ is not nullptr + std::shared_ptr kvStore = std::make_shared(); + distributedScreenStatusManager.kvStore_ = std::static_pointer_cast(kvStore); + // set KvManagerFlowControl is true + mockKvManagerFlowControl(true); + // set KvStoreFlowControl is true + mockKvStoreFlowControl(true); + // set status == DistributedKv::Status::SUCCESS + mockGetDeviceList(true); + // set status == DistributedKv::Status::SUCCESS + mockGetEntries(true); + bool isUsing = true; + EXPECT_EQ(ERR_OK, distributedScreenStatusManager.CheckRemoteDevicesIsUsing(isUsing)); +} + +/** + * @tc.name : DistributedScreen_1600 + * @tc.number : DistributedScreen_1600 + * @tc.desc : Test CheckRemoteDevicesIsUsing function and status == DistributedKv::Status::SUCCESS isUsing = false. + */ +HWTEST_F(DistributedScreenStatusManagerBranchTest, DistributedScreen_1600, Function | SmallTest | Level1) +{ + DistributedScreenStatusManager distributedScreenStatusManager; + // set CheckKvDataManager is true + mockStartWatchDeviceChange(true); + // set kvStore_ is not nullptr + std::shared_ptr kvStore = std::make_shared(); + distributedScreenStatusManager.kvStore_ = std::static_pointer_cast(kvStore); + // set KvManagerFlowControl is true + mockKvManagerFlowControl(true); + // set KvStoreFlowControl is true + mockKvStoreFlowControl(true); + // set status == DistributedKv::Status::SUCCESS + mockGetDeviceList(true); + // set status == DistributedKv::Status::SUCCESS + mockGetEntries(true); + bool isUsing = false; + EXPECT_EQ(ERR_OK, distributedScreenStatusManager.CheckRemoteDevicesIsUsing(isUsing)); +} + +/** + * @tc.name : DistributedScreen_1700 + * @tc.number : DistributedScreen_1700 + * @tc.desc : Test SetLocalScreenStatus function. + */ +HWTEST_F(DistributedScreenStatusManagerBranchTest, DistributedScreen_1700, Function | SmallTest | Level1) +{ + DistributedScreenStatusManager distributedScreenStatusManager; + // set kvStore_ is not nullptr + std::shared_ptr kvStore = std::make_shared(); + distributedScreenStatusManager.kvStore_ = std::static_pointer_cast(kvStore); + // set KvManagerFlowControl is false + mockKvManagerFlowControl(false); + // set KvStoreFlowControl is false + mockKvStoreFlowControl(false); + bool screenOn = true; + EXPECT_EQ(ERR_ANS_DISTRIBUTED_OPERATION_FAILED, distributedScreenStatusManager.SetLocalScreenStatus(screenOn)); +} + +/** + * @tc.name : DistributedScreen_1800 + * @tc.number : DistributedScreen_1800 + * @tc.desc : Test SetLocalScreenStatus function. + */ +HWTEST_F(DistributedScreenStatusManagerBranchTest, DistributedScreen_1800, Function | SmallTest | Level1) +{ + DistributedScreenStatusManager distributedScreenStatusManager; + // set kvStore_ is not nullptr + std::shared_ptr kvStore = std::make_shared(); + distributedScreenStatusManager.kvStore_ = std::static_pointer_cast(kvStore); + // set KvManagerFlowControl is true + mockKvManagerFlowControl(true); + // set KvStoreFlowControl is true + mockKvStoreFlowControl(true); + // set status != DistributedKv::Status::SUCCESS + mockGetLocalDevice(false); + bool screenOn = true; + EXPECT_EQ(ERR_ANS_DISTRIBUTED_GET_INFO_FAILED, distributedScreenStatusManager.SetLocalScreenStatus(screenOn)); +} + +/** + * @tc.name : DistributedScreen_1900 + * @tc.number : DistributedScreen_1900 + * @tc.desc : Test SetLocalScreenStatus function and kvStore_->Put is Status::INVALID_ARGUMENT. + */ +HWTEST_F(DistributedScreenStatusManagerBranchTest, DistributedScreen_1900, Function | SmallTest | Level1) +{ + DistributedScreenStatusManager distributedScreenStatusManager; + // set kvStore_ is not nullptr + std::shared_ptr kvStore = std::make_shared(); + distributedScreenStatusManager.kvStore_ = std::static_pointer_cast(kvStore); + // set KvManagerFlowControl is true + mockKvManagerFlowControl(true); + // set KvStoreFlowControl is true + mockKvStoreFlowControl(true); + // set status == DistributedKv::Status::SUCCESS + mockGetLocalDevice(true); + bool screenOn = true; + EXPECT_EQ(ERR_ANS_DISTRIBUTED_OPERATION_FAILED, distributedScreenStatusManager.SetLocalScreenStatus(screenOn)); +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/services/distributed/test/unittest/distributed_screen_status_manager_branch_test/mock_distributed_flow_control.cpp b/services/distributed/test/unittest/distributed_screen_status_manager_branch_test/mock_distributed_flow_control.cpp new file mode 100755 index 0000000000000000000000000000000000000000..049fc79bc302c356877c91bc12ca97741c565cf3 --- /dev/null +++ b/services/distributed/test/unittest/distributed_screen_status_manager_branch_test/mock_distributed_flow_control.cpp @@ -0,0 +1,54 @@ +/* + * 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_flow_control.h" + +namespace { + bool g_mockKvManagerFlowControlRet = true; + bool g_mockKvStoreFlowControlRet = true; +} + +void mockKvManagerFlowControl(bool mockRet) +{ + g_mockKvManagerFlowControlRet = mockRet; +} + +void mockKvStoreFlowControl(bool mockRet) +{ + g_mockKvStoreFlowControlRet = mockRet; +} + +namespace OHOS { +namespace Notification { +DistributedFlowControl::DistributedFlowControl( + size_t kvManagerSecondMaxinum, size_t kvManagerMinuteMaxinum, size_t kvStoreSecondMaxinum, + size_t kvStoreMinuteMaxinum) + : kvManagerSecondMaxinum_(kvManagerSecondMaxinum), + kvManagerMinuteMaxinum_(kvManagerMinuteMaxinum), + kvStoreSecondMaxinum_(kvStoreSecondMaxinum), + kvStoreMinuteMaxinum_(kvStoreMinuteMaxinum) +{} + +bool DistributedFlowControl::KvManagerFlowControl(void) +{ + return g_mockKvManagerFlowControlRet; +} + +bool DistributedFlowControl::KvStoreFlowControl(void) +{ + return g_mockKvStoreFlowControlRet; +} +} // namespace Notification +} // namespace OHOS diff --git a/services/distributed/test/unittest/distributed_screen_status_manager_branch_test/mock_distributed_kv_data_manager.cpp b/services/distributed/test/unittest/distributed_screen_status_manager_branch_test/mock_distributed_kv_data_manager.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d32b00bb923dfdfbad8d634af05d6ecfc7de2f36 --- /dev/null +++ b/services/distributed/test/unittest/distributed_screen_status_manager_branch_test/mock_distributed_kv_data_manager.cpp @@ -0,0 +1,79 @@ +/* + * 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_kv_data_manager.h" + +#include "mock_single_kv_store.h" + +namespace { + bool g_mockGetDeviceListRet = true; + bool g_mockStartWatchDeviceChangeRet = true; + bool g_mockGetLocalDeviceRet = true; +} + +void mockGetDeviceList(bool mockRet) +{ + g_mockGetDeviceListRet = mockRet; +} + +void mockStartWatchDeviceChange(bool mockRet) +{ + g_mockStartWatchDeviceChangeRet = mockRet; +} + +void mockGetLocalDevice(bool mockRet) +{ + g_mockGetLocalDeviceRet = mockRet; +} + +namespace OHOS { +namespace DistributedKv { +DistributedKvDataManager::DistributedKvDataManager() +{} + +DistributedKvDataManager::~DistributedKvDataManager() +{} + +Status DistributedKvDataManager::GetDeviceList(std::vector &deviceInfoList, DeviceFilterStrategy strategy) +{ + if (false == g_mockGetDeviceListRet) { + return Status::INVALID_ARGUMENT; + } + return Status::SUCCESS; +} + +Status DistributedKvDataManager::GetSingleKvStore(const Options &options, const AppId &appId, const StoreId &storeId, + std::shared_ptr &singleKvStore) +{ + return Status::INVALID_ARGUMENT; +} + +Status DistributedKvDataManager::StartWatchDeviceChange(std::shared_ptr observer) +{ + if (false == g_mockStartWatchDeviceChangeRet) { + return Status::INVALID_ARGUMENT; + } + return Status::SUCCESS; +} + +Status DistributedKvDataManager::GetLocalDevice(DeviceInfo &localDevice) +{ + if (false == g_mockGetLocalDeviceRet) { + return Status::INVALID_ARGUMENT; + } + return Status::SUCCESS; +} +} // namespace DistributedKv +} // namespace OHOS \ No newline at end of file diff --git a/services/distributed/test/unittest/distributed_screen_status_manager_branch_test/mock_single_kv_store.cpp b/services/distributed/test/unittest/distributed_screen_status_manager_branch_test/mock_single_kv_store.cpp new file mode 100755 index 0000000000000000000000000000000000000000..5a54ce10995f5afafe7d403f5457def1d0183afa --- /dev/null +++ b/services/distributed/test/unittest/distributed_screen_status_manager_branch_test/mock_single_kv_store.cpp @@ -0,0 +1,201 @@ +/* + * 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 "mock_single_kv_store.h" +#include "types.h" + +namespace { + bool g_mockGetEntriesRet = true; +} + +void mockGetEntries(bool mockRet) { + g_mockGetEntriesRet = mockRet; +} + +namespace OHOS { +namespace DistributedKv { +Status MockSingleKvStore::GetEntries(const Key &prefixKey, std::vector &entries) const +{ + Entry entry; + entries.emplace_back(entry); + if (false == g_mockGetEntriesRet) { + return Status::INVALID_ARGUMENT; + } + return Status::SUCCESS; +} + +Status MockSingleKvStore::GetEntries(const DataQuery &query, std::vector &entries) const +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::GetResultSet(const Key &prefixKey, std::shared_ptr &resultSet) const +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::GetResultSet( + const DataQuery &query, std::shared_ptr &resultSet) const +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::CloseResultSet(std::shared_ptr &resultSet) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::GetCount(const DataQuery &query, int &result) const +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::Sync(const std::vector &deviceIds, SyncMode mode, uint32_t allowedDelayMs) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::RemoveDeviceData(const std::string &device) +{ + return Status::SUCCESS; +} + +StoreId MockSingleKvStore::GetStoreId() const +{ + StoreId storeId; + storeId.storeId = ""; + return storeId; +} + +Status MockSingleKvStore::Delete(const Key &key) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::Put(const Key &key, const Value &value) +{ + return Status::INVALID_ARGUMENT; +} + +Status MockSingleKvStore::Get(const Key &key, Value &value) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::SubscribeKvStore(SubscribeType subscribeType, std::shared_ptr observer) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::UnSubscribeKvStore(SubscribeType subscribeType, std::shared_ptr observer) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::RegisterSyncCallback(std::shared_ptr callback) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::UnRegisterSyncCallback() +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::PutBatch(const std::vector &entries) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::DeleteBatch(const std::vector &keys) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::StartTransaction() +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::Commit() +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::Rollback() +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::SetSyncParam(const KvSyncParam &syncParam) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::GetSyncParam(KvSyncParam &syncParam) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::SetCapabilityEnabled(bool enabled) const +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::SetCapabilityRange( + const std::vector &localLabels, const std::vector &remoteSupportLabels) const +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::GetSecurityLevel(SecurityLevel &securityLevel) const +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::Sync(const std::vector &deviceIds, SyncMode mode, + const DataQuery &query, std::shared_ptr syncCallback) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::SubscribeWithQuery(const std::vector &deviceIds, const DataQuery &query) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::UnsubscribeWithQuery(const std::vector &deviceIds, const DataQuery &query) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::Backup(const std::string &file, const std::string &baseDir) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::Restore(const std::string &file, const std::string &baseDir) +{ + return Status::SUCCESS; +} + +Status MockSingleKvStore::DeleteBackup( + const std::vector &files, const std::string &baseDir, + std::map &status) +{ + return Status::SUCCESS; +} +} // namespace DistributedKv +} // namespace OHOS