[Orxonox-commit 4864] r9533 - in code/branches/testing: src/libraries/util src/libraries/util/output test/util test/util/output

landauf at orxonox.net landauf at orxonox.net
Sat Mar 2 19:15:39 CET 2013


Author: landauf
Date: 2013-03-02 19:15:37 +0100 (Sat, 02 Mar 2013)
New Revision: 9533

Modified:
   code/branches/testing/src/libraries/util/UtilPrereqs.h
   code/branches/testing/src/libraries/util/output/OutputManager.cc
   code/branches/testing/src/libraries/util/output/OutputManager.h
   code/branches/testing/test/util/OutputTest.cc
   code/branches/testing/test/util/output/OutputDefinitionsTest.cc
   code/branches/testing/test/util/output/OutputListenerTest.cc
Log:
added more tests

Modified: code/branches/testing/src/libraries/util/UtilPrereqs.h
===================================================================
--- code/branches/testing/src/libraries/util/UtilPrereqs.h	2013-02-26 13:10:33 UTC (rev 9532)
+++ code/branches/testing/src/libraries/util/UtilPrereqs.h	2013-03-02 18:15:37 UTC (rev 9533)
@@ -96,6 +96,8 @@
     template <class, ScopeID::Value>
     class ScopedSingleton;
     class ScopeListener;
+    template <class T>
+    class SharedPtr;
     class SignalHandler;
     template <class T>
     class Singleton;

Modified: code/branches/testing/src/libraries/util/output/OutputManager.cc
===================================================================
--- code/branches/testing/src/libraries/util/output/OutputManager.cc	2013-02-26 13:10:33 UTC (rev 9532)
+++ code/branches/testing/src/libraries/util/output/OutputManager.cc	2013-03-02 18:15:37 UTC (rev 9533)
@@ -38,6 +38,7 @@
 #include "LogWriter.h"
 #include "util/Output.h"
 #include "util/StringUtils.h"
