forked from gazebosim/gz-sim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer_Rendering_TEST.cc
156 lines (132 loc) · 4.88 KB
/
Server_Rendering_TEST.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
/*
* Copyright (C) 2022 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 <gz/rendering/RenderEngine.hh>
#include <gz/rendering/RenderingIface.hh>
#include <gz/sim/rendering/Events.hh>
#include <gz/rendering/Scene.hh>
#include "gz/sim/Server.hh"
#include "gz/sim/System.hh"
#include "plugins/MockSystem.hh"
#include "../test/helpers/Relay.hh"
#include "../test/helpers/EnvTestFixture.hh"
using namespace gz;
using namespace gz::sim;
/////////////////////////////////////////////////
class ServerFixture : public InternalFixture<::testing::TestWithParam<int>>
{
};
/////////////////////////////////////////////////
TEST_P(ServerFixture, LoadSdfModelRelativeUri)
{
class CheckMeshPlugin:
public System,
public gz::sim::ISystemConfigure,
public gz::sim::ISystemPreUpdate
{
private: common::ConnectionPtr connection{nullptr};
private: rendering::ScenePtr scene{nullptr};
private: EventManager *eventMgr{nullptr};
void FindScene()
{
auto loadedEngNames = gz::rendering::loadedEngines();
ASSERT_EQ(loadedEngNames.size(), 1);
auto engineName = loadedEngNames[0];
auto engine = gz::rendering::engine(engineName);
ASSERT_TRUE(engine);
ASSERT_EQ(engine->SceneCount(), 1);
auto scenePtr = engine->SceneByIndex(0);
ASSERT_NE(scenePtr, nullptr);
ASSERT_TRUE(scenePtr->IsInitialized());
ASSERT_NE(scenePtr->RootVisual(), nullptr);
this->scene = scenePtr;
};
private: void CheckMeshes(){
if (this->scene == nullptr){
this->FindScene();
}
std::shared_ptr<rendering::Visual> v1 = this->scene->VisualByName(
"relative_resource_uri::L1::V1"
);
// There's only one geometry under this visual, which has to be the mesh.
EXPECT_EQ(v1->GeometryCount(), 1);
std::shared_ptr<rendering::Geometry> v1Geom = v1->GeometryByIndex(0);
// Attempt to cast the geometry into a mesh, in order to determine that
// the mesh has been properly loaded.
std::shared_ptr<rendering::Mesh> v1Mesh =
std::dynamic_pointer_cast<rendering::Mesh>(v1Geom);
EXPECT_NE(v1Mesh, nullptr);
std::shared_ptr<rendering::Visual> v2 = this->scene->VisualByName(
"relative_resource_uri::L1::V2"
);
// There should be no geometries under this visual, as the mesh file
// refers to a non-existent file, and the mesh should not be loaded.
EXPECT_EQ(v2->GeometryCount(), 0);
}
public: void Configure(
const Entity &,
const std::shared_ptr<const sdf::Element> &,
EntityComponentManager &,
EventManager &_eventMgr
)
{
// Register CheckMeshes with the PreRender event.
this->connection =
_eventMgr.Connect<events::PreRender>(
std::bind(&CheckMeshPlugin::CheckMeshes, this)
);
this->eventMgr = &_eventMgr;
};
public: void PreUpdate(const UpdateInfo &, EntityComponentManager &)
{
// Emit a ForceRender event so the PreRender event is triggered.
this->eventMgr->Emit<events::ForceRender>();
};
};
gz::sim::ServerConfig serverConfig;
serverConfig.SetSdfFile(common::joinPaths(PROJECT_SOURCE_PATH,
"test", "worlds", "models", "relative_resource_uri", "model2.sdf"));
// Add the Sensors plugin so rendering is available.
sdf::Plugin sdfSensorsPlugin = sdf::Plugin(
"gz-sim-sensors-system",
"gz::sim::systems::Sensors",
"<render_engine>ogre2</render_engine>"
);
ServerConfig::PluginInfo sensorsPluginInfo = ServerConfig::PluginInfo(
"default",
"world",
sdfSensorsPlugin
);
serverConfig.AddPlugin(sensorsPluginInfo);
sim::Server server = Server(serverConfig);
EXPECT_TRUE(server.HasEntity("relative_resource_uri"));
EXPECT_TRUE(server.HasEntity("L1"));
EXPECT_TRUE(server.HasEntity("V1"));
EXPECT_TRUE(server.HasEntity("V2"));
std::shared_ptr<CheckMeshPlugin> meshChecker =
std::make_shared<CheckMeshPlugin>();
std::optional<bool> meshCheckerAddSuccess = server.AddSystem(meshChecker);
ASSERT_TRUE(meshCheckerAddSuccess);
if (meshCheckerAddSuccess) {
ASSERT_TRUE(meshCheckerAddSuccess.value());
}
ASSERT_TRUE(server.RunOnce());
ASSERT_TRUE(server.RunOnce(false));
}
// Run multiple times. We want to make sure that static globals don't cause
// problems.
INSTANTIATE_TEST_SUITE_P(ServerRepeat, ServerFixture, ::testing::Range(1, 2));