tst_fileformat.cpp 10.2 KB
Newer Older
1
2
#include "qmlprojectitem.h"
#include "filefilteritems.h"
Kai Koehne's avatar
Kai Koehne committed
3
4
5
6
#include "qmlprojectfileformat.h"
#include <QDeclarativeComponent>
#include <QDeclarativeContext>
#include <QDeclarativeEngine>
7
8
#include <QtTest>

9
10
//TESTED_COMPONENT=src/plugins/qmlprojectmanager/fileformat

11
12
using namespace QmlProjectManager;

13
class tst_FileFormat : public QObject
14
15
16
{
    Q_OBJECT
public:
17
    tst_FileFormat();
18
19

private slots:
20
    void testFileFilter();
21
    void testMatchesFile();
22
    void testLibraryPaths();
23
24
};

25
tst_FileFormat::tst_FileFormat()
26
{
Kai Koehne's avatar
Kai Koehne committed
27
    QmlProjectFileFormat::registerDeclarativeTypes();
28
29
30
31
}

QString testDataDir = QLatin1String(SRCDIR "/data");

32
void tst_FileFormat::testFileFilter()
33
34
{
    //
35
    // Search for qml files in directory + subdirectories
36
37
38
39
40
41
42
43
44
    //
    QString projectFile = QLatin1String(
            "import QmlProject 1.0\n"
            "Project {\n"
            "  QmlFiles {"
            "  }"
            "}\n");

    {
Kai Koehne's avatar
Kai Koehne committed
45
46
        QDeclarativeEngine engine;
        QDeclarativeComponent component(&engine);
47
48
        component.setData(projectFile.toUtf8(), QUrl());
        if (!component.isReady())
49
            qDebug() << component.errorString();
50
51
52
53
54
55
56
        QVERIFY(component.isReady());

        QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
        QVERIFY(project);

        project->setSourceDirectory(testDataDir);

57
        QStringList expectedFiles(QStringList() << testDataDir + "/file1.qml"
58
59
                                                << testDataDir + "/file2.qml"
                                                << testDataDir + "/subdir/file3.qml");
60
        QCOMPARE(project->files().toSet(), expectedFiles.toSet());
61
62
63
    }

    //
64
    // search for all qml files in directory
65
66
67
68
69
    //
    projectFile = QLatin1String(
            "import QmlProject 1.0\n"
            "Project {\n"
            "  QmlFiles {\n"
70
            "    recursive: false\n"
71
72
73
74
            "  }\n"
            "}\n");

    {
Kai Koehne's avatar
Kai Koehne committed
75
76
        QDeclarativeEngine engine;
        QDeclarativeComponent component(&engine);
77
        component.setData(projectFile.toUtf8(), QUrl());
78
        if (!component.isReady())
79
            qDebug() << component.errorString();
80
81
82
83
84
85
86
        QVERIFY(component.isReady());

        QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
        QVERIFY(project);

        project->setSourceDirectory(testDataDir);

87
        QStringList expectedFiles(QStringList() << testDataDir + "/file1.qml"
88
                                                << testDataDir + "/file2.qml");
89
        QCOMPARE(project->files().toSet(), expectedFiles.toSet());
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    }

    //
    // search for all qml files in subdirectory
    //
    projectFile = QLatin1String(
            "import QmlProject 1.0\n"
            "Project {\n"
            "  QmlFiles {\n"
            "    directory: \"subdir\"\n"
            "  }\n"
            "}\n");

    {
Kai Koehne's avatar
Kai Koehne committed
104
105
        QDeclarativeEngine engine;
        QDeclarativeComponent component(&engine);
106
107
108
109
110
111
112
113
        component.setData(projectFile.toUtf8(), QUrl());
        QVERIFY(component.isReady());

        QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
        QVERIFY(project);

        project->setSourceDirectory(testDataDir);

114
        QStringList expectedFiles(QStringList() <<  testDataDir + "/subdir/file3.qml");
115
        QCOMPARE(project->files().toSet(), expectedFiles.toSet());
116
117
118
    }

    //
119
    // multiple entries
120
121
122
123
124
    //
    projectFile = QLatin1String(
            "import QmlProject 1.0\n"
            "Project {\n"
            "  QmlFiles {\n"
125
            "    directory: \".\"\n"
126
            "    recursive: false\n"
127
128
129
130
131
132
133
            "  }"
            "  QmlFiles {\n"
            "    directory: \"subdir\"\n"
            "  }\n"
            "}\n");

    {
Kai Koehne's avatar
Kai Koehne committed
134
135
        QDeclarativeEngine engine;
        QDeclarativeComponent component(&engine);
136
        component.setData(projectFile.toUtf8(), QUrl());
137
        if (!component.isReady())
138
            qDebug() << component.errorString();
139
140
141
142
143
144
145
        QVERIFY(component.isReady());

        QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
        QVERIFY(project);

        project->setSourceDirectory(testDataDir);

146
147
148
        QStringList expectedFiles(QStringList() << testDataDir + "/file1.qml"
                                                << testDataDir + "/file2.qml"
                                                << testDataDir + "/subdir/file3.qml");
149
150
        QCOMPARE(project->files().size(), 3);
        QCOMPARE(project->files().toSet(), expectedFiles.toSet());
151
    }
152
153
154
155
156
157
158
159

    //
    // include specific list
    //
    projectFile = QLatin1String(
            "import QmlProject 1.0\n"
            "Project {\n"
            "  QmlFiles {\n"
160
161
            "    paths: [ \"file1.qml\",\n"
            "\"file2.qml\" ]\n"
162
163
164
165
            "  }\n"
            "}\n");

    {
Kai Koehne's avatar
Kai Koehne committed
166
167
        QDeclarativeEngine engine;
        QDeclarativeComponent component(&engine);
168
169
        component.setData(projectFile.toUtf8(), QUrl());
        if (!component.isReady())
170
            qDebug() << component.errorString();
171
172
173
174
175
176
177
178
179
        QVERIFY(component.isReady());

        QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
        QVERIFY(project);

        project->setSourceDirectory(testDataDir);

        QStringList expectedFiles(QStringList() << testDataDir + "/file1.qml"
                                                << testDataDir + "/file2.qml");
180
        QCOMPARE(project->files().toSet(), expectedFiles.toSet());
181
182
    }

183
184
185
186
187
188
189
190
191
192
193
194
    //
    // include specific list
    //
    projectFile = QLatin1String(
            "import QmlProject 1.0\n"
            "Project {\n"
            "  ImageFiles {\n"
            "    directory: \".\"\n"
            "  }\n"
            "}\n");

    {
Kai Koehne's avatar
Kai Koehne committed
195
196
        QDeclarativeEngine engine;
        QDeclarativeComponent component(&engine);
197
198
        component.setData(projectFile.toUtf8(), QUrl());
        if (!component.isReady())
199
            qDebug() << component.errorString();
200
201
202
203
204
205
206
207
208
209
210
        QVERIFY(component.isReady());

        QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
        QVERIFY(project);

        project->setSourceDirectory(testDataDir);

        QStringList expectedFiles(QStringList() << testDataDir + "/image.gif");
        qDebug() << project->files().toSet() << expectedFiles.toSet();
        QCOMPARE(project->files().toSet(), expectedFiles.toSet());
    }
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239

    //
    // use wildcards
    //
    projectFile = QLatin1String(
            "import QmlProject 1.0\n"
            "Project {\n"
            "  ImageFiles {\n"
            "    filter: \"?mage.[gf]if\"\n"
            "  }\n"
            "}\n");

    {
        QDeclarativeEngine engine;
        QDeclarativeComponent component(&engine);
        component.setData(projectFile.toUtf8(), QUrl());
        if (!component.isReady())
            qDebug() << component.errorString();
        QVERIFY(component.isReady());

        QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
        QVERIFY(project);

        project->setSourceDirectory(testDataDir);

        QStringList expectedFiles(QStringList() << testDataDir + "/image.gif");
        qDebug() << project->files().toSet() << expectedFiles.toSet();
        QCOMPARE(project->files().toSet(), expectedFiles.toSet());
    }
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268

    //
    // use Files element (1.1)
    //
    projectFile = QLatin1String(
            "import QmlProject 1.1\n"
            "Project {\n"
            "  Files {\n"
            "    filter: \"image.gif\"\n"
            "  }\n"
            "}\n");

    {
        QDeclarativeEngine engine;
        QDeclarativeComponent component(&engine);
        component.setData(projectFile.toUtf8(), QUrl());
        if (!component.isReady())
            qDebug() << component.errorString();
        QVERIFY(component.isReady());

        QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
        QVERIFY(project);

        project->setSourceDirectory(testDataDir);

        QStringList expectedFiles(QStringList() << testDataDir + "/image.gif");
        qDebug() << project->files().toSet() << expectedFiles.toSet();
        QCOMPARE(project->files().toSet(), expectedFiles.toSet());
    }
269
270
}

