From 90680097797a6aea2e33ab83d487047cb005c135 Mon Sep 17 00:00:00 2001 From: sunjiakun Date: Thu, 14 Nov 2024 20:26:03 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=8B=AC=E7=AB=8B=E7=BC=96?= =?UTF-8?q?=E8=AF=91=E5=A4=B4=E6=96=87=E4=BB=B6=E5=BC=95=E7=94=A8=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: sunjiakun --- .../dbms_services_kit_test.cpp | 8 ++-- .../dbms_services_kit_test/mock_scope_guard.h | 46 +++++++++++++++++++ 2 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 services/dbms/test/unittest/dbms_services_kit_test/mock_scope_guard.h diff --git a/services/dbms/test/unittest/dbms_services_kit_test/dbms_services_kit_test.cpp b/services/dbms/test/unittest/dbms_services_kit_test/dbms_services_kit_test.cpp index 894f828..2b71ce8 100644 --- a/services/dbms/test/unittest/dbms_services_kit_test/dbms_services_kit_test.cpp +++ b/services/dbms/test/unittest/dbms_services_kit_test/dbms_services_kit_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -41,9 +41,9 @@ #endif #include "iservice_registry.h" #include "json_util.h" +#include "mock_scope_guard.h" #include "nativetoken_kit.h" #include "token_setproc.h" -#include "scope_guard.h" #include "service_control.h" #include "softbus_common.h" #include "status_receiver_host.h" @@ -238,7 +238,7 @@ sptr DbmsServicesKitTest::GetInstallerProxy() bool DbmsServicesKitTest::InstallBundle(const std::string &bundlePath) const { setuid(Constants::FOUNDATION_UID); - ScopeGuard uidGuard([&] { setuid(Constants::ROOT_UID); }); + MockScopeGuard uidGuard(std::bind(setuid, Constants::ROOT_UID)); sptr installerProxy = GetInstallerProxy(); if (!installerProxy) { APP_LOGE("get bundle installer failed."); @@ -257,7 +257,7 @@ bool DbmsServicesKitTest::InstallBundle(const std::string &bundlePath) const bool DbmsServicesKitTest::UninstallBundle(const std::string &bundleName) const { setuid(Constants::FOUNDATION_UID); - ScopeGuard uidGuard([&] { setuid(Constants::ROOT_UID); }); + MockScopeGuard uidGuard(std::bind(setuid, Constants::ROOT_UID)); sptr installerProxy = GetInstallerProxy(); if (!installerProxy) { APP_LOGE("get bundle installer failed."); diff --git a/services/dbms/test/unittest/dbms_services_kit_test/mock_scope_guard.h b/services/dbms/test/unittest/dbms_services_kit_test/mock_scope_guard.h new file mode 100644 index 0000000..d499e55 --- /dev/null +++ b/services/dbms/test/unittest/dbms_services_kit_test/mock_scope_guard.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef FOUNDATION_APPEXECFWK_MOCK_BUNDLEMGR_INCLUDE_SCOPE_GUARD +#define FOUNDATION_APPEXECFWK_MOCK_BUNDLEMGR_INCLUDE_SCOPE_GUARD + +#include + +namespace OHOS { +namespace AppExecFwk { +class MockScopeGuard final { +public: + using Function = std::function; + explicit MockScopeGuard(Function fn) : fn_(fn), dismissed_(false) + {} + + ~MockScopeGuard() + { + if (!dismissed_) { + fn_(); + } + } + + void Dismiss() + { + dismissed_ = true; + } + +private: + Function fn_; + bool dismissed_; +}; +} +} +#endif // FOUNDATION_APPEXECFWK_MOCK_BUNDLEMGR_INCLUDE_SCOPE_GUARD \ No newline at end of file -- Gitee