1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qtestlib.qdoc -->
<title>Qt 4.8: Chapter 3: Simulating GUI Events</title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="content">
<a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
</div>
<div class="breadcrumb toolblock">
<ul>
<li class="first"><a href="index.html">Home</a></li>
<!-- Breadcrumbs go here -->
<li><a href="all-examples.html">Examples</a></li>
<li>Chapter 3: Simulating GUI Events</li>
</ul>
</div>
</div>
<div class="content mainContent">
<link rel="prev" href="qtestlib-tutorial2.html" />
<link rel="next" href="qtestlib-tutorial4.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="qtestlib-tutorial2.html">Chapter 2</a>
<a class="nextPage" href="qtestlib-tutorial4.html">Chapter 4</a>
</p><p/>
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#writing-a-gui-test">Writing a GUI test</a></li>
</ul>
</div>
<h1 class="title">Chapter 3: Simulating GUI Events</h1>
<span class="subtitle"></span>
<!-- $$$qtestlib/tutorial3-description -->
<div class="descr"> <a name="details"></a>
<p>Files:</p>
<ul>
<li><a href="qtestlib-tutorial3-testgui-cpp.html">qtestlib/tutorial3/testgui.cpp</a></li>
<li><a href="qtestlib-tutorial3-tutorial3-pro.html">qtestlib/tutorial3/tutorial3.pro</a></li>
</ul>
<p><a href="qtestlib-manual.html#qtestlib">QTestLib</a> features some mechanisms to test graphical user interfaces. Instead of simulating native window system events, <a href="qtestlib-manual.html#qtestlib">QTestLib</a> sends internal Qt events. That means there are no side-effects on the machine the tests are running on.</p>
<p>In this chapter we will se how to write a simple GUI test.</p>
<a name="writing-a-gui-test"></a>
<h2>Writing a GUI test</h2>
<p>This time, let's assume you want to test the behavior of our <a href="qlineedit.html">QLineEdit</a> class. As before, you will need a class that contains your test function:</p>
<pre class="cpp"> <span class="preprocessor">#include <QtGui></span>
<span class="preprocessor">#include <QtTest/QtTest></span>
<span class="keyword">class</span> TestGui: <span class="keyword">public</span> <span class="type"><a href="qobject.html">QObject</a></span>
{
Q_OBJECT
<span class="keyword">private</span> <span class="keyword">slots</span>:
<span class="type">void</span> testGui();
};</pre>
<p>The only difference is that you need to include the <a href="qtgui.html">QtGui</a> class definitions in addition to the <a href="qtest.html">QTest</a> namespace.</p>
<pre class="cpp"> <span class="type">void</span> TestGui<span class="operator">::</span>testGui()
{
<span class="type"><a href="qlineedit.html">QLineEdit</a></span> lineEdit;
<span class="type"><a href="qtest.html">QTest</a></span><span class="operator">::</span>keyClicks(<span class="operator">&</span>lineEdit<span class="operator">,</span> <span class="string">"hello world"</span>);
QCOMPARE(lineEdit<span class="operator">.</span>text()<span class="operator">,</span> <span class="type"><a href="qstring.html">QString</a></span>(<span class="string">"hello world"</span>));
}</pre>
<p>In the implementation of the test function we first create a <a href="qlineedit.html">QLineEdit</a>. Then we simulate writing "hello world" in the line edit using the <a href="qtest.html#keyClicks">QTest::keyClicks</a>() function.</p>
<p><b>Note:</b> The widget must also be shown in order to correctly test keyboard shortcuts.</p>
<p><a href="qtest.html#keyClicks">QTest::keyClicks</a>() 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 <a href="qtest.html#keyClick">QTest::keyClick</a>(), <a href="qtest.html#keyPress">QTest::keyPress</a>(), <a href="qtest.html#keyRelease">QTest::keyRelease</a>(), <a href="qtest.html#mouseClick">QTest::mouseClick</a>(), <a href="qtest.html#mouseDClick">QTest::mouseDClick</a>(), <a href="qtest.html#mouseMove">QTest::mouseMove</a>(), <a href="qtest.html#mousePress">QTest::mousePress</a>() and <a href="qtest.html#mouseRelease">QTest::mouseRelease</a>() functions to simulate the associated GUI events.</p>
<p>Finally, we use the <a href="qtest.html#QCOMPARE">QCOMPARE</a>() macro to check if the line edit's text is as expected.</p>
<p>As before, to make our test case a stand-alone executable, the following two lines are needed:</p>
<pre class="cpp"> <a href="qtest.html#QTEST_MAIN">QTEST_MAIN</a>(TestGui)
<span class="preprocessor">#include "testgui.moc"</span></pre>
<p>The <a href="qtest.html#QTEST_MAIN">QTEST_MAIN</a>() 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.</p>
</div>
<!-- @@@qtestlib/tutorial3 -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="qtestlib-tutorial2.html">Chapter 2</a>
<a class="nextPage" href="qtestlib-tutorial4.html">Chapter 4</a>
</p>
<div class="ft">
<span></span>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2012 Nokia Corporation and/or its
subsidiaries. Documentation contributions included herein are the copyrights of
their respective owners.</p>
<br />
<p>
The documentation provided herein is licensed under the terms of the
<a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
License version 1.3</a> as published by the Free Software Foundation.</p>
<p>
Documentation sources may be obtained from <a href="http://www.qt-project.org">
www.qt-project.org</a>.</p>
<br />
<p>
Nokia, Qt and their respective logos are trademarks of Nokia Corporation
in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. <a title="Privacy Policy"
href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
</div>
</body>
</html>
|