File indexing completed on 2024-05-12 04:49:40

0001 /****************************************************************************************
0002  * Copyright (c) 2009,2010 Maximilian Kossick <maximilian.kossick@googlemail.com>       *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "CollectionLocationTest.h"
0018 
0019 #include "core/support/Components.h"
0020 #include "core/collections/CollectionLocation.h"
0021 #include "core/support/Debug.h"
0022 #include "core/meta/support/MetaConstants.h"
0023 #include "../tests/mocks/MetaMock.h"
0024 #include "MockCollectionLocationDelegate.h"
0025 
0026 #include <QMutex>
0027 #include <QSignalSpy>
0028 #include <QVariantMap>
0029 
0030 #include <gmock/gmock.h>
0031 
0032 QTEST_GUILESS_MAIN( CollectionLocationTest )
0033 
0034 using ::testing::Return;
0035 using ::testing::AnyNumber;
0036 using ::testing::_;
0037 
0038 CollectionLocationTest::CollectionLocationTest()
0039 {
0040     qRegisterMetaType<Meta::TrackList>();
0041     qRegisterMetaType<Meta::AlbumList>();
0042     qRegisterMetaType<Meta::ArtistList>();
0043 }
0044 
0045 namespace Collections {
0046 
0047 class TestRemoveCL : public CollectionLocation
0048 {
0049 public:
0050 
0051     void removeUrlsFromCollection( const Meta::TrackList &tracks ) override
0052     {
0053         count += tracks.count();
0054         slotRemoveOperationFinished();
0055     }
0056 
0057     MOCK_CONST_METHOD0( isWritable, bool() );
0058     MOCK_CONST_METHOD0( isOrganizable, bool() );
0059 
0060     int count;
0061 };
0062 
0063 } //namespace Collections
0064 
0065 void CollectionLocationTest::testSuccessfulCopy()
0066 {
0067     Collections::MockCollectionLocationDelegate *cld = new Collections::MockCollectionLocationDelegate();
0068     EXPECT_CALL( *cld, reallyDelete( _, _) ).Times( AnyNumber() ).WillRepeatedly( Return( true ) );
0069     Amarok::Components::setCollectionLocationDelegate( cld );
0070 
0071     Collections::TestRemoveCL *cl = new Collections::TestRemoveCL();
0072     QSignalSpy spy( cl, &Collections::TestRemoveCL::destroyed );
0073     EXPECT_CALL( *cl, isWritable() ).Times( AnyNumber() ).WillRepeatedly( Return( true ) );
0074     cl->setProperty( "removeSources", true );
0075     cl->count = 0;
0076     QVariantMap map;
0077     map.insert( Meta::Field::URL,  QUrl("file:///IDoNotExist.mp3") );
0078     Meta::TrackPtr file1( new MetaMock( map ) );
0079     cl->transferSuccessful( file1 );
0080 
0081     QVERIFY2( cl->metaObject()->invokeMethod( cl, "slotFinishCopy", Qt::DirectConnection ), "Calling slot failed" );
0082     QCOMPARE( cl->count, 1 );
0083     QVERIFY( spy.wait( 5000 ) );
0084     delete Amarok::Components::setCollectionLocationDelegate( nullptr );
0085 }
0086 
0087 void CollectionLocationTest::testFailedCopy()
0088 {
0089     Collections::MockCollectionLocationDelegate *cld = new Collections::MockCollectionLocationDelegate();
0090     EXPECT_CALL( *cld, reallyDelete( _, _) ).Times( AnyNumber() ).WillRepeatedly( Return( true ) );
0091     EXPECT_CALL( *cld, errorDeleting( _, _ ) ).Times( AnyNumber() );
0092     Amarok::Components::setCollectionLocationDelegate( cld );
0093 
0094     Collections::TestRemoveCL *cl = new Collections::TestRemoveCL();
0095     EXPECT_CALL( *cl, isWritable() ).Times( AnyNumber() ).WillRepeatedly( Return( true ) );
0096     QSignalSpy spy( cl, &Collections::TestRemoveCL::destroyed );
0097     cl->setProperty( "removeSources", true );
0098     cl->count = 0;
0099     QVariantMap map;
0100     map.insert( Meta::Field::URL,  QUrl("file:///IDoNotExist.mp3") );
0101     Meta::TrackPtr file1( new MetaMock( map ) );
0102     cl->transferError( file1, "Test of CollectionLocation" );
0103 
0104     QVERIFY2( cl->metaObject()->invokeMethod( cl, "slotFinishCopy", Qt::DirectConnection ), "Calling slot failed"  );
0105     QCOMPARE( cl->count, 0 );
0106     QVERIFY( spy.wait( 5000 ) );
0107     delete Amarok::Components::setCollectionLocationDelegate( nullptr );
0108 }
0109 
0110 void CollectionLocationTest::testCopyMultipleTracks()
0111 {
0112     Collections::MockCollectionLocationDelegate *cld = new Collections::MockCollectionLocationDelegate();
0113     EXPECT_CALL( *cld, reallyDelete( _, _) ).Times( AnyNumber() ).WillRepeatedly( Return( true ) );
0114     EXPECT_CALL( *cld, errorDeleting( _, _ ) ).Times( AnyNumber() );
0115     Amarok::Components::setCollectionLocationDelegate( cld );
0116 
0117     Collections::TestRemoveCL *cl = new Collections::TestRemoveCL();
0118     QSignalSpy spy( cl, &Collections::TestRemoveCL::destroyed );
0119     EXPECT_CALL( *cl, isWritable() ).Times( AnyNumber() ).WillRepeatedly( Return( true ) );
0120 
0121     cl->setProperty( "removeSources", true );
0122     cl->count = 0;
0123     QVariantMap map;
0124     map.insert( Meta::Field::URL,  QUrl("file:///IDoNotExist.mp3") );
0125     Meta::TrackPtr file1( new MetaMock( map ) );
0126     map.insert( Meta::Field::URL, QUrl("file:///IDoNotExistAsWell.mp3") );
0127     Meta::TrackPtr file2( new MetaMock( map )  );
0128     map.insert( Meta::Field::URL, QUrl("file:///IDoNotExistAsWell.mp3") );
0129     Meta::TrackPtr file3( new MetaMock( map ) );
0130     cl->transferError( file1, "Test of CollectionLocation" );
0131     cl->transferSuccessful( file2 );
0132     cl->transferSuccessful( file3 );
0133 
0134     cl->metaObject()->invokeMethod( cl, "slotFinishCopy", Qt::DirectConnection );
0135     QCOMPARE( cl->count, 2 );
0136     QVERIFY( spy.wait( 5000 ) );
0137     delete Amarok::Components::setCollectionLocationDelegate( nullptr );
0138 }
0139 
0140 void CollectionLocationTest::testFailedCopyWithIncorrectUsageOfCopySuccesful()
0141 {
0142     Collections::MockCollectionLocationDelegate *cld = new Collections::MockCollectionLocationDelegate();
0143     EXPECT_CALL( *cld, reallyDelete( _, _) ).Times( AnyNumber() ).WillRepeatedly( Return( true ) );
0144     EXPECT_CALL( *cld, errorDeleting( _, _ ) ).Times( AnyNumber() );
0145     Amarok::Components::setCollectionLocationDelegate( cld );
0146 
0147     Collections::TestRemoveCL *cl = new Collections::TestRemoveCL();
0148     EXPECT_CALL( *cl, isWritable() ).Times( AnyNumber() ).WillRepeatedly( Return( true ) );
0149     QSignalSpy spy1( cl, &Collections::TestRemoveCL::destroyed );
0150     cl->setProperty( "removeSources", true );
0151     cl->count = 0;
0152     QVariantMap map;
0153     map.insert( Meta::Field::URL,  QUrl("file:///IDoNotExist.mp3") );
0154     Meta::TrackPtr file1( new MetaMock( map ) );
0155     cl->transferError( file1, "Test of CollectionLocation" );
0156     cl->transferSuccessful( file1 );
0157 
0158     cl->metaObject()->invokeMethod( cl, "slotFinishCopy", Qt::DirectConnection );
0159     QVERIFY2( cl->count == 0, "Expected no call to remove");
0160 
0161     QVERIFY( spy1.wait( 5000 ) );
0162     delete Amarok::Components::setCollectionLocationDelegate( nullptr );
0163 
0164     cld = new Collections::MockCollectionLocationDelegate();
0165     EXPECT_CALL( *cld, reallyDelete( _, _) ).Times( AnyNumber() ).WillRepeatedly( Return( true ) );
0166     EXPECT_CALL( *cld, errorDeleting( _, _ ) ).Times( AnyNumber() );
0167     Amarok::Components::setCollectionLocationDelegate( cld );
0168 
0169     cl = new Collections::TestRemoveCL();
0170     EXPECT_CALL( *cl, isWritable() ).Times( AnyNumber() ).WillRepeatedly( Return( true ) );
0171     QSignalSpy spy2( cl, &Collections::TestRemoveCL::destroyed );
0172     cl->setProperty( "removeSources", true );
0173     cl->count = 0;
0174     file1 = Meta::TrackPtr( new MetaMock( map ) );
0175     cl->transferSuccessful( file1 );
0176     cl->transferError( file1, "Test of CollectionLocation" );
0177 
0178     cl->metaObject()->invokeMethod( cl, "slotFinishCopy", Qt::DirectConnection );
0179     QVERIFY2( cl->count == 0, "Expected no call to remove after reversed method call");
0180     QVERIFY( spy2.wait( 5000 ) );
0181     delete Amarok::Components::setCollectionLocationDelegate( nullptr );
0182 }
0183 
0184