+#include "util/SharedPtr.h"
 
 namespace orxonox
 {
@@ -60,13 +61,18 @@
     {
     }
 
+    /*static*/ SharedPtr<OutputManager>& OutputManager::Testing::getInstancePointer()
+    {
+        static SharedPtr<OutputManager> instance(new OutputManager());
+        return instance;
+    }
+
     /**
         @brief Returns the only existing instance of the OutputManager singleton.
     */
     /*static*/ OutputManager& OutputManager::getInstance()
     {
-        static OutputManager instance;
-        return instance;
+        return *OutputManager::Testing::getInstancePointer();
     }
 
     /**

Modified: code/branches/testing/src/libraries/util/output/OutputManager.h
===================================================================
--- code/branches/testing/src/libraries/util/output/OutputManager.h	2013-02-26 13:10:33 UTC (rev 9532)
+++ code/branches/testing/src/libraries/util/output/OutputManager.h	2013-03-02 18:15:37 UTC (rev 9533)
@@ -64,13 +64,17 @@
     class _UtilExport OutputManager : public AdditionalContextListener
     {
         public:
+            OutputManager();
+            OutputManager(const OutputManager&);
+            virtual ~OutputManager();
+
             static OutputManager& getInstance();
             static OutputManager& getInstanceAndCreateListeners();
 
             void pushMessage(OutputLevel level, const OutputContextContainer& context, const std::string& message);
 
-            void registerListener(OutputListener* listener);
-            void unregisterListener(OutputListener* listener);
+            virtual void registerListener(OutputListener* listener);
+            virtual void unregisterListener(OutputListener* listener);
 
             virtual void updatedLevelMask(const OutputListener* listener)
                 { this->updateCombinedLevelMask(); }
@@ -102,10 +106,6 @@
                 { return this->listeners_; }
 
         private:
-            OutputManager();
-            OutputManager(const OutputManager&);
-            ~OutputManager();
-
             void updateMasks();
             void updateCombinedLevelMask();
             void updateCombinedAdditionalContextsLevelMask();
@@ -120,6 +120,12 @@
             std::map<std::string, OutputContextMask> contextMasks_;             ///< Contains all main-contexts and their masks
             std::map<std::string, OutputContextContainer> contextContainers_;   ///< Contains all contexts including sub-contexts and their containers
             OutputContextSubID subcontextCounter_;                              ///< Counts the number of sub-contexts (and generates their IDs)
+
+        public:
+            struct _UtilExport Testing
+            {
+                static SharedPtr<OutputManager>& getInstancePointer();
+            };
     };
 }
 

Modified: code/branches/testing/test/util/OutputTest.cc
===================================================================
--- code/branches/testing/test/util/OutputTest.cc	2013-02-26 13:10:33 UTC (rev 9532)
+++ code/branches/testing/test/util/OutputTest.cc	2013-03-02 18:15:37 UTC (rev 9533)
@@ -11,12 +11,12 @@
         }
     }
 
-    TEST(Output, CanUseOrxout)
+    TEST(OutputTest, CanUseOrxout)
     {
         orxout() << "test" << endl;
     }
 
-    TEST(Output, OrxoutUsesCorrectOutputLevel)
+    TEST(OutputTest, OrxoutUsesCorrectOutputLevel)
     {
         {
             const OutputStream& stream = orxout(internal_warning);
@@ -29,7 +29,7 @@
         }
     }
 
-    TEST(Output, OrxoutUsesCorrectOutputContext)
+    TEST(OutputTest, OrxoutUsesCorrectOutputContext)
     {
         const OutputStream& stream = orxout(verbose, context::unittest());
         EXPECT_EQ(verbose, stream.getOutputLevel());
@@ -38,7 +38,7 @@
         EXPECT_EQ(context::unittest().sub_id, stream.getOutputContext()->sub_id);
     }
 
-    TEST(Output, OrxoutAcceptsFunctionPointer)
+    TEST(OutputTest, OrxoutAcceptsFunctionPointer)
     {
         const OutputStream& stream = orxout(verbose, context::unittest);
         EXPECT_EQ(verbose, stream.getOutputLevel());

Modified: code/branches/testing/test/util/output/OutputDefinitionsTest.cc
===================================================================
--- code/branches/testing/test/util/output/OutputDefinitionsTest.cc	2013-02-26 13:10:33 UTC (rev 9532)
+++ code/branches/testing/test/util/output/OutputDefinitionsTest.cc	2013-03-02 18:15:37 UTC (rev 9533)
@@ -3,7 +3,7 @@
 
 namespace orxonox
 {
-    TEST(OutputDefinitions, Levels)
+    TEST(OutputDefinitionsTest, Levels)
     {
         EXPECT_EQ(0x0000, level::none);
     }

Modified: code/branches/testing/test/util/output/OutputListenerTest.cc
===================================================================
--- code/branches/testing/test/util/output/OutputListenerTest.cc	2013-02-26 13:10:33 UTC (rev 9532)
+++ code/branches/testing/test/util/output/OutputListenerTest.cc	2013-03-02 18:15:37 UTC (rev 9533)
@@ -1,6 +1,8 @@
 #include <gtest/gtest.h>
 #include <gmock/gmock.h>
 #include "util/output/OutputListener.h"
+#include "util/output/OutputManager.h"
+#include "util/SharedPtr.h"
 
 namespace orxonox
 {
@@ -19,12 +21,32 @@
         class MockOutputListener : public OutputListener
         {
             public:
+                MockOutputListener(bool bRegister = true) : OutputListener(bRegister) {}
+
                 MOCK_METHOD3(output, void(OutputLevel, const OutputContextContainer&, const std::vector<std::string>&));
+
+                inline const std::vector<AdditionalContextListener*>& getListeners() const
+                    { return OutputListener::getListeners(); }
         };
+
+        class MockAdditionalContextListener : public AdditionalContextListener
+        {
+            public:
+                MOCK_METHOD1(updatedLevelMask, void(const OutputListener*));
+                MOCK_METHOD1(updatedAdditionalContextsLevelMask, void(const OutputListener*));
+                MOCK_METHOD1(updatedAdditionalContextsMask, void(const OutputListener*));
+        };
+
+        class MockOutputManager : public OutputManager
+        {
+            public:
+                MOCK_METHOD1(registerListener, void(OutputListener*));
+                MOCK_METHOD1(unregisterListener, void(OutputListener*));
+        };
     }
 
     // test default settings
-    TEST(OutputListener, DefaultConstructorAcceptsNothing)
+    TEST(OutputListenerTest, DefaultConstructorAcceptsNothing)
     {
         MockOutputListener listener;
 
@@ -34,7 +56,7 @@
     }
 
     // test setLevelMax
-    TEST(OutputListener, TestSetLevelMax)
+    TEST(OutputListenerTest, SetLevelMax)
     {
         MockOutputListener listener;
 
@@ -51,7 +73,7 @@
     }
 
     // test setLevelRange
-    TEST(OutputListener, TestSetLevelRange)
+    TEST(OutputListenerTest, SetLevelRange)
     {
         MockOutputListener listener;
 
@@ -68,7 +90,7 @@
     }
 
     // test setLevelMask
-    TEST(OutputListener, TestSetLevelMask)
+    TEST(OutputListenerTest, SetLevelMask)
     {
         MockOutputListener listener;
 
@@ -85,7 +107,7 @@
     }
 
     // test setAdditionalContextsLevelMax
-    TEST(OutputListener, TestSetAdditionalContextsLevelMax)
+    TEST(OutputListenerTest, SetAdditionalContextsLevelMax)
     {
         MockOutputListener listener;
 
@@ -102,7 +124,7 @@
     }
 
     // test setAdditionalContextsLevelRange
-    TEST(OutputListener, TestSetAdditionalContextsLevelRange)
+    TEST(OutputListenerTest, SetAdditionalContextsLevelRange)
     {
         MockOutputListener listener;
 
@@ -119,7 +141,7 @@
     }
 
     // test setAdditionalContextsLevelMask
-    TEST(OutputListener, TestSetAdditionalContextsLevelMask)
+    TEST(OutputListenerTest, SetAdditionalContextsLevelMask)
     {
         MockOutputListener listener;
 
@@ -136,7 +158,7 @@
     }
 
     // test setAdditionalContextsMask
-    TEST(OutputListener, TestSetAdditionalContextsMask)
+    TEST(OutputListenerTest, SetAdditionalContextsMask)
     {
         MockOutputListener listener;
 
@@ -149,9 +171,108 @@
         EXPECT_TRUE(mask & context::unittest3().mask);
     }
 
-    // test setAdditionalContextsLevelMask calls OutputManager::updateCombinedAdditionalContextsLevelMask
-    // test setAdditionalContextsMask calls OutputManager::updateCombinedAdditionalContextsMask
+    // test registerListener
+    TEST(OutputListenerTest, RegisterListener)
+    {
+        MockOutputListener outputListener(false);
+        MockAdditionalContextListener additionalContextListener;
 
+        EXPECT_EQ(0u, outputListener.getListeners().size());
+        outputListener.registerListener(&additionalContextListener);
+        EXPECT_EQ(1u, outputListener.getListeners().size());
+        EXPECT_EQ(&additionalContextListener, outputListener.getListeners()[0]);
+    }
+
+    // test unregisterListener
+    TEST(OutputListenerTest, UnregisterListener)
+    {
+        MockOutputListener outputListener(false);
+        MockAdditionalContextListener additionalContextListener;
+
+        outputListener.registerListener(&additionalContextListener);
+        EXPECT_EQ(1u, outputListener.getListeners().size());
+        EXPECT_EQ(&additionalContextListener, outputListener.getListeners()[0]);
+
+        outputListener.unregisterListener(&additionalContextListener);
+        EXPECT_EQ(0u, outputListener.getListeners().size());
+    }
+
+    // test setLevelMask calls OutputManager::updatedLevelMask
+    TEST(OutputListenerTest, SetLevelMaskCallsListeners)
+    {
+        MockOutputListener listener;
+        MockAdditionalContextListener additionalContextListener;
+        listener.registerListener(&additionalContextListener);
+
+        EXPECT_CALL(additionalContextListener, updatedLevelMask(&listener)).Times(1);
+
+        listener.setLevelMask(level::debug_output);
+    }
+
+    // test setAdditionalContextsLevelMask calls OutputManager::updatedAdditionalContextsLevelMask
+    TEST(OutputListenerTest, SetAdditionalContextsLevelMaskCallsListeners)
+    {
+        MockOutputListener listener;
+        MockAdditionalContextListener additionalContextListener;
+        listener.registerListener(&additionalContextListener);
+
+        EXPECT_CALL(additionalContextListener, updatedAdditionalContextsLevelMask(&listener)).Times(1);
+
+        listener.setAdditionalContextsLevelMask(level::debug_output);
+    }
+
+    // test setAdditionalContextsMask calls OutputManager::updatedAdditionalContextsMask
+    TEST(OutputListenerTest, SetAdditionalContextsMaskCallsListeners)
+    {
+        MockOutputListener listener;
+        MockAdditionalContextListener additionalContextListener;
+        listener.registerListener(&additionalContextListener);
+
+        EXPECT_CALL(additionalContextListener, updatedAdditionalContextsMask(&listener)).Times(1);
+
+        listener.setAdditionalContextsMask(context::unittest1().mask);
+    }
+
     // test acceptsOutput
     // test unfilteredOutput
+
+    // Fixture
+    class OutputListenerTestWithMockedOutputManager : public ::testing::Test
+    {
+        public:
+            virtual void SetUp()
+            {
+                this->manager_ = new MockOutputManager();
+                OutputManager::Testing::getInstancePointer() = this->manager_;
+            }
+
+            virtual void TearDown()
+            {
+                OutputManager::Testing::getInstancePointer() = new OutputManager();
+            }
+
+        protected:
+            MockOutputManager* manager_;
+    };
+
+    // test default-constructor calls OutputManager::registerListener
+    TEST_F(OutputListenerTestWithMockedOutputManager, ConstructorRegistersInOutputManager)
+    {
+        EXPECT_CALL(*this->manager_, registerListener(::testing::_)).Times(1);
+        MockOutputListener listener;
+    }
+
+    // test prevent constructor from calling OutputManager::registerListener
+    TEST_F(OutputListenerTestWithMockedOutputManager, PreventRegisteringInOutputManager)
+    {
+        EXPECT_CALL(*this->manager_, registerListener(::testing::_)).Times(0);
+        MockOutputListener listener(false);
+    }
+
+    // test destructor calls OutputManager::unregisterListener
+    TEST_F(OutputListenerTestWithMockedOutputManager, DestructorUnregistersFromOutputManager)
+    {
+        MockOutputListener listener;
+        EXPECT_CALL(*this->manager_, unregisterListener(::testing::_)).Times(1);
+    }
 }




More information about the Orxonox-commit mailing list