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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
|
/************************************************************************
*
* Copyright (C) 2022-2024 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 "interaction.hpp"
#include "tester.hpp"
#include <core/spy_log.hpp>
#include <QAbstractButton>
#include <QTimer>
namespace sight::ui::test
{
//------------------------------------------------------------------------------
static std::string modifiers_to_string(Qt::KeyboardModifiers _modifiers)
{
std::vector<std::string> strings;
if((_modifiers& Qt::ShiftModifier) != 0U)
{
strings.emplace_back("Shift");
}
if((_modifiers& Qt::ControlModifier) != 0U)
{
strings.emplace_back("Control");
}
if((_modifiers& Qt::AltModifier) != 0U)
{
strings.emplace_back("Alt");
}
if((_modifiers& Qt::MetaModifier) != 0U)
{
strings.emplace_back("Meta");
}
if((_modifiers& Qt::KeypadModifier) != 0U)
{
strings.emplace_back("Keypad");
}
if((_modifiers& Qt::GroupSwitchModifier) != 0U)
{
strings.emplace_back("GroupSwitch");
}
std::string res;
if(!strings.empty())
{
for(size_t i = 0 ; i < strings.size() ; i++)
{
if(i != 0)
{
if(i == strings.size() - 1)
{
res += " and ";
}
else
{
res += ", ";
}
}
}
res = res + " key" + (strings.size() > 1 ? "s" : "");
}
return res;
}
//------------------------------------------------------------------------------
static std::string point_to_string(const QPoint& _p)
{
return std::string("(") + std::to_string(_p.x()) + ',' + std::to_string(_p.y()) + ')';
}
mouse_click::mouse_click(Qt::MouseButton _button, Qt::KeyboardModifiers _modifiers, const QPoint& _pos) :
m_button(_button),
m_modifiers(_modifiers),
m_pos(_pos)
{
}
//------------------------------------------------------------------------------
void mouse_click::interact_with(QWidget* _widget) const
{
interact_with<>(_widget);
}
//------------------------------------------------------------------------------
void mouse_click::interact_with(QWindow* _window) const
{
interact_with<>(_window);
}
//------------------------------------------------------------------------------
template<typename T>
void mouse_click::interact_with(T _thing) const
{
qApp->postEvent(
qApp,
new test_event(
[*this, _thing]
{
if(auto* push_button = qobject_cast<QAbstractButton*>(_thing);
m_modifiers == Qt::NoModifier && push_button != nullptr)
{
push_button->click();
}
else
{
QTest::mouseClick(_thing, m_button, m_modifiers, m_pos);
}
})
);
}
//------------------------------------------------------------------------------
std::string mouse_click::to_string() const
{
std::string res;
if(m_button == Qt::LeftButton)
{
res += "left click";
}
else if(m_button == Qt::RightButton)
{
res += "right click";
}
else if(m_button == Qt::MiddleButton)
{
res += "middle click";
}
else
{
res += "other click";
}
const std::string modifiers = modifiers_to_string(m_modifiers);
if(!modifiers.empty())
{
res += " while holding " + modifiers;
}
return res;
}
mouse_double_click::mouse_double_click(Qt::MouseButton _button, Qt::KeyboardModifiers _modifiers, const QPoint& _pos) :
m_button(_button),
m_modifiers(_modifiers),
m_pos(_pos)
{
}
//------------------------------------------------------------------------------
void mouse_double_click::interact_with(QWidget* _widget) const
{
interact_with<>(_widget);
}
//------------------------------------------------------------------------------
void mouse_double_click::interact_with(QWindow* _window) const
{
interact_with<>(_window);
}
//------------------------------------------------------------------------------
template<typename T>
void mouse_double_click::interact_with(T _thing) const
{
qApp->postEvent(
qApp,
new test_event(
[*this, _thing]
{
QTest::mouseDClick(_thing, m_button, m_modifiers, m_pos);
})
);
}
//------------------------------------------------------------------------------
std::string mouse_double_click::to_string() const
{
std::string res;
if(m_button == Qt::LeftButton)
{
res += "left double click";
}
else if(m_button == Qt::RightButton)
{
res += "right double click";
}
else if(m_button == Qt::MiddleButton)
{
res += "middle double click";
}
else
{
res += "other double click";
}
const std::string modifiers = modifiers_to_string(m_modifiers);
if(!modifiers.empty())
{
res += " while holding " + modifiers;
}
return res;
}
mouse_drag::mouse_drag(
const QPoint& _from,
const QPoint& _to,
Qt::MouseButton _button,
Qt::KeyboardModifiers _modifiers
) :
m_from(_from),
m_to(_to),
m_button(_button),
m_modifiers(_modifiers)
{
}
//------------------------------------------------------------------------------
void mouse_drag::interact_with(QWidget* _widget) const
{
interact_with<>(_widget);
}
//------------------------------------------------------------------------------
void mouse_drag::interact_with(QWindow* _window) const
{
interact_with<>(_window);
}
//------------------------------------------------------------------------------
template<typename T>
void mouse_drag::interact_with(T _thing) const
{
qApp->postEvent(
qApp,
new test_event(
[*this, _thing]
{
tester::mouse_move(_thing, m_from);
QTest::mousePress(_thing, m_button, m_modifiers, m_from);
tester::mouse_move(_thing, m_to, -1, m_button, m_modifiers);
QTest::mouseRelease(_thing, m_button, m_modifiers, m_to);
})
);
}
//------------------------------------------------------------------------------
std::string mouse_drag::to_string() const
{
std::string res;
res = res + "drag mouse from " + point_to_string(m_from) + " to " + point_to_string(m_to) + " while holding ";
if(m_button == Qt::LeftButton)
{
res += "left";
}
else if(m_button == Qt::RightButton)
{
res += "right";
}
else if(m_button == Qt::MiddleButton)
{
res += "middle";
}
else
{
res += "other";
}
res += " button";
const std::string modifiers = modifiers_to_string(m_modifiers);
if(!modifiers.empty())
{
res += " and " + modifiers;
}
return res;
}
mouse_wheel::mouse_wheel(const QPoint& _angle_delta, Qt::KeyboardModifiers _modifiers, const QPoint& _position) :
m_angle_delta(_angle_delta),
m_modifiers(_modifiers),
m_position(_position)
{
}
//------------------------------------------------------------------------------
void mouse_wheel::interact_with(QWidget* _widget) const
{
interact_with<>(_widget);
}
//------------------------------------------------------------------------------
void mouse_wheel::interact_with(QWindow* _window) const
{
interact_with<>(_window);
}
//------------------------------------------------------------------------------
template<typename T>
void mouse_wheel::interact_with(T _thing) const
{
QPoint position = m_position;
if(position.isNull())
{
position = QRect(0, 0, _thing->width(), _thing->height()).center();
}
auto* event = new QWheelEvent(
position,
_thing->mapToGlobal(position),
QPoint(),
m_angle_delta,
Qt::NoButton,
m_modifiers,
Qt::ScrollUpdate,
false
);
qApp->postEvent(_thing, event);
}
//------------------------------------------------------------------------------
std::string mouse_wheel::to_string() const
{
std::string res = "rotate the mouse wheel of " + point_to_string(m_angle_delta) + " units";
std::string modifiers = modifiers_to_string(m_modifiers);
if(!modifiers.empty())
{
res += " while holding " + modifiers;
}
return res;
}
keyboard_sequence::keyboard_sequence(std::string _text, Qt::KeyboardModifiers _modifiers) :
m_text(std::move(_text)),
m_modifiers(_modifiers)
{
}
//------------------------------------------------------------------------------
void keyboard_sequence::interact_with(QWidget* _widget) const
{
interact_with<>(_widget);
}
//------------------------------------------------------------------------------
void keyboard_sequence::interact_with(QWindow* _window) const
{
interact_with<>(_window);
}
//------------------------------------------------------------------------------
template<typename T>
void keyboard_sequence::interact_with(T _thing) const
{
qApp->postEvent(
qApp,
new test_event(
[*this, _thing]
{
for(char c : m_text)
{
QTest::keyClick(_thing, c, m_modifiers);
}
})
);
}
//------------------------------------------------------------------------------
std::string keyboard_sequence::to_string() const
{
std::string res = "type '" + m_text + "'";
const std::string modifiers = modifiers_to_string(m_modifiers);
if(!modifiers.empty())
{
res += " while holding " + modifiers;
}
return res;
}
keyboard_click::keyboard_click(Qt::Key _key, Qt::KeyboardModifiers _modifiers) :
m_key(_key),
m_modifiers(_modifiers)
{
}
//------------------------------------------------------------------------------
void keyboard_click::interact_with(QWidget* _widget) const
{
interact_with<>(_widget);
}
//------------------------------------------------------------------------------
void keyboard_click::interact_with(QWindow* _window) const
{
interact_with<>(_window);
}
//------------------------------------------------------------------------------
template<typename T>
void keyboard_click::interact_with(T _thing) const
{
qApp->postEvent(
qApp,
new test_event(
[*this, _thing]
{
QTest::keyClick(_thing, m_key, m_modifiers);
})
);
}
//------------------------------------------------------------------------------
std::string keyboard_click::to_string() const
{
std::string enum_name = QMetaEnum::fromType<Qt::Key>().valueToKey(m_key);
enum_name = enum_name.substr(enum_name.find('_') + 1);
std::string res = "press " + enum_name + " key";
const std::string modifiers = modifiers_to_string(m_modifiers);
if(!modifiers.empty())
{
res += " while holding " + modifiers;
}
return res;
}
pinch_gesture::pinch_gesture(
std::pair<QPoint, QPoint> _first_finger_pos,
std::pair<QPoint, QPoint> _second_finger_pos
) :
m_first_finger_pos(std::move(_first_finger_pos)),
m_second_finger_pos(std::move(_second_finger_pos))
{
SIGHT_ASSERT(
"Two fingers can't be at the same place at the same time",
m_first_finger_pos.first != m_second_finger_pos.first && m_first_finger_pos.second != m_second_finger_pos.second
);
}
//------------------------------------------------------------------------------
void pinch_gesture::interact_with(QWidget* _widget) const
{
interact_with<>(_widget);
}
//------------------------------------------------------------------------------
void pinch_gesture::interact_with(QWindow* _window) const
{
interact_with<>(_window);
}
//------------------------------------------------------------------------------
template<typename T>
void pinch_gesture::interact_with(T _thing) const
{
static constexpr T s_NULL = nullptr; // Required to avoid ambiguous overload compiler error
QTest::touchEvent(_thing, tester::get_dummy_touch_screen())
.press(0, m_first_finger_pos.first, s_NULL)
.press(1, m_second_finger_pos.first, s_NULL);
QTest::touchEvent(_thing, tester::get_dummy_touch_screen())
.move(0, m_first_finger_pos.first, s_NULL)
.move(1, m_second_finger_pos.first, s_NULL);
// If the two fingers are too far in one go, Qt will ignore it as it will consider them as spurious. We must
// therefore divide the moves in multiple steps.
QLineF first_finger_line(m_first_finger_pos.first, m_first_finger_pos.second);
QLineF second_finger_line(m_second_finger_pos.first, m_second_finger_pos.second);
for(int i = 0 ; i < 100 ; i++)
{
QTest::touchEvent(_thing, tester::get_dummy_touch_screen())
.move(0, first_finger_line.pointAt(i / 100.).toPoint(), s_NULL)
.move(1, second_finger_line.pointAt(i / 100.).toPoint(), s_NULL);
}
QTest::touchEvent(_thing, tester::get_dummy_touch_screen())
.move(0, m_first_finger_pos.second, s_NULL)
.move(1, m_second_finger_pos.second, s_NULL);
QTest::touchEvent(_thing, tester::get_dummy_touch_screen())
.release(0, m_first_finger_pos.second, s_NULL)
.release(1, m_second_finger_pos.second, s_NULL);
}
//------------------------------------------------------------------------------
std::string pinch_gesture::to_string() const
{
return "pinch gesture with first finger going from " + point_to_string(m_first_finger_pos.first) + " to "
+ point_to_string(m_first_finger_pos.second) + " and second finger going from " + point_to_string(
m_second_finger_pos.first
) + " to " + point_to_string(m_second_finger_pos.second);
}
} // namespace sight::ui::test
|