Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
src_qtestlib_qtestcase_snippet.cpp
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3//! [0]
4QVERIFY(spy.isValid());
5//! [0]
6
7//! [13]
8QTest::keyClick(myWidget, 'a');
9//! [13]
10
11//! [14]
12QTest::keyClick(myWidget, Qt::Key_Escape);
13
14QTest::keyClick(myWidget, Qt::Key_Escape, Qt::ShiftModifier, 200);
15//! [14]
16
17//! [15]
18QTest::keyClicks(myWidget, "hello world");
19//! [15]
20
21//! [16]
22namespace QTest {
23 template<>
24 char *toString(const MyPoint &point)
25 {
26 const QByteArray ba("MyPoint("
27 + QByteArray::number(point.x()) + ", "
28 + QByteArray::number(point.y()) + ')');
29 return qstrdup(ba.data());
30 }
31}
32//! [16]
33
34//! [toString-overload]
35namespace {
36 char *toString(const MyPoint &point)
37 {
38 return QTest::toString("MyPoint(" +
39 QByteArray::number(point.x()) + ", " +
40 QByteArray::number(point.y()) + ')');
41 }
42}
43//! [toString-overload]
44
46{
47//! [25]
48QTouchDevice *dev = QTest::createTouchDevice();
49QWidget widget;
50
51QTest::touchEvent(&widget, dev)
52 .press(0, QPoint(10, 10));
53QTest::touchEvent(&widget, dev)
54 .stationary(0)
55 .press(1, QPoint(40, 10));
56QTest::touchEvent(&widget, dev)
57 .move(0, QPoint(12, 12))
58 .move(1, QPoint(45, 5));
59QTest::touchEvent(&widget, dev)
60 .release(0, QPoint(12, 12))
61 .release(1, QPoint(45, 5));
62//! [25]
63}
64
65//! [26]
66bool tst_MyXmlParser::parse()
67{
68 MyXmlParser parser;
69 QString input = QFINDTESTDATA("testxml/simple1.xml");
70 QVERIFY(parser.parse(input));
71}
72//! [26]
73
74//! [28]
76QTest::keyClick(&myWindow, Qt::Key_Tab);
77//! [28]
78
79//! [29]
80QTest::keyClick(&myWindow, Qt::Key_Escape);
81QTest::keyClick(&myWindow, Qt::Key_Escape, Qt::ShiftModifier, 200);
82//! [29]
83
84//! [30]
85void TestQLocale::initTestCase_data()
86{
87 QTest::addColumn<QLocale>("locale");
88 QTest::newRow("C") << QLocale::c();
89 QTest::newRow("UKish") << QLocale("en_GB");
90 QTest::newRow("USAish") << QLocale(QLocale::English, QLocale::UnitedStates);
91}
92
93void TestQLocale::roundTripInt_data()
94{
95 QTest::addColumn<int>("number");
96 QTest::newRow("zero") << 0;
97 QTest::newRow("one") << 1;
98 QTest::newRow("two") << 2;
99 QTest::newRow("ten") << 10;
100}
101//! [30]
102
103//! [31]
104void TestQLocale::roundTripInt()
105{
106 QFETCH_GLOBAL(QLocale, locale);
107 QFETCH(int, number);
108 bool ok;
109 QCOMPARE(locale.toInt(locale.toString(number), &ok), number);
110 QVERIFY(ok);
111}
112//! [31]
113
114//! [34]
115char *toString(const MyType &t)
116{
117 char *repr = new char[t.reprSize()];
118 t.writeRepr(repr);
119 return repr;
120}
121//! [34]
122
123//! [35]
124QSignalSpy doubleClickSpy(target, &TargetClass::doubleClicked);
125const QPoint p(1, 2);
126QTest::mousePress(&myWindow, Qt::LeftButton, Qt::NoModifier, p);
127QVERIFY(target.isPressed());
128QTest::mouseRelease(&myWindow, Qt::LeftButton, Qt::NoModifier, p, 10);
129QCOMPARE(target.isPressed(), false);
130QTest::mousePress(&myWindow, Qt::LeftButton, Qt::NoModifier, p, 10);
131QCOMPARE(target.pressCount(), 2);
132QTest::mouseRelease(&myWindow, Qt::LeftButton, Qt::NoModifier, p, 10);
134//! [35]
QCOMPARE(spy.count(), 1)
char * toString(const MyPoint &point)
QSignalSpy doubleClickSpy(target, &TargetClass::doubleClicked)
[34]
void processTouchEvent()
[toString-overload]
char * toString(const MyType &t)
[31]
QWidget myWindow
[26]
const QPoint p(1, 2)
[0]
QVERIFY(db.isValid())