From 0c9e6cf6c870bedc730fe22eabdc56bfec91c400 Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Wed, 3 Feb 2016 11:21:53 +0100 Subject: [PATCH] Add unit test for KModelIndexProxyMapper. --- autotests/CMakeLists.txt | 1 + autotests/kmodelindexproxymappertest.cpp | 105 +++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 autotests/kmodelindexproxymappertest.cpp diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index 731b83b..dc8d779 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -21,6 +21,7 @@ ecm_add_tests( klinkitemselectionmodeltest.cpp testmodelqueuedconnections.cpp kselectionproxymodeltest.cpp + kmodelindexproxymappertest.cpp krecursivefilterproxymodeltest.cpp krearrangecolumnsproxymodeltest.cpp LINK_LIBRARIES KF5::ItemModels Qt5::Test Qt5::Widgets proxymodeltestsuite diff --git a/autotests/kmodelindexproxymappertest.cpp b/autotests/kmodelindexproxymappertest.cpp new file mode 100644 index 0000000..8090ae1 --- /dev/null +++ b/autotests/kmodelindexproxymappertest.cpp @@ -0,0 +1,105 @@ +/* + Copyright (c) 2016 Ableton AG + Author Stephen Kelly + + This library is free software; you can redistribute it and/or modify it + under the terms of the GNU Library General Public License as published by + the Free Software Foundation; either version 2 of the License, or (at your + option) any later version. + + This library is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public + License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301, USA. +*/ + +#include +#include +#include +#include +#include + +#include "kmodelindexproxymapper.h" + +class ModelIndexProxyMapperTest : public QObject +{ + Q_OBJECT +private Q_SLOTS: + + void init(); + + void testIndexMapping(); + void testSelectionMapping(); + +private: + QStringListModel baseModel; + + QIdentityProxyModel proxy_common1; + + QIdentityProxyModel proxy_left1; + QIdentityProxyModel proxy_left2; + QIdentityProxyModel proxy_left3; + + QIdentityProxyModel proxy_right1; + QIdentityProxyModel proxy_right2; + QIdentityProxyModel proxy_right3; + QIdentityProxyModel proxy_right4; +}; + +void ModelIndexProxyMapperTest::init() +{ + baseModel.setStringList({"Monday", "Tuesday", "Wednesday"}); + + proxy_common1.setSourceModel(&baseModel); + + proxy_left1.setSourceModel(&proxy_common1); + proxy_left2.setSourceModel(&proxy_left1); + proxy_left3.setSourceModel(&proxy_left2); + + proxy_right1.setSourceModel(&proxy_common1); + proxy_right2.setSourceModel(&proxy_right1); + proxy_right3.setSourceModel(&proxy_right2); + proxy_right4.setSourceModel(&proxy_right3); +} + +void ModelIndexProxyMapperTest::testIndexMapping() +{ + KModelIndexProxyMapper mapper(&proxy_left3, &proxy_right4); + + auto leftIdx = proxy_left3.index(0, 0); + auto rightIdx = proxy_right4.index(0, 0); + + QVERIFY(leftIdx.isValid()); + QVERIFY(rightIdx.isValid()); + + QCOMPARE(mapper.mapLeftToRight(leftIdx), rightIdx); + QCOMPARE(mapper.mapRightToLeft(rightIdx), leftIdx); +} + +void ModelIndexProxyMapperTest::testSelectionMapping() +{ + KModelIndexProxyMapper mapper(&proxy_left3, &proxy_right4); + + auto leftIdx = proxy_left3.index(0, 0); + auto rightIdx = proxy_right4.index(0, 0); + + auto leftSel = QItemSelection(leftIdx, leftIdx); + auto rightSel = QItemSelection(rightIdx, rightIdx); + + QCOMPARE(leftSel.indexes().size(), 1); + QCOMPARE(rightSel.indexes().size(), 1); + + QVERIFY(leftSel.indexes().first().isValid()); + QVERIFY(rightSel.indexes().first().isValid()); + + QCOMPARE(mapper.mapSelectionLeftToRight(leftSel), rightSel); + QCOMPARE(mapper.mapSelectionRightToLeft(rightSel), leftSel); +} + +QTEST_MAIN(ModelIndexProxyMapperTest) +#include "kmodelindexproxymappertest.moc"