blob: ce95f121f57ca8b510f3d4ef41c5dce1e1eff33a [file] [log] [blame]
William A. Kennington III0a816c52018-07-17 14:40:14 -07001#include <gmock/gmock.h>
2#include <gtest/gtest.h>
William A. Kennington III65db8632018-07-17 14:40:14 -07003#include <sdeventplus/exception.hpp>
William A. Kennington III0a816c52018-07-17 14:40:14 -07004#include <sdeventplus/internal/sdevent.hpp>
5#include <sdeventplus/source/base.hpp>
6#include <sdeventplus/test/sdevent.hpp>
William A. Kennington III65db8632018-07-17 14:40:14 -07007#include <string>
William A. Kennington III0a816c52018-07-17 14:40:14 -07008#include <type_traits>
9
10namespace sdeventplus
11{
12namespace source
13{
14namespace
15{
16
William A. Kennington III65db8632018-07-17 14:40:14 -070017using testing::DoAll;
William A. Kennington III0a816c52018-07-17 14:40:14 -070018using testing::Return;
William A. Kennington III65db8632018-07-17 14:40:14 -070019using testing::SetArgPointee;
William A. Kennington III0a816c52018-07-17 14:40:14 -070020
21class BaseImpl : public Base
22{
23 public:
24 BaseImpl(sd_event_source* source, internal::SdEvent* sdevent) :
25 Base(source, sdevent)
26 {
27 }
28 BaseImpl(sd_event_source* source, std::false_type,
29 internal::SdEvent* sdevent) :
30 Base(source, std::false_type(), sdevent)
31 {
32 }
33};
34
35class BaseTest : public testing::Test
36{
37 protected:
38 testing::StrictMock<test::SdEventMock> mock;
39 sd_event_source* const expected_source =
40 reinterpret_cast<sd_event_source*>(1234);
41};
42
43TEST_F(BaseTest, NewBaseRef)
44{
45 EXPECT_CALL(mock, sd_event_source_ref(expected_source))
46 .WillOnce(Return(expected_source));
47 BaseImpl source(expected_source, &mock);
48
William A. Kennington III65db8632018-07-17 14:40:14 -070049 {
50 testing::InSequence seq;
51 EXPECT_CALL(mock,
52 sd_event_source_set_enabled(expected_source, SD_EVENT_OFF))
53 .WillOnce(Return(0));
54 EXPECT_CALL(mock, sd_event_source_unref(expected_source))
55 .WillOnce(Return(nullptr));
56 }
William A. Kennington III0a816c52018-07-17 14:40:14 -070057}
58
59TEST_F(BaseTest, NewBaseNoRef)
60{
61 BaseImpl source(expected_source, std::false_type(), &mock);
62
William A. Kennington III65db8632018-07-17 14:40:14 -070063 {
64 testing::InSequence seq;
65 EXPECT_CALL(mock,
66 sd_event_source_set_enabled(expected_source, SD_EVENT_OFF))
67 .WillOnce(Return(0));
68 EXPECT_CALL(mock, sd_event_source_unref(expected_source))
69 .WillOnce(Return(nullptr));
70 }
71}
72
73class BaseMethodTest : public BaseTest
74{
75 protected:
76 std::unique_ptr<BaseImpl> base;
77
78 void SetUp()
79 {
80 base = std::make_unique<BaseImpl>(expected_source, std::false_type(),
81 &mock);
82 }
83
84 void TearDown()
85 {
86 {
87 testing::InSequence seq;
88 EXPECT_CALL(mock, sd_event_source_set_enabled(expected_source,
89 SD_EVENT_OFF))
90 .WillOnce(Return(0));
91 EXPECT_CALL(mock, sd_event_source_unref(expected_source))
92 .WillOnce(Return(nullptr));
93 }
94 }
95};
96
97TEST_F(BaseMethodTest, GetDescriptionSuccess)
98{
99 const char* expected = "test_desc";
100 EXPECT_CALL(mock,
101 sd_event_source_get_description(expected_source, testing::_))
102 .WillOnce(DoAll(SetArgPointee<1>(expected), Return(0)));
103 // Intentionally comparing pointers to make sure no copying is happening
104 EXPECT_EQ(expected, base->get_description());
105}
106
107TEST_F(BaseMethodTest, GetDescriptionError)
108{
109 EXPECT_CALL(mock,
110 sd_event_source_get_description(expected_source, testing::_))
111 .WillOnce(Return(-EINVAL));
112 EXPECT_THROW(base->get_description(), SdEventError);
113}
114
115TEST_F(BaseMethodTest, SetDescriptionSuccess)
116{
117 const char* expected = "test desc";
118 // Intentionally comparing pointers to make sure no copying is happening
119 EXPECT_CALL(mock,
120 sd_event_source_set_description(expected_source, expected))
121 .WillOnce(Return(0));
122 base->set_description(expected);
123}
124
125TEST_F(BaseMethodTest, SetDescriptionError)
126{
127 const char* expected = "test desc";
128 // Intentionally comparing pointers to make sure no copying is happening
129 EXPECT_CALL(mock,
130 sd_event_source_set_description(expected_source, expected))
131 .WillOnce(Return(-EINVAL));
132 EXPECT_THROW(base->set_description(expected), SdEventError);
133}
134
135TEST_F(BaseMethodTest, GetPendingSuccess)
136{
137 EXPECT_CALL(mock, sd_event_source_get_pending(expected_source))
138 .WillOnce(Return(0));
139 EXPECT_EQ(0, base->get_pending());
140 EXPECT_CALL(mock, sd_event_source_get_pending(expected_source))
141 .WillOnce(Return(4));
142 EXPECT_EQ(4, base->get_pending());
143}
144
145TEST_F(BaseMethodTest, GetPendingError)
146{
147 EXPECT_CALL(mock, sd_event_source_get_pending(expected_source))
148 .WillOnce(Return(-EINVAL));
149 EXPECT_THROW(base->get_pending(), SdEventError);
150}
151
152TEST_F(BaseMethodTest, GetPrioritySuccess)
153{
154 EXPECT_CALL(mock, sd_event_source_get_priority(expected_source, testing::_))
155 .WillOnce(DoAll(SetArgPointee<1>(1024), Return(0)));
156 EXPECT_EQ(1024, base->get_priority());
157}
158
159TEST_F(BaseMethodTest, GetPriorityError)
160{
161 EXPECT_CALL(mock, sd_event_source_get_priority(expected_source, testing::_))
162 .WillOnce(Return(-EINVAL));
163 EXPECT_THROW(base->get_priority(), SdEventError);
164}
165
166TEST_F(BaseMethodTest, SetPrioritySuccess)
167{
168 EXPECT_CALL(mock, sd_event_source_set_priority(expected_source, 1024))
169 .WillOnce(Return(0));
170 base->set_priority(1024);
171}
172
173TEST_F(BaseMethodTest, SetPriorityError)
174{
175 EXPECT_CALL(mock, sd_event_source_set_priority(expected_source, 1024))
176 .WillOnce(Return(-EINVAL));
177 EXPECT_THROW(base->set_priority(1024), SdEventError);
178}
179
180TEST_F(BaseMethodTest, GetEnabledSuccess)
181{
182 EXPECT_CALL(mock, sd_event_source_get_enabled(expected_source, testing::_))
183 .WillOnce(DoAll(SetArgPointee<1>(SD_EVENT_ON), Return(0)));
184 EXPECT_EQ(SD_EVENT_ON, base->get_enabled());
185}
186
187TEST_F(BaseMethodTest, GetEnabledError)
188{
189 EXPECT_CALL(mock, sd_event_source_get_enabled(expected_source, testing::_))
190 .WillOnce(Return(-EINVAL));
191 EXPECT_THROW(base->get_enabled(), SdEventError);
192}
193
194TEST_F(BaseMethodTest, SetEnabledSuccess)
195{
196 EXPECT_CALL(mock, sd_event_source_set_enabled(expected_source, SD_EVENT_ON))
197 .WillOnce(Return(0));
198 base->set_enabled(SD_EVENT_ON);
199}
200
201TEST_F(BaseMethodTest, SetEnabledError)
202{
203 EXPECT_CALL(mock, sd_event_source_set_enabled(expected_source, SD_EVENT_ON))
204 .WillOnce(Return(-EINVAL));
205 EXPECT_THROW(base->set_enabled(SD_EVENT_ON), SdEventError);
William A. Kennington III0a816c52018-07-17 14:40:14 -0700206}
207
208} // namespace
209} // namespace source
210} // namespace sdeventplus