1 /*
2 * Copyright (c) 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 <chrono>
17 #include <functional>
18 #include <memory>
19 #include <thread>
20
21 #include "gtest/gtest.h"
22
23 #define private public
24 #include "advanced_notification_service.h"
25 #include "ans_inner_errors.h"
26 #include "ans_log_wrapper.h"
27 #include "accesstoken_kit.h"
28 #include "notification_preferences.h"
29 #include "notification_constant.h"
30 #include "pixel_map.h"
31
32 using namespace testing::ext;
33 using namespace OHOS::Security::AccessToken;
34 using namespace OHOS::Media;
35
36 namespace OHOS {
37 namespace Notification {
38 extern void MockGetTokenTypeFlag(ATokenTypeEnum mockRet);
39 extern void MockIsSystemApp(bool isSystemApp);
40
41 class AnsLiveViewServiceTest : public testing::Test {
42 public:
SetUpTestCase()43 static void SetUpTestCase() {};
TearDownTestCase()44 static void TearDownTestCase() {};
45 void SetUp();
46 void TearDown();
47 std::shared_ptr<PixelMap> MakePixelMap(int32_t width, int32_t height);
48
49 private:
50 static sptr<AdvancedNotificationService> advancedNotificationService_;
51 };
52
53 sptr<AdvancedNotificationService> AnsLiveViewServiceTest::advancedNotificationService_ = nullptr;
54
MakePixelMap(int32_t width,int32_t height)55 std::shared_ptr<PixelMap> AnsLiveViewServiceTest::MakePixelMap(int32_t width, int32_t height)
56 {
57 const int32_t PIXEL_BYTES = 4;
58 std::shared_ptr<PixelMap> pixelMap = std::make_shared<PixelMap>();
59 if (pixelMap == nullptr) {
60 return pixelMap;
61 }
62 ImageInfo info;
63 info.size.width = width;
64 info.size.height = height;
65 info.pixelFormat = PixelFormat::ARGB_8888;
66 info.colorSpace = ColorSpace::SRGB;
67 pixelMap->SetImageInfo(info);
68 int32_t rowDataSize = width * PIXEL_BYTES;
69 uint32_t bufferSize = rowDataSize * height;
70 void *buffer = malloc(bufferSize);
71 if (buffer != nullptr) {
72 pixelMap->SetPixelsAddr(buffer, nullptr, bufferSize, AllocatorType::HEAP_ALLOC, nullptr);
73 }
74 EXPECT_NE(buffer, nullptr);
75 return pixelMap;
76 }
77
SetUp()78 void AnsLiveViewServiceTest::SetUp()
79 {
80 GTEST_LOG_(INFO) << "SetUp start";
81
82 advancedNotificationService_ = new (std::nothrow) AdvancedNotificationService();
83 NotificationPreferences::GetInstance()->ClearNotificationInRestoreFactorySettings();
84 advancedNotificationService_->CancelAll(0);
85 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
86 MockIsSystemApp(true);
87 GTEST_LOG_(INFO) << "SetUp end";
88 }
89
TearDown()90 void AnsLiveViewServiceTest::TearDown()
91 {
92 delete advancedNotificationService_;
93 advancedNotificationService_ = nullptr;
94 GTEST_LOG_(INFO) << "TearDown";
95 }
96
97 /**
98 * @tc.name: ProcForDeleteLiveView_00001
99 * @tc.desc: Test ProcForDeleteLiveView
100 * @tc.type: FUNC
101 * @tc.require: issue
102 */
103 HWTEST_F(AnsLiveViewServiceTest, ProcForDeleteLiveView_00001, Function | SmallTest | Level1)
104 {
105 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
106 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
107 request->SetSlotType(slotType);
108 request->SetNotificationId(1);
109 auto liveContent = std::make_shared<NotificationLiveViewContent>();
110 auto content = std::make_shared<NotificationContent>(liveContent);
111 request->SetContent(content);
112 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
113
114 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
115 AdvancedNotificationService::NotificationRequestDb requestDb =
116 { .request = record->request, .bundleOption = bundle};
117 auto ret = advancedNotificationService_->SetNotificationRequestToDb(requestDb);
118 ASSERT_EQ(ret, (int)ERR_OK);
119
120 std::vector<AdvancedNotificationService::NotificationRequestDb> requestsdb;
121 ret = advancedNotificationService_->GetBatchNotificationRequestsFromDb(requestsdb);
122 ASSERT_EQ(requestsdb.size(), 1);
123
124 advancedNotificationService_->ProcForDeleteLiveView(record);
125 requestsdb.clear();
126 ret = advancedNotificationService_->GetBatchNotificationRequestsFromDb(requestsdb);
127 ASSERT_EQ(requestsdb.size(), 0);
128 }
129
130 /**
131 * @tc.name: SetNotificationRequestToDb_00001
132 * @tc.desc: Test SetNotificationRequestToDb
133 * @tc.type: FUNC
134 * @tc.require: issue
135 */
136 HWTEST_F(AnsLiveViewServiceTest, SetNotificationRequestToDb_00001, Function | SmallTest | Level1)
137 {
138 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
139 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
140 request->SetSlotType(slotType);
141 request->SetNotificationId(1);
142 request->SetAutoDeletedTime(NotificationConstant::NO_DELAY_DELETE_TIME);
143 auto liveContent = std::make_shared<NotificationLiveViewContent>();
144 liveContent->SetLiveViewStatus(NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_END);
145 auto content = std::make_shared<NotificationContent>(liveContent);
146 request->SetContent(content);
147 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
148
149 AdvancedNotificationService::NotificationRequestDb requestDb =
150 { .request = request, .bundleOption = bundle};
151 auto ret = advancedNotificationService_->SetNotificationRequestToDb(requestDb);
152 ASSERT_EQ(ret, (int)ERR_OK);
153
154 std::vector<AdvancedNotificationService::NotificationRequestDb> requestsdb;
155 ret = advancedNotificationService_->GetBatchNotificationRequestsFromDb(requestsdb);
156 ASSERT_EQ(requestsdb.size(), 0);
157 }
158
159 /**
160 * @tc.name: GetNotificationRequestFromDb_00001
161 * @tc.desc: Test GetNotificationRequestFromDb
162 * @tc.type: FUNC
163 * @tc.require: issue
164 */
165 HWTEST_F(AnsLiveViewServiceTest, GetNotificationRequestFromDb_00001, Function | SmallTest | Level1)
166 {
167 AdvancedNotificationService::NotificationRequestDb requestsdb;
168 std::string key = "ans_live_view_001";
169 auto ret = advancedNotificationService_->GetNotificationRequestFromDb(key, requestsdb);
170 EXPECT_NE(ret, (int)ERR_OK);
171 }
172
173 /**
174 * @tc.name: GetNotificationRequestFromDb_00002
175 * @tc.desc: Test GetNotificationRequestFromDb
176 * @tc.type: FUNC
177 * @tc.require: issue
178 */
179 HWTEST_F(AnsLiveViewServiceTest, GetNotificationRequestFromDb_00002, Function | SmallTest | Level1)
180 {
181 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
182 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
183 request->SetSlotType(slotType);
184 request->SetNotificationId(1);
185 request->SetReceiverUserId(100);
186 auto liveContent = std::make_shared<NotificationLiveViewContent>();
187 auto content = std::make_shared<NotificationContent>(liveContent);
188 request->SetContent(content);
189 auto bundle = new NotificationBundleOption("test", 1);
190 AdvancedNotificationService::NotificationRequestDb requestDb =
191 { .request = request, .bundleOption = bundle};
192 auto ret = advancedNotificationService_->SetNotificationRequestToDb(requestDb);
193 ASSERT_EQ(ret, (int)ERR_OK);
194 }
195
196 /**
197 * @tc.name: FillLockScreenPicture_00001
198 * @tc.desc: Test FillLockScreenPicture
199 * @tc.type: FUNC
200 * @tc.require: issue
201 */
202 HWTEST_F(AnsLiveViewServiceTest, FillLockScreenPicture_00001, Function | SmallTest | Level1)
203 {
204 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
205 sptr<NotificationRequest> newRequest = new (std::nothrow) NotificationRequest();
206 newRequest->SetSlotType(slotType);
207 newRequest->SetNotificationId(1);
208 auto newLiveContent = std::make_shared<NotificationLiveViewContent>();
209 auto newContent = std::make_shared<NotificationContent>(newLiveContent);
210 newRequest->SetContent(newContent);
211
212 sptr<NotificationRequest> oldRequest = new (std::nothrow) NotificationRequest();
213 oldRequest->SetSlotType(slotType);
214 oldRequest->SetNotificationId(1);
215 auto oldLiveContent = std::make_shared<NotificationLiveViewContent>();
216 auto oldContent = std::make_shared<NotificationContent>(oldLiveContent);
217
218 std::shared_ptr<PixelMap> pixelMap = MakePixelMap(1, 1);
219 oldLiveContent->SetLockScreenPicture(pixelMap);
220 oldRequest->SetContent(oldContent);
221
222 advancedNotificationService_->FillLockScreenPicture(newRequest, oldRequest);
223 EXPECT_NE(newRequest->GetContent()->GetNotificationContent()->GetLockScreenPicture(), nullptr);
224 }
225
226 /**
227 * @tc.name: SetLockScreenPictureToDb_001
228 * @tc.desc: Test SetLockScreenPictureToDb
229 * @tc.type: FUNC
230 * @tc.require: issue
231 */
232 HWTEST_F(AnsLiveViewServiceTest, SetLockScreenPictureToDb_001, Function | SmallTest | Level1)
233 {
234 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
235 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
236 request->SetSlotType(slotType);
237 request->SetNotificationId(1);
238 request->SetAutoDeletedTime(NotificationConstant::NO_DELAY_DELETE_TIME);
239 auto liveContent = std::make_shared<NotificationLiveViewContent>();
240 liveContent->SetLiveViewStatus(NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_CREATE);
241 auto content = std::make_shared<NotificationContent>(liveContent);
242 std::shared_ptr<PixelMap> pixelMap = MakePixelMap(1024, 1);
243 liveContent->SetLockScreenPicture(pixelMap);
244 request->SetContent(content);
245
246 auto ret = advancedNotificationService_->SetLockScreenPictureToDb(request);
247 ASSERT_EQ(ret, (int)ERR_OK);
248 }
249
250 /**
251 * @tc.name: IsSaCreateSystemLiveViewAsBundle_001
252 * @tc.desc: Test IsSaCreateSystemLiveViewAsBundle
253 * @tc.type: FUNC
254 * @tc.require: issue
255 */
256 HWTEST_F(AnsLiveViewServiceTest, IsSaCreateSystemLiveViewAsBundle_001, Function | SmallTest | Level1)
257 {
258 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
259 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
260 request->SetSlotType(slotType);
261 request->SetNotificationId(1);
262 auto localLiveViewContent = std::make_shared<NotificationLocalLiveViewContent>();
263 auto content = std::make_shared<NotificationContent>(localLiveViewContent);
264 request->SetContent(content);
265 int creatorUid = 1;
266 request->SetCreatorUid(creatorUid);
267 int ownerUid = 2;
268 request->SetOwnerUid(ownerUid);
269 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", creatorUid);
270 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
271 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
272 bool flag = advancedNotificationService_->IsSaCreateSystemLiveViewAsBundle(record, creatorUid);
273 ASSERT_EQ(flag, true);
274 }
275
276 /**
277 * @tc.name: IsSaCreateSystemLiveViewAsBundle_002
278 * @tc.desc: Test IsSaCreateSystemLiveViewAsBundle return false
279 * @tc.type: FUNC
280 * @tc.require: issue
281 */
282 HWTEST_F(AnsLiveViewServiceTest, IsSaCreateSystemLiveViewAsBundle_002, Function | SmallTest | Level1)
283 {
284 auto slotType = NotificationConstant::SlotType::SOCIAL_COMMUNICATION;
285 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
286 request->SetSlotType(slotType);
287 request->SetNotificationId(1);
288 auto localLiveViewContent = std::make_shared<NotificationLocalLiveViewContent>();
289 auto content = std::make_shared<NotificationContent>(localLiveViewContent);
290 request->SetContent(content);
291 int creatorUid = 1;
292 request->SetCreatorUid(creatorUid);
293 int ownerUid = 2;
294 request->SetOwnerUid(ownerUid);
295 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", 1);
296 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
297
298 bool flag = advancedNotificationService_->IsSaCreateSystemLiveViewAsBundle(nullptr, creatorUid);
299 ASSERT_EQ(flag, false);
300
301 flag = advancedNotificationService_->IsSaCreateSystemLiveViewAsBundle(record, creatorUid);
302 ASSERT_EQ(flag, false);
303
304 record->notification->GetNotificationRequest().SetSlotType(NotificationConstant::SlotType::LIVE_VIEW);
305 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP);
306 flag = advancedNotificationService_->IsSaCreateSystemLiveViewAsBundle(record, creatorUid);
307 ASSERT_EQ(flag, false);
308 }
309
310 /**
311 * @tc.name: HandleUpdateLiveViewNotificationTimer_001
312 * @tc.desc: Test HandleUpdateLiveViewNotificationTimer
313 * @tc.type: FUNC
314 * @tc.require: issue
315 */
316 HWTEST_F(AnsLiveViewServiceTest, HandleUpdateLiveViewNotificationTimer_001, Function | SmallTest | Level1)
317 {
318 sptr<NotificationRequest> request = new (std::nothrow) NotificationRequest();
319 auto slotType = NotificationConstant::SlotType::LIVE_VIEW;
320 request->SetSlotType(slotType);
321 request->SetNotificationId(1);
322 int32_t TYPE_CODE_DOWNLOAD = 8;
323 auto localLiveViewContent = std::make_shared<NotificationLocalLiveViewContent>();
324 localLiveViewContent->SetType(TYPE_CODE_DOWNLOAD);
325 auto content = std::make_shared<NotificationContent>(localLiveViewContent);
326 request->SetContent(content);
327 int creatorUid = 3051;
328 request->SetCreatorUid(creatorUid);
329 int ownerUid = 20099999;
330 request->SetOwnerUid(ownerUid);
331 sptr<NotificationBundleOption> bundle = new NotificationBundleOption("test", ownerUid);
332 auto record = advancedNotificationService_->MakeNotificationRecord(request, bundle);
333 advancedNotificationService_->AddToNotificationList(record);
334 MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE);
335 auto timer = record->notification->GetFinishTimer();
336 advancedNotificationService_->HandleUpdateLiveViewNotificationTimer(ownerUid, true);
337 ASSERT_EQ(timer, record->notification->GetFinishTimer());
338 advancedNotificationService_->HandleUpdateLiveViewNotificationTimer(ownerUid, false);
339 ASSERT_NE(timer, record->notification->GetFinishTimer());
340 }
341 } // namespace Notification
342 } // namespace OHOS
343