1/****************************************************************************
2**
3** Copyright (C) 2016 BlackBerry Limited. All rights reserved.
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 <QQmlEngine>
31#include <QQmlComponent>
32#include <QQmlFileSelector>
33#include <QQmlApplicationEngine>
34#include <QFileSelector>
35#include <QQmlContext>
36#include <QLoggingCategory>
37#include <qqmlinfo.h>
38#include "../../shared/util.h"
39
40class tst_qqmlfileselector : public QQmlDataTest
41{
42 Q_OBJECT
43public:
44 tst_qqmlfileselector() {}
45
46private slots:
47 void basicTest();
48 void basicTestCached();
49 void applicationEngineTest();
50
51};
52
53void tst_qqmlfileselector::basicTest()
54{
55 QQmlEngine engine;
56 QQmlFileSelector selector(&engine);
57 selector.setExtraSelectors(QStringList() << "basic");
58
59 QQmlComponent component(&engine, testFileUrl(fileName: "basicTest.qml"));
60 QObject *object = component.create();
61 QVERIFY(object != nullptr);
62 QCOMPARE(object->property("value").toString(), QString("selected"));
63
64 delete object;
65}
66
67void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &message)
68{
69 if (type == QtDebugMsg
70 && QByteArray(context.category) == QByteArray("qt.qml.diskcache")
71 && message.contains(s: "QML source file has moved to a different location.")) {
72 QFAIL(message.toUtf8());
73 }
74}
75
76void tst_qqmlfileselector::basicTestCached()
77{
78 basicTest(); // Seed the cache, in case basicTestCached() is run on its own
79 QtMessageHandler defaultHandler = qInstallMessageHandler(&messageHandler);
80 QLoggingCategory::setFilterRules("qt.qml.diskcache.debug=true");
81 basicTest(); // Run again and check that the file is in the cache now
82 QLoggingCategory::setFilterRules(QString());
83 qInstallMessageHandler(defaultHandler);
84}
85
86void tst_qqmlfileselector::applicationEngineTest()
87{
88 QQmlApplicationEngine engine;
89 QQmlFileSelector* selector = QQmlFileSelector::get(&engine);
90 QVERIFY(selector != nullptr);
91 selector->setExtraSelectors(QStringList() << "basic");
92
93 QQmlComponent component(&engine, testFileUrl(fileName: "basicTest.qml"));
94 QObject *object = component.create();
95 QVERIFY(object != nullptr);
96 QCOMPARE(object->property("value").toString(), QString("selected"));
97
98 delete object;
99}
100
101QTEST_MAIN(tst_qqmlfileselector)
102
103#include "tst_qqmlfileselector.moc"
104

source code of qtdeclarative/tests/auto/qml/qqmlfileselector/tst_qqmlfileselector.cpp