Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/test/core_plugin/PluginTest.cc @ 11013

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

make sure the plugin is always unloaded after a test

  • Property svn:eol-style set to native
File size: 7.5 KB
Line 
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:
23                PluginTest()
24                {
25                    this->plugin_ = NULL;
26                }
27
28                static void SetUpTestCase()
29                {
30                    new Core("--noIOConsole");
31                }
32
33                static void TearDownTestCase()
34                {
35                    delete &Core::getInstance();
36                }
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_;
57        };
58    }
59
60
61    TEST_F(PluginTest, CanLoadPlugin)
62    {
63        this->loadPlugin();
64        this->unloadPlugin();
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);
80        this->loadPlugin();
81        EXPECT_TRUE(getIdentifier() != NULL);
82        this->unloadPlugin();
83    }
84
85    TEST_F(PluginTest, UnloadsIdentifier)
86    {
87        this->loadPlugin();
88        EXPECT_TRUE(getIdentifier() != NULL);
89        this->unloadPlugin();
90        EXPECT_TRUE(getIdentifier() == NULL);
91    }
92
93    TEST_F(PluginTest, ReloadsIdentifier)
94    {
95        this->loadPlugin();
96        EXPECT_TRUE(getIdentifier() != NULL);
97        this->unloadPlugin();
98        EXPECT_TRUE(getIdentifier() == NULL);
99        this->loadPlugin();
100        EXPECT_TRUE(getIdentifier() != NULL);
101        this->unloadPlugin();
102    }
103
104    TEST_F(PluginTest, CanCreateObjectFromIdentifier)
105    {
106        this->loadPlugin();
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
119        this->unloadPlugin();
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
133        for (ObjectList<Listable>::iterator it = ObjectList<Listable>::begin(); it; ++it)
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);
154        this->loadPlugin();
155        EXPECT_TRUE(getSingleton() != NULL);
156        this->unloadPlugin();
157    }
158
159    TEST_F(PluginTest, UnloadsSingleton)
160    {
161        this->loadPlugin();
162        EXPECT_TRUE(getSingleton() != NULL);
163        this->unloadPlugin();
164        EXPECT_TRUE(getSingleton() == NULL);
165    }
166
167    TEST_F(PluginTest, ReloadsSingleton)
168    {
169        this->loadPlugin();
170        EXPECT_TRUE(getSingleton() != NULL);
171        this->unloadPlugin();
172        EXPECT_TRUE(getSingleton() == NULL);
173        this->loadPlugin();
174        EXPECT_TRUE(getSingleton() != NULL);
175        this->unloadPlugin();
176    }
177
178    TEST_F(PluginTest, CanCallFunctionOnSingleton)
179    {
180        this->loadPlugin();
181
182        Testsingleton* singleton = getSingleton();
183        ASSERT_TRUE(singleton != NULL);
184
185        EXPECT_EQ(999, singleton->getValue());
186
187        this->unloadPlugin();
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());
211        this->loadPlugin();
212        EXPECT_TRUE(hasCommandLineArgument());
213        this->unloadPlugin();
214    }
215
216    TEST_F(PluginTest, UnloadsCommandLineArgument)
217    {
218        this->loadPlugin();
219        EXPECT_TRUE(hasCommandLineArgument());
220        this->unloadPlugin();
221        EXPECT_FALSE(hasCommandLineArgument());
222    }
223
224    TEST_F(PluginTest, ReloadsCommandLineArgument)
225    {
226        this->loadPlugin();
227        EXPECT_TRUE(hasCommandLineArgument());
228        this->unloadPlugin();
229        EXPECT_FALSE(hasCommandLineArgument());
230        this->loadPlugin();
231        EXPECT_TRUE(hasCommandLineArgument());
232        this->unloadPlugin();
233    }
234
235    TEST_F(PluginTest, CommandLineArgumentHasCorrectValue)
236    {
237        this->loadPlugin();
238
239        ASSERT_TRUE(hasCommandLineArgument());
240        EXPECT_EQ(333, CommandLineParser::getValue("testvalue").get<int>());
241
242        this->unloadPlugin();
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);
258        this->loadPlugin();
259        EXPECT_TRUE(getConsoleCommand() != NULL);
260        this->unloadPlugin();
261    }
262
263    TEST_F(PluginTest, UnloadsConsoleCommand)
264    {
265        this->loadPlugin();
266        EXPECT_TRUE(getConsoleCommand() != NULL);
267        this->unloadPlugin();
268        EXPECT_TRUE(getConsoleCommand() == NULL);
269    }
270
271    TEST_F(PluginTest, ReloadsConsoleCommand)
272    {
273        this->loadPlugin();
274        EXPECT_TRUE(getConsoleCommand() != NULL);
275        this->unloadPlugin();
276        EXPECT_TRUE(getConsoleCommand() == NULL);
277        this->loadPlugin();
278        EXPECT_TRUE(getConsoleCommand() != NULL);
279        this->unloadPlugin();
280    }
281
282    TEST_F(PluginTest, CanCallConsoleCommand)
283    {
284        this->loadPlugin();
285
286        ConsoleCommand* command = getConsoleCommand();
287        ASSERT_TRUE(command != NULL);
288
289        EXPECT_EQ(999, (*command->getExecutor())(333, 666).get<int>());
290
291        this->unloadPlugin();
292    }
293}
Note: See TracBrowser for help on using the repository browser.