Explorar o código

Add Testing utilities to do unit testing with QtTest framework

Within the CTest framework, QTEST_MAIN can't be used as it defines a
duplicated main() function. CTest requires the test function to have
the same name as the test file.
QTest::mouseMove() is not supporting moves whith a button down.
We add a utility function ctkTest::mouseMove() to support the feature.
It is surprising that such behavior doesn't exist by default in Qt. There
might be some associated issues with it.
Julien Finet %!s(int64=13) %!d(string=hai) anos
pai
achega
787c71b014

+ 10 - 0
Libs/Testing/CMake/ctkMacroGenerateMocs.cmake

@@ -0,0 +1,10 @@
+
+# QT4_GENERATE_MOCS(inputfiles ... )
+
+MACRO (QT4_GENERATE_MOCS)
+
+  FOREACH (fileIt ${ARGN})
+    QT4_GENERATE_MOC( ${fileIt} moc_${fileIt})
+  ENDFOREACH(it)
+
+ENDMACRO (QT4_GENERATE_MOCS)

+ 2 - 0
Libs/Testing/CMakeLists.txt

@@ -0,0 +1,2 @@
+# CMake Macros
+INCLUDE(CMake/ctkMacroGenerateMocs.cmake)

+ 98 - 0
Libs/Testing/ctkTest.h

@@ -0,0 +1,98 @@
+/*=========================================================================
+
+  Library:   CTK
+
+  Copyright (c) Kitware Inc.
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0.txt
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=========================================================================*/
+
+// Qt includes
+#include <QtTest/QtTest>
+
+#define CTK_TEST_NOOP_MAIN(TestObject) \
+int TestObject(int argc, char *argv[]) \
+{ \
+    QObject tc; \
+    return QTest::qExec(&tc, argc, argv); \
+}
+
+#ifdef QT_GUI_LIB
+
+//-----------------------------------------------------------------------------
+#define CTK_TEST_MAIN(TestObject) \
+  int TestObject(int argc, char *argv[]) \
+  { \
+    QApplication app(argc, argv); \
+    TestObject##er tc; \
+    return QTest::qExec(&tc, argc, argv); \
+  }
+
+#else
+
+//-----------------------------------------------------------------------------
+#define CTK_TEST_MAIN(TestObject) \
+  int TestObject(int argc, char *argv[]) \
+  { \
+    QCoreApplication app(argc, argv); \
+    QTEST_DISABLE_KEYPAD_NAVIGATION \
+    TestObject##er tc; \
+    return QTest::qExec(&tc, argc, argv); \
+  }
+
+#endif // QT_GUI_LIB
+
+namespace ctkTest
+{
+static void mouseEvent(QTest::MouseAction action, QWidget *widget, Qt::MouseButton button,
+                       Qt::KeyboardModifiers stateKey, QPoint pos, int delay=-1)
+{
+  if (action != QTest::MouseMove)
+    {
+    QTest::mouseEvent(action, widget, button, stateKey, pos, delay);
+    return;
+    }
+  QTEST_ASSERT(widget);
+  //extern int Q_TESTLIB_EXPORT defaultMouseDelay();
+  //if (delay == -1 || delay < defaultMouseDelay())
+  //    delay = defaultMouseDelay();
+  if (delay > 0)
+      QTest::qWait(delay);
+
+  if (pos.isNull())
+      pos = widget->rect().center();
+
+  QTEST_ASSERT(button == Qt::NoButton || button & Qt::MouseButtonMask);
+  QTEST_ASSERT(stateKey == 0 || stateKey & Qt::KeyboardModifierMask);
+
+  stateKey &= static_cast<unsigned int>(Qt::KeyboardModifierMask);
+
+  QMouseEvent me(QEvent::User, QPoint(), Qt::LeftButton, button, stateKey);
+
+  me = QMouseEvent(QEvent::MouseMove, pos, widget->mapToGlobal(pos), button, button, stateKey);
+  QSpontaneKeyEvent::setSpontaneous(&me);
+  if (!qApp->notify(widget, &me))
+    {
+    static const char *mouseActionNames[] =
+        { "MousePress", "MouseRelease", "MouseClick", "MouseDClick", "MouseMove" };
+    QString warning = QString::fromLatin1("Mouse event \"%1\" not accepted by receiving widget");
+    QTest::qWarn(warning.arg(QString::fromLatin1("MouseMove")).toAscii().data());
+    }
+}
+
+inline void mouseMove(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0,
+                      QPoint pos = QPoint(), int delay=-1)
+  { ctkTest::mouseEvent(QTest::MouseMove, widget, button, stateKey, pos, delay); }
+
+}