File indexing completed on 2024-04-21 04:50:17

0001 /*
0002     SPDX-FileCopyrightText: 2011 Michal Malek <michalm@jabster.pl>
0003     SPDX-FileCopyrightText: 1998-2009 Sebastian Trueg <trueg@k3b.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "k3bmetaitemmodeltest.h"
0009 #include "k3bmetaitemmodel.h"
0010 
0011 #include <QStringList>
0012 #include <QIcon>
0013 #include <QStandardItemModel>
0014 #include <QSignalSpy>
0015 #include <QTest>
0016 
0017 QTEST_GUILESS_MAIN( MetaItemModelTest )
0018 
0019 Q_DECLARE_METATYPE( QModelIndex )
0020 
0021 namespace
0022 {
0023     
0024     void checkForIdenticalData(
0025         QAbstractItemModel const& model1, QAbstractItemModel const& model2,
0026         QModelIndex root1 = QModelIndex(), QModelIndex root2 = QModelIndex() )
0027     {
0028         QCOMPARE( model1.rowCount( root1 ), model2.rowCount( root2 ) );
0029         
0030         for( int row = 0; row < model1.rowCount( root1 ); ++row )
0031         {
0032             QCOMPARE( model1.columnCount( root1 ), model2.columnCount( root2 ) );
0033             
0034             for( int col = 0; col < model1.columnCount( root1 ); ++col )
0035             {
0036                 const QModelIndex index1 = model1.index( row, col, root1 );
0037                 const QModelIndex index2 = model2.index( row, col, root2 );
0038                 QCOMPARE( model1.data( index1 ).toString(), model2.data( index2 ).toString() );
0039             }
0040             
0041             const QModelIndex newRoot1 = model1.index( row, 0, root1 );
0042             const QModelIndex newRoot2 = model2.index( row, 0, root2 );
0043             checkForIdenticalData( model1, model2, newRoot1, newRoot2 );
0044         }
0045     }
0046     
0047 } // namespace
0048 
0049 
0050 MetaItemModelTest::MetaItemModelTest()
0051 {
0052     qRegisterMetaType<QModelIndex>();
0053 }
0054 
0055 
0056 void MetaItemModelTest::init()
0057 {
0058     m_listModel = new QStandardItemModel( this );
0059     m_listModel->appendRow( new QStandardItem( "a" ) );
0060     m_listModel->appendRow( new QStandardItem( "b" ) );
0061     m_listModel->appendRow( new QStandardItem( "c" ) );
0062     m_listModel->appendRow( new QStandardItem( "d" ) );
0063     
0064     m_treeModel = new QStandardItemModel( this );
0065     m_treeModel->appendRow( new QStandardItem( QIcon::fromTheme( "edit-copy" ), "copy" ) );
0066     m_treeModel->appendRow( new QStandardItem( QIcon::fromTheme( "edit-paste" ), "paste" ) );
0067     m_treeModel->item( 0 )->appendRow( new QStandardItem( QIcon::fromTheme( "edit-cut" ), "cut" ) );
0068 }
0069 
0070 
0071 void MetaItemModelTest::testCreate()
0072 {
0073     K3b::MetaItemModel model;
0074 }
0075 
0076 
0077 void MetaItemModelTest::testAddSubModel()
0078 {
0079     K3b::MetaItemModel model;
0080     model.addSubModel( "stringList", QIcon::fromTheme( "configure" ), m_listModel );
0081     model.addSubModel( "standard", QIcon::fromTheme( "go-previous" ), m_treeModel );
0082     
0083     QCOMPARE( model.rowCount(), 2 );
0084     
0085     checkForIdenticalData( model, *m_listModel, model.index( 0, 0 ) );
0086     checkForIdenticalData( model, *m_treeModel, model.index( 1, 0 ) );
0087     
0088     QModelIndex firstIndex = model.index( 0, 0 );
0089     QVERIFY( firstIndex.isValid() );
0090     QCOMPARE( model.rowCount( firstIndex ), 4 );
0091     
0092     QModelIndex secondIndex = model.index( 1, 0 );
0093     QVERIFY( secondIndex.isValid() );
0094     QCOMPARE( model.rowCount( secondIndex ), 2 );
0095 }
0096 
0097 
0098 void MetaItemModelTest::testAddFlatSubModel()
0099 {
0100     K3b::MetaItemModel model;
0101     model.addSubModel( "stringList", QIcon::fromTheme( "configure" ), m_listModel, true );
0102     model.addSubModel( "standard", QIcon::fromTheme( "go-previous" ), m_treeModel, true );
0103     
0104     QCOMPARE( model.rowCount(), 6 );
0105     
0106     QModelIndex fifthIndex = model.index( 4, 0 );
0107     QVERIFY( fifthIndex.isValid() );
0108     QCOMPARE( model.rowCount( fifthIndex ), 1 );
0109 }
0110 
0111 
0112 void MetaItemModelTest::testRemoveSubModel()
0113 {
0114     K3b::MetaItemModel model;
0115     model.addSubModel( "stringList", QIcon::fromTheme( "configure" ), m_listModel );
0116     model.addSubModel( "standard", QIcon::fromTheme( "go-previous" ), m_treeModel, true );
0117     
0118     checkForIdenticalData( model, *m_listModel, model.index( 0, 0 ) );
0119     
0120     QCOMPARE( model.rowCount(), 3 );
0121     
0122     model.removeSubModel( m_listModel );
0123     QCOMPARE( model.rowCount(), 2 );
0124     
0125     model.removeSubModel( m_treeModel );
0126     QCOMPARE( model.rowCount(), 0 );
0127 }
0128 
0129 
0130 void MetaItemModelTest::testDynamicChanges()
0131 {
0132     K3b::MetaItemModel model;
0133     model.addSubModel( "stringList", QIcon::fromTheme( "configure" ), m_listModel );
0134     
0135     QSignalSpy rowsAboutToBeRemovedSpy( &model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)) );
0136     QSignalSpy rowsRemovedSpy( &model, SIGNAL(rowsRemoved(QModelIndex,int,int)) );
0137     QList<QVariant> arguments;
0138     
0139     QModelIndex firstIndex = model.index( 0, 0 );
0140     
0141     QCOMPARE( model.rowCount(), 1 );
0142     QCOMPARE( model.rowCount( firstIndex ), 4 ); 
0143     checkForIdenticalData( model, *m_listModel, firstIndex );
0144     
0145     m_listModel->removeRow( 2 );
0146     QCOMPARE( rowsAboutToBeRemovedSpy.count(), 1 );
0147     arguments = rowsAboutToBeRemovedSpy.takeFirst();
0148     QCOMPARE( arguments.count(), 3 );
0149     QCOMPARE( arguments.at( 0 ).value<QModelIndex>(), firstIndex );
0150     QCOMPARE( arguments.at( 1 ).value<int>(), 2 );
0151     QCOMPARE( arguments.at( 2 ).value<int>(), 2 );
0152     QCOMPARE( rowsRemovedSpy.count(), 1 );
0153     arguments = rowsRemovedSpy.takeFirst();
0154     QCOMPARE( arguments.count(), 3 );
0155     QCOMPARE( arguments.at( 0 ).value<QModelIndex>(), firstIndex );
0156     QCOMPARE( arguments.at( 1 ).value<int>(), 2 );
0157     QCOMPARE( arguments.at( 2 ).value<int>(), 2 );
0158     QCOMPARE( model.rowCount( firstIndex ), 3 );
0159     checkForIdenticalData( model, *m_listModel, firstIndex );
0160     
0161     model.addSubModel( "standard", QIcon::fromTheme( "go-previous" ), m_treeModel );
0162     QModelIndex secondIndex = model.index( 1, 0 );
0163     
0164     QCOMPARE( model.rowCount(), 2 );
0165     QCOMPARE( model.rowCount( secondIndex ), 2 );
0166     checkForIdenticalData( model, *m_listModel, firstIndex );
0167     
0168     m_treeModel->removeRow( 1 );
0169     QCOMPARE( rowsAboutToBeRemovedSpy.count(), 1 );
0170     arguments = rowsAboutToBeRemovedSpy.takeFirst();
0171     QCOMPARE( arguments.count(), 3 );
0172     QCOMPARE( arguments.at( 0 ).value<QModelIndex>(), secondIndex );
0173     QCOMPARE( arguments.at( 1 ).value<int>(), 1 );
0174     QCOMPARE( arguments.at( 2 ).value<int>(), 1 );
0175     QCOMPARE( rowsRemovedSpy.count(), 1 );
0176     arguments = rowsRemovedSpy.takeFirst();
0177     QCOMPARE( arguments.count(), 3 );
0178     QCOMPARE( arguments.at( 0 ).value<QModelIndex>(), secondIndex );
0179     QCOMPARE( arguments.at( 1 ).value<int>(), 1 );
0180     QCOMPARE( arguments.at( 2 ).value<int>(), 1 );
0181     QCOMPARE( model.rowCount( secondIndex ), 1 );
0182     
0183     model.removeSubModel( m_listModel );
0184     QCOMPARE( model.rowCount(), 1 );
0185 }
0186 
0187 
0188 void MetaItemModelTest::testDynamicChangesInFlatModel()
0189 {
0190     K3b::MetaItemModel model;
0191     model.addSubModel( "stringList", QIcon::fromTheme( "configure" ), m_listModel, true );
0192     QCOMPARE( model.rowCount(), 4 );
0193     checkForIdenticalData( model, *m_listModel );
0194     
0195     QSignalSpy rowsAboutToBeRemovedSpy( &model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)) );
0196     QSignalSpy rowsRemovedSpy( &model, SIGNAL(rowsRemoved(QModelIndex,int,int)) );
0197     QList<QVariant> arguments;
0198     
0199     m_listModel->removeRow( 2 );
0200     QCOMPARE( rowsAboutToBeRemovedSpy.count(), 1 );
0201     arguments = rowsAboutToBeRemovedSpy.takeFirst();
0202     QCOMPARE( arguments.count(), 3 );
0203     QCOMPARE( arguments.at( 0 ).value<QModelIndex>(), QModelIndex() );
0204     QCOMPARE( arguments.at( 1 ).value<int>(), 2 );
0205     QCOMPARE( arguments.at( 2 ).value<int>(), 2 );
0206     QCOMPARE( rowsRemovedSpy.count(), 1 );
0207     arguments = rowsRemovedSpy.takeFirst();
0208     QCOMPARE( arguments.count(), 3 );
0209     QCOMPARE( arguments.at( 0 ).value<QModelIndex>(), QModelIndex() );
0210     QCOMPARE( arguments.at( 1 ).value<int>(), 2 );
0211     QCOMPARE( arguments.at( 2 ).value<int>(), 2 );
0212     QCOMPARE( model.rowCount(), 3 );
0213     checkForIdenticalData( model, *m_listModel );
0214     
0215     model.addSubModel( "standard", QIcon::fromTheme( "go-previous" ), m_treeModel, true );
0216     QCOMPARE( model.rowCount(), 5 );
0217     
0218     m_treeModel->removeRow( 1 );
0219     QCOMPARE( rowsAboutToBeRemovedSpy.count(), 1 );
0220     arguments = rowsAboutToBeRemovedSpy.takeFirst();
0221     QCOMPARE( arguments.count(), 3 );
0222     QCOMPARE( arguments.at( 0 ).value<QModelIndex>(), QModelIndex() );
0223     QCOMPARE( arguments.at( 1 ).value<int>(), 4 );
0224     QCOMPARE( arguments.at( 2 ).value<int>(), 4 );
0225     QCOMPARE( rowsRemovedSpy.count(), 1 );
0226     arguments = rowsRemovedSpy.takeFirst();
0227     QCOMPARE( arguments.count(), 3 );
0228     QCOMPARE( arguments.at( 0 ).value<QModelIndex>(), QModelIndex() );
0229     QCOMPARE( arguments.at( 1 ).value<int>(), 4 );
0230     QCOMPARE( arguments.at( 2 ).value<int>(), 4 );
0231     QCOMPARE( model.rowCount(), 4 );
0232     
0233     model.removeSubModel( m_listModel );
0234     QCOMPARE( model.rowCount(), 1 );
0235 }
0236 
0237 
0238 void MetaItemModelTest::testDataChanges()
0239 {
0240     K3b::MetaItemModel model;
0241     model.addSubModel( "standard", QIcon::fromTheme( "go-previous" ), m_treeModel, true );
0242     checkForIdenticalData( model, *m_treeModel );
0243     
0244     QSignalSpy dataChangedSpy( &model, SIGNAL(dataChanged(QModelIndex,QModelIndex)) );
0245     m_treeModel->setData( m_treeModel->index( 1, 0 ), "somethingElse" );
0246     checkForIdenticalData( model, *m_treeModel );
0247     
0248     // Check if dataChanged() signal has been emitted
0249     QCOMPARE( dataChangedSpy.count(), 1 );
0250     QList<QVariant> arguments = dataChangedSpy.takeFirst();
0251     QCOMPARE( arguments.count(), 2 );
0252     
0253     QCOMPARE( arguments.at( 0 ).value<QModelIndex>(), model.index( 1, 0 ) );
0254     QCOMPARE( arguments.at( 1 ).value<QModelIndex>(), model.index( 1, 0 ) );
0255 }
0256 
0257 #include "moc_k3bmetaitemmodeltest.cpp"