271
void tst_FileFormat::testMatchesFile()
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
{
    //
    // search for qml files in local directory
    //
    QString projectFile = QLatin1String(
            "import QmlProject 1.0\n"
            "Project {\n"
            "  QmlFiles {"
            "    recursive: true"
            "  }"
            "  JavaScriptFiles {"
            "    paths: [\"script.js\"]"
            "  }"
            "}\n");

Kai Koehne's avatar
Kai Koehne committed
287
288
    QDeclarativeEngine engine;
    QDeclarativeComponent component(&engine);
289
290
    component.setData(projectFile.toUtf8(), QUrl());
    if (!component.isReady())
291
        qDebug() << component.errorString();
292
293
294
295
296
297
298
299
300
301
302
303
304
305
    QVERIFY(component.isReady());

    QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
    QVERIFY(project);

    project->setSourceDirectory(testDataDir);

    QVERIFY(project->matchesFile(testDataDir + "/file1.qml"));
    QVERIFY(project->matchesFile(testDataDir + "/notyetexistingfile.qml"));
    QVERIFY(project->matchesFile(testDataDir + "/subdir/notyetexistingfile.qml"));
    QVERIFY(project->matchesFile(testDataDir + "/script.js"));
    QVERIFY(!project->matchesFile(testDataDir + "/script.css"));
}

