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>
15#include <QtQml/qqmlcomponent.h>
28 static constexpr int m_argc = 1;
29 static constexpr char *m_argv[] = {
const_cast<
char *>(
"tst_qmltc_examples") };
41#define CREATE_DUMMY_ARGC_ARGV()
43 char *argv[] = { const_cast<char *>("tst_qmltc_examples") };
50 QGuiApplication app(argc, argv);
51 app.setApplicationDisplayName(QStringLiteral(
"This example is powered by qmltc!"));
58 QScopedPointer<QmltcExample::myApp> documentRoot(
59 new QmltcExample::myApp(&e,
nullptr, [](
auto& component){
60 component.setWidth(800);
63 documentRoot->setParentItem(window.contentItem());
64 window.setHeight(documentRoot->height());
65 window.setWidth(documentRoot->width());
69 QTimer::singleShot(1000, &app, QGuiApplication::quit);
82 QGuiApplication app(argc, argv);
83 app.setApplicationDisplayName(QStringLiteral(
"This example is powered by QQmlComponent :("));
90 QQmlComponent component(&e);
92 QUrl(QStringLiteral(
"qrc:/qt/qml/QmltcExample/myApp.qml")));
95 QVERIFY2(!component.isError(), qPrintable(component.errorString()));
98 QScopedPointer<QObject> documentRoot(component.create());
99 QQuickItem *documentRootItem = qobject_cast<QQuickItem *>(documentRoot.get());
102 QVERIFY(documentRootItem);
105 documentRootItem->setParentItem(window.contentItem());
106 window.setHeight(documentRootItem->height());
107 window.setWidth(documentRootItem->width());
111 QTimer::singleShot(1000, &app, QGuiApplication::quit);
117#if !defined(QMLTC_TESTS_SOURCE_DIR) || !defined(QMLTC_TESTS_BINARY_DIR)
118# error "Tests assume that QMLTC_TESTS_{SOURCE,BINARY}_DIR are specified (through CMake)"
122template<
typename Predicate>
128 QVERIFY(file.exists());
129 QVERIFY(file.open(QIODeviceBase::ReadOnly | QIODeviceBase::Text));
131 QTextStream stream(&file);
132 while (!stream.atEnd()) {
133 QString line = stream.readLine();
135 content->append(
std::move(line));
142 QSKIP(
"expected C++ files are not bundled with Android tests.");
144 QStringList generatedCode;
145 readFileContent(&generatedCode,
146 QStringLiteral(QMLTC_TESTS_BINARY_DIR)
147 + u"/.qmltc/tst_qmltc_examples/helloworld.h",
148 [](
const QString &) {
return true; });
149 if (QTest::currentTestFailed())
150 QFAIL(
"Reading _generated_ C++ content for special/HelloWorld.qml failed");
152 QStringList documentationCode;
153 const auto filterDocumentationLines = [encounteredStart =
false](QStringView line)
mutable {
154 if (line.startsWith(u"// MAGIC_QMLTC_TEST_DELIMITER_LINE")) {
155 encounteredStart =
true;
158 if (!encounteredStart)
160 line = line.trimmed();
161 return !line.isEmpty() && !line.startsWith(u"//");
163 readFileContent(&documentationCode,
164 QStringLiteral(QMLTC_TESTS_SOURCE_DIR) + u"/special/HelloWorld.qml.cpp",
165 filterDocumentationLines);
166 if (QTest::currentTestFailed())
167 QFAIL(
"Reading special/HelloWorld.qml.cpp failed");
169 QVERIFY(!generatedCode.isEmpty());
170 QVERIFY(!documentationCode.isEmpty());
172 auto begin = generatedCode.cbegin();
173 for (
const QString &existingString : std::as_const(documentationCode)) {
174 auto pos = std::find(begin, generatedCode.cend(), existingString);
175 QVERIFY2(pos != generatedCode.cend(), qPrintable(u"Could not find: " + existingString));
176 begin = std::next(pos);
180#undef CREATE_DUMMY_ARGC_ARGV
182QTEST_APPLESS_MAIN(tst_qmltc_examples)
183#include "tst_qmltc_examples.moc"
tst_qmltc_examples(QObject *parent=nullptr)
void readFileContent(QStringList *content, const QString &url, Predicate filter)
#define CREATE_DUMMY_ARGC_ARGV()