ctkTest.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0.txt
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QtTest/QtTest>
  16. #define CTK_TEST_NOOP_MAIN(TestObject) \
  17. int TestObject(int argc, char *argv[]) \
  18. { \
  19. QObject tc; \
  20. return QTest::qExec(&tc, argc, argv); \
  21. }
  22. #ifdef QT_GUI_LIB
  23. //-----------------------------------------------------------------------------
  24. #define CTK_TEST_MAIN(TestObject) \
  25. int TestObject(int argc, char *argv[]) \
  26. { \
  27. QApplication app(argc, argv); \
  28. TestObject##er tc; \
  29. return QTest::qExec(&tc, argc, argv); \
  30. }
  31. #else
  32. //-----------------------------------------------------------------------------
  33. #define CTK_TEST_MAIN(TestObject) \
  34. int TestObject(int argc, char *argv[]) \
  35. { \
  36. QCoreApplication app(argc, argv); \
  37. QTEST_DISABLE_KEYPAD_NAVIGATION \
  38. TestObject##er tc; \
  39. return QTest::qExec(&tc, argc, argv); \
  40. }
  41. #endif // QT_GUI_LIB
  42. namespace ctkTest
  43. {
  44. static void mouseEvent(QTest::MouseAction action, QWidget *widget, Qt::MouseButton button,
  45. Qt::KeyboardModifiers stateKey, QPoint pos, int delay=-1)
  46. {
  47. if (action != QTest::MouseMove)
  48. {
  49. QTest::mouseEvent(action, widget, button, stateKey, pos, delay);
  50. return;
  51. }
  52. QTEST_ASSERT(widget);
  53. //extern int Q_TESTLIB_EXPORT defaultMouseDelay();
  54. //if (delay == -1 || delay < defaultMouseDelay())
  55. // delay = defaultMouseDelay();
  56. if (delay > 0)
  57. QTest::qWait(delay);
  58. if (pos.isNull())
  59. pos = widget->rect().center();
  60. QTEST_ASSERT(button == Qt::NoButton || button & Qt::MouseButtonMask);
  61. QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask);
  62. stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask);
  63. QMouseEvent me(QEvent::User, QPoint(), Qt::LeftButton, button, stateKey);
  64. me = QMouseEvent(QEvent::MouseMove, pos, widget->mapToGlobal(pos), button, button, stateKey);
  65. QSpontaneKeyEvent::setSpontaneous(&me);
  66. if (!qApp->notify(widget, &me))
  67. {
  68. static const char *mouseActionNames[] =
  69. { "MousePress", "MouseRelease", "MouseClick", "MouseDClick", "MouseMove" };
  70. QString warning = QString::fromLatin1("Mouse event \"%1\" not accepted by receiving widget");
  71. QTest::qWarn(warning.arg(QString::fromLatin1(mouseActionNames[static_cast<int>(action)])).toAscii().data());
  72. }
  73. }
  74. inline void mouseMove(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0,
  75. QPoint pos = QPoint(), int delay=-1)
  76. { ctkTest::mouseEvent(QTest::MouseMove, widget, button, stateKey, pos, delay); }
  77. }