Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/SuperOrxoBros_HS18/SuperOrxoBros_HS18/test/core_plugin/PluginTest.cc @ 12175

Last change on this file since 12175 was 12175, checked in by siramesh, 5 years ago

Super Orxo Bros (Sidharth Ramesh, Nisa Balta, Jeff Ren)

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_ = nullptr;
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_ = nullptr;
47                }
48
49                virtual void TearDown() override
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() == nullptr);
80        this->loadPlugin();
81        EXPECT_TRUE(getIdentifier() != nullptr);
82        this->unloadPlugin();
83    }
84
85    TEST_F(PluginTest, UnloadsIdentifier)
86    {
87        this->loadPlugin();
88        EXPECT_TRUE(getIdentifier() != nullptr);
89        this->unloadPlugin();
90        EXPECT_TRUE(getIdentifier() == nullptr);
91    }
92
93    TEST_F(PluginTest, ReloadsIdentifier)
94    {
95        this->loadPlugin();
96        EXPECT_TRUE(getIdentifier() != nullptr);
97        this->unloadPlugin();
98        EXPECT_TRUE(getIdentifier() == nullptr);
99        this->loadPlugin();
100        EXPECT_TRUE(getIdentifier() != nullptr);
101        this->unloadPlugin();
102    }
103
104    TEST_F(PluginTest, CanCreateObjectFromIdentifier)
105    {
106        this->loadPlugin();
107
108        Identifier* identifier = getIdentifier();
109        ASSERT_TRUE(identifier != nullptr);
110
111        Identifiable* object = identifier->fabricate(nullptr);
112        ASSERT_TRUE(object != nullptr);
113
114        Testclass* testclass = orxonox_cast<Testclass*>(object);
115        ASSERT_TRUE(testclass != nullptr);
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 (Listable* listable : ObjectList<Listable>())
134        {
135            Testsingleton* singleton = dynamic_cast<Testsingleton*>(listable);
136            if (singleton)
137                singletons.push_back(singleton);
138        }
139
140        switch (singletons.size())
141        {
142            case 0:
143                return nullptr;
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() == nullptr);
154        this->loadPlugin();
155        EXPECT_TRUE(getSingleton() != nullptr);
156        this->unloadPlugin();
157    }
158
159    TEST_F(PluginTest, UnloadsSingleton)
160    {
161        this->loadPlugin();
162        EXPECT_TRUE(getSingleton() != nullptr);
163        this->unloadPlugin();
164        EXPECT_TRUE(getSingleton() == nullptr);
165    }
166
167    TEST_F(PluginTest, ReloadsSingleton)
168    {
169        this->loadPlugin();
170        EXPECT_TRUE(getSingleton() != nullptr);
171        this->unloadPlugin();
172        EXPECT_TRUE(getSingleton() == nullptr);
173        this->loadPlugin();
174        EXPECT_TRUE(getSingleton() != nullptr);
175        this->unloadPlugin();
176    }
177
178    TEST_F(PluginTest, CanCallFunctionOnSingleton)
179    {
180        this->loadPlugin();
181
182        Testsingleton* singleton = getSingleton();
183        ASSERT_TRUE(singleton != nullptr);
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() == nullptr);
258        this->loadPlugin();
259        EXPECT_TRUE(getConsoleCommand() != nullptr);
260        this->unloadPlugin();
261    }
262
263    TEST_F(PluginTest, UnloadsConsoleCommand)
264    {
265        this->loadPlugin();
266        EXPECT_TRUE(getConsoleCommand() != nullptr);
267        this->unloadPlugin();
268        EXPECT_TRUE(getConsoleCommand() == nullptr);
269    }
270
271    TEST_F(PluginTest, ReloadsConsoleCommand)
272    {
273        this->loadPlugin();
274        EXPECT_TRUE(getConsoleCommand() != nullptr);
275        this->unloadPlugin();
276        EXPECT_TRUE(getConsoleCommand() == nullptr);
277        this->loadPlugin();
278        EXPECT_TRUE(getConsoleCommand() != nullptr);
279        this->unloadPlugin();
280    }
281
282    TEST_F(PluginTest, CanCallConsoleCommand)
283    {
284        this->loadPlugin();
285
286        ConsoleCommand* command = getConsoleCommand();
287        ASSERT_TRUE(command != nullptr);
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.