Qt 5.9.1 参考手册 QtTest 第4章 回放GUI事件
2017-08-19 21:10
316 查看
Qt 5.9.1 Reference Documentation
Chapter 4: Replaying GUI Events
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.
The approach to storing a series of events and replaying them is quite similar to the approach explained in chapter 2. All you need to do is to add a data function to your test class:
在这一章节,我们将要展示怎样模拟一个GUI事件,怎样存储一系列GUI事件并在一个控件回放这些事件。
存储一系列事件并回放的实现方式和第2章节解释的方式相类似。你所需要做的仅仅是为测试类添加数据函数。
class TestGui: public QObject
{
Q_OBJECT
private slots:
void testGui_data();
void testGui();
};
Writing the Data Function
As before, a test function's associated data function carries the same name, appended by _data.
像前述章节提到的,与一个测试函数相关联的数据函数名为测试函数名加上"_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.
首先,我们用QTest::addColumn() 函数为我们的测试表声明2个元素,一系列GUI事件和这些事件发生在一个控件中所产生的预期结果。
注意:第一个元素的类型为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 QTestEventList elements. 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.
一个QTestEventList可以用GUI事件来填充,而GUI事件可以存储在测试数据中为后续使用,或者在任何控件上回放。
在我们目录的数据函数中,我们创建2个QTestEventList元素。第一个列表由单独的'a'键点击事件构成。我们用QTestEventList::addKeyClick() 函数把这个事件添加到列表中。然后,我们使用QTest::newRow()函数来给一行数据集命名,并以流的方式向表格中输入事件列表和预期的输出。
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.
第2个列表包含2个键点击事件,一个'a'键点击和一个'退格'点击。我们仍然使用 QTestEventList::addKeyClick()函数来添加事件到列表中,并用QTest::newRow() 将事件列表和预期的结果关联上一个名称并输入到表格中。
Rewriting the Test Function
Our 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.
TestGui::testGui() 函数将会执行2次,每次取一行数据函数中我们创建的表格中的记录。
首先,我们用QFETCH() 宏获取一行数据条目中的2个元素。QFETCH()宏有2个参数,元素的数据类型和元素名。然后我们创建一个QLineEdit,然后使用QTestEventList::simulate() 函数将列表中的事件一一应用到控件中。
Finally, we use the QCOMPARE() macro to check if the line edit's text is as expected.
然后我们用 QCOMPARE()宏来比较文本控件的文本是否是预期的那样。
As before, to make our test case a stand-alone executable, the following two lines are needed:
同样的,让我们的测试用例单独执行起来,我们需要添加以下2行代码:
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.
QTEST_MAIN() 简单展开为一个main函数执行所有测试函数,然后,由于所有的声明和实现均写在一个cpp文件中,我们需要包含.moc文件来让qt内建的机制工作。
Chapter 4: Replaying GUI Events
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.
The approach to storing a series of events and replaying them is quite similar to the approach explained in chapter 2. All you need to do is to add a data function to your test class:
在这一章节,我们将要展示怎样模拟一个GUI事件,怎样存储一系列GUI事件并在一个控件回放这些事件。
存储一系列事件并回放的实现方式和第2章节解释的方式相类似。你所需要做的仅仅是为测试类添加数据函数。
class TestGui: public QObject
{
Q_OBJECT
private slots:
void testGui_data();
void testGui();
};
Writing the Data Function
As before, a test function's associated data function carries the same name, appended by _data.
像前述章节提到的,与一个测试函数相关联的数据函数名为测试函数名加上"_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.
首先,我们用QTest::addColumn() 函数为我们的测试表声明2个元素,一系列GUI事件和这些事件发生在一个控件中所产生的预期结果。
注意:第一个元素的类型为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 QTestEventList elements. 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.
一个QTestEventList可以用GUI事件来填充,而GUI事件可以存储在测试数据中为后续使用,或者在任何控件上回放。
在我们目录的数据函数中,我们创建2个QTestEventList元素。第一个列表由单独的'a'键点击事件构成。我们用QTestEventList::addKeyClick() 函数把这个事件添加到列表中。然后,我们使用QTest::newRow()函数来给一行数据集命名,并以流的方式向表格中输入事件列表和预期的输出。
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.
第2个列表包含2个键点击事件,一个'a'键点击和一个'退格'点击。我们仍然使用 QTestEventList::addKeyClick()函数来添加事件到列表中,并用QTest::newRow() 将事件列表和预期的结果关联上一个名称并输入到表格中。
Rewriting the Test Function
Our 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.
TestGui::testGui() 函数将会执行2次,每次取一行数据函数中我们创建的表格中的记录。
首先,我们用QFETCH() 宏获取一行数据条目中的2个元素。QFETCH()宏有2个参数,元素的数据类型和元素名。然后我们创建一个QLineEdit,然后使用QTestEventList::simulate() 函数将列表中的事件一一应用到控件中。
Finally, we use the QCOMPARE() macro to check if the line edit's text is as expected.
然后我们用 QCOMPARE()宏来比较文本控件的文本是否是预期的那样。
As before, to make our test case a stand-alone executable, the following two lines are needed:
同样的,让我们的测试用例单独执行起来,我们需要添加以下2行代码:
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.
QTEST_MAIN() 简单展开为一个main函数执行所有测试函数,然后,由于所有的声明和实现均写在一个cpp文件中,我们需要包含.moc文件来让qt内建的机制工作。
相关文章推荐
- Qt 5.9.1 参考手册 QtTest 第3章 模拟GUI事件
- Qt 5.9.1 参考手册 QtTest 第5章 写一个基准线
- Qt 5.9.1 参考手册 QtTest 第1章 写一个单元测试
- Qt 5.9.1 参考手册 QtTest 第2章 数据驱动测试
- Tkinter 8.5 参考手册: a GUI for Python (五)11 窗口的名称
- JavaScript 事件参考手册
- jQuery 参考手册 - 事件
- jQuery 参考手册 - 事件
- jQuery 参考手册 - 事件
- JavaScript 事件参考手册
- jQuery 参考手册 - 事件
- Tkinter 8.5 参考手册: a GUI for Python(一)
- 【opencv学习之十一】opencv: HighGUI参考手册
- 【Qt开发】Qt gui添加右键菜单事件
- Tkinter 8.5 参考手册: a GUI for Python (二)
- 嵌入式Linux图形系统(GUI)快速参考手册
- Tkinter 8.5 参考手册: a GUI for Python (三)
- MySQL参考手册第4章:MySQL程序集
- Qt5.9.1 qmake 中文手册(二)
- jQuery 参考手册2-jQuery 事件方法