Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
53 QQmlEngine e;
55
56 QScopedPointer<QmltcExample::myApp> documentRoot(
57 new QmltcExample::myApp(&e, nullptr, [](auto& component){
58 component.setWidth(800);
59 }));
60
61 documentRoot->setParentItem(window.contentItem());
62 window.setHeight(documentRoot->height());
63 window.setWidth(documentRoot->width());
64 // ...
66
68
70 window.show();
71 app.exec();
73}
74
75void tst_qmltc_examples::appComponent()
76{
78
79
80 QGuiApplication app(argc, argv);
81 app.setApplicationDisplayName(QStringLiteral("This example is powered by QQmlComponent :("));
82
83 QQmlEngine e;
85
87 component.loadUrl(
88 QUrl(QStringLiteral("qrc:/qt/qml/QmltcExample/myApp.qml")));
90
91 QVERIFY2(!component.isError(), qPrintable(component.errorString()));
92
94 QScopedPointer<QObject> documentRoot(component.create());
95 QQuickItem *documentRootItem = qobject_cast<QQuickItem *>(documentRoot.get());
97
98 QVERIFY(documentRootItem);
99
101 documentRootItem->setParentItem(window.contentItem());
102 window.setHeight(documentRootItem->height());
103 window.setWidth(documentRootItem->width());
104 // ...
106
107 QTimer::singleShot(1000, &app, QGuiApplication::quit);
108
109 window.show();
110 app.exec();
111}
112
113#if !defined(QMLTC_TESTS_SOURCE_DIR) || !defined(QMLTC_TESTS_BINARY_DIR)
114# error "Tests assume that QMLTC_TESTS_{SOURCE,BINARY}_DIR are specified (through CMake)"
115#endif
116
117// Note: QtTest macros need to be in void-returning function, so use output arg.
118template<typename Predicate>
119void readFileContent(QStringList *content, const QString &url, Predicate filter)
120{
121 QVERIFY(content);
122
123 QFile file(url);
126
128 while (!stream.atEnd()) {
129 QString line = stream.readLine();
130 if (filter(line))
131 content->append(std::move(line));
132 }
133}
134
135void tst_qmltc_examples::helloWorld()
136{
137#ifdef Q_OS_ANDROID
138 QSKIP("expected C++ files are not bundled with Android tests.");
139#endif
140 QStringList generatedCode;
141 readFileContent(&generatedCode,
142 QStringLiteral(QMLTC_TESTS_BINARY_DIR)
143 + u"/.qmltc/tst_qmltc_examples/helloworld.h",
144 [](const QString &) { return true; });
146 QFAIL("Reading _generated_ C++ content for special/HelloWorld.qml failed");
147
148 QStringList documentationCode;
149 const auto filterDocumentationLines = [encounteredStart = false](QStringView line) mutable {
150 if (line.startsWith(u"// MAGIC_QMLTC_TEST_DELIMITER_LINE")) {
151 encounteredStart = true;
152 return false; // we don't need this specific line
153 }
154 if (!encounteredStart)
155 return false;
156 line = line.trimmed();
157 return !line.isEmpty() && !line.startsWith(u"//");
158 };
159 readFileContent(&documentationCode,
160 QStringLiteral(QMLTC_TESTS_SOURCE_DIR) + u"/special/HelloWorld.qml.cpp",
161 filterDocumentationLines);
163 QFAIL("Reading special/HelloWorld.qml.cpp failed");
164
165 QVERIFY(!generatedCode.isEmpty());
166 QVERIFY(!documentationCode.isEmpty());
167
168 auto begin = generatedCode.cbegin();
169 for (const QString &existingString : std::as_const(documentationCode)) {
170 auto pos = std::find(begin, generatedCode.cend(), existingString);
171 QVERIFY2(pos != generatedCode.cend(), qPrintable(u"Could not find: " + existingString));
172 begin = std::next(pos);
173 }
174}
175
176#undef CREATE_DUMMY_ARGC_ARGV
177
179#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
QFILE_MAYBE_NODISCARD bool open(OpenMode flags) override
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition qfile.cpp:904
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:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
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:430
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:63
\qmltype Window \instantiates QQuickWindow \inqmlmodule QtQuick
\inmodule QtCore
\inmodule QtCore
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
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:5455
bool isEmpty() const noexcept
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:192
QString trimmed() const &
Definition qstring.h:447
\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)
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:1531
#define QStringLiteral(str)
#define QTEST_APPLESS_MAIN(TestObject)
Definition qtest.h:277
#define QSKIP(statement,...)
Definition qtestcase.h:270
#define QFAIL(message)
Definition qtestcase.h:64
#define QVERIFY(statement)
Definition qtestcase.h:58
#define QVERIFY2(statement, description)
Definition qtestcase.h:70
#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()