Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added unittest for plugin loading and unloading

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