• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 <gtest/gtest.h>
17 #include <random>
18 #include "calendar_log.h"
19 #include "native_calendar_manager.h"
20 
21 namespace OHOS::CalendarApi::Native {
22 class CalendarManagerTest : public testing::Test {
23 public:
24     /* SetUpTestCase:The preset action of the test suite is executed before the first TestCase */
SetUpTestSuite(void)25     static void SetUpTestSuite(void) {}
26 
27     /* TearDownTestCase:The test suite cleanup action is executed after the last TestCase */
TearDownTestSuite(void)28     static void TearDownTestSuite(void) {}
29     /* SetUp:Execute before each test case */
SetUp()30     void SetUp()
31     {
32         CalendarManager::GetInstance().DeleteAllCalendars();
33     }
34 
35     /* TearDown:Execute after each test case */
TearDown()36     void TearDown()
37     {
38     }
39 };
40 
41 HWTEST_F(CalendarManagerTest, getCalendar_with_not_account, testing::ext::TestSize.Level1)
42 {
43     auto calendar = CalendarManager::GetInstance().GetCalendar(std::nullopt);
44     ASSERT_TRUE(calendar != nullptr);
45     auto account = calendar->GetAccount();
46     EXPECT_TRUE(CalendarManager::IsDefaultAccount(calendar->GetAccount()));
47 }
48 
49 HWTEST_F(CalendarManagerTest, getCalendar_which_no_exist, testing::ext::TestSize.Level1)
50 {
51     CalendarAccount test_account {
52         "name_getCalendar_which_no_exist",
53         "local",
54         "displayName_getCalendar_which_no_exist"
55     };
56     auto calendar = CalendarManager::GetInstance().GetCalendar(test_account);
57     ASSERT_FALSE(calendar);
58 }
59 
60 HWTEST_F(CalendarManagerTest, getCalendar_test_exist, testing::ext::TestSize.Level1)
61 {
62     CalendarAccount test_account {
63         "name_getCalendar_test_exist",
64         "local",
65     };
66     auto calendar = CalendarManager::GetInstance().GetCalendar(test_account);
67     if (!calendar || CalendarManager::IsDefaultAccount(calendar->GetAccount())) {
68         calendar = CalendarManager::GetInstance().CreateCalendar(test_account);
69         ASSERT_TRUE(calendar);
70     }
71     auto accountExpect = calendar->GetAccount();
72     EXPECT_EQ(accountExpect.name, test_account.name);
73     EXPECT_EQ(accountExpect.type, test_account.type);
74 }
75 
76 HWTEST_F(CalendarManagerTest, createCalendar_which_not_exist, testing::ext::TestSize.Level1)
77 {
78     CalendarAccount test_account {
79         "name_createCalendar_which_not_exist",
80         "local",
81     };
82     auto calendar = CalendarManager::GetInstance().GetCalendar(test_account);
83     ASSERT_FALSE(calendar);
84     calendar = CalendarManager::GetInstance().CreateCalendar(test_account);
85     ASSERT_TRUE(calendar);
86     auto accountExpect = calendar->GetAccount();
87     EXPECT_EQ(accountExpect.name, test_account.name);
88     EXPECT_EQ(accountExpect.type, test_account.type);
89 }
90 
91 HWTEST_F(CalendarManagerTest, createCalendar_which_already_exist, testing::ext::TestSize.Level1)
92 {
93     CalendarAccount test_account {
94         "createCalendar_which_already_exist",
95         "local",
96         "displayName_unit_test"
97     };
98     auto calendar = CalendarManager::GetInstance().GetCalendar(test_account);
99     ASSERT_FALSE(calendar);
100     calendar = CalendarManager::GetInstance().CreateCalendar(test_account);
101     ASSERT_TRUE(calendar);
102     auto newCalendar = CalendarManager::GetInstance().CreateCalendar(test_account);
103     ASSERT_TRUE(newCalendar); // todo is bug!!
104 }
105 
106 HWTEST_F(CalendarManagerTest, getAllCalendars_test_1, testing::ext::TestSize.Level1)
107 {
108     CalendarAccount test_account {
109         "createAllCalendars_which_already_exist_1",
110         "local",
111         "displayName_unit_test"
112     };
113     CalendarAccount test_account1 {
114         "createAllCalendars_which_already_exist_2",
115         "local",
116         "displayName_unit_test"
117     };
118     CalendarManager::GetInstance().CreateCalendar(test_account);
119     CalendarManager::GetInstance().CreateCalendar(test_account1);
120 
121     auto calendars = CalendarManager::GetInstance().GetAllCalendars();
122     ASSERT_EQ(calendars.size(), 3); // 3 is include defalut calendar
123 }
124 
125 HWTEST_F(CalendarManagerTest, getAllCalendars_only_default_calendar, testing::ext::TestSize.Level1)
126 {
127     auto calendars = CalendarManager::GetInstance().GetAllCalendars();
128     ASSERT_EQ(calendars.size(), 1); // 1 is defalut calendar
129     auto calendar = calendars.at(0);
130     ASSERT_TRUE(calendar != nullptr);
131     auto account = calendar->GetAccount();
132     EXPECT_TRUE(CalendarManager::IsDefaultAccount(calendar->GetAccount()));
133 }
134 
135 HWTEST_F(CalendarManagerTest, getAccount_test_1, testing::ext::TestSize.Level1)
136 {
137     CalendarAccount test_account {
138         "name_getAccount_test_1",
139         "local",
140         "displayName_getAccount_test_1"
141     };
142     auto calendar = CalendarManager::GetInstance().GetCalendar(test_account);
143     ASSERT_FALSE(calendar);
144     calendar = CalendarManager::GetInstance().CreateCalendar(test_account);
145     ASSERT_TRUE(calendar);
146     auto newCalendar = CalendarManager::GetInstance().CreateCalendar(test_account);
147     ASSERT_TRUE(newCalendar); // todo is bug!!
148 }
149 }