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 #include <gtest/gtest.h> 16 #include <gmock/gmock.h> 17 #include <memory> 18 #include "native_calendar.h" 19 #include "calendar_log.h" 20 #include "native_calendar_manager.h" 21 22 23 namespace OHOS::CalendarApi::Native { 24 const std::string TEST_NAME = "EventServiceTest"; 25 static CalendarAccount account { 26 TEST_NAME, 27 "local", 28 "EventServiceTest" 29 }; 30 class EventServiceTest : public testing::Test { 31 public: SetUpTestSuite(void)32 static void SetUpTestSuite(void) 33 { 34 calendar = CalendarManager::GetInstance().CreateCalendar(account); 35 ASSERT_TRUE(calendar != nullptr); 36 } 37 TearDownTestSuite(void)38 static void TearDownTestSuite(void) 39 { 40 auto ret = CalendarManager::GetInstance().DeleteCalendar(*calendar.get()); 41 ASSERT_TRUE(ret); 42 } SetUp()43 void SetUp() {}; TearDown()44 void TearDown() {}; 45 static std::shared_ptr<Calendar> calendar; 46 }; 47 48 std::shared_ptr<Calendar> EventServiceTest::calendar = nullptr; 49 50 HWTEST_F(EventServiceTest, AddEventWithService, testing::ext::TestSize.Level1) 51 { 52 Event event; 53 const string title = "AddEventWithService"; 54 event.title = title; 55 EventService testService { 56 "Meeting", 57 "//testuri", 58 "test_discription" 59 }; 60 event.service = std::make_optional<EventService>(testService); 61 auto eventId = calendar->AddEvent(event); 62 ASSERT_NE(eventId, 0); 63 auto events = calendar->GetEvents(FilterByTitle(title), {}); 64 ASSERT_EQ(events.size(), 1); 65 auto resultEvent = events.at(0); 66 EXPECT_EQ(resultEvent.title.value(), title); 67 ASSERT_NE(resultEvent.service, std::nullopt); 68 auto result = resultEvent.service.value(); 69 EXPECT_EQ(result.type, testService.type); 70 EXPECT_EQ(result.uri, testService.uri); 71 EXPECT_EQ(result.description.value(), testService.description.value()); 72 } 73 74 HWTEST_F(EventServiceTest, AddEventWithNoService, testing::ext::TestSize.Level1) 75 { 76 Event event; 77 const string title = "AddEventWithNoService"; 78 event.title = title; 79 auto eventId = calendar->AddEvent(event); 80 ASSERT_NE(eventId, 0); 81 auto events = calendar->GetEvents(FilterByTitle(title), {}); 82 ASSERT_EQ(events.size(), 1); 83 auto resultEvent = events.at(0); 84 EXPECT_EQ(resultEvent.title.value(), title); 85 ASSERT_EQ(resultEvent.service, std::nullopt); 86 } 87 }