Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/cpp11_v3/test/core_plugin/PluginTest.cc @ 11054

Last change on this file since 11054 was 11054, checked in by landauf, 8 years ago

merged branch cpp11_v2 into cpp11_v3

  • Property svn:eol-style set to native
File size: 7.5 KB
RevLine 
[11012]1#include <gtest/gtest.h>
2#include <vector>
3
4#include "util/Exception.h"
5#include "core/Core.h"
6#include "core/CoreIncludes.h"
7#include "core/module/PluginReference.h"
8#include "core/commandline/CommandLineParser.h"
9#include "core/command/ConsoleCommand.h"
10#include "core/command/ConsoleCommandManager.h"
11
12#include "Testclass.h"
13#include "Testsingleton.h"
14
15namespace orxonox
16{
17    namespace
18    {
19        // Fixture
20        class PluginTest : public ::testing::Test
21        {
22            public:
[11013]23                PluginTest()
24                {
25                    this->plugin_ = NULL;
26                }
27
[11012]28                static void SetUpTestCase()
29                {
30                    new Core("--noIOConsole");
31                }
32
33                static void TearDownTestCase()
34                {
35                    delete &Core::getInstance();
36                }
[11013]37
38                void loadPlugin()
39                {
40                    this->plugin_ = new PluginReference("testplugin");
41                }
42
43                void unloadPlugin()
44                {
45                    delete this->plugin_;
46                    this->plugin_ = NULL;
47                }
48
49                virtual void TearDown()
50                {
51                    // make sure the plugin is unloaded
52                    this->unloadPlugin();
53                }
54
55            private:
56                PluginReference* plugin_;
[11012]57        };
58    }
59
60
61    TEST_F(PluginTest, CanLoadPlugin)
62    {
[11013]63        this->loadPlugin();
64        this->unloadPlugin();
[11012]65    }
66
67
68    ///////////////////////////////////////////
69    /////////////// Identifier ////////////////
70    ///////////////////////////////////////////
71
72    Identifier* getIdentifier()
73    {
74        return ClassByString("Testclass");
75    }
76
77    TEST_F(PluginTest, LoadsIdentifier)
78    {
79        EXPECT_TRUE(getIdentifier() == NULL);
[11013]80        this->loadPlugin();
[11012]81        EXPECT_TRUE(getIdentifier() != NULL);
[11013]82        this->unloadPlugin();
[11012]83    }
84
85    TEST_F(PluginTest, UnloadsIdentifier)
86    {
[11013]87        this->loadPlugin();
[11012]88        EXPECT_TRUE(getIdentifier() != NULL);
[11013]89        this->unloadPlugin();
[11012]90        EXPECT_TRUE(getIdentifier() == NULL);
91    }
92
93    TEST_F(PluginTest, ReloadsIdentifier)
94    {
[11013]95        this->loadPlugin();
[11012]96        EXPECT_TRUE(getIdentifier() != NULL);
[11013]97        this->unloadPlugin();
[11012]98        EXPECT_TRUE(getIdentifier() == NULL);
[11013]99        this->loadPlugin();
[11012]100        EXPECT_TRUE(getIdentifier() != NULL);
[11013]101        this->unloadPlugin();
[11012]102    }
103
104    TEST_F(PluginTest, CanCreateObjectFromIdentifier)
105    {
[11013]106        this->loadPlugin();
[11012]107
108        Identifier* identifier = getIdentifier();
109        ASSERT_TRUE(identifier != NULL);
110
111        Identifiable* object = identifier->fabricate(NULL);
112        ASSERT_TRUE(object != NULL);
113
114        Testclass* testclass = orxonox_cast<Testclass*>(object);
115        ASSERT_TRUE(testclass != NULL);
116
117        EXPECT_EQ(666, testclass->getValue());
118
[11013]119        this->unloadPlugin();
[11012]120    }
121
122
123    ///////////////////////////////////////////
124    //////////////// Singleton ////////////////
125    ///////////////////////////////////////////
126
127    // Cannot directly use Testsingleton::getInstance() because we don't link the test to the plugin.
128    // Also cannot directly use ObjectList<Testsingleton> because the Identifier is not known before the plugin is loaded.
129    Testsingleton* getSingleton()
130    {
131        std::vector<Testsingleton*> singletons;
132
[11054]133        for (ObjectList<Listable>::iterator it = ObjectList<Listable>().begin(); it; ++it)
[11012]134        {
135            Testsingleton* singleton = dynamic_cast<Testsingleton*>(*it);
136            if (singleton)
137                singletons.push_back(singleton);
138        }
139
140        switch (singletons.size())
141        {
142            case 0:
143                return NULL;
144            case 1:
145                return singletons[0];
146            default:
147                throw std::exception(); // unexpected number of singletons found
148        }
149    }
150
151    TEST_F(PluginTest, LoadsSingleton)
152    {
153        EXPECT_TRUE(getSingleton() == NULL);
[11013]154        this->loadPlugin();
[11012]155        EXPECT_TRUE(getSingleton() != NULL);
[11013]156        this->unloadPlugin();
[11012]157    }
158
159    TEST_F(PluginTest, UnloadsSingleton)
160    {
[11013]161        this->loadPlugin();
[11012]162        EXPECT_TRUE(getSingleton() != NULL);
[11013]163        this->unloadPlugin();
[11012]164        EXPECT_TRUE(getSingleton() == NULL);
165    }
166
167    TEST_F(PluginTest, ReloadsSingleton)
168    {
[11013]169        this->loadPlugin();
[11012]170        EXPECT_TRUE(getSingleton() != NULL);
[11013]171        this->unloadPlugin();
[11012]172        EXPECT_TRUE(getSingleton() == NULL);
[11013]173        this->loadPlugin();
[11012]174        EXPECT_TRUE(getSingleton() != NULL);
[11013]175        this->unloadPlugin();
[11012]176    }
177
178    TEST_F(PluginTest, CanCallFunctionOnSingleton)
179    {
[11013]180        this->loadPlugin();
[11012]181
182        Testsingleton* singleton = getSingleton();
183        ASSERT_TRUE(singleton != NULL);
184
185        EXPECT_EQ(999, singleton->getValue());
186
[11013]187        this->unloadPlugin();
[11012]188    }
189
190
191    ///////////////////////////////////////////
192    ////////// Command Line Argument //////////
193    ///////////////////////////////////////////
194
195    bool hasCommandLineArgument()
196    {
197        try
198        {
199            CommandLineParser::getValue("testvalue");
200            return true;
201        }
202        catch (const ArgumentException&)
203        {
204            return false;
205        }
206    }
207
208    TEST_F(PluginTest, LoadsCommandLineArgument)
209    {
210        EXPECT_FALSE(hasCommandLineArgument());
[11013]211        this->loadPlugin();
[11012]212        EXPECT_TRUE(hasCommandLineArgument());
[11013]213        this->unloadPlugin();
[11012]214    }
215
216    TEST_F(PluginTest, UnloadsCommandLineArgument)
217    {
[11013]218        this->loadPlugin();
[11012]219        EXPECT_TRUE(hasCommandLineArgument());
[11013]220        this->unloadPlugin();
[11012]221        EXPECT_FALSE(hasCommandLineArgument());
222    }
223
224    TEST_F(PluginTest, ReloadsCommandLineArgument)
225    {
[11013]226        this->loadPlugin();
[11012]227        EXPECT_TRUE(hasCommandLineArgument());
[11013]228        this->unloadPlugin();
[11012]229        EXPECT_FALSE(hasCommandLineArgument());
[11013]230        this->loadPlugin();
[11012]231        EXPECT_TRUE(hasCommandLineArgument());
[11013]232        this->unloadPlugin();
[11012]233    }
234
235    TEST_F(PluginTest, CommandLineArgumentHasCorrectValue)
236    {
[11013]237        this->loadPlugin();
[11012]238
239        ASSERT_TRUE(hasCommandLineArgument());
240        EXPECT_EQ(333, CommandLineParser::getValue("testvalue").get<int>());
241
[11013]242        this->unloadPlugin();
[11012]243    }
244
245
246    ///////////////////////////////////////////
247    ///////////// Console Command /////////////
248    ///////////////////////////////////////////
249
250    ConsoleCommand* getConsoleCommand()
251    {
252        return ConsoleCommandManager::getInstance().getCommand("testcommand");
253    }
254
255    TEST_F(PluginTest, LoadsConsoleCommand)
256    {
257        EXPECT_TRUE(getConsoleCommand() == NULL);
[11013]258        this->loadPlugin();
[11012]259        EXPECT_TRUE(getConsoleCommand() != NULL);
[11013]260        this->unloadPlugin();
[11012]261    }
262
263    TEST_F(PluginTest, UnloadsConsoleCommand)
264    {
[11013]265        this->loadPlugin();
[11012]266        EXPECT_TRUE(getConsoleCommand() != NULL);
[11013]267        this->unloadPlugin();
[11012]268        EXPECT_TRUE(getConsoleCommand() == NULL);
269    }
270
271    TEST_F(PluginTest, ReloadsConsoleCommand)
272    {
[11013]273        this->loadPlugin();
[11012]274        EXPECT_TRUE(getConsoleCommand() != NULL);
[11013]275        this->unloadPlugin();
[11012]276        EXPECT_TRUE(getConsoleCommand() == NULL);
[11013]277        this->loadPlugin();
[11012]278        EXPECT_TRUE(getConsoleCommand() != NULL);
[11013]279        this->unloadPlugin();
[11012]280    }
281
282    TEST_F(PluginTest, CanCallConsoleCommand)
283    {
[11013]284        this->loadPlugin();
[11012]285
286        ConsoleCommand* command = getConsoleCommand();
287        ASSERT_TRUE(command != NULL);
288
289        EXPECT_EQ(999, (*command->getExecutor())(333, 666).get<int>());
290
[11013]291        this->unloadPlugin();
[11012]292    }
293}
Note: See TracBrowser for help on using the repository browser.