From 2fb15e5d92ba78b31b09ebc87ddae0a89701117c Mon Sep 17 00:00:00 2001 From: wangbin Date: Wed, 26 Mar 2025 16:22:20 +0800 Subject: [PATCH] add new test cases for coverage Signed-off-by: wangbin --- .../service/test/BUILD.gn | 41 +++++ .../service/test/kvdb_service_test.cpp | 122 ++++++++++++++ .../test/mock/screenlock_manager_mock.cpp | 25 +++ .../service/test/mock/want_mock.cpp | 36 +++++ .../service/test/mock/want_mock.h | 38 +++++ .../service/test/screen_lock_mock_test.cpp | 153 ++++++++++++++++++ .../service/test/user_delegate_mock_test.cpp | 14 +- 7 files changed, 423 insertions(+), 6 deletions(-) create mode 100644 services/distributeddataservice/service/test/mock/screenlock_manager_mock.cpp create mode 100644 services/distributeddataservice/service/test/mock/want_mock.cpp create mode 100644 services/distributeddataservice/service/test/mock/want_mock.h create mode 100644 services/distributeddataservice/service/test/screen_lock_mock_test.cpp diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index c4d836733..2269f90da 100755 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -1876,6 +1876,46 @@ ohos_unittest("BackupManagerServiceTest") { ] } +ohos_unittest("ScreenLockMockTest") { + module_out_path = module_output_path + + include_dirs = [ + "${data_service_path}/adapter/include/screenlock", + "${data_service_path}/framework/include", + "${data_service_path}/framework/include/account", + "${data_service_path}/service/test/mock", + ] + + sources = [ + "${data_service_path}/adapter/screenlock/src/screen_lock.cpp", + "mock/account_delegate_mock.cpp", + "mock/screenlock_manager_mock.cpp", + "mock/want_mock.cpp", + "screen_lock_mock_test.cpp", + ] + + deps = [ "${data_service_path}/framework:distributeddatasvcfwk" ] + + external_deps = [ + "ability_base:want", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "googletest:gmock_main", + "googletest:gtest_main", + "hilog:libhilog", + "kv_store:datamgr_common", + "screenlock_mgr:screenlock_client", + ] + + cflags = [ + "-Dprivate=public", + "-Dprotected=public", + ] + + cflags_cc = cflags + defines = [ "OPENSSL_SUPPRESS_DEPRECATED" ] +} + ############################################################################### group("unittest") { testonly = true @@ -1952,6 +1992,7 @@ group("unittest") { ":PermissionValidatorMockTest", ":PermissionValidatorTest", ":PermitDelegateMockTest", + ":ScreenLockMockTest", ":ValueProxyServiceTest", ] } diff --git a/services/distributeddataservice/service/test/kvdb_service_test.cpp b/services/distributeddataservice/service/test/kvdb_service_test.cpp index 80990d179..ae3b38fcf 100644 --- a/services/distributeddataservice/service/test/kvdb_service_test.cpp +++ b/services/distributeddataservice/service/test/kvdb_service_test.cpp @@ -45,6 +45,7 @@ using namespace DistributedDB; using namespace OHOS; using namespace OHOS::DistributedData; using namespace OHOS::Security::AccessToken; +using namespace OHOS::DistributedKv; using StoreMetaData = OHOS::DistributedData::StoreMetaData; using DBPassword = DistributedDB::CipherPassword; using DBStatus = DistributedDB::DBStatus; @@ -111,9 +112,20 @@ public: static void TearDownTestCase(void){}; void SetUp(){}; void TearDown(){}; + static constexpr int currentDelay = 500; + static constexpr int scheduleDelay = 1000; + static constexpr size_t threadMaxNum = 5; + static constexpr size_t threadMinNum = 2; protected: }; +namespace { +Status MyOwnSyncFunc(const KvStoreSyncManager::SyncEnd &syncEnd) +{ + return Status::SUCCESS; +} +} + class KVDBWatcherTest : public testing::Test { public: static void SetUpTestCase(void){}; @@ -306,6 +318,22 @@ HWTEST_F(KvStoreSyncManagerTest, AddSyncOperation, TestSize.Level0) EXPECT_EQ(kvStatus, Status::INVALID_ARGUMENT); } +/** +* @tc.name: AddSyncOperation001 +* @tc.desc: test add SyncOperation. +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(KvStoreSyncManagerTest, AddSyncOperation001, TestSize.Level1) +{ + DistributedKv::KvStoreSyncManager syncManager; + uintptr_t syncId = 1; + uint32_t delayMs = 0; + KvStoreSyncManager::SyncEnd syncEnd = nullptr; + Status stus = syncManager.AddSyncOperation(syncId, delayMs, MyOwnSyncFunc, syncEnd); + EXPECT_TRUE(stus == Status::SUCCESS); +} + /** * @tc.name: RemoveSyncOperation * @tc.desc: RemoveSyncOperation test the return result of input with different values. @@ -320,6 +348,42 @@ HWTEST_F(KvStoreSyncManagerTest, RemoveSyncOperation, TestSize.Level0) EXPECT_EQ(kvStatus, Status::ERROR); } +/** +* @tc.name: RemoveSyncOperation001 +* @tc.desc: test remove SyncOperation. +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(KvStoreSyncManagerTest, RemoveSyncOperation001, TestSize.Level1) +{ + KvStoreSyncManager::KvSyncOperation oper1 = { + .syncId = 1, + .opSeq = 0, + .delayMs = 0, + .syncFunc = nullptr, + .syncEnd = nullptr + }; + + KvStoreSyncManager::KvSyncOperation oper2 = { + .syncId = 0, + .opSeq = 0, + .delayMs = 0, + .syncFunc = nullptr, + .syncEnd = nullptr + }; + DistributedKv::KvStoreSyncManager syncManager; + syncManager.realtimeSyncingOps_.emplace_back(oper1); + syncManager.delaySyncingOps_.emplace_back(oper1); + KvStoreSyncManager::TimePoint currentTime = std::chrono::steady_clock::now(); + syncManager.scheduleSyncOps_.emplace(currentTime, oper2); + Status stus = syncManager.RemoveSyncOperation(0); + EXPECT_TRUE(stus == Status::SUCCESS); + + syncManager.scheduleSyncOps_.emplace(currentTime, oper1); + stus = syncManager.RemoveSyncOperation(0); + EXPECT_TRUE(stus == Status::ERROR); +} + /** * @tc.name: DoRemoveSyncingOp * @tc.desc: DoRemoveSyncingOp test the return result of input with different values. @@ -355,6 +419,64 @@ HWTEST_F(KvStoreSyncManagerTest, GetTimeoutSyncOps, TestSize.Level0) EXPECT_EQ(kvStatus, true); } +/** +* @tc.name: GetTimeoutSyncOps001 +* @tc.desc: test get timeout SyncOps. +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(KvStoreSyncManagerTest, GetTimeoutSyncOps001, TestSize.Level1) +{ + KvStoreSyncManager::KvSyncOperation operation = { + .syncId = 1, + .opSeq = 0, + .delayMs = 0, + .syncFunc = nullptr, + .syncEnd = nullptr, + .beginTime = std::chrono::steady_clock::now() + }; + DistributedKv::KvStoreSyncManager syncManager; + syncManager.realtimeSyncingOps_.emplace_back(operation); + auto start = std::chrono::steady_clock::now(); + auto expireTime = start + std::chrono::milliseconds(scheduleDelay); + syncManager.scheduleSyncOps_.emplace(expireTime, operation); + auto currentTime = start + std::chrono::milliseconds(KvStoreSyncManagerTest::currentDelay); + std::list syncOps; + syncOps.emplace_back(operation); + bool ret = syncManager.GetTimeoutSyncOps(currentTime, syncOps); + EXPECT_FALSE(ret); +} + +/** +* @tc.name: Schedule +* @tc.desc: test Schedule. +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(KvStoreSyncManagerTest, Schedule, TestSize.Level1) +{ + KvStoreSyncManager::KvSyncOperation syncOperation = { + .syncId = 1, + .opSeq = 0, + .delayMs = 0, + .syncFunc = &MyOwnSyncFunc, + .syncEnd = nullptr, + .beginTime = std::chrono::steady_clock::now() + }; + + DistributedKv::KvStoreSyncManager syncManager; + syncManager.realtimeSyncingOps_.clear(); + syncManager.delaySyncingOps_.clear(); + auto start = std::chrono::steady_clock::now(); + auto expireTime = start + std::chrono::milliseconds(scheduleDelay); + auto currentTime = start + std::chrono::milliseconds(currentDelay); + syncManager.scheduleSyncOps_.emplace(expireTime, syncOperation); + std::shared_ptr executors = std::make_shared(threadMaxNum, threadMinNum); + ASSERT_NE(executors, nullptr); + syncManager.SetThreadPool(executors); + EXPECT_NO_FATAL_FAILURE(syncManager.Schedule(currentTime)); +} + /** * @tc.name: KVDBWatcher * @tc.desc: KVDBWatcher test the return result of input with different values. diff --git a/services/distributeddataservice/service/test/mock/screenlock_manager_mock.cpp b/services/distributeddataservice/service/test/mock/screenlock_manager_mock.cpp new file mode 100644 index 000000000..e260565d8 --- /dev/null +++ b/services/distributeddataservice/service/test/mock/screenlock_manager_mock.cpp @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2025 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 "screenlock_manager.h" + +namespace OHOS { +namespace ScreenLock { +sptr ScreenLockManager::GetInstance() +{ + return nullptr; +} +} +} \ No newline at end of file diff --git a/services/distributeddataservice/service/test/mock/want_mock.cpp b/services/distributeddataservice/service/test/mock/want_mock.cpp new file mode 100644 index 000000000..6a7d59d56 --- /dev/null +++ b/services/distributeddataservice/service/test/mock/want_mock.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2025 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 "want_mock.h" + +namespace OHOS { +namespace AAFwk { +std::string Want::GetAction() const +{ + if (BWant::wantB == nullptr) { + return ""; + } + return BWant::wantB->GetAction(); +} + +int Want::GetIntParam(const std::string &key, const int defaultValue) const +{ + if (BWant::wantB == nullptr) { + return 0; + } + return BWant::wantB->GetIntParam(key, defaultValue); +} +} +} \ No newline at end of file diff --git a/services/distributeddataservice/service/test/mock/want_mock.h b/services/distributeddataservice/service/test/mock/want_mock.h new file mode 100644 index 000000000..822e4aeb3 --- /dev/null +++ b/services/distributeddataservice/service/test/mock/want_mock.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTEDDATA_SERVICE_TEST_WANT_MOCK_H +#define OHOS_DISTRIBUTEDDATA_SERVICE_TEST_WANT_MOCK_H +#include +#include + +namespace OHOS { +namespace AAFwk { +class BWant { +public: + virtual std::string GetAction() const = 0; + virtual int GetIntParam(const std::string &, const int) const = 0; + static inline std::shared_ptr wantB = nullptr; + virtual ~BWant() = default; +}; + +class WantMock : public BWant { +public: + MOCK_METHOD(std::string, GetAction, (), (const)); + MOCK_METHOD(int, GetIntParam, (const std::string &, const int), (const)); +}; +} +} +#endif // OHOS_DISTRIBUTEDDATA_SERVICE_TEST_WANT_MOCK_H \ No newline at end of file diff --git a/services/distributeddataservice/service/test/screen_lock_mock_test.cpp b/services/distributeddataservice/service/test/screen_lock_mock_test.cpp new file mode 100644 index 000000000..c919301a2 --- /dev/null +++ b/services/distributeddataservice/service/test/screen_lock_mock_test.cpp @@ -0,0 +1,153 @@ +/* +* Copyright (c) 2025 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 +#include +#include "screen_lock.h" +#include "want_mock.h" +#include "account_delegate_mock.h" + +using namespace testing::ext; +using namespace testing; +using namespace std; +using namespace OHOS::DistributedData; +using namespace OHOS::EventFwk; +using namespace OHOS::AAFwk; + +namespace OHOS::Test { +namespace DistributedDataTest { +namespace { +void SetCallback(int32_t user) +{ + return; +} +} + +class ScreenLockMockObserver : public ScreenManager::Observer { +public: + void OnScreenUnlocked(int32_t user) override + { + } + + std::string GetName() override + { + return name; + } + + void SetName(const std::string &name) + { + this->name = name; + } +private: + std::string name = "screenMockObserver"; +}; + +class ScreenLockMockTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp() {} + void TearDown() {} + static inline shared_ptr wantMock = nullptr; + static constexpr size_t threadMaxNum = 5; + static constexpr size_t threadMinNum = 2; +}; + +void ScreenLockMockTest::SetUpTestCase() +{ + wantMock = make_shared(); + BWant::wantB = wantMock; +} + +void ScreenLockMockTest::TearDownTestCase() +{ + BWant::wantB = nullptr; + wantMock = nullptr; +} + +/** +* @tc.name: IsLocked +* @tc.desc: test IsLocked abnormal branch. +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(ScreenLockMockTest, IsLocked, TestSize.Level1) +{ + ScreenLock screenLock; + bool ret = screenLock.IsLocked(); + EXPECT_TRUE(!ret); +} + +/** +* @tc.name: Subscribe +* @tc.desc: test Unsubscribe abnormal branch. +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(ScreenLockMockTest, Unsubscribe, TestSize.Level1) +{ + GTEST_LOG_(INFO)<<"ScreenLockMockTest Unsubscribe start"; + ScreenLock screenLock; + auto observer = std::make_shared(); + ASSERT_NE(observer, nullptr); + observer->SetName(""); + screenLock.Unsubscribe(observer); + observer->SetName("my_screenlocktest"); + EXPECT_NO_FATAL_FAILURE(screenLock.Unsubscribe(observer)); + screenLock.Subscribe(observer); + EXPECT_NO_FATAL_FAILURE(screenLock.Unsubscribe(observer)); + GTEST_LOG_(INFO)<<"ScreenLockMockTest Unsubscribe end"; +} + +/** +* @tc.name: OnReceiveEvent +* @tc.desc: test OnReceiveEvent. +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(ScreenLockMockTest, OnReceiveEvent, TestSize.Level1) +{ + GTEST_LOG_(INFO)<<"ScreenLockMockTest OnReceiveEvent start"; + Want want; + CommonEventData comEvtData(want); + ScreenLock screenLock; + std::string ret = CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED + "_Suffix"; + EXPECT_CALL(*wantMock, GetAction()).WillOnce(Return(ret)); + shared_ptr executors = make_shared(threadMaxNum, threadMinNum); + screenLock.BindExecutor(executors); + screenLock.SubscribeScreenEvent(); + ASSERT_NE(screenLock.eventSubscriber_, nullptr); + EXPECT_NO_FATAL_FAILURE(screenLock.eventSubscriber_->OnReceiveEvent(comEvtData)); + + ret = CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED; + std::vector users; + EXPECT_CALL(*wantMock, GetAction()).WillOnce(Return(ret)); + EXPECT_CALL(*wantMock, GetIntParam(_, _)).WillOnce(Return(EventSubscriber::INVALID_USER)); + EXPECT_CALL(AccountDelegateMock::Init(), QueryForegroundUsers(_)) + .WillOnce(DoAll(SetArgReferee<0>(users), Return(true))); + EXPECT_NO_FATAL_FAILURE(screenLock.eventSubscriber_->OnReceiveEvent(comEvtData)); + + users.push_back(1); + screenLock.eventSubscriber_->SetEventCallback(SetCallback); + EXPECT_CALL(*wantMock, GetAction()).WillOnce(Return(ret)); + EXPECT_CALL(*wantMock, GetIntParam(_, _)).WillOnce(Return(EventSubscriber::INVALID_USER)); + EXPECT_CALL(AccountDelegateMock::Init(), QueryForegroundUsers(_)) + .WillOnce(DoAll(SetArgReferee<0>(users), Return(true))); + EXPECT_NO_FATAL_FAILURE(screenLock.eventSubscriber_->OnReceiveEvent(comEvtData)); + GTEST_LOG_(INFO)<<"ScreenLockMockTest OnReceiveEvent end"; +} +} +} \ No newline at end of file diff --git a/services/distributeddataservice/service/test/user_delegate_mock_test.cpp b/services/distributeddataservice/service/test/user_delegate_mock_test.cpp index 373782b7e..4b1c84090 100644 --- a/services/distributeddataservice/service/test/user_delegate_mock_test.cpp +++ b/services/distributeddataservice/service/test/user_delegate_mock_test.cpp @@ -48,6 +48,8 @@ public: void SetUp() {} void TearDown() {} static inline shared_ptr devMgrAdapterMock = nullptr; + static constexpr size_t threadMaxNum = 5; + static constexpr size_t threadMinNum = 2; }; void UserDelegateMockTest::SetUpTestCase(void) @@ -68,7 +70,7 @@ void UserDelegateMockTest::TearDownTestCase(void) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(UserDelegateMockTest, GetLocalUserStatus, TestSize.Level0) +HWTEST_F(UserDelegateMockTest, GetLocalUserStatus, TestSize.Level1) { DeviceInfo devInfo = { .uuid = "AFAGA45WF3663FAGA" }; EXPECT_CALL(*devMgrAdapterMock, GetLocalDevice()).WillOnce(Return(devInfo)); @@ -82,7 +84,7 @@ HWTEST_F(UserDelegateMockTest, GetLocalUserStatus, TestSize.Level0) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(UserDelegateMockTest, InitLocalUserMeta, TestSize.Level0) +HWTEST_F(UserDelegateMockTest, InitLocalUserMeta, TestSize.Level1) { EXPECT_CALL(AccountDelegateMock::Init(), QueryUsers(_)).WillOnce(Return(false)); bool ret = UserDelegate::GetInstance().InitLocalUserMeta(); @@ -102,7 +104,7 @@ HWTEST_F(UserDelegateMockTest, InitLocalUserMeta, TestSize.Level0) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(UserDelegateMockTest, GetLocalUsers, TestSize.Level0) +HWTEST_F(UserDelegateMockTest, GetLocalUsers, TestSize.Level1) { DeviceInfo devInfo = { .uuid = "TESTGHJJ46785FAGA9323FGAGATEST" }; EXPECT_CALL(*devMgrAdapterMock, GetLocalDevice()).WillOnce(Return(devInfo)); @@ -119,7 +121,7 @@ HWTEST_F(UserDelegateMockTest, GetLocalUsers, TestSize.Level0) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(UserDelegateMockTest, GetUsers, TestSize.Level0) +HWTEST_F(UserDelegateMockTest, GetUsers, TestSize.Level1) { std::string deviceId = "45677afatghfrttWISKKM"; std::map val; @@ -135,12 +137,12 @@ HWTEST_F(UserDelegateMockTest, GetUsers, TestSize.Level0) * @tc.type: FUNC * @tc.require: */ -HWTEST_F(UserDelegateMockTest, Init, TestSize.Level0) +HWTEST_F(UserDelegateMockTest, Init, TestSize.Level1) { EXPECT_CALL(AccountDelegateMock::Init(), Subscribe(_)).WillRepeatedly(Return(0)); DeviceInfo devInfo = { .uuid = "HJJ4FGAGAAGA45WF3663FAGA" }; EXPECT_CALL(*devMgrAdapterMock, GetLocalDevice()).WillRepeatedly(Return(devInfo)); - std::shared_ptr poolPtr = std::make_shared(0, 1); + std::shared_ptr poolPtr = std::make_shared(threadMaxNum, threadMinNum); ASSERT_NE(poolPtr, nullptr); UserDelegate::GetInstance().executors_ = poolPtr; ASSERT_NE(UserDelegate::GetInstance().executors_, nullptr); -- Gitee