1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "qmlprojectitem.h"
34 #include "filefilteritems.h"
35 #include "qmlprojectfileformat.h"
36 #include <QDeclarativeComponent>
37 #include <QDeclarativeContext>
38 #include <QDeclarativeEngine>
41 //TESTED_COMPONENT=src/plugins/qmlprojectmanager/fileformat
43 using namespace QmlProjectManager;
45 class tst_FileFormat : public QObject
52 void testFileFilter();
53 void testMatchesFile();
54 void testLibraryPaths();
58 tst_FileFormat::tst_FileFormat()
60 QmlProjectFileFormat::registerDeclarativeTypes();
63 QString testDataDir = QLatin1String(SRCDIR "/data");
65 void tst_FileFormat::testFileFilter()
68 // Search for qml files in directory + subdirectories
70 QString projectFile = QLatin1String(
71 "import QmlProject 1.0\n"
78 QDeclarativeEngine engine;
79 QDeclarativeComponent component(&engine);
80 component.setData(projectFile.toUtf8(), QUrl());
81 if (!component.isReady())
82 qDebug() << component.errorString();
83 QVERIFY(component.isReady());
85 QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
88 project->setSourceDirectory(testDataDir);
90 QStringList expectedFiles(QStringList() << testDataDir + "/file1.qml"
91 << testDataDir + "/file2.qml"
92 << testDataDir + "/subdir/file3.qml");
93 QCOMPARE(project->files().toSet(), expectedFiles.toSet());
97 // search for all qml files in directory
99 projectFile = QLatin1String(
100 "import QmlProject 1.0\n"
103 " recursive: false\n"
108 QDeclarativeEngine engine;
109 QDeclarativeComponent component(&engine);
110 component.setData(projectFile.toUtf8(), QUrl());
111 if (!component.isReady())
112 qDebug() << component.errorString();
113 QVERIFY(component.isReady());
115 QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
118 project->setSourceDirectory(testDataDir);
120 QStringList expectedFiles(QStringList() << testDataDir + "/file1.qml"
121 << testDataDir + "/file2.qml");
122 QCOMPARE(project->files().toSet(), expectedFiles.toSet());
126 // search for all qml files in subdirectory
128 projectFile = QLatin1String(
129 "import QmlProject 1.0\n"
132 " directory: \"subdir\"\n"
137 QDeclarativeEngine engine;
138 QDeclarativeComponent component(&engine);
139 component.setData(projectFile.toUtf8(), QUrl());
140 QVERIFY(component.isReady());
142 QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
145 project->setSourceDirectory(testDataDir);
147 QStringList expectedFiles(QStringList() << testDataDir + "/subdir/file3.qml");
148 QCOMPARE(project->files().toSet(), expectedFiles.toSet());
154 projectFile = QLatin1String(
155 "import QmlProject 1.0\n"
158 " directory: \".\"\n"
159 " recursive: false\n"
162 " directory: \"subdir\"\n"
167 QDeclarativeEngine engine;
168 QDeclarativeComponent component(&engine);
169 component.setData(projectFile.toUtf8(), QUrl());
170 if (!component.isReady())
171 qDebug() << component.errorString();
172 QVERIFY(component.isReady());
174 QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
177 project->setSourceDirectory(testDataDir);
179 QStringList expectedFiles(QStringList() << testDataDir + "/file1.qml"
180 << testDataDir + "/file2.qml"
181 << testDataDir + "/subdir/file3.qml");
182 QCOMPARE(project->files().size(), 3);
183 QCOMPARE(project->files().toSet(), expectedFiles.toSet());
187 // include specific list
189 projectFile = QLatin1String(
190 "import QmlProject 1.0\n"
193 " paths: [ \"file1.qml\",\n"
199 QDeclarativeEngine engine;
200 QDeclarativeComponent component(&engine);
201 component.setData(projectFile.toUtf8(), QUrl());
202 if (!component.isReady())
203 qDebug() << component.errorString();
204 QVERIFY(component.isReady());
206 QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
209 project->setSourceDirectory(testDataDir);
211 QStringList expectedFiles(QStringList() << testDataDir + "/file1.qml"
212 << testDataDir + "/file2.qml");
213 QCOMPARE(project->files().toSet(), expectedFiles.toSet());
217 // include specific list
219 projectFile = QLatin1String(
220 "import QmlProject 1.0\n"
223 " directory: \".\"\n"
228 QDeclarativeEngine engine;
229 QDeclarativeComponent component(&engine);
230 component.setData(projectFile.toUtf8(), QUrl());
231 if (!component.isReady())
232 qDebug() << component.errorString();
233 QVERIFY(component.isReady());
235 QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
238 project->setSourceDirectory(testDataDir);
240 QStringList expectedFiles(QStringList() << testDataDir + "/image.gif");
241 qDebug() << project->files().toSet() << expectedFiles.toSet();
242 QCOMPARE(project->files().toSet(), expectedFiles.toSet());
248 projectFile = QLatin1String(
249 "import QmlProject 1.0\n"
252 " filter: \"?mage.[gf]if\"\n"
257 QDeclarativeEngine engine;
258 QDeclarativeComponent component(&engine);
259 component.setData(projectFile.toUtf8(), QUrl());
260 if (!component.isReady())
261 qDebug() << component.errorString();
262 QVERIFY(component.isReady());
264 QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
267 project->setSourceDirectory(testDataDir);
269 QStringList expectedFiles(QStringList() << testDataDir + "/image.gif");
270 qDebug() << project->files().toSet() << expectedFiles.toSet();
271 QCOMPARE(project->files().toSet(), expectedFiles.toSet());
275 // use Files element (1.1)
277 projectFile = QLatin1String(
278 "import QmlProject 1.1\n"
281 " filter: \"image.gif\"\n"
286 QDeclarativeEngine engine;
287 QDeclarativeComponent component(&engine);
288 component.setData(projectFile.toUtf8(), QUrl());
289 if (!component.isReady())
290 qDebug() << component.errorString();
291 QVERIFY(component.isReady());
293 QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
296 project->setSourceDirectory(testDataDir);
298 QStringList expectedFiles(QStringList() << testDataDir + "/image.gif");
299 qDebug() << project->files().toSet() << expectedFiles.toSet();
300 QCOMPARE(project->files().toSet(), expectedFiles.toSet());
304 void tst_FileFormat::testMatchesFile()
307 // search for qml files in local directory
309 QString projectFile = QLatin1String(
310 "import QmlProject 1.0\n"
316 " paths: [\"script.js\"]"
320 QDeclarativeEngine engine;
321 QDeclarativeComponent component(&engine);
322 component.setData(projectFile.toUtf8(), QUrl());
323 if (!component.isReady())
324 qDebug() << component.errorString();
325 QVERIFY(component.isReady());
327 QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
330 project->setSourceDirectory(testDataDir);
332 QVERIFY(project->matchesFile(testDataDir + "/file1.qml"));
333 QVERIFY(project->matchesFile(testDataDir + "/notyetexistingfile.qml"));
334 QVERIFY(project->matchesFile(testDataDir + "/subdir/notyetexistingfile.qml"));
335 QVERIFY(project->matchesFile(testDataDir + "/script.js"));
336 QVERIFY(!project->matchesFile(testDataDir + "/script.css"));
339 void tst_FileFormat::testLibraryPaths()
342 // search for qml files in local directory
344 QString projectFile = QLatin1String(
345 "import QmlProject 1.0\n"
347 " importPaths: [ \"../otherLibrary\", \"library\" ]\n"
351 QDeclarativeEngine engine;
352 QDeclarativeComponent component(&engine);
353 component.setData(projectFile.toUtf8(), QUrl());
354 if (!component.isReady())
355 qDebug() << component.errorString();
356 QVERIFY(component.isReady());
358 QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
361 project->setSourceDirectory(testDataDir);
363 QStringList expectedPaths(QStringList() << SRCDIR "/otherLibrary"
364 << SRCDIR "/data/library");
365 qDebug() << expectedPaths << project->importPaths();
366 QCOMPARE(project->importPaths().toSet(), expectedPaths.toSet());
370 void tst_FileFormat::testMainFile()
373 // search for qml files in local directory
375 QString projectFile = QLatin1String(
376 "import QmlProject 1.1\n"
378 " mainFile: \"file1.qml\"\n"
382 QDeclarativeEngine engine;
383 QDeclarativeComponent component(&engine);
384 component.setData(projectFile.toUtf8(), QUrl());
385 if (!component.isReady())
386 qDebug() << component.errorString();
387 QVERIFY(component.isReady());
389 QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
392 QCOMPARE(project->mainFile(), QString("file1.qml"));
396 QTEST_MAIN(tst_FileFormat);
397 #include "tst_fileformat.moc"