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