diff options
author | Richard Moe Gustavsen <richard.gustavsen@qt.io> | 2019-10-01 14:01:42 +0200 |
---|---|---|
committer | Mitch Curtis <mitch.curtis@qt.io> | 2019-10-14 11:15:11 +0200 |
commit | 67ef2583e481a37d51cb9d5d4d6046069559a754 (patch) | |
tree | 4a5e9f72664ba44ff13370f97e09c3f12e503377 /tests/auto/qml/qqmldelegatemodel | |
parent | ee62b2824fe91e95a3f8218b93ba55a2ef6660d0 (diff) |
VDMAbstractItemModelDataType: ensure meta-type is initialized
Since the delegate chooser can call value() on a delegate model
before the first call to createItem(), we need to ensure
that all structures (and in particular, m_rolenames) are
populated also in this case.
This is especially important for cases where value() is called
for every item in the model, as is the case for e.g. auto-completion
for combo boxes, as delegate creation is particularly expensive
there.
Task-number: QTBUG-78858
Change-Id: I66138d74839b9645d11c69c29f2e70264c65a68d
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'tests/auto/qml/qqmldelegatemodel')
5 files changed, 269 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmldelegatemodel/data/abstractItemModel.qml b/tests/auto/qml/qqmldelegatemodel/data/abstractItemModel.qml new file mode 100644 index 0000000000..01ebfd8e1e --- /dev/null +++ b/tests/auto/qml/qqmldelegatemodel/data/abstractItemModel.qml @@ -0,0 +1,37 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQml.Models 2.15 +import QtQuick 2.15 + +import Test 1.0 + +DelegateModel { + model: AbstractItemModel {} + delegate: Item {} +} diff --git a/tests/auto/qml/qqmldelegatemodel/data/integerModel.qml b/tests/auto/qml/qqmldelegatemodel/data/integerModel.qml new file mode 100644 index 0000000000..f6cd886e09 --- /dev/null +++ b/tests/auto/qml/qqmldelegatemodel/data/integerModel.qml @@ -0,0 +1,35 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQml.Models 2.15 +import QtQuick 2.15 + +DelegateModel { + model: 100 + delegate: Item {} +} diff --git a/tests/auto/qml/qqmldelegatemodel/data/listModel.qml b/tests/auto/qml/qqmldelegatemodel/data/listModel.qml new file mode 100644 index 0000000000..1ee8bcac72 --- /dev/null +++ b/tests/auto/qml/qqmldelegatemodel/data/listModel.qml @@ -0,0 +1,45 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQml.Models 2.15 +import QtQuick 2.15 + +DelegateModel { + model: ListModel { + ListElement { + name: "Item 0" + } + ListElement { + name: "Item 1" + } + ListElement { + name: "Item 2" + } + } + delegate: Item {} +} diff --git a/tests/auto/qml/qqmldelegatemodel/qqmldelegatemodel.pro b/tests/auto/qml/qqmldelegatemodel/qqmldelegatemodel.pro new file mode 100644 index 0000000000..7fdd3ab5f1 --- /dev/null +++ b/tests/auto/qml/qqmldelegatemodel/qqmldelegatemodel.pro @@ -0,0 +1,13 @@ +CONFIG += testcase +TARGET = tst_qqmldelegatemodel +macos:CONFIG -= app_bundle + +QT += qml testlib core-private qml-private qmlmodels-private + +SOURCES += tst_qqmldelegatemodel.cpp + +include (../../shared/util.pri) + +TESTDATA = data/* + +OTHER_FILES += data/* diff --git a/tests/auto/qml/qqmldelegatemodel/tst_qqmldelegatemodel.cpp b/tests/auto/qml/qqmldelegatemodel/tst_qqmldelegatemodel.cpp new file mode 100644 index 0000000000..87f42c0c8a --- /dev/null +++ b/tests/auto/qml/qqmldelegatemodel/tst_qqmldelegatemodel.cpp @@ -0,0 +1,139 @@ +/**************************************************************************** +** +** Copyright (C) 2019 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:GPL-EXCEPT$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3 as published by the Free Software +** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtTest/qtest.h> +#include <QtQml/qqmlcomponent.h> +#include <QtQmlModels/private/qqmldelegatemodel_p.h> + +#include "../../shared/util.h" + +class tst_QQmlDelegateModel : public QQmlDataTest +{ + Q_OBJECT + +public: + tst_QQmlDelegateModel(); + +private slots: + void valueWithoutCallingObjectFirst_data(); + void valueWithoutCallingObjectFirst(); +}; + +class AbstractItemModel : public QAbstractItemModel +{ + Q_OBJECT +public: + AbstractItemModel() + { + for (int i = 0; i < 3; ++i) + mValues.append(QString::fromLatin1("Item %1").arg(i)); + } + + QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override + { + if (parent.isValid()) + return QModelIndex(); + + return createIndex(row, column); + } + + QModelIndex parent(const QModelIndex &) const override + { + return QModelIndex(); + } + + int rowCount(const QModelIndex &parent = QModelIndex()) const override + { + if (parent.isValid()) + return 0; + + return mValues.count(); + } + + int columnCount(const QModelIndex &parent) const override + { + if (parent.isValid()) + return 0; + + return 1; + } + + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override + { + if (role != Qt::DisplayRole) + return QVariant(); + + return mValues.at(index.row()); + } + +private: + QVector<QString> mValues; +}; + +tst_QQmlDelegateModel::tst_QQmlDelegateModel() +{ + qmlRegisterType<AbstractItemModel>("Test", 1, 0, "AbstractItemModel"); +} + +void tst_QQmlDelegateModel::valueWithoutCallingObjectFirst_data() +{ + QTest::addColumn<QUrl>("qmlFileUrl"); + QTest::addColumn<int>("index"); + QTest::addColumn<QString>("role"); + QTest::addColumn<QVariant>("expectedValue"); + + QTest::addRow("integer") << testFileUrl("integerModel.qml") + << 50 << QString::fromLatin1("modelData") << QVariant(50); + QTest::addRow("ListModel") << testFileUrl("listModel.qml") + << 1 << QString::fromLatin1("name") << QVariant(QLatin1String("Item 1")); + QTest::addRow("QAbstractItemModel") << testFileUrl("abstractItemModel.qml") + << 1 << QString::fromLatin1("display") << QVariant(QLatin1String("Item 1")); +} + +// Tests that it's possible to call variantValue() without creating +// costly delegate items first via object(). +void tst_QQmlDelegateModel::valueWithoutCallingObjectFirst() +{ + QFETCH(const QUrl, qmlFileUrl); + QFETCH(const int, index); + QFETCH(const QString, role); + QFETCH(const QVariant, expectedValue); + + QQmlEngine engine; + QQmlComponent component(&engine); + component.loadUrl(qmlFileUrl); + QScopedPointer<QObject> root(component.create()); + QVERIFY2(root, qPrintable(component.errorString())); + QQmlDelegateModel *model = qobject_cast<QQmlDelegateModel*>(root.data()); + QVERIFY(model); + QCOMPARE(model->variantValue(index, role), expectedValue); +} + +QTEST_MAIN(tst_QQmlDelegateModel) + +#include "tst_qqmldelegatemodel.moc" |