ctkTestApplication.h 4.8 KB

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