ctkTest.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. // STD includes
  17. #include <limits>
  18. #ifndef __ctkTest_h
  19. #define __ctkTest_h
  20. #define CTK_TEST_NOOP_MAIN(TestObject) \
  21. int TestObject(int argc, char *argv[]) \
  22. { \
  23. QObject tc; \
  24. return QTest::qExec(&tc, argc, argv); \
  25. }
  26. #if (QT_VERSION < 0x50000 && QT_GUI_LIB) || (QT_VERSION >= 0x50000 && QT_WIDGETS_LIB)
  27. //-----------------------------------------------------------------------------
  28. #define CTK_TEST_MAIN(TestObject) \
  29. int TestObject(int argc, char *argv[]) \
  30. { \
  31. QApplication app(argc, argv); \
  32. QTEST_DISABLE_KEYPAD_NAVIGATION \
  33. TestObject##er tc; \
  34. return QTest::qExec(&tc, argc, argv); \
  35. }
  36. #else
  37. //-----------------------------------------------------------------------------
  38. #define CTK_TEST_MAIN(TestObject) \
  39. int TestObject(int argc, char *argv[]) \
  40. { \
  41. QCoreApplication app(argc, argv); \
  42. TestObject##er tc; \
  43. return QTest::qExec(&tc, argc, argv); \
  44. }
  45. #endif // QT_GUI_LIB
  46. namespace ctkTest
  47. {
  48. #if (QT_VERSION < 0x50000 && QT_GUI_LIB) || (QT_VERSION >= 0x50000 && QT_WIDGETS_LIB)
  49. // ----------------------------------------------------------------------------
  50. static void mouseEvent(QTest::MouseAction action, QWidget *widget, Qt::MouseButton button,
  51. Qt::KeyboardModifiers stateKey, QPoint pos, int delay=-1)
  52. {
  53. if (action != QTest::MouseMove)
  54. {
  55. QTest::mouseEvent(action, widget, button, stateKey, pos, delay);
  56. return;
  57. }
  58. QTEST_ASSERT(widget);
  59. //extern int Q_TESTLIB_EXPORT defaultMouseDelay();
  60. //if (delay == -1 || delay < defaultMouseDelay())
  61. // delay = defaultMouseDelay();
  62. if (delay > 0)
  63. QTest::qWait(delay);
  64. if (pos.isNull())
  65. pos = widget->rect().center();
  66. QTEST_ASSERT(button == Qt::NoButton || button & Qt::MouseButtonMask);
  67. QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask);
  68. stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask);
  69. QMouseEvent me(QEvent::User, QPoint(), Qt::LeftButton, button, stateKey);
  70. me = QMouseEvent(QEvent::MouseMove, pos, widget->mapToGlobal(pos), button, button, stateKey);
  71. QSpontaneKeyEvent::setSpontaneous(&me);
  72. if (!qApp->notify(widget, &me))
  73. {
  74. static const char *mouseActionNames[] =
  75. { "MousePress", "MouseRelease", "MouseClick", "MouseDClick", "MouseMove" };
  76. QString warning = QString::fromLatin1("Mouse event \"%1\" not accepted by receiving widget");
  77. QTest::qWarn(qPrintable(warning.arg(mouseActionNames[static_cast<int>(action)])));
  78. }
  79. }
  80. #endif
  81. #if (QT_VERSION < 0x50000 && QT_GUI_LIB) || (QT_VERSION >= 0x50000 && QT_WIDGETS_LIB)
  82. // ----------------------------------------------------------------------------
  83. inline void mouseMove(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0,
  84. QPoint pos = QPoint(), int delay=-1)
  85. { ctkTest::mouseEvent(QTest::MouseMove, widget, button, stateKey, pos, delay); }
  86. #endif
  87. // ----------------------------------------------------------------------------
  88. inline void COMPARE(double v1, double v2)
  89. {
  90. // QCOMPARE fails to compare NaN numbers
  91. if (v2 != v2)
  92. {
  93. QVERIFY(v1 != v1);
  94. }
  95. // QCOMPARE fails to compare - infinity
  96. else if (v2 == -std::numeric_limits<double>::infinity())
  97. {
  98. QVERIFY(v1 == -std::numeric_limits<double>::infinity());
  99. }
  100. // QCOMPARE fails to compare infinity
  101. else if (v2 == std::numeric_limits<double>::infinity())
  102. {
  103. QVERIFY(v1 == std::numeric_limits<double>::infinity());
  104. }
  105. // QCOMPARE doesn't like to compare zeroes
  106. else
  107. {
  108. QCOMPARE(v1, v2);
  109. }
  110. }
  111. } // end ctkTest namespace
  112. #endif