1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include <QtQml/qqmlengine.h>
44 #include <QtQml/qqmlcomponent.h>
47 #include "../../shared/testhttpserver.h"
48 #include "../../shared/util.h"
50 #define SERVER_ADDR "http://127.0.0.1:14456"
51 #define SERVER_PORT 14456
54 class tst_qqmlmoduleplugin : public QQmlDataTest
60 virtual void initTestCase();
62 void importsPlugin2();
63 void importsPlugin21();
64 void importsMixedQmlCppPlugin();
65 void incorrectPluginCase();
66 void importPluginWithQmlFile();
67 void remoteImportWithQuotedUrl();
68 void remoteImportWithUnquotedUri();
69 void versionNotInstalled();
70 void versionNotInstalled_data();
71 void implicitQmldir();
72 void implicitQmldir_data();
74 void importsNested_data();
75 void importLocalModule();
76 void importLocalModule_data();
79 QString m_importsDirectory;
80 QString m_dataImportsDirectory;
83 void tst_qqmlmoduleplugin::initTestCase()
85 QQmlDataTest::initTestCase();
86 m_importsDirectory = QFINDTESTDATA(QStringLiteral("imports"));
87 QVERIFY2(QFileInfo(m_importsDirectory).isDir(),
88 qPrintable(QString::fromLatin1("Imports directory '%1' does not exist.").arg(m_importsDirectory)));
89 m_dataImportsDirectory = directory() + QStringLiteral("/imports");
90 QVERIFY2(QFileInfo(m_dataImportsDirectory).isDir(),
91 qPrintable(QString::fromLatin1("Imports directory '%1' does not exist.").arg(m_dataImportsDirectory)));
94 #define VERIFY_ERRORS(errorfile) \
96 if (qgetenv("DEBUG") != "" && !component.errors().isEmpty()) \
97 qWarning() << "Unexpected Errors:" << component.errors(); \
98 QVERIFY(!component.isError()); \
99 QVERIFY(component.errors().isEmpty()); \
101 QString verify_errors_file_name = testFile(errorfile); \
102 QFile file(verify_errors_file_name); \
103 QVERIFY(file.open(QIODevice::ReadOnly | QIODevice::Text)); \
104 QByteArray data = file.readAll(); \
106 QList<QByteArray> expected = data.split('\n'); \
107 expected.removeAll(QByteArray("")); \
108 QList<QQmlError> errors = component.errors(); \
109 QList<QByteArray> actual; \
110 for (int ii = 0; ii < errors.count(); ++ii) { \
111 const QQmlError &error = errors.at(ii); \
112 QByteArray errorStr = QByteArray::number(error.line()) + ":" + \
113 QByteArray::number(error.column()) + ":" + \
114 error.description().toUtf8(); \
115 actual << errorStr; \
117 if (qgetenv("DEBUG") != "" && expected != actual) { \
118 qWarning() << "Expected:" << expected << "Actual:" << actual; \
120 if (qgetenv("QDECLARATIVELANGUAGE_UPDATEERRORS") != "" && expected != actual) {\
121 QFile file(testFile(errorfile)); \
122 QVERIFY(file.open(QIODevice::WriteOnly)); \
123 for (int ii = 0; ii < actual.count(); ++ii) { \
124 file.write(actual.at(ii)); file.write("\n"); \
128 QCOMPARE(expected, actual); \
132 void tst_qqmlmoduleplugin::importsPlugin()
135 engine.addImportPath(m_importsDirectory);
136 QTest::ignoreMessage(QtWarningMsg, "plugin created");
137 QTest::ignoreMessage(QtWarningMsg, "import worked");
138 QQmlComponent component(&engine, testFileUrl(QStringLiteral("works.qml")));
139 foreach (QQmlError err, component.errors())
142 QObject *object = component.create();
143 QVERIFY(object != 0);
144 QCOMPARE(object->property("value").toInt(),123);
148 void tst_qqmlmoduleplugin::importsPlugin2()
151 engine.addImportPath(m_importsDirectory);
152 QTest::ignoreMessage(QtWarningMsg, "plugin2 created");
153 QTest::ignoreMessage(QtWarningMsg, "import2 worked");
154 QQmlComponent component(&engine, testFileUrl(QStringLiteral("works2.qml")));
155 foreach (QQmlError err, component.errors())
158 QObject *object = component.create();
159 QVERIFY(object != 0);
160 QCOMPARE(object->property("value").toInt(),123);
164 void tst_qqmlmoduleplugin::importsPlugin21()
167 engine.addImportPath(m_importsDirectory);
168 QTest::ignoreMessage(QtWarningMsg, "plugin2.1 created");
169 QTest::ignoreMessage(QtWarningMsg, "import2.1 worked");
170 QQmlComponent component(&engine, testFileUrl(QStringLiteral("works21.qml")));
171 foreach (QQmlError err, component.errors())
174 QObject *object = component.create();
175 QVERIFY(object != 0);
176 QCOMPARE(object->property("value").toInt(),123);
180 void tst_qqmlmoduleplugin::incorrectPluginCase()
183 engine.addImportPath(m_importsDirectory);
185 QQmlComponent component(&engine, testFileUrl(QStringLiteral("incorrectCase.qml")));
187 QList<QQmlError> errors = component.errors();
188 QCOMPARE(errors.count(), 1);
190 #if defined(Q_OS_MAC) || defined(Q_OS_WIN32)
191 #if defined(Q_OS_MAC)
192 QString libname = "libPluGin.dylib";
193 #elif defined(Q_OS_WIN32)
194 QString libname = "PluGin.dll";
196 QString expectedError = QLatin1String("plugin cannot be loaded for module \"com.nokia.WrongCase\": File name case mismatch for \"") + QDir(m_importsDirectory).filePath("com/nokia/WrongCase/" + libname) + QLatin1String("\"");
198 QString expectedError = QLatin1String("module \"com.nokia.WrongCase\" plugin \"PluGin\" not found");
201 QCOMPARE(errors.at(0).description(), expectedError);
204 void tst_qqmlmoduleplugin::importPluginWithQmlFile()
206 QString path = m_importsDirectory;
208 // QTBUG-16885: adding an import path with a lower-case "c:" causes assert failure
209 // (this only happens if the plugin includes pure QML files)
211 QVERIFY(path.at(0).isUpper() && path.at(1) == QLatin1Char(':'));
212 path = path.at(0).toLower() + path.mid(1);
216 engine.addImportPath(path);
218 QQmlComponent component(&engine, testFileUrl(QStringLiteral("pluginWithQmlFile.qml")));
219 foreach (QQmlError err, component.errors())
222 QObject *object = component.create();
223 QVERIFY(object != 0);
227 void tst_qqmlmoduleplugin::remoteImportWithQuotedUrl()
229 TestHTTPServer server(SERVER_PORT);
230 QVERIFY(server.isValid());
231 server.serveDirectory(m_dataImportsDirectory);
234 QQmlComponent component(&engine);
235 component.setData("import \"" SERVER_ADDR "/com/nokia/PureQmlModule\" \nComponentA { width: 300; ComponentB{} }", QUrl());
237 QTRY_COMPARE(component.status(), QQmlComponent::Ready);
238 QObject *object = component.create();
239 QCOMPARE(object->property("width").toInt(), 300);
240 QVERIFY(object != 0);
243 foreach (QQmlError err, component.errors())
248 void tst_qqmlmoduleplugin::remoteImportWithUnquotedUri()
250 TestHTTPServer server(SERVER_PORT);
251 QVERIFY(server.isValid());
252 server.serveDirectory(m_dataImportsDirectory);
255 engine.addImportPath(m_dataImportsDirectory);
256 QQmlComponent component(&engine);
257 component.setData("import com.nokia.PureQmlModule 1.0 \nComponentA { width: 300; ComponentB{} }", QUrl());
260 QTRY_COMPARE(component.status(), QQmlComponent::Ready);
261 QObject *object = component.create();
262 QVERIFY(object != 0);
263 QCOMPARE(object->property("width").toInt(), 300);
266 foreach (QQmlError err, component.errors())
273 void tst_qqmlmoduleplugin::importsMixedQmlCppPlugin()
276 engine.addImportPath(m_importsDirectory);
279 QQmlComponent component(&engine, testFileUrl(QStringLiteral("importsMixedQmlCppPlugin.qml")));
281 QObject *o = component.create();
282 QVERIFY2(o != 0, QQmlDataTest::msgComponentError(component, &engine));
283 QCOMPARE(o->property("test").toBool(), true);
288 QQmlComponent component(&engine, testFileUrl(QStringLiteral("importsMixedQmlCppPlugin.2.qml")));
290 QObject *o = component.create();
291 QVERIFY2(o != 0, QQmlDataTest::msgComponentError(component, &engine));
292 QCOMPARE(o->property("test").toBool(), true);
293 QCOMPARE(o->property("test2").toBool(), true);
300 void tst_qqmlmoduleplugin::versionNotInstalled_data()
302 QTest::addColumn<QString>("file");
303 QTest::addColumn<QString>("errorFile");
305 QTest::newRow("versionNotInstalled") << "versionNotInstalled.qml" << "versionNotInstalled.errors.txt";
306 QTest::newRow("versionNotInstalled") << "versionNotInstalled.2.qml" << "versionNotInstalled.2.errors.txt";
309 void tst_qqmlmoduleplugin::versionNotInstalled()
311 QFETCH(QString, file);
312 QFETCH(QString, errorFile);
315 engine.addImportPath(m_importsDirectory);
317 QQmlComponent component(&engine, testFileUrl(file));
318 VERIFY_ERRORS(errorFile.toLatin1().constData());
322 // test that errors are reporting correctly for plugin loading and qmldir parsing
323 void tst_qqmlmoduleplugin::implicitQmldir_data()
325 QTest::addColumn<QString>("directory");
326 QTest::addColumn<QString>("file");
327 QTest::addColumn<QString>("errorFile");
329 // parsing qmldir succeeds, but plugin specified in the qmldir file doesn't exist
330 QTest::newRow("implicitQmldir") << "implicit1" << "temptest.qml" << "implicitQmldir.errors.txt";
332 // parsing qmldir fails due to syntax errors, etc.
333 QTest::newRow("implicitQmldir2") << "implicit2" << "temptest2.qml" << "implicitQmldir.2.errors.txt";
335 void tst_qqmlmoduleplugin::implicitQmldir()
337 QFETCH(QString, directory);
338 QFETCH(QString, file);
339 QFETCH(QString, errorFile);
341 QString importPath = testFile(directory);
342 QString fileName = directory + QDir::separator() + file;
343 QString errorFileName = directory + QDir::separator() + errorFile;
344 QUrl testUrl = testFileUrl(fileName);
347 engine.addImportPath(importPath);
349 QQmlComponent component(&engine, testUrl);
350 QList<QQmlError> errors = component.errors();
351 VERIFY_ERRORS(errorFileName.toLatin1().constData());
352 QTest::ignoreMessage(QtWarningMsg, "QQmlComponent: Component is not ready");
353 QObject *obj = component.create();
358 void tst_qqmlmoduleplugin::importsNested_data()
360 QTest::addColumn<QString>("file");
361 QTest::addColumn<QString>("errorFile");
363 // Note: no other test case should import the plugin used for this test, or the
364 // wrong order test will pass spuriously
365 QTest::newRow("wrongOrder") << "importsNested.1.qml" << "importsNested.1.errors.txt";
366 QTest::newRow("missingImport") << "importsNested.3.qml" << "importsNested.3.errors.txt";
367 QTest::newRow("invalidVersion") << "importsNested.4.qml" << "importsNested.4.errors.txt";
368 QTest::newRow("correctOrder") << "importsNested.2.qml" << QString();
370 void tst_qqmlmoduleplugin::importsNested()
372 QFETCH(QString, file);
373 QFETCH(QString, errorFile);
375 // Note: because imports are cached between test case data rows (and the plugins remain loaded),
376 // these tests should really be run in new instances of the app...
379 engine.addImportPath(m_importsDirectory);
381 if (!errorFile.isEmpty()) {
382 QTest::ignoreMessage(QtWarningMsg, "QQmlComponent: Component is not ready");
385 QQmlComponent component(&engine, testFile(file));
386 QObject *obj = component.create();
388 if (errorFile.isEmpty()) {
389 if (qgetenv("DEBUG") != "" && !component.errors().isEmpty())
390 qWarning() << "Unexpected Errors:" << component.errors();
394 QList<QQmlError> errors = component.errors();
395 VERIFY_ERRORS(errorFile.toLatin1().constData());
400 void tst_qqmlmoduleplugin::importLocalModule()
402 QFETCH(QString, qml);
403 QFETCH(int, majorVersion);
404 QFETCH(int, minorVersion);
407 QQmlComponent component(&engine);
408 component.setData(qml.toUtf8(), testFileUrl("empty.qml"));
410 QScopedPointer<QObject> object(component.create());
411 QVERIFY(object != 0);
412 QCOMPARE(object->property("majorVersion").value<int>(), majorVersion);
413 QCOMPARE(object->property("minorVersion").value<int>(), minorVersion);
416 void tst_qqmlmoduleplugin::importLocalModule_data()
418 QTest::addColumn<QString>("qml");
419 QTest::addColumn<int>("majorVersion");
420 QTest::addColumn<int>("minorVersion");
422 QTest::newRow("default version")
423 << "import \"localModule\"\n"
427 QTest::newRow("specific version")
428 << "import \"localModule\" 1.1\n"
432 QTest::newRow("lesser version")
433 << "import \"localModule\" 1.0\n"
437 // Note: this does not match the behaviour of installed modules, which fail for this case:
438 QTest::newRow("nonexistent version")
439 << "import \"localModule\" 1.3\n"
443 QTest::newRow("high version")
444 << "import \"localModule\" 2.0\n"
449 QTEST_MAIN(tst_qqmlmoduleplugin)
451 #include "tst_qqmlmoduleplugin.moc"