1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 // Lookup of libraries
43 // Test bundle as a qmldir
48 #include <QQmlComponent>
49 #include "../../shared/util.h"
50 #include <private/qqmlbundle_p.h>
52 class tst_qqmlbundle : public QQmlDataTest
61 void componentFromBundle();
62 void relativeResolution();
64 void relativeQmldir();
69 QStringList findFiles(const QDir &d);
70 bool makeBundle(const QString &path, const QString &name);
73 void tst_qqmlbundle::initTestCase()
75 QQmlDataTest::initTestCase();
78 // Test we create a QQmlComponent for a file inside a bundle
79 void tst_qqmlbundle::componentFromBundle()
81 QVERIFY(makeBundle(testFile("componentFromBundle"), "my.bundle"));
84 engine.addNamedBundle("mybundle", testFile("componentFromBundle/my.bundle"));
86 QQmlComponent component(&engine, QUrl("bundle://mybundle/test.qml"));
87 QVERIFY(component.isReady());
89 QObject *o = component.create();
92 QCOMPARE(o->property("test1").toInt(), 11);
93 QCOMPARE(o->property("test2").toBool(), true);
98 // Tests that relative QML components are resolved without a qmldir
99 void tst_qqmlbundle::relativeResolution()
101 // Root of the bundle
103 QVERIFY(makeBundle(testFile("relativeResolution.1"), "my.bundle"));
106 engine.addNamedBundle("mybundle", testFile("relativeResolution.1/my.bundle"));
108 QQmlComponent component(&engine, QUrl("bundle://mybundle/test.qml"));
109 QVERIFY(component.isReady());
111 QObject *o = component.create();
114 QCOMPARE(o->property("test1").toInt(), 11);
115 QCOMPARE(o->property("test2").toBool(), true);
120 // Non-root of the bundle
122 QVERIFY(makeBundle(testFile("relativeResolution.2"), "my.bundle"));
125 engine.addNamedBundle("mybundle", testFile("relativeResolution.2/my.bundle"));
127 QQmlComponent component(&engine, QUrl("bundle://mybundle/subdir/test.qml"));
128 QVERIFY(component.isReady());
130 QObject *o = component.create();
133 QCOMPARE(o->property("test1").toInt(), 11);
134 QCOMPARE(o->property("test2").toBool(), true);
140 // Test that a bundle can be imported explicitly from outside a bundle
141 void tst_qqmlbundle::bundleImport()
143 QVERIFY(makeBundle(testFile("bundleImport"), "my.bundle"));
146 engine.addNamedBundle("mybundle", testFile("bundleImport/my.bundle"));
149 QQmlComponent component(&engine, testFileUrl("bundleImport/bundleImport.1.qml"));
150 QVERIFY(component.isReady());
152 QObject *o = component.create();
155 QCOMPARE(o->property("test1").toReal(), qreal(1918));
156 QCOMPARE(o->property("test2").toString(), QString("Hello world!"));
162 QQmlComponent component(&engine, testFileUrl("bundleImport/bundleImport.2.qml"));
163 QVERIFY(component.isReady());
165 QObject *o = component.create();
168 QCOMPARE(o->property("test1").toReal(), qreal(1432));
169 QCOMPARE(o->property("test2").toString(), QString("Jeronimo"));
175 // Test a relative import inside a bundle uses qmldir
176 void tst_qqmlbundle::relativeQmldir()
178 QVERIFY(makeBundle(testFile("relativeQmldir"), "my.bundle"));
181 engine.addNamedBundle("mybundle", testFile("relativeQmldir/my.bundle"));
183 QQmlComponent component(&engine, QUrl("bundle://mybundle/test.qml"));
184 QVERIFY(component.isReady());
186 QObject *o = component.create();
189 QCOMPARE(o->property("test1").toReal(), qreal(67));
190 QCOMPARE(o->property("test2").toReal(), qreal(88));
195 // Test C++ plugins are resolved relative to the bundle container file
196 void tst_qqmlbundle::import()
198 QVERIFY(makeBundle(testFile("imports/bundletest"), "qmldir"));
201 engine.addImportPath(testFile("imports"));
203 QQmlComponent component(&engine, testFileUrl("import.qml"));
204 QVERIFY(component.isReady());
206 QObject *o = component.create();
209 QCOMPARE(o->property("value").toInt(), 32);
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)
220 QDir bundleDir = dir;
221 if (!bundleDir.cd("bundledata"))
224 QStringList fileNames = findFiles(bundleDir);
226 QString bundleFile = dir.absolutePath() + QDir::separator() + name;
228 QQmlBundle bundle(bundleFile);
229 if (!bundle.open(QFile::WriteOnly))
232 foreach (const QString &fileName, fileNames) {
233 QString shortFileName = fileName.mid(bundleDir.absolutePath().length() + 1);
234 bundle.add(shortFileName, fileName);
240 QStringList tst_qqmlbundle::findFiles(const QDir &d)
244 QStringList files = d.entryList(QDir::Files);
245 foreach (const QString &file, files)
246 rv << d.absoluteFilePath(file);
248 QStringList dirs = d.entryList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
249 foreach (const QString &dir, dirs) {
252 rv << findFiles(sub);
258 QTEST_MAIN(tst_qqmlbundle)
260 #include "tst_qqmlbundle.moc"