diff --git a/tests/SortFilterProxyModel.pro b/tests/SortFilterProxyModel.pro index 4488b3f..c206ceb 100644 --- a/tests/SortFilterProxyModel.pro +++ b/tests/SortFilterProxyModel.pro @@ -6,11 +6,13 @@ CONFIG += c++11 warn_on qmltestcase qml_debug no_keywords include(../SortFilterProxyModel.pri) HEADERS += \ - indexsorter.h + indexsorter.h \ + testroles.h SOURCES += \ tst_sortfilterproxymodel.cpp \ - indexsorter.cpp + indexsorter.cpp \ + testroles.cpp OTHER_FILES += \ tst_rangefilter.qml \ @@ -20,4 +22,5 @@ OTHER_FILES += \ tst_helpers.qml \ tst_builtins.qml \ tst_rolesorter.qml \ - tst_stringsorter.qml + tst_stringsorter.qml \ + tst_proxyroles.qml diff --git a/tests/testroles.cpp b/tests/testroles.cpp new file mode 100644 index 0000000..4cdc533 --- /dev/null +++ b/tests/testroles.cpp @@ -0,0 +1,37 @@ +#include "testroles.h" +#include + +QVariant StaticRole::value() const +{ + return m_value; +} + +void StaticRole::setValue(const QVariant& value) +{ + if (m_value == value) + return; + + m_value = value; + Q_EMIT valueChanged(); + invalidate(); +} + +QVariant StaticRole::data(const QModelIndex& sourceIndex, const qqsfpm::QQmlSortFilterProxyModel& proxyModel) +{ + Q_UNUSED(sourceIndex) + Q_UNUSED(proxyModel) + return m_value; +} + +QVariant SourceIndexRole::data(const QModelIndex& sourceIndex, const qqsfpm::QQmlSortFilterProxyModel& proxyModel) +{ + Q_UNUSED(proxyModel) + return sourceIndex.row(); +} + +void registerTestRolesTypes() { + qmlRegisterType("SortFilterProxyModel.Test", 0, 2, "StaticRole"); + qmlRegisterType("SortFilterProxyModel.Test", 0, 2, "SourceIndexRole"); +} + +Q_COREAPP_STARTUP_FUNCTION(registerTestRolesTypes) diff --git a/tests/testroles.h b/tests/testroles.h new file mode 100644 index 0000000..928f701 --- /dev/null +++ b/tests/testroles.h @@ -0,0 +1,35 @@ +#ifndef TESTROLES_H +#define TESTROLES_H + +#include "proxyrole.h" + +class StaticRole : public qqsfpm::ProxyRole +{ + Q_OBJECT + Q_PROPERTY(QVariant value READ value WRITE setValue NOTIFY valueChanged) +public: + using qqsfpm::ProxyRole::ProxyRole; + + QVariant value() const; + void setValue(const QVariant& value); + +Q_SIGNALS: + void valueChanged(); + +protected: + +private: + QVariant data(const QModelIndex& sourceIndex, const qqsfpm::QQmlSortFilterProxyModel& proxyModel) override; + QVariant m_value; +}; + +class SourceIndexRole : public qqsfpm::ProxyRole +{ +public: + using qqsfpm::ProxyRole::ProxyRole; + +private: + QVariant data(const QModelIndex& sourceIndex, const qqsfpm::QQmlSortFilterProxyModel& proxyModel) override; +}; + +#endif // TESTROLES_H diff --git a/tests/tst_proxyroles.qml b/tests/tst_proxyroles.qml new file mode 100644 index 0000000..5caca3c --- /dev/null +++ b/tests/tst_proxyroles.qml @@ -0,0 +1,58 @@ +import QtQuick 2.0 +import QtQml 2.2 +import QtTest 1.1 +import SortFilterProxyModel 0.2 +import SortFilterProxyModel.Test 0.2 +import QtQml 2.2 + +Item { + ListModel { + id: listModel + ListElement { test: "first"; keep: true } + ListElement { test: "second"; keep: true } + ListElement { test: "third"; keep: true } + } + + SortFilterProxyModel { + id: testModel + sourceModel: listModel + filters: ValueFilter { + roleName: "keep" + value: true + } + + proxyRoles: [ + StaticRole { + id: proxyRole + name: "staticRole" + value: "foo" + }, + SourceIndexRole { + name: "sourceIndexRole" + } + ] + } + + Instantiator { + id: instantiator + model: testModel + QtObject { + property string staticRole: model.staticRole + property int sourceIndexRole: model.sourceIndexRole + } + } + + TestCase { + name: "ProxyRoles" + + function test_proxyRole() { + compare(instantiator.object.staticRole, "foo"); + proxyRole.value = "bar"; + compare(instantiator.object.staticRole, "bar"); + compare(instantiator.object.sourceIndexRole, 0); + compare(testModel.get(1, "sourceIndexRole"), 1); + listModel.setProperty(1, "keep", false); + compare(testModel.get(1, "sourceIndexRole"), 2); + } + } +}