diff --git a/demos/qt5/foo.cpp b/demos/qt5/foo.cpp index ab5620d6..8366ec0a 100644 --- a/demos/qt5/foo.cpp +++ b/demos/qt5/foo.cpp @@ -3,7 +3,24 @@ #include "foo.h" Foo::Foo() : QWidget(NULL) { + m_button = new QPushButton("Foo Button", this); + m_button->setGeometry(QRect(QPoint(50, 60), + QSize(120, 50))); + connect(m_button, SIGNAL (released()), this, SLOT (handleButton())); + myToggle = true; +} +void Foo::handleButton() { + if (myToggle) { + m_button->setText("Button Foo"); + } else { + m_button->setText("Foo Button"); + } + myToggle = !myToggle; +} + +int Foo::FortyTwo() { + return 42; } class Bar_private : public QWidget { diff --git a/demos/qt5/foo.h b/demos/qt5/foo.h index e05b3e99..50820d65 100644 --- a/demos/qt5/foo.h +++ b/demos/qt5/foo.h @@ -4,13 +4,20 @@ #define _FOO #include +#include class Foo : public QWidget { Q_OBJECT signals: void test(); + private slots: + void handleButton(); public: Foo(); + int FortyTwo(); + QPushButton *m_button; + public: + bool myToggle; }; #endif diff --git a/demos/qt5/main.cpp b/demos/qt5/main.cpp index 4f407c6c..966a1af8 100644 --- a/demos/qt5/main.cpp +++ b/demos/qt5/main.cpp @@ -4,12 +4,13 @@ //#include //#include "mainwindow.h" #include "ui_but.h" +#include "foo.h" int main(int argc, char **argv) { Q_INIT_RESOURCE(res); QApplication app(argc, argv); - QWidget window; + Foo window; Ui::Form ui; ui.setupUi(&window); window.show(); diff --git a/demos/qt5/testqt5.cpp b/demos/qt5/testqt5.cpp new file mode 100644 index 00000000..935539b2 --- /dev/null +++ b/demos/qt5/testqt5.cpp @@ -0,0 +1,31 @@ +// Example of Qt5 Unit test with QtTest library +// Federico Pellegrin, 2019 (fedepell) + +#include "foo.h" +#include + +class TestQt5Test: public QObject { + Q_OBJECT + private: + Foo myFoo; + private slots: + void testGui(); + void testFunc(); +}; + +// Test of the UI by simulating a button click and button label reading +void TestQt5Test::testGui() { + QCOMPARE(myFoo.m_button->text(), QString("Foo Button")); + QTest::mouseClick(myFoo.m_button,Qt::LeftButton,Qt::NoModifier,QPoint(5,5),0); + QCOMPARE(myFoo.m_button->text(), QString("Button Foo")); +} + +// Test of a normal function +void TestQt5Test::testFunc() { + QCOMPARE(myFoo.FortyTwo(), 44); // this fails! 42 != 44 +} + +QTEST_MAIN(TestQt5Test) + +#include "testqt5.moc" + diff --git a/demos/qt5/wscript b/demos/qt5/wscript index 5ab374c9..73eb0521 100644 --- a/demos/qt5/wscript +++ b/demos/qt5/wscript @@ -1,6 +1,7 @@ #! /usr/bin/env python # encoding: utf-8 # Thomas Nagy, 2016 (ita) +# Federico Pellegrin, 2019 (fedepell) VERSION='0.0.1' APPNAME='qt5_test' @@ -9,10 +10,10 @@ top = '.' out = 'build' def options(opt): - opt.load('compiler_cxx qt5') + opt.load('compiler_cxx qt5 waf_unit_test') def configure(conf): - conf.load('compiler_cxx qt5') + conf.load('compiler_cxx qt5 waf_unit_test') #conf.env.append_value('CXXFLAGS', ['-g']) # test def build(bld): @@ -32,3 +33,25 @@ def build(bld): langname = 'somefile', # include the .qm files from somefile.qrc ) + # Example of integration of Qt5 Unit tests using Qt5Test using waf_unit_test + bld( + features = 'qt5 cxx cxxprogram test', + use = 'QT5CORE QT5WIDGETS QT5TEST', + source = 'foo.cpp testqt5.cpp', + moc = 'foo.h', + target = 'footest', + includes = '.', + # ut_str = './${SRC} -o test-report.xml,xunitxml', # put output to a xunit xml + ) + + bld.add_post_fun(print_test_results) # print output of test runner to user + + +def print_test_results(bld): + lst = getattr(bld, 'utest_results', []) + if not lst: + return + for (f, code, out, err) in lst: + print(out.decode('utf-8')) + print(err.decode('utf-8')) +