File: tst_qqmlfileselector.cpp

package info (click to toggle)
qt6-declarative 6.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 308,920 kB
  • sloc: cpp: 775,911; javascript: 514,247; xml: 10,855; python: 2,806; ansic: 2,253; java: 810; sh: 262; makefile: 41; php: 27
file content (109 lines) | stat: -rw-r--r-- 3,793 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (C) 2016 BlackBerry Limited. All rights reserved.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include <qtest.h>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QQmlFileSelector>
#include <QQmlApplicationEngine>
#include <QFileSelector>
#include <QQmlContext>
#include <QLoggingCategory>
#include <qqmlinfo.h>
#include <QtQuickTestUtils/private/qmlutils_p.h>

class tst_qqmlfileselector : public QQmlDataTest
{
    Q_OBJECT
public:
    tst_qqmlfileselector() : QQmlDataTest(QT_QMLTEST_DATADIR) {}

private slots:
    void basicTest();
    void basicTestCached();
    void applicationEngineTest();
    void qmldirCompatibility();
};

void tst_qqmlfileselector::basicTest()
{
    QQmlEngine engine;
    QQmlFileSelector selector(&engine);
    selector.setExtraSelectors(QStringList() << "basic");

    QQmlComponent component(&engine, testFileUrl("basicTest.qml"));
    std::unique_ptr<QObject> object { component.create() };
    QVERIFY(object.get() != nullptr);
    QCOMPARE(object->property("value").toString(), QString("selected"));
}

void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &message)
{
    if (type == QtDebugMsg
            && QByteArray(context.category) == QByteArray("qt.qml.diskcache")
            && message.contains("QML source file has moved to a different location.")) {
        QFAIL(message.toUtf8());
    }
}

void tst_qqmlfileselector::basicTestCached()
{
    basicTest(); // Seed the cache, in case basicTestCached() is run on its own
    QtMessageHandler defaultHandler = qInstallMessageHandler(&messageHandler);
    QLoggingCategory::setFilterRules("qt.qml.diskcache.debug=true");
    basicTest(); // Run again and check that the file is in the cache now
    QLoggingCategory::setFilterRules(QString());
    qInstallMessageHandler(defaultHandler);
}

void tst_qqmlfileselector::applicationEngineTest()
{
    QQmlApplicationEngine engine;
    engine.setExtraFileSelectors(QStringList() << "basic");
    engine.load(testFileUrl("basicTest.qml"));

    QVERIFY(!engine.rootObjects().isEmpty());
    QObject *object = engine.rootObjects().at(0);
    QVERIFY(object != nullptr);
    QCOMPARE(object->property("value").toString(), QString("selected"));
}

void tst_qqmlfileselector::qmldirCompatibility()
{
    {
        // No error for multiple files with different selectors, and the matching one is chosen
        // for +macos and +linux selectors.
        QQmlApplicationEngine engine;
        engine.addImportPath(dataDirectory());
        engine.load(testFileUrl("qmldirtest/main.qml"));
        QVERIFY(!engine.rootObjects().isEmpty());
        QObject *object = engine.rootObjects().at(0);
        auto color = object->property("color").value<QColor>();
#if defined(Q_OS_LINUX) && !defined(Q_OS_ANDROID)
        QCOMPARE(object->objectName(), "linux");
        QCOMPARE(color, QColorConstants::Svg::blue);
#elif defined(Q_OS_DARWIN)
        QCOMPARE(object->objectName(), "macos");
        QCOMPARE(color, QColorConstants::Svg::yellow);
#else
        QCOMPARE(object->objectName(), "base");
        QCOMPARE(color, QColorConstants::Svg::green);
#endif
    }

    {
        // If nothing matches, the _base_ file is chosen, not the first or the last one.
        // This also holds when using the implicit import.
        QQmlApplicationEngine engine;
        engine.addImportPath(dataDirectory());
        engine.load(testFileUrl("qmldirtest2/main.qml"));
        QVERIFY(!engine.rootObjects().isEmpty());
        QObject *object = engine.rootObjects().at(0);
        QCOMPARE(object->property("color").value<QColor>(), QColorConstants::Svg::green);
        QCOMPARE(object->objectName(), "base");
    }
}

QTEST_MAIN(tst_qqmlfileselector)

#include "tst_qqmlfileselector.moc"