Chapter 4: Replaying GUI EventsFiles: In this final chapter, we will show how to simulate a GUI event, and how to store a series of GUI events as well as replay them on a widget. The approach to storing a series of events and replay them, is quite similar to the approach explained in chapter 2; all you need is to add a data function to your test class: ** This file is part of the example classes of the Qt Toolkit. ** ** This file may be used under the terms of the GNU General Public ** License version 2.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 GNU ** General Public Licensing requirements will be met: ** http://www.trolltech.com/products/qt/opensource.html ** ** If you are unsure which license is appropriate for your use, please ** review the following information: ** http://www.trolltech.com/products/qt/licensing.html or contact the ** sales department at sales@trolltech.com. ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ****************************************************************************/ #include <QtGui> #include <QtTest/QtTest> class TestGui: public QObject { Q_OBJECT private slots: void testGui_data(); void testGui(); }; Writing the Data FunctionAs before, a test function's associated data function carries the same name, appended by _data. void TestGui::testGui_data() { QTest::addColumn<QTestEventList>("events"); QTest::addColumn<QString>("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 << ""; } First, we define the elements of the table using the QTest::addColumn() function: A list of GUI events, and the expected result of applying the list of events on a QWidget. Note that the type of the first element is QTestEventList. A QTestEventList can be populated with GUI events that can be stored as test data for later usage, or be replayed on any QWidget. In our current data function, we create two QTestEventLists. The first list consists of a single click to the 'a' key. We add the event to the list using the QTestEventList::addKeyClick() function. Then we use the QTest::newRow() function to give the data set a name, and stream the event list and the expected result into the table. The second list consists of two key clicks: an 'a' with a following 'backspace'. Again we use the QTestEventList::addKeyClick() to add the events to the list, and QTest::newRow() to put the event list and the expected result into the table with an associated name. Rewriting the Test FunctionOur test can now be rewritten: void TestGui::testGui() { QFETCH(QTestEventList, events); QFETCH(QString, expected); QLineEdit lineEdit; events.simulate(&lineEdit); QCOMPARE(lineEdit.text(), expected); } The TestGui::testGui() function will be executed two times, once for each entry in the test data that we created in the associated TestGui::testGui_data() function. First, we fetch the two elements of the data set using the QFETCH() macro. QFETCH() takes two arguments: The data type of the element and the element name. Then we create a QLineEdit, and apply the list of events on that widget using the QTestEventList::simulate() function. Finally, we use the QCOMPARE() macro to check if the line edit's text is as expected. As before, to make our test case a stand-alone executable, the following two lines are needed: QTEST_MAIN(TestGui) #include "testgui.moc" The QTEST_MAIN() macro expands to a simple main() method that runs all the test functions, and since both the declaration and the implementation of our test class are in a .cpp file, we also need to include the generated moc file to make Qt's introspection work. |
Publicité
Best OfActualités les plus luesSemaine
Mois
Année
Le blog Digia au hasardCréer des applications avec un style Metro avec Qt, exemples en QML et C++, un article de Digia Qt traduit par Thibaut CuvelierLe blog Digia est l'endroit privilégié pour la communication sur l'édition commerciale de Qt, où des réponses publiques sont apportées aux questions les plus posées au support. Lire l'article.
CommunautéRessources
Liens utilesContact
Qt dans le magazine |
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt 4.2 | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com