File indexing completed on 2024-05-26 04:52:34

0001 /***************************************************************************
0002  *   Copyright (c) 2009 Sven Krohlas <sven@asbest-online.de>               *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; if not, write to the                         *
0016  *   Free Software Foundation, Inc.,                                       *
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
0018  ***************************************************************************/
0019 
0020 #include "TestMetaFileTrack.h"
0021 
0022 #include "config-amarok-test.h"
0023 #include "amarokconfig.h"
0024 
0025 #include <QTemporaryDir>
0026 
0027 #include <QTest>
0028 #include <QFile>
0029 #include <QDir>
0030 #include <QDateTime>
0031 #include <QFileInfo>
0032 
0033 
0034 QTEST_GUILESS_MAIN( TestMetaFileTrack )
0035 
0036 TestMetaFileTrack::TestMetaFileTrack()
0037     : m_tmpDir( nullptr )
0038 {}
0039 
0040 void TestMetaFileTrack::initTestCase()
0041 {
0042     AmarokConfig::instance("amarokrc");
0043 
0044     m_tmpDir = new QTemporaryDir();
0045     QVERIFY( m_tmpDir->isValid() );
0046     m_origTrackPath = QString( AMAROK_TEST_DIR ) + "/data/audio/Platz 01.mp3";
0047     QVERIFY( QFile::exists( m_origTrackPath ) );
0048 }
0049 
0050 void TestMetaFileTrack::cleanupTestCase()
0051 {
0052     delete m_tmpDir;
0053 }
0054 
0055 void TestMetaFileTrack::init()
0056 {
0057     static const QString tmpFileNameBase( "tempfile.mp3" );
0058     static int i = 0;
0059     // create new file name for every test: we need to start with clean statistics
0060     m_tmpFileName = QString( "%1%2-%3" ).arg( m_tmpDir->path() ).arg( i++ ).arg( tmpFileNameBase );
0061     QVERIFY( QFile::copy( m_origTrackPath, m_tmpFileName ) );
0062     m_track = new MetaFile::Track( QUrl::fromLocalFile(m_tmpFileName) );
0063     QVERIFY( m_track );
0064 }
0065 
0066 void TestMetaFileTrack::testNameAndSetTitle()
0067 {
0068     // why aren't those called set/getTitle?
0069     QCOMPARE( m_track->name(), QString( "Platz 01" ) );
0070 
0071     m_track->setTitle( "" );
0072     //when there is no title, we default to using the filename without extension
0073     QCOMPARE( m_track->name(), QString( "tempfile" ) );
0074 
0075     m_track->setTitle( "test" );
0076     QCOMPARE( m_track->name(), QString( "test" ) );
0077 
0078     m_track->setTitle( "Another Test" );
0079     QCOMPARE( m_track->name(), QString( "Another Test" ) );
0080 
0081     m_track->setTitle( "Some Umlauts: äöü" );
0082     QCOMPARE( m_track->name(), QString( "Some Umlauts: äöü" ) );
0083 
0084     m_track->setTitle( "Platz 01" );
0085     QCOMPARE( m_track->name(), QString( "Platz 01" ) );
0086 }
0087 
0088 void TestMetaFileTrack::testPrettyName()
0089 {
0090     QCOMPARE( m_track->prettyName(), QString( "Platz 01" ) );
0091 
0092     m_track->setTitle( "" );
0093     //when there is no title, we default to using the filename without extension
0094     QCOMPARE( m_track->prettyName(), QString( "tempfile" ) );
0095 
0096     m_track->setTitle( "test" );
0097     QCOMPARE( m_track->prettyName(), QString( "test" ) );
0098 
0099     m_track->setTitle( "Another Test" );
0100     QCOMPARE( m_track->prettyName(), QString( "Another Test" ) );
0101 
0102     m_track->setTitle( "Some Umlauts: äöü" );
0103     QCOMPARE( m_track->prettyName(), QString( "Some Umlauts: äöü" ) );
0104 
0105     m_track->setTitle( "Platz 01" );
0106     QCOMPARE( m_track->prettyName(), QString( "Platz 01" ) );
0107 }
0108 
0109 void TestMetaFileTrack::testSortableName()
0110 {
0111     QCOMPARE( m_track->sortableName(), QString( "Platz 01" ) );
0112 
0113     m_track->setTitle( "test" );
0114     QCOMPARE( m_track->sortableName(), QString( "test" ) );
0115 
0116     m_track->setTitle( "Another Test" );
0117     QCOMPARE( m_track->sortableName(), QString( "Another Test" ) );
0118 
0119     m_track->setTitle( "Some Umlauts: äöü" );
0120     QCOMPARE( m_track->sortableName(), QString( "Some Umlauts: äöü" ) );
0121 
0122     m_track->setTitle( "Platz 01" );
0123     QCOMPARE( m_track->sortableName(), QString( "Platz 01" ) );
0124 }
0125 
0126 void TestMetaFileTrack::testPlayableUrl()
0127 {
0128     const QUrl tempUrl = m_track->playableUrl();
0129     QCOMPARE( tempUrl.toLocalFile(), m_tmpFileName );
0130 }
0131 
0132 void TestMetaFileTrack::testPrettyUrl()
0133 {
0134     const QUrl tempUrl = QUrl::fromLocalFile(m_track->prettyUrl());
0135     QCOMPARE( tempUrl.toLocalFile(), m_tmpFileName );
0136 }
0137 
0138 void TestMetaFileTrack::testUidUrl()
0139 {
0140     QUrl tempUrl(m_track->uidUrl());
0141     QCOMPARE( tempUrl.toLocalFile(), m_tmpFileName );
0142 }
0143 
0144 void TestMetaFileTrack::testIsPlayable()
0145 {
0146     QVERIFY( m_track->isPlayable() );
0147 }
0148 
0149 void TestMetaFileTrack::testIsEditable()
0150 {
0151     QVERIFY( m_track->isEditable() );
0152 
0153     QFile testFile( m_tmpFileName );
0154 
0155     QVERIFY( testFile.setPermissions( {} ) );
0156     /* When the tests are run as root under Linux, the file is accessible even when it
0157      * has no permission bits set. Just skip one verify in this case in order not to
0158      * break whole test. */
0159     if( !QFileInfo( testFile ).isReadable() )
0160         QVERIFY( !m_track->isEditable() );
0161 
0162     QVERIFY( testFile.setPermissions( QFile::ReadOwner | QFile::WriteOwner ) );
0163     QVERIFY( m_track->isEditable() );
0164 }
0165 
0166 void TestMetaFileTrack::testSetGetAlbum()
0167 {
0168     QCOMPARE( m_track->album()->name(), QString( "" ) );
0169 
0170     m_track->setAlbum( "test" );
0171     QCOMPARE( m_track->album()->name(), QString( "test" ) );
0172 
0173     m_track->setAlbum( "Another Test" );
0174     QCOMPARE( m_track->album()->name(), QString( "Another Test" ) );
0175 
0176     m_track->setAlbum( "Some Umlauts: äöü" );
0177     QCOMPARE( m_track->album()->name(), QString( "Some Umlauts: äöü" ) );
0178 
0179     m_track->setAlbum( "" );
0180     QCOMPARE( m_track->album()->name(), QString( "" ) );
0181 }
0182 
0183 void TestMetaFileTrack::testSetGetArtist()
0184 {
0185     QCOMPARE( m_track->artist()->name(), QString( "Free Music Charts" ) );
0186 
0187     m_track->setArtist( "" );
0188     QCOMPARE( m_track->artist()->name(), QString( "" ) );
0189 
0190     m_track->setArtist( "test" );
0191     QCOMPARE( m_track->artist()->name(), QString( "test" ) );
0192 
0193     m_track->setArtist( "Another Test" );
0194     QCOMPARE( m_track->artist()->name(), QString( "Another Test" ) );
0195 
0196     m_track->setArtist( "Some Umlauts: äöü" );
0197     QCOMPARE( m_track->artist()->name(), QString( "Some Umlauts: äöü" ) );
0198 
0199     m_track->setArtist( "Free Music Charts" );
0200     QCOMPARE( m_track->artist()->name(), QString( "Free Music Charts" ) );
0201 }
0202 
0203 void TestMetaFileTrack::testSetGetGenre()
0204 {
0205     QCOMPARE( m_track->genre()->name(), QString( "Vocal" ) );
0206 
0207     m_track->setGenre( "rock" );
0208     QCOMPARE( m_track->genre()->name(), QString( "rock" ) );
0209 
0210     m_track->setGenre( "rock / pop" );
0211     QCOMPARE( m_track->genre()->name(), QString( "rock / pop" ) );
0212 
0213     m_track->setGenre( "Some Umlauts: äöü" );
0214     QCOMPARE( m_track->genre()->name(), QString( "Some Umlauts: äöü" ) );
0215 
0216     m_track->setGenre( "" );
0217     QCOMPARE( m_track->genre()->name(), QString( "" ) );
0218 
0219     m_track->setGenre( "28 Vocal" );
0220     QCOMPARE( m_track->genre()->name(), QString( "28 Vocal" ) );
0221 }
0222 
0223 void TestMetaFileTrack::testSetGetComposer()
0224 {
0225     QCOMPARE( m_track->composer()->name(), QString( "" ) );
0226 
0227     m_track->setComposer( "test" );
0228     QCOMPARE( m_track->composer()->name(), QString( "test" ) );
0229 
0230     m_track->setComposer( "Ludwig Van Beethoven" );
0231     QCOMPARE( m_track->composer()->name(), QString( "Ludwig Van Beethoven" ) );
0232 
0233     m_track->setComposer( "Georg Friedrich Händel" );
0234     QCOMPARE( m_track->composer()->name(), QString( "Georg Friedrich Händel" ) );
0235 
0236     m_track->setComposer( "" );
0237     QCOMPARE( m_track->composer()->name(), QString( "" ) );
0238 }
0239 
0240 void TestMetaFileTrack::testSetGetYear()
0241 {
0242     QCOMPARE( m_track->composer()->name(), QString( "" ) );
0243 
0244     m_track->setComposer( "test" );
0245     QCOMPARE( m_track->composer()->name(), QString( "test" ) );
0246 
0247     m_track->setComposer( "2009" );
0248     QCOMPARE( m_track->composer()->name(), QString( "2009" ) );
0249 
0250     m_track->setComposer( "1" );
0251     QCOMPARE( m_track->composer()->name(), QString( "1" ) );
0252 
0253     m_track->setComposer( "0" );
0254     QCOMPARE( m_track->composer()->name(), QString( "0" ) );
0255 
0256     m_track->setComposer( "-1" );
0257     QCOMPARE( m_track->composer()->name(), QString( "-1" ) );
0258 
0259     m_track->setComposer( "" );
0260     QCOMPARE( m_track->composer()->name(), QString( "" ) );
0261 }
0262 
0263 void TestMetaFileTrack::testSetGetComment()
0264 {
0265     QCOMPARE( m_track->comment(), QString( "" ) );
0266 
0267     m_track->setComment( "test" );
0268     QCOMPARE( m_track->comment(), QString( "test" ) );
0269 
0270     m_track->setComment( "2009" );
0271     QCOMPARE( m_track->comment(), QString( "2009" ) );
0272 
0273     m_track->setComment( QString::fromUtf8( "Some Umlauts: äöü" ) );
0274     QCOMPARE( m_track->comment(), QString::fromUtf8( "Some Umlauts: äöü" ) );
0275 
0276     m_track->setComment( "" );
0277     QCOMPARE( m_track->comment(), QString( "" ) );
0278 }
0279 
0280 void TestMetaFileTrack::testSetGetScore()
0281 {
0282     // try with write back enabled...
0283     AmarokConfig::setWriteBackStatistics( true );
0284 
0285     Meta::StatisticsPtr statistics = m_track->statistics();
0286     QCOMPARE( statistics->score(), 0.0 );
0287 
0288     /* now the code actually stores the score in track and then it reads it back.
0289      * the precision it uses is pretty low and it was failing the qFuzzyCompare<double>
0290      * Just make it use qFuzzyCompare<float>() */
0291 
0292     statistics->setScore( 3 );
0293     QCOMPARE( float( statistics->score() ), float( 3.0 ) );
0294 
0295     statistics->setScore( 12.55 );
0296     QCOMPARE( float( statistics->score() ), float( 12.55 ) );
0297 
0298     statistics->setScore( 100 );
0299     QCOMPARE( float( statistics->score() ), float( 100.0 ) );
0300 
0301     statistics->setScore( 0 );
0302     QCOMPARE( float( statistics->score() ), float( 0.0 ) );
0303 
0304     // and with writeback disabled
0305     AmarokConfig::setWriteBackStatistics( false );
0306 
0307     statistics->setScore( 3 );
0308     QCOMPARE( float( statistics->score() ), float( 0.0 ) );
0309 
0310     statistics->setScore( 12.55 );
0311     QCOMPARE( float( statistics->score() ), float( 0.0 ) );
0312 }
0313 
0314 void TestMetaFileTrack::testSetGetRating()
0315 {
0316     // try with write back enabled...
0317     AmarokConfig::setWriteBackStatistics( true );
0318 
0319     Meta::StatisticsPtr statistics = m_track->statistics();
0320     QCOMPARE( m_track->statistics()->rating(), 0 );
0321 
0322     m_track->statistics()->setRating( 1 );
0323     QCOMPARE( m_track->statistics()->rating(), 1 );
0324 
0325     m_track->statistics()->setRating( 23 );
0326     QCOMPARE( m_track->statistics()->rating(), 23 ); // should this be possible?
0327 
0328     m_track->statistics()->setRating( 0 );
0329     QCOMPARE( m_track->statistics()->rating(), 0 );
0330 
0331     // and with writeback disabled
0332     AmarokConfig::setWriteBackStatistics( false );
0333 
0334     m_track->statistics()->setRating( 1 );
0335     QCOMPARE( m_track->statistics()->rating(), 0 );
0336 
0337     m_track->statistics()->setRating( 23 );
0338     QCOMPARE( m_track->statistics()->rating(), 0 );
0339 }
0340 
0341 void TestMetaFileTrack::testSetGetTrackNumber()
0342 {
0343     QCOMPARE( m_track->trackNumber(), 0 );
0344 
0345     m_track->setTrackNumber( 1 );
0346     QCOMPARE( m_track->trackNumber(), 1 );
0347 
0348     m_track->setTrackNumber( 23 );
0349     QCOMPARE( m_track->trackNumber(), 23 );
0350 
0351     m_track->setTrackNumber( -12 );
0352     QCOMPARE( m_track->trackNumber(), -12 ); // should this be possible?
0353 
0354     m_track->setTrackNumber( 0 );
0355     QCOMPARE( m_track->trackNumber(), 0 );
0356 }
0357 
0358 void TestMetaFileTrack::testSetGetDiscNumber()
0359 {
0360     QCOMPARE( m_track->discNumber(), 0 );
0361 
0362     m_track->setDiscNumber( 1 );
0363     QCOMPARE( m_track->discNumber(), 1 );
0364 
0365     m_track->setDiscNumber( 23 );
0366     QCOMPARE( m_track->discNumber(), 23 );
0367 
0368     m_track->setDiscNumber( -12 );
0369     QCOMPARE( m_track->discNumber(), -12 ); // should this be possible?
0370 
0371     m_track->setDiscNumber( 0 );
0372     QCOMPARE( m_track->discNumber(), 0 );
0373 }
0374 
0375 void TestMetaFileTrack::testLength()
0376 {
0377     QCOMPARE( m_track->length(), 12000LL );
0378 }
0379 
0380 void TestMetaFileTrack::testFilesize()
0381 {
0382     QCOMPARE( m_track->filesize(), 389454 );
0383 }
0384 
0385 void TestMetaFileTrack::testSampleRate()
0386 {
0387     QCOMPARE( m_track->sampleRate(), 44100 );
0388 }
0389 
0390 void TestMetaFileTrack::testBitrate()
0391 {
0392     QCOMPARE( m_track->bitrate(), 257 );
0393 }
0394 
0395 void TestMetaFileTrack::testSetGetLastPlayed()
0396 {
0397     QSKIP( "lastPlayed reading/saving not (yet) available in MetaFile::Track", SkipAll );
0398     QCOMPARE( m_track->statistics()->lastPlayed(), QDateTime() );
0399 
0400     m_track->finishedPlaying( 1.0 );
0401     QVERIFY( m_track->statistics()->lastPlayed().isValid() );
0402 }
0403 
0404 void TestMetaFileTrack::testSetGetFirstPlayed()
0405 {
0406     QSKIP( "firstPlayed reading/saving not (yet) available in MetaFile::Track", SkipAll );
0407     QCOMPARE( m_track->statistics()->firstPlayed(), QDateTime() );
0408 
0409     m_track->finishedPlaying( 1.0 );
0410     QVERIFY( m_track->statistics()->firstPlayed().isValid() );
0411 }
0412 
0413 void TestMetaFileTrack::testSetGetPlayCount()
0414 {
0415     // try with write back enabled...
0416     AmarokConfig::setWriteBackStatistics( true );
0417 
0418     QCOMPARE( m_track->statistics()->playCount(), 0 );
0419 
0420     m_track->finishedPlaying( 1.0 );
0421     QCOMPARE( m_track->statistics()->playCount(), 1 );
0422 
0423     m_track->statistics()->setPlayCount( 0 );
0424     QCOMPARE( m_track->statistics()->playCount(), 0 );
0425 
0426     // and with writeback disabled
0427     AmarokConfig::setWriteBackStatistics( false );
0428 
0429     m_track->finishedPlaying( 1.0 );
0430     QCOMPARE( m_track->statistics()->playCount(), 0 );
0431 
0432     m_track->statistics()->setPlayCount( 12 );
0433     QCOMPARE( m_track->statistics()->playCount(), 0 );
0434 }
0435 
0436 void TestMetaFileTrack::testReplayGain()
0437 {
0438     QCOMPARE( int(m_track->replayGain( Meta::ReplayGain_Track_Gain ) * 1000), -6655 );
0439     QCOMPARE( int(m_track->replayGain( Meta::ReplayGain_Album_Gain ) * 1000), -6655 );
0440     QCOMPARE( int(m_track->replayGain( Meta::ReplayGain_Track_Peak ) * 10000), 41263 );
0441     QCOMPARE( int(m_track->replayGain( Meta::ReplayGain_Album_Peak ) * 10000), 41263 );
0442 }
0443 
0444 void TestMetaFileTrack::testType()
0445 {
0446     QCOMPARE( m_track->type(), QString( "mp3" ) );
0447 }
0448 
0449 void TestMetaFileTrack::testCreateDate()
0450 {
0451     QFileInfo fi( m_tmpFileName );
0452     QDateTime created = fi.birthTime();
0453     // m_track->createDate() is rounded to full second because it is created from full seconds
0454     // created therefore also needs to be rounded
0455     QCOMPARE( m_track->createDate().toSecsSinceEpoch(), created.toSecsSinceEpoch() );
0456 }