fee01c2bf38cfede13a7e82737a705280c6a7700
[profile/ivi/qtdeclarative.git] / tests / auto / qml / qqmlbundle / tst_qqmlbundle.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 // Lookup of libraries
43 // Test bundle as a qmldir
44
45 #include <qtest.h>
46 #include <QDebug>
47 #include <QQmlEngine>
48 #include <QQmlComponent>
49 #include "../../shared/util.h"
50 #include <private/qqmlbundle_p.h>
51
52 class tst_qqmlbundle : public QQmlDataTest
53 {
54     Q_OBJECT
55 public:
56     tst_qqmlbundle() {}
57
58 private slots:
59     void initTestCase();
60
61     void componentFromBundle();
62     void relativeResolution();
63     void bundleImport();
64     void relativeQmldir();
65
66     void import();
67
68 private:
69     QStringList findFiles(const QDir &d);
70     bool makeBundle(const QString &path, const QString &name);
71 };
72
73 void tst_qqmlbundle::initTestCase()
74 {
75     QQmlDataTest::initTestCase();
76 }
77
78 // Test we create a QQmlComponent for a file inside a bundle
79 void tst_qqmlbundle::componentFromBundle()
80 {
81     QVERIFY(makeBundle(testFile("componentFromBundle"), "my.bundle"));
82
83     QQmlEngine engine;
84     engine.addNamedBundle("mybundle", testFile("componentFromBundle/my.bundle"));
85
86     QQmlComponent component(&engine, QUrl("bundle://mybundle/test.qml"));
87     QVERIFY(component.isReady());
88
89     QObject *o = component.create();
90     QVERIFY(o != 0);
91
92     QCOMPARE(o->property("test1").toInt(), 11);
93     QCOMPARE(o->property("test2").toBool(), true);
94
95     delete o;
96 }
97
98 // Tests that relative QML components are resolved without a qmldir
99 void tst_qqmlbundle::relativeResolution()
100 {
101     // Root of the bundle
102     {
103     QVERIFY(makeBundle(testFile("relativeResolution.1"), "my.bundle"));
104
105     QQmlEngine engine;
106     engine.addNamedBundle("mybundle", testFile("relativeResolution.1/my.bundle"));
107
108     QQmlComponent component(&engine, QUrl("bundle://mybundle/test.qml"));
109     QVERIFY(component.isReady());
110
111     QObject *o = component.create();
112     QVERIFY(o != 0);
113
114     QCOMPARE(o->property("test1").toInt(), 11);
115     QCOMPARE(o->property("test2").toBool(), true);
116
117     delete o;
118     }
119
120     // Non-root of the bundle
121     {
122     QVERIFY(makeBundle(testFile("relativeResolution.2"), "my.bundle"));
123
124     QQmlEngine engine;
125     engine.addNamedBundle("mybundle", testFile("relativeResolution.2/my.bundle"));
126
127     QQmlComponent component(&engine, QUrl("bundle://mybundle/subdir/test.qml"));
128     QVERIFY(component.isReady());
129
130     QObject *o = component.create();
131     QVERIFY(o != 0);
132
133     QCOMPARE(o->property("test1").toInt(), 11);
134     QCOMPARE(o->property("test2").toBool(), true);
135
136     delete o;
137     }
138 }
139
140 // Test that a bundle can be imported explicitly from outside a bundle
141 void tst_qqmlbundle::bundleImport()
142 {
143     QVERIFY(makeBundle(testFile("bundleImport"), "my.bundle"));
144
145     QQmlEngine engine;
146     engine.addNamedBundle("mybundle", testFile("bundleImport/my.bundle"));
147
148     {
149     QQmlComponent component(&engine, testFileUrl("bundleImport/bundleImport.1.qml"));
150     QVERIFY(component.isReady());
151
152     QObject *o = component.create();
153     QVERIFY(o != 0);
154
155     QCOMPARE(o->property("test1").toReal(), qreal(1918));
156     QCOMPARE(o->property("test2").toString(), QString("Hello world!"));
157
158     delete o;
159     }
160
161     {
162     QQmlComponent component(&engine, testFileUrl("bundleImport/bundleImport.2.qml"));
163     QVERIFY(component.isReady());
164
165     QObject *o = component.create();
166     QVERIFY(o != 0);
167
168     QCOMPARE(o->property("test1").toReal(), qreal(1432));
169     QCOMPARE(o->property("test2").toString(), QString("Jeronimo"));
170
171     delete o;
172     }
173 }
174
175 // Test a relative import inside a bundle uses qmldir
176 void tst_qqmlbundle::relativeQmldir()
177 {
178     QVERIFY(makeBundle(testFile("relativeQmldir"), "my.bundle"));
179
180     QQmlEngine engine;
181     engine.addNamedBundle("mybundle", testFile("relativeQmldir/my.bundle"));
182
183     QQmlComponent component(&engine, QUrl("bundle://mybundle/test.qml"));
184     QVERIFY(component.isReady());
185
186     QObject *o = component.create();
187     QVERIFY(o != 0);
188
189     QCOMPARE(o->property("test1").toReal(), qreal(67));
190     QCOMPARE(o->property("test2").toReal(), qreal(88));
191
192     delete o;
193 }
194
195 // Test C++ plugins are resolved relative to the bundle container file
196 void tst_qqmlbundle::import()
197 {
198     QVERIFY(makeBundle(testFile("imports/bundletest"), "qmldir"));
199
200     QQmlEngine engine;
201     engine.addImportPath(testFile("imports"));
202
203     QQmlComponent component(&engine, testFileUrl("import.qml"));
204     QVERIFY(component.isReady());
205
206     QObject *o = component.create();
207     QVERIFY(o != 0);
208
209     QCOMPARE(o->property("value").toInt(), 32);
210
211     delete o;
212 }
213
214 // Transform the data available under <path>/bundledata to a bundle named <path>/<name>
215 bool tst_qqmlbundle::makeBundle(const QString &path, const QString &name)
216 {
217     QDir dir(path);
218     dir.remove(name);
219
220     QDir bundleDir = dir;
221     if (!bundleDir.cd("bundledata"))
222         return false;
223
224     QStringList fileNames = findFiles(bundleDir);
225
226     QString bundleFile = dir.absolutePath() + QDir::separator() + name;
227
228     QQmlBundle bundle(bundleFile);
229     if (!bundle.open(QFile::WriteOnly))
230         return false;
231
232     foreach (const QString &fileName, fileNames) {
233         QString shortFileName = fileName.mid(bundleDir.absolutePath().length() + 1);
234         bundle.add(shortFileName, fileName);
235     }
236
237     return true;
238 }
239
240 QStringList tst_qqmlbundle::findFiles(const QDir &d)
241 {
242     QStringList rv;
243
244     QStringList files = d.entryList(QDir::Files);
245     foreach (const QString &file, files)
246         rv << d.absoluteFilePath(file);
247
248     QStringList dirs = d.entryList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
249     foreach (const QString &dir, dirs) {
250         QDir sub = d;
251         sub.cd(dir);
252         rv << findFiles(sub);
253     }
254
255     return rv;
256 }
257
258 QTEST_MAIN(tst_qqmlbundle)
259
260 #include "tst_qqmlbundle.moc"