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]
35char *toString(const MyPoint &point) // should be inside the same namespace as MyPoint
36{
37 return QTest::toString("MyPoint(" +
38 QByteArray::number(point.x()) + ", " +
39 QByteArray::number(point.y()) + ')');
40}
41//! [toString-overload]
42
44{
45//! [25]
46QTouchDevice *dev = QTest::createTouchDevice();
47QWidget widget;
48
49QTest::touchEvent(&widget, dev)
50 .press(0, QPoint(10, 10));
51QTest::touchEvent(&widget, dev)
52 .stationary(0)
53 .press(1, QPoint(40, 10));
54QTest::touchEvent(&widget, dev)
55 .move(0, QPoint(12, 12))
56 .move(1, QPoint(45, 5));
57QTest::touchEvent(&widget, dev)
58 .release(0, QPoint(12, 12))
59 .release(1, QPoint(45, 5));
60//! [25]
61}
62
63//! [26]
64bool tst_MyXmlParser::parse()
65{
66 MyXmlParser parser;
67 QString input = QFINDTESTDATA("testxml/simple1.xml");
68 QVERIFY(parser.parse(input));
69}
70//! [26]
71
72//! [28]
74QTest::keyClick(&myWindow, Qt::Key_Tab);
75//! [28]
76
77//! [29]
78QTest::keyClick(&myWindow, Qt::Key_Escape);
79QTest::keyClick(&myWindow, Qt::Key_Escape, Qt::ShiftModifier, 200);
80//! [29]
81
82//! [30]
83void TestQLocale::initTestCase_data()
84{
85 QTest::addColumn<QLocale>("locale");
86 QTest::newRow("C") << QLocale::c();
87 QTest::newRow("UKish") << QLocale("en_GB");
88 QTest::newRow("USAish") << QLocale(QLocale::English, QLocale::UnitedStates);
89}
90
91void TestQLocale::roundTripInt_data()
92{
93 QTest::addColumn<int>("number");
94 QTest::newRow("zero") << 0;
95 QTest::newRow("one") << 1;
96 QTest::newRow("two") << 2;
97 QTest::newRow("ten") << 10;
98}
99//! [30]
100
101//! [31]
102void TestQLocale::roundTripInt()
103{
104 QFETCH_GLOBAL(QLocale, locale);
105 QFETCH(int, number);
106 bool ok;
107 QCOMPARE(locale.toInt(locale.toString(number), &ok), number);
108 QVERIFY(ok);
109}
110//! [31]
111
112//! [34]
113char *toString(const MyType &t)
114{
115 char *repr = new char[t.reprSize()];
116 t.writeRepr(repr);
117 return repr;
118}
119//! [34]
120
121//! [35]
122QSignalSpy doubleClickSpy(target, &TargetClass::doubleClicked);
123const QPoint p(1, 2);
124QTest::mousePress(&myWindow, Qt::LeftButton, Qt::NoModifier, p);
125QVERIFY(target.isPressed());
126QTest::mouseRelease(&myWindow, Qt::LeftButton, Qt::NoModifier, p, 10);
127QCOMPARE(target.isPressed(), false);
128QTest::mousePress(&myWindow, Qt::LeftButton, Qt::NoModifier, p, 10);
129QCOMPARE(target.pressCount(), 2);
130QTest::mouseRelease(&myWindow, Qt::LeftButton, Qt::NoModifier, p, 10);
132//! [35]
QCOMPARE(spy.count(), 1)
char * toString(const MyPoint &point)
QSignalSpy doubleClickSpy(target, &TargetClass::doubleClicked)
[34]
void processTouchEvent()
[toString-overload]
QWidget myWindow
[26]
char * toString(const MyPoint &point)
[16]
const QPoint p(1, 2)
QVERIFY(db.isValid())