From 6edf033f9473b9b31ddc0244d0a034e1c282d9ab Mon Sep 17 00:00:00 2001 From: fangJinliang1 Date: Sun, 24 Apr 2022 22:20:07 +0800 Subject: [PATCH] add benchmark test case Signed-off-by: fangJinliang1 Change-Id: Iec57ff898654b29ab6825460a860db997e8fe5ca Signed-off-by: fangJinliang1 --- bundle.json | 3 +- .../BUILD.gn | 4 +- .../common_event_services_module_test.cpp | 4 +- .../BUILD.gn | 4 +- ...t_services_publish_ordered_module_test.cpp | 4 +- services/test/unittest/BUILD.gn | 18 +- .../test/unittest/common_event_dump_test.cpp | 1 + .../unittest/common_event_freeze_test.cpp | 1 + ..._event_publish_ordered_event_unit_test.cpp | 1 + ...ent_publish_permission_event_unit_test.cpp | 1 + ...common_event_publish_system_event_test.cpp | 1 + .../unittest/common_event_sticky_test.cpp | 1 + .../common_event_subscribe_unit_test.cpp | 1 + .../common_event_unsubscribe_unit_test.cpp | 1 + .../mock/include/mock_bundle_manager.h | 5 +- .../unittest/mock/src/mock_bundle_manager.cpp | 4 +- test/benchmarktest/BUILD.gn | 30 + .../common_event_publish_test/BUILD.gn | 50 + .../common_event_publish_test.cpp | 123 ++ .../common_event_service_test/BUILD.gn | 67 + .../common_event_manager_service_test.cpp | 223 +++ .../mock/include/mock_bundle_manager.h | 1294 ++++++++--------- test/mock/include/mock_ipc_skeleton.h | 61 + .../mock}/mock_access_token_helper.cpp | 71 +- .../src => test/mock}/mock_bundle_manager.cpp | 94 +- test/mock/mock_ipc.cpp | 39 + 26 files changed, 1303 insertions(+), 803 deletions(-) create mode 100644 test/benchmarktest/BUILD.gn create mode 100644 test/benchmarktest/common_event_publish_test/BUILD.gn create mode 100644 test/benchmarktest/common_event_publish_test/common_event_publish_test.cpp create mode 100644 test/benchmarktest/common_event_service_test/BUILD.gn create mode 100644 test/benchmarktest/common_event_service_test/common_event_manager_service_test.cpp rename {services/test => test}/mock/include/mock_bundle_manager.h (83%) create mode 100644 test/mock/include/mock_ipc_skeleton.h rename {services/test/unittest/mock/src => test/mock}/mock_access_token_helper.cpp (84%) rename {services/test/mock/src => test/mock}/mock_bundle_manager.cpp (90%) create mode 100644 test/mock/mock_ipc.cpp diff --git a/bundle.json b/bundle.json index 65fcd79..48e0fbc 100644 --- a/bundle.json +++ b/bundle.json @@ -131,7 +131,8 @@ "//base/notification/common_event_service/test/systemtest:systemtest", "//base/notification/common_event_service/tools:moduletest", "//base/notification/common_event_service/tools:systemtest", - "//base/notification/common_event_service/tools:unittest" + "//base/notification/common_event_service/tools:unittest", + "//base/notification/common_event_service/test/benchmarktest:benchmarktest" ] } } diff --git a/services/test/moduletest/common_event_services_module_test/BUILD.gn b/services/test/moduletest/common_event_services_module_test/BUILD.gn index 6cf7f44..8bfb886 100644 --- a/services/test/moduletest/common_event_services_module_test/BUILD.gn +++ b/services/test/moduletest/common_event_services_module_test/BUILD.gn @@ -20,7 +20,7 @@ ohos_moduletest("cesModuleTest") { module_out_path = module_output_path sources = [ - "${services_path}/test/mock/src/mock_bundle_manager.cpp", + "${common_event_service_path}/test/mock/mock_bundle_manager.cpp", "${services_path}/test/mock/src/mock_ipc.cpp", "common_event_services_module_test.cpp", ] @@ -43,7 +43,7 @@ ohos_moduletest("cesModuleTest") { "//foundation/distributedschedule/samgr/services/samgr/native/include", "//foundation/distributedschedule/samgr/utils/native/include", "${services_path}/include", - "${services_path}/test/mock/include", + "${common_event_service_path}/test/mock/include", "//third_party/json/include", "//utils/native/base/include", "//utils/system/safwk/native/include", diff --git a/services/test/moduletest/common_event_services_module_test/common_event_services_module_test.cpp b/services/test/moduletest/common_event_services_module_test/common_event_services_module_test.cpp index 782e7ac..5c06aec 100644 --- a/services/test/moduletest/common_event_services_module_test/common_event_services_module_test.cpp +++ b/services/test/moduletest/common_event_services_module_test/common_event_services_module_test.cpp @@ -40,7 +40,7 @@ namespace EventFwk { namespace { std::mutex mtx_; const time_t TIME_OUT_SECONDS_LIMIT = 3; -static OHOS::sptr bundleObject = nullptr; +static OHOS::sptr bundleObject = nullptr; std::shared_ptr handlerPtr; } // namespace @@ -76,7 +76,7 @@ void cesModuleTest::SetUpTestCase() auto task = []() { EventRunner::GetMainEventRunner()->Run(); }; handlerPtr->PostTask(task); - bundleObject = new OHOS::EventFwk::MockBundleMgrService(); + bundleObject = new OHOS::AppExecFwk::MockBundleMgrService(); OHOS::DelayedSingleton::GetInstance()->sptrBundleMgr_ = OHOS::iface_cast(bundleObject); OHOS::DelayedSingleton::GetInstance()->OnStart(); diff --git a/services/test/moduletest/common_event_services_publish_ordered_module_test/BUILD.gn b/services/test/moduletest/common_event_services_publish_ordered_module_test/BUILD.gn index 82db372..9732cdf 100644 --- a/services/test/moduletest/common_event_services_publish_ordered_module_test/BUILD.gn +++ b/services/test/moduletest/common_event_services_publish_ordered_module_test/BUILD.gn @@ -20,7 +20,7 @@ ohos_moduletest("CesPublishOrderedEventModuleTest") { module_out_path = module_output_path sources = [ - "${services_path}/test/mock/src/mock_bundle_manager.cpp", + "${common_event_service_path}/test/mock/mock_bundle_manager.cpp", "${services_path}/test/mock/src/mock_ipc.cpp", "common_event_services_publish_ordered_module_test.cpp", ] @@ -43,7 +43,7 @@ ohos_moduletest("CesPublishOrderedEventModuleTest") { "//foundation/distributedschedule/samgr/services/samgr/native/include", "//foundation/distributedschedule/samgr/utils/native/include", "${services_path}/include", - "${services_path}/test/mock/include", + "${common_event_service_path}/test/mock/include", "//third_party/json/include", "//utils/native/base/include", "//utils/system/safwk/native/include", diff --git a/services/test/moduletest/common_event_services_publish_ordered_module_test/common_event_services_publish_ordered_module_test.cpp b/services/test/moduletest/common_event_services_publish_ordered_module_test/common_event_services_publish_ordered_module_test.cpp index a39857e..ca7984f 100644 --- a/services/test/moduletest/common_event_services_publish_ordered_module_test/common_event_services_publish_ordered_module_test.cpp +++ b/services/test/moduletest/common_event_services_publish_ordered_module_test/common_event_services_publish_ordered_module_test.cpp @@ -40,7 +40,7 @@ using namespace OHOS::EventFwk; using OHOS::Parcel; namespace { -static OHOS::sptr p_bundleObject = nullptr; +static OHOS::sptr p_bundleObject = nullptr; const std::string EVENTCASE1 = "com.ces.test.event.case1"; const std::string EVENTCASE2 = "com.ces.test.event.case2"; const std::string EVENTCASE3 = "com.ces.test.event.case3"; @@ -386,7 +386,7 @@ private: void CesPublishOrderedEventModuleTest::SetUpTestCase(void) { - p_bundleObject = new OHOS::EventFwk::MockBundleMgrService(); + p_bundleObject = new OHOS::AppExecFwk::MockBundleMgrService(); OHOS::DelayedSingleton::GetInstance()->sptrBundleMgr_ = OHOS::iface_cast(p_bundleObject); OHOS::DelayedSingleton::GetInstance()->OnStart(); diff --git a/services/test/unittest/BUILD.gn b/services/test/unittest/BUILD.gn index 1dadeca..8f0827e 100644 --- a/services/test/unittest/BUILD.gn +++ b/services/test/unittest/BUILD.gn @@ -19,7 +19,7 @@ module_output_path = "common_event_service/services" config("cesfwk_services_config") { visibility = [ ":*" ] - include_dirs = [ "${services_path}/test/mock/include" ] + include_dirs = [ "${services_path}/test/unittest/mock/include" ] cflags = [] @@ -32,7 +32,7 @@ ohos_unittest("common_event_publish_system_event_test") { module_out_path = module_output_path sources = [ - "${services_path}/test/unittest/mock/src/mock_access_token_helper.cpp", + "${common_event_service_path}/test/mock/mock_access_token_helper.cpp", "${services_path}/test/unittest/mock/src/mock_bundle_manager.cpp", "common_event_publish_system_event_test.cpp", ] @@ -63,7 +63,7 @@ ohos_unittest("common_event_dump_test") { module_out_path = module_output_path sources = [ - "${services_path}/test/unittest/mock/src/mock_access_token_helper.cpp", + "${common_event_service_path}/test/mock/mock_access_token_helper.cpp", "${services_path}/test/unittest/mock/src/mock_bundle_manager.cpp", "common_event_dump_test.cpp", ] @@ -94,7 +94,7 @@ ohos_unittest("common_event_freeze_test") { module_out_path = module_output_path sources = [ - "${services_path}/test/unittest/mock/src/mock_access_token_helper.cpp", + "${common_event_service_path}/test/mock/mock_access_token_helper.cpp", "${services_path}/test/unittest/mock/src/mock_bundle_manager.cpp", "common_event_freeze_test.cpp", ] @@ -125,7 +125,7 @@ ohos_unittest("common_event_sticky_test") { module_out_path = module_output_path sources = [ - "${services_path}/test/unittest/mock/src/mock_access_token_helper.cpp", + "${common_event_service_path}/test/mock/mock_access_token_helper.cpp", "${services_path}/test/unittest/mock/src/mock_bundle_manager.cpp", "common_event_sticky_test.cpp", ] @@ -156,7 +156,7 @@ ohos_unittest("common_event_subscribe_unit_test") { module_out_path = module_output_path sources = [ - "${services_path}/test/unittest/mock/src/mock_access_token_helper.cpp", + "${common_event_service_path}/test/mock/mock_access_token_helper.cpp", "${services_path}/test/unittest/mock/src/mock_bundle_manager.cpp", "${services_path}/test/unittest/mock/src/mock_ipc.cpp", "common_event_subscribe_unit_test.cpp", @@ -188,7 +188,7 @@ ohos_unittest("common_event_unsubscribe_unit_test") { module_out_path = module_output_path sources = [ - "${services_path}/test/unittest/mock/src/mock_access_token_helper.cpp", + "${common_event_service_path}/test/mock/mock_access_token_helper.cpp", "${services_path}/test/unittest/mock/src/mock_bundle_manager.cpp", "common_event_unsubscribe_unit_test.cpp", ] @@ -219,7 +219,7 @@ ohos_unittest("common_event_publish_ordered_event_unit_test") { module_out_path = module_output_path sources = [ - "${services_path}/test/unittest/mock/src/mock_access_token_helper.cpp", + "${common_event_service_path}/test/mock/mock_access_token_helper.cpp", "${services_path}/test/unittest/mock/src/mock_bundle_manager.cpp", "common_event_publish_ordered_event_unit_test.cpp", ] @@ -250,7 +250,7 @@ ohos_unittest("common_event_publish_permission_event_unit_test") { module_out_path = module_output_path sources = [ - "${services_path}/test/unittest/mock/src/mock_access_token_helper.cpp", + "${common_event_service_path}/test/mock/mock_access_token_helper.cpp", "${services_path}/test/unittest/mock/src/mock_bundle_manager.cpp", "common_event_publish_permission_event_unit_test.cpp", ] diff --git a/services/test/unittest/common_event_dump_test.cpp b/services/test/unittest/common_event_dump_test.cpp index cf17455..e7c051d 100644 --- a/services/test/unittest/common_event_dump_test.cpp +++ b/services/test/unittest/common_event_dump_test.cpp @@ -29,6 +29,7 @@ using namespace testing::ext; using namespace OHOS::EventFwk; +using namespace OHOS::AppExecFwk; namespace { const std::string EVENT = "com.ces.test.event"; diff --git a/services/test/unittest/common_event_freeze_test.cpp b/services/test/unittest/common_event_freeze_test.cpp index 7fa5943..c47bd52 100644 --- a/services/test/unittest/common_event_freeze_test.cpp +++ b/services/test/unittest/common_event_freeze_test.cpp @@ -29,6 +29,7 @@ using namespace testing::ext; using namespace OHOS::EventFwk; +using namespace OHOS::AppExecFwk; namespace { const std::string EVENTCASE1 = "com.ces.test.event.case1"; const std::string EVENTCASE2 = "com.ces.test.event.case2"; diff --git a/services/test/unittest/common_event_publish_ordered_event_unit_test.cpp b/services/test/unittest/common_event_publish_ordered_event_unit_test.cpp index 95c6797..a3a1d07 100644 --- a/services/test/unittest/common_event_publish_ordered_event_unit_test.cpp +++ b/services/test/unittest/common_event_publish_ordered_event_unit_test.cpp @@ -30,6 +30,7 @@ using namespace testing::ext; using namespace OHOS::EventFwk; +using namespace OHOS::AppExecFwk; namespace { constexpr uint8_t PID = 0; diff --git a/services/test/unittest/common_event_publish_permission_event_unit_test.cpp b/services/test/unittest/common_event_publish_permission_event_unit_test.cpp index c314462..7ea47cc 100644 --- a/services/test/unittest/common_event_publish_permission_event_unit_test.cpp +++ b/services/test/unittest/common_event_publish_permission_event_unit_test.cpp @@ -32,6 +32,7 @@ using namespace testing::ext; using namespace OHOS; using namespace OHOS::EventFwk; +using namespace OHOS::AppExecFwk; namespace { const std::string EVENT = "com.ces.test.event"; diff --git a/services/test/unittest/common_event_publish_system_event_test.cpp b/services/test/unittest/common_event_publish_system_event_test.cpp index c1a7c58..b71ef4f 100644 --- a/services/test/unittest/common_event_publish_system_event_test.cpp +++ b/services/test/unittest/common_event_publish_system_event_test.cpp @@ -29,6 +29,7 @@ using namespace testing::ext; using namespace OHOS::EventFwk; +using namespace OHOS::AppExecFwk; namespace { constexpr uint8_t PID = 0; diff --git a/services/test/unittest/common_event_sticky_test.cpp b/services/test/unittest/common_event_sticky_test.cpp index ed4dc31..60c2b78 100644 --- a/services/test/unittest/common_event_sticky_test.cpp +++ b/services/test/unittest/common_event_sticky_test.cpp @@ -30,6 +30,7 @@ using namespace testing::ext; using namespace OHOS::EventFwk; +using namespace OHOS::AppExecFwk; namespace { const std::string EVENT = "com.ces.test.event"; diff --git a/services/test/unittest/common_event_subscribe_unit_test.cpp b/services/test/unittest/common_event_subscribe_unit_test.cpp index f19e55f..4619c95 100644 --- a/services/test/unittest/common_event_subscribe_unit_test.cpp +++ b/services/test/unittest/common_event_subscribe_unit_test.cpp @@ -30,6 +30,7 @@ using namespace testing::ext; using namespace OHOS::EventFwk; +using namespace OHOS::AppExecFwk; namespace { const std::string EVENT = "com.ces.test.event"; diff --git a/services/test/unittest/common_event_unsubscribe_unit_test.cpp b/services/test/unittest/common_event_unsubscribe_unit_test.cpp index 5fc7dc3..e17d3df 100644 --- a/services/test/unittest/common_event_unsubscribe_unit_test.cpp +++ b/services/test/unittest/common_event_unsubscribe_unit_test.cpp @@ -29,6 +29,7 @@ using namespace testing::ext; using namespace OHOS::EventFwk; +using namespace OHOS::AppExecFwk; namespace { const std::string EVENT = "com.ces.test.event"; diff --git a/services/test/unittest/mock/include/mock_bundle_manager.h b/services/test/unittest/mock/include/mock_bundle_manager.h index 5344d89..934804d 100644 --- a/services/test/unittest/mock/include/mock_bundle_manager.h +++ b/services/test/unittest/mock/include/mock_bundle_manager.h @@ -22,7 +22,6 @@ #include "application_info.h" #include "bundle_mgr_host.h" #include "bundle_mgr_interface.h" -#include "hilog_wrapper.h" #include "ohos/aafwk/content/want.h" #include "iremote_proxy.h" #include "iremote_stub.h" @@ -32,9 +31,7 @@ namespace AppExecFwk { class MockBundleMgrService : public BundleMgrHost { public: MockBundleMgrService() - { - HILOG_ERROR("BundleMgrService::BundleMgrService"); - } + {} ~MockBundleMgrService() {} diff --git a/services/test/unittest/mock/src/mock_bundle_manager.cpp b/services/test/unittest/mock/src/mock_bundle_manager.cpp index f788d0a..8e5867e 100644 --- a/services/test/unittest/mock/src/mock_bundle_manager.cpp +++ b/services/test/unittest/mock/src/mock_bundle_manager.cpp @@ -18,7 +18,7 @@ #include "mock_bundle_manager.h" namespace OHOS { -namespace EventFwk { +namespace AppExecFwk { constexpr uint16_t SYSTEM_UID = 1000; void MockBundleMgrService::MockSetIsSystemApp(bool isSystemApp) @@ -29,7 +29,6 @@ void MockBundleMgrService::MockSetIsSystemApp(bool isSystemApp) bool MockBundleMgrService::CheckIsSystemAppByUid(const int uid) { - EVENT_LOGD("MockBundleMgrService::CheckIsSystemAppByUid"); if (isSystemAppMock_) { return isSystemApp_; } @@ -38,7 +37,6 @@ bool MockBundleMgrService::CheckIsSystemAppByUid(const int uid) int MockBundleMgrService::CheckPermission(const std::string &bundleName, const std::string &permission) { - EVENT_LOGD("MockBundleMgrService::CheckPermission"); static int num1 = 0; static int num2 = 0; diff --git a/test/benchmarktest/BUILD.gn b/test/benchmarktest/BUILD.gn new file mode 100644 index 0000000..44fca6d --- /dev/null +++ b/test/benchmarktest/BUILD.gn @@ -0,0 +1,30 @@ +# 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. + +import("//base/notification/common_event_service/event.gni") +import("//build/ohos.gni") + +config("bechmark_test_config") { + include_dirs = [ "//third_party/json/include" ] + + configs = [] +} + +group("benchmarktest") { + testonly = true + + deps = [ + "common_event_publish_test:benchmarktest", + "common_event_service_test:benchmarktest", + ] +} diff --git a/test/benchmarktest/common_event_publish_test/BUILD.gn b/test/benchmarktest/common_event_publish_test/BUILD.gn new file mode 100644 index 0000000..75f3c96 --- /dev/null +++ b/test/benchmarktest/common_event_publish_test/BUILD.gn @@ -0,0 +1,50 @@ +# Copyright (c) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//base/notification/common_event_service/event.gni") +import("//build/test.gni") + +module_output_path = "common_event_service/benchmarktest" + +ohos_benchmarktest("Common_Event_Publish_Test") { + module_out_path = module_output_path + include_dirs = [ "${services_path}/include" ] + + sources = [ "common_event_publish_test.cpp" ] + + deps = [ + "${ces_native_path}:cesfwk_innerkits", + "//third_party/benchmark:benchmark", + + # "//third_party/googletest:gtest_main", + # "//utils/native/base:utils", + ] + + external_deps = [ + "ability_base:want", + "eventhandler:libeventhandler", + "ipc:ipc_core", + ] + subsystem_name = "notification" + part_name = "common_event_service" +} + +group("benchmarktest") { + testonly = true + deps = [] + + deps += [ + # deps file + ":Common_Event_Publish_Test", + ] +} diff --git a/test/benchmarktest/common_event_publish_test/common_event_publish_test.cpp b/test/benchmarktest/common_event_publish_test/common_event_publish_test.cpp new file mode 100644 index 0000000..0a3d2f7 --- /dev/null +++ b/test/benchmarktest/common_event_publish_test/common_event_publish_test.cpp @@ -0,0 +1,123 @@ +/* + * 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 +#include + +#include "common_event_constant.h" +#include "common_event_listener.h" +#include "common_event_manager.h" +#include "singleton.h" + +using namespace OHOS::EventFwk; + +namespace { +class BenchmarkCommonEventPublish : public benchmark::Fixture { +public: + BenchmarkCommonEventPublish() + { + Iterations(iterations); + Repetitions(repetitions); + ReportAggregatesOnly(); + } + + virtual ~BenchmarkCommonEventPublish() override = default; + + void SetUp(const ::benchmark::State &state) override + {} + + void TearDown(const ::benchmark::State &state) override + {} + +protected: + const int32_t repetitions = 1; + const int32_t iterations = 100; +}; + +class CommonEventSubscriberBenchmark : public CommonEventSubscriber { +public: + explicit CommonEventSubscriberBenchmark(const CommonEventSubscribeInfo &subscribeInfo) : CommonEventSubscriber( + subscribeInfo) {}; + virtual ~CommonEventSubscriberBenchmark() {}; + virtual void OnReceiveEvent(const CommonEventData &data) {}; +}; + +/** +* @tc.name: SubscribeCommonEventTestCase +* @tc.desc: SubscribeCommonEvent +* @tc.type: FUNC +* @tc.require: +*/ +BENCHMARK_F(BenchmarkCommonEventPublish, SubscribeCommonEventTestCase)(benchmark::State &state) +{ + std::string eventName = "SUBSCRIBER_EVENT_BENCHMARK"; + MatchingSkills matchingSkills; + matchingSkills.AddEvent(eventName); + CommonEventSubscribeInfo subscribeInfo(matchingSkills); + auto subscriberPtr = std::make_shared(subscribeInfo); + while (state.KeepRunning()) { + CommonEventManager::SubscribeCommonEvent(subscriberPtr); + } +} + +/** +* @tc.name: UnsubscribeCommonEventTestCase +* @tc.desc: UnsubscribeCommonEvent +* @tc.type: FUNC +* @tc.require: +*/ +BENCHMARK_F(BenchmarkCommonEventPublish, UnsubscribeCommonEventTestCase)(benchmark::State &state) +{ + std::string eventName = "UNSUBSCRIBER_EVENT_BENCHMARK"; + MatchingSkills matchingSkills; + matchingSkills.AddEvent(eventName); + CommonEventSubscribeInfo subscribeInfo(matchingSkills); + subscribeInfo.SetPriority(1000); + auto subscriberPtr = std::make_shared(subscribeInfo); + + while (state.KeepRunning()) { + if (OHOS::EventFwk::CommonEventManager::SubscribeCommonEvent(subscriberPtr)) { + OHOS::EventFwk::CommonEventManager::UnSubscribeCommonEvent(subscriberPtr); + } + } +} + +/** +* @tc.name: PublishCommonEventTestCase +* @tc.desc: PublishCommonEvent +* @tc.type: FUNC +* @tc.require: +*/ +BENCHMARK_F(BenchmarkCommonEventPublish, PublishCommonEventTestCase)(benchmark::State &state) +{ + std::string eventAction = "PUBLISH_COMMON_EVENT_BENCHMARK"; + std::string entity = "ADDENTITY"; + bool stickty = false; + Want testWant; + testWant.SetAction(eventAction); + testWant.AddEntity(entity); + CommonEventData commonEventData(testWant); + CommonEventPublishInfo publishInfo; + publishInfo.SetSticky(stickty); + + while (state.KeepRunning()) { + OHOS::EventFwk::CommonEventManager::PublishCommonEvent(commonEventData, publishInfo); + } +} +} + +// Run the benchmark +BENCHMARK_MAIN(); \ No newline at end of file diff --git a/test/benchmarktest/common_event_service_test/BUILD.gn b/test/benchmarktest/common_event_service_test/BUILD.gn new file mode 100644 index 0000000..d577fb0 --- /dev/null +++ b/test/benchmarktest/common_event_service_test/BUILD.gn @@ -0,0 +1,67 @@ +# 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. + +import("//base/notification/common_event_service/event.gni") +import("//build/test.gni") + +module_output_path = "common_event_service/benchmarktest" + +ohos_benchmarktest("Common_Event_Manager_Service_Test") { + module_out_path = module_output_path + include_dirs = [ + "include", + "${common_event_service_path}/test/mock/include", + "${ces_core_path}/include", + "${ces_innerkits_path}/include", + "//foundation/distributedschedule/safwk/services/safwk/include", + "${services_path}/include", + ] + + sources = [ + "${common_event_service_path}/test/mock/mock_access_token_helper.cpp", + "${common_event_service_path}/test/mock/mock_bundle_manager.cpp", + "${common_event_service_path}/test/mock/mock_ipc.cpp", + "common_event_manager_service_test.cpp", + ] + + deps = [ + "${ces_common_path}:libevent_common", + "${ces_core_path}:cesfwk_core", + "${ces_native_path}:cesfwk_innerkits", + "${services_path}:cesfwk_services", + "//third_party/benchmark:benchmark", + "//utils/native/base:utils", + ] + + external_deps = [ + "ability_base:want", + "access_token:libaccesstoken_sdk", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "eventhandler:libeventhandler", + "ipc:ipc_core", + "ipc:libdbinder", + ] + subsystem_name = "notification" + part_name = "common_event_service" +} + +group("benchmarktest") { + testonly = true + deps = [] + + deps += [ + # deps file + ":Common_Event_Manager_Service_Test", + ] +} diff --git a/test/benchmarktest/common_event_service_test/common_event_manager_service_test.cpp b/test/benchmarktest/common_event_service_test/common_event_manager_service_test.cpp new file mode 100644 index 0000000..354f58f --- /dev/null +++ b/test/benchmarktest/common_event_service_test/common_event_manager_service_test.cpp @@ -0,0 +1,223 @@ +/* + * 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 +#include + +#define private public +#include "common_event_constant.h" +#include "common_event_listener.h" +#include "common_event_manager_service.h" +#include "mock_bundle_manager.h" +#include "singleton.h" + +using namespace OHOS; +using namespace OHOS::EventFwk; + +namespace { +class BenchmarkCommonEventManagerService : public benchmark::Fixture { +public: + BenchmarkCommonEventManagerService() + { + Iterations(iterations); + Repetitions(repetitions); + ReportAggregatesOnly(); + } + + virtual ~BenchmarkCommonEventManagerService() override = default; + + void SetUp(const ::benchmark::State &state) override + { + OHOS::DelayedSingleton::GetInstance()->OnStart(); + } + void TearDown(const ::benchmark::State &state) override + { + OHOS::DelayedSingleton::GetInstance()->OnStop(); + } + +protected: + const int32_t repetitions = 3; + const int32_t iterations = 100; +}; + +class CommonEventSubscriberBenchmark : public CommonEventSubscriber { +public: + explicit CommonEventSubscriberBenchmark(const CommonEventSubscribeInfo &subscribeInfo) : CommonEventSubscriber( + subscribeInfo) {}; + virtual ~CommonEventSubscriberBenchmark() {}; + virtual void OnReceiveEvent(const CommonEventData &data) {}; +}; +/** + * @tc.name: CommonEventSubscribeTestCase001 + * @tc.desc: SubscribeCommonEvent: event is normal + * @tc.type: FUNC + * @tc.require: + */ +BENCHMARK_F(BenchmarkCommonEventManagerService, CommonEventSubscribeTestCase001)(benchmark::State &state) +{ + std::string eventName = "SUBSCRIBEEVENT_BENCHMARK"; + MatchingSkills matchingSkills; + matchingSkills.AddEvent(eventName); + CommonEventSubscribeInfo subscribeInfo(matchingSkills); + auto subscriberPtr = std::make_shared(subscribeInfo); + OHOS::sptr commonEventListener = new CommonEventListener(subscriberPtr); + + while (state.KeepRunning()) { + bool result = OHOS::DelayedSingleton::GetInstance()->SubscribeCommonEvent( + subscribeInfo, commonEventListener); + if (!result) { + state.SkipWithError("SubscribeCommonEvent failed."); + } + } +} + +/** + * @tc.name: CommonEventSubscribeTestCase002 + * @tc.desc: SubscribeCommonEvent: event is number + * @tc.type: FUNC + * @tc.require: + */ +BENCHMARK_F(BenchmarkCommonEventManagerService, CommonEventSubscribeTestCase002)(benchmark::State &state) +{ + std::string eventName = "1"; + MatchingSkills matchingSkills; + matchingSkills.AddEvent(eventName); + CommonEventSubscribeInfo subscribeInfo(matchingSkills); + auto subscriberPtr = std::make_shared(subscribeInfo); + OHOS::sptr commonEventListener = new CommonEventListener(subscriberPtr); + + while (state.KeepRunning()) { + bool result = OHOS::DelayedSingleton::GetInstance()->SubscribeCommonEvent( + subscribeInfo, commonEventListener); + if (!result) { + state.SkipWithError("SubscribeCommonEvent failed."); + } + } +} + +/** + * @tc.name: CommonEventSubscribeTestCase003 + * @tc.desc: SubscribeCommonEvent: event is empty + * @tc.type: FUNC + * @tc.require: + */ +BENCHMARK_F(BenchmarkCommonEventManagerService, CommonEventSubscribeTestCase003)(benchmark::State &state) +{ + std::string eventName = ""; + MatchingSkills matchingSkills; + matchingSkills.AddEvent(eventName); + CommonEventSubscribeInfo subscribeInfo(matchingSkills); + auto subscriberPtr = std::make_shared(subscribeInfo); + OHOS::sptr commonEventListener = new CommonEventListener(subscriberPtr); + + while (state.KeepRunning()) { + bool result = OHOS::DelayedSingleton::GetInstance()->SubscribeCommonEvent( + subscribeInfo, commonEventListener); + if (!result) { + state.SkipWithError("SubscribeCommonEvent failed."); + } + } +} + +/** + * @tc.name: CommonEventUnsubscribeTestCase + * @tc.desc: UnsubscribeCommonEvent + * @tc.type: FUNC + * @tc.require: + */ +BENCHMARK_F(BenchmarkCommonEventManagerService, CommonEventUnsubscribeTestCase)(benchmark::State &state) +{ + std::string eventName = "SUBSCRIBEEVENT_BENCHMARK"; + MatchingSkills matchingSkills; + matchingSkills.AddEvent(eventName); + CommonEventSubscribeInfo subscribeInfo(matchingSkills); + auto subscriberPtr = std::make_shared(subscribeInfo); + OHOS::sptr commonEventListener = new CommonEventListener(subscriberPtr); + + while (state.KeepRunning()) { + bool result = OHOS::DelayedSingleton::GetInstance()->UnsubscribeCommonEvent( + commonEventListener); + if (!result) { + state.SkipWithError("UnsubscribeCommonEvent failed."); + } + } +} + +/** + * @tc.name: CommonEventPublishTestCase + * @tc.desc: PublishCommonEvent + * @tc.type: FUNC + * @tc.require: + */ +BENCHMARK_F(BenchmarkCommonEventManagerService, CommonEventPublishTestCase)(benchmark::State &state) +{ + std::string eventName = "SUBSCRIBEEVENT_BENCHMARK"; + std::string eventAction = "SUBSCRIBEEVENT_BENCHMARK"; + bool stickty = false; + MatchingSkills matchingSkills; + matchingSkills.AddEvent(eventName); + Want testWant; + testWant.SetAction(eventAction); + CommonEventData commonEventData(testWant); + CommonEventPublishInfo publishInfo; + publishInfo.SetSticky(stickty); + CommonEventSubscribeInfo subscribeInfo(matchingSkills); + auto subscriberPtr = std::make_shared(subscribeInfo); + OHOS::sptr commonEventListener = new CommonEventListener(subscriberPtr); + + while (state.KeepRunning()) { + bool result = OHOS::DelayedSingleton::GetInstance()->PublishCommonEvent( + commonEventData, publishInfo, commonEventListener, UNDEFINED_USER); + if (!result) { + state.SkipWithError("PublishCommonEvent failed."); + } + } +} + +/** + * @tc.name: CommonEventDumpStateTestCase + * @tc.desc: DumpState + * @tc.type: FUNC + * @tc.require: + */ +BENCHMARK_F(BenchmarkCommonEventManagerService, CommonEventDumpStateTestCase)(benchmark::State &state) +{ + std::string eventName = "SUBSCRIBEEVENT_BENCHMARK"; + MatchingSkills matchingSkills; + matchingSkills.AddEvent(eventName); + CommonEventSubscribeInfo subscribeInfo(matchingSkills); + auto subscriberPtr = std::make_shared(subscribeInfo); + OHOS::sptr commonEventListener = new CommonEventListener(subscriberPtr); + + bool result = OHOS::DelayedSingleton::GetInstance()->SubscribeCommonEvent( + subscribeInfo, commonEventListener); + if (!result) { + state.SkipWithError("DumpState subscribe common event failed."); + } + + while (state.KeepRunning()) { + std::vector stateTest; + if (OHOS::DelayedSingleton::GetInstance()->DumpState("", ALL_USER, stateTest)) { + if (stateTest.size() < 1) { + state.SkipWithError("DumpState failed."); + } + } + } +} +} + +// Run the benchmark +BENCHMARK_MAIN(); \ No newline at end of file diff --git a/services/test/mock/include/mock_bundle_manager.h b/test/mock/include/mock_bundle_manager.h similarity index 83% rename from services/test/mock/include/mock_bundle_manager.h rename to test/mock/include/mock_bundle_manager.h index 271ef7d..563b1f0 100644 --- a/services/test/mock/include/mock_bundle_manager.h +++ b/test/mock/include/mock_bundle_manager.h @@ -1,693 +1,601 @@ -/* - * Copyright (c) 2021-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. - */ - -#ifndef FOUNDATION_EVENT_CESFWK_SERVICES_TEST_UNITTEST_MOCK_INCLUDE_MOCK_BUNDLE_MANAGER_H -#define FOUNDATION_EVENT_CESFWK_SERVICES_TEST_UNITTEST_MOCK_INCLUDE_MOCK_BUNDLE_MANAGER_H - -#include - -#include "ability_info.h" -#include "application_info.h" -#include "bundle_mgr_host.h" -#include "bundle_mgr_interface.h" -#include "event_log_wrapper.h" -#include "iremote_proxy.h" -#include "iremote_stub.h" -#include "want.h" - -namespace OHOS { -namespace EventFwk { -using namespace OHOS::AppExecFwk; - -class MockBundleMgrService : public BundleMgrHost { -public: - MockBundleMgrService() - { - EVENT_LOGD("BundleMgrService::BundleMgrService"); - } - - ~MockBundleMgrService() - {} - - /** - * @brief Obtains the ApplicationInfo based on a given bundle name. - * @param appName Indicates the application bundle name to be queried. - * @param flag Indicates the flag used to specify information contained - * in the ApplicationInfo object that will be returned. - * @param userId Indicates the user ID. - * @param appInfo Indicates the obtained ApplicationInfo object. - * @return Returns true if the application is successfully obtained; returns false otherwise. - */ - virtual bool GetApplicationInfo( - const std::string &appName, const ApplicationFlag flag, const int userId, ApplicationInfo &appInfo) override - { - return true; - } - - /** - * @brief Obtains information about all installed applications of a specified user. - * @param flag Indicates the flag used to specify information contained - * in the ApplicationInfo objects that will be returned. - * @param userId Indicates the user ID. - * @param appInfos Indicates all of the obtained ApplicationInfo objects. - * @return Returns true if the application is successfully obtained; returns false otherwise. - */ - virtual bool GetApplicationInfos( - const ApplicationFlag flag, const int userId, std::vector &appInfos) override - { - return true; - } - - /** - * @brief Obtains the BundleInfo based on a given bundle name. - * @param bundleName Indicates the application bundle name to be queried. - * @param flag Indicates the information contained in the BundleInfo object to be returned. - * @param bundleInfo Indicates the obtained BundleInfo object. - * @param userId Indicates the user ID. - * @return Returns true if the BundleInfo is successfully obtained; returns false otherwise. - */ - virtual bool GetBundleInfo( - const std::string &bundleName, const BundleFlag flag, BundleInfo &bundleInfo, int32_t userId) override - { - return true; - } - - /** - * @brief Obtains BundleInfo of all bundles available in the system. - * @param flag Indicates the flag used to specify information contained in the BundleInfo that will be returned. - * @param bundleInfos Indicates all of the obtained BundleInfo objects. - * @param userId Indicates the user ID. - * @return Returns true if the BundleInfos is successfully obtained; returns false otherwise. - */ - virtual bool GetBundleInfos( - const BundleFlag flag, std::vector &bundleInfos, int32_t userId) override - { - return true; - } - - /** - * @brief Obtains the application UID based on the given bundle name and user ID. - * @param bundleName Indicates the bundle name of the application. - * @param userId Indicates the user ID. - * @return Returns the uid if successfully obtained; returns -1 otherwise. - */ - virtual int GetUidByBundleName(const std::string &bundleName, const int userId) override - { - return 0; - } - - /** - * @brief Obtains the bundle name of a specified application based on the given UID. - * @param uid Indicates the uid. - * @param bundleName Indicates the obtained bundle name. - * @return Returns true if the bundle name is successfully obtained; returns false otherwise. - */ - virtual bool GetBundleNameForUid(const int uid, std::string &bundleName) override - { - bundleName = "hello"; - return true; - } - - /** - * @brief Obtains an array of all group IDs associated with a specified bundle. - * @param bundleName Indicates the bundle name. - * @param gids Indicates the group IDs associated with the specified bundle. - * @return Returns true if the gids is successfully obtained; returns false otherwise. - */ - virtual bool GetBundleGids(const std::string &bundleName, std::vector &gids) override - { - return true; - } - - /** - * @brief Obtains the type of a specified application based on the given bundle name. - * @param bundleName Indicates the bundle name. - * @return Returns "system" if the bundle is a system application; returns "third-party" otherwise. - */ - virtual std::string GetAppType(const std::string &bundleName) override - { - return ""; - } - - /** - * @brief Checks whether the app is system app by it's UID. - * @param uid Indicates the uid. - * @return Returns true if the bundle is a system application; returns false otherwise. - */ - virtual bool CheckIsSystemAppByUid(const int uid) override; - - /** - * @brief Obtains the BundleInfo of application bundles based on the specified metaData. - * @param metaData Indicates the metadata to get in the bundle. - * @param bundleInfos Indicates all of the obtained BundleInfo objects. - * @return Returns true if the BundleInfos is successfully obtained; returns false otherwise. - */ - virtual bool GetBundleInfosByMetaData(const std::string &metaData, std::vector &bundleInfos) override - { - return true; - } - - /** - * @brief Queries the AbilityInfo by the given Want. - * @param want Indicates the information of the ability. - * @param abilityInfo Indicates the obtained AbilityInfo object. - * @return Returns true if the AbilityInfo is successfully obtained; returns false otherwise. - */ - virtual bool QueryAbilityInfo(const AAFwk::Want &want, AbilityInfo &abilityInfo) override - { - return true; - } - - /** - * @brief Queries the AbilityInfo of list by the given Want. - * @param want Indicates the information of the ability. - * @param abilityInfos Indicates the obtained AbilityInfos object. - * @return Returns true if the AbilityInfos is successfully obtained; returns false otherwise. - */ - virtual bool QueryAbilityInfos(const Want &want, std::vector &abilityInfos) override - { - return true; - } - - /** - * @brief Queries the AbilityInfo of list for clone by the given Want. - * @param want Indicates the information of the ability. - * @param abilityInfos Indicates the obtained AbilityInfos object. - * @return Returns true if the AbilityInfos is successfully obtained; returns false otherwise. - */ - virtual bool QueryAbilityInfosForClone(const Want &want, std::vector &abilityInfos) override - { - return true; - } - - /** - * @brief Queries the AbilityInfo by ability.uri in config.json. - * @param abilityUri Indicates the uri of the ability. - * @param abilityInfo Indicates the obtained AbilityInfo object. - * @return Returns true if the AbilityInfo is successfully obtained; returns false otherwise. - */ - virtual bool QueryAbilityInfoByUri(const std::string &abilityUri, AbilityInfo &abilityInfo) override - { - return true; - } - - /** - * @brief Obtains the BundleInfo of all keep-alive applications in the system. - * @param bundleInfos Indicates all of the obtained BundleInfo objects. - * @return Returns true if the BundleInfos is successfully obtained; returns false otherwise. - */ - virtual bool QueryKeepAliveBundleInfos(std::vector &bundleInfos) override - { - return true; - } - - /** - * @brief Obtains information about an application bundle contained in an ohos Ability Package (HAP). - * @param hapFilePath Indicates the absolute file path of the HAP. - * @param flag Indicates the information contained in the BundleInfo object to be returned. - * @param bundleInfo Indicates the obtained BundleInfo object. - * @return Returns true if the BundleInfo is successfully obtained; returns false otherwise. - */ - virtual bool GetBundleArchiveInfo( - const std::string &hapFilePath, const BundleFlag flag, BundleInfo &bundleInfo) override - { - return true; - } - - /** - * @brief Obtains the HAP module info of a specific ability. - * @param abilityInfo Indicates the ability. - * @param hapModuleInfo Indicates the obtained HapModuleInfo object. - * @return Returns true if the HapModuleInfo is successfully obtained; returns false otherwise. - */ - virtual bool GetHapModuleInfo(const AbilityInfo &abilityInfo, HapModuleInfo &hapModuleInfo) override - { - return true; - } - - /** - * @brief Obtains the Want for starting the main ability of an application based on the given bundle name. - * @param bundleName Indicates the bundle name. - * @param want Indicates the obtained launch Want object. - * @return Returns true if the launch Want object is successfully obtained; returns false otherwise. - */ - virtual bool GetLaunchWantForBundle(const std::string &bundleName, Want &want) override - { - return true; - } - - /** - * @brief Confirms with the permission management module to check whether a request prompt is required for granting - * a certain permission. - * @param bundleName Indicates the name of the bundle to check. - * @param permission Indicates the permission to check. - * @param userId Indicates the user ID. - * @return Returns true if the current application does not have the permission and the user does not turn off - * further requests; returns false if the current application already has the permission, the permission is rejected - * by the system, or the permission is denied by the user and the user has turned off further requests. - */ - virtual bool CanRequestPermission( - const std::string &bundleName, const std::string &permissionName, const int userId) override - { - return true; - } - - /** - * @brief Checks whether the publickeys of two bundles are the same. - * @param firstBundleName Indicates the first bundle name. - * @param secondBundleName Indicates the second bundle name. - * @return Returns SIGNATURE_UNKNOWN_BUNDLE if at least one of the given bundles is not found; - * returns SIGNATURE_NOT_MATCHED if their publickeys are different; - * returns SIGNATURE_MATCHED if their publickeys are the same. - */ - virtual int CheckPublicKeys(const std::string &firstBundleName, const std::string &secondBundleName) override - { - return 0; - } - - /** - * @brief Checks whether a specified bundle has been granted a specific permission. - * @param bundleName Indicates the name of the bundle to check. - * @param permission Indicates the permission to check. - * @return Returns 0 if the bundle has the permission; returns -1 otherwise. - */ - virtual int CheckPermission(const std::string &bundleName, const std::string &permission) override; - - /** - * @brief Obtains detailed information about a specified permission. - * @param permissionName Indicates the name of the ohos permission. - * @param permissionDef Indicates the object containing detailed information about the given ohos permission. - * @return Returns true if the PermissionDef object is successfully obtained; returns false otherwise. - */ - virtual bool GetPermissionDef(const std::string &permissionName, PermissionDef &permissionDef) override - { - return true; - } - - /** - * @brief Obtains all known permission groups in the system. - * @param permissionDefs Indicates the list of objects containing the permission group information. - * @return Returns true if the PermissionDef objects is successfully obtained; returns false otherwise. - */ - virtual bool GetAllPermissionGroupDefs(std::vector &permissionDefs) override - { - return true; - } - - /** - * @brief Obtains all known permission groups in the system. - * @param permissions Indicates the permission array. - * @param appNames Indicates the list of application names that have the specified permissions. - * @return Returns true if the application names is successfully obtained; returns false otherwise. - */ - virtual bool GetAppsGrantedPermissions( - const std::vector &permissions, std::vector &appNames) override - { - return true; - } - - /** - * @brief Requests a certain permission from user. - * @param bundleName Indicates the name of the bundle to request permission. - * @param permission Indicates the permission to request permission. - * @param userId Indicates the user ID. - * @return Returns true if the permission request successfully; returns false otherwise. - */ - virtual bool RequestPermissionFromUser( - const std::string &bundleName, const std::string &permission, const int userId) override - { - return true; - } - - /** - * @brief Checks whether the system has a specified capability. - * @param capName Indicates the name of the system feature to check. - * @return Returns true if the given feature specified by name is available in the system; returns false otherwise. - */ - virtual bool HasSystemCapability(const std::string &capName) override - { - return true; - } - - /** - * @brief Obtains the capabilities that are available in the system. - * @param systemCaps Indicates the list of capabilities available in the system. - * @return Returns true if capabilities in the system are successfully obtained; returns false otherwise. - */ - virtual bool GetSystemAvailableCapabilities(std::vector &systemCaps) override - { - return true; - } - - /** - * @brief Checks whether the current device has been started in safe mode. - * @return Returns true if the device is in safe mode; returns false otherwise. - */ - virtual bool IsSafeMode() override - { - return true; - } - - /** - * @brief Clears application running data of a specified application. - * @param bundleName Indicates the bundle name of the application whose data is to be cleared. - * @return Returns true if the data cleared successfully; returns false otherwise. - */ - virtual bool CleanBundleDataFiles(const std::string &bundleName, const int userId) override - { - return true; - } - - /** - * @brief Registers the specific bundle status callback. - * @param bundleStatusCallback Indicates the callback to be invoked for returning the bundle status changed result. - * @return Returns true if this function is successfully called; returns false otherwise. - */ - virtual bool RegisterBundleStatusCallback(const sptr &bundleStatusCallback) override - { - return true; - } - - /** - * @brief Clears the specific bundle status callback. - * @param bundleStatusCallback Indicates the callback to be cleared. - * @return Returns true if this function is successfully called; returns false otherwise. - */ - virtual bool ClearBundleStatusCallback(const sptr &bundleStatusCallback) override - { - return true; - } - - /** - * @brief Unregisters all the callbacks of status changed. - * @return Returns true if this function is successfully called; returns false otherwise. - */ - virtual bool UnregisterBundleStatusCallback() override - { - return true; - } - - /** - * @brief Dumps the bundle informations with specific flags. - * @param flag Indicates the information contained in the dump result. - * @param bundleName Indicates the bundle name if needed. - * @param userId Indicates the user ID. - * @param result Indicates the dump information result. - * @return Returns true if the dump result is successfully obtained; returns false otherwise. - */ - virtual bool DumpInfos( - const DumpFlag flag, const std::string &bundleName, int32_t userId, std::string &result) override - { - return true; - } - - /** - * @brief Checks whether a specified application is enabled. - * @param bundleName Indicates the bundle name of the application. - * @return Returns true if the application is enabled; returns false otherwise. - */ - virtual bool IsApplicationEnabled(const std::string &bundleName) override - { - return true; - } - - /** - * @brief Obtains the interface used to install and uninstall bundles. - * @return Returns a pointer to IBundleInstaller class if exist; returns nullptr otherwise. - */ - virtual sptr GetBundleInstaller() override - { - return nullptr; - } - - /** - * @brief Obtains the interface used to create or delete user. - * @return Returns a pointer to IBundleUserMgr class if exist; returns nullptr otherwise. - */ - virtual sptr GetBundleUserMgr() override - { - return nullptr; - } - - /** - * @brief Sets whether to enable a specified ability. - * @param abilityInfo Indicates information about the ability to check. - * @return Returns true if the ability is enabled; returns false otherwise. - */ - virtual bool IsAbilityEnabled(const AbilityInfo &abilityInfo) override - { - return true; - } - - /** - * @brief Obtains all bundle names of a specified application based on the given application UID. - * @param uid Indicates the uid. - * @param bundleNames Indicates the obtained bundle names. - * @return Returns true if the bundle names is successfully obtained; returns false otherwise. - */ - virtual bool GetBundlesForUid(const int uid, std::vector &bundleNames) override - { - return true; - } - - /** - * @brief Obtains the formal name associated with the given UID. - * @param uid Indicates the uid. - * @param name Indicates the obtained formal name. - * @return Returns true if the formal name is successfully obtained; returns false otherwise. - */ - virtual bool GetNameForUid(const int uid, std::string &name) override - { - return true; - } - - /** - * @brief Registers a callback for listening for permission changes of all UIDs. - * @param callback Indicates the callback method to register. - * @return Returns true if this function is successfully called; returns false otherwise. - */ - virtual bool RegisterAllPermissionsChanged(const sptr &callback) override - { - return true; - } - - /** - * @brief Registers a callback for listening for permission changes of specified UIDs. - * @param uids Indicates the list of UIDs whose permission changes will be monitored. - * @param callback Indicates the callback method to register. - * @return Returns true if this function is successfully called; returns false otherwise. - */ - virtual bool RegisterPermissionsChanged( - const std::vector &uids, const sptr &callback) override - { - return true; - } - - /** - * @brief Unregisters a specified callback for listening for permission changes. - * @param callback Indicates the callback method to register. - * @return Returns true if this function is successfully called; returns false otherwise. - */ - virtual bool UnregisterPermissionsChanged(const sptr &callback) override - { - return true; - } - - /** - * @brief Obtains the application ID based on the given bundle name and user ID. - * @param bundleName Indicates the bundle name of the application. - * @param userId Indicates the user ID. - * @return Returns the application ID if successfully obtained; returns empty string otherwise. - */ - virtual std::string GetAppIdByBundleName(const std::string &bundleName, const int userId) override - { - return ""; - } - - /** - * @brief Obtains the FormInfo objects provided by all applications on the device. - * @param formInfo List of FormInfo objects if obtained; returns an empty List if no FormInfo is available on the - * device. - * @return Returns true if this function is successfully called; returns false otherwise. - */ - virtual bool GetAllFormsInfo(std::vector &formInfos) override - { - return true; - } - - /** - * @brief Obtains the FormInfo objects provided by a specified application on the device. - * @param bundleName Indicates the bundle name of the application. - * @param formInfo List of FormInfo objects if obtained; returns an empty List if no FormInfo is available on the - * device. - * @return Returns true if this function is successfully called; returns false otherwise. - */ - virtual bool GetFormsInfoByApp(const std::string &bundleName, std::vector &formInfos) override - { - return true; - } - - /** - * @brief Obtains the FormInfo objects provided by a specified module name. - * @param formInfo List of FormInfo objects if obtained; returns an empty List if no FormInfo is available on the - * device. - * @param moduleName Indicates the module name of the application. - * @param bundleName Indicates the bundle name of the application. - * @return Returns true if this function is successfully called; returns false otherwise. - */ - virtual bool GetFormsInfoByModule( - const std::string &bundleName, const std::string &moduleName, std::vector &formInfos) override - { - return true; - } - - /** - * @brief Obtains the ShortcutInfo objects provided by a specified application on the device. - * @param bundleName Indicates the bundle name of the application. - * @param shortcutInfos List of ShortcutInfo objects if obtained. - * @return Returns true if this function is successfully called; returns false otherwise. - */ - virtual bool GetShortcutInfos(const std::string &bundleName, std::vector &shortcutInfos) override - { - return true; - } - - /** - * @brief Obtains the CommonEventInfo objects provided by a event key on the device. - * @param eventKey Indicates the event of the subscribe. - * @param commonEventInfos List of CommonEventInfo objects if obtained. - * @return Returns true if this function is successfully called; returns false otherwise. - */ - virtual bool GetAllCommonEventInfo(const std::string &eventKey, - std::vector &commonEventInfos) override - { - return true; - } - - void MockSetIsSystemApp(bool isSystemApp); - /** - * @brief Gets module usage record list in descending order of lastLaunchTime. - * @param maxNum the return size of the records, must be in range of 1 to 1000. - * @param moduleUsageRecords List of ModuleUsageRecord objects if obtained. - * @return Returns true if this function is successfully called; returns false otherwise. - */ - virtual bool GetModuleUsageRecords( - const int32_t number, std::vector &moduleUsageRecords) override - { - return true; - } - - /** - * @brief Notifies a specified ability for ability. - * @param bundleName Indicates the bundle name of the ability to ability. - * @param abilityName Indicates the name of the ability to ability. - * @param launchTime Indicates the ability launchTime. - * @return Returns true if this function is successfully called; returns false otherwise. - */ - virtual bool NotifyAbilityLifeStatus( - const std::string &bundleName, const std::string &abilityName, const int64_t launchTime, const int uid) override - { - return true; - } - - /** - * @brief Remove cloned bundle. - * @param bundleName Indicates the bundle name of remove cloned bundle. - * @param uid Indicates the uid of remove cloned bundle. - * @return Returns true if this function is successfully called; returns false otherwise. - */ - virtual bool RemoveClonedBundle(const std::string &bundleName, const int32_t uid) override - { - return true; - } - - /** - * @brief create bundle clone. - * @param bundleName Indicates the bundle name of create bundle clone. - * @return Returns true if this function is successfully called; returns false otherwise. - */ - virtual bool BundleClone(const std::string &bundleName) override - { - return true; - } - - /** - * @brief Obtains an array of all group IDs associated with the given bundle name and UID. - * @param bundleName Indicates the bundle name. - * @param uid Indicates the uid. - * @param gids Indicates the group IDs associated with the specified bundle. - * @return Returns true if the gids is successfully obtained; returns false otherwise. - */ - virtual bool GetBundleGidsByUid(const std::string &bundleName, const int &uid, std::vector &gids) override - { - return true; - } - - /** - * @brief Query the AbilityInfo by ability.uri in config.json. - * @param abilityUri Indicates the uri of the ability. - * @param abilityInfos Indicates the obtained AbilityInfos object. - * @return Returns true if the AbilityInfo is successfully obtained; returns false otherwise. - */ - virtual bool QueryAbilityInfosByUri(const std::string &abilityUri, std::vector &abilityInfos) override - { - return true; - } - - /** - * @brief Checks whether a specified bundle has been granted a specific permission. - * @param bundleName Indicates the name of the bundle to check. - * @param permission Indicates the permission to check. - * @param userId Indicates the user ID. - * @return Returns 0 if the bundle has the permission; returns -1 otherwise. - */ - virtual int CheckPermissionByUid( - const std::string &bundleName, const std::string &permission, const int userId) override - { - return 0; - } - - /** - * @brief Determine whether the application is in the allow list. - * @param bundleName Indicates the bundle Names. - * @return Returns true if bundle name in the allow list successfully; returns false otherwise. - */ - virtual bool CheckBundleNameInAllowList(const std::string &bundleName) override - { - return true; - } - - /** - * @brief Obtains the DistributedBundleInfo based on a given bundle name and networkId. - * @param networkId Indicates the networkId of remote device. - * @param bundleName Indicates the application bundle name to be queried. - * @param distributedBundleInfo Indicates the obtained DistributedBundleInfo object. - * @return Returns true if the DistributedBundleInfo is successfully obtained; returns false otherwise. - */ - virtual bool GetDistributedBundleInfo( - const std::string &networkId, const std::string &bundleName, - DistributedBundleInfo &distributedBundleInfo) override - { - return true; - } - -private: - bool isSystemApp_ = false; - bool isSystemAppMock_ = false; -}; -} // namespace EventFwk -} // namespace OHOS - -#endif // FOUNDATION_EVENT_CESFWK_SERVICES_TEST_UNITTEST_MOCK_INCLUDE_MOCK_BUNDLE_MANAGER_H \ No newline at end of file +/* + * Copyright (c) 2021-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. + */ + +#ifndef FOUNDATION_EVENT_CESFWK_SERVICES_TEST_UNITTEST_MOCK_INCLUDE_MOCK_BUNDLE_MANAGER_H +#define FOUNDATION_EVENT_CESFWK_SERVICES_TEST_UNITTEST_MOCK_INCLUDE_MOCK_BUNDLE_MANAGER_H + +#include + +#include "ability_info.h" +#include "application_info.h" +#include "bundle_mgr_host.h" +#include "bundle_mgr_interface.h" +#include "ohos/aafwk/content/want.h" +#include "iremote_proxy.h" +#include "iremote_stub.h" + +namespace OHOS { +namespace AppExecFwk { +class MockBundleMgrService : public BundleMgrHost { +public: + /** + * @brief Obtains the ApplicationInfo based on a given bundle name. + * @param appName Indicates the application bundle name to be queried. + * @param flag Indicates the flag used to specify information contained + * in the ApplicationInfo object that will be returned. + * @param userId Indicates the user ID. + * @param appInfo Indicates the obtained ApplicationInfo object. + * @return Returns true if the application is successfully obtained; returns false otherwise. + */ + virtual bool GetApplicationInfo( + const std::string &appName, const ApplicationFlag flag, const int userId, ApplicationInfo &appInfo) override + { + return true; + } + + /** + * @brief Obtains information about all installed applications of a specified user. + * @param flag Indicates the flag used to specify information contained + * in the ApplicationInfo objects that will be returned. + * @param userId Indicates the user ID. + * @param appInfos Indicates all of the obtained ApplicationInfo objects. + * @return Returns true if the application is successfully obtained; returns false otherwise. + */ + virtual bool GetApplicationInfos( + const ApplicationFlag flag, const int userId, std::vector &appInfos) override + { + return true; + } + + /** + * @brief Obtains the BundleInfo based on a given bundle name. + * @param bundleName Indicates the application bundle name to be queried. + * @param flag Indicates the information contained in the BundleInfo object to be returned. + * @param bundleInfo Indicates the obtained BundleInfo object. + * @param userId Indicates the user ID. + * @return Returns true if the BundleInfo is successfully obtained; returns false otherwise. + */ + virtual bool GetBundleInfo( + const std::string &bundleName, const BundleFlag flag, BundleInfo &bundleInfo, int32_t userId) override + { + return true; + } + + /** + * @brief Obtains BundleInfo of all bundles available in the system. + * @param flag Indicates the flag used to specify information contained in the BundleInfo that will be returned. + * @param bundleInfos Indicates all of the obtained BundleInfo objects. + * @param userId Indicates the user ID. + * @return Returns true if the BundleInfos is successfully obtained; returns false otherwise. + */ + virtual bool GetBundleInfos( + const BundleFlag flag, std::vector &bundleInfos, int32_t userId) override + { + return true; + } + + /** + * @brief Obtains the application UID based on the given bundle name and user ID. + * @param bundleName Indicates the bundle name of the application. + * @param userId Indicates the user ID. + * @return Returns the uid if successfully obtained; returns -1 otherwise. + */ + virtual int GetUidByBundleName(const std::string &bundleName, const int userId) override + { + return 0; + } + + /** + * @brief Obtains the bundle name of a specified application based on the given UID. + * @param uid Indicates the uid. + * @param bundleName Indicates the obtained bundle name. + * @return Returns true if the bundle name is successfully obtained; returns false otherwise. + */ + virtual bool GetBundleNameForUid(const int uid, std::string &bundleName) override + { + bundleName = "hello"; + return true; + } + + /** + * @brief Obtains an array of all group IDs associated with a specified bundle. + * @param bundleName Indicates the bundle name. + * @param gids Indicates the group IDs associated with the specified bundle. + * @return Returns true if the gids is successfully obtained; returns false otherwise. + */ + virtual bool GetBundleGids(const std::string &bundleName, std::vector &gids) override + { + return true; + } + + /** + * @brief Obtains the type of a specified application based on the given bundle name. + * @param bundleName Indicates the bundle name. + * @return Returns "system" if the bundle is a system application; returns "third-party" otherwise. + */ + virtual std::string GetAppType(const std::string &bundleName) override + { + return ""; + } + + /** + * @brief Check whether the app is system app by it's UID. + * @param uid Indicates the uid. + * @return Returns true if the bundle is a system application; returns false otherwise. + */ + virtual bool CheckIsSystemAppByUid(const int uid) override; + + /** + * @brief Obtains the BundleInfo of application bundles based on the specified metaData. + * @param metaData Indicates the metadata to get in the bundle. + * @param bundleInfos Indicates all of the obtained BundleInfo objects. + * @return Returns true if the BundleInfos is successfully obtained; returns false otherwise. + */ + virtual bool GetBundleInfosByMetaData(const std::string &metaData, std::vector &bundleInfos) override + { + return true; + } + + /** + * @brief Query the AbilityInfo by the given Want. + * @param want Indicates the information of the ability. + * @param abilityInfo Indicates the obtained AbilityInfo object. + * @return Returns true if the AbilityInfo is successfully obtained; returns false otherwise. + */ + virtual bool QueryAbilityInfo(const AAFwk::Want &want, AbilityInfo &abilityInfo) override + { + return true; + } + + /** + * @brief Query the AbilityInfo of list by the given Want. + * @param want Indicates the information of the ability. + * @param abilityInfos Indicates the obtained AbilityInfos object. + * @return Returns true if the AbilityInfos is successfully obtained; returns false otherwise. + */ + virtual bool QueryAbilityInfos(const Want &want, std::vector &abilityInfos) override + { + return true; + } + + /** + * @brief Query the AbilityInfo of list for clone by the given Want. + * @param want Indicates the information of the ability. + * @param abilityInfos Indicates the obtained AbilityInfos object. + * @return Returns true if the AbilityInfos is successfully obtained; returns false otherwise. + */ + virtual bool QueryAbilityInfosForClone(const Want &want, std::vector &abilityInfos) override + { + return true; + } + + /** + * @brief Query the AbilityInfo by ability.uri in config.json. + * @param abilityUri Indicates the uri of the ability. + * @param abilityInfo Indicates the obtained AbilityInfo object. + * @return Returns true if the AbilityInfo is successfully obtained; returns false otherwise. + */ + virtual bool QueryAbilityInfoByUri(const std::string &abilityUri, AbilityInfo &abilityInfo) override + { + return true; + } + + /** + * @brief Obtains the BundleInfo of all keep-alive applications in the system. + * @param bundleInfos Indicates all of the obtained BundleInfo objects. + * @return Returns true if the BundleInfos is successfully obtained; returns false otherwise. + */ + virtual bool QueryKeepAliveBundleInfos(std::vector &bundleInfos) override + { + return true; + } + + /** + * @brief Obtains information about an application bundle contained in an ohos Ability Package (HAP). + * @param hapFilePath Indicates the absolute file path of the HAP. + * @param flag Indicates the information contained in the BundleInfo object to be returned. + * @param bundleInfo Indicates the obtained BundleInfo object. + * @return Returns true if the BundleInfo is successfully obtained; returns false otherwise. + */ + virtual bool GetBundleArchiveInfo( + const std::string &hapFilePath, const BundleFlag flag, BundleInfo &bundleInfo) override + { + return true; + } + + /** + * @brief Obtain the HAP module info of a specific ability. + * @param abilityInfo Indicates the ability. + * @param hapModuleInfo Indicates the obtained HapModuleInfo object. + * @return Returns true if the HapModuleInfo is successfully obtained; returns false otherwise. + */ + virtual bool GetHapModuleInfo(const AbilityInfo &abilityInfo, HapModuleInfo &hapModuleInfo) override + { + return true; + } + + /** + * @brief Obtains the Want for starting the main ability of an application based on the given bundle name. + * @param bundleName Indicates the bundle name. + * @param want Indicates the obtained launch Want object. + * @return Returns true if the launch Want object is successfully obtained; returns false otherwise. + */ + virtual bool GetLaunchWantForBundle(const std::string &bundleName, Want &want) override + { + return true; + } + + /** + * @brief Confirms with the permission management module to check whether a request prompt is required for granting + * a certain permission. + * @param bundleName Indicates the name of the bundle to check. + * @param permission Indicates the permission to check. + * @param userId Indicates the user ID. + * @return Returns true if the current application does not have the permission and the user does not turn off + * further requests; returns false if the current application already has the permission, the permission is rejected + * by the system, or the permission is denied by the user and the user has turned off further requests. + */ + virtual bool CanRequestPermission( + const std::string &bundleName, const std::string &permissionName, const int userId) override + { + return true; + } + + /** + * @brief Checks whether the publickeys of two bundles are the same. + * @param firstBundleName Indicates the first bundle name. + * @param secondBundleName Indicates the second bundle name. + * @return Returns SIGNATURE_UNKNOWN_BUNDLE if at least one of the given bundles is not found; + * returns SIGNATURE_NOT_MATCHED if their publickeys are different; + * returns SIGNATURE_MATCHED if their publickeys are the same. + */ + virtual int CheckPublicKeys(const std::string &firstBundleName, const std::string &secondBundleName) override + { + return 0; + } + + /** + * @brief Checks whether a specified bundle has been granted a specific permission. + * @param bundleName Indicates the name of the bundle to check. + * @param permission Indicates the permission to check. + * @return Returns 0 if the bundle has the permission; returns -1 otherwise. + */ + virtual int CheckPermission(const std::string &bundleName, const std::string &permission) override; + /** + * @brief Obtains detailed information about a specified permission. + * @param permissionName Indicates the name of the ohos permission. + * @param permissionDef Indicates the object containing detailed information about the given ohos permission. + * @return Returns true if the PermissionDef object is successfully obtained; returns false otherwise. + */ + virtual bool GetPermissionDef(const std::string &permissionName, PermissionDef &permissionDef) override + { + return true; + } + + /** + * @brief Obtains all known permission groups in the system. + * @param permissionDefs Indicates the list of objects containing the permission group information. + * @return Returns true if the PermissionDef objects is successfully obtained; returns false otherwise. + */ + virtual bool GetAllPermissionGroupDefs(std::vector &permissionDefs) override + { + return true; + } + + /** + * @brief Obtains all known permission groups in the system. + * @param permissions Indicates the permission array. + * @param appNames Indicates the list of application names that have the specified permissions. + * @return Returns true if the application names is successfully obtained; returns false otherwise. + */ + virtual bool GetAppsGrantedPermissions( + const std::vector &permissions, std::vector &appNames) override + { + return true; + } + + /** + * @brief Requests a certain permission from user. + * @param bundleName Indicates the name of the bundle to request permission. + * @param permission Indicates the permission to request permission. + * @param userId Indicates the user ID. + * @return Returns true if the permission request successfully; returns false otherwise. + */ + virtual bool RequestPermissionFromUser( + const std::string &bundleName, const std::string &permission, const int userId) override + { + return true; + } + + /** + * @brief Checks whether the system has a specified capability. + * @param capName Indicates the name of the system feature to check. + * @return Returns true if the given feature specified by name is available in the system; returns false otherwise. + */ + virtual bool HasSystemCapability(const std::string &capName) override + { + return true; + } + + /** + * @brief Obtains the capabilities that are available in the system. + * @param systemCaps Indicates the list of capabilities available in the system. + * @return Returns true if capabilities in the system are successfully obtained; returns false otherwise. + */ + virtual bool GetSystemAvailableCapabilities(std::vector &systemCaps) override + { + return true; + } + + /** + * @brief Checks whether the current device has been started in safe mode. + * @return Returns true if the device is in safe mode; returns false otherwise. + */ + virtual bool IsSafeMode() override + { + return true; + } + + /** + * @brief Clears application running data of a specified application. + * @param bundleName Indicates the bundle name of the application whose data is to be cleared. + * @return Returns true if the data cleared successfully; returns false otherwise. + */ + virtual bool CleanBundleDataFiles(const std::string &bundleName, const int userId = 0) override + { + return true; + } + + /** + * @brief Register the specific bundle status callback. + * @param bundleStatusCallback Indicates the callback to be invoked for returning the bundle status changed result. + * @return Returns true if this function is successfully called; returns false otherwise. + */ + virtual bool RegisterBundleStatusCallback(const sptr &bundleStatusCallback) override + { + return true; + } + + /** + * @brief Clear the specific bundle status callback. + * @param bundleStatusCallback Indicates the callback to be cleared. + * @return Returns true if this function is successfully called; returns false otherwise. + */ + virtual bool ClearBundleStatusCallback(const sptr &bundleStatusCallback) override + { + return true; + } + + /** + * @brief Unregister all the callbacks of status changed. + * @return Returns true if this function is successfully called; returns false otherwise. + */ + virtual bool UnregisterBundleStatusCallback() override + { + return true; + } + + /** + * @brief Dump the bundle informations with specific flags. + * @param flag Indicates the information contained in the dump result. + * @param bundleName Indicates the bundle name if needed. + * @param userId Indicates the user ID. + * @param result Indicates the dump information result. + * @return Returns true if the dump result is successfully obtained; returns false otherwise. + */ + virtual bool DumpInfos( + const DumpFlag flag, const std::string &bundleName, int32_t userId, std::string &result) override + { + return true; + } + + /** + * @brief Checks whether a specified application is enabled. + * @param bundleName Indicates the bundle name of the application. + * @return Returns true if the application is enabled; returns false otherwise. + */ + virtual bool IsApplicationEnabled(const std::string &bundleName) override + { + return true; + } + + /** + * @brief Obtains the interface used to install and uninstall bundles. + * @return Returns a pointer to IBundleInstaller class if exist; returns nullptr otherwise. + */ + virtual sptr GetBundleInstaller() override + { + return nullptr; + } + + /** + * @brief Obtains the interface used to create or delete user. + * @return Returns a pointer to IBundleUserMgr class if exist; returns nullptr otherwise. + */ + virtual sptr GetBundleUserMgr() override + { + return nullptr; + } + + /** + * @brief Sets whether to enable a specified ability. + * @param abilityInfo Indicates information about the ability to check. + * @return Returns true if the ability is enabled; returns false otherwise. + */ + virtual bool IsAbilityEnabled(const AbilityInfo &abilityInfo) override + { + return true; + } + + /** + * @brief Obtains all bundle names of a specified application based on the given application UID. + * @param uid Indicates the uid. + * @param bundleNames Indicates the obtained bundle names. + * @return Returns true if the bundle names is successfully obtained; returns false otherwise. + */ + virtual bool GetBundlesForUid(const int uid, std::vector &bundleNames) override + { + return true; + } + + /** + * @brief Obtains the formal name associated with the given UID. + * @param uid Indicates the uid. + * @param name Indicates the obtained formal name. + * @return Returns true if the formal name is successfully obtained; returns false otherwise. + */ + virtual bool GetNameForUid(const int uid, std::string &name) override + { + return true; + } + + /** + * @brief Registers a callback for listening for permission changes of all UIDs. + * @param callback Indicates the callback method to register. + * @return Returns true if this function is successfully called; returns false otherwise. + */ + virtual bool RegisterAllPermissionsChanged(const sptr &callback) override + { + return true; + } + + /** + * @brief Registers a callback for listening for permission changes of specified UIDs. + * @param uids Indicates the list of UIDs whose permission changes will be monitored. + * @param callback Indicates the callback method to register. + * @return Returns true if this function is successfully called; returns false otherwise. + */ + virtual bool RegisterPermissionsChanged( + const std::vector &uids, const sptr &callback) override + { + return true; + } + + /** + * @brief Unregisters a specified callback for listening for permission changes. + * @param callback Indicates the callback method to register. + * @return Returns true if this function is successfully called; returns false otherwise. + */ + virtual bool UnregisterPermissionsChanged(const sptr &callback) override + { + return true; + } + + /** + * @brief Obtains the application ID based on the given bundle name and user ID. + * @param bundleName Indicates the bundle name of the application. + * @param userId Indicates the user ID. + * @return Returns the application ID if successfully obtained; returns empty string otherwise. + */ + virtual std::string GetAppIdByBundleName(const std::string &bundleName, const int userId) override + { + return ""; + } + + /** + * @brief Obtains the FormInfo objects provided by all applications on the device. + * @param formInfo List of FormInfo objects if obtained; returns an empty List if no FormInfo is available on the + * device. + * @return Returns true if this function is successfully called; returns false otherwise. + */ + virtual bool GetAllFormsInfo(std::vector &formInfos) override + { + return true; + } + + /** + * @brief Obtains the FormInfo objects provided by a specified application on the device. + * @param bundleName Indicates the bundle name of the application. + * @param formInfo List of FormInfo objects if obtained; returns an empty List if no FormInfo is available on the + * device. + * @return Returns true if this function is successfully called; returns false otherwise. + */ + virtual bool GetFormsInfoByApp(const std::string &bundleName, std::vector &formInfos) override + { + return true; + } + + /** + * @brief Obtains the FormInfo objects provided by a specified module name. + * @param formInfo List of FormInfo objects if obtained; returns an empty List if no FormInfo is available on the + * device. + * @param moduleName Indicates the module name of the application. + * @param bundleName Indicates the bundle name of the application. + * @return Returns true if this function is successfully called; returns false otherwise. + */ + virtual bool GetFormsInfoByModule( + const std::string &bundleName, const std::string &moduleName, std::vector &formInfos) override + { + return true; + } + + /** + * @brief Obtains the ShortcutInfo objects provided by a specified application on the device. + * @param bundleName Indicates the bundle name of the application. + * @param shortcutInfos List of ShortcutInfo objects if obtained. + * @return Returns true if this function is successfully called; returns false otherwise. + */ + virtual bool GetShortcutInfos(const std::string &bundleName, std::vector &shortcutInfos) override + { + return true; + } + + /** + * @brief Obtains the CommonEventInfo objects provided by an event key on the device. + * @param eventKey Indicates the event of the subscribe. + * @param commonEventInfos List of CommonEventInfo objects if obtained. + * @return Returns true if this function is successfully called; returns false otherwise. + */ + virtual bool GetAllCommonEventInfo(const std::string &eventKey, + std::vector &commonEventInfos) override + { + return true; + } + + /** + * @brief Get module usage record list in descending order of lastLaunchTime. + * @param maxNum the return size of the records, must be in range of 1 to 1000. + * @param moduleUsageRecords List of ModuleUsageRecord objects if obtained. + * @return Returns true if this function is successfully called; returns false otherwise. + */ + virtual bool GetModuleUsageRecords( + const int32_t number, std::vector &moduleUsageRecords) override + { + return true; + } + + /** + * @brief Notify a specified ability for ability. + * @param bundleName Indicates the bundle name of the ability to ability. + * @param abilityName Indicates the name of the ability to ability. + * @param launchTime Indicates the ability launchTime. + * @return Returns true if this function is successfully called; returns false otherwise. + */ + virtual bool NotifyAbilityLifeStatus(const std::string &bundleName, + const std::string &abilityName, const int64_t launchTime, const int uid) override + { + return true; + } + + void MockSetIsSystemApp(bool isSystemApp); + +private: + bool isSystemApp_ = false; + bool isSystemAppMock_ = false; +}; +} // namespace AppExecFwk +} // namespace OHOS + +#endif // FOUNDATION_EVENT_CESFWK_SERVICES_TEST_UNITTEST_MOCK_INCLUDE_MOCK_BUNDLE_MANAGER_H diff --git a/test/mock/include/mock_ipc_skeleton.h b/test/mock/include/mock_ipc_skeleton.h new file mode 100644 index 0000000..d47bdcd --- /dev/null +++ b/test/mock/include/mock_ipc_skeleton.h @@ -0,0 +1,61 @@ +/* + * 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. + */ + +#ifndef OHOS_MOCK_IPC_SKELETON_H +#define OHOS_MOCK_IPC_SKELETON_H + +#include "iremote_object.h" + +namespace OHOS { +class IPCSkeleton { +public: + IPCSkeleton() = default; + ~IPCSkeleton() = default; + + // default max is 4, only if you need a customize value + static bool SetMaxWorkThreadNum(int maxThreadNum); + + // join current thread into work loop. + static void JoinWorkThread(); + + // remove current thread from work loop. + static void StopWorkThread(); + + static pid_t GetCallingPid(); + + static pid_t GetCallingUid(); + + static std::string GetLocalDeviceID(); + + static std::string GetCallingDeviceID(); + + static bool IsLocalCalling(); + + static IPCSkeleton &GetInstance(); + + static sptr GetContextObject(); + + static bool SetContextObject(sptr &object); + + static int FlushCommands(IRemoteObject *object); + + static std::string ResetCallingIdentity(); + + static bool SetCallingIdentity(std::string &identity); + + static void SetCallingUid(pid_t uid); +}; +} // namespace OHOS +#endif // OHOS_MOCK_IPC_SKELETON_H diff --git a/services/test/unittest/mock/src/mock_access_token_helper.cpp b/test/mock/mock_access_token_helper.cpp similarity index 84% rename from services/test/unittest/mock/src/mock_access_token_helper.cpp rename to test/mock/mock_access_token_helper.cpp index 442eb79..d004b02 100644 --- a/services/test/unittest/mock/src/mock_access_token_helper.cpp +++ b/test/mock/mock_access_token_helper.cpp @@ -1,37 +1,34 @@ -/* - * 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 "access_token_helper.h" -#include "event_log_wrapper.h" - -namespace OHOS { -namespace EventFwk { -using namespace OHOS::Security; - -constexpr unsigned int PERMISSION_GRANTED = 1; - -bool AccessTokenHelper::VerifyNativeToken(const AccessToken::AccessTokenID &callerToken) -{ - EVENT_LOGD("MockAccessTokenHelper::VerifyNativeToken"); - return callerToken == PERMISSION_GRANTED; -} - -int AccessTokenHelper::VerifyAccessToken(const AccessToken::AccessTokenID &callerToken, const std::string &permission) -{ - EVENT_LOGD("MockAccessTokenHelper::VerifyAccessToken"); - return callerToken != PERMISSION_GRANTED; -} -} // namespace EventFwk -} // namespace OHOS +/* + * 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 "access_token_helper.h" + +namespace OHOS { +namespace EventFwk { +using namespace OHOS::Security; + +constexpr unsigned int PERMISSION_GRANTED = 1; + +bool AccessTokenHelper::VerifyNativeToken(const AccessToken::AccessTokenID &callerToken) +{ + return callerToken == PERMISSION_GRANTED; +} + +int AccessTokenHelper::VerifyAccessToken(const AccessToken::AccessTokenID &callerToken, const std::string &permission) +{ + return callerToken == PERMISSION_GRANTED; +} +} // namespace EventFwk +} // namespace OHOS diff --git a/services/test/mock/src/mock_bundle_manager.cpp b/test/mock/mock_bundle_manager.cpp similarity index 90% rename from services/test/mock/src/mock_bundle_manager.cpp rename to test/mock/mock_bundle_manager.cpp index d54e5d3..6b2170b 100644 --- a/services/test/mock/src/mock_bundle_manager.cpp +++ b/test/mock/mock_bundle_manager.cpp @@ -1,49 +1,47 @@ -/* - * Copyright (c) 2021-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_bundle_manager.h" -#include "ability_info.h" -#include "application_info.h" - -namespace OHOS { -namespace EventFwk { -constexpr int SYSTEM_UID = 1000; - -void MockBundleMgrService::MockSetIsSystemApp(bool isSystemApp) -{ - isSystemAppMock_ = true; - isSystemApp_ = isSystemApp; -} - -bool MockBundleMgrService::CheckIsSystemAppByUid(const int uid) -{ - EVENT_LOGD("MockBundleMgrService::CheckIsSystemAppByUid"); - if (isSystemAppMock_) { - return isSystemApp_; - } - return (uid > SYSTEM_UID) ? false : true; -} - -int MockBundleMgrService::CheckPermission(const std::string &bundleName, const std::string &permission) -{ - EVENT_LOGD("MockBundleMgrService::CheckPermission"); - if (!bundleName.compare("hello")) { - return 0; - } else { - return -1; - } -} -} // namespace AppExecFwk +/* + * Copyright (c) 2021-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_bundle_manager.h" +#include "ability_info.h" +#include "application_info.h" + +namespace OHOS { +namespace AppExecFwk { +constexpr int SYSTEM_UID = 1000; + +void MockBundleMgrService::MockSetIsSystemApp(bool isSystemApp) +{ + isSystemAppMock_ = true; + isSystemApp_ = isSystemApp; +} + +bool MockBundleMgrService::CheckIsSystemAppByUid(const int uid) +{ + if (isSystemAppMock_) { + return isSystemApp_; + } + return (uid > SYSTEM_UID) ? false : true; +} + +int MockBundleMgrService::CheckPermission(const std::string &bundleName, const std::string &permission) +{ + if (!bundleName.compare("hello")) { + return 0; + } else { + return -1; + } +} +} // namespace AppExecFwk } // namespace OHOS \ No newline at end of file diff --git a/test/mock/mock_ipc.cpp b/test/mock/mock_ipc.cpp new file mode 100644 index 0000000..58c9674 --- /dev/null +++ b/test/mock/mock_ipc.cpp @@ -0,0 +1,39 @@ +/* + * 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_ipc_skeleton.h" + +namespace OHOS { +#ifdef CONFIG_IPC_SINGLE +using namespace IPC_SINGLE; +#endif + +pid_t uid_ = 1000; + +pid_t IPCSkeleton::GetCallingUid() +{ + return uid_; +} + +pid_t IPCSkeleton::GetCallingPid() +{ + return 1; +} + +void IPCSkeleton::SetCallingUid(pid_t uid) +{ + uid_ = uid; +} +} // namespace OHOS