• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 "errors.h"
17 #include "notification_content.h"
18 #include "notification_record.h"
19 #include "notification_request.h"
20 #include <chrono>
21 #include <functional>
22 #include <gtest/gtest.h>
23 #include <memory>
24 #include <thread>
25 
26 #include <vector>
27 
28 #define private public
29 
30 #include "accesstoken_kit.h"
31 #include "ans_const_define.h"
32 #include "ans_inner_errors.h"
33 #include "ans_log_wrapper.h"
34 #include "ans_notification.h"
35 #include "common_event_manager.h"
36 #include "common_event_support.h"
37 #include "disturb_manager.h"
38 #include "iremote_object.h"
39 #include "notification_preferences.h"
40 #include "notification_subscriber.h"
41 #include "notification_subscriber_manager.h"
42 #include "system_event_observer.h"
43 #include "notification_constant.h"
44 #include "want_agent_info.h"
45 #include "want_agent_helper.h"
46 #include "want_params.h"
47 #include "bundle_manager_helper.h"
48 
49 using namespace testing;
50 using namespace testing::ext;
51 using namespace OHOS::Security::AccessToken;
52 using namespace OHOS::Media;
53 
54 extern void MockVerifyNativeToken(bool mockRet);
55 extern void MockQueryForgroundOsAccountId(bool mockRet, uint8_t mockCase);
56 namespace OHOS {
57 namespace Notification {
58 extern void MockGetTokenTypeFlag(ATokenTypeEnum mockRet);
59 extern void MockIsSystemApp(bool isSystemApp);
60 extern void MockIsVerfyPermisson(bool isVerify);
61 
62 class DisturbManagerTest : public testing::Test {
63 public:
SetUpTestCase()64     static void SetUpTestCase() {};
TearDownTestCase()65     static void TearDownTestCase() {};
66     void SetUp();
67     void TearDown();
68 
69 private:
70     std::shared_ptr<DisturbManager> disturbManager_;
71 };
72 
SetUp()73 void DisturbManagerTest::SetUp()
74 {
75     disturbManager_ = std::make_shared<DisturbManager>();
76 }
77 
TearDown()78 void DisturbManagerTest::TearDown()
79 {
80     disturbManager_ = nullptr;
81 }
82 
83 /**
84  * @tc.number    : DisturbManagerTest_20000
85  * @tc.name      : DisturbManagerTest_20000
86  * @tc.desc      : Test invalid userId return ERR_ANS_INVALID_PARAM
87  * @tc.require   : #I61RF2
88  */
89 HWTEST_F(DisturbManagerTest, DisturbManagerTest_20000, Function | SmallTest | Level1)
90 {
91     int32_t userId = -2;
92 
93     sptr<NotificationDoNotDisturbDate> date = nullptr;
94     ASSERT_EQ(disturbManager_->GetDoNotDisturbDateByUserSyncQueue(userId, date), ERR_ANS_INVALID_PARAM);
95     ASSERT_EQ(disturbManager_->SetDoNotDisturbDateByUserSyncQueue(userId, date), ERR_ANS_INVALID_PARAM);
96 }
97 
98 /**
99  * @tc.number    : DisturbManagerTest
100  * @tc.name      : CheckSystemAndControllerPermission_0100
101  * @tc.desc      : Test CheckSystemAndControllerPermission return ERR_ANS_NON_SYSTEM_APP.
102  * @tc.require   : #I6P8UI
103  */
104 HWTEST_F(DisturbManagerTest, CheckSystemAndControllerPermission_0100, Function | SmallTest | Level1)
105 {
106     MockVerifyNativeToken(false);
107     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
108     MockIsSystemApp(false);
109 
110     ASSERT_EQ(disturbManager_->CheckSystemAndControllerPermission(), ERR_ANS_NON_SYSTEM_APP);
111 }
112 
113 /**
114  * @tc.number    : DisturbManagerTest
115  * @tc.name      : CheckSystemAndControllerPermission_0200
116  * @tc.desc      : Test CheckSystemAndControllerPermission return ERR_ANS_PERMISSION_DENIED.
117  * @tc.require   : #I6P8UI
118  */
119 HWTEST_F(DisturbManagerTest, CheckSystemAndControllerPermission_0200, Function | SmallTest | Level1)
120 {
121     MockVerifyNativeToken(false);
122     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_HAP);
123     MockIsVerfyPermisson(false);
124 
125     ASSERT_EQ(disturbManager_->CheckSystemAndControllerPermission(), ERR_ANS_NON_SYSTEM_APP);
126 }
127 
128 /**
129  * @tc.number  : DisturbManagerTest_21000
130  * @tc.name    : DisturbManagerTest_21000
131  * @tc.desc    : Test SetDoNotDisturbDate function and GetActiveUserId is false
132  */
133 HWTEST_F(DisturbManagerTest, DisturbManagerTest_21000, Function | SmallTest | Level1)
134 {
135     sptr<NotificationDoNotDisturbDate> date = nullptr;
136     MockQueryForgroundOsAccountId(false, 1);
137     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
138 
139     ASSERT_EQ(disturbManager_->SetDoNotDisturbDate(date), ERR_ANS_GET_ACTIVE_USER_FAILED);
140 }
141 
142 /**
143  * @tc.number  : DisturbManagerTest_22000
144  * @tc.name    : DisturbManagerTest_22000
145  * @tc.desc    : Test GetDoNotDisturbDateSyncQueue function and GetActiveUserId is false
146  */
147 HWTEST_F(DisturbManagerTest, DisturbManagerTest_22000, Function | SmallTest | Level1)
148 {
149     sptr<NotificationDoNotDisturbDate> date = nullptr;
150     MockQueryForgroundOsAccountId(false, 1);
151     MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_NATIVE);
152 
153     ASSERT_EQ(disturbManager_->GetDoNotDisturbDateSyncQueue(date), ERR_ANS_GET_ACTIVE_USER_FAILED);
154 }
155 
156 /**
157  * @tc.number    : DisturbManagerTest_03600
158  * @tc.name      : ANS_AddDoNotDisturbProfiles_0100
159  * @tc.desc      : Test AddDoNotDisturbProfiles function
160  */
161 HWTEST_F(DisturbManagerTest, DisturbManagerTest_03600, Function | SmallTest | Level1)
162 {
163     MockQueryForgroundOsAccountId(true, 1);
164     sptr<NotificationDoNotDisturbProfile> date = nullptr;
165     std::vector<sptr<NotificationDoNotDisturbProfile>> profiles = { date };
166     auto ret = disturbManager_->AddDoNotDisturbProfilesSyncQueue(profiles);
167     ASSERT_EQ(ret, (int)ERR_OK);
168 }
169 
170 
171 /**
172  * @tc.number    : DisturbManagerTest_04200
173  * @tc.name      : ANS_AddDoNotDisturbProfiles_0100
174  * @tc.desc      : Test AddDoNotDisturbProfiles function
175  */
176 HWTEST_F(DisturbManagerTest, DisturbManagerTest_04200, Function | SmallTest | Level1)
177 {
178     MockQueryForgroundOsAccountId(true, 1);
179     sptr<NotificationDoNotDisturbProfile> date = nullptr;
180     std::vector<sptr<NotificationDoNotDisturbProfile>> profiles = { date };
181     auto ret = disturbManager_->RemoveDoNotDisturbProfilesSyncQueue(profiles);
182     ASSERT_EQ(ret, (int)ERR_OK);
183 }
184 
185 /**
186  * @tc.number    : DisturbManagerTest_10500
187  * @tc.name      : ANS_SetDisturbMode_10500
188  * @tc.desc      : Test SetDisturbMode function
189  */
190 HWTEST_F(DisturbManagerTest, DisturbManagerTest_10500, Function | SmallTest | Level1)
191 {
192     MockQueryForgroundOsAccountId(true, 1);
193     sptr<NotificationDoNotDisturbDate> date =
194         new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0);
195     ASSERT_EQ((int)disturbManager_->SetDoNotDisturbDate(date), (int)ERR_OK);
196 
197     std::chrono::system_clock::time_point timePoint = std::chrono::system_clock::now();
198     auto beginDuration = std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch());
199     int64_t beginDate = beginDuration.count();
200     timePoint += std::chrono::hours(1);
201     auto endDuration = std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch());
202     int64_t endDate = endDuration.count();
203     date = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::ONCE, beginDate, endDate);
204     ASSERT_EQ((int)disturbManager_->SetDoNotDisturbDate(date), (int)ERR_OK);
205 
206     timePoint = std::chrono::system_clock::now();
207     beginDuration = std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch());
208     beginDate = beginDuration.count();
209     timePoint += std::chrono::hours(1);
210     endDuration = std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch());
211     endDate = endDuration.count();
212     date = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::DAILY, beginDate, endDate);
213     ASSERT_EQ((int)disturbManager_->SetDoNotDisturbDate(date), (int)ERR_OK);
214 
215     timePoint = std::chrono::system_clock::now();
216     beginDuration = std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch());
217     beginDate = beginDuration.count();
218     timePoint += std::chrono::hours(1);
219     endDuration = std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch());
220     endDate = endDuration.count();
221     date = new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::CLEARLY, beginDate, endDate);
222     ASSERT_EQ((int)disturbManager_->SetDoNotDisturbDate(date), (int)ERR_OK);
223 }
224 
225 
226 /**
227  * @tc.number    : DisturbManagerTest_10600
228  * @tc.name      : ANS_GetDisturbMode_10600
229  * @tc.desc      : Test GetDisturbMode function
230  */
231 HWTEST_F(DisturbManagerTest, DisturbManagerTest_10600, Function | SmallTest | Level1)
232 {
233     MockQueryForgroundOsAccountId(true, 1);
234     sptr<NotificationDoNotDisturbDate> date =
235         new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::NONE, 0, 0);
236 
237     ASSERT_EQ((int)disturbManager_->SetDoNotDisturbDate(date), (int)ERR_OK);
238 
239     sptr<NotificationDoNotDisturbDate> result = nullptr;
240     ASSERT_EQ((int)disturbManager_->GetDoNotDisturbDateSyncQueue(result), (int)ERR_OK);
241     ASSERT_NE(result, nullptr);
242     ASSERT_EQ(result->GetDoNotDisturbType(), NotificationConstant::DoNotDisturbType::NONE);
243     ASSERT_EQ(result->GetBeginDate(), 0);
244     ASSERT_EQ(result->GetEndDate(), 0);
245 }
246 
247 /**
248  * @tc.number    : DisturbManagerTest_10700
249  * @tc.name      : ANS_GetDisturbMode_10700
250  * @tc.desc      : Test GetDisturbMode function
251  */
252 HWTEST_F(DisturbManagerTest, DisturbManagerTest_10700, Function | SmallTest | Level1)
253 {
254     MockQueryForgroundOsAccountId(true, 1);
255     std::chrono::system_clock::time_point timePoint = std::chrono::system_clock::now();
256     timePoint = std::chrono::time_point_cast<std::chrono::minutes>(timePoint);
257     timePoint += std::chrono::hours(1);
258     auto beginDuration = std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch());
259     int64_t beginDate = beginDuration.count();
260     timePoint += std::chrono::hours(1);
261     auto endDuration = std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch());
262     int64_t endDate = endDuration.count();
263 
264     sptr<NotificationDoNotDisturbDate> date =
265         new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::ONCE, beginDate, endDate);
266     ASSERT_EQ((int)disturbManager_->SetDoNotDisturbDate(date), (int)ERR_OK);
267 
268     sptr<NotificationDoNotDisturbDate> result = nullptr;
269     ASSERT_EQ((int)disturbManager_->GetDoNotDisturbDateSyncQueue(result), (int)ERR_OK);
270     ASSERT_NE(result, nullptr);
271     ASSERT_EQ(result->GetDoNotDisturbType(), NotificationConstant::DoNotDisturbType::ONCE);
272     ASSERT_EQ(result->GetBeginDate(), beginDate);
273     ASSERT_EQ(result->GetEndDate(), endDate);
274 }
275 
276 /**
277  * @tc.number    : DisturbManagerTest_10800
278  * @tc.name      : ANS_GetDisturbMode_10800
279  * @tc.desc      : Test GetDisturbMode function
280  */
281 HWTEST_F(DisturbManagerTest, DisturbManagerTest_10800, Function | SmallTest | Level1)
282 {
283     MockQueryForgroundOsAccountId(true, 1);
284     std::chrono::system_clock::time_point timePoint = std::chrono::system_clock::now();
285     timePoint = std::chrono::time_point_cast<std::chrono::minutes>(timePoint);
286     timePoint += std::chrono::hours(1);
287     auto beginDuration = std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch());
288     int64_t beginDate = beginDuration.count();
289     timePoint += std::chrono::hours(1);
290     auto endDuration = std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch());
291     int64_t endDate = endDuration.count();
292 
293     sptr<NotificationDoNotDisturbDate> date =
294         new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::DAILY, beginDate, endDate);
295 
296     ASSERT_EQ((int)disturbManager_->SetDoNotDisturbDate(date), (int)ERR_OK);
297     sptr<NotificationDoNotDisturbDate> result = nullptr;
298     ASSERT_EQ((int)disturbManager_->GetDoNotDisturbDateSyncQueue(result), (int)ERR_OK);
299     ASSERT_NE(result, nullptr);
300     ASSERT_EQ(result->GetDoNotDisturbType(), NotificationConstant::DoNotDisturbType::DAILY);
301     ASSERT_EQ(result->GetBeginDate(), beginDate);
302     ASSERT_EQ(result->GetEndDate(), endDate);
303 }
304 
305 /**
306  * @tc.number    : DisturbManagerTest_10900
307  * @tc.name      : ANS_GetDisturbMode_10900
308  * @tc.desc      : Test GetDisturbMode function
309  */
310 HWTEST_F(DisturbManagerTest, DisturbManagerTest_10900, Function | SmallTest | Level1)
311 {
312     MockQueryForgroundOsAccountId(true, 1);
313     std::chrono::system_clock::time_point timePoint = std::chrono::system_clock::now();
314     timePoint = std::chrono::time_point_cast<std::chrono::minutes>(timePoint);
315     timePoint += std::chrono::hours(1);
316     auto beginDuration = std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch());
317     int64_t beginDate = beginDuration.count();
318     timePoint += std::chrono::hours(1);
319     auto endDuration = std::chrono::duration_cast<std::chrono::milliseconds>(timePoint.time_since_epoch());
320     int64_t endDate = endDuration.count();
321 
322     sptr<NotificationDoNotDisturbDate> date =
323         new NotificationDoNotDisturbDate(NotificationConstant::DoNotDisturbType::CLEARLY, beginDate, endDate);
324     ASSERT_EQ((int)disturbManager_->SetDoNotDisturbDate(date), (int)ERR_OK);
325 
326     sptr<NotificationDoNotDisturbDate> result = nullptr;
327     ASSERT_EQ((int)disturbManager_->GetDoNotDisturbDateSyncQueue(result), (int)ERR_OK);
328     ASSERT_NE(result, nullptr);
329     ASSERT_EQ(result->GetDoNotDisturbType(), NotificationConstant::DoNotDisturbType::CLEARLY);
330     ASSERT_EQ(result->GetBeginDate(), beginDate);
331     ASSERT_EQ(result->GetEndDate(), endDate);
332 }
333 
334 /**
335  * @tc.number    : DisturbManagerTest_14200
336  * @tc.name      : ANS_DoesSupportDoNotDisturbMode_0100
337  * @tc.desc      : Test DoesSupportDoNotDisturbMode function when the result is ERR_OK
338  * @tc.require   : issueI5S4VP
339  */
340 HWTEST_F(DisturbManagerTest, DisturbManagerTest_14200, Function | SmallTest | Level1)
341 {
342     MockVerifyNativeToken(true);
343     MockIsVerfyPermisson(true);
344     sptr<NotificationRequest> req = new NotificationRequest();
345     EXPECT_NE(req, nullptr);
346     bool doesSupport = true;
347     ASSERT_EQ(disturbManager_->DoesSupportDoNotDisturbModeInner(doesSupport), (int)ERR_OK);
348 }
349 
350 /**
351  * @tc.number    : DisturbManagerTest_15000
352  * @tc.name      : ANS_GetDoNotDisturbDateByUserSyncQueue_0100
353  * @tc.desc      : Test GetDoNotDisturbDateByUserSyncQueue function when the result is ERR_OK
354  * @tc.require   : issueI5S4VP
355  */
356 HWTEST_F(DisturbManagerTest, DisturbManagerTest_15000, Function | SmallTest | Level1)
357 {
358     int32_t userId = 100;
359     sptr<NotificationDoNotDisturbDate> date = nullptr;
360     ASSERT_EQ(disturbManager_->GetDoNotDisturbDateByUserSyncQueue(userId, date), (int)ERR_OK);
361 }
362 
363 /**
364  * @tc.name: HandleRemoveDoNotDisturbProfiles_0100
365  * @tc.desc: test HandleRemoveDoNotDisturbProfiles when ReadParcelableVector return false.
366  * @tc.type: FUNC
367  */
368  HWTEST_F(DisturbManagerTest, HandleRemoveDoNotDisturbProfiles_0100, TestSize.Level1)
369  {
370      MessageParcel data;
371      MessageParcel reply;
372      ErrCode ret = disturbManager_->HandleRemoveDoNotDisturbProfiles(data, reply);
373      EXPECT_EQ(ret, ERR_ANS_PARCELABLE_FAILED);
374  }
375 
376 
377 /**
378  * @tc.name: HandleAddDoNotDisturbProfiles_0100
379  * @tc.desc: test HandleAddDoNotDisturbProfiles when ReadParcelableVector return false.
380  * @tc.type: FUNC
381  */
382 HWTEST_F(DisturbManagerTest, HandleAddDoNotDisturbProfiles_0100, TestSize.Level1)
383 {
384     MessageParcel data;
385     MessageParcel reply;
386     ErrCode ret = disturbManager_->HandleAddDoNotDisturbProfiles(data, reply);
387     EXPECT_EQ(ret, ERR_ANS_PARCELABLE_FAILED);
388 }
389 
390 }
391 }