Skip to content

Commit

Permalink
Fixup unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
julianoes committed Oct 25, 2024
1 parent 6076b02 commit a55ba6f
Show file tree
Hide file tree
Showing 20 changed files with 187 additions and 174 deletions.
76 changes: 12 additions & 64 deletions src/mavsdk/plugins/camera/camera_definition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,16 @@ bool CameraDefinition::load_string(const std::string& content)

std::string CameraDefinition::get_model() const
{
std::lock_guard<std::mutex> lock(_mutex);

return _model;
}

std::string CameraDefinition::get_vendor() const
{
std::lock_guard<std::mutex> lock(_mutex);

return _vendor;
}

bool CameraDefinition::parse_xml()
{
std::lock_guard<std::mutex> lock(_mutex);

auto e_mavlinkcamera = _doc.FirstChildElement("mavlinkcamera");
if (!e_mavlinkcamera) {
LogErr() << "Tag mavlinkcamera not found";
Expand Down Expand Up @@ -183,7 +177,7 @@ bool CameraDefinition::parse_xml()
for (auto e_update = e_updates->FirstChildElement("update"); e_update != nullptr;
e_update = e_update->NextSiblingElement("update")) {
// LogDebug() << "Updates: " << e_update->GetText();
new_parameter->updates.push_back(e_update->GetText());
new_parameter->updates.emplace_back(e_update->GetText());
}
}

Expand Down Expand Up @@ -292,7 +286,7 @@ CameraDefinition::parse_options(
for (auto e_exclude = e_exclusions->FirstChildElement("exclude"); e_exclude != nullptr;
e_exclude = e_exclude->NextSiblingElement("exclude")) {
// LogDebug() << "Exclude: " << e_exclude->GetText();
new_option->exclusions.push_back(e_exclude->GetText());
new_option->exclusions.emplace_back(e_exclude->GetText());
}
}

Expand Down Expand Up @@ -449,59 +443,32 @@ std::pair<bool, CameraDefinition::Option> CameraDefinition::find_default(

void CameraDefinition::assume_default_settings()
{
std::lock_guard<std::mutex> lock(_mutex);
reset_to_default_settings(false);
}

void CameraDefinition::reset_to_default_settings(bool needs_updating)
{
_current_settings.clear();

for (const auto& parameter : _parameter_map) {
// if (parameter.second->is_range) {

InternalCurrentSetting new_setting;
new_setting.value = parameter.second->default_option.value;
new_setting.needs_updating = true;
new_setting.needs_updating = needs_updating;
_current_settings[parameter.first] = new_setting;

//} else {

// for (const auto &option : parameter.second->options) {
// if (!option->is_default) {
// //LogDebug() << option->name << " not default";
// continue;
// }
// //LogDebug() << option->name << " default value: " << option->value << " (type: "
// <<
// // option->value.typestr() << ")";

// InternalCurrentSetting new_setting;
// new_setting.value = option->value;
// new_setting.needs_updating = false;
// _current_settings[parameter.first] = new_setting;
// }
//}
}
}

bool CameraDefinition::get_all_settings(std::unordered_map<std::string, ParamValue>& settings)
{
std::lock_guard<std::mutex> lock(_mutex);

settings.clear();
for (const auto& current_setting : _current_settings) {
settings[current_setting.first] = current_setting.second.value;
}

return (settings.size() > 0);
return !settings.empty();
}

bool CameraDefinition::get_possible_settings(std::unordered_map<std::string, ParamValue>& settings)
{
std::lock_guard<std::mutex> lock(_mutex);

return get_possible_settings_locked(settings);
}

bool CameraDefinition::get_possible_settings_locked(
std::unordered_map<std::string, ParamValue>& settings)
{
settings.clear();

Expand Down Expand Up @@ -543,8 +510,6 @@ bool CameraDefinition::get_possible_settings_locked(

bool CameraDefinition::set_setting(const std::string& name, const ParamValue& value)
{
std::lock_guard<std::mutex> lock(_mutex);

if (_parameter_map.find(name) == _parameter_map.end()) {
LogErr() << "Unknown setting to set: " << name;
return false;
Expand Down Expand Up @@ -585,8 +550,6 @@ bool CameraDefinition::set_setting(const std::string& name, const ParamValue& va

bool CameraDefinition::get_setting(const std::string& name, ParamValue& value)
{
std::lock_guard<std::mutex> lock(_mutex);

if (_current_settings.find(name) == _current_settings.end()) {
LogErr() << "Unknown setting to get: " << name;
return false;
Expand All @@ -596,15 +559,14 @@ bool CameraDefinition::get_setting(const std::string& name, ParamValue& value)
value = _current_settings.at(name).value;
return true;
} else {
LogWarn() << "Needs updating.";
return false;
}
}

bool CameraDefinition::get_option_value(
const std::string& param_name, const std::string& option_value, ParamValue& value)
{
std::lock_guard<std::mutex> lock(_mutex);

if (_parameter_map.find(param_name) == _parameter_map.end()) {
LogErr() << "Unknown parameter to get option: " << param_name;
return false;
Expand All @@ -622,8 +584,6 @@ bool CameraDefinition::get_option_value(

bool CameraDefinition::get_all_options(const std::string& name, std::vector<ParamValue>& values)
{
std::lock_guard<std::mutex> lock(_mutex);

values.clear();

if (_parameter_map.find(name) == _parameter_map.end()) {
Expand All @@ -641,8 +601,6 @@ bool CameraDefinition::get_all_options(const std::string& name, std::vector<Para
bool CameraDefinition::get_possible_options(
const std::string& name, std::vector<ParamValue>& values)
{
std::lock_guard<std::mutex> lock(_mutex);

values.clear();

if (_parameter_map.find(name) == _parameter_map.end()) {
Expand All @@ -651,7 +609,7 @@ bool CameraDefinition::get_possible_options(
}

std::unordered_map<std::string, ParamValue> settings;
if (!get_possible_settings_locked(settings)) {
if (!get_possible_settings(settings)) {
return false;
}
if (settings.find(name) == settings.end()) {
Expand Down Expand Up @@ -707,7 +665,7 @@ bool CameraDefinition::get_possible_options(
// Only look at current set option.
if (_current_settings[parameter.first].value == option->value) {
// Go through parameter ranges but only concerning the parameter that
// we're interested in..
// we're interested in.
if (option->parameter_ranges.find(name) != option->parameter_ranges.end()) {
for (const auto& range : option->parameter_ranges[name]) {
allowed_ranges.push_back(range.second);
Expand All @@ -725,7 +683,7 @@ bool CameraDefinition::get_possible_options(
option_allowed = true;
}
}
if (option_allowed || allowed_ranges.size() == 0) {
if (option_allowed || allowed_ranges.empty()) {
values.push_back(option->value);
}
}
Expand All @@ -735,8 +693,6 @@ bool CameraDefinition::get_possible_options(

void CameraDefinition::get_unknown_params(std::vector<std::pair<std::string, ParamValue>>& params)
{
std::lock_guard<std::mutex> lock(_mutex);

params.clear();

for (const auto& parameter : _parameter_map) {
Expand All @@ -748,17 +704,13 @@ void CameraDefinition::get_unknown_params(std::vector<std::pair<std::string, Par

void CameraDefinition::set_all_params_unknown()
{
std::lock_guard<std::mutex> lock(_mutex);

for (auto& parameter : _parameter_map) {
_current_settings[parameter.first].needs_updating = true;
}
}

bool CameraDefinition::is_setting_range(const std::string& name)
{
std::lock_guard<std::mutex> lock(_mutex);

if (_parameter_map.find(name) == _parameter_map.end()) {
LogWarn() << "Setting " << name << " not found.";
return false;
Expand All @@ -769,8 +721,6 @@ bool CameraDefinition::is_setting_range(const std::string& name)

bool CameraDefinition::get_setting_str(const std::string& name, std::string& description)
{
std::lock_guard<std::mutex> lock(_mutex);

description.clear();

if (_parameter_map.find(name) == _parameter_map.end()) {
Expand All @@ -785,8 +735,6 @@ bool CameraDefinition::get_setting_str(const std::string& name, std::string& des
bool CameraDefinition::get_option_str(
const std::string& setting_name, const std::string& option_name, std::string& description)
{
std::lock_guard<std::mutex> lock(_mutex);

description.clear();

if (_parameter_map.find(setting_name) == _parameter_map.end()) {
Expand Down
9 changes: 4 additions & 5 deletions src/mavsdk/plugins/camera/camera_definition.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <vector>
#include <memory>
#include <unordered_map>
#include <mutex>
#include <string>
#include <tuple>
#include <utility>
Expand All @@ -23,8 +22,12 @@ class CameraDefinition {
std::string get_vendor() const;
std::string get_model() const;

// This is to just assume everything is the default, mostly for testing.
void assume_default_settings();

// This is to start and mark things as requiring an update.
void reset_to_default_settings(bool needs_updating);

struct Setting {
std::string name;
ParamValue value;
Expand Down Expand Up @@ -54,8 +57,6 @@ class CameraDefinition {
const CameraDefinition& operator=(const CameraDefinition&) = delete;

private:
bool get_possible_settings_locked(std::unordered_map<std::string, ParamValue>& settings);

using ParameterRange = std::unordered_map<std::string, ParamValue>;

struct Option {
Expand Down Expand Up @@ -92,8 +93,6 @@ class CameraDefinition {
std::pair<bool, Option> find_default(
const std::vector<std::shared_ptr<Option>>& options, const std::string& default_str);

mutable std::mutex _mutex{};

tinyxml2::XMLDocument _doc{};

std::unordered_map<std::string, std::shared_ptr<Parameter>> _parameter_map{};
Expand Down
Loading

0 comments on commit a55ba6f

Please sign in to comment.