Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-updates > by-pkgid > b796bb6846bef0871594624de2c980c0 > files > 2710

qtbase5-doc-5.12.6-4.mga7.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qttestlib-manual.qdoc -->
  <title>Chapter 4: Replaying GUI Events | Qt Test 5.12.6</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td >Qt 5.12</td><td ><a href="qttest-index.html">Qt Test</a></td><td >Chapter 4: Replaying GUI Events</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qttest-index.html">Qt 5.12.6 Reference Documentation</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
  <link rel="prev" href="qttestlib-tutorial3-example.html" />
  <link rel="next" href="qttestlib-tutorial5-example.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="qttestlib-tutorial3-example.html">Chapter 3</a>
<span class="naviSeparator">  &#9702;  </span>
<a class="nextPage" href="qttestlib-tutorial5-example.html">Chapter 5</a>
</p><p/>
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#writing-the-data-function">Writing the Data Function</a></li>
<li class="level1"><a href="#rewriting-the-test-function">Rewriting the Test Function</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Chapter 4: Replaying GUI Events</h1>
<span class="subtitle"></span>
<!-- $$$tutorial4-brief -->
<p>How to replay GUI events.</p>
<!-- @@@tutorial4 -->
<!-- $$$tutorial4-description -->
<div class="descr"> <a name="details"></a>
<p>In this 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.</p>
<p>The approach to storing a series of events and replaying them is quite similar to the approach explained in <a href="qttestlib-tutorial2-example.html">chapter 2</a>. All you need to do is to add a data function to your test class:</p>
<pre class="cpp">

  <span class="keyword">class</span> TestGui: <span class="keyword">public</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span>
  {
      Q_OBJECT

  <span class="keyword">private</span> <span class="keyword">slots</span>:
      <span class="type">void</span> testGui_data();
      <span class="type">void</span> testGui();
  };

</pre>
<a name="writing-the-data-function"></a>
<h2 id="writing-the-data-function">Writing the Data Function</h2>
<p>As before, a test function's associated data function carries the same name, appended by <code>_data</code>.</p>
<pre class="cpp">

  <span class="type">void</span> TestGui<span class="operator">::</span>testGui_data()
  {
      <span class="type"><a href="qtest.html">QTest</a></span><span class="operator">::</span>addColumn<span class="operator">&lt;</span><span class="type"><a href="qtesteventlist.html">QTestEventList</a></span><span class="operator">&gt;</span>(<span class="string">&quot;events&quot;</span>);
      <span class="type"><a href="qtest.html">QTest</a></span><span class="operator">::</span>addColumn<span class="operator">&lt;</span><span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">&gt;</span>(<span class="string">&quot;expected&quot;</span>);

      <span class="type"><a href="qtesteventlist.html">QTestEventList</a></span> list1;
      list1<span class="operator">.</span>addKeyClick(<span class="char">'a'</span>);
      <span class="type"><a href="qtest.html">QTest</a></span><span class="operator">::</span>newRow(<span class="string">&quot;char&quot;</span>) <span class="operator">&lt;</span><span class="operator">&lt;</span> list1 <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;a&quot;</span>;

      <span class="type"><a href="qtesteventlist.html">QTestEventList</a></span> list2;
      list2<span class="operator">.</span>addKeyClick(<span class="char">'a'</span>);
      list2<span class="operator">.</span>addKeyClick(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>Key_Backspace);
      <span class="type"><a href="qtest.html">QTest</a></span><span class="operator">::</span>newRow(<span class="string">&quot;there and back again&quot;</span>) <span class="operator">&lt;</span><span class="operator">&lt;</span> list2 <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;&quot;</span>;
  }

</pre>
<p>First, we define the elements of the table using the <a href="qtest.html#addColumn">QTest::addColumn</a>() function: A list of GUI events, and the expected result of applying the list of events on a <a href="../qtwidgets/qwidget.html">QWidget</a>. Note that the type of the first element is <a href="qtesteventlist.html">QTestEventList</a>.</p>
<p>A <a href="qtesteventlist.html">QTestEventList</a> can be populated with GUI events that can be stored as test data for later usage, or be replayed on any <a href="../qtwidgets/qwidget.html">QWidget</a>.</p>
<p>In our current data function, we create two <a href="qtesteventlist.html">QTestEventList</a> elements. The first list consists of a single click to the 'a' key. We add the event to the list using the <a href="qtesteventlist.html#addKeyClick">QTestEventList::addKeyClick</a>() function. Then we use the <a href="qtest.html#newRow">QTest::newRow</a>() function to give the data set a name, and stream the event list and the expected result into the table.</p>
<p>The second list consists of two key clicks: an 'a' with a following 'backspace'. Again we use the <a href="qtesteventlist.html#addKeyClick">QTestEventList::addKeyClick</a>() to add the events to the list, and <a href="qtest.html#newRow">QTest::newRow</a>() to put the event list and the expected result into the table with an associated name.</p>
<a name="rewriting-the-test-function"></a>
<h2 id="rewriting-the-test-function">Rewriting the Test Function</h2>
<p>Our test can now be rewritten:</p>
<pre class="cpp">

  <span class="type">void</span> TestGui<span class="operator">::</span>testGui()
  {
      QFETCH(<span class="type"><a href="qtesteventlist.html">QTestEventList</a></span><span class="operator">,</span> events);
      QFETCH(<span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">,</span> expected);

      <span class="type"><a href="../qtwidgets/qlineedit.html">QLineEdit</a></span> lineEdit;

      events<span class="operator">.</span>simulate(<span class="operator">&amp;</span>lineEdit);

      QCOMPARE(lineEdit<span class="operator">.</span>text()<span class="operator">,</span> expected);
  }

</pre>
<p>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.</p>
<p>First, we fetch the two elements of the data set using the <a href="qtest.html#QFETCH">QFETCH</a>() macro. <a href="qtest.html#QFETCH">QFETCH</a>() takes two arguments: the data type of the element and the element name. Then we create a <a href="../qtwidgets/qlineedit.html">QLineEdit</a>, and apply the list of events on that widget using the <a href="qtesteventlist.html#simulate">QTestEventList::simulate</a>() function.</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">

  QTEST_MAIN(TestGui)
  <span class="preprocessor">#include &quot;testgui.moc&quot;</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>
<p>Files:</p>
<ul>
<li><a href="qttestlib-tutorial4-testgui-cpp.html">tutorial4/testgui.cpp</a></li>
<li><a href="qttestlib-tutorial4-tutorial4-pro.html">tutorial4/tutorial4.pro</a></li>
</ul>
</div>
<!-- @@@tutorial4 -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="qttestlib-tutorial3-example.html">Chapter 3</a>
<span class="naviSeparator">  &#9702;  </span>
<a class="nextPage" href="qttestlib-tutorial5-example.html">Chapter 5</a>
</p>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    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.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>