306
void tst_FileFormat::testLibraryPaths()
307
308
309
310
311
312
313
{
    //
    // search for qml files in local directory
    //
    QString projectFile = QLatin1String(
            "import QmlProject 1.0\n"
            "Project {\n"
314
            "  importPaths: [ \"../otherLibrary\", \"library\" ]\n"
315
316
317
            "}\n");

    {
Kai Koehne's avatar
Kai Koehne committed
318
319
        QDeclarativeEngine engine;
        QDeclarativeComponent component(&engine);
320
321
        component.setData(projectFile.toUtf8(), QUrl());
        if (!component.isReady())
322
            qDebug() << component.errorString();
323
324
325
326
327
328
329
        QVERIFY(component.isReady());

        QmlProjectItem *project = qobject_cast<QmlProjectItem*>(component.create());
        QVERIFY(project);

        project->setSourceDirectory(testDataDir);

330
331
332
        QStringList expectedPaths(QStringList() << SRCDIR "/otherLibrary"
                                                << SRCDIR "/data/library");
        qDebug() << expectedPaths << project->importPaths();
333
        QCOMPARE(project->importPaths().toSet(), expectedPaths.toSet());
334
335
336
    }
}

337

338
QTEST_MAIN(tst_FileFormat);
339
#include "tst_fileformat.moc"