1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
|
/****************************************************************************************
* Copyright (c) 2011 Ralf Engels <ralf-engels@gmx.de> *
* *
* This program is free software; you can redistribute it and/or modify it under *
* the terms of the GNU General Public License as published by the Free Software *
* Foundation; either version 2 of the License, or (at your option) any later *
* version. *
* *
* This program 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 General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License along with *
* this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************************/
#include "TestDynamicModel.h"
#include "dynamic/Bias.h"
#include "dynamic/BiasedPlaylist.h"
#include "dynamic/DynamicModel.h"
#include "core/support/Amarok.h"
#include "core/support/Debug.h"
#include <QByteArray>
#include <QDataStream>
#include <QSignalSpy>
#include <KTempDir>
#include <qtest_kde.h>
Q_DECLARE_METATYPE(QModelIndex);
KTempDir *s_tmpDir = 0;
// We return a special saveLocation.
QString Amarok::saveLocation( const QString &directory )
{
return s_tmpDir->name() + directory;
}
QTEST_KDEMAIN( TestDynamicModel, GUI )
TestDynamicModel::TestDynamicModel()
{
qRegisterMetaType<QModelIndex>();
}
void
TestDynamicModel::init()
{
s_tmpDir = new KTempDir();
}
void
TestDynamicModel::cleanup()
{
delete s_tmpDir;
}
void
TestDynamicModel::testData()
{
Dynamic::DynamicModel *model = Dynamic::DynamicModel::instance();
// load from the empty directory
model->loadPlaylists();
// now we should have the four default playlists
QModelIndex playlistIndex = model->index( 0, 0 );
QCOMPARE( model->data( playlistIndex ).toString(), QString("Random") );
QCOMPARE( model->data( playlistIndex, Qt::EditRole ).toString(), QString("Random") );
QVERIFY( model->data( playlistIndex, Dynamic::DynamicModel::PlaylistRole ).isValid() );
QVERIFY( !model->data( playlistIndex, Dynamic::DynamicModel::BiasRole ).isValid() );
QModelIndex biasIndex = model->index( 0, 0, playlistIndex );
QVERIFY( !model->data( biasIndex ).toString().isEmpty() );
QVERIFY( !model->data( biasIndex, Dynamic::DynamicModel::PlaylistRole ).isValid() );
QVERIFY( model->data( biasIndex, Dynamic::DynamicModel::BiasRole ).isValid() );
}
void
TestDynamicModel::testPlaylistIndex()
{
Dynamic::DynamicModel *model = Dynamic::DynamicModel::instance();
// load from the empty directory
model->loadPlaylists();
// now we should have the four default playlists
QCOMPARE( model->rowCount(), 4 );
QCOMPARE( model->columnCount(), 1 );
// -- random playlist with one bias
QModelIndex playlistIndex = model->index( 0, 0 );
QModelIndex biasIndex = model->index( 0, 0, playlistIndex );
QCOMPARE( model->rowCount( playlistIndex ), 1 );
QCOMPARE( model->rowCount( biasIndex ), 0 );
QCOMPARE( playlistIndex.parent(), QModelIndex() );
QCOMPARE( biasIndex.parent(), playlistIndex );
// -- albumplay playlist with bias structure
playlistIndex = model->index( 2, 0 );
biasIndex = model->index( 0, 0, playlistIndex );
QModelIndex subBiasIndex = model->index( 1, 0, biasIndex );
QCOMPARE( model->rowCount( playlistIndex ), 1 );
QCOMPARE( model->rowCount( biasIndex ), 2 );
QCOMPARE( model->rowCount( subBiasIndex ), 0 );
QCOMPARE( playlistIndex.parent(), QModelIndex() );
QCOMPARE( biasIndex.parent(), playlistIndex );
QCOMPARE( subBiasIndex.parent(), biasIndex );
// and now the non-model index functions:
model->setActivePlaylist( 2 );
Dynamic::DynamicPlaylist* playlist = model->activePlaylist();
playlistIndex = model->index( model->activePlaylistIndex(), 0 );
QCOMPARE( model->index( playlist ), playlistIndex );
Dynamic::BiasPtr bias = qobject_cast<Dynamic::BiasedPlaylist*>(playlist)->bias();
biasIndex = model->index( 0, 0, playlistIndex );
QCOMPARE( model->index( bias ), biasIndex );
Dynamic::BiasPtr subBias = qobject_cast<Dynamic::AndBias*>(bias.data())->biases().at(0);
subBiasIndex = model->index( 0, 0, biasIndex );
QCOMPARE( model->index( subBias ), subBiasIndex );
}
void
TestDynamicModel::testSlots()
{
Dynamic::DynamicModel *model = Dynamic::DynamicModel::instance();
// load from the empty directory
model->loadPlaylists();
QSignalSpy spy1( model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)) );
QSignalSpy spy2( model, SIGNAL(rowsRemoved(QModelIndex,int,int)) );
QSignalSpy spy3( model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)) );
QSignalSpy spy4( model, SIGNAL(rowsInserted(QModelIndex,int,int)) );
// -- removeAt with playlist
QModelIndex playlistIndex = model->index( 1, 0 );
QString oldName = model->data( playlistIndex ).toString();
model->removeAt( playlistIndex );
QCOMPARE( spy1.count(), 1 );
QCOMPARE( spy3.count(), 0 );
QList<QVariant> args1 = spy1.takeFirst();
QVERIFY( args1.value(0).canConvert<QModelIndex>() );
QCOMPARE( args1.value(0).value<QModelIndex>(), QModelIndex() );
QCOMPARE( args1.value(1).toInt(), 1 );
QCOMPARE( args1.value(2).toInt(), 1 );
QCOMPARE( spy2.count(), 1 );
spy2.takeFirst();
// name should be different
playlistIndex = model->index( 1, 0 );
QVERIFY( model->data( playlistIndex ).toString() != oldName );
QCOMPARE( model->rowCount(), 3 );
// -- removeAt with bias
playlistIndex = model->index( 1, 0 );
QModelIndex biasIndex = model->index( 0, 0, playlistIndex );
QModelIndex subBiasIndex = model->index( 0, 0, biasIndex );
QCOMPARE( model->rowCount( biasIndex ), 2 );
model->removeAt( subBiasIndex );
QCOMPARE( spy1.count(), 1 );
QCOMPARE( spy3.count(), 0 );
args1 = spy1.takeFirst();
QCOMPARE( args1.count(), 3 );
QVERIFY( args1.value(0).canConvert<QModelIndex>() );
QCOMPARE( args1.value(0).value<QModelIndex>(), biasIndex );
QCOMPARE( args1.value(1).toInt(), 0 );
QCOMPARE( args1.value(2).toInt(), 0 );
QCOMPARE( spy2.count(), 1 );
spy2.takeFirst();
QCOMPARE( model->rowCount( biasIndex ), 1 );
QCOMPARE( model->rowCount(), 3 ); // only the bias was removed
// -- cloneAt with level 2 bias
playlistIndex = model->index( 1, 0 );
biasIndex = model->index( 0, 0, playlistIndex );
subBiasIndex = model->index( 0, 0, biasIndex );
QCOMPARE( model->rowCount( biasIndex ), 1 );
QModelIndex resultIndex = model->cloneAt(subBiasIndex);
QCOMPARE( resultIndex.row(), 1 );
QCOMPARE( resultIndex.parent(), biasIndex );
QCOMPARE( spy3.count(), 1 );
args1 = spy3.takeFirst();
QVERIFY( args1.value(0).canConvert<QModelIndex>() );
QCOMPARE( args1.value(0).value<QModelIndex>(), biasIndex );
QCOMPARE( args1.value(1).toInt(), 1 );
QCOMPARE( args1.value(2).toInt(), 1 );
QCOMPARE( spy4.count(), 1 );
spy4.takeFirst();
QCOMPARE( model->rowCount( biasIndex ), 2 );
QCOMPARE( model->rowCount(), 3 ); // only the bias was cloned
// -- newPlaylist
QCOMPARE( spy1.count(), 0 );
QCOMPARE( spy3.count(), 0 );
QCOMPARE( model->rowCount(), 3 );
resultIndex = model->newPlaylist();
QCOMPARE( model->rowCount(), 4 );
QCOMPARE( resultIndex.row(), 3 );
QCOMPARE( resultIndex.parent(), QModelIndex() );
QCOMPARE( spy1.count(), 0 );
QCOMPARE( spy3.count(), 1 );
args1 = spy3.takeFirst();
QVERIFY( args1.value(0).canConvert<QModelIndex>() );
QCOMPARE( args1.value(0).value<QModelIndex>(), QModelIndex() );
QCOMPARE( args1.value(1).toInt(), 3 );
QCOMPARE( args1.value(2).toInt(), 3 );
QCOMPARE( spy4.count(), 1 );
spy4.takeFirst();
// -- cloneAt with playlist
playlistIndex = model->index( 1, 0 );
QCOMPARE( model->rowCount(), 4 );
resultIndex = model->cloneAt(playlistIndex);
QCOMPARE( model->rowCount(), 5 );
QCOMPARE( resultIndex.row(), 4 );
QCOMPARE( resultIndex.parent(), QModelIndex() );
QCOMPARE( model->rowCount( resultIndex ), 1 );
QCOMPARE( spy3.count(), 1 );
args1 = spy3.takeFirst();
QVERIFY( args1.value(0).canConvert<QModelIndex>() );
QCOMPARE( args1.value(0).value<QModelIndex>(), QModelIndex() );
QCOMPARE( args1.value(1).toInt(), 4 );
QCOMPARE( args1.value(2).toInt(), 4 );
QCOMPARE( spy4.count(), 1 );
spy4.takeFirst();
}
QModelIndex
TestDynamicModel::serializeUnserialize( const QModelIndex& index )
{
Dynamic::DynamicModel *model = Dynamic::DynamicModel::instance();
QByteArray bytes;
QDataStream stream( &bytes, QIODevice::WriteOnly );
model->serializeIndex( &stream, index );
QDataStream stream2( &bytes, QIODevice::ReadOnly );
return model->unserializeIndex( &stream2 );
}
void
TestDynamicModel::testSerializeIndex()
{
Dynamic::DynamicModel *model = Dynamic::DynamicModel::instance();
// load from the empty directory
model->loadPlaylists();
QModelIndex playlistIndex = model->index( 2, 0 );
QModelIndex biasIndex = model->index( 0, 0, playlistIndex );
QModelIndex subBiasIndex = model->index( 0, 0, biasIndex );
QCOMPARE( QModelIndex(), serializeUnserialize( QModelIndex() ) );
QCOMPARE( biasIndex, serializeUnserialize( biasIndex ) );
QCOMPARE( subBiasIndex, serializeUnserialize( subBiasIndex ) );
}
void
TestDynamicModel::testDnD()
{
Dynamic::DynamicModel *model = Dynamic::DynamicModel::instance();
// load from the empty directory
model->loadPlaylists();
// -- copy a playlist
QModelIndex playlistIndex = model->index( 2, 0 );
QModelIndexList indexes;
indexes << playlistIndex;
int oldRowCount = model->rowCount();
QString oldName = model->data( playlistIndex ).toString();
QMimeData* data = model->mimeData( indexes );
QVERIFY( model->dropMimeData( data, Qt::CopyAction, 0, 0, QModelIndex() ) );
QCOMPARE( model->rowCount(), oldRowCount + 1 );
playlistIndex = model->index( 0, 0 );
QCOMPARE( oldName, model->data( playlistIndex ).toString() );
delete data;
// -- move a playlist (to the end)
playlistIndex = model->index( 0, 0 );
indexes.clear();
indexes << playlistIndex;
oldRowCount = model->rowCount();
oldName = model->data( playlistIndex ).toString();
data = model->mimeData( indexes );
QVERIFY( model->dropMimeData( data, Qt::MoveAction, oldRowCount, 0, QModelIndex() ) );
QCOMPARE( model->rowCount(), oldRowCount );
playlistIndex = model->index( oldRowCount - 1, 0 );
QCOMPARE( oldName, model->data( playlistIndex ).toString() );
delete data;
// -- copy a bias
// TODO
QModelIndex biasIndex = model->index( 0, 0, playlistIndex );
QModelIndex subBiasIndex = model->index( 0, 0, biasIndex );
}
void
TestDynamicModel::testRemoveActive()
{
Dynamic::DynamicModel *model = Dynamic::DynamicModel::instance();
// load from the empty directory
model->loadPlaylists();
QCOMPARE( model->rowCount(), 4 );
// -- try to remove the active playlist
model->setActivePlaylist( 2 );
QCOMPARE( model->activePlaylistIndex(), 2 );
Dynamic::DynamicPlaylist* pl = model->activePlaylist();
model->removeAt( model->index( pl ) );
QCOMPARE( model->rowCount(), 3 );
QVERIFY( model->activePlaylist() != pl );
// -- now remove all playlists remaining three playlists
model->removeAt( model->index( model->activePlaylist() ) );
model->removeAt( model->index( model->activePlaylist() ) );
model->removeAt( model->index( model->activePlaylist() ) );
QCOMPARE( model->rowCount(), 0 );
QCOMPARE( model->activePlaylist(), static_cast<Dynamic::DynamicPlaylist*>(0) );
}
#include "TestDynamicModel.moc"
|