1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include <QtDeclarative/qdeclarativeengine.h>
44 #include <QtDeclarative/qdeclarativecomponent.h>
47 #include "../shared/testhttpserver.h"
48 #include "../../../shared/util.h"
50 #define SERVER_ADDR "http://127.0.0.1:14450"
51 #define SERVER_PORT 14450
54 class tst_qdeclarativemoduleplugin : public QObject
58 tst_qdeclarativemoduleplugin()
64 void importsPlugin2();
65 void importsPlugin21();
66 void importsMixedQmlCppPlugin();
67 void incorrectPluginCase();
68 void importPluginWithQmlFile();
69 void remoteImportWithQuotedUrl();
70 void remoteImportWithUnquotedUri();
71 void versionNotInstalled();
72 void versionNotInstalled_data();
73 void implicitQmldir();
74 void implicitQmldir_data();
78 // In Symbian OS test data is located in applications private dir
82 #define VERIFY_ERRORS(errorfile) \
84 if (qgetenv("DEBUG") != "" && !component.errors().isEmpty()) \
85 qWarning() << "Unexpected Errors:" << component.errors(); \
86 QVERIFY(!component.isError()); \
87 QVERIFY(component.errors().isEmpty()); \
89 QString verify_errors_file_name = QLatin1String(SRCDIR) + QDir::separator() + QLatin1String("data") + QDir::separator() + QLatin1String(errorfile); \
90 QFile file(verify_errors_file_name); \
91 QVERIFY(file.open(QIODevice::ReadOnly | QIODevice::Text)); \
92 QByteArray data = file.readAll(); \
94 QList<QByteArray> expected = data.split('\n'); \
95 expected.removeAll(QByteArray("")); \
96 QList<QDeclarativeError> errors = component.errors(); \
97 QList<QByteArray> actual; \
98 for (int ii = 0; ii < errors.count(); ++ii) { \
99 const QDeclarativeError &error = errors.at(ii); \
100 QByteArray errorStr = QByteArray::number(error.line()) + ":" + \
101 QByteArray::number(error.column()) + ":" + \
102 error.description().toUtf8(); \
103 actual << errorStr; \
105 if (qgetenv("DEBUG") != "" && expected != actual) { \
106 qWarning() << "Expected:" << expected << "Actual:" << actual; \
108 if (qgetenv("QDECLARATIVELANGUAGE_UPDATEERRORS") != "" && expected != actual) {\
109 QFile file(QLatin1String(SRCDIR) + QDir::separator() + QLatin1String("data") + QDir::separator() + QLatin1String(errorfile)); \
110 QVERIFY(file.open(QIODevice::WriteOnly)); \
111 for (int ii = 0; ii < actual.count(); ++ii) { \
112 file.write(actual.at(ii)); file.write("\n"); \
116 QCOMPARE(expected, actual); \
120 inline QUrl TEST_FILE(const QString &filename)
122 QFileInfo fileInfo(__FILE__);
123 return QUrl::fromLocalFile(fileInfo.absoluteDir().filePath(filename));
126 void tst_qdeclarativemoduleplugin::importsPlugin()
128 QDeclarativeEngine engine;
129 engine.addImportPath(QLatin1String(SRCDIR) + QDir::separator() + QLatin1String("imports"));
130 QTest::ignoreMessage(QtWarningMsg, "plugin created");
131 QTest::ignoreMessage(QtWarningMsg, "import worked");
132 QDeclarativeComponent component(&engine, TEST_FILE("data/works.qml"));
133 foreach (QDeclarativeError err, component.errors())
136 QObject *object = component.create();
137 QVERIFY(object != 0);
138 QCOMPARE(object->property("value").toInt(),123);
142 void tst_qdeclarativemoduleplugin::importsPlugin2()
144 QDeclarativeEngine engine;
145 engine.addImportPath(QLatin1String(SRCDIR) + QDir::separator() + QLatin1String("imports"));
146 QTest::ignoreMessage(QtWarningMsg, "plugin2 created");
147 QTest::ignoreMessage(QtWarningMsg, "import2 worked");
148 QDeclarativeComponent component(&engine, TEST_FILE("data/works2.qml"));
149 foreach (QDeclarativeError err, component.errors())
152 QObject *object = component.create();
153 QVERIFY(object != 0);
154 QCOMPARE(object->property("value").toInt(),123);
158 void tst_qdeclarativemoduleplugin::importsPlugin21()
160 QDeclarativeEngine engine;
161 engine.addImportPath(QLatin1String(SRCDIR) + QDir::separator() + QLatin1String("imports"));
162 QTest::ignoreMessage(QtWarningMsg, "plugin2.1 created");
163 QTest::ignoreMessage(QtWarningMsg, "import2.1 worked");
164 QDeclarativeComponent component(&engine, TEST_FILE("data/works21.qml"));
165 foreach (QDeclarativeError err, component.errors())
168 QObject *object = component.create();
169 QVERIFY(object != 0);
170 QCOMPARE(object->property("value").toInt(),123);
174 void tst_qdeclarativemoduleplugin::incorrectPluginCase()
176 QDeclarativeEngine engine;
177 engine.addImportPath(QLatin1String(SRCDIR) + QDir::separator() + QLatin1String("imports"));
179 QDeclarativeComponent component(&engine, TEST_FILE("data/incorrectCase.qml"));
181 QList<QDeclarativeError> errors = component.errors();
182 QCOMPARE(errors.count(), 1);
184 #if defined(Q_OS_MAC) || defined(Q_OS_WIN32)
185 #if defined(Q_OS_MAC)
186 QString libname = "libPluGin.dylib";
187 #elif defined(Q_OS_WIN32)
188 QString libname = "PluGin.dll";
190 QString expectedError = QLatin1String("plugin cannot be loaded for module \"com.nokia.WrongCase\": File name case mismatch for \"") + QFileInfo(__FILE__).absoluteDir().filePath("imports/com/nokia/WrongCase/" + libname) + QLatin1String("\"");
192 QString expectedError = QLatin1String("module \"com.nokia.WrongCase\" plugin \"PluGin\" not found");
195 QCOMPARE(errors.at(0).description(), expectedError);
198 void tst_qdeclarativemoduleplugin::importPluginWithQmlFile()
200 QString path = QLatin1String(SRCDIR) + QDir::separator() + QLatin1String("imports");
202 // QTBUG-16885: adding an import path with a lower-case "c:" causes assert failure
203 // (this only happens if the plugin includes pure QML files)
205 QVERIFY(path.at(0).isUpper() && path.at(1) == QLatin1Char(':'));
206 path = path.at(0).toLower() + path.mid(1);
209 QDeclarativeEngine engine;
210 engine.addImportPath(path);
212 QDeclarativeComponent component(&engine, TEST_FILE("data/pluginWithQmlFile.qml"));
213 foreach (QDeclarativeError err, component.errors())
216 QObject *object = component.create();
217 QVERIFY(object != 0);
221 void tst_qdeclarativemoduleplugin::remoteImportWithQuotedUrl()
223 TestHTTPServer server(SERVER_PORT);
224 QVERIFY(server.isValid());
225 server.serveDirectory(SRCDIR "/imports");
227 QDeclarativeEngine engine;
228 QDeclarativeComponent component(&engine);
229 component.setData("import \"http://127.0.0.1:14450/com/nokia/PureQmlModule\" \nComponentA { width: 300; ComponentB{} }", QUrl());
231 QTRY_COMPARE(component.status(), QDeclarativeComponent::Ready);
232 QObject *object = component.create();
233 QCOMPARE(object->property("width").toInt(), 300);
234 QVERIFY(object != 0);
237 foreach (QDeclarativeError err, component.errors())
242 void tst_qdeclarativemoduleplugin::remoteImportWithUnquotedUri()
244 TestHTTPServer server(SERVER_PORT);
245 QVERIFY(server.isValid());
246 server.serveDirectory(SRCDIR "/imports");
248 QDeclarativeEngine engine;
249 engine.addImportPath(QLatin1String(SRCDIR) + QDir::separator() + QLatin1String("imports"));
250 QDeclarativeComponent component(&engine);
251 component.setData("import com.nokia.PureQmlModule 1.0 \nComponentA { width: 300; ComponentB{} }", QUrl());
254 QTRY_COMPARE(component.status(), QDeclarativeComponent::Ready);
255 QObject *object = component.create();
256 QVERIFY(object != 0);
257 QCOMPARE(object->property("width").toInt(), 300);
260 foreach (QDeclarativeError err, component.errors())
266 void tst_qdeclarativemoduleplugin::importsMixedQmlCppPlugin()
268 QDeclarativeEngine engine;
269 engine.addImportPath(QLatin1String(SRCDIR) + QDir::separator() + QLatin1String("imports"));
272 QDeclarativeComponent component(&engine, TEST_FILE("data/importsMixedQmlCppPlugin.qml"));
274 QObject *o = component.create();
276 QCOMPARE(o->property("test").toBool(), true);
281 QDeclarativeComponent component(&engine, TEST_FILE("data/importsMixedQmlCppPlugin.2.qml"));
283 QObject *o = component.create();
285 QCOMPARE(o->property("test").toBool(), true);
286 QCOMPARE(o->property("test2").toBool(), true);
293 void tst_qdeclarativemoduleplugin::versionNotInstalled_data()
295 QTest::addColumn<QString>("file");
296 QTest::addColumn<QString>("errorFile");
298 QTest::newRow("versionNotInstalled") << "data/versionNotInstalled.qml" << "versionNotInstalled.errors.txt";
299 QTest::newRow("versionNotInstalled") << "data/versionNotInstalled.2.qml" << "versionNotInstalled.2.errors.txt";
302 void tst_qdeclarativemoduleplugin::versionNotInstalled()
304 QFETCH(QString, file);
305 QFETCH(QString, errorFile);
307 QDeclarativeEngine engine;
308 engine.addImportPath(QLatin1String(SRCDIR) + QDir::separator() + QLatin1String("imports"));
310 QDeclarativeComponent component(&engine, TEST_FILE(file));
311 VERIFY_ERRORS(errorFile.toLatin1().constData());
315 // test that errors are reporting correctly for plugin loading and qmldir parsing
316 void tst_qdeclarativemoduleplugin::implicitQmldir_data()
318 QTest::addColumn<QString>("directory");
319 QTest::addColumn<QString>("file");
320 QTest::addColumn<QString>("errorFile");
322 // parsing qmldir succeeds, but plugin specified in the qmldir file doesn't exist
323 QTest::newRow("implicitQmldir") << "implicit1" << "temptest.qml" << "implicitQmldir.errors.txt";
325 // parsing qmldir fails due to syntax errors, etc.
326 QTest::newRow("implicitQmldir2") << "implicit2" << "temptest2.qml" << "implicitQmldir.2.errors.txt";
328 void tst_qdeclarativemoduleplugin::implicitQmldir()
330 QFETCH(QString, directory);
331 QFETCH(QString, file);
332 QFETCH(QString, errorFile);
334 QString importPath = QLatin1String(SRCDIR) + QDir::separator() + QLatin1String("data") + QDir::separator() + directory;
335 QString fileName = QLatin1String("data") + QDir::separator() + directory + QDir::separator() + file;
336 QString errorFileName = directory + QDir::separator() + errorFile;
337 QUrl testFileUrl = TEST_FILE(fileName);
339 QDeclarativeEngine engine;
340 engine.addImportPath(importPath);
342 QDeclarativeComponent component(&engine, testFileUrl);
343 QList<QDeclarativeError> errors = component.errors();
344 VERIFY_ERRORS(errorFileName.toLatin1().constData());
345 QTest::ignoreMessage(QtWarningMsg, "QDeclarativeComponent: Component is not ready");
346 QObject *obj = component.create();
352 QTEST_MAIN(tst_qdeclarativemoduleplugin)
354 #include "tst_qdeclarativemoduleplugin.moc"