test: Add tests for the proxy role functionality

This commit is contained in:
Grecko 2017-09-16 16:50:39 +02:00
parent 400f453d91
commit 48ac2bfcd7
4 changed files with 136 additions and 3 deletions

View File

@ -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

37
tests/testroles.cpp Normal file
View File

@ -0,0 +1,37 @@
#include "testroles.h"
#include <QtQml>
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<StaticRole>("SortFilterProxyModel.Test", 0, 2, "StaticRole");
qmlRegisterType<SourceIndexRole>("SortFilterProxyModel.Test", 0, 2, "SourceIndexRole");
}
Q_COREAPP_STARTUP_FUNCTION(registerTestRolesTypes)

35
tests/testroles.h Normal file
View File

@ -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

58
tests/tst_proxyroles.qml Normal file
View File

@ -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);
}
}
}