-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathminimal_scene.cc
171 lines (142 loc) · 4.86 KB
/
minimal_scene.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright (C) 2021 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <gtest/gtest.h>
#include <QtTest/QtTest>
#include <ignition/common/Console.hh>
#include <ignition/math/Color.hh>
#include <ignition/math/Pose3.hh>
#include <ignition/rendering/Camera.hh>
#include <ignition/rendering/RenderEngine.hh>
#include <ignition/rendering/RenderingIface.hh>
#include <ignition/rendering/Scene.hh>
#include <ignition/utilities/ExtraTestMacros.hh>
#include "test_config.h" // NOLINT(build/include)
#include "../helpers/TestHelper.hh"
#include "ignition/gui/Application.hh"
#include "ignition/gui/GuiEvents.hh"
#include "ignition/gui/Plugin.hh"
#include "ignition/gui/MainWindow.hh"
int g_argc = 1;
char* g_argv[] =
{
reinterpret_cast<char*>(const_cast<char*>("./MinimalScene_TEST")),
};
using namespace ignition;
using namespace gui;
/////////////////////////////////////////////////
TEST(MinimalSceneTest, IGN_UTILS_TEST_ENABLED_ONLY_ON_LINUX(Load))
{
common::Console::SetVerbosity(4);
Application app(g_argc, g_argv);
app.AddPluginPath(std::string(PROJECT_BINARY_PATH) + "/lib");
EXPECT_TRUE(app.LoadPlugin("MinimalScene"));
// Get main window
auto win = app.findChild<MainWindow *>();
ASSERT_NE(nullptr, win);
// Get plugin
auto plugins = win->findChildren<Plugin *>();
EXPECT_EQ(plugins.size(), 1);
auto plugin = plugins[0];
EXPECT_EQ(plugin->Title(), "3D Scene");
// Cleanup
auto pluginName = plugin->CardItem()->objectName().toStdString();
EXPECT_TRUE(app.RemovePlugin(pluginName));
plugins.clear();
}
/////////////////////////////////////////////////
TEST(MinimalSceneTest, IGN_UTILS_TEST_ENABLED_ONLY_ON_LINUX(Config))
{
common::Console::SetVerbosity(4);
Application app(g_argc, g_argv);
app.AddPluginPath(std::string(PROJECT_BINARY_PATH) + "/lib");
// Load plugin
const char *pluginStr =
"<plugin filename=\"MinimalScene\">"
"<engine>ogre</engine>"
"<scene>banana</scene>"
"<ambient_light>1.0 0 0</ambient_light>"
"<background_color>0 1 0</background_color>"
"<camera_pose>1 2 3 0 0 1.57</camera_pose>"
"<camera_clip>"
" <near>0.1</near>"
" <far>5000</far>"
"</camera_clip>"
"<horizontal_fov>60</horizontal_fov>"
"</plugin>";
tinyxml2::XMLDocument pluginDoc;
pluginDoc.Parse(pluginStr);
EXPECT_TRUE(app.LoadPlugin("MinimalScene",
pluginDoc.FirstChildElement("plugin")));
// Get main window
auto win = app.findChild<MainWindow *>();
ASSERT_NE(nullptr, win);
// Show, but don't exec, so we don't block
win->QuickWindow()->show();
// Filter events
bool receivedPreRenderEvent{false};
bool receivedRenderEvent{false};
auto testHelper = std::make_unique<TestHelper>();
testHelper->forwardEvent = [&](QEvent *_event)
{
if (_event->type() == events::PreRender::kType)
{
receivedPreRenderEvent = true;
}
if (_event->type() == events::Render::kType)
{
receivedRenderEvent = true;
}
};
// Check scene
auto engine = rendering::engine("ogre");
ASSERT_NE(nullptr, engine);
int sleep = 0;
int maxSleep = 30;
while (!receivedRenderEvent && sleep < maxSleep)
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
QCoreApplication::processEvents();
sleep++;
}
EXPECT_TRUE(receivedPreRenderEvent);
EXPECT_TRUE(receivedRenderEvent);
EXPECT_EQ(1u, engine->SceneCount());
auto scene = engine->SceneByName("banana");
ASSERT_NE(nullptr, scene);
EXPECT_EQ(math::Color(0, 1, 0), scene->BackgroundColor());
EXPECT_EQ(math::Color(1, 0, 0), scene->AmbientLight());
auto root = scene->RootVisual();
ASSERT_NE(nullptr, root);
EXPECT_EQ(1u, root->ChildCount());
// Check camera
auto camera = std::dynamic_pointer_cast<rendering::Camera>(
root->ChildByIndex(0));
ASSERT_NE(nullptr, camera);
EXPECT_EQ(math::Pose3d(1, 2, 3, 0, 0, 1.57), camera->WorldPose());
EXPECT_DOUBLE_EQ(0.1, camera->NearClipPlane());
EXPECT_DOUBLE_EQ(5000.0, camera->FarClipPlane());
EXPECT_DOUBLE_EQ(60, camera->HFOV().Degree());
// Cleanup
auto plugins = win->findChildren<Plugin *>();
EXPECT_EQ(1, plugins.size());
auto pluginName = plugins[0]->CardItem()->objectName().toStdString();
EXPECT_TRUE(app.RemovePlugin(pluginName));
plugins.clear();
win->QuickWindow()->close();
engine->DestroyScene(scene);
EXPECT_TRUE(rendering::unloadEngine(engine->Name()));
}