File indexing completed on 2024-05-12 05:40:52
0001 /*************************************************************************** 0002 * Copyright (C) 2018 by Renaud Guezennec * 0003 * http://renaudguezennec.homelinux.org/accueil,3.html * 0004 * * 0005 * Rolisteam is free software; you can redistribute it and/or modify * 0006 * it under the terms of the GNU General Public License as published by * 0007 * the Free Software Foundation; either version 2 of the License, or * 0008 * (at your option) any later version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, * 0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0013 * GNU General Public License for more details. * 0014 * * 0015 * You should have received a copy of the GNU General Public License * 0016 * along with this program; if not, write to the * 0017 * Free Software Foundation, Inc., * 0018 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0019 ***************************************************************************/ 0020 0021 #include <QAbstractItemModelTester> 0022 #include <QModelIndex> 0023 #include <QModelIndexList> 0024 #include <QtCore/QString> 0025 #include <QtTest/QtTest> 0026 #include <memory> 0027 0028 #include "model/musicmodel.h" 0029 0030 class TestMusicModel : public QObject 0031 { 0032 Q_OBJECT 0033 public: 0034 TestMusicModel(); 0035 0036 private slots: 0037 void init(); 0038 0039 void addTest(); 0040 void addTest_data(); 0041 0042 void insertTest(); 0043 void removeTest(); 0044 0045 private: 0046 std::unique_ptr<MusicModel> m_model; 0047 }; 0048 0049 TestMusicModel::TestMusicModel() {} 0050 0051 void TestMusicModel::init() 0052 { 0053 m_model.reset(new MusicModel()); 0054 new QAbstractItemModelTester(m_model.get()); 0055 } 0056 0057 void TestMusicModel::addTest() 0058 { 0059 QFETCH(QList<QUrl>, list); 0060 QFETCH(int, expected); 0061 0062 QVERIFY(m_model->rowCount() == 0); 0063 0064 m_model->addSong(list); 0065 0066 QCOMPARE(m_model->rowCount(), expected); 0067 } 0068 0069 void TestMusicModel::addTest_data() 0070 { 0071 QTest::addColumn<QList<QUrl>>("list"); 0072 QTest::addColumn<int>("expected"); 0073 0074 QTest::addRow("list1") << QList<QUrl>() << 0; 0075 QTest::addRow("list2") << QList<QUrl>({QUrl("song.mp3")}) << 1; 0076 QTest::addRow("list3") << QList<QUrl>({QUrl("song.mp3"), QUrl("song1.ogg")}) << 2; 0077 QTest::addRow("list3") << QList<QUrl>({QUrl("song.mp3"), QUrl("song1.ogg"), QUrl("song4.wav")}) << 3; 0078 } 0079 0080 void TestMusicModel::removeTest() 0081 { 0082 QList<QUrl> list; 0083 list << QUrl("song1.mp3") << QUrl("song2.ogg"); 0084 0085 m_model->addSong(list); 0086 0087 QCOMPARE(m_model->rowCount(), 2); 0088 0089 m_model->removeAll(); 0090 0091 QCOMPARE(m_model->rowCount(), 0); 0092 0093 m_model->addSong(list); 0094 0095 QCOMPARE(m_model->rowCount(), 2); 0096 0097 QModelIndexList indexList; 0098 auto index= m_model->index(0, 0); 0099 indexList << index; 0100 m_model->removeSong(indexList); 0101 0102 QCOMPARE(m_model->rowCount(), 1); 0103 0104 m_model->removeSong(indexList); 0105 0106 QCOMPARE(m_model->rowCount(), 0); 0107 indexList.clear(); 0108 0109 m_model->addSong(list); 0110 0111 QCOMPARE(m_model->rowCount(), 2); 0112 0113 index= m_model->index(0, 0); 0114 auto index1= m_model->index(1, 0); 0115 indexList << index << index1; 0116 m_model->removeSong(indexList); 0117 QCOMPARE(m_model->rowCount(), 0); 0118 } 0119 0120 void TestMusicModel::insertTest() 0121 { 0122 QList<QUrl> list; 0123 list << QUrl("/home/file/song1.mp3") << QUrl("/home/file/song2.ogg"); 0124 m_model->addSong(list); 0125 0126 QCOMPARE(m_model->rowCount(), 2); 0127 QString song("/home/file/songInserted.mp3"); 0128 m_model->insertSong(1, song); 0129 0130 QCOMPARE(m_model->rowCount(), 3); 0131 0132 auto index= m_model->index(1, 0); 0133 QCOMPARE(index.data().toString(), "songInserted.mp3"); 0134 } 0135 0136 QTEST_MAIN(TestMusicModel); 0137 0138 #include "tst_musicmodel.moc"