Skip to content

Commit

Permalink
Add UserCommands MaterialColor test.
Browse files Browse the repository at this point in the history
Signed-off-by: Benjamin Perseghetti <[email protected]>
  • Loading branch information
bperseghetti committed Jan 16, 2024
1 parent 7cc1d95 commit cfb0d74
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
68 changes: 68 additions & 0 deletions test/integration/user_commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <gz/msgs/entity.pb.h>
#include <gz/msgs/entity_factory.pb.h>
#include <gz/msgs/light.pb.h>
#include <gz/msgs/material_color.pb.h>
#include <gz/msgs/physics.pb.h>
#include <gz/msgs/pose.pb.h>
#include <gz/msgs/pose_v.pb.h>
Expand Down Expand Up @@ -1046,6 +1047,73 @@ TEST_F(UserCommandsTest, GZ_UTILS_TEST_ENABLED_ONLY_ON_LINUX(Light))
spotLightComp->Data().Diffuse());
}

/////////////////////////////////////////////////
TEST_F(UserCommandsTest, GZ_UTILS_TEST_ENABLED_ONLY_ON_LINUX(MaterialColor))
{
// Start server
ServerConfig serverConfig;
const auto sdfFile = gz::common::joinPaths(
std::string(PROJECT_SOURCE_PATH), "test", "worlds", "shapes.sdf");
serverConfig.SetSdfFile(sdfFile);

Server server(serverConfig);
EXPECT_FALSE(server.Running());
EXPECT_FALSE(*server.Running(0));

// Create a system just to get the ECM
EntityComponentManager *ecm{nullptr};
test::Relay testSystem;
testSystem.OnPreUpdate([&](const sim::UpdateInfo &,
sim::EntityComponentManager &_ecm)
{
ecm = &_ecm;
});

server.AddSystem(testSystem.systemPtr);

// Run server and check we have the ECM
EXPECT_EQ(nullptr, ecm);
server.Run(true, 1, false);
EXPECT_NE(nullptr, ecm);

transport::Node node;

// Camera Ball
auto boxVisualEntity =
ecm->EntityByComponents(components::Name("box_visual"));
ASSERT_NE(kNullEntity, boxVisualEntity);

// check box visual's initial values
auto boxVisualComp = ecm->Component<components::Material>(boxVisualEntity);
ASSERT_NE(nullptr, boxVisualComp);
EXPECT_EQ(math::Color(1.0f, 0.0f, 0.0f, 1.0f),
boxVisualComp->Data().Diffuse());

// Test material_color topic
const std::string materialColorTopic = "/world/material_color/material_color";

msgs::MaterialColor materialColorMsg;
materialColorMsg.set_name("box_visual");
materialColorMsg.set_parent_name("box_link");
gz::msgs::Set(materialColorMsg.mutable_diffuse(),
gz::math::Color(1.0f, 1.0f, 1.0f, 1.0f));

// Publish material color
auto pub = node.Advertise<msgs::MaterialColor>(materialColorTopic);
pub.Publish(materialColorMsg);

server.Run(true, 100, false);
// Sleep for a small duration to allow Run thread to start
GZ_SLEEP_MS(10);

auto boxVisCmdComp = ecm->Component<components::VisualCmd>(boxVisualEntity);
ASSERT_NE(nullptr, boxVisualComp);
EXPECT_FLOAT_EQ(0.0f, boxVisCmdComp->Data().material().diffuse().r());
EXPECT_FLOAT_EQ(0.0f, boxVisCmdComp->Data().material().diffuse().g());
EXPECT_FLOAT_EQ(1.0f, boxVisCmdComp->Data().material().diffuse().b());
EXPECT_FLOAT_EQ(1.0f, boxVisCmdComp->Data().material().diffuse().a());
}

/////////////////////////////////////////////////
TEST_F(UserCommandsTest, GZ_UTILS_TEST_DISABLED_ON_WIN32(Physics))
{
Expand Down
59 changes: 59 additions & 0 deletions test/worlds/material_color.sdf
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" ?>
<sdf version="1.6">
<world name="material_color">
<physics name="1ms" type="ode">
<max_step_size>0.001</max_step_size>
<real_time_factor>0</real_time_factor>
</physics>
<plugin
filename="gz-sim-scene-broadcaster-system"
name="gz::sim::systems::SceneBroadcaster">
</plugin>
<plugin
filename="gz-sim-user-commands-system"
name="gz::sim::systems::UserCommands">
</plugin>
<plugin
filename="gz-sim-sensors-system"
name="gz::sim::systems::Sensors">
<render_engine>ogre2</render_engine>
</plugin>
<model name="camera_ball">
<pose>0 0.0 0.0 0 0 0</pose>
<link name="sphere">
<!-- Added a render sensor to trigger RenderUtil:Update -->
<sensor name="camera" type="camera">
<pose>1 0 1.3 0 0 0</pose>
<camera>
<horizontal_fov>1.047</horizontal_fov>
<image>
<width>320</width>
<height>240</height>
</image>
<clip>
<near>0.1</near>
<far>100</far>
</clip>
</camera>
<always_on>1</always_on>
<update_rate>30</update_rate>
<visualize>false</visualize>
<topic>camera</topic>
</sensor>
<visual name="sphere">
<geometry>
<sphere>
<radius>0.5</radius>
</sphere>
</geometry>
<material>
<ambient>0.3 0.3 0.3 1</ambient>
<diffuse>0.3 0.3 0.3 1</diffuse>
<specular>0.3 0.3 0.3 1</specular>
<emmisive>0.3 0.3 0.3 1</emmisive>
</material>
</visual>
</link>
</model>
</world>
</sdf>

0 comments on commit cfb0d74

Please sign in to comment.