Warning, file /multimedia/k3b/tests/k3bmodelutilstest.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 "k3bmodelutilstest.h"
0009 #include "k3bmodelutils.h"
0010 
0011 #include <QStandardItemModel>
0012 #include <QTest>
0013 
0014 QTEST_GUILESS_MAIN( ModelUtilsTest )
0015 
0016 ModelUtilsTest::ModelUtilsTest()
0017 {
0018 }
0019 
0020 void ModelUtilsTest::testCommonCheckState()
0021 {
0022     QStandardItemModel* listModel = new QStandardItemModel( this );
0023     listModel->appendRow( new QStandardItem );
0024     listModel->appendRow( new QStandardItem );
0025     listModel->appendRow( new QStandardItem );
0026     listModel->appendRow( new QStandardItem );
0027     
0028     QModelIndexList indexes;
0029     for( int i = 0; i < listModel->rowCount(); ++i )
0030         indexes.push_back( listModel->index( i, 0 ) );
0031     
0032     QCOMPARE( K3b::ModelUtils::commonCheckState( indexes ), Qt::Unchecked );
0033     
0034     listModel->item( 2 )->setCheckState( Qt::Checked );
0035     QCOMPARE( K3b::ModelUtils::commonCheckState( indexes ), Qt::PartiallyChecked );
0036     
0037     listModel->item( 0 )->setCheckState( Qt::Checked );
0038     listModel->item( 1 )->setCheckState( Qt::Checked );
0039     listModel->item( 3 )->setCheckState( Qt::Checked );
0040     QCOMPARE( K3b::ModelUtils::commonCheckState( indexes ), Qt::Checked );
0041 }
0042 
0043 void ModelUtilsTest::testToggleCommonCheckState()
0044 {
0045     QStandardItemModel* listModel = new QStandardItemModel( this );
0046     listModel->appendRow( new QStandardItem );
0047     listModel->appendRow( new QStandardItem );
0048     listModel->appendRow( new QStandardItem );
0049     listModel->appendRow( new QStandardItem );
0050     
0051     for( int i = 0; i < listModel->rowCount(); ++i )
0052         QCOMPARE( listModel->item( i )->checkState(), Qt::Unchecked );
0053     
0054     QModelIndexList indexes;
0055     for( int i = 0; i < listModel->rowCount(); ++i )
0056         indexes.push_back( listModel->index( i, 0 ) );
0057     
0058     K3b::ModelUtils::toggleCommonCheckState( listModel, indexes );
0059     for( int i = 0; i < listModel->rowCount(); ++i )
0060         QCOMPARE( listModel->item( i )->checkState(), Qt::Checked );
0061     
0062     K3b::ModelUtils::toggleCommonCheckState( listModel, indexes );
0063     for( int i = 0; i < listModel->rowCount(); ++i )
0064         QCOMPARE( listModel->item( i )->checkState(), Qt::Unchecked );
0065     
0066     listModel->item( 2 )->setCheckState( Qt::Checked );
0067     K3b::ModelUtils::toggleCommonCheckState( listModel, indexes );
0068     for( int i = 0; i < listModel->rowCount(); ++i )
0069         QCOMPARE( listModel->item( i )->checkState(), Qt::Checked );
0070     
0071     K3b::ModelUtils::toggleCommonCheckState( listModel, indexes );
0072     for( int i = 0; i < listModel->rowCount(); ++i )
0073         QCOMPARE( listModel->item( i )->checkState(), Qt::Unchecked );
0074     
0075     listModel->item( 2 )->setCheckState( Qt::PartiallyChecked );
0076     K3b::ModelUtils::toggleCommonCheckState( listModel, indexes );
0077     for( int i = 0; i < listModel->rowCount(); ++i )
0078         QCOMPARE( listModel->item( i )->checkState(), Qt::Checked );
0079 }
0080 
0081 void ModelUtilsTest::testCommonText()
0082 {
0083     QStandardItemModel* listModel = new QStandardItemModel( this );
0084     listModel->appendRow( new QStandardItem );
0085     listModel->appendRow( new QStandardItem );
0086     listModel->appendRow( new QStandardItem );
0087     listModel->appendRow( new QStandardItem );
0088     
0089     QModelIndexList indexes;
0090     for( int i = 0; i < listModel->rowCount(); ++i )
0091         indexes.push_back( listModel->index( i, 0 ) );
0092     
0093     QCOMPARE( K3b::ModelUtils::commonText( indexes ), QString() );
0094     
0095     listModel->item( 2 )->setText( "a" );
0096     QCOMPARE( K3b::ModelUtils::commonText( indexes ), QString() );
0097     
0098     listModel->item( 0 )->setText( "a" );
0099     listModel->item( 1 )->setText( "a" );
0100     listModel->item( 3 )->setText( "a" );
0101     QCOMPARE( K3b::ModelUtils::commonText( indexes ), QString( "a" ) );
0102     
0103     listModel->item( 1 )->setText( "b" );
0104     QCOMPARE( K3b::ModelUtils::commonText( indexes ), QString() );
0105 }
0106 
0107 void ModelUtilsTest::testSetCommonText()
0108 {
0109     QStandardItemModel* listModel = new QStandardItemModel( this );
0110     listModel->appendRow( new QStandardItem );
0111     listModel->appendRow( new QStandardItem );
0112     listModel->appendRow( new QStandardItem );
0113     listModel->appendRow( new QStandardItem );
0114     
0115     for( int i = 0; i < listModel->rowCount(); ++i )
0116         QCOMPARE( listModel->item( i )->text(), QString() );
0117     
0118     QModelIndexList indexes;
0119     for( int i = 0; i < listModel->rowCount(); ++i )
0120         indexes.push_back( listModel->index( i, 0 ) );
0121     
0122     K3b::ModelUtils::setCommonText( listModel, indexes, "a" );
0123     for( int i = 0; i < listModel->rowCount(); ++i )
0124         QCOMPARE( listModel->item( i )->text(), QString( "a" ) );
0125     
0126     K3b::ModelUtils::setCommonText( listModel, indexes, QString() );
0127     for( int i = 0; i < listModel->rowCount(); ++i )
0128         QCOMPARE( listModel->item( i )->text(), QString( "a" ) );
0129     
0130     listModel->item( 2 )->setText( "b" );
0131     K3b::ModelUtils::setCommonText( listModel, indexes, QString() );
0132     QCOMPARE( listModel->item( 2 )->text(), QString( "b" ) );
0133 }
0134 
0135 #include "moc_k3bmodelutilstest.cpp"