Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

Chapter 3: Simulating GUI Events

QTestLib features some mechanisms to test graphical user interfaces. Instead of simulating native window system events, QTestLib sends internal Qt events. That means there are no side-effects on the machine the tests are running on.

In this chapter we will se how to write a simple GUI test.

Writing a GUI test

This time, let's assume you want to test the behavior of our QLineEdit class. As before, you will need a class that contains your test function:

 #include <QtWidgets>
 #include <QtTest/QtTest>

 class TestGui: public QObject
 {
     Q_OBJECT

 private slots:
     void testGui();

 };

The only difference is that you need to include the QtGui class definitions in addition to the QTest namespace.

 void TestGui::testGui()
 {
     QLineEdit lineEdit;

     QTest::keyClicks(&lineEdit, "hello world");

     QCOMPARE(lineEdit.text(), QString("hello world"));
 }

In the implementation of the test function we first create a QLineEdit. Then we simulate writing "hello world" in the line edit using the QTest::keyClicks() function.

Note: The widget must also be shown in order to correctly test keyboard shortcuts.

QTest::keyClicks() simulates clicking a sequence of keys on a widget. Optionally, a keyboard modifier can be specified as well as a delay (in milliseconds) of the test after each key click. In a similar way, you can use the QTest::keyClick(), QTest::keyPress(), QTest::keyRelease(), QTest::mouseClick(), QTest::mouseDClick(), QTest::mouseMove(), QTest::mousePress() and QTest::mouseRelease() functions to simulate the associated GUI events.

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.

Files:

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 5.0-snapshot
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 !
 
 
 
 
Partenaires

Hébergement Web