Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/wiimote/test/util/output/OutputManagerTest.cc @ 12072

Last change on this file since 12072 was 9547, checked in by landauf, 12 years ago

return pointers instead of references because they could be null

  • Property svn:eol-style set to native
File size: 18.0 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(NULL == OutputManager::getInstance().getMemoryWriter());
57        EXPECT_TRUE(NULL == OutputManager::getInstance().getConsoleWriter());
58        EXPECT_TRUE(NULL == OutputManager::getInstance().getLogWriter());
59    }
60
61    // test getInstanceAndCreateListeners creates listeners
62    TEST(OutputManagerTest, GetInstanceAndCreateListenersCreatesDefaultListeners)
63    {
64        EXPECT_TRUE(NULL != OutputManager::getInstanceAndCreateListeners().getMemoryWriter());
65        EXPECT_TRUE(NULL != OutputManager::getInstanceAndCreateListeners().getConsoleWriter());
66        EXPECT_TRUE(NULL != 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
102    // test unregisterListener removes listener from list
103    TEST(OutputManagerTest, UnregisterListenerRemovesListenerFromList)
104    {
105        OutputManager manager;
106        MockOutputListener listener;
107        manager.registerListener(&listener);
108        EXPECT_FALSE(manager.getListeners().empty());
109
110        manager.unregisterListener(&listener);
111
112        EXPECT_TRUE(manager.getListeners().empty());
113    }
114
115    // test registerListener registers manager at listener
116    TEST(OutputManagerTest, RegisterListenerRegistersManagerAtListener)
117    {
118        OutputManager manager;
119        MockOutputListener listener(false);
120        EXPECT_TRUE(listener.getListeners().empty());
121
122        manager.registerListener(&listener);
123
124        EXPECT_FALSE(listener.getListeners().empty());
125    }
126
127    // test unregisterListener unregisters manager from listener
128    TEST(OutputManagerTest, UnregisterListenerRemovesManagerFromListener)
129    {
130        OutputManager manager;
131        MockOutputListener listener(false);
132        manager.registerListener(&listener);
133        EXPECT_FALSE(listener.getListeners().empty());
134
135        manager.unregisterListener(&listener);
136
137        EXPECT_TRUE(listener.getListeners().empty());
138    }
139
140    // test registerListener adjusts masks
141    TEST(OutputManagerTest, RegisterListenerAdjustsMasks)
142    {
143        OutputManager manager;
144        EXPECT_EQ(0u, manager.getCombinedLevelMask());
145        EXPECT_EQ(0u, manager.getCombinedAdditionalContextsLevelMask());
146        EXPECT_EQ(0u, manager.getCombinedAdditionalContextsMask());
147
148        MockOutputListener listener;
149        listener.setLevelMask(level::internal_warning);
150        listener.setAdditionalContextsLevelMask(level::verbose_more);
151        listener.setAdditionalContextsMask(context::unittest2().mask);
152
153        manager.registerListener(&listener);
154
155        EXPECT_EQ(level::internal_warning, manager.getCombinedLevelMask());
156        EXPECT_EQ(level::verbose_more, manager.getCombinedAdditionalContextsLevelMask());
157        EXPECT_EQ(context::unittest2().mask, manager.getCombinedAdditionalContextsMask());
158    }
159
160    // test unregisterListener adjusts masks
161    TEST(OutputManagerTest, UnregisterListenerAdjustsMasks)
162    {
163        OutputManager manager;
164        MockOutputListener listener;
165        listener.setLevelMask(level::internal_warning);
166        listener.setAdditionalContextsLevelMask(level::verbose_more);
167        listener.setAdditionalContextsMask(context::unittest2().mask);
168
169        manager.registerListener(&listener);
170
171        EXPECT_EQ(level::internal_warning, manager.getCombinedLevelMask());
172        EXPECT_EQ(level::verbose_more, manager.getCombinedAdditionalContextsLevelMask());
173        EXPECT_EQ(context::unittest2().mask, manager.getCombinedAdditionalContextsMask());
174
175        manager.unregisterListener(&listener);
176
177        EXPECT_EQ(0u, manager.getCombinedLevelMask());
178        EXPECT_EQ(0u, manager.getCombinedAdditionalContextsLevelMask());
179        EXPECT_EQ(0u, manager.getCombinedAdditionalContextsMask());
180    }
181
182    // test updatedLevelMask updates mask
183    TEST(OutputManagerTest, UpdatedLevelMask)
184    {
185        MockOutputManager manager;
186        MockOutputListener listener;
187        manager.registerListener(&listener);
188
189        EXPECT_EQ(0u, manager.getCombinedLevelMask());
190        EXPECT_CALL(manager, updatedLevelMask(&listener)).WillRepeatedly(::testing::Invoke(&manager, &MockOutputManager::originalUpdatedLevelMask));
191
192        listener.setLevelMask(level::internal_warning);
193
194        EXPECT_EQ(level::internal_warning, manager.getCombinedLevelMask());
195    }
196
197    // test updatedAdditionalContextsLevelMask updates mask
198    TEST(OutputManagerTest, UpdatedAdditionalContextsLevelMask)
199    {
200        MockOutputManager manager;
201        MockOutputListener listener;
202        manager.registerListener(&listener);
203
204        EXPECT_EQ(0u, manager.getCombinedAdditionalContextsLevelMask());
205        EXPECT_CALL(manager, updatedAdditionalContextsLevelMask(&listener)).WillRepeatedly(::testing::Invoke(&manager, &MockOutputManager::originalUpdatedAdditionalContextsLevelMask));
206
207        listener.setAdditionalContextsLevelMask(level::internal_warning);
208
209        EXPECT_EQ(level::internal_warning, manager.getCombinedAdditionalContextsLevelMask());
210    }
211
212    // test updatedAdditionalContextsMask updates mask
213    TEST(OutputManagerTest, UpdatedAdditionalContextsMask)
214    {
215        MockOutputManager manager;
216        MockOutputListener listener;
217        manager.registerListener(&listener);
218
219        EXPECT_EQ(0u, manager.getCombinedAdditionalContextsMask());
220        EXPECT_CALL(manager, updatedAdditionalContextsMask(&listener)).WillRepeatedly(::testing::Invoke(&manager, &MockOutputManager::originalUpdatedAdditionalContextsMask));
221
222        listener.setAdditionalContextsMask(context::unittest2().mask);
223
224        EXPECT_EQ(context::unittest2().mask, manager.getCombinedAdditionalContextsMask());
225    }
226
227    // test combines masks of all listeners
228    TEST(OutputManagerTest, CombinesMasksOfAllListeners)
229    {
230        OutputManager manager;
231        EXPECT_EQ(0u, manager.getCombinedLevelMask());
232        EXPECT_EQ(0u, manager.getCombinedAdditionalContextsLevelMask());
233        EXPECT_EQ(0u, manager.getCombinedAdditionalContextsMask());
234
235        MockOutputListener listener1;
236        listener1.setLevelMask(level::user_error);
237        listener1.setAdditionalContextsLevelMask(level::verbose);
238        listener1.setAdditionalContextsMask(context::unittest1().mask);
239        manager.registerListener(&listener1);
240
241        MockOutputListener listener2;
242        listener2.setLevelMask(level::user_warning);
243        listener2.setAdditionalContextsLevelMask(level::verbose_more);
244        listener2.setAdditionalContextsMask(context::unittest2().mask);
245        manager.registerListener(&listener2);
246
247        MockOutputListener listener3;
248        listener3.setLevelMask(level::internal_warning);
249        listener3.setAdditionalContextsLevelMask(level::verbose_ultra);
250        listener3.setAdditionalContextsMask(context::unittest3().mask);
251        manager.registerListener(&listener3);
252
253        EXPECT_EQ(level::user_error | level::user_warning | level::internal_warning, manager.getCombinedLevelMask());
254        EXPECT_EQ(level::verbose | level::verbose_more | level::verbose_ultra, manager.getCombinedAdditionalContextsLevelMask());
255        EXPECT_EQ(context::unittest1().mask | context::unittest2().mask | context::unittest3().mask, manager.getCombinedAdditionalContextsMask());
256    }
257
258    // test acceptsOutput
259    TEST(OutputManagerTest, AcceptsOutput)
260    {
261        OutputManager manager;
262        MockOutputListener listener;
263        listener.setLevelMask(level::internal_warning);
264        listener.setAdditionalContextsLevelMask(level::verbose_more);
265        listener.setAdditionalContextsMask(context::unittest2().mask);
266
267        manager.registerListener(&listener);
268
269        EXPECT_FALSE(manager.acceptsOutput(level::user_error, context::undefined()));
270        EXPECT_FALSE(manager.acceptsOutput(level::internal_error, context::undefined()));
271        EXPECT_TRUE(manager.acceptsOutput(level::internal_warning, context::undefined()));
272        EXPECT_FALSE(manager.acceptsOutput(level::internal_status, context::undefined()));
273        EXPECT_FALSE(manager.acceptsOutput(level::verbose_more, context::undefined()));
274        EXPECT_FALSE(manager.acceptsOutput(level::verbose_ultra, context::undefined()));
275
276        EXPECT_FALSE(manager.acceptsOutput(level::user_error, context::unittest1()));
277        EXPECT_FALSE(manager.acceptsOutput(level::internal_error, context::unittest1()));
278        EXPECT_TRUE(manager.acceptsOutput(level::internal_warning, context::unittest1()));
279        EXPECT_FALSE(manager.acceptsOutput(level::internal_status, context::unittest1()));
280        EXPECT_FALSE(manager.acceptsOutput(level::verbose_more, context::unittest1()));
281        EXPECT_FALSE(manager.acceptsOutput(level::verbose_ultra, context::unittest1()));
282
283        EXPECT_FALSE(manager.acceptsOutput(level::user_error, context::unittest2()));
284        EXPECT_FALSE(manager.acceptsOutput(level::internal_error, context::unittest2()));
285        EXPECT_TRUE(manager.acceptsOutput(level::internal_warning, context::unittest2()));
286        EXPECT_FALSE(manager.acceptsOutput(level::internal_status, context::unittest2()));
287        EXPECT_TRUE(manager.acceptsOutput(level::verbose_more, context::unittest2()));
288        EXPECT_FALSE(manager.acceptsOutput(level::verbose_ultra, context::unittest2()));
289    }
290
291    // test pushMessage sends lines to listeners
292    TEST(OutputManagerTest, PushMessageSendsLinesToListeners)
293    {
294        OutputManager manager;
295        MockOutputListener listener;
296        manager.registerListener(&listener);
297
298        std::vector<std::string> lines;
299        lines.push_back("some output");
300
301        EXPECT_CALL(listener, unfilteredOutput(level::user_status, context::unittest1(), lines));
302
303        manager.pushMessage(level::user_status, context::unittest1(), "some output");
304    }
305
306    // test pushMessage splits lines by \n
307    TEST(OutputManagerTest, PushMessageSplitsTextAtNewlineChar)
308    {
309        OutputManager manager;
310        MockOutputListener listener;
311        manager.registerListener(&listener);
312
313        std::vector<std::string> lines;
314        lines.push_back("some output");
315        lines.push_back("and some more output");
316        lines.push_back("!!!");
317
318        EXPECT_CALL(listener, unfilteredOutput(level::user_status, context::unittest1(), lines));
319
320        manager.pushMessage(level::user_status, context::unittest1(), "some output\nand some more output\n!!!");
321    }
322
323    // test registerContext for new context
324    TEST(OutputManagerTest, RegisterNewContext)
325    {
326        OutputManager manager;
327        OutputContextContainer container = manager.registerContext("whatever");
328        EXPECT_EQ("whatever", container.name);
329        EXPECT_EQ(0, container.sub_id);
330    }
331
332    // test registerContext for existing context
333    TEST(OutputManagerTest, RegisterContextTwice)
334    {
335        OutputManager manager;
336        OutputContextContainer container1 = manager.registerContext("whatever");
337        OutputContextContainer container2 = manager.registerContext("whatever");
338        EXPECT_EQ("whatever", container1.name);
339        EXPECT_EQ(0, container1.sub_id);
340        EXPECT_EQ(container1, container2);
341    }
342
343    // test registerContext for multiple contexts
344    TEST(OutputManagerTest, RegisterMultipleContexts)
345    {
346        OutputManager manager;
347        OutputContextContainer container1 = manager.registerContext("whatever");
348        OutputContextContainer container2 = manager.registerContext("asdf");
349        EXPECT_EQ("whatever", container1.name);
350        EXPECT_EQ(0, container1.sub_id);
351        EXPECT_EQ("asdf", container2.name);
352        EXPECT_EQ(0, container2.sub_id);
353        EXPECT_NE(container1.mask, container2.mask);
354    }
355
356    // test registerContext for new subcontext
357    TEST(OutputManagerTest, RegisterNewSubcontext)
358    {
359        OutputManager manager;
360        OutputContextContainer container = manager.registerContext("base", "subcontext");
361        EXPECT_EQ("base::subcontext", container.name);
362        EXPECT_NE(0, container.sub_id);
363    }
364
365    // test registerContext for existing subcontext
366    TEST(OutputManagerTest, RegisterSubcontextTwice)
367    {
368        OutputManager manager;
369        OutputContextContainer container1 = manager.registerContext("base", "subcontext");
370        OutputContextContainer container2 = manager.registerContext("base", "subcontext");
371        EXPECT_EQ("base::subcontext", container1.name);
372        EXPECT_NE(0, container1.sub_id);
373        EXPECT_EQ(container1, container2);
374    }
375
376    // test registerContext for multiple subcontexts
377    TEST(OutputManagerTest, RegisterMultipleSubcontexts)
378    {
379        OutputManager manager;
380        OutputContextContainer container1 = manager.registerContext("base", "subcontext");
381        OutputContextContainer container2 = manager.registerContext("base", "meh");
382        EXPECT_EQ("base::subcontext", container1.name);
383        EXPECT_NE(0, container1.sub_id);
384        EXPECT_EQ("base::meh", container2.name);
385        EXPECT_NE(0, container2.sub_id);
386        EXPECT_EQ(container1.mask, container2.mask);
387        EXPECT_NE(container1.sub_id, container2.sub_id);
388    }
389
390    // test subcontext has mask of base context
391    TEST(OutputManagerTest, SubcontextHasMaskOfBaseContext)
392    {
393        OutputManager manager;
394        OutputContextContainer container1 = manager.registerContext("base", "subcontext");
395        EXPECT_EQ("base::subcontext", container1.name);
396        EXPECT_NE(0, container1.sub_id);
397
398        OutputContextContainer container2 = manager.registerContext("base");
399        EXPECT_EQ("base", container2.name);
400        EXPECT_EQ(0, container2.sub_id);
401
402        EXPECT_EQ(container1.mask, container2.mask);
403    }
404
405    // test getLevelName
406    TEST(OutputManagerTest, GetLevelName)
407    {
408        OutputManager manager;
409        EXPECT_EQ("Debug", manager.getLevelName(level::debug_output));
410        EXPECT_EQ("Error", manager.getLevelName(level::user_error));
411        EXPECT_EQ("Status", manager.getLevelName(level::user_status));
412        EXPECT_EQ("Warning (internal)", manager.getLevelName(level::internal_warning));
413        EXPECT_EQ("Info (internal)", manager.getLevelName(level::internal_info));
414        EXPECT_EQ("Verbose", manager.getLevelName(level::verbose));
415    }
416
417    // test getDefaultPrefix for undefined context
418    TEST(OutputManagerTest, GetDefaultPrefixWithoutContext)
419    {
420        OutputManager manager;
421        std::string prefix = manager.getDefaultPrefix(level::debug_output, context::undefined());
422        EXPECT_EQ(manager.getLevelName(level::debug_output) + ": ", prefix);
423    }
424
425    // test getDefaultPrefix for regular context
426    TEST(OutputManagerTest, GetDefaultPrefixWithContext)
427    {
428        OutputManager manager;
429        std::string prefix = manager.getDefaultPrefix(level::debug_output, context::unittest1());
430        EXPECT_EQ(manager.getLevelName(level::debug_output) + ": [unittest1] ", prefix);
431    }
432}
Note: See TracBrowser for help on using the repository browser.