diff --git a/bundle.json b/bundle.json index ebbd5d0989ec6a50fa5d061c07a1b39d7bb11646..de90867b4846157511478d74907d0036f38a3410 100644 --- a/bundle.json +++ b/bundle.json @@ -88,6 +88,7 @@ "//base/notification/ans_standard/frameworks/ans/test/moduletest:moduletest", "//base/notification/ans_standard/frameworks/ans/native/test/unittest:unittest", "//base/notification/ans_standard/test:systemtest", + "//base/notification/ans_standard/test/bechmarktest:benchmarktest", "//base/notification/ans_standard/test/fuzztest:fuzztest" ] } diff --git a/test/bechmarktest/BUILD.gn b/test/bechmarktest/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..8c70f9303876f2acacc5c3a6fd962ae221b5ffaf --- /dev/null +++ b/test/bechmarktest/BUILD.gn @@ -0,0 +1,29 @@ +# 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("//build/ohos.gni") + +config("bechmark_test_config") { + include_dirs = [ "//third_party/json/include" ] + + configs = [] +} + +group("benchmarktest") { + testonly = true + + deps = [ + "notification_publish_test:benchmarktest", + "notification_services_test:benchmarktest", + ] +} diff --git a/test/bechmarktest/notification_publish_test/BUILD.gn b/test/bechmarktest/notification_publish_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..89201bd7bd0b525da9df9d73219e61d9a6721b77 --- /dev/null +++ b/test/bechmarktest/notification_publish_test/BUILD.gn @@ -0,0 +1,55 @@ +# 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/ans_standard/notification.gni") +import("//build/test.gni") + +module_output_path = "ans_standard/benchmarktest" + +ohos_benchmarktest("Benchmark_Publish_Test") { + module_out_path = module_output_path + include_dirs = [ + "${interfaces_path}/ans/native/include", + "${services_path}/ans/include", + ] + + sources = [ "notification_publish_test.cpp" ] + + deps = [ + "${frameworks_path}/ans/core:ans_core", + "${frameworks_path}/ans/native:ans_innerkits", + "//third_party/benchmark:benchmark", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:wantagent_innerkits", + "multimedia_image_standard:image_native", + "native_appdatamgr:native_rdb", + ] + subsystem_name = "notification" + part_name = "ans_standard" +} + +group("benchmarktest") { + testonly = true + deps = [] + + deps += [ + # deps file + ":Benchmark_Publish_Test", + ] +} diff --git a/test/bechmarktest/notification_publish_test/notification_publish_test.cpp b/test/bechmarktest/notification_publish_test/notification_publish_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..02a3485802b9500d78cb49dd9c9036aee0ea3bc0 --- /dev/null +++ b/test/bechmarktest/notification_publish_test/notification_publish_test.cpp @@ -0,0 +1,199 @@ +/* + * 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 "notification_action_button.h" +#include "notification_request.h" +#include "notification_content.h" +#include "notification_helper.h" +#include "want_agent_helper.h" +#include "want_agent_info.h" + +using namespace OHOS; +using namespace OHOS::Notification; +using namespace OHOS::AbilityRuntime; + +namespace { +class BenchmarkNotificationPublish : public benchmark::Fixture { +public: + BenchmarkNotificationPublish() + { + Iterations(iterations); + Repetitions(repetitions); + ReportAggregatesOnly(); + + InitTextRequest(); + InitWantAgentRequest(); + InitButtonRequest(); + } + + virtual ~BenchmarkNotificationPublish() override = default; + + void SetUp(const ::benchmark::State &state) override + {} + + void TearDown(const ::benchmark::State &state) override + {} + +protected: + void InitTextRequest(); + void InitWantAgentRequest(); + void InitButtonRequest(); + + const int32_t repetitions = 1; + const int32_t iterations = 100; + + NotificationRequest req_; + NotificationRequest reqWantAgent_; + NotificationRequest reqButton_; +}; + +void BenchmarkNotificationPublish::InitTextRequest() +{ + std::shared_ptr mediaContent = std::make_shared(); + std::shared_ptr content = std::make_shared(mediaContent); + req_.SetContent(content); + req_.SetSlotType(NotificationConstant::OTHER); + std::vector style; + style.push_back("style"); + req_.SetNotificationUserInputHistory(style); + req_.SetOwnerBundleName("bundleName"); + req_.SetLabel("Text"); +} + +void BenchmarkNotificationPublish::InitWantAgentRequest() +{ + std::shared_ptr mediaContent = std::make_shared(); + std::shared_ptr content = std::make_shared(mediaContent); + AbilityRuntime::WantAgent::WantAgentInfo paramsInfo; + std::shared_ptr wantAgent = + AbilityRuntime::WantAgent::WantAgentHelper::GetWantAgent(paramsInfo); + reqWantAgent_.SetContent(content); + reqWantAgent_.SetSlotType(NotificationConstant::OTHER); + reqWantAgent_.SetWantAgent(wantAgent); + reqWantAgent_.SetOwnerBundleName("bundleName"); + reqWantAgent_.SetLabel("Text"); +} + +void BenchmarkNotificationPublish::InitButtonRequest() +{ + std::shared_ptr mediaContent = std::make_shared(); + std::shared_ptr content = std::make_shared(mediaContent); + AbilityRuntime::WantAgent::WantAgentInfo paramsInfo; + std::shared_ptr wantAgent = + AbilityRuntime::WantAgent::WantAgentHelper::GetWantAgent(paramsInfo); + std::shared_ptr actionButton = + NotificationActionButton::Create(nullptr, "title", wantAgent); + std::shared_ptr onlyUserInput = NotificationUserInput::Create("onlyUserInputKey"); + AAFwk::WantParams additional; + actionButton->AddAdditionalData(additional); + actionButton->SetSemanticActionButton(NotificationConstant::NONE_ACTION_BUTTON); + actionButton->SetAutoCreatedReplies(true); + actionButton->AddMimeTypeOnlyUserInput(onlyUserInput); + actionButton->SetContextDependent(true); + reqButton_.SetContent(content); + reqButton_.SetSlotType(NotificationConstant::OTHER); + reqButton_.AddActionButton(actionButton); + reqButton_.SetOwnerBundleName("bundleName"); + reqButton_.SetLabel("Text"); +} + +/** +* @tc.name: PublishNotificationTestCase001 +* @tc.desc: publish text notifcation +* @tc.type: FUNC +* @tc.require: +*/ +BENCHMARK_F(BenchmarkNotificationPublish, PublishNotificationTestCase001)(benchmark::State &state) +{ + int id = 0; + while (state.KeepRunning()) { + req_.SetNotificationId(id++); + NotificationHelper::PublishNotification(req_); + } +} + +/** +* @tc.name: CancelNotificationTestCase +* @tc.desc: cancel notification +* @tc.type: FUNC +* @tc.require: +*/ +BENCHMARK_F(BenchmarkNotificationPublish, CancelNotificationTestCase)(benchmark::State &state) +{ + int id = 100; + while (state.KeepRunning()) { + id--; + NotificationHelper::CancelNotification(id); + } +} + +/** +* @tc.name: PublishNotificationTestCase002 +* @tc.desc: publish wantAgent notifcation +* @tc.type: FUNC +* @tc.require: +*/ +BENCHMARK_F(BenchmarkNotificationPublish, PublishNotificationTestCase002)(benchmark::State &state) +{ + int id = 0; + while (state.KeepRunning()) { + reqWantAgent_.SetNotificationId(id++); + NotificationHelper::PublishNotification(reqWantAgent_); + } +} + +BENCHMARK_REGISTER_F(BenchmarkNotificationPublish, CancelNotificationTestCase)->Iterations(100)-> + Repetitions(1)->ReportAggregatesOnly(); + +/** +* @tc.name: PublishNotificationTestCase003 +* @tc.desc: publish wantAgent notifcation +* @tc.type: FUNC +* @tc.require: +*/ +BENCHMARK_F(BenchmarkNotificationPublish, PublishNotificationTestCase003)(benchmark::State &state) +{ + int id = 0; + while (state.KeepRunning()) { + reqButton_.SetNotificationId(id++); + NotificationHelper::PublishNotification(reqButton_); + } +} + +BENCHMARK_REGISTER_F(BenchmarkNotificationPublish, CancelNotificationTestCase)->Iterations(100)-> + Repetitions(1)->ReportAggregatesOnly(); + +/** +* @tc.name: RemoveNotificationTestCase +* @tc.desc: remove notification +* @tc.type: FUNC +* @tc.require: +*/ +BENCHMARK_F(BenchmarkNotificationPublish, RemoveNotificationTestCase)(benchmark::State &state) +{ + int id = 100; + while (state.KeepRunning()) { + id--; + NotificationHelper::RemoveNotifications(); + } +} +} + +// Run the benchmark +BENCHMARK_MAIN(); \ No newline at end of file diff --git a/test/bechmarktest/notification_services_test/BUILD.gn b/test/bechmarktest/notification_services_test/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..ad31455aae62e011d9d801f9e317440a6ff039eb --- /dev/null +++ b/test/bechmarktest/notification_services_test/BUILD.gn @@ -0,0 +1,62 @@ +# 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/ans_standard/notification.gni") +import("//build/test.gni") + +module_output_path = "ans_standard/benchmarktest" + +ohos_benchmarktest("Benchmark_Notification_Servece_Test") { + module_out_path = module_output_path + include_dirs = [ + "include", + "mock/include", + "${services_path}/ans/include", + ] + + sources = [ + "mock/mock_bundle_manager_helper.cpp", + "mock/mock_ipc.cpp", + "notification_service_test.cpp", + ] + + deps = [ + "${frameworks_path}/ans/core:ans_core", + "${frameworks_path}/ans/native:ans_innerkits", + "${services_path}/ans:libans", + "//third_party/benchmark:benchmark", + "//third_party/googletest:gtest_main", + "//utils/native/base:utils", + ] + + external_deps = [ + "ability_base:want", + "ability_base:zuri", + "ability_runtime:wantagent_innerkits", + "ces_standard:cesfwk_innerkits", + "multimedia_image_standard:image_native", + "native_appdatamgr:native_rdb", + ] + subsystem_name = "notification" + part_name = "ans_standard" +} + +group("benchmarktest") { + testonly = true + deps = [] + + deps += [ + # deps file + ":Benchmark_Notification_Servece_Test", + ] +} diff --git a/test/bechmarktest/notification_services_test/mock/include/mock_ipc_skeleton.h b/test/bechmarktest/notification_services_test/mock/include/mock_ipc_skeleton.h new file mode 100644 index 0000000000000000000000000000000000000000..a954c7e984fcb213bbf424da29454bba3de76e6d --- /dev/null +++ b/test/bechmarktest/notification_services_test/mock/include/mock_ipc_skeleton.h @@ -0,0 +1,61 @@ +/* + * 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. + */ + +#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/test/bechmarktest/notification_services_test/mock/mock_bundle_manager_helper.cpp b/test/bechmarktest/notification_services_test/mock/mock_bundle_manager_helper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..72e792b407396ec86ebf7222fdacd6e59f4aa9b8 --- /dev/null +++ b/test/bechmarktest/notification_services_test/mock/mock_bundle_manager_helper.cpp @@ -0,0 +1,68 @@ +/* + * 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 "bundle_manager_helper.h" + +namespace OHOS { +namespace Notification { +BundleManagerHelper::BundleManagerHelper() +{} + +BundleManagerHelper::~BundleManagerHelper() +{} + +void BundleManagerHelper::OnRemoteDied(const wptr &object) +{} + +std::string BundleManagerHelper::GetBundleNameByUid(int uid) +{ + return "bundleName"; +} + +int BundleManagerHelper::GetDefaultUidByBundleName(const std::string &bundle, const int32_t userId) +{ + return 10000; +} + +bool BundleManagerHelper::IsSystemApp(int uid) +{ + return true; +} + +bool BundleManagerHelper::CheckApiCompatibility(const sptr &bundleOption) +{ + return true; +} + +bool BundleManagerHelper::GetBundleInfoByBundleName( + const std::string bundle, const int32_t userId, AppExecFwk::BundleInfo &bundleInfo) +{ + return true; +} + +void BundleManagerHelper::Connect() +{} + +void BundleManagerHelper::Disconnect() +{} + +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED +bool BundleManagerHelper::GetDistributedNotificationEnabled(const std::string &bundleName, const int userId) +{ + return true; +} +#endif +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/test/bechmarktest/notification_services_test/mock/mock_ipc.cpp b/test/bechmarktest/notification_services_test/mock/mock_ipc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e533d584f9b55b9ada190103f22a6ba1139e71f1 --- /dev/null +++ b/test/bechmarktest/notification_services_test/mock/mock_ipc.cpp @@ -0,0 +1,39 @@ +/* + * 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. + */ + +#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 diff --git a/test/bechmarktest/notification_services_test/notification_service_test.cpp b/test/bechmarktest/notification_services_test/notification_service_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2cb35a80ac6d858ab725cd40b01555b5b1588b10 --- /dev/null +++ b/test/bechmarktest/notification_services_test/notification_service_test.cpp @@ -0,0 +1,444 @@ +/* + * 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 + +#define private public +#include "advanced_notification_service.h" +#include "ans_const_define.h" +#include "ans_inner_errors.h" +#include "mock_ipc_skeleton.h" +#include "notification.h" +#include "notification_subscriber.h" + + +using namespace OHOS; +using namespace OHOS::Notification; +using namespace OHOS::AbilityRuntime; + +namespace { +class TestAnsSubscriber : public NotificationSubscriber { +public: + void OnConnected() override + {} + void OnDisconnected() override + {} + void OnDied() override + {} + void OnUpdate(const std::shared_ptr &sortingMap) override + {} + void OnDoNotDisturbDateChange(const std::shared_ptr &date) override + {} + void OnEnabledNotificationChanged( + const std::shared_ptr &callbackData) override + {} + void OnCanceled(const std::shared_ptr &request) override + {} + void OnCanceled(const std::shared_ptr &request, + const std::shared_ptr &sortingMap, int deleteReason) override + {} + void OnConsumed(const std::shared_ptr &request) override + {} + void OnConsumed(const std::shared_ptr &request, + const std::shared_ptr &sortingMap) override + {} +}; + +class BenchmarkNotificationService : public benchmark::Fixture { +public: + BenchmarkNotificationService() + { + Iterations(iterations); + Repetitions(repetitions); + ReportAggregatesOnly(); + } + + virtual ~BenchmarkNotificationService() override = default; + + void SetUp(const ::benchmark::State &state) override + {} + void TearDown(const ::benchmark::State &state) override + {} + +protected: + const int32_t repetitions = 3; + const int32_t iterations = 100; + static sptr advancedNotificationService_; +}; + +sptr BenchmarkNotificationService::advancedNotificationService_ = + AdvancedNotificationService::GetInstance(); + +/** + * @tc.name: AddSlotTestCase + * @tc.desc: AddSlot + * @tc.type: FUNC + * @tc.require: + */ +BENCHMARK_F(BenchmarkNotificationService, AddSlotTestCase)(benchmark::State &state) +{ + std::vector> slots; + sptr slot = new NotificationSlot(NotificationConstant::SlotType::CUSTOM); + slots.push_back(slot); + while (state.KeepRunning()) { + ErrCode errCode = advancedNotificationService_->AddSlots(slots); + if (errCode != ERR_OK) { + state.SkipWithError("AddSlotTestCase failed."); + } + } +} + +/** + * @tc.name: RemoveSlotByTypeTestCase + * @tc.desc: RemoveSlotByType + * @tc.type: FUNC + * @tc.require: + */ +BENCHMARK_F(BenchmarkNotificationService, RemoveSlotByTypeTestCase)(benchmark::State &state) +{ + std::vector> slots; + sptr slot = new NotificationSlot(NotificationConstant::SlotType::CUSTOM); + slots.push_back(slot); + while (state.KeepRunning()) { + ErrCode errCode = advancedNotificationService_->AddSlots(slots); + if (errCode != ERR_OK) { + state.SkipWithError("RemoveSlotByTypeTestCase add failed."); + } + + errCode = advancedNotificationService_->RemoveSlotByType(NotificationConstant::SlotType::CUSTOM); + if (errCode != ERR_OK) { + state.SkipWithError("RemoveSlotByTypeTestCase remove failed."); + } + } +} + +/** + * @tc.name: AddSlotGroupTestCase + * @tc.desc: AddSlotGroup + * @tc.type: FUNC + * @tc.require: + */ +BENCHMARK_F(BenchmarkNotificationService, AddSlotGroupTestCase)(benchmark::State &state) +{ + std::vector> groups; + sptr group = new NotificationSlotGroup("id0", "name0"); + groups.push_back(group); + while (state.KeepRunning()) { + ErrCode errCode = advancedNotificationService_->AddSlotGroups(groups); + if (errCode != ERR_OK) { + state.SkipWithError("AddSlotGroupTestCase failed."); + } + } +} + +/** + * @tc.name: RemoveSlotGroupsTestCase + * @tc.desc: RemoveSlotGroups + * @tc.type: FUNC + * @tc.require: + */ +BENCHMARK_F(BenchmarkNotificationService, RemoveSlotGroupsTestCase)(benchmark::State &state) +{ + std::vector> groups; + sptr group = new NotificationSlotGroup("id0", "name0"); + groups.push_back(group); + std::vector groupIds; + groupIds.push_back("id0"); + + while (state.KeepRunning()) { + ErrCode errCode = advancedNotificationService_->AddSlotGroups(groups); + if (errCode != ERR_OK) { + state.SkipWithError("RemoveSlotGroupsTestCase add failed."); + } + + errCode = advancedNotificationService_->RemoveSlotGroups(groupIds); + if (errCode != ERR_OK) { + state.SkipWithError("RemoveSlotGroupsTestCase remove failed."); + } + } +} + +/** + * @tc.name: SubscribeTestCase + * @tc.desc: Subscribe + * @tc.type: FUNC + * @tc.require: + */ +BENCHMARK_F(BenchmarkNotificationService, SubscribeTestCase)(benchmark::State &state) +{ + auto subscriber = new TestAnsSubscriber(); + sptr info = new NotificationSubscribeInfo(); + while (state.KeepRunning()) { + ErrCode errCode = advancedNotificationService_->Subscribe(subscriber->GetImpl(), info); + if (errCode != ERR_OK) { + state.SkipWithError("SubscribeTestCase failed."); + } + } +} + +/** + * @tc.name: PublishNotificationTestCase001 + * @tc.desc: Publish a normal text type notification. + * @tc.type: FUNC + * @tc.require: + */ +BENCHMARK_F(BenchmarkNotificationService, PublishNotificationTestCase001)(benchmark::State &state) +{ + sptr req = new (std::nothrow) NotificationRequest(1); + EXPECT_NE(req, nullptr); + req->SetSlotType(NotificationConstant::SlotType::OTHER); + req->SetLabel("req's label"); + std::string label = "publish's label"; + std::shared_ptr normalContent = std::make_shared(); + EXPECT_NE(normalContent, nullptr); + normalContent->SetText("normalContent's text"); + normalContent->SetTitle("normalContent's title"); + std::shared_ptr content = std::make_shared(normalContent); + EXPECT_NE(content, nullptr); + req->SetContent(content); + + while (state.KeepRunning()) { + ErrCode errCode = advancedNotificationService_->Publish(label, req); + if (errCode != ERR_OK) { + state.SkipWithError("PublishNotificationTestCase001 failed."); + } + } +} + +/** + * @tc.name: CancelNotificationTestCase001 + * @tc.desc: Cancel a normal text type notification. + * @tc.type: FUNC + * @tc.require: + */ +BENCHMARK_F(BenchmarkNotificationService, CancelNotificationTestCase001)(benchmark::State &state) +{ + sptr req = new (std::nothrow) NotificationRequest(0); + EXPECT_NE(req, nullptr); + req->SetSlotType(NotificationConstant::SlotType::OTHER); + req->SetLabel("req's label"); + std::string label = "publish's label"; + std::shared_ptr normalContent = std::make_shared(); + EXPECT_NE(normalContent, nullptr); + normalContent->SetText("normalContent's text"); + normalContent->SetTitle("normalContent's title"); + std::shared_ptr content = std::make_shared(normalContent); + EXPECT_NE(content, nullptr); + req->SetContent(content); + + int id = 0; + while (state.KeepRunning()) { + req->SetNotificationId(id); + ErrCode errCode = advancedNotificationService_->Publish(label, req); + if (errCode != ERR_OK) { + state.SkipWithError("CancelNotificationTestCase001 publish failed."); + } + advancedNotificationService_->Cancel(id, label); + id++; + } +} + +/** + * @tc.name: SetNotificationBadgeNumTestCase + * @tc.desc: SetNotificationBadgeNum + * @tc.type: FUNC + * @tc.require: + */ +BENCHMARK_F(BenchmarkNotificationService, SetNotificationBadgeNumTestCase)(benchmark::State &state) +{ + while (state.KeepRunning()) { + ErrCode errCode = advancedNotificationService_->SetNotificationBadgeNum(2); + if (errCode != ERR_OK) { + state.SkipWithError("SetNotificationBadgeNumTestCase failed."); + } + } +} + +/** + * @tc.name: GetBundleImportanceTestCase + * @tc.desc: GetBundleImportance + * @tc.type: FUNC + * @tc.require: + */ +BENCHMARK_F(BenchmarkNotificationService, GetBundleImportanceTestCase)(benchmark::State &state) +{ + int importance = 0; + while (state.KeepRunning()) { + ErrCode errCode = advancedNotificationService_->GetBundleImportance(importance); + if (errCode != ERR_OK) { + state.SkipWithError("GetBundleImportanceTestCase failed."); + } + } +} + +/** + * @tc.name: SetPrivateNotificationsAllowedTestCase + * @tc.desc: SetPrivateNotificationsAllowed + * @tc.type: FUNC + * @tc.require: + */ +BENCHMARK_F(BenchmarkNotificationService, SetPrivateNotificationsAllowedTestCase)(benchmark::State &state) +{ + while (state.KeepRunning()) { + ErrCode errCode = advancedNotificationService_->SetPrivateNotificationsAllowed(true); + if (errCode != ERR_OK) { + state.SkipWithError("SetPrivateNotificationsAllowedTestCase failed."); + } + } +} + +/** + * @tc.name: GetPrivateNotificationsAllowedTestCase + * @tc.desc: GetPrivateNotificationsAllowed + * @tc.type: FUNC + * @tc.require: + */ +BENCHMARK_F(BenchmarkNotificationService, GetPrivateNotificationsAllowedTestCase)(benchmark::State &state) +{ + while (state.KeepRunning()) { + ErrCode errCode = advancedNotificationService_->SetPrivateNotificationsAllowed(true); + if (errCode != ERR_OK) { + state.SkipWithError("GetPrivateNotificationsAllowed set failed."); + } + + bool allow = false; + errCode = advancedNotificationService_->GetPrivateNotificationsAllowed(allow); + if (!allow || errCode != ERR_OK) { + state.SkipWithError("GetPrivateNotificationsAllowed get failed."); + } + } +} + +/** + * @tc.name: SetShowBadgeEnabledForBundleTestCase + * @tc.desc: SetShowBadgeEnabledForBundle + * @tc.type: FUNC + * @tc.require: + */ +BENCHMARK_F(BenchmarkNotificationService, SetShowBadgeEnabledForBundleTestCase)(benchmark::State &state) +{ + sptr bundleOption = new NotificationBundleOption("bundleName", 1000); + while (state.KeepRunning()) { + ErrCode errCode = advancedNotificationService_->SetShowBadgeEnabledForBundle(bundleOption, true); + if (errCode != ERR_OK) { + state.SkipWithError("SetShowBadgeEnabledForBundleTestCase failed."); + } + } +} + +/** + * @tc.name: GetShowBadgeEnabledForBundleTestCase + * @tc.desc: GetShowBadgeEnabledForBundle + * @tc.type: FUNC + * @tc.require: + */ +BENCHMARK_F(BenchmarkNotificationService, GetShowBadgeEnabledForBundleTestCase)(benchmark::State &state) +{ + sptr bundleOption = new NotificationBundleOption("bundleName", 1000); + while (state.KeepRunning()) { + ErrCode errCode = advancedNotificationService_->SetShowBadgeEnabledForBundle(bundleOption, true); + if (errCode != ERR_OK) { + state.SkipWithError("GetShowBadgeEnabledForBundleTestCase set failed."); + } + + bool allow = false; + errCode = advancedNotificationService_->GetShowBadgeEnabledForBundle(bundleOption, allow); + if (!allow || errCode != ERR_OK) { + state.SkipWithError("GetShowBadgeEnabledForBundleTestCase get failed."); + } + } +} + +/** + * @tc.name: GetAllActiveNotificationsTestCase + * @tc.desc: GetAllActiveNotifications + * @tc.type: FUNC + * @tc.require: + */ +BENCHMARK_F(BenchmarkNotificationService, GetAllActiveNotificationsTestCase)(benchmark::State &state) +{ + std::vector> notifications; + while (state.KeepRunning()) { + ErrCode errCode = advancedNotificationService_->GetAllActiveNotifications(notifications); + if (errCode != ERR_OK) { + state.SkipWithError("GetAllActiveNotificationsTestCase failed."); + } + } +} + +/** + * @tc.name: SetNotificationsEnabledForAllBundlesTestCase + * @tc.desc: SetNotificationsEnabledForAllBundles + * @tc.type: FUNC + * @tc.require: + */ +BENCHMARK_F(BenchmarkNotificationService, SetNotificationsEnabledForAllBundlesTestCase)(benchmark::State &state) +{ + std::vector> notifications; + while (state.KeepRunning()) { + ErrCode errCode = advancedNotificationService_->SetNotificationsEnabledForAllBundles(std::string(), true); + if (errCode != ERR_OK) { + state.SkipWithError("SetNotificationsEnabledForAllBundlesTestCase failed."); + } + } +} + +/** + * @tc.name: IsAllowedNotifyTestCase + * @tc.desc: IsAllowedNotify + * @tc.type: FUNC + * @tc.require: + */ +BENCHMARK_F(BenchmarkNotificationService, IsAllowedNotifyTestCase)(benchmark::State &state) +{ + std::vector> notifications; + while (state.KeepRunning()) { + ErrCode errCode = advancedNotificationService_->SetNotificationsEnabledForAllBundles(std::string(), true); + if (errCode != ERR_OK) { + state.SkipWithError("IsAllowedNotifyTestCase set failed."); + } + + bool allowed = false; + errCode = advancedNotificationService_->IsAllowedNotify(allowed); + if (!allowed || errCode != ERR_OK) { + state.SkipWithError("IsAllowedNotifyTestCase get failed."); + } + } +} + +/** + * @tc.name: SetNotificationsEnabledForSpecialBundleTestCase + * @tc.desc: SetNotificationsEnabledForSpecialBundle + * @tc.type: FUNC + * @tc.require: + */ +BENCHMARK_F(BenchmarkNotificationService, SetNotificationsEnabledForSpecialBundleTestCase)(benchmark::State &state) +{ + sptr bundleOption = new NotificationBundleOption("bundleName", 1000); + while (state.KeepRunning()) { + ErrCode errCode = advancedNotificationService_->SetNotificationsEnabledForSpecialBundle( + std::string(), bundleOption, true); + if (errCode != ERR_OK) { + state.SkipWithError("SetNotificationsEnabledForSpecialBundleTestCase failed."); + } + } +} +} + +// Run the benchmark +BENCHMARK_MAIN(); \ No newline at end of file