Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/test/util/output/OutputManagerTest.cc @ 12205

Last change on this file since 12205 was 11071, checked in by landauf, 9 years ago

merged branch cpp11_v3 back to trunk

  • Property svn:eol-style set to native
File size: 18.6 KB
Line 
1#include <gtest/gtest.h>
2#include <gmock/gmock.h>
3
4#define DISABLE_OUTPUT_CONTEXT_STATIC_CACHE
5
6#include "util/Output.h"
7#include "util/output/OutputManager.h"
8#include "util/output/OutputListener.h"
9
10namespace orxonox
11{
12    namespace context
13    {
14        namespace
15        {
16            REGISTER_OUTPUT_CONTEXT(unittest1);
17            REGISTER_OUTPUT_CONTEXT(unittest2);
18            REGISTER_OUTPUT_CONTEXT(unittest3);
19
20            REGISTER_OUTPUT_SUBCONTEXT(unittest3, sub1);
21            REGISTER_OUTPUT_SUBCONTEXT(unittest3, sub2);
22            REGISTER_OUTPUT_SUBCONTEXT(unittest3, sub3);
23        }
24    }
25
26    namespace
27    {
28        class MockOutputManager : public OutputManager
29        {
30            public:
31                MOCK_METHOD1(updatedLevelMask, void(const OutputListener*));
32                MOCK_METHOD1(updatedAdditionalContextsLevelMask, void(const OutputListener*));
33                MOCK_METHOD1(updatedAdditionalContextsMask, void(const OutputListener*));
34
35                void originalUpdatedLevelMask(const OutputListener* listener) { this->OutputManager::updatedLevelMask(listener); }
36                void originalUpdatedAdditionalContextsLevelMask(const OutputListener* listener) { this->OutputManager::updatedAdditionalContextsLevelMask(listener); }
37                void originalUpdatedAdditionalContextsMask(const OutputListener* listener) { this->OutputManager::updatedAdditionalContextsMask(listener); }
38        };
39
40        class MockOutputListener : public OutputListener
41        {
42            public:
43                MockOutputListener(bool bRegister = true) : OutputListener(bRegister) {}
44
45                MOCK_METHOD3(output, void(OutputLevel, const OutputContextContainer&, const std::vector<std::string>&));
46                MOCK_METHOD3(unfilteredOutput, void(OutputLevel, const OutputContextContainer&, const std::vector<std::string>&));
47
48                inline const std::vector<AdditionalContextListener*>& getListeners() const
49                    { return OutputListener::getListeners(); }
50        };
51    }
52
53    // test getInstance does not create listeners
54    TEST(OutputManagerTest, GetInstanceDoesNotCreateDefaultListeners)
55    {
56        EXPECT_TRUE(nullptr == OutputManager::getInstance().getMemoryWriter());
57        EXPECT_TRUE(nullptr == OutputManager::getInstance().getConsoleWriter());
58        EXPECT_TRUE(nullptr == OutputManager::getInstance().getLogWriter());
59    }
60
61    // test getInstanceAndCreateListeners creates listeners
62    TEST(OutputManagerTest, GetInstanceAndCreateListenersCreatesDefaultListeners)
63    {
64        EXPECT_TRUE(nullptr != OutputManager::getInstanceAndCreateListeners().getMemoryWriter());
65        EXPECT_TRUE(nullptr != OutputManager::getInstanceAndCreateListeners().getConsoleWriter());
66        EXPECT_TRUE(nullptr != OutputManager::getInstanceAndCreateListeners().getLogWriter());
67    }
68
69    // test getInstanceAndCreateListeners called again does not re-create listeners
70    TEST(OutputManagerTest, GetInstanceAndCreateListenersDoesNotCreateDefaultListenersTwice)
71    {
72        MemoryWriter* memoryWriter = OutputManager::getInstanceAndCreateListeners().getMemoryWriter();
73        ConsoleWriter* consoleWriter = OutputManager::getInstanceAndCreateListeners().getConsoleWriter();
74        LogWriter* logWriter = OutputManager::getInstanceAndCreateListeners().getLogWriter();
75
76        EXPECT_TRUE(memoryWriter == OutputManager::getInstanceAndCreateListeners().getMemoryWriter());
77        EXPECT_TRUE(consoleWriter == OutputManager::getInstanceAndCreateListeners().getConsoleWriter());
78        EXPECT_TRUE(logWriter == OutputManager::getInstanceAndCreateListeners().getLogWriter());
79    }
80
81    // test constructor
82    TEST(OutputManagerTest, MasksAreZeroAfterConstruction)
83    {
84        OutputManager manager;
85        EXPECT_EQ(0u, manager.getCombinedLevelMask());
86        EXPECT_EQ(0u, manager.getCombinedAdditionalContextsLevelMask());
87        EXPECT_EQ(0u, manager.getCombinedAdditionalContextsMask());
88    }
89
90    // test registerListener adds listener to list
91    TEST(OutputManagerTest, RegisterListenerAddsListenerToList)
92    {
93        OutputManager manager;
94        MockOutputListener listener;
95        EXPECT_TRUE(manager.getListeners().empty());
96
97        manager.registerListener(&listener);
98
99        EXPECT_FALSE(manager.getListeners().empty());
100
101        manager.unregisterListener(&listener);
102    }
103
104    // test unregisterListener removes listener from list
105    TEST(OutputManagerTest, UnregisterListenerRemovesListenerFromList)
106    {
107        OutputManager manager;
108        MockOutputListener listener;
109        manager.registerListener(&listener);
110        EXPECT_FALSE(manager.getListeners().empty());
111
112        manager.unregisterListener(&listener);
113
114        EXPECT_TRUE(manager.getListeners().empty());
115    }
116
117    // test registerListener registers manager at listener
118    TEST(OutputManagerTest, RegisterListenerRegistersManagerAtListener)
119    {
120        OutputManager manager;
121        MockOutputListener listener(false);
122        EXPECT_TRUE(listener.getListeners().empty());
123
124        manager.registerListener(&listener);
125
126        EXPECT_FALSE(listener.getListeners().empty());
127
128        manager.unregisterListener(&listener);
129    }
130
131    // test unregisterListener unregisters manager from listener
132    TEST(OutputManagerTest, UnregisterListenerRemovesManagerFromListener)
133    {
134        OutputManager manager;
135        MockOutputListener listener(false);
136        manager.registerListener(&listener);
137        EXPECT_FALSE(listener.getListeners().empty());
138
139        manager.unregisterListener(&listener);
140
141        EXPECT_TRUE(listener.getListeners().empty());
142    }
143
144    // test registerListener adjusts masks
145    TEST(OutputManagerTest, RegisterListenerAdjustsMasks)
146    {
147        OutputManager manager;
148        EXPECT_EQ(0u, manager.getCombinedLevelMask());
149        EXPECT_EQ(0u, manager.getCombinedAdditionalContextsLevelMask());
150        EXPECT_EQ(0u, manager.getCombinedAdditionalContextsMask());
151
152        MockOutputListener listener;
153        listener.setLevelMask(level::internal_warning);
154        listener.setAdditionalContextsLevelMask(level::verbose_more);
155        listener.setAdditionalContextsMask(context::unittest2().mask);
156
157        manager.registerListener(&listener);
158
159        EXPECT_EQ(level::internal_warning, manager.getCombinedLevelMask());
160        EXPECT_EQ(level::verbose_more, manager.getCombinedAdditionalContextsLevelMask());
161        EXPECT_EQ(context::unittest2().mask, manager.getCombinedAdditionalContextsMask());
162
163        manager.unregisterListener(&listener);
164    }
165
166    // test unregisterListener adjusts masks
167    TEST(OutputManagerTest, UnregisterListenerAdjustsMasks)
168    {
169        OutputManager manager;
170        MockOutputListener listener;
171        listener.setLevelMask(level::internal_warning);
172        listener.setAdditionalContextsLevelMask(level::verbose_more);
173        listener.setAdditionalContextsMask(context::unittest2().mask);
174
175        manager.registerListener(&listener);
176
177        EXPECT_EQ(level::internal_warning, manager.getCombinedLevelMask());
178        EXPECT_EQ(level::verbose_more, manager.getCombinedAdditionalContextsLevelMask());
179        EXPECT_EQ(context::unittest2().mask, manager.getCombinedAdditionalContextsMask());
180
181        manager.unregisterListener(&listener);
182
183        EXPECT_EQ(0u, manager.getCombinedLevelMask());
184        EXPECT_EQ(0u, manager.getCombinedAdditionalContextsLevelMask());
185        EXPECT_EQ(0u, manager.getCombinedAdditionalContextsMask());
186    }
187
188    // test updatedLevelMask updates mask
189    TEST(OutputManagerTest, UpdatedLevelMask)
190    {
191        MockOutputManager manager;
192        MockOutputListener listener;
193        manager.registerListener(&listener);
194
195        EXPECT_EQ(0u, manager.getCombinedLevelMask());
196        EXPECT_CALL(manager, updatedLevelMask(&listener)).WillRepeatedly(::testing::Invoke(&manager, &MockOutputManager::originalUpdatedLevelMask));
197
198        listener.setLevelMask(level::internal_warning);
199
200        EXPECT_EQ(level::internal_warning, manager.getCombinedLevelMask());
201
202        manager.unregisterListener(&listener);
203    }
204
205    // test updatedAdditionalContextsLevelMask updates mask
206    TEST(OutputManagerTest, UpdatedAdditionalContextsLevelMask)
207    {
208        MockOutputManager manager;
209        MockOutputListener listener;
210        manager.registerListener(&listener);
211
212        EXPECT_EQ(0u, manager.getCombinedAdditionalContextsLevelMask());
213        EXPECT_CALL(manager, updatedAdditionalContextsLevelMask(&listener)).WillRepeatedly(::testing::Invoke(&manager, &MockOutputManager::originalUpdatedAdditionalContextsLevelMask));
214
215        listener.setAdditionalContextsLevelMask(level::internal_warning);
216
217        EXPECT_EQ(level::internal_warning, manager.getCombinedAdditionalContextsLevelMask());
218
219        manager.unregisterListener(&listener);
220    }
221
222    // test updatedAdditionalContextsMask updates mask
223    TEST(OutputManagerTest, UpdatedAdditionalContextsMask)
224    {
225        MockOutputManager manager;
226        MockOutputListener listener;
227        manager.registerListener(&listener);
228
229        EXPECT_EQ(0u, manager.getCombinedAdditionalContextsMask());
230        EXPECT_CALL(manager, updatedAdditionalContextsMask(&listener)).WillRepeatedly(::testing::Invoke(&manager, &MockOutputManager::originalUpdatedAdditionalContextsMask));
231
232        listener.setAdditionalContextsMask(context::unittest2().mask);
233
234        EXPECT_EQ(context::unittest2().mask, manager.getCombinedAdditionalContextsMask());
235
236        manager.unregisterListener(&listener);
237    }
238
239    // test combines masks of all listeners
240    TEST(OutputManagerTest, CombinesMasksOfAllListeners)
241    {
242        OutputManager manager;
243        EXPECT_EQ(0u, manager.getCombinedLevelMask());
244        EXPECT_EQ(0u, manager.getCombinedAdditionalContextsLevelMask());
245        EXPECT_EQ(0u, manager.getCombinedAdditionalContextsMask());
246
247        MockOutputListener listener1;
248        listener1.setLevelMask(level::user_error);
249        listener1.setAdditionalContextsLevelMask(level::verbose);
250        listener1.setAdditionalContextsMask(context::unittest1().mask);
251        manager.registerListener(&listener1);
252
253        MockOutputListener listener2;
254        listener2.setLevelMask(level::user_warning);
255        listener2.setAdditionalContextsLevelMask(level::verbose_more);
256        listener2.setAdditionalContextsMask(context::unittest2().mask);
257        manager.registerListener(&listener2);
258
259        MockOutputListener listener3;
260        listener3.setLevelMask(level::internal_warning);
261        listener3.setAdditionalContextsLevelMask(level::verbose_ultra);
262        listener3.setAdditionalContextsMask(context::unittest3().mask);
263        manager.registerListener(&listener3);
264
265        EXPECT_EQ(level::user_error | level::user_warning | level::internal_warning, manager.getCombinedLevelMask());
266        EXPECT_EQ(level::verbose | level::verbose_more | level::verbose_ultra, manager.getCombinedAdditionalContextsLevelMask());
267        EXPECT_EQ(context::unittest1().mask | context::unittest2().mask | context::unittest3().mask, manager.getCombinedAdditionalContextsMask());
268
269        manager.unregisterListener(&listener1);
270        manager.unregisterListener(&listener2);
271        manager.unregisterListener(&listener3);
272    }
273
274    // test acceptsOutput
275    TEST(OutputManagerTest, AcceptsOutput)
276    {
277        OutputManager manager;
278        MockOutputListener listener;
279        listener.setLevelMask(level::internal_warning);
280        listener.setAdditionalContextsLevelMask(level::verbose_more);
281        listener.setAdditionalContextsMask(context::unittest2().mask);
282
283        manager.registerListener(&listener);
284
285        EXPECT_FALSE(manager.acceptsOutput(level::user_error, context::undefined()));
286        EXPECT_FALSE(manager.acceptsOutput(level::internal_error, context::undefined()));
287        EXPECT_TRUE(manager.acceptsOutput(level::internal_warning, context::undefined()));
288        EXPECT_FALSE(manager.acceptsOutput(level::internal_status, context::undefined()));
289        EXPECT_FALSE(manager.acceptsOutput(level::verbose_more, context::undefined()));
290        EXPECT_FALSE(manager.acceptsOutput(level::verbose_ultra, context::undefined()));
291
292        EXPECT_FALSE(manager.acceptsOutput(level::user_error, context::unittest1()));
293        EXPECT_FALSE(manager.acceptsOutput(level::internal_error, context::unittest1()));
294        EXPECT_TRUE(manager.acceptsOutput(level::internal_warning, context::unittest1()));
295        EXPECT_FALSE(manager.acceptsOutput(level::internal_status, context::unittest1()));
296        EXPECT_FALSE(manager.acceptsOutput(level::verbose_more, context::unittest1()));
297        EXPECT_FALSE(manager.acceptsOutput(level::verbose_ultra, context::unittest1()));
298
299        EXPECT_FALSE(manager.acceptsOutput(level::user_error, context::unittest2()));
300        EXPECT_FALSE(manager.acceptsOutput(level::internal_error, context::unittest2()));
301        EXPECT_TRUE(manager.acceptsOutput(level::internal_warning, context::unittest2()));
302        EXPECT_FALSE(manager.acceptsOutput(level::internal_status, context::unittest2()));
303        EXPECT_TRUE(manager.acceptsOutput(level::verbose_more, context::unittest2()));
304        EXPECT_FALSE(manager.acceptsOutput(level::verbose_ultra, context::unittest2()));
305
306        manager.unregisterListener(&listener);
307    }
308
309    // test pushMessage sends lines to listeners
310    TEST(OutputManagerTest, PushMessageSendsLinesToListeners)
311    {
312        OutputManager manager;
313        MockOutputListener listener;
314        manager.registerListener(&listener);
315
316        std::vector<std::string> lines;
317        lines.push_back("some output");
318
319        EXPECT_CALL(listener, unfilteredOutput(level::user_status, context::unittest1(), lines));
320
321        manager.pushMessage(level::user_status, context::unittest1(), "some output");
322
323        manager.unregisterListener(&listener);
324    }
325
326    // test pushMessage splits lines by \n
327    TEST(OutputManagerTest, PushMessageSplitsTextAtNewlineChar)
328    {
329        OutputManager manager;
330        MockOutputListener listener;
331        manager.registerListener(&listener);
332
333        std::vector<std::string> lines;
334        lines.push_back("some output");
335        lines.push_back("and some more output");
336        lines.push_back("!!!");
337
338        EXPECT_CALL(listener, unfilteredOutput(level::user_status, context::unittest1(), lines));
339
340        manager.pushMessage(level::user_status, context::unittest1(), "some output\nand some more output\n!!!");
341
342        manager.unregisterListener(&listener);
343    }
344
345    // test registerContext for new context
346    TEST(OutputManagerTest, RegisterNewContext)
347    {
348        OutputManager manager;
349        OutputContextContainer container = manager.registerContext("whatever");
350        EXPECT_EQ("whatever", container.name);
351        EXPECT_EQ(0, container.sub_id);
352    }
353
354    // test registerContext for existing context
355    TEST(OutputManagerTest, RegisterContextTwice)
356    {
357        OutputManager manager;
358        OutputContextContainer container1 = manager.registerContext("whatever");
359        OutputContextContainer container2 = manager.registerContext("whatever");
360        EXPECT_EQ("whatever", container1.name);
361        EXPECT_EQ(0, container1.sub_id);
362        EXPECT_EQ(container1, container2);
363    }
364
365    // test registerContext for multiple contexts
366    TEST(OutputManagerTest, RegisterMultipleContexts)
367    {
368        OutputManager manager;
369        OutputContextContainer container1 = manager.registerContext("whatever");
370        OutputContextContainer container2 = manager.registerContext("asdf");
371        EXPECT_EQ("whatever", container1.name);
372        EXPECT_EQ(0, container1.sub_id);
373        EXPECT_EQ("asdf", container2.name);
374        EXPECT_EQ(0, container2.sub_id);
375        EXPECT_NE(container1.mask, container2.mask);
376    }
377
378    // test registerContext for new subcontext
379    TEST(OutputManagerTest, RegisterNewSubcontext)
380    {
381        OutputManager manager;
382        OutputContextContainer container = manager.registerContext("base", "subcontext");
383        EXPECT_EQ("base::subcontext", container.name);
384        EXPECT_NE(0, container.sub_id);
385    }
386
387    // test registerContext for existing subcontext
388    TEST(OutputManagerTest, RegisterSubcontextTwice)
389    {
390        OutputManager manager;
391        OutputContextContainer container1 = manager.registerContext("base", "subcontext");
392        OutputContextContainer container2 = manager.registerContext("base", "subcontext");
393        EXPECT_EQ("base::subcontext", container1.name);
394        EXPECT_NE(0, container1.sub_id);
395        EXPECT_EQ(container1, container2);
396    }
397
398    // test registerContext for multiple subcontexts
399    TEST(OutputManagerTest, RegisterMultipleSubcontexts)
400    {
401        OutputManager manager;
402        OutputContextContainer container1 = manager.registerContext("base", "subcontext");
403        OutputContextContainer container2 = manager.registerContext("base", "meh");
404        EXPECT_EQ("base::subcontext", container1.name);
405        EXPECT_NE(0, container1.sub_id);
406        EXPECT_EQ("base::meh", container2.name);
407        EXPECT_NE(0, container2.sub_id);
408        EXPECT_EQ(container1.mask, container2.mask);
409        EXPECT_NE(container1.sub_id, container2.sub_id);
410    }
411
412    // test subcontext has mask of base context
413    TEST(OutputManagerTest, SubcontextHasMaskOfBaseContext)
414    {
415        OutputManager manager;
416        OutputContextContainer container1 = manager.registerContext("base", "subcontext");
417        EXPECT_EQ("base::subcontext", container1.name);
418        EXPECT_NE(0, container1.sub_id);
419
420        OutputContextContainer container2 = manager.registerContext("base");
421        EXPECT_EQ("base", container2.name);
422        EXPECT_EQ(0, container2.sub_id);
423
424        EXPECT_EQ(container1.mask, container2.mask);
425    }
426
427    // test getLevelName
428    TEST(OutputManagerTest, GetLevelName)
429    {
430        OutputManager manager;
431        EXPECT_EQ("Debug", manager.getLevelName(level::debug_output));
432        EXPECT_EQ("Error", manager.getLevelName(level::user_error));
433        EXPECT_EQ("Status", manager.getLevelName(level::user_status));
434        EXPECT_EQ("Warning (internal)", manager.getLevelName(level::internal_warning));
435        EXPECT_EQ("Info (internal)", manager.getLevelName(level::internal_info));
436        EXPECT_EQ("Verbose", manager.getLevelName(level::verbose));
437    }
438
439    // test getDefaultPrefix for undefined context
440    TEST(OutputManagerTest, GetDefaultPrefixWithoutContext)
441    {
442        OutputManager manager;
443        std::string prefix = manager.getDefaultPrefix(level::debug_output, context::undefined());
444        EXPECT_EQ(manager.getLevelName(level::debug_output) + ": ", prefix);
445    }
446
447    // test getDefaultPrefix for regular context
448    TEST(OutputManagerTest, GetDefaultPrefixWithContext)
449    {
450        OutputManager manager;
451        std::string prefix = manager.getDefaultPrefix(level::debug_output, context::unittest1());
452        EXPECT_EQ(manager.getLevelName(level::debug_output) + ": [unittest1] ", prefix);
453    }
454}
Note: See TracBrowser for help on using the repository browser.