• 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.Level0)
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_TRUE(calendar);
58     ASSERT_EQ(calendar->GetId(), -1);
59 }
60 
61 HWTEST_F(CalendarManagerTest, getCalendar_test_exist, testing::ext::TestSize.Level1)
62 {
63     CalendarAccount test_account {
64         "name_getCalendar_test_exist",
65         "local",
66     };
67     auto calendar = CalendarManager::GetInstance().GetCalendar(test_account);
68     ASSERT_TRUE(calendar);
69     if (calendar->GetId() == -1 || CalendarManager::IsDefaultAccount(calendar->GetAccount())) {
70         calendar = CalendarManager::GetInstance().CreateCalendar(test_account);
71         ASSERT_TRUE(calendar);
72     }
73     auto accountExpect = calendar->GetAccount();
74     EXPECT_EQ(accountExpect.name, test_account.name);
75     EXPECT_EQ(accountExpect.type, test_account.type);
76 }
77 
78 HWTEST_F(CalendarManagerTest, createCalendar_which_not_exist, testing::ext::TestSize.Level1)
79 {
80     CalendarAccount test_account {
81         "name_createCalendar_which_not_exist",
82         "local",
83     };
84     auto calendar = CalendarManager::GetInstance().GetCalendar(test_account);
85     ASSERT_TRUE(calendar);
86     ASSERT_EQ(calendar->GetId(), -1);
87     calendar = CalendarManager::GetInstance().CreateCalendar(test_account);
88     ASSERT_TRUE(calendar);
89     auto accountExpect = calendar->GetAccount();
90     EXPECT_EQ(accountExpect.name, test_account.name);
91     EXPECT_EQ(accountExpect.type, test_account.type);
92 }
93 
94 HWTEST_F(CalendarManagerTest, createCalendar_which_already_exist, testing::ext::TestSize.Level1)
95 {
96     CalendarAccount test_account {
97         "createCalendar_which_already_exist",
98         "local",
99         "displayName_unit_test"
100     };
101     auto calendar = CalendarManager::GetInstance().GetCalendar(test_account);
102     ASSERT_TRUE(calendar);
103     ASSERT_EQ(calendar->GetId(), -1);
104     calendar = CalendarManager::GetInstance().CreateCalendar(test_account);
105     ASSERT_TRUE(calendar);
106     auto newCalendar = CalendarManager::GetInstance().CreateCalendar(test_account);
107     ASSERT_TRUE(newCalendar); // todo is bug!!
108 }
109 
110 HWTEST_F(CalendarManagerTest, getAllCalendars_test_1, testing::ext::TestSize.Level1)
111 {
112     CalendarAccount test_account {
113         "createAllCalendars_which_already_exist_1",
114         "local",
115         "displayName_unit_test"
116     };
117     CalendarAccount test_account1 {
118         "createAllCalendars_which_already_exist_2",
119         "local",
120         "displayName_unit_test"
121     };
122     CalendarManager::GetInstance().CreateCalendar(test_account);
123     CalendarManager::GetInstance().CreateCalendar(test_account1);
124 
125     auto calendars = CalendarManager::GetInstance().GetAllCalendars();
126     ASSERT_EQ(calendars.size(), 3); // 3 is include defalut calendar
127 }
128 
129 HWTEST_F(CalendarManagerTest, getAllCalendars_only_default_calendar, testing::ext::TestSize.Level1)
130 {
131     auto calendars = CalendarManager::GetInstance().GetAllCalendars();
132     ASSERT_EQ(calendars.size(), 1); // 1 is defalut calendar
133     auto calendar = calendars.at(0);
134     ASSERT_TRUE(calendar != nullptr);
135     auto account = calendar->GetAccount();
136     EXPECT_TRUE(CalendarManager::IsDefaultAccount(calendar->GetAccount()));
137 }
138 
139 HWTEST_F(CalendarManagerTest, getAccount_test_1, testing::ext::TestSize.Level1)
140 {
141     CalendarAccount test_account {
142         "name_getAccount_test_1",
143         "local",
144         "displayName_getAccount_test_1"
145     };
146     auto calendar = CalendarManager::GetInstance().GetCalendar(test_account);
147     ASSERT_TRUE(calendar);
148     ASSERT_EQ(calendar->GetId(), -1);
149     calendar = CalendarManager::GetInstance().CreateCalendar(test_account);
150     ASSERT_TRUE(calendar);
151     auto newCalendar = CalendarManager::GetInstance().CreateCalendar(test_account);
152     ASSERT_TRUE(newCalendar); // todo is bug!!
153 }
154 }