1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
/************************************************************************
*
* Copyright (C) 2023 IRCAD France
*
* This file is part of Sight.
*
* Sight is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Sight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sight. If not, see <https://www.gnu.org/licenses/>.
*
***********************************************************************/
#include "select.hpp"
#include "dialog.hpp"
#include <utility>
namespace sight::ui::test::helper
{
selector::selector(const char* _object_name) :
selector(type::from_main, _object_name)
{
}
selector::selector(const std::string& _object_name) :
selector(type::from_main, _object_name)
{
}
//------------------------------------------------------------------------------
selector selector::from_main(const std::string& _object_name)
{
return {type::from_main, _object_name};
}
//------------------------------------------------------------------------------
selector selector::from_dialog(const std::string& _object_name)
{
return {type::from_dialog, _object_name};
}
//------------------------------------------------------------------------------
selector selector::from_parent(const std::string& _parent_name, const std::string& _child_name)
{
return {type::from_parent, Data {std::in_place_type<std::pair<std::string, std::string> >, _parent_name, _child_name
}
};
}
//------------------------------------------------------------------------------
selector selector::from_current(const std::string& _object_name)
{
return {type::from_current, _object_name};
}
//------------------------------------------------------------------------------
selector selector::current()
{
return {type::current, {}};
}
//------------------------------------------------------------------------------
selector selector::dialog()
{
return {type::dialog, {}};
}
//------------------------------------------------------------------------------
selector selector::with_timeout(int _timeout) const
{
selector copy = *this;
copy.m_timeout = _timeout;
return copy;
}
//------------------------------------------------------------------------------
selector selector::with_condition(std::function<bool(QObject*)> _condition) const
{
selector copy = *this;
copy.m_conditions.push_back(_condition);
return copy;
}
//------------------------------------------------------------------------------
void selector::select(tester& _tester) const
{
static const auto s_VERIFY_CONDITIONS = [*this](QObject* _obj)
{
return std::ranges::all_of(
m_conditions,
[_obj](std::function<bool(QObject*)> _condition)
{
return _condition(_obj);
});
};
switch(m_type)
{
case type::from_main:
_tester.take(std::get<std::string>(m_data), std::get<std::string>(m_data), s_VERIFY_CONDITIONS, m_timeout);
break;
case type::from_dialog:
{
std::string child_name = std::get<std::string>(m_data);
dialog::take(_tester, "dialog", child_name);
_tester.yields(std::get<std::string>(m_data), child_name, s_VERIFY_CONDITIONS, m_timeout);
break;
}
case type::from_parent:
{
auto [parentName, childName] = std::get<std::pair<std::string, std::string> >(m_data);
_tester.take(parentName, parentName, always_true, m_timeout);
_tester.yields(childName, childName, s_VERIFY_CONDITIONS, m_timeout);
break;
}
case type::from_current:
_tester.yields(
std::get<std::string>(m_data),
std::get<std::string>(m_data),
s_VERIFY_CONDITIONS,
m_timeout
);
break;
case type::current:
break;
case type::dialog:
dialog::take(_tester, "dialog");
}
}
//------------------------------------------------------------------------------
std::string selector::get_description(const tester& _tester) const
{
switch(m_type)
{
case type::from_main:
return std::get<std::string>(m_data);
case type::from_dialog:
return std::get<std::string>(m_data) + " (from dialog)";
case type::from_parent:
{
auto [parentName, childName] = std::get<std::pair<std::string, std::string> >(m_data);
return childName + " (child of " + parentName + ")";
}
case type::from_current:
return std::get<std::string>(m_data) + " (from " + _tester.get_description() + ")";
case type::current:
return _tester.get_description();
case type::dialog:
return "dialog";
}
return "";
}
selector::selector(enum type _type, Data _data) :
m_type(_type),
m_data(std::move(_data))
{
}
} // namespace sight::ui::test::helper
|