ctkTestApplication.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. /*=========================================================================
  15. Program: Visualization Toolkit
  16. Module: $RCSfile: QTestApp.h,v $
  17. Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  18. All rights reserved.
  19. See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
  20. This software is distributed WITHOUT ANY WARRANTY; without even
  21. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  22. PURPOSE. See the above copyright notice for more information.
  23. =========================================================================*/
  24. /*-------------------------------------------------------------------------
  25. Copyright 2008 Sandia Corporation.
  26. Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
  27. the U.S. Government retains certain rights in this software.
  28. -------------------------------------------------------------------------*/
  29. #ifndef __ctkTestApplication_h
  30. #define __ctkTestApplication_h
  31. // Qt includes
  32. #include <QApplication>
  33. #include <QVector>
  34. #include <QByteArray>
  35. #include <QTimer>
  36. /// Helper macro allowing to declare a test
  37. #define QCTK_DECLARE_TEST(TEST_NAME) \
  38. namespace \
  39. { \
  40. class _TEST_NAME : public ctkTestApplication \
  41. { \
  42. public: \
  43. _TEST_NAME(int _argc, char * _argv []): \
  44. ctkTestApplication(_argc, _argv){} \
  45. virtual void runTest(); \
  46. }; \
  47. \
  48. void _TEST_NAME::runTest() \
  49. /// Helper macro allowing to define a test
  50. #define QCTK_RUN_TEST(TEST_NAME) \
  51. } \
  52. \
  53. int TEST_NAME(int _argc, char * _argv [] ) \
  54. { \
  55. _TEST_NAME app(_argc, _argv); \
  56. QTimer::singleShot(0, &app, SLOT(runTestSlot())); \
  57. return _TEST_NAME::exec(); \
  58. }
  59. /// Helper macro allowing to exit the event loop specifying a return code
  60. #define QCTK_EXIT_TEST(_status) \
  61. QCoreApplication::exit(_status); \
  62. return;
  63. #include "ctkWidgetsExport.h"
  64. /// \ingroup Widgets
  65. class CTK_WIDGETS_EXPORT ctkTestApplication : public QObject
  66. {
  67. Q_OBJECT
  68. public:
  69. ctkTestApplication(int _argc, char** _argv);
  70. ~ctkTestApplication();
  71. /// This function could be overloaded to implement test that required
  72. /// an active event loop
  73. virtual void runTest();
  74. ///
  75. /// If reportErrorsOnExit is true, then the return value will
  76. /// be the number of warning messages plus the number of error messages
  77. /// produced by QDebug during execution.
  78. static int exec(bool reportErrorsOnExit=false);
  79. #if QT_VERSION >= 0x50000
  80. static void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg);
  81. #else
  82. static void messageHandler(QtMsgType type, const char *msg);
  83. #endif
  84. static void delay(int ms);
  85. static bool simulateEvent(QWidget* w, QEvent* e);
  86. static void keyUp(QWidget* w, Qt::Key key, Qt::KeyboardModifiers mod, int ms);
  87. static void keyDown(QWidget* w, Qt::Key key, Qt::KeyboardModifiers mod, int ms);
  88. static void keyClick(QWidget* w, Qt::Key key, Qt::KeyboardModifiers mod, int ms);
  89. static void mouseDown(QWidget* w, QPoint pos, Qt::MouseButton btn,
  90. Qt::KeyboardModifiers mod, int ms);
  91. static void mouseUp(QWidget* w, QPoint pos, Qt::MouseButton btn,
  92. Qt::KeyboardModifiers mod, int ms);
  93. static void mouseMove(QWidget* w, QPoint pos, Qt::MouseButton btn,
  94. Qt::KeyboardModifiers mod, int ms);
  95. static void mouseClick(QWidget* w, QPoint pos, Qt::MouseButton btn,
  96. Qt::KeyboardModifiers mod, int ms);
  97. static void mouseDClick(QWidget* w, QPoint pos, Qt::MouseButton btn,
  98. Qt::KeyboardModifiers mod, int ms);
  99. public Q_SLOTS:
  100. /// Slot responsible to invoke the virtual function 'runTest'.
  101. /// The typical use case consists in calling that slot using a singleShot QTimer
  102. void runTestSlot();
  103. private:
  104. QApplication* App;
  105. static int Error;
  106. QList<QByteArray> Argv;
  107. QVector<char*> Argvp;
  108. int Argc;
  109. };
  110. #endif