William A. Kennington III | 8f90e28 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 1 | #include <gmock/gmock.h> |
| 2 | #include <gtest/gtest.h> |
William A. Kennington III | 631c246 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 3 | #include <memory> |
William A. Kennington III | 8f90e28 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 4 | #include <sdeventplus/event.hpp> |
| 5 | #include <sdeventplus/exception.hpp> |
| 6 | #include <sdeventplus/test/sdevent.hpp> |
| 7 | #include <type_traits> |
| 8 | |
| 9 | namespace sdeventplus |
| 10 | { |
| 11 | namespace |
| 12 | { |
| 13 | |
| 14 | using testing::DoAll; |
| 15 | using testing::Return; |
| 16 | using testing::SetArgPointee; |
| 17 | |
| 18 | class EventTest : public testing::Test |
| 19 | { |
| 20 | protected: |
William A. Kennington III | 0a816c5 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 21 | testing::StrictMock<test::SdEventMock> mock; |
| 22 | sd_event* const expected_event = reinterpret_cast<sd_event*>(1234); |
William A. Kennington III | 8f90e28 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | TEST_F(EventTest, NewEventRef) |
| 26 | { |
| 27 | EXPECT_CALL(mock, sd_event_ref(expected_event)) |
| 28 | .WillOnce(Return(expected_event)); |
| 29 | Event event(expected_event, &mock); |
| 30 | |
| 31 | EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr)); |
| 32 | } |
| 33 | |
| 34 | TEST_F(EventTest, NewEventNoRef) |
| 35 | { |
| 36 | Event event(expected_event, std::false_type(), &mock); |
| 37 | |
| 38 | EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr)); |
| 39 | } |
| 40 | |
| 41 | TEST_F(EventTest, GetNewEvent) |
| 42 | { |
| 43 | EXPECT_CALL(mock, sd_event_new(testing::_)) |
| 44 | .WillOnce(DoAll(SetArgPointee<0>(expected_event), Return(0))); |
| 45 | Event event = Event::get_new(&mock); |
| 46 | |
| 47 | EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr)); |
| 48 | } |
| 49 | |
| 50 | TEST_F(EventTest, GetNewEventFail) |
| 51 | { |
| 52 | EXPECT_CALL(mock, sd_event_new(testing::_)).WillOnce(Return(-EINVAL)); |
| 53 | EXPECT_THROW(Event::get_new(&mock), SdEventError); |
| 54 | } |
| 55 | |
| 56 | TEST_F(EventTest, GetDefaultEvent) |
| 57 | { |
| 58 | EXPECT_CALL(mock, sd_event_default(testing::_)) |
| 59 | .WillOnce(DoAll(SetArgPointee<0>(expected_event), Return(0))); |
| 60 | Event event = Event::get_default(&mock); |
| 61 | |
| 62 | EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr)); |
| 63 | } |
| 64 | |
| 65 | TEST_F(EventTest, GetDefaultEventFail) |
| 66 | { |
| 67 | EXPECT_CALL(mock, sd_event_default(testing::_)).WillOnce(Return(-EINVAL)); |
| 68 | EXPECT_THROW(Event::get_default(&mock), SdEventError); |
| 69 | } |
| 70 | |
William A. Kennington III | 631c246 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 71 | class EventMethodTest : public EventTest |
| 72 | { |
| 73 | protected: |
| 74 | std::unique_ptr<Event> event; |
| 75 | |
| 76 | void SetUp() |
| 77 | { |
| 78 | event = |
| 79 | std::make_unique<Event>(expected_event, std::false_type(), &mock); |
| 80 | } |
| 81 | |
| 82 | void TearDown() |
| 83 | { |
| 84 | EXPECT_CALL(mock, sd_event_unref(expected_event)) |
| 85 | .WillOnce(Return(nullptr)); |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | TEST_F(EventMethodTest, LoopSuccess) |
William A. Kennington III | 8f90e28 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 90 | { |
| 91 | EXPECT_CALL(mock, sd_event_loop(expected_event)).WillOnce(Return(0)); |
William A. Kennington III | 631c246 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 92 | EXPECT_EQ(0, event->loop()); |
William A. Kennington III | 8f90e28 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 93 | } |
| 94 | |
William A. Kennington III | 631c246 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 95 | TEST_F(EventMethodTest, LoopUserError) |
William A. Kennington III | 8f90e28 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 96 | { |
| 97 | const int user_error = 10; |
| 98 | EXPECT_CALL(mock, sd_event_loop(expected_event)) |
| 99 | .WillOnce(Return(user_error)); |
William A. Kennington III | 6364dd9 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 100 | EXPECT_EQ(user_error, event->loop()); |
William A. Kennington III | 8f90e28 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 101 | } |
| 102 | |
William A. Kennington III | 631c246 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 103 | TEST_F(EventMethodTest, LoopInternalError) |
William A. Kennington III | 8f90e28 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 104 | { |
| 105 | EXPECT_CALL(mock, sd_event_loop(expected_event)).WillOnce(Return(-EINVAL)); |
William A. Kennington III | 631c246 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 106 | EXPECT_THROW(event->loop(), SdEventError); |
| 107 | } |
| 108 | |
| 109 | TEST_F(EventMethodTest, GetWatchdogSuccess) |
| 110 | { |
| 111 | EXPECT_CALL(mock, sd_event_get_watchdog(expected_event)) |
| 112 | .WillOnce(Return(0)); |
| 113 | EXPECT_EQ(0, event->get_watchdog()); |
| 114 | |
| 115 | EXPECT_CALL(mock, sd_event_get_watchdog(expected_event)) |
| 116 | .WillOnce(Return(2)); |
| 117 | EXPECT_EQ(2, event->get_watchdog()); |
| 118 | } |
| 119 | |
| 120 | TEST_F(EventMethodTest, GetWatchdogError) |
| 121 | { |
| 122 | EXPECT_CALL(mock, sd_event_get_watchdog(expected_event)) |
| 123 | .WillOnce(Return(-EINVAL)); |
| 124 | EXPECT_THROW(event->get_watchdog(), SdEventError); |
| 125 | } |
| 126 | |
| 127 | TEST_F(EventMethodTest, SetWatchdogSuccess) |
| 128 | { |
| 129 | EXPECT_CALL(mock, sd_event_set_watchdog(expected_event, 0)) |
| 130 | .WillOnce(Return(0)); |
| 131 | EXPECT_EQ(0, event->set_watchdog(0)); |
| 132 | |
| 133 | EXPECT_CALL(mock, sd_event_set_watchdog(expected_event, 1)) |
| 134 | .WillOnce(Return(2)); |
| 135 | EXPECT_EQ(2, event->set_watchdog(1)); |
| 136 | } |
| 137 | |
| 138 | TEST_F(EventMethodTest, SetWatchdogError) |
| 139 | { |
| 140 | EXPECT_CALL(mock, sd_event_set_watchdog(expected_event, 1)) |
| 141 | .WillOnce(Return(-EINVAL)); |
| 142 | EXPECT_THROW(event->set_watchdog(1), SdEventError); |
William A. Kennington III | 8f90e28 | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | } // namespace |
| 146 | } // namespace sdeventplus |