OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / tests / auto / qml / qmlprojectmanager / fileformat / tst_fileformat.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "qmlprojectitem.h"
34 #include "filefilteritems.h"
35 #include "qmlprojectfileformat.h"
36 #include <QDeclarativeComponent>
37 #include <QDeclarativeContext>
38 #include <QDeclarativeEngine>
39 #include <QtTest>
40
41 //TESTED_COMPONENT=src/plugins/qmlprojectmanager/fileformat
42
43 using namespace QmlProjectManager;
44
45 class tst_FileFormat : public QObject
46 {
47     Q_OBJECT
48 public:
49     tst_FileFormat();
50
51 private slots:
52     void testFileFilter();
53     void testMatchesFile();
54     void testLibraryPaths();
55     void testMainFile();
56 };
57
58 tst_FileFormat::tst_FileFormat()
59 {
60     QmlProjectFileFormat::registerDeclarativeTypes();
61 }
62
63 QString testDataDir = QLatin1String(SRCDIR "/data");
64
65 void tst_FileFormat::testFileFilter()
66 {
67     //
68     // Search for qml files in directory + subdirectories
69     //
70     QString projectFile = QLatin1String(
71             "import QmlProject 1.0\n"
72             "Project {\n"
73             "  QmlFiles {"
74             "  }"
75             "}\n");
76
77     {
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());
84
85         QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
86         QVERIFY(project);
87
88         project->setSourceDirectory(testDataDir);
89
90         QStringList expectedFiles(QStringList() << testDataDir + "/file1.qml"
91                                                 << testDataDir + "/file2.qml"
92                                                 << testDataDir + "/subdir/file3.qml");
93         QCOMPARE(project->files().toSet(), expectedFiles.toSet());
94     }
95
96     //
97     // search for all qml files in directory
98     //
99     projectFile = QLatin1String(
100             "import QmlProject 1.0\n"
101             "Project {\n"
102             "  QmlFiles {\n"
103             "    recursive: false\n"
104             "  }\n"
105             "}\n");
106
107     {
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());
114
115         QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
116         QVERIFY(project);
117
118         project->setSourceDirectory(testDataDir);
119
120         QStringList expectedFiles(QStringList() << testDataDir + "/file1.qml"
121                                                 << testDataDir + "/file2.qml");
122         QCOMPARE(project->files().toSet(), expectedFiles.toSet());
123     }
124
125     //
126     // search for all qml files in subdirectory
127     //
128     projectFile = QLatin1String(
129             "import QmlProject 1.0\n"
130             "Project {\n"
131             "  QmlFiles {\n"
132             "    directory: \"subdir\"\n"
133             "  }\n"
134             "}\n");
135
136     {
137         QDeclarativeEngine engine;
138         QDeclarativeComponent component(&engine);
139         component.setData(projectFile.toUtf8(), QUrl());
140         QVERIFY(component.isReady());
141
142         QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
143         QVERIFY(project);
144
145         project->setSourceDirectory(testDataDir);
146
147         QStringList expectedFiles(QStringList() <<  testDataDir + "/subdir/file3.qml");
148         QCOMPARE(project->files().toSet(), expectedFiles.toSet());
149     }
150
151     //
152     // multiple entries
153     //
154     projectFile = QLatin1String(
155             "import QmlProject 1.0\n"
156             "Project {\n"
157             "  QmlFiles {\n"
158             "    directory: \".\"\n"
159             "    recursive: false\n"
160             "  }"
161             "  QmlFiles {\n"
162             "    directory: \"subdir\"\n"
163             "  }\n"
164             "}\n");
165
166     {
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());
173
174         QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
175         QVERIFY(project);
176
177         project->setSourceDirectory(testDataDir);
178
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());
184     }
185
186     //
187     // include specific list
188     //
189     projectFile = QLatin1String(
190             "import QmlProject 1.0\n"
191             "Project {\n"
192             "  QmlFiles {\n"
193             "    paths: [ \"file1.qml\",\n"
194             "\"file2.qml\" ]\n"
195             "  }\n"
196             "}\n");
197
198     {
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());
205
206         QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
207         QVERIFY(project);
208
209         project->setSourceDirectory(testDataDir);
210
211         QStringList expectedFiles(QStringList() << testDataDir + "/file1.qml"
212                                                 << testDataDir + "/file2.qml");
213         QCOMPARE(project->files().toSet(), expectedFiles.toSet());
214     }
215
216     //
217     // include specific list
218     //
219     projectFile = QLatin1String(
220             "import QmlProject 1.0\n"
221             "Project {\n"
222             "  ImageFiles {\n"
223             "    directory: \".\"\n"
224             "  }\n"
225             "}\n");
226
227     {
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());
234
235         QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
236         QVERIFY(project);
237
238         project->setSourceDirectory(testDataDir);
239
240         QStringList expectedFiles(QStringList() << testDataDir + "/image.gif");
241         qDebug() << project->files().toSet() << expectedFiles.toSet();
242         QCOMPARE(project->files().toSet(), expectedFiles.toSet());
243     }
244
245     //
246     // use wildcards
247     //
248     projectFile = QLatin1String(
249             "import QmlProject 1.0\n"
250             "Project {\n"
251             "  ImageFiles {\n"
252             "    filter: \"?mage.[gf]if\"\n"
253             "  }\n"
254             "}\n");
255
256     {
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());
263
264         QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
265         QVERIFY(project);
266
267         project->setSourceDirectory(testDataDir);
268
269         QStringList expectedFiles(QStringList() << testDataDir + "/image.gif");
270         qDebug() << project->files().toSet() << expectedFiles.toSet();
271         QCOMPARE(project->files().toSet(), expectedFiles.toSet());
272     }
273
274     //
275     // use Files element (1.1)
276     //
277     projectFile = QLatin1String(
278             "import QmlProject 1.1\n"
279             "Project {\n"
280             "  Files {\n"
281             "    filter: \"image.gif\"\n"
282             "  }\n"
283             "}\n");
284
285     {
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());
292
293         QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
294         QVERIFY(project);
295
296         project->setSourceDirectory(testDataDir);
297
298         QStringList expectedFiles(QStringList() << testDataDir + "/image.gif");
299         qDebug() << project->files().toSet() << expectedFiles.toSet();
300         QCOMPARE(project->files().toSet(), expectedFiles.toSet());
301     }
302 }
303
304 void tst_FileFormat::testMatchesFile()
305 {
306     //
307     // search for qml files in local directory
308     //
309     QString projectFile = QLatin1String(
310             "import QmlProject 1.0\n"
311             "Project {\n"
312             "  QmlFiles {"
313             "    recursive: true"
314             "  }"
315             "  JavaScriptFiles {"
316             "    paths: [\"script.js\"]"
317             "  }"
318             "}\n");
319
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());
326
327     QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
328     QVERIFY(project);
329
330     project->setSourceDirectory(testDataDir);
331
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"));
337 }
338
339 void tst_FileFormat::testLibraryPaths()
340 {
341     //
342     // search for qml files in local directory
343     //
344     QString projectFile = QLatin1String(
345             "import QmlProject 1.0\n"
346             "Project {\n"
347             "  importPaths: [ \"../otherLibrary\", \"library\" ]\n"
348             "}\n");
349
350     {
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());
357
358         QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
359         QVERIFY(project);
360
361         project->setSourceDirectory(testDataDir);
362
363         QStringList expectedPaths(QStringList() << SRCDIR "/otherLibrary"
364                                                 << SRCDIR "/data/library");
365         qDebug() << expectedPaths << project->importPaths();
366         QCOMPARE(project->importPaths().toSet(), expectedPaths.toSet());
367     }
368 }
369
370 void tst_FileFormat::testMainFile()
371 {
372     //
373     // search for qml files in local directory
374     //
375     QString projectFile = QLatin1String(
376             "import QmlProject 1.1\n"
377             "Project {\n"
378             "  mainFile: \"file1.qml\"\n"
379             "}\n");
380
381     {
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());
388
389         QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
390         QVERIFY(project);
391
392         QCOMPARE(project->mainFile(), QString("file1.qml"));
393     }
394 }
395
396 QTEST_MAIN(tst_FileFormat);
397 #include "tst_fileformat.moc"