Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
tst_qmltc_examples.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4// Note: this file is published under a license that is different from a default
5// test sources license. This is intentional to comply with default
6// snippet license.
7
8#include <QtTest/qtest.h>
9#include <QtCore/qstring.h>
10#include <QtCore/qtimer.h>
11#include <QtGui/qguiapplication.h>
12#include <QtQuick/qquickwindow.h>
13
15#include <QtQml/qqmlcomponent.h>
17
19#include "myapp.h" // include generated C++ header
21
22#include <algorithm>
23
25{
27
28 static constexpr int m_argc = 1;
29 static constexpr char *m_argv[] = { const_cast<char *>("tst_qmltc_examples") };
30
31public:
33
34private slots:
35 void app();
36 void appComponent();
37
38 void helloWorld();
39};
40
41#define CREATE_DUMMY_ARGC_ARGV() \
42 int argc = 1; \
43 char *argv[] = { const_cast<char *>("tst_qmltc_examples") };
44
45void tst_qmltc_examples::app()
46{
48
49
50 QGuiApplication app(argc, argv);
51 app.setApplicationDisplayName(QStringLiteral("This example is powered by qmltc!"));
52
55
56 QScopedPointer<QmltcExample::myApp> documentRoot(new QmltcExample::myApp(&e));
57
58 documentRoot->setParentItem(window.contentItem());
59 window.setHeight(documentRoot->height());
60 window.setWidth(documentRoot->width());
61 // ...
63
65
67 window.show();
68 app.exec();
70}
71
72void tst_qmltc_examples::appComponent()
73{
75
76
77 QGuiApplication app(argc, argv);
78 app.setApplicationDisplayName(QStringLiteral("This example is powered by QQmlComponent :("));
79
82
84 component.loadUrl(
85 QUrl(QStringLiteral("qrc:/qt/qml/QmltcExample/myApp.qml")));
87
88 QVERIFY2(!component.isError(), qPrintable(component.errorString()));
89
91 QScopedPointer<QObject> documentRoot(component.create());
92 QQuickItem *documentRootItem = qobject_cast<QQuickItem *>(documentRoot.get());
94
95 QVERIFY(documentRootItem);
96
98 documentRootItem->setParentItem(window.contentItem());
99 window.setHeight(documentRootItem->height());
100 window.setWidth(documentRootItem->width());
101 // ...
103
104 QTimer::singleShot(1000, &app, QGuiApplication::quit);
105
106 window.show();
107 app.exec();
108}
109
110#if !defined(QMLTC_TESTS_SOURCE_DIR) || !defined(QMLTC_TESTS_BINARY_DIR)
111# error "Tests assume that QMLTC_TESTS_{SOURCE,BINARY}_DIR are specified (through CMake)"
112#endif
113
114// Note: QtTest macros need to be in void-returning function, so use output arg.
115template<typename Predicate>
116void readFileContent(QStringList *content, const QString &url, Predicate filter)
117{
118 QVERIFY(content);
119
120 QFile file(url);
123
125 while (!stream.atEnd()) {
126 QString line = stream.readLine();
127 if (filter(line))
128 content->append(std::move(line));
129 }
130}
131
132void tst_qmltc_examples::helloWorld()
133{
134#ifdef Q_OS_ANDROID
135 QSKIP("expected C++ files are not bundled with Android tests.");
136#endif
137 QStringList generatedCode;
138 readFileContent(&generatedCode,
139 QStringLiteral(QMLTC_TESTS_BINARY_DIR)
140 + u"/.qmltc/tst_qmltc_examples/helloworld.h",
141 [](const QString &) { return true; });
143 QFAIL("Reading _generated_ C++ content for special/HelloWorld.qml failed");
144
145 QStringList documentationCode;
146 const auto filterDocumentationLines = [encounteredStart = false](QStringView line) mutable {
147 if (line.startsWith(u"// MAGIC_QMLTC_TEST_DELIMITER_LINE")) {
148 encounteredStart = true;
149 return false; // we don't need this specific line
150 }
151 if (!encounteredStart)
152 return false;
153 line = line.trimmed();
154 return !line.isEmpty() && !line.startsWith(u"//");
155 };
156 readFileContent(&documentationCode,
157 QStringLiteral(QMLTC_TESTS_SOURCE_DIR) + u"/special/HelloWorld.qml.cpp",
158 filterDocumentationLines);
160 QFAIL("Reading special/HelloWorld.qml.cpp failed");
161
162 QVERIFY(!generatedCode.isEmpty());
163 QVERIFY(!documentationCode.isEmpty());
164
165 auto begin = generatedCode.cbegin();
166 for (const QString &existingString : std::as_const(documentationCode)) {
167 auto pos = std::find(begin, generatedCode.cend(), existingString);
168 QVERIFY2(pos != generatedCode.cend(), qPrintable(u"Could not find: " + existingString));
169 begin = std::next(pos);
170 }
171}
172
173#undef CREATE_DUMMY_ARGC_ARGV
174
176#include "tst_qmltc_examples.moc"
static int exec()
Enters the main event loop and waits until exit() is called, then returns the value that was set to e...
static void quit()
\threadsafe
\inmodule QtCore
Definition qfile.h:93
bool open(OpenMode flags) override
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition qfile.cpp:881
bool exists() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qfile.cpp:351
\macro qGuiApp
static void setApplicationDisplayName(const QString &name)
\inmodule QtCore
Definition qobject.h:90
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:311
template< class T > T qobject_cast(const QObject *object)
Returns the given object cast to type T if the object is of type T (or of a subclass); otherwise retu...
Definition qobject.h:394
The QQmlComponent class encapsulates a QML component definition.
The QQmlEngine class provides an environment for instantiating QML components.
Definition qqmlengine.h:57
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:64
\qmltype Window \instantiates QQuickWindow \inqmlmodule QtQuick
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore
Definition qstringview.h:76
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition qstring.cpp:5299
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:1083
QString trimmed() const &
Definition qstring.h:380
\inmodule QtCore
\inmodule QtCore
Definition qtimer.h:20
bool singleShot
whether the timer is a single-shot timer
Definition qtimer.h:22
\inmodule QtCore
Definition qurl.h:94
[qqmlcomponent-include]
tst_qmltc_examples(QObject *parent=nullptr)
double e
Q_TESTLIB_EXPORT bool currentTestFailed()
Returns true if the current test function has failed, otherwise false.
static QDBusError::ErrorType get(const char *name)
EGLStreamKHR stream
GLint GLsizei GLsizei height
GLint GLsizei width
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
GLbyte by
static qreal component(const QPointF &point, unsigned int i)
QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIterator begin(const QRegularExpressionMatchIterator &iterator)
#define qPrintable(string)
Definition qstring.h:1391
#define QStringLiteral(str)
#define QTEST_APPLESS_MAIN(TestObject)
Definition qtest.h:651
#define QSKIP(statement,...)
Definition qtestcase.h:254
#define QFAIL(message)
Definition qtestcase.h:35
#define QVERIFY(statement)
Definition qtestcase.h:29
#define QVERIFY2(statement, description)
Definition qtestcase.h:41
#define Q_OBJECT
#define slots
view show()
[18] //! [19]
QFile file
[0]
QUrl url("example.com")
[constructor-url-reference]
dialog exec()
QApplication app(argc, argv)
[0]
aWidget window() -> setWindowTitle("New Window Title")
[2]
view create()
void readFileContent(QStringList *content, const QString &url, Predicate filter)
#define CREATE_DUMMY_ARGC_ARGV()