File indexing completed on 2022-10-04 15:34:12

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 "k3bdataprojectmodeltest.h"
0009 #include "k3bdatadoc.h"
0010 #include "k3bdataprojectmodel.h"
0011 #include "k3bdataitem.h"
0012 #include "k3bdiritem.h"
0013 #include "k3bspecialdataitem.h"
0014 #include "k3btestutils.h"
0015 
0016 #include <QSignalSpy>
0017 #include <QTest>
0018 
0019 QTEST_GUILESS_MAIN( DataProjectModelTest )
0020 
0021 Q_DECLARE_METATYPE( QModelIndex )
0022 
0023 DataProjectModelTest::DataProjectModelTest()
0024 {
0025     qRegisterMetaType<QModelIndex>();
0026 }
0027 
0028 
0029 void DataProjectModelTest::init()
0030 {
0031     m_doc = new K3b::DataDoc;
0032     m_doc->newDocument();
0033     m_doc->root()->addDataItem( new K3b::DirItem( "First directory" ) ); // index 0
0034     m_doc->root()->addDataItem( new K3b::SpecialDataItem( 1024, "file1" ) ); // index 1
0035     m_doc->root()->addDataItem( new K3b::SpecialDataItem( 2048, "file2" ) ); // index 2
0036     K3b::DirItem* secondDirectory = new K3b::DirItem( "Second directory" );
0037     m_doc->root()->addDataItem( secondDirectory ); // index 3
0038     secondDirectory->addDataItem( new K3b::SpecialDataItem( 1024, "file1" ) ); // index 3 -> 0
0039     secondDirectory->addDataItem( new K3b::SpecialDataItem( 512, "file2" ) ); // index 3 -> 1
0040     m_doc->root()->addDataItem( new K3b::SpecialDataItem( 300, "file3" ) ); // index 4
0041     m_doc->root()->addDataItem( new K3b::SpecialDataItem( 400, "file4" ) ); // index 5
0042 }
0043 
0044 
0045 void DataProjectModelTest::cleanp()
0046 {
0047     m_doc->deleteLater();
0048 }
0049 
0050 
0051 void DataProjectModelTest::testCreate()
0052 {
0053     K3b::DataProjectModel model( m_doc );
0054 }
0055 
0056 
0057 void DataProjectModelTest::testAdd()
0058 {
0059     K3b::DataProjectModel model( m_doc );
0060     TestUtils::InsertRemoveModelSpy spy( &model,
0061                                          SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
0062                                          SIGNAL(rowsInserted(QModelIndex,int,int)) );
0063 
0064     m_doc->addEmptyDir( "Third directory", m_doc->root() );
0065     spy.check( model.indexForItem( m_doc->root() ), 6 );
0066 }
0067 
0068 
0069 void DataProjectModelTest::testRemove()
0070 {
0071     QVariantList args;
0072     K3b::DataProjectModel model( m_doc );
0073     TestUtils::InsertRemoveModelSpy spy( &model,
0074                                          SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
0075                                          SIGNAL(rowsRemoved(QModelIndex,int,int)) );
0076 
0077     m_doc->root()->removeDataItems( 4, 1 );
0078     spy.check( model.indexForItem( m_doc->root() ), 4 );
0079 
0080     K3b::DirItem* dir = dynamic_cast<K3b::DirItem*>( m_doc->root()->children().at( 3 ) );
0081     QVERIFY( dir != 0 );
0082     dir->removeDataItems( 0, 1 );
0083     spy.check( model.indexForItem( dir ), 0 );
0084 
0085     m_doc->root()->removeDataItems( 3, 1 );
0086     spy.check( model.indexForItem( m_doc->root() ), 3 );
0087 }
0088 
0089