Merge branch 'master' into qtquick2
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativemoduleplugin / tst_qdeclarativemoduleplugin.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <qtest.h>
42 #include <qdir.h>
43 #include <QtDeclarative/qdeclarativeengine.h>
44 #include <QtDeclarative/qdeclarativecomponent.h>
45 #include <QDebug>
46
47 #include "../shared/testhttpserver.h"
48 #include "../../../shared/util.h"
49
50 #define SERVER_ADDR "http://127.0.0.1:14450"
51 #define SERVER_PORT 14450
52
53
54 class tst_qdeclarativemoduleplugin : public QObject
55 {
56     Q_OBJECT
57 public:
58     tst_qdeclarativemoduleplugin()
59     {
60     }
61
62 private slots:
63     void importsPlugin();
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();
75 };
76
77 #ifdef Q_OS_SYMBIAN
78 // In Symbian OS test data is located in applications private dir
79 #define SRCDIR "."
80 #endif
81
82 #define VERIFY_ERRORS(errorfile) \
83     if (!errorfile) { \
84         if (qgetenv("DEBUG") != "" && !component.errors().isEmpty()) \
85             qWarning() << "Unexpected Errors:" << component.errors(); \
86         QVERIFY(!component.isError()); \
87         QVERIFY(component.errors().isEmpty()); \
88     } else { \
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(); \
93         file.close(); \
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; \
104         } \
105         if (qgetenv("DEBUG") != "" && expected != actual) { \
106             qWarning() << "Expected:" << expected << "Actual:" << actual; \
107         } \
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"); \
113             } \
114             file.close(); \
115         } else { \
116             QCOMPARE(expected, actual); \
117         } \
118     }
119
120 inline QUrl TEST_FILE(const QString &filename)
121 {
122     QFileInfo fileInfo(__FILE__);
123     return QUrl::fromLocalFile(fileInfo.absoluteDir().filePath(filename));
124 }
125
126 void tst_qdeclarativemoduleplugin::importsPlugin()
127 {
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())
134         qWarning() << err;
135     VERIFY_ERRORS(0);
136     QObject *object = component.create();
137     QVERIFY(object != 0);
138     QCOMPARE(object->property("value").toInt(),123);
139     delete object;
140 }
141
142 void tst_qdeclarativemoduleplugin::importsPlugin2()
143 {
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())
150         qWarning() << err;
151     VERIFY_ERRORS(0);
152     QObject *object = component.create();
153     QVERIFY(object != 0);
154     QCOMPARE(object->property("value").toInt(),123);
155     delete object;
156 }
157
158 void tst_qdeclarativemoduleplugin::importsPlugin21()
159 {
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())
166         qWarning() << err;
167     VERIFY_ERRORS(0);
168     QObject *object = component.create();
169     QVERIFY(object != 0);
170     QCOMPARE(object->property("value").toInt(),123);
171     delete object;
172 }
173
174 void tst_qdeclarativemoduleplugin::incorrectPluginCase()
175 {
176     QDeclarativeEngine engine;
177     engine.addImportPath(QLatin1String(SRCDIR) + QDir::separator() + QLatin1String("imports"));
178
179     QDeclarativeComponent component(&engine, TEST_FILE("data/incorrectCase.qml"));
180
181     QList<QDeclarativeError> errors = component.errors();
182     QCOMPARE(errors.count(), 1);
183
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";
189 #endif
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("\"");
191 #else
192     QString expectedError = QLatin1String("module \"com.nokia.WrongCase\" plugin \"PluGin\" not found");
193 #endif
194
195     QCOMPARE(errors.at(0).description(), expectedError);
196 }
197
198 void tst_qdeclarativemoduleplugin::importPluginWithQmlFile()
199 {
200     QString path = QLatin1String(SRCDIR) + QDir::separator() + QLatin1String("imports");
201
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)
204     #ifdef Q_OS_WIN
205         QVERIFY(path.at(0).isUpper() && path.at(1) == QLatin1Char(':'));
206         path = path.at(0).toLower() + path.mid(1);
207     #endif
208
209     QDeclarativeEngine engine;
210     engine.addImportPath(path);
211
212     QDeclarativeComponent component(&engine, TEST_FILE("data/pluginWithQmlFile.qml"));
213     foreach (QDeclarativeError err, component.errors())
214         qWarning() << err;
215     VERIFY_ERRORS(0);
216     QObject *object = component.create();
217     QVERIFY(object != 0);
218     delete object;
219 }
220
221 void tst_qdeclarativemoduleplugin::remoteImportWithQuotedUrl()
222 {
223     TestHTTPServer server(SERVER_PORT);
224     QVERIFY(server.isValid());
225     server.serveDirectory(SRCDIR "/imports");
226
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());
230
231     QTRY_COMPARE(component.status(), QDeclarativeComponent::Ready);
232     QObject *object = component.create();
233     QCOMPARE(object->property("width").toInt(), 300);
234     QVERIFY(object != 0);
235     delete object;
236
237     foreach (QDeclarativeError err, component.errors())
238         qWarning() << err;
239     VERIFY_ERRORS(0);
240 }
241
242 void tst_qdeclarativemoduleplugin::remoteImportWithUnquotedUri()
243 {
244     TestHTTPServer server(SERVER_PORT);
245     QVERIFY(server.isValid());
246     server.serveDirectory(SRCDIR "/imports");
247
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());
252
253
254     QTRY_COMPARE(component.status(), QDeclarativeComponent::Ready);
255     QObject *object = component.create();
256     QVERIFY(object != 0);
257     QCOMPARE(object->property("width").toInt(), 300);
258     delete object;
259
260     foreach (QDeclarativeError err, component.errors())
261         qWarning() << err;
262     VERIFY_ERRORS(0);
263 }
264
265 // QTBUG-17324
266 void tst_qdeclarativemoduleplugin::importsMixedQmlCppPlugin()
267 {
268     QDeclarativeEngine engine;
269     engine.addImportPath(QLatin1String(SRCDIR) + QDir::separator() + QLatin1String("imports"));
270
271     {
272     QDeclarativeComponent component(&engine, TEST_FILE("data/importsMixedQmlCppPlugin.qml"));
273
274     QObject *o = component.create();
275     QVERIFY(o != 0);
276     QCOMPARE(o->property("test").toBool(), true);
277     delete o;
278     }
279
280     {
281     QDeclarativeComponent component(&engine, TEST_FILE("data/importsMixedQmlCppPlugin.2.qml"));
282
283     QObject *o = component.create();
284     QVERIFY(o != 0);
285     QCOMPARE(o->property("test").toBool(), true);
286     QCOMPARE(o->property("test2").toBool(), true);
287     delete o;
288     }
289
290
291 }
292
293 void tst_qdeclarativemoduleplugin::versionNotInstalled_data()
294 {
295     QTest::addColumn<QString>("file");
296     QTest::addColumn<QString>("errorFile");
297
298     QTest::newRow("versionNotInstalled") << "data/versionNotInstalled.qml" << "versionNotInstalled.errors.txt";
299     QTest::newRow("versionNotInstalled") << "data/versionNotInstalled.2.qml" << "versionNotInstalled.2.errors.txt";
300 }
301
302 void tst_qdeclarativemoduleplugin::versionNotInstalled()
303 {
304     QFETCH(QString, file);
305     QFETCH(QString, errorFile);
306
307     QDeclarativeEngine engine;
308     engine.addImportPath(QLatin1String(SRCDIR) + QDir::separator() + QLatin1String("imports"));
309
310     QDeclarativeComponent component(&engine, TEST_FILE(file));
311     VERIFY_ERRORS(errorFile.toLatin1().constData());
312 }
313
314
315 // test that errors are reporting correctly for plugin loading and qmldir parsing
316 void tst_qdeclarativemoduleplugin::implicitQmldir_data()
317 {
318     QTest::addColumn<QString>("directory");
319     QTest::addColumn<QString>("file");
320     QTest::addColumn<QString>("errorFile");
321
322     // parsing qmldir succeeds, but plugin specified in the qmldir file doesn't exist
323     QTest::newRow("implicitQmldir") << "implicit1" << "temptest.qml" << "implicitQmldir.errors.txt";
324
325     // parsing qmldir fails due to syntax errors, etc.
326     QTest::newRow("implicitQmldir2") << "implicit2" << "temptest2.qml" << "implicitQmldir.2.errors.txt";
327 }
328 void tst_qdeclarativemoduleplugin::implicitQmldir()
329 {
330     QFETCH(QString, directory);
331     QFETCH(QString, file);
332     QFETCH(QString, errorFile);
333
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);
338
339     QDeclarativeEngine engine;
340     engine.addImportPath(importPath);
341
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();
347     QVERIFY(!obj);
348     delete obj;
349 }
350
351
352 QTEST_MAIN(tst_qdeclarativemoduleplugin)
353
354 #include "tst_qdeclarativemoduleplugin.moc"