summaryrefslogtreecommitdiffstats
path: root/src/testlib/doc/snippets/code/src_qtestlib_qtestcase_snippet.cpp
blob: aa24746522a9518fcc95e888ae81762befc08430 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
//! [0]
QVERIFY(spy.isValid());
//! [0]

//! [13]
QTest::keyClick(myWidget, 'a');
//! [13]

//! [14]
QTest::keyClick(myWidget, Qt::Key_Escape);

QTest::keyClick(myWidget, Qt::Key_Escape, Qt::ShiftModifier, 200);
//! [14]

//! [15]
QTest::keyClicks(myWidget, "hello world");
//! [15]

//! [16]
namespace QTest {
    template<>
    char *toString(const MyPoint &point)
    {
        const QByteArray ba("MyPoint("
                            + QByteArray::number(point.x()) + ", "
                            + QByteArray::number(point.y()) + ')');
        return qstrdup(ba.data());
    }
}
//! [16]

//! [toString-overload]
namespace {
    char *toString(const MyPoint &point)
    {
        return QTest::toString("MyPoint(" +
                               QByteArray::number(point.x()) + ", " +
                               QByteArray::number(point.y()) + ')');
    }
}
//! [toString-overload]

void processTouchEvent()
{
//! [25]
QTouchDevice *dev = QTest::createTouchDevice();
QWidget widget;

QTest::touchEvent(&widget, dev)
    .press(0, QPoint(10, 10));
QTest::touchEvent(&widget, dev)
    .stationary(0)
    .press(1, QPoint(40, 10));
QTest::touchEvent(&widget, dev)
    .move(0, QPoint(12, 12))
    .move(1, QPoint(45, 5));
QTest::touchEvent(&widget, dev)
    .release(0, QPoint(12, 12))
    .release(1, QPoint(45, 5));
//! [25]
}

//! [26]
bool tst_MyXmlParser::parse()
{
    MyXmlParser parser;
    QString input = QFINDTESTDATA("testxml/simple1.xml");
    QVERIFY(parser.parse(input));
}
//! [26]

//! [28]
QWidget myWindow;
QTest::keyClick(&myWindow, Qt::Key_Tab);
//! [28]

//! [29]
QTest::keyClick(&myWindow, Qt::Key_Escape);
QTest::keyClick(&myWindow, Qt::Key_Escape, Qt::ShiftModifier, 200);
//! [29]

//! [30]
void TestQLocale::initTestCase_data()
{
    QTest::addColumn<QLocale>("locale");
    QTest::newRow("C") << QLocale::c();
    QTest::newRow("UKish") << QLocale("en_GB");
    QTest::newRow("USAish") << QLocale(QLocale::English, QLocale::UnitedStates);
}

void TestQLocale::roundTripInt_data()
{
    QTest::addColumn<int>("number");
    QTest::newRow("zero") << 0;
    QTest::newRow("one") << 1;
    QTest::newRow("two") << 2;
    QTest::newRow("ten") << 10;
}
//! [30]

//! [31]
void TestQLocale::roundTripInt()
{
    QFETCH_GLOBAL(QLocale, locale);
    QFETCH(int, number);
    bool ok;
    QCOMPARE(locale.toInt(locale.toString(number), &ok), number);
    QVERIFY(ok);
}
//! [31]

//! [34]
char *toString(const MyType &t)
{
    char *repr = new char[t.reprSize()];
    t.writeRepr(repr);
    return repr;
}
//! [34]

//! [35]
QSignalSpy doubleClickSpy(target, &TargetClass::doubleClicked);
const QPoint p(1, 2);
QTest::mousePress(&myWindow, Qt::LeftButton, Qt::NoModifier, p);
QVERIFY(target.isPressed());
QTest::mouseRelease(&myWindow, Qt::LeftButton, Qt::NoModifier, p, 10);
QCOMPARE(target.isPressed(), false);
QTest::mousePress(&myWindow, Qt::LeftButton, Qt::NoModifier, p, 10);
QCOMPARE(target.pressCount(), 2);
QTest::mouseRelease(&myWindow, Qt::LeftButton, Qt::NoModifier, p, 10);
QCOMPARE(doubleClickSpy.count(), 1);
//! [35]