• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
17 #include <gmock/gmock.h>
18 #include <memory>
19 #include "native_calendar.h"
20 #include "native_calendar_manager.h"
21 #include "native_util.h"
22 
23 
24 namespace OHOS::CalendarApi::Native {
25 const std::string TEST_NAME = "EventRecurrenceRuleTest";
26 static CalendarAccount account {
27     TEST_NAME,
28     "local",
29     "displayName_EventRecurrenceRuleTest"
30 };
31 class EventRecurrenceRuleTest : public testing::Test {
32 public:
SetUpTestSuite(void)33     static void SetUpTestSuite(void)
34     {
35         calendar = CalendarManager::GetInstance().CreateCalendar(account);
36         ASSERT_TRUE(calendar != nullptr);
37     }
38 
TearDownTestSuite(void)39     static void TearDownTestSuite(void)
40     {
41         auto ret = CalendarManager::GetInstance().DeleteCalendar(*calendar.get());
42         ASSERT_TRUE(ret);
43     }
SetUp()44     void SetUp() {};
TearDown()45     void TearDown() {};
46     static std::shared_ptr<Calendar> calendar;
47 };
48 
49 std::shared_ptr<Calendar> EventRecurrenceRuleTest::calendar = nullptr;
50 
51 HWTEST_F(EventRecurrenceRuleTest, SetRRuleValueNORule, testing::ext::TestSize.Level1)
52 {
53     RecurrenceRule out;
54     out.recurrenceFrequency = NORULE;
55     RecurrenceRule newOut;
56     newOut.count = 10;
57     newOut.interval = 1;
58     newOut.recurrenceFrequency = DAILY;
59     std::string value;
60     std::map<std::string, std::string> ruleMap;
61     ruleMap.insert(std::pair<std::string, std::string>("FREQ", "DAILY"));
62     ruleMap.insert(std::pair<std::string, std::string>("COUNT", "10"));
63     ruleMap.insert(std::pair<std::string, std::string>("INTERVAL", "1"));
64     SetRRuleValue(ruleMap, out);
65     ASSERT_EQ(newOut.recurrenceFrequency, out.recurrenceFrequency);
66     ASSERT_EQ(newOut.interval.value(), out.interval.value());
67     ASSERT_EQ(newOut.count.value(), out.count.value());
68 }
69 
70 HWTEST_F(EventRecurrenceRuleTest, SetRRuleValueByWeekDay, testing::ext::TestSize.Level1)
71 {
72     RecurrenceRule out;
73     out.recurrenceFrequency = WEEKLY;
74     RecurrenceRule newOut;
75     newOut.recurrenceFrequency = WEEKLY;
76     newOut.daysOfWeek = std::make_optional<vector<int64_t>>();
77     newOut.daysOfWeek->push_back(3);
78     std::string value;
79     std::map<std::string, std::string> ruleMap;
80     ruleMap.insert(std::pair<std::string, std::string>("FREQ", "WEEKLY"));
81     ruleMap.insert(std::pair<std::string, std::string>("BYDAY", "WE"));
82     SetRRuleValue(ruleMap, out);
83     ASSERT_EQ(newOut.recurrenceFrequency, out.recurrenceFrequency);
84     ASSERT_EQ(newOut.daysOfWeek.value()[0], out.daysOfWeek.value()[0]);
85 }
86 
87 HWTEST_F(EventRecurrenceRuleTest, SetRRuleValueByMonthWeekDay, testing::ext::TestSize.Level1)
88 {
89     RecurrenceRule out;
90     out.recurrenceFrequency = MONTHLY;
91     RecurrenceRule newOut;
92     newOut.daysOfWeek = std::make_optional<vector<int64_t>>();
93     newOut.weeksOfMonth = std::make_optional<vector<int64_t>>();
94     newOut.daysOfWeek->push_back(2);
95     newOut.weeksOfMonth->push_back(1);
96     newOut.recurrenceFrequency = MONTHLY;
97     newOut.expire = 1713643350000;
98     std::string value;
99     std::map<std::string, std::string> ruleMap;
100     ruleMap.insert(std::pair<std::string, std::string>("FREQ", "MONTHLY"));
101     ruleMap.insert(std::pair<std::string, std::string>("BYDAY", "1TU"));
102     ruleMap.insert(std::pair<std::string, std::string>("UNTIL", "20240421T040230Z"));
103     SetRRuleValue(ruleMap, out);
104     ASSERT_EQ(newOut.recurrenceFrequency, out.recurrenceFrequency);
105     ASSERT_EQ(newOut.weeksOfMonth.value()[0], out.weeksOfMonth.value()[0]);
106     ASSERT_EQ(newOut.daysOfWeek.value()[0], out.daysOfWeek.value()[0]);
107     ASSERT_EQ(newOut.expire.value(), out.expire.value());
108 }
109 
110 HWTEST_F(EventRecurrenceRuleTest, SetRRuleValueByYearMonthDay, testing::ext::TestSize.Level1)
111 {
112     RecurrenceRule out;
113     out.recurrenceFrequency = YEARLY;
114     RecurrenceRule newOut;
115     newOut.daysOfMonth = std::make_optional<vector<int64_t>>();
116     newOut.monthsOfYear = std::make_optional<vector<int64_t>>();
117     newOut.recurrenceFrequency = YEARLY;
118     newOut.monthsOfYear->push_back(3);
119     newOut.daysOfMonth->push_back(8);
120     std::string value;
121     std::map<std::string, std::string> ruleMap;
122     ruleMap.insert(std::pair<std::string, std::string>("FREQ", "YEARLY"));
123     ruleMap.insert(std::pair<std::string, std::string>("BYMONTHDAY", "8"));
124     ruleMap.insert(std::pair<std::string, std::string>("BYMONTH", "3"));
125     SetRRuleValue(ruleMap, out);
126     ASSERT_EQ(newOut.recurrenceFrequency, out.recurrenceFrequency);
127     ASSERT_EQ(newOut.monthsOfYear.value()[0], out.monthsOfYear.value()[0]);
128     ASSERT_EQ(newOut.daysOfMonth.value()[0], out.daysOfMonth.value()[0]);
129 }
130 
131 HWTEST_F(EventRecurrenceRuleTest, SetRRuleValueByYearWeekDay, testing::ext::TestSize.Level1)
132 {
133     RecurrenceRule out;
134     out.recurrenceFrequency = YEARLY;
135     RecurrenceRule newOut;
136     newOut.daysOfWeek = std::make_optional<vector<int64_t>>();
137     newOut.weeksOfYear = std::make_optional<vector<int64_t>>();
138     newOut.recurrenceFrequency = YEARLY;
139     newOut.daysOfWeek->push_back(6);
140     newOut.weeksOfYear->push_back(6);
141     std::string value;
142     std::map<std::string, std::string> ruleMap;
143     ruleMap.insert(std::pair<std::string, std::string>("FREQ", "YEARLY"));
144     ruleMap.insert(std::pair<std::string, std::string>("BYWEEKNO", "6"));
145     ruleMap.insert(std::pair<std::string, std::string>("BYDAY", "SA"));
146     SetRRuleValue(ruleMap, out);
147     ASSERT_EQ(newOut.recurrenceFrequency, out.recurrenceFrequency);
148     ASSERT_EQ(newOut.daysOfWeek.value()[0], out.daysOfWeek.value()[0]);
149     ASSERT_EQ(newOut.weeksOfYear.value()[0], out.weeksOfYear.value()[0]);
150 }
151 
152 HWTEST_F(EventRecurrenceRuleTest, SetRRuleValueByYearDay, testing::ext::TestSize.Level1)
153 {
154     RecurrenceRule out;
155     out.recurrenceFrequency = YEARLY;
156     RecurrenceRule newOut;
157     newOut.daysOfYear = std::make_optional<vector<int64_t>>();
158     newOut.recurrenceFrequency = YEARLY;
159     newOut.daysOfYear->push_back(36);
160     std::string value;
161     std::map<std::string, std::string> ruleMap;
162     ruleMap.insert(std::pair<std::string, std::string>("FREQ", "YEARLY"));
163     ruleMap.insert(std::pair<std::string, std::string>("BYYEARDAY", "36"));
164     SetRRuleValue(ruleMap, out);
165     ASSERT_EQ(newOut.recurrenceFrequency, out.recurrenceFrequency);
166     ASSERT_EQ(newOut.daysOfYear.value()[0], out.daysOfYear.value()[0]);
167 }
168 
169 HWTEST_F(EventRecurrenceRuleTest, SetByDayOfRRuleTest, testing::ext::TestSize.Level1)
170 {
171     std::vector<std::string> weekDayList;
172     weekDayList.push_back("2MO");
173     RecurrenceRule out;
174     RecurrenceRule newOut;
175     newOut.count = 10;
176     newOut.interval = 1;
177     newOut.recurrenceFrequency = DAILY;
178     newOut.daysOfWeek = std::make_optional<vector<int64_t>>();
179     newOut.weeksOfMonth = std::make_optional<vector<int64_t>>();
180     newOut.daysOfWeek->push_back(1);
181     newOut.weeksOfMonth->push_back(2);
182     SetByDayOfRRule(weekDayList, out);
183     auto outDaysOfWeekList = out.daysOfWeek.value();
184     auto newDaysOfWeekList = newOut.daysOfWeek.value();
185     auto outWeeksOfMonthList = out.weeksOfMonth.value();
186     auto newWeeksOfMonthList = newOut.weeksOfMonth.value();
187     ASSERT_EQ(newDaysOfWeekList[0], outDaysOfWeekList[0]);
188     ASSERT_EQ(newWeeksOfMonthList[0], outWeeksOfMonthList[0]);
189 }
190 
191 HWTEST_F(EventRecurrenceRuleTest, ColorParse, testing::ext::TestSize.Level1)
192 {
193     std::string colorStr = "123";
194     variant<string, int64_t> colorValue;
195     colorValue = 123;
196     bool corlor = ColorParse(colorStr, colorValue);
197 
198     ASSERT_EQ(corlor, 0);
199 }
200 
201 HWTEST_F(EventRecurrenceRuleTest, ColorParseRed, testing::ext::TestSize.Level1)
202 {
203     std::string colorStr = "#FF0000";
204     variant<string, int64_t> colorValue;
205     colorValue = 123;
206     bool corlor = ColorParse(colorStr, colorValue);
207 
208     ASSERT_EQ(corlor, 1);
209 }
210 
211 HWTEST_F(EventRecurrenceRuleTest, ColorParseNULL, testing::ext::TestSize.Level1)
212 {
213     std::string colorStr = "";
214     variant<string, int64_t> colorValue;
215     colorValue = 123;
216     bool corlor = ColorParse(colorStr, colorValue);
217 
218     ASSERT_EQ(corlor, 0);
219 }
220 
221 HWTEST_F(EventRecurrenceRuleTest, ColorParseLen, testing::ext::TestSize.Level1)
222 {
223     std::string colorStr = "#FF000";
224     variant<string, int64_t> colorValue;
225     colorValue = 123;
226     bool corlor = ColorParse(colorStr, colorValue);
227 
228     ASSERT_EQ(corlor, 0);
229 }
230 
231 HWTEST_F(EventRecurrenceRuleTest, GetUTCTime, testing::ext::TestSize.Level1)
232 {
233     const int64_t timeValue = 1713672150000;
234     const std::string timeStr = "20240421T040230Z";
235 
236     const auto timeUTCStr = GetUTCTime(timeValue);
237 
238     EXPECT_EQ(timeStr, timeUTCStr);
239 }
240 
241 HWTEST_F(EventRecurrenceRuleTest, GetUTCTimes, testing::ext::TestSize.Level1)
242 {
243     std::vector<int64_t> excludedDates;
244     const int64_t timeValue = 1713672150000;
245     const std::string timeStr = "20240421T040230Z";
246 
247     excludedDates.emplace_back(timeValue);
248     const auto timeUTCStr = GetUTCTimes(excludedDates);
249     EXPECT_EQ(timeStr, timeUTCStr);
250 
251     const int64_t timeVal = 1718942550000;
252     excludedDates.emplace_back(timeVal);
253     const std::string timeString = "20240421T040230Z,20240621T040230Z";
254 
255     const auto timeUTCString = GetUTCTimes(excludedDates);
256     EXPECT_EQ(timeString, timeUTCString);
257 }
258 
259 HWTEST_F(EventRecurrenceRuleTest, TimeToUTC, testing::ext::TestSize.Level1)
260 {
261     const int64_t timeValue = 1713643350000;
262     const std::string timeStr = "20240421T040230Z";
263 
264     const int64_t timeUTCValue = TimeToUTC(timeStr);
265 
266     EXPECT_EQ(timeValue, timeUTCValue);
267 }
268 
269 HWTEST_F(EventRecurrenceRuleTest, TimeToUTCError, testing::ext::TestSize.Level1)
270 {
271     const int64_t timeValue = 0;
272     const std::string timeStr = "202404";
273 
274     const int64_t timeUTCValue = TimeToUTC(timeStr);
275 
276     EXPECT_EQ(timeValue, timeUTCValue);
277 }
278 HWTEST_F(EventRecurrenceRuleTest, GetRuleWithDay, testing::ext::TestSize.Level1)
279 {
280     const int64_t timeValue = 1713672150000;
281     Event event;
282     event.startTime = 1713664950000;
283     RecurrenceRule recurrenceRule;
284     recurrenceRule.recurrenceFrequency = DAILY;
285     recurrenceRule.expire = std::make_optional<int64_t>(timeValue);
286     event.recurrenceRule = std::make_optional<RecurrenceRule>(recurrenceRule);
287     std::string rrule = "FREQ=DAILY;UNTIL=20240421T040230Z;WKST=SU";
288 
289     const auto value = GetRule(event);
290 
291     EXPECT_EQ(value, rrule);
292 }
293 
294 HWTEST_F(EventRecurrenceRuleTest, GetRuleWithWeekly, testing::ext::TestSize.Level1)
295 {
296     Event event;
297     event.startTime = 1713664950000;
298     RecurrenceRule recurrenceRule;
299     recurrenceRule.recurrenceFrequency = WEEKLY;
300     recurrenceRule.interval = std::make_optional<int64_t>(2);
301     recurrenceRule.count = std::make_optional<int64_t>(3);
302     event.recurrenceRule = std::make_optional<RecurrenceRule>(recurrenceRule);
303     std::string rrule = "FREQ=WEEKLY;COUNT=3;INTERVAL=2;WKST=SU;BYDAY=SU";
304 
305     const auto value = GetRule(event);
306 
307     EXPECT_EQ(value, rrule);
308 }
309 
310 HWTEST_F(EventRecurrenceRuleTest, GetRuleWithMonthly, testing::ext::TestSize.Level1)
311 {
312     const int64_t timeValue = 1713672150000;
313     Event event;
314     event.startTime = 1713664950000;
315     RecurrenceRule recurrenceRule;
316     recurrenceRule.recurrenceFrequency = MONTHLY;
317     recurrenceRule.expire = std::make_optional<int64_t>(timeValue);
318     event.recurrenceRule = std::make_optional<RecurrenceRule>(recurrenceRule);
319     std::string rrule = "FREQ=MONTHLY;UNTIL=20240421T040230Z;WKST=SU;BYMONTHDAY=21";
320 
321     const auto value = GetRule(event);
322 
323     EXPECT_EQ(value, rrule);
324 }
325 
326 HWTEST_F(EventRecurrenceRuleTest, GetRuleWithYearly, testing::ext::TestSize.Level1)
327 {
328     const int64_t timeValue = 1713672150000;
329     Event event;
330     event.startTime = 1713664950000;
331     RecurrenceRule recurrenceRule;
332     recurrenceRule.recurrenceFrequency = YEARLY;
333     recurrenceRule.expire = std::make_optional<int64_t>(timeValue);
334     event.recurrenceRule = std::make_optional<RecurrenceRule>(recurrenceRule);
335     std::string rrule = "FREQ=YEARLY;UNTIL=20240421T040230Z;WKST=SU;BYMONTHDAY=21;BYMONTH=4";
336 
337     const auto value = GetRule(event);
338 
339     EXPECT_EQ(value, rrule);
340 }
341 
342 HWTEST_F(EventRecurrenceRuleTest, GetRuleWithWeeklyList, testing::ext::TestSize.Level1)
343 {
344     Event event;
345     RecurrenceRule recurrenceRule;
346     recurrenceRule.recurrenceFrequency = WEEKLY;
347     recurrenceRule.daysOfWeek = {1, 2, 3, 4, 5, 6, 7};
348     event.recurrenceRule = std::make_optional<RecurrenceRule>(recurrenceRule);
349     std::string rrule = "FREQ=WEEKLY;WKST=SU;BYDAY=MO,TU,WE,TH,FR,SA,SU";
350     const auto value = GetRule(event);
351 
352     EXPECT_EQ(value, rrule);
353 }
354 
355 HWTEST_F(EventRecurrenceRuleTest, GetRuleWithWeeksOfMonthlyList, testing::ext::TestSize.Level1)
356 {
357     Event event;
358     RecurrenceRule recurrenceRule;
359     recurrenceRule.recurrenceFrequency = MONTHLY;
360     recurrenceRule.weeksOfMonth = {2, 3};
361     recurrenceRule.daysOfWeek = {3, 5};
362     event.recurrenceRule = std::make_optional<RecurrenceRule>(recurrenceRule);
363     std::string rrule = "FREQ=MONTHLY;WKST=SU;BYDAY=2WE,3FR";
364     const auto value = GetRule(event);
365 
366     EXPECT_EQ(value, rrule);
367 }
368 
369 HWTEST_F(EventRecurrenceRuleTest, GetRuleWithDaysOfMonthlyList, testing::ext::TestSize.Level1)
370 {
371     Event event;
372     RecurrenceRule recurrenceRule;
373     recurrenceRule.recurrenceFrequency = MONTHLY;
374     recurrenceRule.daysOfMonth = {18, 19};
375     event.recurrenceRule = std::make_optional<RecurrenceRule>(recurrenceRule);
376     std::string rrule = "FREQ=MONTHLY;WKST=SU;BYMONTHDAY=18,19";
377     const auto value = GetRule(event);
378 
379     EXPECT_EQ(value, rrule);
380 }
381 
382 HWTEST_F(EventRecurrenceRuleTest, GetRuleWithMonthsOfYearlyList, testing::ext::TestSize.Level1)
383 {
384     Event event;
385     RecurrenceRule recurrenceRule;
386     recurrenceRule.recurrenceFrequency = YEARLY;
387     recurrenceRule.monthsOfYear = {5, 6};
388     recurrenceRule.daysOfMonth = {20, 21};
389     event.recurrenceRule = std::make_optional<RecurrenceRule>(recurrenceRule);
390     std::string rrule = "FREQ=YEARLY;WKST=SU;BYMONTHDAY=20,21;BYMONTH=5,6";
391     const auto value = GetRule(event);
392 
393     EXPECT_EQ(value, rrule);
394 }
395 
396 HWTEST_F(EventRecurrenceRuleTest, GetRuleWithDaysOfYearlyList, testing::ext::TestSize.Level1)
397 {
398     Event event;
399     RecurrenceRule recurrenceRule;
400     recurrenceRule.recurrenceFrequency = YEARLY;
401     recurrenceRule.daysOfYear = {180, 360, 365};
402     event.recurrenceRule = std::make_optional<RecurrenceRule>(recurrenceRule);
403     std::string rrule = "FREQ=YEARLY;WKST=SU;BYYEARDAY=180,360,365";
404     const auto value = GetRule(event);
405 
406     EXPECT_EQ(value, rrule);
407 }
408 
409 HWTEST_F(EventRecurrenceRuleTest, GetRuleWithWeeksOfYearlyList, testing::ext::TestSize.Level1)
410 {
411     Event event;
412     RecurrenceRule recurrenceRule;
413     recurrenceRule.recurrenceFrequency = YEARLY;
414     recurrenceRule.weeksOfYear = {5, 6, 7};
415     recurrenceRule.daysOfWeek = {5, 6, 7};
416     event.recurrenceRule = std::make_optional<RecurrenceRule>(recurrenceRule);
417     std::string rrule = "FREQ=YEARLY;WKST=SU;BYWEEKNO=5,6,7;BYDAY=FR,SA,SU";
418     const auto value = GetRule(event);
419 
420     EXPECT_EQ(value, rrule);
421 }
422 
423 HWTEST_F(EventRecurrenceRuleTest, GetRuleWithYearlyList, testing::ext::TestSize.Level1)
424 {
425     Event event;
426     RecurrenceRule recurrenceRule;
427     recurrenceRule.recurrenceFrequency = YEARLY;
428     recurrenceRule.interval = 2;
429     event.recurrenceRule = std::make_optional<RecurrenceRule>(recurrenceRule);
430     std::string rrule = "FREQ=YEARLY;INTERVAL=2;WKST=SU;BYMONTHDAY=1;BYMONTH=1";
431     const auto value = GetRule(event);
432 
433     EXPECT_EQ(value, rrule);
434 }
435 
436 HWTEST_F(EventRecurrenceRuleTest, GetRuleWithDayOfWeekMonthYearlyList, testing::ext::TestSize.Level1)
437 {
438     Event event;
439     RecurrenceRule recurrenceRule;
440     recurrenceRule.recurrenceFrequency = YEARLY;
441     recurrenceRule.daysOfWeek = {1, 3, 5};
442     recurrenceRule.weeksOfMonth = {2, 3, 4};
443     recurrenceRule.monthsOfYear = {6, 7, 8};
444     event.recurrenceRule = std::make_optional<RecurrenceRule>(recurrenceRule);
445     std::string rrule = "FREQ=YEARLY;WKST=SU;BYDAY=2MO,3WE,4FR;BYMONTH=6,7,8";
446     const auto value = GetRule(event);
447 
448     EXPECT_EQ(value, rrule);
449 }
450 
451 HWTEST_F(EventRecurrenceRuleTest, BuildValueEventRecurrenceRule, testing::ext::TestSize.Level1)
452 {
453     Event event;
454     event.identifier = std::make_optional<std::string>("1111");
455     event.isLunar = std::make_optional<bool>(true);
456     RecurrenceRule recurrenceRule;
457     recurrenceRule.recurrenceFrequency = YEARLY;
458     recurrenceRule.daysOfWeek = {1, 3, 5};
459     recurrenceRule.weeksOfMonth = {2, 3, 4};
460     recurrenceRule.monthsOfYear = {6, 7, 8};
461     recurrenceRule.excludedDates = {1727254841000};
462     event.recurrenceRule = std::make_optional<RecurrenceRule>(recurrenceRule);
463     std::string rrule = "FREQ=YEARLY;WKST=SU;BYDAY=2MO,3WE,4FR;BYMONTH=6,7,8";
464     std::string excludedDateStr = "20240925T090041Z";
465     DataShare::DataShareValuesBucket newShareValuesBucket;
466     newShareValuesBucket.Put("rrule", rrule);
467     newShareValuesBucket.Put("exdate", excludedDateStr);
468     auto shareValuesBucket = BuildValueEvent(event, 0, 0);
469     auto itRrule = shareValuesBucket.valuesMap.find("rrule");
470     auto *itRruleValue = std::get_if<std::string>(&itRrule->second);
471     std::string rruleVal = *itRruleValue;
472     auto itExcludedDate = shareValuesBucket.valuesMap.find("exdate");
473     auto *itExcludedDateValue = std::get_if<std::string>(&itExcludedDate->second);
474     std::string excludedDate = *itExcludedDateValue;
475     EXPECT_EQ(rrule, rruleVal);
476     EXPECT_EQ(excludedDateStr, excludedDate);
477 }
478 
479 }