mpengine/tsrc/unittest_mpsongscanner/src/unittest_mpsongscanner.cpp
changeset 41 ea59c434026a
equal deleted inserted replaced
32:c163ef0b758d 41:ea59c434026a
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Unit test for MpSongScanner
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QSignalSpy>
       
    19 
       
    20 #include "stub/inc/mpmpxharvesterframeworkwrapper.h"
       
    21 #include "unittest_mpsongscanner.h"
       
    22 
       
    23 
       
    24 // Do this so we can access all member variables.
       
    25 #define private public
       
    26 #define protected public
       
    27 #include "mpsongscanner.h"
       
    28 #undef protected
       
    29 #undef private
       
    30 
       
    31 
       
    32 /*!
       
    33  Make our test case a stand-alone executable that runs all the test functions.
       
    34  */
       
    35 int main(int argc, char *argv[])
       
    36 {
       
    37     QApplication app(argc, argv);
       
    38     TestMpSongScanner tv;
       
    39 
       
    40     if ( argc > 1 ) {
       
    41         return QTest::qExec( &tv, argc, argv);
       
    42     }
       
    43     else {
       
    44         char *pass[3];
       
    45         pass[0] = argv[0];
       
    46         pass[1] = "-o";
       
    47         pass[2] = "c:\\data\\unittest_mpsongscanner.txt";
       
    48 
       
    49         return QTest::qExec(&tv, 3, pass);
       
    50     }
       
    51 }
       
    52 
       
    53 TestMpSongScanner::TestMpSongScanner()
       
    54     : mTest( 0 ),
       
    55       mHarvesterWrapper( 0 )
       
    56 {
       
    57 }
       
    58 
       
    59 TestMpSongScanner::~TestMpSongScanner()
       
    60 {
       
    61     delete mTest;
       
    62     delete mHarvesterWrapper;
       
    63 }
       
    64 
       
    65 /*!
       
    66  Called before the first testfunction is executed.
       
    67  */
       
    68 void TestMpSongScanner::initTestCase()
       
    69 {
       
    70 }
       
    71 
       
    72 /*!
       
    73  Called after the last testfunction was executed.
       
    74  */
       
    75 void TestMpSongScanner::cleanupTestCase()
       
    76 {
       
    77 }
       
    78 
       
    79 /*!
       
    80  Called before each testfunction is executed.
       
    81  */
       
    82 void TestMpSongScanner::init()
       
    83 {
       
    84     qRegisterMetaType<MpxDiskEvents>("MpxDiskEvents");
       
    85     mHarvesterWrapper = new MpMpxHarvesterFrameworkWrapper();
       
    86     mTest = new MpSongScanner( mHarvesterWrapper );
       
    87 }
       
    88 
       
    89 /*!
       
    90  Called after every testfunction.
       
    91  */
       
    92 void TestMpSongScanner::cleanup()
       
    93 {
       
    94     delete mTest;
       
    95     mTest = 0;
       
    96     delete mHarvesterWrapper;
       
    97     mHarvesterWrapper = 0;
       
    98 }
       
    99 
       
   100 /*!
       
   101  Tests constructor.
       
   102 */
       
   103 void TestMpSongScanner::testConstructor()
       
   104 {
       
   105     QVERIFY( mTest->mMpxHarvesterWrapper );
       
   106     QVERIFY( !mTest->mScanning );
       
   107     QVERIFY( mTest->mAutomaticScan );
       
   108 }
       
   109 
       
   110 /*!
       
   111  testScan.
       
   112  */
       
   113 void TestMpSongScanner::testScan()
       
   114 {
       
   115     // Manual Scan
       
   116     mTest->scan( false );
       
   117     QVERIFY( mTest->mScanning );
       
   118     QVERIFY( !mTest->mAutomaticScan );
       
   119     QVERIFY( mTest->mMpxHarvesterWrapper->mScanRequested );
       
   120 
       
   121     // Automatic Scan
       
   122     cleanup();
       
   123     init();
       
   124     mTest->scan( true );
       
   125     QVERIFY( mTest->mScanning );
       
   126     QVERIFY( mTest->mAutomaticScan );
       
   127     QVERIFY( mTest->mMpxHarvesterWrapper->mScanRequested );
       
   128 }
       
   129 
       
   130 /*!
       
   131  testIsAutomaticScan.
       
   132  */
       
   133 void TestMpSongScanner::testIsAutomaticScan()
       
   134 {
       
   135     mTest->mAutomaticScan = true;
       
   136     QVERIFY( mTest->isAutomaticScan() );
       
   137     mTest->mAutomaticScan = false;
       
   138     QVERIFY( !mTest->isAutomaticScan() );
       
   139 }
       
   140 
       
   141 /*!
       
   142  testCancelScan
       
   143  */
       
   144 void TestMpSongScanner::testCancelScan( )
       
   145 {
       
   146     mTest->scan( true );
       
   147     mTest->cancelScan();
       
   148     QVERIFY( !mTest->mScanning );
       
   149     QVERIFY( !mTest->mMpxHarvesterWrapper->mScanRequested );
       
   150 }
       
   151 
       
   152 /*!
       
   153  testScanStartedSignal
       
   154  */
       
   155 void TestMpSongScanner::testScanStartedSignal( )
       
   156 {
       
   157     QSignalSpy spy( mTest, SIGNAL( scanStarted() ) );
       
   158     mTest->scan( true );
       
   159     mTest->mMpxHarvesterWrapper->emitScanStarted();
       
   160     QTest::qWait(500);
       
   161     QCOMPARE( spy.count(), 1 );
       
   162 }
       
   163 
       
   164 /*!
       
   165  testHandleScanEnded
       
   166  */
       
   167 void TestMpSongScanner::testHandleScanEnded( )
       
   168 {
       
   169     int itemsAdded( -1 );
       
   170     int error( KErrNone );
       
   171 
       
   172     // Scand endend with error KErrDiskFull 1/4
       
   173     {
       
   174     QSignalSpy spyScanFinished( mTest, SIGNAL( scanFinished( int, int ) ) );
       
   175     itemsAdded = 10;
       
   176     error = KErrDiskFull;
       
   177     mTest->scan( true );
       
   178     mTest->mMpxHarvesterWrapper->emitScanEnded( itemsAdded, error );
       
   179     QTest::qWait(500);
       
   180     QCOMPARE( spyScanFinished.count(), 1 );
       
   181     QCOMPARE( spyScanFinished.at(0).at(0), QVariant( MpSongScanner::ScanErrorDiskFull ) );
       
   182     QCOMPARE( spyScanFinished.at(0).at(1), QVariant( 0 ) );
       
   183     QVERIFY( !mTest->mScanning );
       
   184     }
       
   185 
       
   186     // Any other error 2/4
       
   187     {
       
   188     cleanup();
       
   189     init();
       
   190     QSignalSpy spyScanFinished( mTest, SIGNAL( scanFinished( int, int ) ) );
       
   191     itemsAdded = 10;
       
   192     error = KErrUnknown;
       
   193     mTest->scan( true );
       
   194     mTest->mMpxHarvesterWrapper->emitScanEnded( itemsAdded, error );
       
   195     QTest::qWait(500);
       
   196     QCOMPARE( spyScanFinished.count(), 1 );
       
   197     QCOMPARE( spyScanFinished.at(0).at(0), QVariant( MpSongScanner::ScanGeneralError ) );
       
   198     QCOMPARE( spyScanFinished.at(0).at(1), QVariant( itemsAdded ) );
       
   199     QVERIFY( !mTest->mScanning );
       
   200     }
       
   201 
       
   202     // Normal scan finish (No error, no cancel) 3/4
       
   203     {
       
   204     cleanup();
       
   205     init();
       
   206     QSignalSpy spyScanFinished( mTest, SIGNAL( scanFinished( int, int ) ) );
       
   207     itemsAdded = 20;
       
   208     error = KErrNone;
       
   209     mTest->scan( true );
       
   210     mTest->mMpxHarvesterWrapper->emitScanEnded( itemsAdded, error );
       
   211     QTest::qWait(500);
       
   212     QCOMPARE( spyScanFinished.count(), 1 );
       
   213     QCOMPARE( spyScanFinished.at(0).at(0), QVariant( MpSongScanner::ScanErrorNone ) );
       
   214     QCOMPARE( spyScanFinished.at(0).at(1), QVariant( itemsAdded ) );
       
   215     QVERIFY( !mTest->mScanning );
       
   216     }
       
   217 
       
   218     // Scan canceled 4/4
       
   219     {
       
   220     cleanup();
       
   221     init();
       
   222     QSignalSpy spyScanFinished( mTest, SIGNAL( scanFinished( int, int ) ) );
       
   223     itemsAdded = 20;
       
   224     error = KErrNone;
       
   225     mTest->scan( true );
       
   226     mTest->cancelScan();
       
   227     mTest->mMpxHarvesterWrapper->emitScanEnded( itemsAdded, error );
       
   228     QTest::qWait(500);
       
   229     QCOMPARE( spyScanFinished.count(), 1 );
       
   230     QCOMPARE( spyScanFinished.at(0).at(0), QVariant( MpSongScanner::ScanGeneralError ) );
       
   231     QCOMPARE( spyScanFinished.at(0).at(1), QVariant( itemsAdded ) );
       
   232     QVERIFY( !mTest->mScanning );
       
   233     }
       
   234 }
       
   235 
       
   236 /*!
       
   237  testScanCountChangedSignal
       
   238  */
       
   239 void TestMpSongScanner::testScanCountChangedSignal()
       
   240 {
       
   241     int itemsAdded = 15;
       
   242     QSignalSpy spy( mTest, SIGNAL( scanCountChanged( int ) ) );
       
   243     mTest->scan( true );
       
   244     mTest->mMpxHarvesterWrapper->emitScanCountChanged( itemsAdded );
       
   245     QTest::qWait(500);
       
   246     QCOMPARE( spy.count(), 1 );
       
   247     QCOMPARE( spy.at(0).at(0), QVariant( itemsAdded ) );
       
   248 }
       
   249 
       
   250 /*!
       
   251  testHandleDiskEvent
       
   252  */
       
   253 void TestMpSongScanner::testHandleDiskEvent()
       
   254 {
       
   255     QSignalSpy spyScanFinished( mTest, SIGNAL( scanFinished( int, int ) ) );
       
   256     mTest->scan( true );
       
   257     mTest->mMpxHarvesterWrapper->emitDiskEvent();
       
   258     QTest::qWait(500);
       
   259     QCOMPARE( spyScanFinished.count(), 1 );
       
   260     QCOMPARE( spyScanFinished.at(0).at(0), QVariant( MpSongScanner::ScanInterrupted ) );
       
   261     QCOMPARE( spyScanFinished.at(0).at(1), QVariant( 0 ) );
       
   262     QVERIFY( !mTest->mScanning );
       
   263 }
       
   264 
       
   265 //end of file