Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qttestlib-tutorial4.qdoc
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// Copyright (C) 2016 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
4
5/*!
6 \page qttestlib-tutorial4-example.html
7 \previouspage {Chapter 3: Simulating GUI Events}{Chapter 3}
8 \nextpage {Chapter 5: Writing a Benchmark}{Chapter 5}
9
10 \title Chapter 4: Replaying GUI Events
11 \brief How to replay GUI events.
12
13 In this chapter, we will show how to simulate a GUI event,
14 and how to store a series of GUI events as well as replay them on
15 a widget.
16
17 The approach to storing a series of events and replaying them is
18 quite similar to the approach explained in \l {Chapter 2:
19 Data Driven Testing}{chapter 2}. All you need to do is to add a data
20 function to your test class:
21
22 \snippet tutorial4/testgui.cpp 0
23
24 \section1 Writing the Data Function
25
26 As before, a test function's associated data function carries the
27 same name, appended by \c{_data}.
28
29 \snippet tutorial4/testgui.cpp 1
30
31 First, we define the elements of the table using the
32 QTest::addColumn() function: A list of GUI events, and the
33 expected result of applying the list of events on a QWidget. Note
34 that the type of the first element is \l QTestEventList.
35
36 A QTestEventList can be populated with GUI events that can be
37 stored as test data for later usage, or be replayed on any
38 QWidget.
39
40 In our current data function, we create two \l
41 {QTestEventList} elements. The first list consists of a single click to
42 the 'a' key. We add the event to the list using the
43 QTestEventList::addKeyClick() function. Then we use the
44 QTest::newRow() function to give the data set a name, and
45 stream the event list and the expected result into the table.
46
47 The second list consists of two key clicks: an 'a' with a
48 following 'backspace'. Again we use the
49 QTestEventList::addKeyClick() to add the events to the list, and
50 QTest::newRow() to put the event list and the expected
51 result into the table with an associated name.
52
53 \section1 Rewriting the Test Function
54
55 Our test can now be rewritten:
56
57 \snippet tutorial4/testgui.cpp 2
58
59 The TestGui::testGui() function will be executed two times,
60 once for each entry in the test data that we created in the
61 associated TestGui::testGui_data() function.
62
63 First, we fetch the two elements of the data set using the \l
64 QFETCH() macro. \l QFETCH() takes two arguments: the data type of
65 the element and the element name. Then we create a QLineEdit, and
66 apply the list of events on that widget using the
67 QTestEventList::simulate() function.
68
69 Finally, we use the QCOMPARE() macro to check if the line edit's
70 text is as expected.
71
72 \section1 Preparing the Stand-Alone Executable
73
74 As before, to make our test case a stand-alone executable,
75 the following two lines are needed:
76
77 \snippet tutorial4/testgui.cpp 3
78
79 The QTEST_MAIN() macro expands to a simple main() method that
80 runs all the test functions, and since both the declaration and
81 the implementation of our test class are in a .cpp file, we also
82 need to include the generated moc file to make Qt's introspection
83 work.
84
85 \section1 Building the Executable
86
87 \include {building-examples.qdocinc} {building the executable} {tutorial4}
88
89 \section1 Running the Executable
90
91 Running the resulting executable should give you the following
92 output:
93
94 \snippet code/doc_src_qtestlib.qdoc 13
95*/