1 | #include <gtest/gtest.h> |
---|
2 | #include <gmock/gmock.h> |
---|
3 | #include "util/output/OutputListener.h" |
---|
4 | #include "util/output/OutputManager.h" |
---|
5 | |
---|
6 | namespace orxonox |
---|
7 | { |
---|
8 | namespace context |
---|
9 | { |
---|
10 | namespace |
---|
11 | { |
---|
12 | REGISTER_OUTPUT_CONTEXT(unittest1); |
---|
13 | REGISTER_OUTPUT_CONTEXT(unittest2); |
---|
14 | REGISTER_OUTPUT_CONTEXT(unittest3); |
---|
15 | } |
---|
16 | } |
---|
17 | |
---|
18 | namespace |
---|
19 | { |
---|
20 | class MockOutputListener : public OutputListener |
---|
21 | { |
---|
22 | public: |
---|
23 | MockOutputListener(bool bRegister = true) : OutputListener(bRegister) {} |
---|
24 | |
---|
25 | MOCK_METHOD3(output, void(OutputLevel, const OutputContextContainer&, const std::vector<std::string>&)); |
---|
26 | |
---|
27 | inline const std::vector<AdditionalContextListener*>& getListeners() const |
---|
28 | { return OutputListener::getListeners(); } |
---|
29 | }; |
---|
30 | |
---|
31 | class MockAdditionalContextListener : public AdditionalContextListener |
---|
32 | { |
---|
33 | public: |
---|
34 | MOCK_METHOD1(updatedLevelMask, void(const OutputListener*)); |
---|
35 | MOCK_METHOD1(updatedAdditionalContextsLevelMask, void(const OutputListener*)); |
---|
36 | MOCK_METHOD1(updatedAdditionalContextsMask, void(const OutputListener*)); |
---|
37 | }; |
---|
38 | |
---|
39 | class MockOutputManager : public OutputManager |
---|
40 | { |
---|
41 | public: |
---|
42 | MOCK_METHOD1(registerListener, void(OutputListener*)); |
---|
43 | MOCK_METHOD1(unregisterListener, void(OutputListener*)); |
---|
44 | }; |
---|
45 | } |
---|
46 | |
---|
47 | // test default settings |
---|
48 | TEST(OutputListenerTest, DefaultConstructorAcceptsNothing) |
---|
49 | { |
---|
50 | MockOutputListener listener; |
---|
51 | |
---|
52 | EXPECT_EQ(level::none, listener.getLevelMask()); |
---|
53 | EXPECT_EQ(level::none, listener.getAdditionalContextsLevelMask()); |
---|
54 | EXPECT_EQ(context::none, listener.getAdditionalContextsMask()); |
---|
55 | } |
---|
56 | |
---|
57 | // test setLevelMax |
---|
58 | TEST(OutputListenerTest, SetLevelMax) |
---|
59 | { |
---|
60 | MockOutputListener listener; |
---|
61 | |
---|
62 | listener.setLevelMax(level::user_warning); |
---|
63 | |
---|
64 | OutputLevel mask = listener.getLevelMask(); |
---|
65 | |
---|
66 | EXPECT_FALSE(mask & level::none); |
---|
67 | EXPECT_TRUE(mask & level::message); |
---|
68 | EXPECT_TRUE(mask & level::debug_output); |
---|
69 | EXPECT_TRUE(mask & level::user_error); |
---|
70 | EXPECT_TRUE(mask & level::user_warning); |
---|
71 | EXPECT_FALSE(mask & level::user_status); |
---|
72 | } |
---|
73 | |
---|
74 | // test setLevelRange |
---|
75 | TEST(OutputListenerTest, SetLevelRange) |
---|
76 | { |
---|
77 | MockOutputListener listener; |
---|
78 | |
---|
79 | listener.setLevelRange(level::debug_output, level::user_warning); |
---|
80 | |
---|
81 | OutputLevel mask = listener.getLevelMask(); |
---|
82 | |
---|
83 | EXPECT_FALSE(mask & level::none); |
---|
84 | EXPECT_FALSE(mask & level::message); |
---|
85 | EXPECT_TRUE(mask & level::debug_output); |
---|
86 | EXPECT_TRUE(mask & level::user_error); |
---|
87 | EXPECT_TRUE(mask & level::user_warning); |
---|
88 | EXPECT_FALSE(mask & level::user_status); |
---|
89 | } |
---|
90 | |
---|
91 | // test setLevelMask |
---|
92 | TEST(OutputListenerTest, SetLevelMask) |
---|
93 | { |
---|
94 | MockOutputListener listener; |
---|
95 | |
---|
96 | listener.setLevelMask(static_cast<OutputLevel>(level::debug_output | level::user_warning)); |
---|
97 | |
---|
98 | OutputLevel mask = listener.getLevelMask(); |
---|
99 | |
---|
100 | EXPECT_FALSE(mask & level::none); |
---|
101 | EXPECT_FALSE(mask & level::message); |
---|
102 | EXPECT_TRUE(mask & level::debug_output); |
---|
103 | EXPECT_FALSE(mask & level::user_error); |
---|
104 | EXPECT_TRUE(mask & level::user_warning); |
---|
105 | EXPECT_FALSE(mask & level::user_status); |
---|
106 | } |
---|
107 | |
---|
108 | // test setAdditionalContextsLevelMax |
---|
109 | TEST(OutputListenerTest, SetAdditionalContextsLevelMax) |
---|
110 | { |
---|
111 | MockOutputListener listener; |
---|
112 | |
---|
113 | listener.setAdditionalContextsLevelMax(level::user_warning); |
---|
114 | |
---|
115 | OutputLevel mask = listener.getAdditionalContextsLevelMask(); |
---|
116 | |
---|
117 | EXPECT_FALSE(mask & level::none); |
---|
118 | EXPECT_TRUE(mask & level::message); |
---|
119 | EXPECT_TRUE(mask & level::debug_output); |
---|
120 | EXPECT_TRUE(mask & level::user_error); |
---|
121 | EXPECT_TRUE(mask & level::user_warning); |
---|
122 | EXPECT_FALSE(mask & level::user_status); |
---|
123 | } |
---|
124 | |
---|
125 | // test setAdditionalContextsLevelRange |
---|
126 | TEST(OutputListenerTest, SetAdditionalContextsLevelRange) |
---|
127 | { |
---|
128 | MockOutputListener listener; |
---|
129 | |
---|
130 | listener.setAdditionalContextsLevelRange(level::debug_output, level::user_warning); |
---|
131 | |
---|
132 | OutputLevel mask = listener.getAdditionalContextsLevelMask(); |
---|
133 | |
---|
134 | EXPECT_FALSE(mask & level::none); |
---|
135 | EXPECT_FALSE(mask & level::message); |
---|
136 | EXPECT_TRUE(mask & level::debug_output); |
---|
137 | EXPECT_TRUE(mask & level::user_error); |
---|
138 | EXPECT_TRUE(mask & level::user_warning); |
---|
139 | EXPECT_FALSE(mask & level::user_status); |
---|
140 | } |
---|
141 | |
---|
142 | // test setAdditionalContextsLevelMask |
---|
143 | TEST(OutputListenerTest, SetAdditionalContextsLevelMask) |
---|
144 | { |
---|
145 | MockOutputListener listener; |
---|
146 | |
---|
147 | listener.setAdditionalContextsLevelMask(static_cast<OutputLevel>(level::debug_output | level::user_warning)); |
---|
148 | |
---|
149 | OutputLevel mask = listener.getAdditionalContextsLevelMask(); |
---|
150 | |
---|
151 | EXPECT_FALSE(mask & level::none); |
---|
152 | EXPECT_FALSE(mask & level::message); |
---|
153 | EXPECT_TRUE(mask & level::debug_output); |
---|
154 | EXPECT_FALSE(mask & level::user_error); |
---|
155 | EXPECT_TRUE(mask & level::user_warning); |
---|
156 | EXPECT_FALSE(mask & level::user_status); |
---|
157 | } |
---|
158 | |
---|
159 | // test setAdditionalContextsMask |
---|
160 | TEST(OutputListenerTest, SetAdditionalContextsMask) |
---|
161 | { |
---|
162 | MockOutputListener listener; |
---|
163 | |
---|
164 | listener.setAdditionalContextsMask(context::unittest1().mask | context::unittest3().mask); |
---|
165 | |
---|
166 | OutputContextMask mask = listener.getAdditionalContextsMask(); |
---|
167 | |
---|
168 | EXPECT_TRUE(mask & context::unittest1().mask); |
---|
169 | EXPECT_FALSE(mask & context::unittest2().mask); |
---|
170 | EXPECT_TRUE(mask & context::unittest3().mask); |
---|
171 | } |
---|
172 | |
---|
173 | // test registerListener |
---|
174 | TEST(OutputListenerTest, RegisterListener) |
---|
175 | { |
---|
176 | MockOutputListener outputListener(false); |
---|
177 | MockAdditionalContextListener additionalContextListener; |
---|
178 | |
---|
179 | EXPECT_EQ(0u, outputListener.getListeners().size()); |
---|
180 | outputListener.registerListener(&additionalContextListener); |
---|
181 | EXPECT_EQ(1u, outputListener.getListeners().size()); |
---|
182 | EXPECT_EQ(&additionalContextListener, outputListener.getListeners()[0]); |
---|
183 | } |
---|
184 | |
---|
185 | // test unregisterListener |
---|
186 | TEST(OutputListenerTest, UnregisterListener) |
---|
187 | { |
---|
188 | MockOutputListener outputListener(false); |
---|
189 | MockAdditionalContextListener additionalContextListener; |
---|
190 | |
---|
191 | outputListener.registerListener(&additionalContextListener); |
---|
192 | EXPECT_EQ(1u, outputListener.getListeners().size()); |
---|
193 | EXPECT_EQ(&additionalContextListener, outputListener.getListeners()[0]); |
---|
194 | |
---|
195 | outputListener.unregisterListener(&additionalContextListener); |
---|
196 | EXPECT_EQ(0u, outputListener.getListeners().size()); |
---|
197 | } |
---|
198 | |
---|
199 | // test setLevelMask calls OutputManager::updatedLevelMask |
---|
200 | TEST(OutputListenerTest, SetLevelMaskCallsListeners) |
---|
201 | { |
---|
202 | MockOutputListener listener; |
---|
203 | MockAdditionalContextListener additionalContextListener; |
---|
204 | listener.registerListener(&additionalContextListener); |
---|
205 | |
---|
206 | EXPECT_CALL(additionalContextListener, updatedLevelMask(&listener)).Times(1); |
---|
207 | |
---|
208 | listener.setLevelMask(level::debug_output); |
---|
209 | } |
---|
210 | |
---|
211 | // test setAdditionalContextsLevelMask calls OutputManager::updatedAdditionalContextsLevelMask |
---|
212 | TEST(OutputListenerTest, SetAdditionalContextsLevelMaskCallsListeners) |
---|
213 | { |
---|
214 | MockOutputListener listener; |
---|
215 | MockAdditionalContextListener additionalContextListener; |
---|
216 | listener.registerListener(&additionalContextListener); |
---|
217 | |
---|
218 | EXPECT_CALL(additionalContextListener, updatedAdditionalContextsLevelMask(&listener)).Times(1); |
---|
219 | |
---|
220 | listener.setAdditionalContextsLevelMask(level::debug_output); |
---|
221 | } |
---|
222 | |
---|
223 | // test setAdditionalContextsMask calls OutputManager::updatedAdditionalContextsMask |
---|
224 | TEST(OutputListenerTest, SetAdditionalContextsMaskCallsListeners) |
---|
225 | { |
---|
226 | MockOutputListener listener; |
---|
227 | MockAdditionalContextListener additionalContextListener; |
---|
228 | listener.registerListener(&additionalContextListener); |
---|
229 | |
---|
230 | EXPECT_CALL(additionalContextListener, updatedAdditionalContextsMask(&listener)).Times(1); |
---|
231 | |
---|
232 | listener.setAdditionalContextsMask(context::unittest1().mask); |
---|
233 | } |
---|
234 | |
---|
235 | // test acceptsOutput |
---|
236 | namespace |
---|
237 | { |
---|
238 | void testContext(const OutputListener& listener, const OutputContextContainer& context, bool accepted) |
---|
239 | { |
---|
240 | EXPECT_TRUE(listener.acceptsOutput(level::message, context)); |
---|
241 | EXPECT_TRUE(listener.acceptsOutput(level::user_error, context)); |
---|
242 | EXPECT_TRUE(listener.acceptsOutput(level::user_warning, context)); |
---|
243 | |
---|
244 | if (accepted) |
---|
245 | { |
---|
246 | EXPECT_TRUE(listener.acceptsOutput(level::user_status, context)); |
---|
247 | EXPECT_TRUE(listener.acceptsOutput(level::internal_error, context)); |
---|
248 | EXPECT_TRUE(listener.acceptsOutput(level::internal_info, context)); |
---|
249 | } |
---|
250 | else |
---|
251 | { |
---|
252 | EXPECT_FALSE(listener.acceptsOutput(level::user_status, context)); |
---|
253 | EXPECT_FALSE(listener.acceptsOutput(level::internal_error, context)); |
---|
254 | EXPECT_FALSE(listener.acceptsOutput(level::internal_info, context)); |
---|
255 | } |
---|
256 | |
---|
257 | EXPECT_FALSE(listener.acceptsOutput(level::verbose, context)); |
---|
258 | EXPECT_FALSE(listener.acceptsOutput(level::verbose_ultra, context)); |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | TEST(OutputListenerTest, AcceptsOutputNoAdditionalContext) |
---|
263 | { |
---|
264 | MockOutputListener listener; |
---|
265 | |
---|
266 | listener.setLevelMax(level::user_warning); |
---|
267 | |
---|
268 | testContext(listener, context::undefined(), false); |
---|
269 | testContext(listener, context::unittest1(), false); |
---|
270 | testContext(listener, context::unittest2(), false); |
---|
271 | } |
---|
272 | |
---|
273 | TEST(OutputListenerTest, AcceptsOutputWithAdditionalContext) |
---|
274 | { |
---|
275 | MockOutputListener listener; |
---|
276 | |
---|
277 | listener.setLevelMax(level::user_warning); |
---|
278 | listener.setAdditionalContextsMask(context::unittest1().mask); |
---|
279 | listener.setAdditionalContextsLevelMax(level::internal_info); |
---|
280 | |
---|
281 | testContext(listener, context::undefined(), false); |
---|
282 | testContext(listener, context::unittest1(), true); |
---|
283 | testContext(listener, context::unittest2(), false); |
---|
284 | } |
---|
285 | |
---|
286 | TEST(OutputListenerTest, AcceptsOutputWithTwoAdditionalContexts) |
---|
287 | { |
---|
288 | MockOutputListener listener; |
---|
289 | |
---|
290 | listener.setLevelMax(level::user_warning); |
---|
291 | |
---|
292 | listener.setLevelMax(level::user_warning); |
---|
293 | listener.setAdditionalContextsMask(context::unittest1().mask | context::unittest2().mask); |
---|
294 | listener.setAdditionalContextsLevelMax(level::internal_info); |
---|
295 | |
---|
296 | testContext(listener, context::undefined(), false); |
---|
297 | testContext(listener, context::unittest1(), true); |
---|
298 | testContext(listener, context::unittest2(), true); |
---|
299 | } |
---|
300 | |
---|
301 | // test unfilteredOutput |
---|
302 | TEST(OutputListenerTest, UnfilteredOutputCallsOutputIfOutputAccepted) |
---|
303 | { |
---|
304 | MockOutputListener listener; |
---|
305 | |
---|
306 | listener.setLevelMax(level::user_warning); |
---|
307 | |
---|
308 | const OutputLevel& level = level::message; |
---|
309 | const OutputContextContainer& context = context::undefined(); |
---|
310 | |
---|
311 | EXPECT_TRUE(listener.acceptsOutput(level, context)); |
---|
312 | |
---|
313 | std::vector<std::string> lines; |
---|
314 | EXPECT_CALL(listener, output(level, context, lines)).Times(1); |
---|
315 | |
---|
316 | listener.unfilteredOutput(level, context, lines); |
---|
317 | } |
---|
318 | |
---|
319 | TEST(OutputListenerTest, UnfilteredOutputDoesNotCallOutputIfOutputNotAccepted) |
---|
320 | { |
---|
321 | MockOutputListener listener; |
---|
322 | |
---|
323 | listener.setLevelMax(level::user_warning); |
---|
324 | |
---|
325 | const OutputLevel& level = level::verbose; |
---|
326 | const OutputContextContainer& context = context::undefined(); |
---|
327 | |
---|
328 | EXPECT_FALSE(listener.acceptsOutput(level, context)); |
---|
329 | |
---|
330 | std::vector<std::string> lines; |
---|
331 | EXPECT_CALL(listener, output(level, context, lines)).Times(0); |
---|
332 | |
---|
333 | listener.unfilteredOutput(level, context, lines); |
---|
334 | } |
---|
335 | |
---|
336 | // Fixture |
---|
337 | class OutputListenerTestWithMockedOutputManager : public ::testing::Test |
---|
338 | { |
---|
339 | public: |
---|
340 | virtual void SetUp() |
---|
341 | { |
---|
342 | this->manager_ = new MockOutputManager(); |
---|
343 | OutputManager::Testing::getInstancePointer().reset(this->manager_); |
---|
344 | } |
---|
345 | |
---|
346 | virtual void TearDown() |
---|
347 | { |
---|
348 | OutputManager::Testing::getInstancePointer().reset(new OutputManager()); |
---|
349 | } |
---|
350 | |
---|
351 | protected: |
---|
352 | MockOutputManager* manager_; |
---|
353 | }; |
---|
354 | |
---|
355 | // test default-constructor calls OutputManager::registerListener |
---|
356 | TEST_F(OutputListenerTestWithMockedOutputManager, ConstructorRegistersInOutputManager) |
---|
357 | { |
---|
358 | EXPECT_CALL(*this->manager_, registerListener(::testing::_)).Times(1); |
---|
359 | MockOutputListener listener; |
---|
360 | } |
---|
361 | |
---|
362 | // test prevent constructor from calling OutputManager::registerListener |
---|
363 | TEST_F(OutputListenerTestWithMockedOutputManager, PreventRegisteringInOutputManager) |
---|
364 | { |
---|
365 | EXPECT_CALL(*this->manager_, registerListener(::testing::_)).Times(0); |
---|
366 | MockOutputListener listener(false); |
---|
367 | } |
---|
368 | |
---|
369 | // test destructor calls OutputManager::unregisterListener |
---|
370 | TEST_F(OutputListenerTestWithMockedOutputManager, DestructorUnregistersFromOutputManager) |
---|
371 | { |
---|
372 | MockOutputListener listener; |
---|
373 | EXPECT_CALL(*this->manager_, unregisterListener(::testing::_)).Times(1); |
---|
374 | } |
---|
375 | } |
---|