1 /*
2  * \copyright Copyright (c) 2016-2021 Governikus GmbH & Co. KG, Germany
3  */
4 
5 #include "Env.h"
6 #include "LogHandler.h"
7 #include "ReaderManager.h"
8 #include "ResourceLoader.h"
9 #include "UIPlugInQml.h"
10 
11 #include <QFontDatabase>
12 #include <QLoggingCategory>
13 #include <QQmlContext>
14 #include <QQmlEngine>
15 #include <QQmlFileSelector>
16 #include <QtQuickTest>
17 
18 
19 using namespace governikus;
20 
21 Q_DECLARE_LOGGING_CATEGORY(init)
22 
23 class QmlTestRunner
24 	: public QObject
25 {
26 	Q_OBJECT
27 
28 	// Mock of the UIPluginQml properties
29 	Q_PROPERTY(bool debugBuild MEMBER mFalse CONSTANT)
30 	Q_PROPERTY(bool developerVersion MEMBER mFalse CONSTANT)
31 	Q_PROPERTY(bool dominated MEMBER mFalse CONSTANT)
32 	Q_PROPERTY(QVariantMap safeAreaMargins MEMBER mSafeAreaMargins CONSTANT)
33 	Q_PROPERTY(bool highContrastEnabled MEMBER mFalse CONSTANT)
34 	Q_PROPERTY(QString platformStyle MEMBER mPlatformStyle CONSTANT)
35 	Q_PROPERTY(QString fixedFontFamily MEMBER mFixedFontFamily CONSTANT)
36 
37 	private:
38 		const bool mFalse = false;
39 		const QVariantMap mSafeAreaMargins = {
40 			{"top", 0}, {"right", 0}, {"bottom", 0}, {"left", 0}
41 		};
42 		QString mPlatformStyle;
43 		QString mFixedFontFamily;
44 
45 	public:
QmlTestRunner()46 		QmlTestRunner()
47 		{
48 			// Enable this warning again when our minimum Qt version is 5.14
49 			QLoggingCategory::setFilterRules(QStringLiteral("qt.qml.connections.warning=false"));
50 		}
51 
52 
applyPlatformStyle(const QString & pPlatformStyle)53 		Q_INVOKABLE void applyPlatformStyle(const QString& pPlatformStyle)
54 		{
55 			Q_UNUSED(pPlatformStyle)
56 		}
57 
58 	public Q_SLOTS:
applicationAvailable()59 		void applicationAvailable()
60 		{
61 			QThread::currentThread()->setObjectName(QStringLiteral("MainThread"));
62 			ResourceLoader::getInstance().init();
63 			Env::getSingleton<ReaderManager>()->init();
64 			UIPlugInQml::registerQmlTypes();
65 		}
66 
67 
cleanupTestCase()68 		void cleanupTestCase()
69 		{
70 			ResourceLoader::getInstance().shutdown();
71 			Env::getSingleton<ReaderManager>()->shutdown();
72 		}
73 
74 
qmlEngineAvailable(QQmlEngine * pEngine)75 		void qmlEngineAvailable(QQmlEngine* pEngine)
76 		{
77 			pEngine->rootContext()->setContextProperty(QStringLiteral("plugin"), this);
78 
79 			connect(pEngine, &QQmlEngine::warnings, [](const QList<QQmlError>& pWarnings){
80 						bool fail = false;
81 						for (auto& warning : pWarnings)
82 						{
83 							QWARN(warning.toString().toLatin1().constData());
84 #if (QT_VERSION < QT_VERSION_CHECK(5, 15, 1))
85 							fail |= !warning.description().contains("QML Connections: Implicitly defined onFoo properties in Connections are deprecated. Use this syntax instead:");
86 #else
87 							fail = true;
88 #endif
89 						}
90 
91 						if (fail)
92 						{
93 							QCoreApplication::quit();
94 							QFAIL("QQmlEngine has errors");
95 						}
96 					});
97 
98 			const QStringList selectors = QQmlFileSelector::get(pEngine)->selector()->extraSelectors();
99 			mPlatformStyle = selectors.join(QLatin1String(","));
100 
101 			mFixedFontFamily = QFontDatabase::systemFont(QFontDatabase::FixedFont).family();
102 		}
103 
104 
105 };
106 
107 QUICK_TEST_MAIN_WITH_SETUP(qml, QmlTestRunner)
108 
109 #include "QmlTestRunner.moc"
110