1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29#include <qtest.h>
30#include <QObject>
31#include <QQmlEngine>
32#include <QQmlComponent>
33#include <QDebug>
34
35// Loading QML files from embedded resources is a common QML usecase.
36// This test just verifies that it works at a basic level, and with the qrc:/ syntax
37
38class tst_qrcqml : public QObject
39{
40 Q_OBJECT
41public:
42 tst_qrcqml();
43
44private slots:
45 void basicLoad_data();
46 void basicLoad();
47 void qrcImport_data();
48 void qrcImport();
49};
50
51tst_qrcqml::tst_qrcqml()
52{
53}
54
55void tst_qrcqml::basicLoad_data()
56{
57 QTest::addColumn<QString>(name: "url");
58 QTest::addColumn<QString>(name: "token");
59
60 QTest::newRow(dataTag: "simple")
61 << "qrc:/data/main.qml"
62 << "foo";
63
64 QTest::newRow(dataTag: "aliased")
65 << "qrc:/main.qml"
66 << "bar";
67
68 QTest::newRow(dataTag: "prefixed")
69 << "qrc:/search/main.qml"
70 << "baz";
71
72 /* This is not supported:
73 QTest::newRow("without qrc scheme")
74 << ":/data/main.qml"
75 << "hello";
76 */
77}
78
79void tst_qrcqml::basicLoad()
80{
81 QFETCH(QString, url);
82 QFETCH(QString, token);
83
84 QQmlEngine e;
85 QQmlComponent c(&e, QUrl(url));
86 QVERIFY(c.isReady());
87 QObject* o = c.create();
88 QVERIFY(o);
89 QCOMPARE(o->property("tokenProperty").toString(), token);
90 delete o;
91}
92
93void tst_qrcqml::qrcImport_data()
94{
95 QTest::addColumn<QString>(name: "importPath");
96 QTest::addColumn<QString>(name: "token");
97
98 QTest::newRow(dataTag: "qrc path")
99 << ":/imports"
100 << "foo";
101
102 QTest::newRow(dataTag: "qrc url")
103 << "qrc:/imports"
104 << "foo";
105}
106
107void tst_qrcqml::qrcImport()
108{
109 QFETCH(QString, importPath);
110 QFETCH(QString, token);
111
112 QQmlEngine e;
113 e.addImportPath(dir: importPath);
114 QQmlComponent c(&e, QUrl("qrc:///importtest.qml"));
115 QVERIFY(c.isReady());
116 QObject *o = c.create();
117 QVERIFY(o);
118 QCOMPARE(o->property("tokenProperty").toString(), token);
119 delete o;
120}
121
122QTEST_MAIN(tst_qrcqml)
123
124#include "tst_qrcqml.moc"
125

source code of qtdeclarative/tests/auto/qml/qrcqml/tst_qrcqml.cpp