• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <benchmark/benchmark.h>
17 #include <gtest/gtest.h>
18 #include <string>
19 #include <vector>
20 
21 #define private public
22 #include "advanced_notification_service.h"
23 #include "ans_const_define.h"
24 #include "ans_inner_errors.h"
25 #include "mock_ipc_skeleton.h"
26 #include "notification.h"
27 #include "notification_subscriber.h"
28 #include "accesstoken_kit.h"
29 #include "nativetoken_kit.h"
30 #include "token_setproc.h"
31 #include "ans_permission_def.h"
32 
33 using namespace OHOS;
34 using namespace OHOS::Notification;
35 using namespace OHOS::AbilityRuntime;
36 using namespace OHOS::Notification;
37 
38 namespace {
39 const uint32_t TOKEN_ID = 0x08000000;
40 
AddPermission()41 void AddPermission()
42 {
43     uint64_t tokenId;
44     const char *perms[2];
45     perms[0] = OHOS::Notification::OHOS_PERMISSION_NOTIFICATION_CONTROLLER;
46     perms[1] = OHOS::Notification::OHOS_PERMISSION_NOTIFICATION_AGENT_CONTROLLER;
47     NativeTokenInfoParams infoInstance = {
48         .dcapsNum = 0,
49         .permsNum = 2,
50         .aclsNum = 0,
51         .dcaps = NULL,
52         .perms = perms,
53         .acls = NULL,
54         .processName = "com.distributed.notification.service.test",
55         .aplStr = "system_core",
56 
57     };
58     tokenId = GetAccessTokenId(&infoInstance);
59     SetSelfTokenID(tokenId);
60     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
61 }
62 
63 class TestAnsSubscriber : public NotificationSubscriber {
64 public:
OnConnected()65     void OnConnected() override
66     {}
OnDisconnected()67     void OnDisconnected() override
68     {}
OnUpdate(const std::shared_ptr<NotificationSortingMap> & sortingMap)69     void OnUpdate(const std::shared_ptr<NotificationSortingMap> &sortingMap) override
70     {}
OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> & date)71     void OnDoNotDisturbDateChange(const std::shared_ptr<NotificationDoNotDisturbDate> &date) override
72     {}
OnEnabledNotificationChanged(const std::shared_ptr<EnabledNotificationCallbackData> & callbackData)73     void OnEnabledNotificationChanged(
74         const std::shared_ptr<EnabledNotificationCallbackData> &callbackData) override
75     {}
OnDied()76     void OnDied() override
77     {}
OnCanceled(const std::shared_ptr<OHOS::Notification::Notification> & request,const std::shared_ptr<NotificationSortingMap> & sortingMap,int deleteReason)78     void OnCanceled(const std::shared_ptr<OHOS::Notification::Notification> &request,
79         const std::shared_ptr<NotificationSortingMap> &sortingMap, int deleteReason) override
80     {}
OnConsumed(const std::shared_ptr<OHOS::Notification::Notification> & request,const std::shared_ptr<NotificationSortingMap> & sortingMap)81     void OnConsumed(const std::shared_ptr<OHOS::Notification::Notification> &request,
82         const std::shared_ptr<NotificationSortingMap> &sortingMap) override
83     {}
OnBadgeChanged(const std::shared_ptr<BadgeNumberCallbackData> & badgeData)84     void OnBadgeChanged(const std::shared_ptr<BadgeNumberCallbackData> &badgeData) override
85     {}
OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> & callbackData)86     void OnBadgeEnabledChanged(const sptr<EnabledNotificationCallbackData> &callbackData) override
87     {}
OnBatchCanceled(const std::vector<std::shared_ptr<OHOS::Notification::Notification>> & requestList,const std::shared_ptr<NotificationSortingMap> & sortingMap,int32_t deleteReason)88     void OnBatchCanceled(const std::vector<std::shared_ptr<OHOS::Notification::Notification>>
89         &requestList, const std::shared_ptr<NotificationSortingMap> &sortingMap, int32_t deleteReason) override
90     {}
91 };
92 
93 class BenchmarkNotificationService : public benchmark::Fixture {
94 public:
BenchmarkNotificationService()95     BenchmarkNotificationService()
96     {
97         Iterations(iterations);
98         Repetitions(repetitions);
99         ReportAggregatesOnly();
100     }
101 
102     ~BenchmarkNotificationService() override = default;
103 
SetUp(const::benchmark::State & state)104     void SetUp(const ::benchmark::State &state) override
105     {
106         if (flag) {
107             AddPermission();
108             flag = false;
109         }
110     }
TearDown(const::benchmark::State & state)111     void TearDown(const ::benchmark::State &state) override
112     {}
113 
114 protected:
115     const int32_t repetitions = 3;
116     const int32_t iterations = 100;
117     bool flag = true;
118     static sptr<AdvancedNotificationService> advancedNotificationService_;
119 };
120 
121 sptr<AdvancedNotificationService> BenchmarkNotificationService::advancedNotificationService_ =
122     AdvancedNotificationService::GetInstance();
123 
124 /**
125  * @tc.name: AddSlotTestCase
126  * @tc.desc: AddSlot
127  * @tc.type: FUNC
128  * @tc.require:
129  */
BENCHMARK_F(BenchmarkNotificationService,AddSlotTestCase)130 BENCHMARK_F(BenchmarkNotificationService, AddSlotTestCase)(benchmark::State &state)
131 {
132     std::vector<sptr<NotificationSlot>> slots;
133     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::CUSTOM);
134     slots.push_back(slot);
135     while (state.KeepRunning()) {
136         ErrCode errCode = advancedNotificationService_->AddSlots(slots);
137         if (errCode != ERR_OK) {
138             state.SkipWithError("AddSlotTestCase failed.");
139         }
140     }
141 }
142 
143 /**
144  * @tc.name: RemoveSlotByTypeTestCase
145  * @tc.desc: RemoveSlotByType
146  * @tc.type: FUNC
147  * @tc.require:
148  */
BENCHMARK_F(BenchmarkNotificationService,RemoveSlotByTypeTestCase)149 BENCHMARK_F(BenchmarkNotificationService, RemoveSlotByTypeTestCase)(benchmark::State &state)
150 {
151     std::vector<sptr<NotificationSlot>> slots;
152     sptr<NotificationSlot> slot = new NotificationSlot(NotificationConstant::SlotType::CUSTOM);
153     slots.push_back(slot);
154     while (state.KeepRunning()) {
155         ErrCode errCode = advancedNotificationService_->AddSlots(slots);
156         if (errCode != ERR_OK) {
157             state.SkipWithError("RemoveSlotByTypeTestCase add failed.");
158         }
159 
160         errCode = advancedNotificationService_->RemoveSlotByType(NotificationConstant::SlotType::CUSTOM);
161         if (errCode != ERR_OK) {
162             state.SkipWithError("RemoveSlotByTypeTestCase remove failed.");
163         }
164     }
165 }
166 
167 /**
168  * @tc.name: SubscribeTestCase
169  * @tc.desc: Subscribe
170  * @tc.type: FUNC
171  * @tc.require:
172  */
BENCHMARK_F(BenchmarkNotificationService,SubscribeTestCase)173 BENCHMARK_F(BenchmarkNotificationService, SubscribeTestCase)(benchmark::State &state)
174 {
175     auto subscriber = new TestAnsSubscriber();
176     sptr<NotificationSubscribeInfo> info = new NotificationSubscribeInfo();
177     while (state.KeepRunning()) {
178         ErrCode errCode = advancedNotificationService_->Subscribe(subscriber->GetImpl(), info);
179         if (errCode != ERR_OK) {
180             state.SkipWithError("SubscribeTestCase failed.");
181         }
182     }
183 }
184 
185 /**
186  * @tc.name: PublishNotificationTestCase001
187  * @tc.desc: Publish a normal text type notification.
188  * @tc.type: FUNC
189  * @tc.require:
190  */
BENCHMARK_F(BenchmarkNotificationService,PublishNotificationTestCase001)191 BENCHMARK_F(BenchmarkNotificationService, PublishNotificationTestCase001)(benchmark::State &state)
192 {
193     IPCSkeleton::SetCallingTokenID(0);
194     sptr<NotificationRequest> req = new (std::nothrow) NotificationRequest(1);
195     EXPECT_NE(req, nullptr);
196     req->SetSlotType(NotificationConstant::SlotType::OTHER);
197     req->SetLabel("req's label");
198     req->SetCreatorUid(100);
199     std::string label = "publish's label";
200     std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
201     EXPECT_NE(normalContent, nullptr);
202     normalContent->SetText("normalContent's text");
203     normalContent->SetTitle("normalContent's title");
204     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
205     EXPECT_NE(content, nullptr);
206     req->SetContent(content);
207 
208     while (state.KeepRunning()) {
209         ErrCode errCode = advancedNotificationService_->Publish(label, req);
210         if (errCode != ERR_OK) {
211             state.SkipWithError("PublishNotificationTestCase001 failed.");
212         }
213     }
214 }
215 
216 /**
217  * @tc.name: CancelNotificationTestCase001
218  * @tc.desc: Cancel a normal text type notification.
219  * @tc.type: FUNC
220  * @tc.require:
221  */
BENCHMARK_F(BenchmarkNotificationService,CancelNotificationTestCase001)222 BENCHMARK_F(BenchmarkNotificationService, CancelNotificationTestCase001)(benchmark::State &state)
223 {
224     IPCSkeleton::SetCallingTokenID(0);
225     sptr<NotificationRequest> req = new (std::nothrow) NotificationRequest(0);
226     EXPECT_NE(req, nullptr);
227     req->SetSlotType(NotificationConstant::SlotType::OTHER);
228     req->SetLabel("req's label");
229     req->SetCreatorUid(100);
230     std::string label = "publish's label";
231     std::shared_ptr<NotificationNormalContent> normalContent = std::make_shared<NotificationNormalContent>();
232     EXPECT_NE(normalContent, nullptr);
233     normalContent->SetText("normalContent's text");
234     normalContent->SetTitle("normalContent's title");
235     std::shared_ptr<NotificationContent> content = std::make_shared<NotificationContent>(normalContent);
236     EXPECT_NE(content, nullptr);
237     req->SetContent(content);
238 
239     int id = 0;
240     while (state.KeepRunning()) {
241         req->SetNotificationId(id);
242         ErrCode errCode = advancedNotificationService_->Publish(label, req);
243         if (errCode != ERR_OK) {
244             state.SkipWithError("CancelNotificationTestCase001 publish failed.");
245         }
246         advancedNotificationService_->Cancel(id, label, "");
247     }
248 }
249 
250 /**
251  * @tc.name: SetNotificationBadgeNumTestCase
252  * @tc.desc: SetNotificationBadgeNum
253  * @tc.type: FUNC
254  * @tc.require:
255  */
BENCHMARK_F(BenchmarkNotificationService,SetNotificationBadgeNumTestCase)256 BENCHMARK_F(BenchmarkNotificationService, SetNotificationBadgeNumTestCase)(benchmark::State &state)
257 {
258     while (state.KeepRunning()) {
259         ErrCode errCode = advancedNotificationService_->SetNotificationBadgeNum(2);
260         if (errCode != ERR_OK) {
261             state.SkipWithError("SetNotificationBadgeNumTestCase failed.");
262         }
263     }
264 }
265 
266 /**
267  * @tc.name: GetBundleImportanceTestCase
268  * @tc.desc: GetBundleImportance
269  * @tc.type: FUNC
270  * @tc.require:
271  */
BENCHMARK_F(BenchmarkNotificationService,GetBundleImportanceTestCase)272 BENCHMARK_F(BenchmarkNotificationService, GetBundleImportanceTestCase)(benchmark::State &state)
273 {
274     int importance = 0;
275     while (state.KeepRunning()) {
276         ErrCode errCode = advancedNotificationService_->GetBundleImportance(importance);
277         if (errCode != ERR_OK) {
278             state.SkipWithError("GetBundleImportanceTestCase failed.");
279         }
280     }
281 }
282 
283 /**
284  * @tc.name: SetShowBadgeEnabledForBundleTestCase
285  * @tc.desc: SetShowBadgeEnabledForBundle
286  * @tc.type: FUNC
287  * @tc.require:
288  */
BENCHMARK_F(BenchmarkNotificationService,SetShowBadgeEnabledForBundleTestCase)289 BENCHMARK_F(BenchmarkNotificationService, SetShowBadgeEnabledForBundleTestCase)(benchmark::State &state)
290 {
291     sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption("bundleName", 1000);
292     while (state.KeepRunning()) {
293         ErrCode errCode = advancedNotificationService_->SetShowBadgeEnabledForBundle(bundleOption, true);
294         if (errCode != ERR_OK) {
295             state.SkipWithError("SetShowBadgeEnabledForBundleTestCase failed.");
296         }
297     }
298 }
299 
300 /**
301  * @tc.name: GetShowBadgeEnabledForBundleTestCase
302  * @tc.desc: GetShowBadgeEnabledForBundle
303  * @tc.type: FUNC
304  * @tc.require:
305  */
BENCHMARK_F(BenchmarkNotificationService,GetShowBadgeEnabledForBundleTestCase)306 BENCHMARK_F(BenchmarkNotificationService, GetShowBadgeEnabledForBundleTestCase)(benchmark::State &state)
307 {
308     sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption("bundleName", 1000);
309     while (state.KeepRunning()) {
310         ErrCode errCode = advancedNotificationService_->SetShowBadgeEnabledForBundle(bundleOption, true);
311         if (errCode != ERR_OK) {
312             state.SkipWithError("GetShowBadgeEnabledForBundleTestCase set failed.");
313         }
314 
315         bool allow = false;
316         errCode = advancedNotificationService_->GetShowBadgeEnabledForBundle(bundleOption, allow);
317         if (!allow || errCode != ERR_OK) {
318             state.SkipWithError("GetShowBadgeEnabledForBundleTestCase get failed.");
319         }
320     }
321 }
322 
323 /**
324  * @tc.name: GetAllActiveNotificationsTestCase
325  * @tc.desc: GetAllActiveNotifications
326  * @tc.type: FUNC
327  * @tc.require:
328  */
BENCHMARK_F(BenchmarkNotificationService,GetAllActiveNotificationsTestCase)329 BENCHMARK_F(BenchmarkNotificationService, GetAllActiveNotificationsTestCase)(benchmark::State &state)
330 {
331     std::vector<sptr<OHOS::Notification::Notification>> notifications;
332     while (state.KeepRunning()) {
333         ErrCode errCode = advancedNotificationService_->GetAllActiveNotifications(notifications);
334         if (errCode != ERR_OK) {
335             state.SkipWithError("GetAllActiveNotificationsTestCase failed.");
336         }
337     }
338 }
339 
340 /**
341  * @tc.name: SetNotificationsEnabledForAllBundlesTestCase
342  * @tc.desc: SetNotificationsEnabledForAllBundles
343  * @tc.type: FUNC
344  * @tc.require:
345  */
BENCHMARK_F(BenchmarkNotificationService,SetNotificationsEnabledForAllBundlesTestCase)346 BENCHMARK_F(BenchmarkNotificationService, SetNotificationsEnabledForAllBundlesTestCase)(benchmark::State &state)
347 {
348     std::vector<sptr<OHOS::Notification::Notification>> notifications;
349     while (state.KeepRunning()) {
350         ErrCode errCode = advancedNotificationService_->SetNotificationsEnabledForAllBundles(std::string(), true);
351         if (errCode != ERR_OK) {
352             state.SkipWithError("SetNotificationsEnabledForAllBundlesTestCase failed.");
353         }
354     }
355 }
356 
357 /**
358  * @tc.name: IsAllowedNotifyTestCase
359  * @tc.desc: IsAllowedNotify
360  * @tc.type: FUNC
361  * @tc.require:
362  */
BENCHMARK_F(BenchmarkNotificationService,IsAllowedNotifyTestCase)363 BENCHMARK_F(BenchmarkNotificationService, IsAllowedNotifyTestCase)(benchmark::State &state)
364 {
365     std::vector<sptr<OHOS::Notification::Notification>> notifications;
366     while (state.KeepRunning()) {
367         ErrCode errCode = advancedNotificationService_->SetNotificationsEnabledForAllBundles(std::string(), true);
368         if (errCode != ERR_OK) {
369             state.SkipWithError("IsAllowedNotifyTestCase set failed.");
370         }
371 
372         bool allowed = false;
373         errCode = advancedNotificationService_->IsAllowedNotify(allowed);
374         if (!allowed || errCode != ERR_OK) {
375             state.SkipWithError("IsAllowedNotifyTestCase get failed.");
376         }
377     }
378 }
379 
380 /**
381  * @tc.name: SetNotificationsEnabledForSpecialBundleTestCase
382  * @tc.desc: SetNotificationsEnabledForSpecialBundle
383  * @tc.type: FUNC
384  * @tc.require:
385  */
BENCHMARK_F(BenchmarkNotificationService,SetNotificationsEnabledForSpecialBundleTestCase)386 BENCHMARK_F(BenchmarkNotificationService, SetNotificationsEnabledForSpecialBundleTestCase)(benchmark::State &state)
387 {
388     sptr<NotificationBundleOption> bundleOption = new NotificationBundleOption("bundleName", 1000);
389     while (state.KeepRunning()) {
390         ErrCode errCode = advancedNotificationService_->SetNotificationsEnabledForSpecialBundle(
391                 std::string(), bundleOption, true);
392         if (errCode != ERR_OK) {
393             state.SkipWithError("SetNotificationsEnabledForSpecialBundleTestCase failed.");
394         }
395     }
396 }
397 }
398 
399 // Run the benchmark
400 BENCHMARK_MAIN();
401