blob: da935cf747db9e55070c11584325a5450e9b7f6a [file] [log] [blame]
William A. Kennington III8f90e282018-07-17 14:40:14 -07001#include <gmock/gmock.h>
2#include <gtest/gtest.h>
3#include <sdeventplus/event.hpp>
4#include <sdeventplus/exception.hpp>
5#include <sdeventplus/test/sdevent.hpp>
6#include <type_traits>
7
8namespace sdeventplus
9{
10namespace
11{
12
13using testing::DoAll;
14using testing::Return;
15using testing::SetArgPointee;
16
17class EventTest : public testing::Test
18{
19 protected:
20 testing::StrictMock<SdEventMock> mock;
21 sd_event *const expected_event = reinterpret_cast<sd_event *>(1234);
22};
23
24TEST_F(EventTest, NewEventRef)
25{
26 EXPECT_CALL(mock, sd_event_ref(expected_event))
27 .WillOnce(Return(expected_event));
28 Event event(expected_event, &mock);
29
30 EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
31}
32
33TEST_F(EventTest, NewEventNoRef)
34{
35 Event event(expected_event, std::false_type(), &mock);
36
37 EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
38}
39
40TEST_F(EventTest, GetNewEvent)
41{
42 EXPECT_CALL(mock, sd_event_new(testing::_))
43 .WillOnce(DoAll(SetArgPointee<0>(expected_event), Return(0)));
44 Event event = Event::get_new(&mock);
45
46 EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
47}
48
49TEST_F(EventTest, GetNewEventFail)
50{
51 EXPECT_CALL(mock, sd_event_new(testing::_)).WillOnce(Return(-EINVAL));
52 EXPECT_THROW(Event::get_new(&mock), SdEventError);
53}
54
55TEST_F(EventTest, GetDefaultEvent)
56{
57 EXPECT_CALL(mock, sd_event_default(testing::_))
58 .WillOnce(DoAll(SetArgPointee<0>(expected_event), Return(0)));
59 Event event = Event::get_default(&mock);
60
61 EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
62}
63
64TEST_F(EventTest, GetDefaultEventFail)
65{
66 EXPECT_CALL(mock, sd_event_default(testing::_)).WillOnce(Return(-EINVAL));
67 EXPECT_THROW(Event::get_default(&mock), SdEventError);
68}
69
70TEST_F(EventTest, LoopSuccess)
71{
72 EXPECT_CALL(mock, sd_event_loop(expected_event)).WillOnce(Return(0));
73 EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
74 EXPECT_EQ(0, Event(expected_event, std::false_type(), &mock).loop());
75}
76
77TEST_F(EventTest, LoopUserError)
78{
79 const int user_error = 10;
80 EXPECT_CALL(mock, sd_event_loop(expected_event))
81 .WillOnce(Return(user_error));
82 EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
83 EXPECT_EQ(user_error,
84 Event(expected_event, std::false_type(), &mock).loop());
85}
86
87TEST_F(EventTest, LoopInternalError)
88{
89 EXPECT_CALL(mock, sd_event_loop(expected_event)).WillOnce(Return(-EINVAL));
90 EXPECT_CALL(mock, sd_event_unref(expected_event)).WillOnce(Return(nullptr));
91 EXPECT_THROW(Event(expected_event, std::false_type(), &mock).loop(),
92 SdEventError);
93}
94
95} // namespace
96} // namespace sdeventplus