-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ba03a61
commit 1b7c015
Showing
14 changed files
with
332 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) 2020 Stefan Fabian. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
#ifndef QML_ROS_PLUGIN_PACKAGE_H | ||
#define QML_ROS_PLUGIN_PACKAGE_H | ||
|
||
#include <QtCore> | ||
|
||
namespace qml_ros_plugin | ||
{ | ||
|
||
/*! | ||
* A wrapper for ros::package | ||
*/ | ||
class Package | ||
{ | ||
Q_GADGET | ||
|
||
public: | ||
/*! | ||
* Runs a command of the form 'rospack <cmd>'. (This does not make a subprocess call!) | ||
* @param cmd The command passed to rospack. | ||
* @return The output of the command as a string. | ||
*/ | ||
Q_INVOKABLE QString command( const QString &cmd ); | ||
|
||
/*! | ||
* Queries the path to a package. | ||
* @param package_name The name of the package. | ||
* @return The fully-qualified path to the given package or an empty string if the package is not found. | ||
*/ | ||
Q_INVOKABLE QString getPath( const QString &package_name ); | ||
|
||
/*! | ||
* @return A list of all packages. | ||
*/ | ||
Q_INVOKABLE QStringList getAll(); | ||
|
||
/*! | ||
* Queries for all plugins exported for a given package. | ||
* | ||
* @code{.xml} | ||
* <export> | ||
* <name attribute="value"/> | ||
* <rviz plugin="${prefix}/plugin_description.xml"/> | ||
* </export> | ||
* @endcode | ||
* | ||
* To query for rviz plugins you would pass 'rviz' as the name and 'plugin' as the attribute. | ||
* | ||
* @param name The name of the export tag. | ||
* @param attribute The name of the attribute for the value is obtained. | ||
* @param force_recrawl Forces rospack to rediscover everything on the system before running the search. | ||
* @return A map with the name of the package exporting something for name as the key (string) and a QStringList | ||
* containing all exported values as the value. | ||
*/ | ||
Q_INVOKABLE QVariantMap getPlugins( const QString &name, const QString &attribute, bool force_recrawl = false ); | ||
}; | ||
} | ||
|
||
Q_DECLARE_METATYPE( qml_ros_plugin::Package ); | ||
|
||
#endif //QML_ROS_PLUGIN_PACKAGE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) 2020 Stefan Fabian. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
#include "qml_ros_plugin/package.h" | ||
#include "qml_ros_plugin/message_conversions.h" | ||
|
||
#include <ros/package.h> | ||
#include <ros/ros.h> | ||
|
||
namespace qml_ros_plugin | ||
{ | ||
|
||
QString Package::command( const QString &cmd ) | ||
{ | ||
return QString::fromStdString( ros::package::command( cmd.toStdString())); | ||
} | ||
|
||
QString Package::getPath( const QString &package_name ) | ||
{ | ||
return QString::fromStdString( ros::package::getPath( package_name.toStdString())); | ||
} | ||
|
||
QStringList Package::getAll() | ||
{ | ||
QStringList result; | ||
ros::V_string result_std; | ||
if ( !ros::package::getAll( result_std )) | ||
{ | ||
ROS_WARN_NAMED( "qml_ros_plugin", "Failed to get packages!" ); | ||
return {}; | ||
} | ||
result.reserve( result_std.size()); | ||
for ( const auto &s : result_std ) | ||
result.append( QString::fromStdString( s )); | ||
return result; | ||
} | ||
|
||
QVariantMap Package::getPlugins( const QString &name, const QString &attribute, bool force_recrawl ) | ||
{ | ||
std::vector<std::pair<std::string, std::string>> exports; | ||
ros::package::getPlugins( name.toStdString(), attribute.toStdString(), exports, force_recrawl ); | ||
QVariantMap result; | ||
for ( const auto &pair : exports ) | ||
{ | ||
const QString &package = QString::fromStdString( pair.first ); | ||
const QString &value = QString::fromStdString( pair.second ); | ||
if ( result.contains( package )) | ||
{ | ||
conversion::obtainValueAsReference<QStringList>( result[package] ).append( value ); | ||
continue; | ||
} | ||
result.insert( package, QStringList{ value } ); | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// | ||
// Created by Stefan Fabian on 04.03.20. | ||
// | ||
|
||
#include "common.h" | ||
#include "message_comparison.h" | ||
//#ifndef _WIN32 | ||
//#include <unistd.h> | ||
//#endif | ||
|
||
#include <qml_ros_plugin/ros.h> | ||
|
||
#include <ros/ros.h> | ||
#include <ros/package.h> | ||
|
||
#ifdef _WIN32 | ||
int setenv(const char *name, const char *value, int overwrite) | ||
{ | ||
if(!overwrite) | ||
{ | ||
size_t envsize = 0; | ||
errno_t errcode = getenv_s(&envsize, NULL, 0, name); | ||
if(errcode || envsize) | ||
return errcode; | ||
} | ||
return _putenv_s(name, value); | ||
} | ||
#endif | ||
|
||
template<typename ContainerA, typename KeyType> | ||
::testing::AssertionResult listEquals( const ContainerA &container, const std::vector<KeyType> &values ) | ||
{ | ||
std::map<KeyType, bool> found; | ||
for ( const auto &key : values ) | ||
{ | ||
found.insert( { key, false } ); | ||
} | ||
for ( const auto &value : container ) | ||
{ | ||
if ( found.find( value ) == found.end()) | ||
return ::testing::AssertionFailure() << value << " is in container but was not expected!"; | ||
found[value] = true; | ||
} | ||
for ( const auto &pair : found ) | ||
{ | ||
if ( !pair.second ) | ||
return ::testing::AssertionFailure() << pair.first << " was not found in container."; | ||
} | ||
return ::testing::AssertionSuccess(); | ||
} | ||
|
||
using namespace qml_ros_plugin; | ||
|
||
TEST( Package, package ) | ||
{ | ||
RosQmlSingletonWrapper wrapper; | ||
QString path = wrapper.package().getPath( ROS_PACKAGE_NAME ); | ||
ASSERT_FALSE( path.isEmpty()); | ||
EXPECT_EQ( path.toStdString(), ros::package::getPath( ROS_PACKAGE_NAME )); | ||
|
||
char *oldrpp = getenv( "ROS_PACKAGE_PATH" ); | ||
std::string package_path = path.toStdString() + "/test/test_packages"; | ||
setenv( "ROS_PACKAGE_PATH", package_path.c_str(), 1 ); | ||
path = wrapper.package().getPath( ROS_PACKAGE_NAME ); | ||
EXPECT_TRUE( path.isEmpty()); | ||
|
||
EXPECT_TRUE( listEquals( wrapper.package().getAll(), | ||
std::vector<QString>{ "poor_people", "rich_people", "rick_astley", "world" } )); | ||
|
||
QVariantMap plugins = wrapper.package().getPlugins( "world", "value" ); | ||
std::map<QString, std::vector<QString>> expected_plugins = {{ "rich_people", { "Nothing but despair" }}, | ||
{ "rick_astley", { "Never Gonna Give You Up" }}}; | ||
for ( const auto &key : plugins.keys()) | ||
{ | ||
ASSERT_NE( expected_plugins.find( key ), expected_plugins.end()) << key; | ||
EXPECT_TRUE( listEquals( plugins[key].toStringList(), expected_plugins[key] )) << key; | ||
} | ||
|
||
plugins = wrapper.package().getPlugins( "world", "wars" ); | ||
expected_plugins = {{ "rich_people", { "true" }}}; | ||
|
||
for ( const auto &key : plugins.keys()) | ||
{ | ||
ASSERT_NE( expected_plugins.find( key ), expected_plugins.end()) << key; | ||
EXPECT_TRUE( listEquals( plugins[key].toStringList(), expected_plugins[key] )) << key; | ||
} | ||
|
||
QString output = wrapper.package().command( "depends-on poor_people" ); | ||
EXPECT_EQ( output.trimmed(), QString( "rich_people" )); | ||
|
||
setenv( "ROS_PACKAGE_PATH", (package_path + "/not_existing_subfolder").c_str(), 1 ); | ||
ASSERT_TRUE( wrapper.package().getAll().empty()); | ||
|
||
setenv( "ROS_PACKAGE_PATH", oldrpp, 1 ); | ||
} | ||
|
||
int main( int argc, char **argv ) | ||
{ | ||
testing::InitGoogleTest( &argc, argv ); | ||
ros::init( argc, argv, "test_logging" ); | ||
QCoreApplication app( argc, argv ); | ||
return RUN_ALL_TESTS(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<launch> | ||
<test test-name="package" pkg="qml_ros_plugin" type="test_package"/> | ||
</launch> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0"?> | ||
<package format="2"> | ||
<name>poor_people</name> | ||
<version>1.0.0</version> | ||
<description>The poor_people package</description> | ||
|
||
<maintainer email="qml_ros_plugin@stefanfabian.me">Stefan Fabian</maintainer> | ||
|
||
<license>Birthright</license> | ||
|
||
<buildtool_depend>catkin</buildtool_depend> | ||
|
||
<depend>world</depend> | ||
|
||
<export> | ||
</export> | ||
</package> |
Oops, something went wrong.