summaryrefslogtreecommitdiffstats
path: root/src/testlib/doc/snippets/code/src_qtestlib_qtestcase_snippet.cpp
diff options
context:
space:
mode:
authorNico Vertriest <nico.vertriest@qt.io>2020-02-27 12:58:26 +0100
committerNico Vertriest <nico.vertriest@qt.io>2020-04-03 13:13:55 +0000
commit713cd83200f3c60eac5d389dfabc44be1446e2ac (patch)
tree5f5eb19254e661022d8db88f3cc772bdc7a60e29 /src/testlib/doc/snippets/code/src_qtestlib_qtestcase_snippet.cpp
parent0c2d6c163f6e7908a7cc9a626f027cb38a5d7786 (diff)
Doc: Make Qt Test snippets compilable
Task-number: QTBUG-81498 Change-Id: I22f07cd539e5e317b6cf15eb369d59915146bd13 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/testlib/doc/snippets/code/src_qtestlib_qtestcase_snippet.cpp')
-rw-r--r--src/testlib/doc/snippets/code/src_qtestlib_qtestcase_snippet.cpp168
1 files changed, 168 insertions, 0 deletions
diff --git a/src/testlib/doc/snippets/code/src_qtestlib_qtestcase_snippet.cpp b/src/testlib/doc/snippets/code/src_qtestlib_qtestcase_snippet.cpp
new file mode 100644
index 0000000000..f414ee4252
--- /dev/null
+++ b/src/testlib/doc/snippets/code/src_qtestlib_qtestcase_snippet.cpp
@@ -0,0 +1,168 @@
+/****************************************************************************
+**
+** Copyright (C) 2020 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the documentation of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+//! [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]