00001
00002
00003
00004
00005
00006
00007
00008 #define RTT_STATIC
00009
00010 #include <rtt_dynamic_reconfigure/server.h>
00011 #include <rtt_dynamic_reconfigure/auto_config.h>
00012 #include <gtest/gtest.h>
00013
00014 #include <rtt/plugin/PluginLoader.hpp>
00015 #include <rtt/Logger.hpp>
00016
00017 #include <rtt/deployment/ComponentLoader.hpp>
00018 #include <rtt/os/main.h>
00019
00020 #include <rtt/internal/GlobalService.hpp>
00021 #include <rtt/OperationCaller.hpp>
00022
00023 #include <dynamic_reconfigure/ConfigDescription.h>
00024 #include <dynamic_reconfigure/config_tools.h>
00025
00026 #include <rtt_dynamic_reconfigure_tests/TestConfig.h>
00027 #include "test_component.hpp"
00028
00029 using namespace rtt_dynamic_reconfigure;
00030 using namespace rtt_dynamic_reconfigure_tests;
00031
00032 class DynamicReconfigureTest : public ::testing::Test
00033 {
00034 public:
00035 DynamicReconfigureTestComponent tc;
00036
00037
00038
00039 };
00040
00041 static const dynamic_reconfigure::Group *getGroup(const dynamic_reconfigure::ConfigDescription::_groups_type *groups, const std::string &name)
00042 {
00043 if (!groups) return 0;
00044 for(dynamic_reconfigure::ConfigDescription::_groups_type::const_iterator it = groups->begin(); it != groups->end(); ++it) {
00045 if (it->name == name) return &(*it);
00046 }
00047 return 0;
00048 }
00049
00050 static const dynamic_reconfigure::ParamDescription *getParamDescription(const dynamic_reconfigure::Group *group, const std::string &name)
00051 {
00052 if (!group) return 0;
00053 for(dynamic_reconfigure::Group::_parameters_type::const_iterator it = group->parameters.begin(); it != group->parameters.end(); ++it) {
00054 if (it->name == name) return &(*it);
00055 }
00056 return 0;
00057 }
00058
00059 TEST_F(DynamicReconfigureTest, ConfigDescription)
00060 {
00061
00062 ASSERT_TRUE(tc.loadService("test_reconfigure"));
00063 ASSERT_TRUE(tc.provides()->hasService("test_reconfigure"));
00064
00065
00066 EXPECT_EQ(0, tc.props.int_param);
00067 EXPECT_EQ(0, tc.props.double_param);
00068 EXPECT_EQ("", tc.props.str_param);
00069 EXPECT_FALSE(tc.props.bool_param);
00070
00071
00072 ASSERT_TRUE(tc.properties()->getPropertyType<double>("non_existent"));
00073 EXPECT_EQ(5.0, *(tc.properties()->getPropertyType<double>("non_existent")));
00074
00075
00076 boost::shared_ptr<Server<TestConfig> > server = boost::dynamic_pointer_cast<Server<TestConfig> >(tc.provides("test_reconfigure"));
00077 ASSERT_TRUE(server.get());
00078
00079
00080 dynamic_reconfigure::ConfigDescriptionPtr description = server->getDescriptionMessage();
00081 EXPECT_TRUE(getParamDescription(getGroup(&(description->groups), "Default"), "int_param"));
00082 EXPECT_TRUE(getParamDescription(getGroup(&(description->groups), "Default"), "double_param"));
00083 EXPECT_TRUE(getParamDescription(getGroup(&(description->groups), "Default"), "str_param"));
00084 EXPECT_TRUE(getParamDescription(getGroup(&(description->groups), "Default"), "bool_param"));
00085 EXPECT_TRUE(getParamDescription(getGroup(&(description->groups), "Default"), "non_existent"));
00086
00087
00088
00089
00090 struct {
00091 int int_param;
00092 double double_param;
00093 std::string str_param;
00094 bool bool_param;
00095 std::string group_param;
00096 } temp;
00097 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "int_param", temp.int_param));
00098 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "double_param", temp.double_param));
00099 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "str_param", temp.str_param));
00100 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "bool_param", temp.bool_param));
00101
00102 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "int_param", temp.int_param));
00103 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "double_param", temp.double_param));
00104 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "str_param", temp.str_param));
00105 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "bool_param", temp.bool_param));
00106
00107 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "int_param", temp.int_param));
00108 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "double_param", temp.double_param));
00109 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "str_param", temp.str_param));
00110 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "bool_param", temp.bool_param));
00111
00112 }
00113
00114 TEST_F(DynamicReconfigureTest, MinMaxDefault)
00115 {
00116
00117 ASSERT_TRUE(tc.loadService("test_reconfigure"));
00118 ASSERT_TRUE(tc.provides()->hasService("test_reconfigure"));
00119
00120
00121 const RTT::PropertyBag &min = tc.provides("test_reconfigure")->properties()->getPropertyType<RTT::PropertyBag>("min")->rvalue();
00122 EXPECT_EQ(0, *(min.getPropertyType<int>("int_param")));
00123 EXPECT_EQ(0.0, *(min.getPropertyType<double>("double_param")));
00124 EXPECT_EQ(0.0, *(min.getPropertyType<double>("non_existent")));
00125
00126
00127 const RTT::PropertyBag &max = tc.provides("test_reconfigure")->properties()->getPropertyType<RTT::PropertyBag>("max")->rvalue();
00128 EXPECT_EQ(100, *(max.getPropertyType<int>("int_param")));
00129 EXPECT_EQ(1.0, *(max.getPropertyType<double>("double_param")));
00130 EXPECT_EQ(10.0, *(max.getPropertyType<double>("non_existent")));
00131
00132
00133 const RTT::PropertyBag &dflt = tc.provides("test_reconfigure")->properties()->getPropertyType<RTT::PropertyBag>("default")->rvalue();
00134 EXPECT_EQ(50, *(dflt.getPropertyType<int>("int_param")));
00135 EXPECT_EQ(.5, *(dflt.getPropertyType<double>("double_param")));
00136 EXPECT_EQ("Hello World", dflt.getPropertyType<std::string>("str_param")->rvalue());
00137 EXPECT_TRUE(*(dflt.getPropertyType<bool>("bool_param")));
00138 EXPECT_EQ(5.0, *(dflt.getPropertyType<double>("non_existent")));
00139 }
00140
00141 TEST_F(DynamicReconfigureTest, AutoConfig)
00142 {
00143
00144 ASSERT_TRUE(tc.loadService("reconfigure"));
00145 ASSERT_TRUE(tc.provides()->hasService("reconfigure"));
00146
00147
00148 boost::shared_ptr<Server<AutoConfig> > server = boost::dynamic_pointer_cast<Server<AutoConfig> >(tc.provides("reconfigure"));
00149 ASSERT_TRUE(server.get());
00150
00151
00152 ASSERT_TRUE(tc.provides("reconfigure")->properties()->getPropertyType<RTT::PropertyBag>("min"));
00153 ASSERT_TRUE(tc.provides("reconfigure")->properties()->getPropertyType<RTT::PropertyBag>("max"));
00154 ASSERT_TRUE(tc.provides("reconfigure")->properties()->getPropertyType<RTT::PropertyBag>("default"));
00155 ASSERT_EQ(tc.properties()->size(), tc.provides("reconfigure")->properties()->getPropertyType<RTT::PropertyBag>("min")->rvalue().size());
00156 ASSERT_EQ(tc.properties()->size(), tc.provides("reconfigure")->properties()->getPropertyType<RTT::PropertyBag>("max")->rvalue().size());
00157 ASSERT_EQ(tc.properties()->size(), tc.provides("reconfigure")->properties()->getPropertyType<RTT::PropertyBag>("default")->rvalue().size());
00158
00159
00160 EXPECT_EQ(0, tc.props.int_param);
00161 EXPECT_EQ(0.0, tc.props.double_param);
00162 EXPECT_EQ("", tc.props.str_param);
00163 EXPECT_FALSE(tc.props.bool_param);
00164 EXPECT_EQ(0.0, tc.props.float_param);
00165 EXPECT_EQ(0, tc.props.uint_param);
00166 EXPECT_TRUE(tc.props.bag_param.size() == 1);
00167 EXPECT_EQ("", tc.props.str_param_in_bag);
00168 EXPECT_EQ(1.0, tc.props.vector3_param.x);
00169 EXPECT_EQ(2.0, tc.props.vector3_param.y);
00170 EXPECT_EQ(3.0, tc.props.vector3_param.z);
00171
00172
00173 const RTT::PropertyBag &dflt = tc.provides("reconfigure")->properties()->getPropertyType<RTT::PropertyBag>("default")->rvalue();
00174 ASSERT_TRUE(dflt.getPropertyType<int>("int_param"));
00175 EXPECT_EQ(tc.props.int_param, *(dflt.getPropertyType<int>("int_param")));
00176 ASSERT_TRUE(dflt.getPropertyType<double>("double_param"));
00177 EXPECT_EQ(tc.props.double_param, *(dflt.getPropertyType<double>("double_param")));
00178 ASSERT_TRUE(dflt.getPropertyType<std::string>("str_param"));
00179 EXPECT_EQ(tc.props.str_param, dflt.getPropertyType<std::string>("str_param")->rvalue());
00180 ASSERT_TRUE(dflt.getPropertyType<bool>("bool_param"));
00181 EXPECT_EQ(tc.props.bool_param, *(dflt.getPropertyType<bool>("bool_param")));
00182 ASSERT_TRUE(dflt.getPropertyType<float>("float_param"));
00183 EXPECT_EQ(tc.props.float_param, *(dflt.getPropertyType<float>("float_param")));
00184 ASSERT_TRUE(dflt.getPropertyType<unsigned int>("uint_param"));
00185 EXPECT_EQ(tc.props.uint_param, *(dflt.getPropertyType<unsigned int>("uint_param")));
00186
00187 ASSERT_TRUE(dflt.getPropertyType<RTT::PropertyBag>("bag_param"));
00188 const RTT::PropertyBag &dflt_bag = *(dflt.getPropertyType<RTT::PropertyBag>("bag_param"));
00189 EXPECT_TRUE(dflt_bag.size() == 1);
00190 ASSERT_TRUE(dflt_bag.getPropertyType<std::string>("str_param"));
00191 EXPECT_EQ(tc.props.str_param_in_bag, dflt_bag.getPropertyType<std::string>("str_param")->rvalue());
00192
00193 ASSERT_TRUE(dflt.getPropertyType<RTT::PropertyBag>("vector3_param"));
00194 const RTT::PropertyBag &dflt_vector3_bag = *(dflt.getPropertyType<RTT::PropertyBag>("vector3_param"));
00195 EXPECT_TRUE(dflt_vector3_bag.size() == 3);
00196 ASSERT_TRUE(dflt_vector3_bag.getPropertyType<double>("x"));
00197 ASSERT_TRUE(dflt_vector3_bag.getPropertyType<double>("y"));
00198 ASSERT_TRUE(dflt_vector3_bag.getPropertyType<double>("z"));
00199 EXPECT_EQ(tc.props.vector3_param.x, dflt_vector3_bag.getPropertyType<double>("x")->rvalue());
00200 EXPECT_EQ(tc.props.vector3_param.y, dflt_vector3_bag.getPropertyType<double>("y")->rvalue());
00201 EXPECT_EQ(tc.props.vector3_param.z, dflt_vector3_bag.getPropertyType<double>("z")->rvalue());
00202
00203
00204 dynamic_reconfigure::ConfigDescriptionPtr description = server->getDescriptionMessage();
00205 ASSERT_TRUE(description->groups.size() == 3);
00206 EXPECT_EQ("Default", description->groups[0].name);
00207 EXPECT_EQ("", description->groups[0].type);
00208 EXPECT_EQ(6, description->groups[0].parameters.size());
00209 EXPECT_EQ(0, description->groups[0].parent);
00210 EXPECT_EQ(0, description->groups[0].id);
00211 EXPECT_EQ("bag_param", description->groups[1].name);
00212 EXPECT_EQ("", description->groups[1].type);
00213 EXPECT_EQ(1, description->groups[1].parameters.size());
00214 EXPECT_EQ(0, description->groups[1].parent);
00215 EXPECT_EQ(1, description->groups[1].id);
00216 EXPECT_EQ("vector3_param", description->groups[2].name);
00217 EXPECT_EQ("", description->groups[2].type);
00218 EXPECT_EQ(3, description->groups[2].parameters.size());
00219 EXPECT_EQ(0, description->groups[2].parent);
00220 EXPECT_EQ(2, description->groups[2].id);
00221
00222
00223 struct {
00224 int int_param;
00225 double double_param;
00226 std::string str_param;
00227 bool bool_param;
00228 double float_param;
00229 int uint_param;
00230 std::string str_param_in_bag;
00231 geometry_msgs::Vector3 vector3_param;
00232 } temp;
00233 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "int_param", temp.int_param));
00234 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "double_param", temp.double_param));
00235 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "str_param", temp.str_param));
00236 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "bool_param", temp.bool_param));
00237 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "float_param", temp.float_param));
00238 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "uint_param", temp.uint_param));
00239 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "bag_param__str_param", temp.str_param_in_bag));
00240 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "vector3_param__x", temp.vector3_param.x));
00241 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "vector3_param__y", temp.vector3_param.y));
00242 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "vector3_param__z", temp.vector3_param.z));
00243 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "int_param", temp.int_param));
00244 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "double_param", temp.double_param));
00245 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "str_param", temp.str_param));
00246 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "bool_param", temp.bool_param));
00247 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "float_param", temp.float_param));
00248 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "uint_param", temp.uint_param));
00249 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "bag_param__str_param", temp.str_param_in_bag));
00250 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "vector3_param__x", temp.vector3_param.x));
00251 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "vector3_param__y", temp.vector3_param.y));
00252 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "vector3_param__z", temp.vector3_param.z));
00253 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "int_param", temp.int_param));
00254 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "double_param", temp.double_param));
00255 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "str_param", temp.str_param));
00256 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "bool_param", temp.bool_param));
00257 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "float_param", temp.float_param));
00258 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "uint_param", temp.uint_param));
00259 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "bag_param__str_param", temp.str_param_in_bag));
00260 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "vector3_param__x", temp.vector3_param.x));
00261 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "vector3_param__y", temp.vector3_param.y));
00262 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "vector3_param__z", temp.vector3_param.z));
00263 }
00264
00265 TEST_F(DynamicReconfigureTest, AutoConfigAddPropertiesAndRefresh)
00266 {
00267
00268 ASSERT_TRUE(tc.loadService("reconfigure"));
00269 ASSERT_TRUE(tc.provides()->hasService("reconfigure"));
00270
00271
00272 boost::shared_ptr<Server<AutoConfig> > server = boost::dynamic_pointer_cast<Server<AutoConfig> >(tc.provides("reconfigure"));
00273 ASSERT_TRUE(server.get());
00274
00275
00276 tc.properties()->ownProperty(new RTT::Property<std::string>("new_param"));
00277
00278
00279 server->refresh();
00280
00281
00282 dynamic_reconfigure::ConfigDescriptionPtr description = server->getDescriptionMessage();
00283 std::string str;
00284 EXPECT_TRUE(getParamDescription(getGroup(&(description->groups), "Default"), "new_param"));
00285 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "new_param", str));
00286 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "new_param", str));
00287 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "new_param", str));
00288 }
00289
00290 int main(int argc, char **argv) {
00291 ::testing::InitGoogleTest(&argc, argv);
00292
00293
00294 __os_init(argc, argv);
00295
00296 RTT::Logger::log().setStdStream(std::cerr);
00297 RTT::Logger::log().mayLogStdOut(true);
00298 RTT::Logger::log().setLogLevel(RTT::Logger::Debug);
00299
00300 RTT::ComponentLoader::Instance()->import("rtt_ros", std::string());
00301 RTT::OperationCaller<bool(std::string)> import = RTT::internal::GlobalService::Instance()->provides("ros")->getOperation("import");
00302 import("rtt_dynamic_reconfigure_tests");
00303
00304 return RUN_ALL_TESTS();
00305 }