• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 <gtest/gtest.h>
17 
18 #define private public
19 #define protected public
20 #include "reminder_request_calendar.h"
21 #undef private
22 #undef protected
23 
24 #include "ans_log_wrapper.h"
25 #include "reminder_helper.h"
26 
27 using namespace testing::ext;
28 namespace OHOS {
29 namespace Notification {
30 class ReminderRequestCalendarTest : public testing::Test {
31 public:
SetUpTestCase()32     static void SetUpTestCase()
33     {
34         ReminderHelper::CancelAllReminders();
35     }
TearDownTestCase()36     static void TearDownTestCase() {}
SetUp()37     void SetUp() {}
TearDown()38     void TearDown()
39     {
40         ReminderHelper::CancelAllReminders();
41     }
42 
CreateCalendar(tm & nowTime)43     std::shared_ptr<ReminderRequestCalendar> CreateCalendar(tm &nowTime)
44     {
45         time_t now;
46         (void)time(&now);  // unit is seconds.
47         tm *tmp = localtime(&now);
48         if (tmp == nullptr) {
49             return nullptr;
50         }
51         nowTime = *tmp;
52         nowTime.tm_year = 0;
53         nowTime.tm_mon = 0;
54         nowTime.tm_mday = 1;
55         nowTime.tm_hour = 1;
56         nowTime.tm_min = 1;
57         std::vector<uint8_t> repeatMonths;
58         std::vector<uint8_t> repeatDays;
59         repeatMonths.push_back(1);
60         repeatDays.push_back(1);
61         auto calendar = std::make_shared<ReminderRequestCalendar>(nowTime, repeatMonths, repeatDays);
62         calendar->SetNextTriggerTime();
63         return calendar;
64     }
65 
IsVectorEqual(std::vector<uint8_t> & vectorA,std::vector<uint8_t> & vectorB)66     bool IsVectorEqual(std::vector<uint8_t> &vectorA, std::vector<uint8_t> &vectorB)
67     {
68         if (vectorA.size() != vectorB.size()) {
69             return false;
70         }
71         if (vectorA.size() == 0) {
72             return true;
73         }
74         auto vitA = vectorA.begin();
75         auto vitB = vectorB.begin();
76         while (vitA != vectorA.end()) {
77             if (*vitA != *vitB) {
78                 return false;
79             }
80             ++vitA;
81             ++vitB;
82         }
83         return true;
84     }
85 };
86 
87 /**
88  * @tc.name: initDateTime_00100
89  * @tc.desc: Check firstDesignateYear set successfully.
90  * @tc.type: FUNC
91  * @tc.require: SR000GN4CU AR000GNF1V
92  */
93 HWTEST_F(ReminderRequestCalendarTest, initDateTime_00100, Function | SmallTest | Level1)
94 {
95     struct tm nowTime;
96     auto calendar = ReminderRequestCalendarTest::CreateCalendar(nowTime);
97     EXPECT_NE(nullptr, calendar);
98     int32_t firstDesignateYear = calendar->GetActualTime(ReminderRequest::TimeTransferType::YEAR, nowTime.tm_year);
99     EXPECT_TRUE(firstDesignateYear == calendar->GetFirstDesignateYear()) << "Set first designate year error.";
100 }
101 
102 /**
103  * @tc.name: initDateTime_00200
104  * @tc.desc: Check firstDesignateMonth set successfully.
105  * @tc.type: FUNC
106  * @tc.require: SR000GN4CU AR000GNF1V
107  */
108 HWTEST_F(ReminderRequestCalendarTest, initDateTime_00200, Function | SmallTest | Level1)
109 {
110     struct tm nowTime;
111     auto calendar = ReminderRequestCalendarTest::CreateCalendar(nowTime);
112     EXPECT_NE(nullptr, calendar);
113     int firstDesignateMonth = calendar->GetActualTime(ReminderRequest::TimeTransferType::MONTH, nowTime.tm_mon);
114     EXPECT_TRUE(firstDesignateMonth == calendar->GetFirstDesignageMonth()) << "Set first designate month error.";
115 }
116 
117 /**
118  * @tc.name: initDateTime_00300
119  * @tc.desc: Check firstDesignateDay set successfully.
120  * @tc.type: FUNC
121  * @tc.require: SR000GN4CU AR000GNF1V
122  */
123 HWTEST_F(ReminderRequestCalendarTest, initDateTime_00300, Function | SmallTest | Level1)
124 {
125     struct tm nowTime;
126     auto calendar = ReminderRequestCalendarTest::CreateCalendar(nowTime);
127     EXPECT_NE(nullptr, calendar);
128     int firstDesignateDay = nowTime.tm_mday;
129     EXPECT_TRUE(firstDesignateDay == calendar->GetFirstDesignateDay()) << "Set first designate day error.";
130 }
131 
132 /**
133  * @tc.name: initDateTime_00400
134  * @tc.desc: Check repeatMonth set with normal value successfully.
135  * @tc.type: FUNC
136  * @tc.require: SR000GN4CU AR000GNF1V
137  */
138 HWTEST_F(ReminderRequestCalendarTest, initDateTime_00400, Function | SmallTest | Level1)
139 {
140     time_t now;
141     (void)time(&now);  // unit is seconds.
142     tm *tmp = localtime(&now);
143     EXPECT_NE(nullptr, tmp);
144     struct tm nowTime = *tmp;
145 
146     std::vector<uint8_t> repeatMonths;
147     std::vector<uint8_t> repeatDays;
148     repeatMonths.push_back(1);
149     repeatDays.push_back(1);
150     auto calendar = std::make_shared<ReminderRequestCalendar>(nowTime, repeatMonths, repeatDays);
151     calendar->SetNextTriggerTime();
152     std::vector<uint8_t> actualRepeatMonths = calendar->GetRepeatMonths();
153     EXPECT_TRUE(ReminderRequestCalendarTest::IsVectorEqual(repeatMonths, actualRepeatMonths))
154         << "Set repeat month with 1 error.";
155 
156     repeatMonths.clear();
157     repeatMonths.push_back(12);
158     calendar = std::make_shared<ReminderRequestCalendar>(nowTime, repeatMonths, repeatDays);
159     calendar->SetNextTriggerTime();
160     actualRepeatMonths = calendar->GetRepeatMonths();
161     EXPECT_TRUE(ReminderRequestCalendarTest::IsVectorEqual(repeatMonths, actualRepeatMonths))
162         << "Set repeat month with 12 error.";
163 
164     repeatMonths.clear();
165     for (uint8_t i = 1; i <= 12; i++) {
166         repeatMonths.push_back(i);
167     }
168     calendar = std::make_shared<ReminderRequestCalendar>(nowTime, repeatMonths, repeatDays);
169     calendar->SetNextTriggerTime();
170     actualRepeatMonths = calendar->GetRepeatMonths();
171     EXPECT_TRUE(ReminderRequestCalendarTest::IsVectorEqual(repeatMonths, actualRepeatMonths))
172         << "Set repeat month with 1~12 error.";
173 }
174 
175 /**
176  * @tc.name: initDateTime_00500
177  * @tc.desc: Check repeatMonth set with exception value successfully.
178  * @tc.type: FUNC
179  * @tc.require: SR000GN4CU AR000GNF1V
180  */
181 HWTEST_F(ReminderRequestCalendarTest, initDateTime_00500, Function | SmallTest | Level1)
182 {
183     time_t now;
184     time(&now);  // unit is seconds.
185     tm *tmp = localtime(&now);
186     EXPECT_NE(nullptr, tmp);
187     tm nowTime = *tmp;
188     nowTime.tm_year += 1;
189     std::vector<uint8_t> repeatMonths;
190     std::vector<uint8_t> repeatDays;
191     repeatMonths.push_back(-1);
192     repeatDays.push_back(1);
193     auto calendar = std::make_shared<ReminderRequestCalendar>(nowTime, repeatMonths, repeatDays);
194     calendar->SetNextTriggerTime();
195     std::vector<uint8_t> actualRepeatMonths = calendar->GetRepeatMonths();
196     EXPECT_TRUE(actualRepeatMonths.size() == 0) << "Set repeat month with -1 error.";
197 
198     repeatMonths.clear();
199     repeatMonths.push_back(13);
200     calendar = std::make_shared<ReminderRequestCalendar>(nowTime, repeatMonths, repeatDays);
201     calendar->SetNextTriggerTime();
202     actualRepeatMonths = calendar->GetRepeatMonths();
203     EXPECT_TRUE(actualRepeatMonths.size() == 0) << "Set repeat month with 13 error.";
204 }
205 
206 /**
207  * @tc.name: initDateTime_00600
208  * @tc.desc: Check repeatDay set with nomal value successfully.
209  * @tc.type: FUNC
210  * @tc.require: SR000GN4CU AR000GNF1V
211  */
212 HWTEST_F(ReminderRequestCalendarTest, initDateTime_00600, Function | SmallTest | Level1)
213 {
214     time_t now;
215     (void)time(&now);  // unit is seconds.
216     tm *tmp = localtime(&now);
217     EXPECT_NE(nullptr, tmp);
218     tm nowTime = *tmp;
219     std::vector<uint8_t> repeatMonths;
220     std::vector<uint8_t> repeatDays;
221     repeatMonths.push_back(1);
222     repeatDays.push_back(1);
223     auto calendar = std::make_shared<ReminderRequestCalendar>(nowTime, repeatMonths, repeatDays);
224     calendar->SetNextTriggerTime();
225     std::vector<uint8_t> actualRepeatDays = calendar->GetRepeatDays();
226     EXPECT_TRUE(ReminderRequestCalendarTest::IsVectorEqual(repeatDays, actualRepeatDays))
227         << "Set repeat day with 1 error.";
228 
229     repeatDays.clear();
230     repeatDays.push_back(31);
231     calendar = std::make_shared<ReminderRequestCalendar>(nowTime, repeatMonths, repeatDays);
232     calendar->SetNextTriggerTime();
233     actualRepeatDays = calendar->GetRepeatDays();
234     EXPECT_TRUE(ReminderRequestCalendarTest::IsVectorEqual(repeatDays, actualRepeatDays))
235         << "Set repeat day with 31 error.";
236 
237     repeatDays.clear();
238     for (uint8_t i = 1; i <= 31; i++) {
239         repeatDays.push_back(i);
240     }
241     calendar = std::make_shared<ReminderRequestCalendar>(nowTime, repeatMonths, repeatDays);
242     calendar->SetNextTriggerTime();
243     actualRepeatDays = calendar->GetRepeatDays();
244     EXPECT_TRUE(ReminderRequestCalendarTest::IsVectorEqual(repeatDays, actualRepeatDays))
245         << "Set repeat day with 1~31 error.";
246 }
247 
248 /**
249  * @tc.name: initDateTime_00700
250  * @tc.desc: Check repeatDay set with exception value successfully.
251  * @tc.type: FUNC
252  * @tc.require: SR000GN4CU AR000GNF1V
253  */
254 HWTEST_F(ReminderRequestCalendarTest, initDateTime_00700, Function | SmallTest | Level1)
255 {
256     time_t now;
257     (void)time(&now);  // unit is seconds.
258     tm *tmp = localtime(&now);
259     EXPECT_NE(nullptr, tmp);
260     tm nowTime = *tmp;
261     nowTime.tm_year += 1;
262     std::vector<uint8_t> repeatMonths;
263     std::vector<uint8_t> repeatDays;
264     repeatMonths.push_back(-1);
265     repeatDays.push_back(-1);
266     auto calendar = std::make_shared<ReminderRequestCalendar>(nowTime, repeatMonths, repeatDays);
267     calendar->SetNextTriggerTime();
268     std::vector<uint8_t> actualRepeatDays = calendar->GetRepeatDays();
269     EXPECT_TRUE(actualRepeatDays.size() == 0) << "Set repeat day with -1 error.";
270 
271     repeatDays.clear();
272     repeatDays.push_back(32);
273     calendar = std::make_shared<ReminderRequestCalendar>(nowTime, repeatMonths, repeatDays);
274     calendar->SetNextTriggerTime();
275     actualRepeatDays = calendar->GetRepeatDays();
276     EXPECT_TRUE(actualRepeatDays.size() == 0) << "Set repeat day with 32 error.";
277 }
278 
279 /**
280  * @tc.name: initDateTime_00800
281  * @tc.desc: Check hour set successfully.
282  * @tc.type: FUNC
283  * @tc.require: SR000GN4CU AR000GNF1V
284  */
285 HWTEST_F(ReminderRequestCalendarTest, initDateTime_00800, Function | SmallTest | Level1)
286 {
287     struct tm nowTime;
288     auto calendar = ReminderRequestCalendarTest::CreateCalendar(nowTime);
289     EXPECT_NE(nullptr, calendar);
290     EXPECT_TRUE(1 == calendar->GetHour()) << "Set hour error.";
291 }
292 
293 /**
294  * @tc.name: initDateTime_00900
295  * @tc.desc: Check minut set successfully.
296  * @tc.type: FUNC
297  * @tc.require: SR000GN4CU AR000GNF1V
298  */
299 HWTEST_F(ReminderRequestCalendarTest, initDateTime_00900, Function | SmallTest | Level1)
300 {
301     struct tm nowTime;
302     auto calendar = ReminderRequestCalendarTest::CreateCalendar(nowTime);
303     EXPECT_NE(nullptr, calendar);
304     EXPECT_TRUE(1 == calendar->GetMinute()) << "Set minute error.";
305     EXPECT_TRUE(0 == calendar->GetSecond()) << "Set seconds error.";
306 }
307 
308 /**
309  * @tc.name: initDateTime_01000
310  * @tc.desc: Test InitDateTime parameters.
311  * @tc.type: FUNC
312  * @tc.require: issue
313  */
314 HWTEST_F(ReminderRequestCalendarTest, initDateTime_01000, Function | SmallTest | Level1)
315 {
316     struct tm nowTime;
317     auto calendar = ReminderRequestCalendarTest::CreateCalendar(nowTime);
318     EXPECT_NE(nullptr, calendar);
319     calendar->InitDateTime();
320     EXPECT_EQ(calendar->IsRepeatReminder(), true);
321 }
322 
323 /**
324  * @tc.name: OnDateTimeChange_01000
325  * @tc.desc: Test OnDateTimeChange parameters.
326  * @tc.type: FUNC
327  * @tc.require: issue
328  */
329 HWTEST_F(ReminderRequestCalendarTest, OnDateTimeChange_01000, Function | SmallTest | Level1)
330 {
331     struct tm nowTime;
332     auto calendar = ReminderRequestCalendarTest::CreateCalendar(nowTime);
333     EXPECT_NE(nullptr, calendar);
334     EXPECT_EQ(calendar->OnDateTimeChange(), false);
335 }
336 
337 /**
338  * @tc.name: OnTimeZoneChange_01000
339  * @tc.desc: Test OnTimeZoneChange parameters.
340  * @tc.type: FUNC
341  * @tc.require: issue
342  */
343 HWTEST_F(ReminderRequestCalendarTest, OnTimeZoneChange_01000, Function | SmallTest | Level1)
344 {
345     struct tm nowTime;
346     auto calendar = ReminderRequestCalendarTest::CreateCalendar(nowTime);
347     EXPECT_NE(nullptr, calendar);
348     EXPECT_EQ(calendar->OnTimeZoneChange(), false);
349 }
350 
351 /**
352  * @tc.name: UpdateNextReminder_01000
353  * @tc.desc: Test UpdateNextReminder parameters.
354  * @tc.type: FUNC
355  * @tc.require: issue
356  */
357 HWTEST_F(ReminderRequestCalendarTest, UpdateNextReminder_01000, Function | SmallTest | Level1)
358 {
359     struct tm nowTime;
360     auto calendar = ReminderRequestCalendarTest::CreateCalendar(nowTime);
361     EXPECT_NE(nullptr, calendar);
362     EXPECT_EQ(calendar->UpdateNextReminder(), true);
363 }
364 
365 /**
366  * @tc.name: PreGetNextTriggerTimeIgnoreSnooze_01000
367  * @tc.desc: Test PreGetNextTriggerTimeIgnoreSnooze parameters.
368  * @tc.type: FUNC
369  * @tc.require: issue
370  */
371 HWTEST_F(ReminderRequestCalendarTest, PreGetNextTriggerTimeIgnoreSnooze_01000, Function | SmallTest | Level1)
372 {
373     bool ignoreRepeat = true;
374     bool forceToGetNext = true;
375     struct tm nowTime;
376     auto calendar = ReminderRequestCalendarTest::CreateCalendar(nowTime);
377     EXPECT_NE(nullptr, calendar);
378     EXPECT_EQ(calendar->PreGetNextTriggerTimeIgnoreSnooze(ignoreRepeat, forceToGetNext),
379     calendar->GetNextTriggerTime());
380 }
381 
382 /**
383  * @tc.name: PreGetNextTriggerTimeIgnoreSnooze_02000
384  * @tc.desc: Test PreGetNextTriggerTimeIgnoreSnooze parameters.
385  * @tc.type: FUNC
386  * @tc.require: issue
387  */
388 HWTEST_F(ReminderRequestCalendarTest, PreGetNextTriggerTimeIgnoreSnooze_02000, Function | SmallTest | Level1)
389 {
390     bool ignoreRepeat = false;
391     bool forceToGetNext = true;
392     time_t now;
393     time(&now);  // unit is seconds.
394     tm *tmp = localtime(&now);
395     EXPECT_NE(nullptr, tmp);
396     tm nowTime = *tmp;
397     nowTime.tm_year += 1;
398     std::vector<uint8_t> repeatMonths;
399     std::vector<uint8_t> repeatDays;
400     repeatMonths.push_back(-1);
401     repeatDays.push_back(1);
402     auto calendar = std::make_shared<ReminderRequestCalendar>(nowTime, repeatMonths, repeatDays);
403     EXPECT_EQ(calendar->PreGetNextTriggerTimeIgnoreSnooze(ignoreRepeat, forceToGetNext), 0);
404 }
405 
406 /**
407  * @tc.name: Marshalling_00001
408  * @tc.desc: Test Marshalling parameters.
409  * @tc.type: FUNC
410  * @tc.require: issue
411  */
412 HWTEST_F(ReminderRequestCalendarTest, Marshalling_00001, Function | SmallTest | Level1)
413 {
414     Parcel parcel;
415     struct tm nowTime;
416     auto calendar = ReminderRequestCalendarTest::CreateCalendar(nowTime);
417     EXPECT_NE(nullptr, calendar);
418     EXPECT_EQ(calendar->Marshalling(parcel), true);
419 }
420 
421 /**
422  * @tc.name: Unmarshalling_00001
423  * @tc.desc: Test Unmarshalling parameters.
424  * @tc.type: FUNC
425  * @tc.require: issue
426  */
427 HWTEST_F(ReminderRequestCalendarTest, Unmarshalling_001, Function | SmallTest | Level1)
428 {
429     bool unmarshalling = true;
430     Parcel parcel;
431     struct tm nowTime;
432     auto calendar = ReminderRequestCalendarTest::CreateCalendar(nowTime);
433     EXPECT_NE(nullptr, calendar);
434     if (nullptr != calendar) {
435         if (nullptr == calendar->Unmarshalling(parcel)) {
436             unmarshalling = false;
437         }
438     }
439     EXPECT_EQ(unmarshalling, false);
440 }
441 
442 /**
443  * @tc.name: ReadFromParcel_00001
444  * @tc.desc: Test ReadFromParcel parameters.
445  * @tc.type: FUNC
446  * @tc.require: issueI
447  */
448 HWTEST_F(ReminderRequestCalendarTest, ReadFromParcel_00001, Function | SmallTest | Level1)
449 {
450     Parcel parcel;
451     struct tm nowTime;
452     auto calendar = ReminderRequestCalendarTest::CreateCalendar(nowTime);
453     EXPECT_NE(nullptr, calendar);
454     EXPECT_EQ(calendar->ReadFromParcel(parcel), false);
455 }
456 }
457 }