From e5fcad302d86d316390c6b0f62759a067313e8a9 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Mon, 23 Mar 2009 10:18:55 +0100 Subject: Long live Qt 4.5! --- examples/qtestlib/README | 38 ++++++++ examples/qtestlib/qtestlib.pro | 8 ++ examples/qtestlib/tutorial1/testqstring.cpp | 65 +++++++++++++ examples/qtestlib/tutorial1/tutorial1.pro | 8 ++ examples/qtestlib/tutorial2/testqstring.cpp | 81 ++++++++++++++++ examples/qtestlib/tutorial2/tutorial2.pro | 8 ++ examples/qtestlib/tutorial3/testgui.cpp | 71 ++++++++++++++ examples/qtestlib/tutorial3/tutorial3.pro | 8 ++ examples/qtestlib/tutorial4/testgui.cpp | 91 ++++++++++++++++++ examples/qtestlib/tutorial4/tutorial4.pro | 8 ++ examples/qtestlib/tutorial5/benchmarking.cpp | 135 +++++++++++++++++++++++++++ examples/qtestlib/tutorial5/tutorial5.pro | 8 ++ 12 files changed, 529 insertions(+) create mode 100644 examples/qtestlib/README create mode 100644 examples/qtestlib/qtestlib.pro create mode 100644 examples/qtestlib/tutorial1/testqstring.cpp create mode 100644 examples/qtestlib/tutorial1/tutorial1.pro create mode 100644 examples/qtestlib/tutorial2/testqstring.cpp create mode 100644 examples/qtestlib/tutorial2/tutorial2.pro create mode 100644 examples/qtestlib/tutorial3/testgui.cpp create mode 100644 examples/qtestlib/tutorial3/tutorial3.pro create mode 100644 examples/qtestlib/tutorial4/testgui.cpp create mode 100644 examples/qtestlib/tutorial4/tutorial4.pro create mode 100644 examples/qtestlib/tutorial5/benchmarking.cpp create mode 100644 examples/qtestlib/tutorial5/tutorial5.pro (limited to 'examples/qtestlib') diff --git a/examples/qtestlib/README b/examples/qtestlib/README new file mode 100644 index 0000000000..e89bf5d726 --- /dev/null +++ b/examples/qtestlib/README @@ -0,0 +1,38 @@ +The QTestLib framework is a tool for unit testing Qt based applications +and libraries. QTestLib provides all the functionality commonly found +in unit testing frameworks as well as extensions for testing graphical +user interfaces. + + +The examples in this directory should be run from the command line so +that output printed to the console can be examined. + +Documentation for these examples can be found via the "Tutorial and Examples" +link in the main Qt documentation. + +Finding the Qt Examples and Demos launcher +========================================== + +On Windows: + +The launcher can be accessed via the Windows Start menu. Select the menu +entry entitled "Qt Examples and Demos" entry in the submenu containing +the Qt tools. + +On Mac OS X: + +For the binary distribution, the qtdemo executable is installed in the +/Developer/Applications/Qt directory. For the source distribution, it is +installed alongside the other Qt tools on the path specified when Qt is +configured. + +On Unix/Linux: + +The qtdemo executable is installed alongside the other Qt tools on the path +specified when Qt is configured. + +On all platforms: + +The source code for the launcher can be found in the demos/qtdemo directory +in the Qt package. This example is built at the same time as the Qt libraries, +tools, examples, and demonstrations. diff --git a/examples/qtestlib/qtestlib.pro b/examples/qtestlib/qtestlib.pro new file mode 100644 index 0000000000..8addbcba63 --- /dev/null +++ b/examples/qtestlib/qtestlib.pro @@ -0,0 +1,8 @@ +TEMPLATE = subdirs +SUBDIRS = tutorial1 tutorial2 tutorial3 tutorial4 tutorial5 + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtestlib +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS qtestlib.pro README +sources.path = $$[QT_INSTALL_EXAMPLES]/qtestlib +INSTALLS += target sources diff --git a/examples/qtestlib/tutorial1/testqstring.cpp b/examples/qtestlib/tutorial1/testqstring.cpp new file mode 100644 index 0000000000..cc2e7caa1f --- /dev/null +++ b/examples/qtestlib/tutorial1/testqstring.cpp @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +#include + +class TestQString: public QObject +{ + Q_OBJECT +private slots: + void toUpper(); +}; +//! [0] + +//! [1] +void TestQString::toUpper() +{ + QString str = "Hello"; + QCOMPARE(str.toUpper(), QString("HELLO")); +} +//! [1] + +//! [2] +QTEST_MAIN(TestQString) +#include "testqstring.moc" +//! [2] + diff --git a/examples/qtestlib/tutorial1/tutorial1.pro b/examples/qtestlib/tutorial1/tutorial1.pro new file mode 100644 index 0000000000..baa15b29f6 --- /dev/null +++ b/examples/qtestlib/tutorial1/tutorial1.pro @@ -0,0 +1,8 @@ +SOURCES = testqstring.cpp +CONFIG += qtestlib + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtestlib/tutorial1 +sources.files = $$SOURCES *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtestlib/tutorial1 +INSTALLS += target sources diff --git a/examples/qtestlib/tutorial2/testqstring.cpp b/examples/qtestlib/tutorial2/testqstring.cpp new file mode 100644 index 0000000000..1c1f6f70e6 --- /dev/null +++ b/examples/qtestlib/tutorial2/testqstring.cpp @@ -0,0 +1,81 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include + +//! [0] +class TestQString: public QObject +{ + Q_OBJECT + +private slots: + void toUpper_data(); + void toUpper(); +}; +//! [0] + +//! [1] +void TestQString::toUpper_data() +{ + QTest::addColumn("string"); + QTest::addColumn("result"); + + QTest::newRow("all lower") << "hello" << "HELLO"; + QTest::newRow("mixed") << "Hello" << "HELLO"; + QTest::newRow("all upper") << "HELLO" << "HELLO"; +} +//! [1] + +//! [2] +void TestQString::toUpper() +{ + QFETCH(QString, string); + QFETCH(QString, result); + + QCOMPARE(string.toUpper(), result); +} +//! [2] + +//! [3] +QTEST_MAIN(TestQString) +#include "testqstring.moc" +//! [3] + diff --git a/examples/qtestlib/tutorial2/tutorial2.pro b/examples/qtestlib/tutorial2/tutorial2.pro new file mode 100644 index 0000000000..44211d8fc4 --- /dev/null +++ b/examples/qtestlib/tutorial2/tutorial2.pro @@ -0,0 +1,8 @@ +SOURCES = testqstring.cpp +CONFIG += qtestlib + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtestlib/tutorial2 +sources.files = $$SOURCES *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtestlib/tutorial2 +INSTALLS += target sources diff --git a/examples/qtestlib/tutorial3/testgui.cpp b/examples/qtestlib/tutorial3/testgui.cpp new file mode 100644 index 0000000000..8e116f8cdc --- /dev/null +++ b/examples/qtestlib/tutorial3/testgui.cpp @@ -0,0 +1,71 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +//! [0] +#include +#include + +class TestGui: public QObject +{ + Q_OBJECT + +private slots: + void testGui(); + +}; +//! [0] + +//! [1] +void TestGui::testGui() +{ + QLineEdit lineEdit; + + QTest::keyClicks(&lineEdit, "hello world"); + + QCOMPARE(lineEdit.text(), QString("hello world")); +} +//! [1] + +//! [2] +QTEST_MAIN(TestGui) +#include "testgui.moc" +//! [2] + diff --git a/examples/qtestlib/tutorial3/tutorial3.pro b/examples/qtestlib/tutorial3/tutorial3.pro new file mode 100644 index 0000000000..b494ba8c6a --- /dev/null +++ b/examples/qtestlib/tutorial3/tutorial3.pro @@ -0,0 +1,8 @@ +SOURCES = testgui.cpp +CONFIG += qtestlib + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtestlib/tutorial3 +sources.files = $$SOURCES *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtestlib/tutorial3 +INSTALLS += target sources diff --git a/examples/qtestlib/tutorial4/testgui.cpp b/examples/qtestlib/tutorial4/testgui.cpp new file mode 100644 index 0000000000..f392fec4c7 --- /dev/null +++ b/examples/qtestlib/tutorial4/testgui.cpp @@ -0,0 +1,91 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include + +//! [0] +class TestGui: public QObject +{ + Q_OBJECT + +private slots: + void testGui_data(); + void testGui(); +}; +//! [0] + +//! [1] +void TestGui::testGui_data() +{ + QTest::addColumn("events"); + QTest::addColumn("expected"); + + QTestEventList list1; + list1.addKeyClick('a'); + QTest::newRow("char") << list1 << "a"; + + QTestEventList list2; + list2.addKeyClick('a'); + list2.addKeyClick(Qt::Key_Backspace); + QTest::newRow("there and back again") << list2 << ""; +} +//! [1] + +//! [2] +void TestGui::testGui() +{ + QFETCH(QTestEventList, events); + QFETCH(QString, expected); + + QLineEdit lineEdit; + + events.simulate(&lineEdit); + + QCOMPARE(lineEdit.text(), expected); +} +//! [2] + +//! [3] +QTEST_MAIN(TestGui) +#include "testgui.moc" +//! [3] + diff --git a/examples/qtestlib/tutorial4/tutorial4.pro b/examples/qtestlib/tutorial4/tutorial4.pro new file mode 100644 index 0000000000..0777d4841f --- /dev/null +++ b/examples/qtestlib/tutorial4/tutorial4.pro @@ -0,0 +1,8 @@ +SOURCES = testgui.cpp +CONFIG += qtestlib + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtestlib/tutorial4 +sources.files = $$SOURCES *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtestlib/tutorial4 +INSTALLS += target sources diff --git a/examples/qtestlib/tutorial5/benchmarking.cpp b/examples/qtestlib/tutorial5/benchmarking.cpp new file mode 100644 index 0000000000..05ca266e55 --- /dev/null +++ b/examples/qtestlib/tutorial5/benchmarking.cpp @@ -0,0 +1,135 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include + +class TestBenchmark : public QObject +{ + Q_OBJECT +private slots: + void simple(); + void multiple_data(); + void multiple(); + void series_data(); + void series(); +}; + +//! [0] +void TestBenchmark::simple() +{ + QString str1 = QLatin1String("This is a test string"); + QString str2 = QLatin1String("This is a test string"); + + QCOMPARE(str1.localeAwareCompare(str2), 0); + + QBENCHMARK { + str1.localeAwareCompare(str2); + } +} +//! [0] + +//! [1] +void TestBenchmark::multiple_data() +{ + QTest::addColumn("useLocaleCompare"); + QTest::newRow("locale aware compare") << true; + QTest::newRow("standard compare") << false; +} +//! [1] + +//! [2] +void TestBenchmark::multiple() +{ + QFETCH(bool, useLocaleCompare); + QString str1 = QLatin1String("This is a test string"); + QString str2 = QLatin1String("This is a test string"); + + int result; + if (useLocaleCompare) { + QBENCHMARK { + result = str1.localeAwareCompare(str2); + } + } else { + QBENCHMARK { + result = (str1 == str2); + } + } +} +//! [2] + +//! [3] +void TestBenchmark::series_data() +{ + QTest::addColumn("useLocaleCompare"); + QTest::addColumn("stringSize"); + + for (int i = 1; i < 10000; i += 2000) { + QByteArray size = QByteArray::number(i); + QTest::newRow(("locale aware compare--" + size).constData()) << true << i; + QTest::newRow(("standard compare--" + size).constData()) << false << i; + } +} +//! [4] + +//! [5] +void TestBenchmark::series() +{ + QFETCH(bool, useLocaleCompare); + QFETCH(int, stringSize); + + QString str1 = QString().fill('A', stringSize); + QString str2 = QString().fill('A', stringSize); + int result; + if (useLocaleCompare) { + QBENCHMARK { + result = str1.localeAwareCompare(str2); + } + } else { + QBENCHMARK { + result = (str1 == str2); + } + } +} +//! [5] + +QTEST_MAIN(TestBenchmark) +#include "benchmarking.moc" diff --git a/examples/qtestlib/tutorial5/tutorial5.pro b/examples/qtestlib/tutorial5/tutorial5.pro new file mode 100644 index 0000000000..0310916ebd --- /dev/null +++ b/examples/qtestlib/tutorial5/tutorial5.pro @@ -0,0 +1,8 @@ +SOURCES = benchmarking.cpp +CONFIG += qtestlib + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/qtestlib/tutorial5 +sources.files = $$SOURCES *.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/qtestlib/tutorial5 +INSTALLS += target sources -- cgit v1.2.3