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