Added tests to verify the core sorter functionnality

Issue #22 where a Sorter being disabled doesn't trigger a sort change is
exposed.
This commit is contained in:
Pierre-Yves Siret 2017-01-24 19:07:15 +01:00
parent a3d95d2290
commit 9a72c73226
4 changed files with 162 additions and 5 deletions

View File

@ -5,11 +5,15 @@ CONFIG += c++11 warn_on qmltestcase qml_debug
include(../SortFilterProxyModel.pri)
SOURCES += tst_sortfilterproxymodel.cpp
HEADERS += \
indexsorter.h
SOURCES += \
tst_sortfilterproxymodel.cpp \
indexsorter.cpp
OTHER_FILES += \
tst_rangefilter.qml \
tst_indexfilter.qml
DISTFILES += \
tst_sourceroles.qml
tst_indexfilter.qml \
tst_sourceroles.qml \
tst_sorters.qml

19
tests/indexsorter.cpp Normal file
View File

@ -0,0 +1,19 @@
#include "indexsorter.h"
#include <QtQml>
int IndexSorter::compare(const QModelIndex &sourceLeft, const QModelIndex &sourceRight) const
{
return sourceLeft.row() - sourceRight.row();
}
int ReverseIndexSorter::compare(const QModelIndex &sourceLeft, const QModelIndex &sourceRight) const
{
return sourceRight.row() - sourceLeft.row();
}
void registerIndexSorterTypes() {
qmlRegisterType<IndexSorter>("SortFilterProxyModel.Test", 0, 2, "IndexSorter");
qmlRegisterType<ReverseIndexSorter>("SortFilterProxyModel.Test", 0, 2, "ReverseIndexSorter");
}
Q_COREAPP_STARTUP_FUNCTION(registerIndexSorterTypes)

20
tests/indexsorter.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef INDEXSORTER_H
#define INDEXSORTER_H
#include <sorter.h>
class IndexSorter : public qqsfpm::Sorter
{
public:
using qqsfpm::Sorter::Sorter;
int compare(const QModelIndex& sourceLeft, const QModelIndex& sourceRight) const override;
};
class ReverseIndexSorter : public qqsfpm::Sorter
{
public:
using qqsfpm::Sorter::Sorter;
int compare(const QModelIndex& sourceLeft, const QModelIndex& sourceRight) const override;
};
#endif // INDEXSORTER_H

114
tests/tst_sorters.qml Normal file
View File

@ -0,0 +1,114 @@
import QtQuick 2.0
import QtQml 2.2
import QtTest 1.1
import SortFilterProxyModel 0.2
import SortFilterProxyModel.Test 0.2
Item {
ListModel {
id: listModel
ListElement { test: "first" }
ListElement { test: "second" }
ListElement { test: "third" }
ListElement { test: "fourth" }
}
property list<QtObject> sorters: [
QtObject {
property string tag: "no sorter"
property bool notASorter: true
property var expectedValues: ["first", "second", "third", "fourth"]
},
IndexSorter {
property string tag: "Dummy IndexSorter"
property var expectedValues: ["first", "second", "third", "fourth"]
},
ReverseIndexSorter {
property string tag: "Dummy ReverseIndexSorter"
property var expectedValues: ["fourth", "third", "second", "first"]
},
IndexSorter {
property string tag: "Disabled dummy IndexSorter"
enabled: false
property var expectedValues: ["first", "second", "third", "fourth"]
},
ReverseIndexSorter {
property string tag: "Disabled dummy ReverseIndexSorter"
enabled: false
property var expectedValues: ["first", "second", "third", "fourth"]
},
IndexSorter {
property string tag: "Descending dummy IndexSorter"
ascendingOrder: false
property var expectedValues: ["fourth", "third", "second", "first"]
},
ReverseIndexSorter {
property string tag: "Descending dummy ReverseIndexSorter"
ascendingOrder: false
property var expectedValues: ["first", "second", "third", "fourth"]
},
IndexSorter {
property string tag: "Disabled descending dummy IndexSorter"
enabled: false
ascendingOrder: false
property var expectedValues: ["first", "second", "third", "fourth"]
},
ReverseIndexSorter {
property string tag: "Disabled descending dummy ReverseIndexSorter"
enabled: false
ascendingOrder: false
property var expectedValues: ["first", "second", "third", "fourth"]
}
]
ReverseIndexSorter {
id: reverseIndexSorter
}
SortFilterProxyModel {
id: testModel
sourceModel: listModel
}
TestCase {
name: "SortersTests"
function test_indexOrder_data() {
return sorters;
}
function test_indexOrder(sorter) {
testModel.sorters = sorter;
verifyModelValues(testModel, sorter.expectedValues);
}
function test_enablingSorter() {
reverseIndexSorter.enabled = false;
testModel.sorters = reverseIndexSorter;
var expectedValuesBeforeEnabling = ["first", "second", "third", "fourth"];
var expectedValuesAfterEnabling = ["fourth", "third", "second", "first"];
verifyModelValues(testModel, expectedValuesBeforeEnabling);
reverseIndexSorter.enabled = true;
verifyModelValues(testModel, expectedValuesAfterEnabling);
}
function test_disablingSorter() {
reverseIndexSorter.enabled = true;
testModel.sorters = reverseIndexSorter;
var expectedValuesBeforeDisabling = ["fourth", "third", "second", "first"];
var expectedValuesAfterDisabling = ["first", "second", "third", "fourth"];
verifyModelValues(testModel, expectedValuesBeforeDisabling);
reverseIndexSorter.enabled = false;
verifyModelValues(testModel, expectedValuesAfterDisabling);
}
function verifyModelValues(model, expectedValues) {
verify(model.count === expectedValues.length,
"Expected count " + expectedValues.length + ", actual count: " + model.count);
for (var i = 0; i < model.count; i++)
{
var modelValue = model.data(model.index(i, 0));
verify(modelValue === expectedValues[i],
"Expected testModel value " + expectedValues[i] + ", actual: " + modelValue);
}
}
}
}