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, DefaultUpdateCallback)
00142 {
00143
00144 ASSERT_TRUE(tc.loadService("reconfigure"));
00145 ASSERT_TRUE(tc.provides()->hasService("reconfigure"));
00146
00147
00148 EXPECT_FALSE(tc.updatePropertiesCalled);
00149 EXPECT_FALSE(tc.updatePropertiesConstCalled);
00150 EXPECT_FALSE(tc.notifyPropertiesUpdateCalled);
00151
00152
00153 dynamic_reconfigure::Reconfigure reconfigure;
00154 EXPECT_TRUE(tc.setConfigCallback("reconfigure", reconfigure.request, reconfigure.response));
00155 EXPECT_FALSE(tc.updatePropertiesCalled);
00156 EXPECT_FALSE(tc.updatePropertiesConstCalled);
00157 EXPECT_FALSE(tc.notifyPropertiesUpdateCalled);
00158 }
00159
00160 TEST_F(DynamicReconfigureTest, UpdatePropertiesCallback)
00161 {
00162
00163 tc.addOperation("updateProperties", &DynamicReconfigureTestComponent::updateProperties, &tc);
00164
00165
00166 ASSERT_TRUE(tc.loadService("reconfigure"));
00167 ASSERT_TRUE(tc.provides()->hasService("reconfigure"));
00168
00169
00170 EXPECT_TRUE(tc.updatePropertiesCalled);
00171 EXPECT_FALSE(tc.updatePropertiesConstCalled);
00172 EXPECT_FALSE(tc.notifyPropertiesUpdateCalled);
00173 tc.updatePropertiesCalled = false;
00174
00175
00176 dynamic_reconfigure::Reconfigure reconfigure;
00177 EXPECT_TRUE(tc.setConfigCallback("reconfigure", reconfigure.request, reconfigure.response));
00178 EXPECT_TRUE(tc.updatePropertiesCalled);
00179 EXPECT_FALSE(tc.updatePropertiesConstCalled);
00180 EXPECT_FALSE(tc.notifyPropertiesUpdateCalled);
00181 }
00182
00183 TEST_F(DynamicReconfigureTest, UpdatePropertiesConstCallback)
00184 {
00185
00186 tc.addOperation("updateProperties", &DynamicReconfigureTestComponent::updatePropertiesConst, &tc);
00187
00188
00189 ASSERT_TRUE(tc.loadService("reconfigure"));
00190 ASSERT_TRUE(tc.provides()->hasService("reconfigure"));
00191
00192
00193 EXPECT_FALSE(tc.updatePropertiesCalled);
00194 EXPECT_TRUE(tc.updatePropertiesConstCalled);
00195 EXPECT_FALSE(tc.notifyPropertiesUpdateCalled);
00196 tc.updatePropertiesConstCalled = false;
00197
00198
00199 dynamic_reconfigure::Reconfigure reconfigure;
00200 EXPECT_TRUE(tc.setConfigCallback("reconfigure", reconfigure.request, reconfigure.response));
00201 EXPECT_FALSE(tc.updatePropertiesCalled);
00202 EXPECT_TRUE(tc.updatePropertiesConstCalled);
00203 EXPECT_FALSE(tc.notifyPropertiesUpdateCalled);
00204 }
00205
00206 TEST_F(DynamicReconfigureTest, NotifyPropertiesUpdateCallback)
00207 {
00208
00209 tc.addOperation("notifyPropertiesUpdate", &DynamicReconfigureTestComponent::notifyPropertiesUpdate, &tc);
00210
00211
00212 ASSERT_TRUE(tc.loadService("reconfigure"));
00213 ASSERT_TRUE(tc.provides()->hasService("reconfigure"));
00214
00215
00216 EXPECT_FALSE(tc.updatePropertiesCalled);
00217 EXPECT_FALSE(tc.updatePropertiesConstCalled);
00218 EXPECT_TRUE(tc.notifyPropertiesUpdateCalled);
00219 tc.notifyPropertiesUpdateCalled = false;
00220
00221
00222 dynamic_reconfigure::Reconfigure reconfigure;
00223 EXPECT_TRUE(tc.setConfigCallback("reconfigure", reconfigure.request, reconfigure.response));
00224 EXPECT_FALSE(tc.updatePropertiesCalled);
00225 EXPECT_FALSE(tc.updatePropertiesConstCalled);
00226 EXPECT_TRUE(tc.notifyPropertiesUpdateCalled);
00227 }
00228
00229 TEST_F(DynamicReconfigureTest, AutoConfig)
00230 {
00231
00232 ASSERT_TRUE(tc.loadService("reconfigure"));
00233 ASSERT_TRUE(tc.provides()->hasService("reconfigure"));
00234
00235
00236 boost::shared_ptr<Server<AutoConfig> > server = boost::dynamic_pointer_cast<Server<AutoConfig> >(tc.provides("reconfigure"));
00237 ASSERT_TRUE(server.get());
00238
00239
00240 ASSERT_TRUE(tc.provides("reconfigure")->properties()->getPropertyType<RTT::PropertyBag>("min"));
00241 ASSERT_TRUE(tc.provides("reconfigure")->properties()->getPropertyType<RTT::PropertyBag>("max"));
00242 ASSERT_TRUE(tc.provides("reconfigure")->properties()->getPropertyType<RTT::PropertyBag>("default"));
00243 ASSERT_EQ(tc.properties()->size(), tc.provides("reconfigure")->properties()->getPropertyType<RTT::PropertyBag>("min")->rvalue().size());
00244 ASSERT_EQ(tc.properties()->size(), tc.provides("reconfigure")->properties()->getPropertyType<RTT::PropertyBag>("max")->rvalue().size());
00245 ASSERT_EQ(tc.properties()->size(), tc.provides("reconfigure")->properties()->getPropertyType<RTT::PropertyBag>("default")->rvalue().size());
00246
00247
00248 EXPECT_EQ(0, tc.props.int_param);
00249 EXPECT_EQ(0.0, tc.props.double_param);
00250 EXPECT_EQ("", tc.props.str_param);
00251 EXPECT_FALSE(tc.props.bool_param);
00252 EXPECT_EQ(0.0, tc.props.float_param);
00253 EXPECT_EQ(0, tc.props.uint_param);
00254 EXPECT_TRUE(tc.props.bag_param.size() == 1);
00255 EXPECT_EQ("", tc.props.str_param_in_bag);
00256 EXPECT_EQ(1.0, tc.props.vector3_param.x);
00257 EXPECT_EQ(2.0, tc.props.vector3_param.y);
00258 EXPECT_EQ(3.0, tc.props.vector3_param.z);
00259
00260
00261 const RTT::PropertyBag &dflt = tc.provides("reconfigure")->properties()->getPropertyType<RTT::PropertyBag>("default")->rvalue();
00262 ASSERT_TRUE(dflt.getPropertyType<int>("int_param"));
00263 EXPECT_EQ(tc.props.int_param, *(dflt.getPropertyType<int>("int_param")));
00264 ASSERT_TRUE(dflt.getPropertyType<double>("double_param"));
00265 EXPECT_EQ(tc.props.double_param, *(dflt.getPropertyType<double>("double_param")));
00266 ASSERT_TRUE(dflt.getPropertyType<std::string>("str_param"));
00267 EXPECT_EQ(tc.props.str_param, dflt.getPropertyType<std::string>("str_param")->rvalue());
00268 ASSERT_TRUE(dflt.getPropertyType<bool>("bool_param"));
00269 EXPECT_EQ(tc.props.bool_param, *(dflt.getPropertyType<bool>("bool_param")));
00270 ASSERT_TRUE(dflt.getPropertyType<float>("float_param"));
00271 EXPECT_EQ(tc.props.float_param, *(dflt.getPropertyType<float>("float_param")));
00272 ASSERT_TRUE(dflt.getPropertyType<unsigned int>("uint_param"));
00273 EXPECT_EQ(tc.props.uint_param, *(dflt.getPropertyType<unsigned int>("uint_param")));
00274
00275 ASSERT_TRUE(dflt.getPropertyType<RTT::PropertyBag>("bag_param"));
00276 const RTT::PropertyBag &dflt_bag = *(dflt.getPropertyType<RTT::PropertyBag>("bag_param"));
00277 EXPECT_TRUE(dflt_bag.size() == 1);
00278 ASSERT_TRUE(dflt_bag.getPropertyType<std::string>("str_param"));
00279 EXPECT_EQ(tc.props.str_param_in_bag, dflt_bag.getPropertyType<std::string>("str_param")->rvalue());
00280
00281 ASSERT_TRUE(dflt.getPropertyType<RTT::PropertyBag>("vector3_param"));
00282 const RTT::PropertyBag &dflt_vector3_bag = *(dflt.getPropertyType<RTT::PropertyBag>("vector3_param"));
00283 EXPECT_TRUE(dflt_vector3_bag.size() == 3);
00284 ASSERT_TRUE(dflt_vector3_bag.getPropertyType<double>("x"));
00285 ASSERT_TRUE(dflt_vector3_bag.getPropertyType<double>("y"));
00286 ASSERT_TRUE(dflt_vector3_bag.getPropertyType<double>("z"));
00287 EXPECT_EQ(tc.props.vector3_param.x, dflt_vector3_bag.getPropertyType<double>("x")->rvalue());
00288 EXPECT_EQ(tc.props.vector3_param.y, dflt_vector3_bag.getPropertyType<double>("y")->rvalue());
00289 EXPECT_EQ(tc.props.vector3_param.z, dflt_vector3_bag.getPropertyType<double>("z")->rvalue());
00290
00291
00292 dynamic_reconfigure::ConfigDescriptionPtr description = server->getDescriptionMessage();
00293 ASSERT_TRUE(description->groups.size() == 3);
00294 EXPECT_EQ("Default", description->groups[0].name);
00295 EXPECT_EQ("", description->groups[0].type);
00296 EXPECT_EQ(6, description->groups[0].parameters.size());
00297 EXPECT_EQ(0, description->groups[0].parent);
00298 EXPECT_EQ(0, description->groups[0].id);
00299 EXPECT_EQ("bag_param", description->groups[1].name);
00300 EXPECT_EQ("", description->groups[1].type);
00301 EXPECT_EQ(1, description->groups[1].parameters.size());
00302 EXPECT_EQ(0, description->groups[1].parent);
00303 EXPECT_EQ(1, description->groups[1].id);
00304 EXPECT_EQ("vector3_param", description->groups[2].name);
00305 EXPECT_EQ("", description->groups[2].type);
00306 EXPECT_EQ(3, description->groups[2].parameters.size());
00307 EXPECT_EQ(0, description->groups[2].parent);
00308 EXPECT_EQ(2, description->groups[2].id);
00309
00310
00311 struct {
00312 int int_param;
00313 double double_param;
00314 std::string str_param;
00315 bool bool_param;
00316 double float_param;
00317 int uint_param;
00318 std::string str_param_in_bag;
00319 geometry_msgs::Vector3 vector3_param;
00320 } temp;
00321 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "int_param", temp.int_param));
00322 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "double_param", temp.double_param));
00323 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "str_param", temp.str_param));
00324 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "bool_param", temp.bool_param));
00325 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "float_param", temp.float_param));
00326 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "uint_param", temp.uint_param));
00327 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "bag_param__str_param", temp.str_param_in_bag));
00328 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "vector3_param__x", temp.vector3_param.x));
00329 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "vector3_param__y", temp.vector3_param.y));
00330 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "vector3_param__z", temp.vector3_param.z));
00331 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "int_param", temp.int_param));
00332 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "double_param", temp.double_param));
00333 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "str_param", temp.str_param));
00334 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "bool_param", temp.bool_param));
00335 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "float_param", temp.float_param));
00336 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "uint_param", temp.uint_param));
00337 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "bag_param__str_param", temp.str_param_in_bag));
00338 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "vector3_param__x", temp.vector3_param.x));
00339 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "vector3_param__y", temp.vector3_param.y));
00340 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "vector3_param__z", temp.vector3_param.z));
00341 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "int_param", temp.int_param));
00342 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "double_param", temp.double_param));
00343 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "str_param", temp.str_param));
00344 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "bool_param", temp.bool_param));
00345 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "float_param", temp.float_param));
00346 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "uint_param", temp.uint_param));
00347 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "bag_param__str_param", temp.str_param_in_bag));
00348 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "vector3_param__x", temp.vector3_param.x));
00349 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "vector3_param__y", temp.vector3_param.y));
00350 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "vector3_param__z", temp.vector3_param.z));
00351 }
00352
00353 TEST_F(DynamicReconfigureTest, AutoConfigAddPropertiesAndRefresh)
00354 {
00355
00356 ASSERT_TRUE(tc.loadService("reconfigure"));
00357 ASSERT_TRUE(tc.provides()->hasService("reconfigure"));
00358
00359
00360 boost::shared_ptr<Server<AutoConfig> > server = boost::dynamic_pointer_cast<Server<AutoConfig> >(tc.provides("reconfigure"));
00361 ASSERT_TRUE(server.get());
00362
00363
00364 tc.properties()->ownProperty(new RTT::Property<std::string>("new_param"));
00365
00366
00367 server->refresh();
00368
00369
00370 dynamic_reconfigure::ConfigDescriptionPtr description = server->getDescriptionMessage();
00371 std::string str;
00372 EXPECT_TRUE(getParamDescription(getGroup(&(description->groups), "Default"), "new_param"));
00373 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->dflt, "new_param", str));
00374 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->min, "new_param", str));
00375 EXPECT_TRUE(dynamic_reconfigure::ConfigTools::getParameter(description->max, "new_param", str));
00376 }
00377
00378 int main(int argc, char **argv) {
00379 ::testing::InitGoogleTest(&argc, argv);
00380
00381
00382 __os_init(argc, argv);
00383
00384 RTT::Logger::log().setStdStream(std::cerr);
00385 RTT::Logger::log().mayLogStdOut(true);
00386 RTT::Logger::log().setLogLevel(RTT::Logger::Debug);
00387
00388 RTT::ComponentLoader::Instance()->import("rtt_ros", std::string());
00389 RTT::OperationCaller<bool(std::string)> import = RTT::internal::GlobalService::Instance()->provides("ros")->getOperation("import");
00390 import("rtt_dynamic_reconfigure_tests");
00391
00392 return RUN_ALL_TESTS();
00393 }