imagehandlingutilities/thumbnailmanager/thumbnailmanagerqt/tsrc/test_qtnmwrapper.cpp
branchRCL_3
changeset 41 9d4d3445ce6e
parent 40 6257223ede8a
child 47 3d743e824514
equal deleted inserted replaced
40:6257223ede8a 41:9d4d3445ce6e
     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: 
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QtGui>
       
    19 #include <QtTest/QtTest>
       
    20 #include <QEventLoop>
       
    21 #include <QDebug>
       
    22 #include <QImage>
       
    23 #include <fbs.h>
       
    24 
       
    25 #include <thumbnaildata.h>
       
    26 
       
    27 #include <thumbnailmanager_qt.h>
       
    28 #include "thumbnailmanager_p_qt.h"
       
    29 
       
    30 class TestThumbnailManager : public QObject
       
    31 {
       
    32     Q_OBJECT
       
    33 
       
    34 public:
       
    35     TestThumbnailManager(): QObject(), wrapper( NULL ), ipixmap( NULL ) {};
       
    36 
       
    37 public slots:
       
    38     void thumbnailReady( QPixmap , void * , int , int );
       
    39 
       
    40     void thumbnailReady_p( QPixmap , void * , int , int );
       
    41 
       
    42     void thumbnailReadyPixmap( QPixmap , void * , int , int );
       
    43 
       
    44     void thumbnailReadyImage( QImage , void * , int , int );
       
    45 
       
    46 private slots:
       
    47     void init();
       
    48     void cleanup();
       
    49 
       
    50     void testThumbnailReadyError();
       
    51     void testThumbnailReadyValid();
       
    52     
       
    53     void testThumbnailReadyImage();
       
    54     void testThumbnailReadyPixmap();
       
    55     void testThumbnailReadyImageAndPixmap();
       
    56 
       
    57     void createAndDestroy();
       
    58     void qualityPreference();
       
    59     void thumbnailSize();
       
    60     void thumbnailMode();
       
    61     
       
    62     void getThumbnailByName();
       
    63     void getThumbnailById();
       
    64     void setThumbnail();
       
    65     void setThumbnailFromPath();
       
    66     void cancelRequest();
       
    67     void changePriority();
       
    68     void deleteThumbnailsByName();
       
    69     void deleteThumbnailsById();
       
    70 
       
    71     void testPriorities_data();
       
    72     void testPriorities();
       
    73 
       
    74 
       
    75 public:
       
    76     ThumbnailManager *wrapper;
       
    77     ThumbnailManagerPrivate *wrapper_p;
       
    78     
       
    79     QPixmap* ipixmap;
       
    80     
       
    81     int aid; 
       
    82     int aerrorCode;
       
    83     bool pixmapNull;
       
    84     
       
    85     //pixmap
       
    86     int pixmapId;
       
    87     int pixmapErr;
       
    88     void *pixmapDataPtr;
       
    89     
       
    90     //image
       
    91     int imageId;
       
    92     int imageErr;
       
    93     void *imageDataPtr;
       
    94     
       
    95 };
       
    96 
       
    97 enum testDataType{
       
    98     AllNull,
       
    99     BitmapValid
       
   100 };
       
   101 
       
   102 class TestThumbnailData : public MThumbnailData
       
   103 {
       
   104 public:
       
   105     static const int BITMAP_WIDTH = 26;
       
   106     static const int BITMAP_HEIGHT = 15;
       
   107     
       
   108 public:
       
   109     TestThumbnailData( testDataType type, void* clientData_ = 0) : bitmap(0), clientData(clientData_)
       
   110     {
       
   111         switch( type ){
       
   112         case AllNull:
       
   113         break;
       
   114 
       
   115         case BitmapValid:
       
   116             bitmap = new CFbsBitmap();
       
   117             QVERIFY( !bitmap->Create(TSize(BITMAP_WIDTH, BITMAP_HEIGHT),EColor64K) );
       
   118             QVERIFY( !bitmap->Load( _L("c:\\tnmwrapper_tsrc.mbm") ) );
       
   119         break;
       
   120         };
       
   121     
       
   122     };
       
   123 
       
   124     ~TestThumbnailData() { 
       
   125         if( bitmap ) {
       
   126             delete bitmap;
       
   127         }
       
   128     };
       
   129     
       
   130     CFbsBitmap* Bitmap() {return bitmap;};
       
   131 
       
   132     CFbsBitmap* DetachBitmap() {return bitmap;};
       
   133 
       
   134     TAny* ClientData(){ return clientData; };
       
   135 
       
   136 public:
       
   137     CFbsBitmap* bitmap;
       
   138     TAny* clientData;
       
   139 
       
   140 };
       
   141 
       
   142 // --- test initialization ---
       
   143 
       
   144 void TestThumbnailManager::init()
       
   145 {
       
   146     wrapper = new ThumbnailManager();
       
   147     connect( wrapper, SIGNAL(thumbnailReady( QPixmap , void* , int, int ) ),
       
   148             this, SLOT( thumbnailReady( QPixmap , void* , int , int )));
       
   149     
       
   150     wrapper_p = wrapper->d; //new ThumbnailManagerPrivate();
       
   151 }
       
   152     
       
   153 void TestThumbnailManager::cleanup()
       
   154 {
       
   155     disconnect( wrapper, SIGNAL(thumbnailReady( QPixmap , void* , int, int ) ),
       
   156             this, SLOT( thumbnailReady( QPixmap , void* , int , int )));
       
   157     delete wrapper;
       
   158     wrapper = NULL;
       
   159 
       
   160     if( ipixmap ) {
       
   161         delete ipixmap;
       
   162         ipixmap = NULL;
       
   163         }
       
   164  
       
   165  //   delete wrapper_p;
       
   166 }
       
   167 
       
   168 // --- test ---
       
   169 
       
   170 /**
       
   171  * Simple create and destroy.
       
   172  */
       
   173 void TestThumbnailManager::createAndDestroy()
       
   174 {
       
   175     //empty
       
   176 }
       
   177 
       
   178 /**
       
   179  * Checking quality preference flag.
       
   180  */
       
   181 void TestThumbnailManager::qualityPreference()
       
   182 {
       
   183     QVERIFY( wrapper->setQualityPreference( ThumbnailManager::OptimizeForQuality ) );
       
   184     QVERIFY( wrapper->setQualityPreference( ThumbnailManager::OptimizeForPerformance ) );
       
   185     QVERIFY( wrapper->qualityPreference() == ThumbnailManager::OptimizeForPerformance );
       
   186 }
       
   187 
       
   188 /**
       
   189  * Checking thumbnail size flag.
       
   190  */
       
   191 void TestThumbnailManager::thumbnailSize()
       
   192 {
       
   193     QVERIFY( wrapper->setThumbnailSize( ThumbnailManager::ThumbnailSmall ) );
       
   194     QVERIFY( wrapper->setThumbnailSize( ThumbnailManager::ThumbnailMedium ) );
       
   195     QVERIFY( wrapper->setThumbnailSize( ThumbnailManager::ThumbnailLarge ) );
       
   196     QVERIFY( wrapper->setThumbnailSize( QSize( 100, 100 ) ) );
       
   197     QVERIFY( wrapper->thumbnailSize() == QSize( 100, 100 ) );
       
   198 }
       
   199 
       
   200 /**
       
   201  * Checking thumbnail mode flag.
       
   202  */
       
   203 void TestThumbnailManager::thumbnailMode()
       
   204 {
       
   205     QVERIFY( wrapper->setMode( ThumbnailManager::Default ) );
       
   206     QVERIFY( wrapper->setMode( ThumbnailManager::AllowAnySize ) );
       
   207     QVERIFY( wrapper->setMode( ThumbnailManager::DoNotCreate ) );
       
   208     
       
   209     QVERIFY( wrapper->setMode( ThumbnailManager::CropToAspectRatio ) );
       
   210     QVERIFY( wrapper->mode() == ThumbnailManager::CropToAspectRatio );
       
   211 }
       
   212 
       
   213 /**
       
   214  * Checking thumbnail mode flag.
       
   215  */
       
   216 void TestThumbnailManager::getThumbnailByName()
       
   217 {
       
   218     wrapper->setMode( ThumbnailManager::CropToAspectRatio );
       
   219     wrapper->setThumbnailSize( QSize( 200, 50 )); 
       
   220     QVERIFY( wrapper->getThumbnail( "c:\\tnmwrapper_tsrc.png", NULL, -99 ) != -1 );
       
   221     QVERIFY( wrapper->getThumbnail( "c:/tnmwrapper_tsrc.png", NULL, -99 ) != -1 );
       
   222 }
       
   223 
       
   224 void TestThumbnailManager::getThumbnailById()
       
   225 {
       
   226     wrapper->setMode( ThumbnailManager::CropToAspectRatio );
       
   227     wrapper->setThumbnailSize( QSize( 200, 50 )); 
       
   228     QVERIFY( wrapper->getThumbnail( 2, NULL, -99 ) != -1 );
       
   229 }
       
   230 
       
   231 void TestThumbnailManager::setThumbnail()
       
   232 {
       
   233     ipixmap = new QPixmap();
       
   234     ipixmap->load( "c:\\tnmwrapper.bmp" );
       
   235     wrapper->setMode( ThumbnailManager::CropToAspectRatio );
       
   236     wrapper->setThumbnailSize(ThumbnailManager::ThumbnailMedium); 
       
   237     QVERIFY( wrapper->setThumbnail( *ipixmap, "c:\\tnmwrapper_tsrc.png" ) != -1 );
       
   238     ipixmap->fill();
       
   239     QVERIFY( wrapper->setThumbnail( *ipixmap, "c:\\tnmwrapper_tsrc.png" ) != -1 );
       
   240 
       
   241 	//QI,mage
       
   242 	QImage *img = new QImage("c:\\tnmwrapper.bmp");
       
   243     QVERIFY( wrapper->setThumbnail( *img, "c:\\tnmwrapper_tsrc.png" ) != -1 );
       
   244     img->fill(0);
       
   245     QVERIFY( wrapper->setThumbnail( *img, "c:\\tnmwrapper_tsrc.png" ) != -1 );
       
   246 	delete img;
       
   247 }
       
   248 
       
   249 void TestThumbnailManager::setThumbnailFromPath()
       
   250 { 
       
   251     // normal URI
       
   252     QVERIFY( wrapper->setThumbnail( "c:\\tnmwrapper_tsrc.png", "c:\\tnmwrapper_tsrc.vcf", "contact/x-vcard" ) != -1 );
       
   253     
       
   254     // virtual URI
       
   255     QVERIFY( wrapper->setThumbnail( "c:\\tnmwrapper_tsrc.png", "c:tnmwrapper_tsrc.vcf", "contact/x-vcard" ) != -1 );
       
   256     
       
   257     // no mime
       
   258     QVERIFY( wrapper->setThumbnail( "c:\\tnmwrapper_tsrc.png", "c:tnmwrapper_tsrc.vcf" ) != -1 );
       
   259 }
       
   260 
       
   261 void TestThumbnailManager::cancelRequest()
       
   262 {
       
   263     wrapper->setMode( ThumbnailManager::CropToAspectRatio );
       
   264     wrapper->setThumbnailSize( QSize( 200, 50 )); 
       
   265     QVERIFY( wrapper->cancelRequest( wrapper->getThumbnail( "c:\\tnmwrapper_tsrc.png", NULL, -99 ) ) );
       
   266     QVERIFY( !wrapper->cancelRequest( 123 ) ); //test request not found
       
   267 }
       
   268 
       
   269 void TestThumbnailManager::changePriority()
       
   270 {
       
   271     wrapper->setMode( ThumbnailManager::CropToAspectRatio );
       
   272     wrapper->setThumbnailSize( QSize( 200, 50 )); 
       
   273     QVERIFY( wrapper->changePriority( wrapper->getThumbnail( "c:\\tnmwrapper_tsrc.png", NULL, -99 ), -80 ) );
       
   274 }
       
   275 
       
   276 void TestThumbnailManager::deleteThumbnailsByName()
       
   277 {
       
   278     wrapper->deleteThumbnails( "c:/tnmwrapper_tsrc.png" );
       
   279     wrapper->deleteThumbnails( "c:\\tnmwrapper_tsrc.png" );
       
   280 }
       
   281 
       
   282 void TestThumbnailManager::deleteThumbnailsById()
       
   283 {
       
   284     wrapper->deleteThumbnails( 2 );
       
   285 }
       
   286 
       
   287 void TestThumbnailManager::testThumbnailReadyError()
       
   288 {
       
   289     int a=0;
       
   290 
       
   291     QVERIFY( connect( wrapper_p, SIGNAL(thumbnailReady( QPixmap , void* , int, int ) ),
       
   292         this, SLOT( thumbnailReady_p( QPixmap , void* , int , int )), Qt::DirectConnection ) );
       
   293 
       
   294     //test bytearray not null and thumbnail error
       
   295     TestThumbnailData tdata1(AllNull);
       
   296     aid = 12; 
       
   297     aerrorCode = -1;
       
   298     pixmapNull = true;
       
   299     wrapper_p->ThumbnailReady( aerrorCode, tdata1, aid );
       
   300     
       
   301     disconnect( wrapper_p, SIGNAL(thumbnailReady( QPixmap , void* , int, int ) ),
       
   302         this, SLOT( thumbnailReady_p( QPixmap , void* , int , int )));
       
   303 }
       
   304 
       
   305 void TestThumbnailManager::testThumbnailReadyValid()
       
   306 {
       
   307     QVERIFY( connect( wrapper_p, SIGNAL(thumbnailReady( QPixmap , void* , int, int ) ),
       
   308         this, SLOT( thumbnailReady_p( QPixmap , void* , int , int )), Qt::DirectConnection ) );
       
   309 
       
   310     TestThumbnailData tdata3( BitmapValid );
       
   311     aid = 10; 
       
   312     aerrorCode = 0;
       
   313     pixmapNull = false;
       
   314     wrapper_p->ThumbnailReady( aerrorCode, tdata3, aid );
       
   315 
       
   316     disconnect( wrapper_p, SIGNAL(thumbnailReady( QPixmap , void* , int, int ) ),
       
   317         this, SLOT( thumbnailReady_p( QPixmap , void* , int , int )));
       
   318 }
       
   319 
       
   320 void TestThumbnailManager::testThumbnailReadyImage()
       
   321 {
       
   322     QVERIFY( connect( wrapper_p, SIGNAL(thumbnailReady( QImage , void* , int, int ) ),
       
   323         this, SLOT( thumbnailReadyImage( QImage , void* , int , int )), Qt::DirectConnection ) );
       
   324 
       
   325     pixmapId = 0;
       
   326     pixmapErr = 0;
       
   327     pixmapDataPtr = 0;
       
   328 
       
   329     imageId = 432;
       
   330     imageErr = KErrNone;
       
   331     imageDataPtr = reinterpret_cast<void*>(0x1234);
       
   332     
       
   333     TestThumbnailData td = TestThumbnailData(BitmapValid);
       
   334     td.clientData = imageDataPtr;
       
   335 
       
   336     wrapper_p->ThumbnailReady(imageErr, td, imageId);
       
   337     
       
   338     QVERIFY(td.bitmap!=0);
       
   339     QVERIFY(td.bitmap->SizeInPixels().iWidth == TestThumbnailData::BITMAP_WIDTH);
       
   340     QVERIFY(td.bitmap->SizeInPixels().iHeight == TestThumbnailData::BITMAP_HEIGHT);
       
   341 
       
   342     disconnect( wrapper_p, SIGNAL(thumbnailReady( QImage , void* , int, int ) ),
       
   343         this, SLOT( thumbnailReadyImage(QImage, void* , int , int )));  
       
   344 }
       
   345 
       
   346 void TestThumbnailManager::testThumbnailReadyPixmap()
       
   347 {
       
   348     QVERIFY( connect( wrapper_p, SIGNAL(thumbnailReady( QPixmap , void* , int, int ) ),
       
   349         this, SLOT( thumbnailReadyPixmap( QPixmap , void* , int , int )), Qt::DirectConnection ) );
       
   350 
       
   351     pixmapId = 432;
       
   352     pixmapErr = -7;
       
   353     pixmapDataPtr = reinterpret_cast<void*>(0x1234);
       
   354 
       
   355     imageId = 0;
       
   356     imageErr = 0;
       
   357     imageDataPtr = 0;
       
   358 
       
   359     TestThumbnailData td = TestThumbnailData(BitmapValid);
       
   360     td.clientData = pixmapDataPtr;
       
   361 
       
   362     wrapper_p->ThumbnailReady(pixmapErr, td, pixmapId);
       
   363     
       
   364     QVERIFY(td.bitmap!=0);
       
   365     QVERIFY(td.bitmap->SizeInPixels().iWidth == TestThumbnailData::BITMAP_WIDTH);
       
   366     QVERIFY(td.bitmap->SizeInPixels().iHeight == TestThumbnailData::BITMAP_HEIGHT);
       
   367 
       
   368     disconnect( wrapper_p, SIGNAL(thumbnailReady( QPixmap , void* , int, int ) ),
       
   369         this, SLOT( thumbnailReadyPixmap( QPixmap , void* , int , int )));
       
   370 }
       
   371 
       
   372 void TestThumbnailManager::testThumbnailReadyImageAndPixmap()
       
   373 {
       
   374     QVERIFY( connect( wrapper_p, SIGNAL(thumbnailReady( QPixmap , void* , int, int ) ),
       
   375         this, SLOT( thumbnailReadyPixmap( QPixmap , void* , int , int )), Qt::DirectConnection ) );
       
   376 
       
   377     QVERIFY( connect( wrapper_p, SIGNAL(thumbnailReady( QImage , void* , int, int ) ),
       
   378         this, SLOT( thumbnailReadyImage( QImage , void* , int , int )), Qt::DirectConnection ) );
       
   379 
       
   380     pixmapId = 432;
       
   381     pixmapErr = KErrNone;
       
   382     pixmapDataPtr = reinterpret_cast<void*>(0x1234);
       
   383 
       
   384     imageId = pixmapId;
       
   385     imageErr = pixmapErr;
       
   386     imageDataPtr = pixmapDataPtr;
       
   387 
       
   388     TestThumbnailData td = TestThumbnailData(BitmapValid);
       
   389     td.clientData = pixmapDataPtr;
       
   390 
       
   391     wrapper_p->ThumbnailReady(pixmapErr, td, pixmapId);
       
   392 
       
   393     QVERIFY(td.bitmap!=0);
       
   394     QVERIFY(td.bitmap->SizeInPixels().iWidth == TestThumbnailData::BITMAP_WIDTH);
       
   395     QVERIFY(td.bitmap->SizeInPixels().iHeight == TestThumbnailData::BITMAP_HEIGHT);
       
   396 
       
   397     disconnect( wrapper_p, SIGNAL(thumbnailReady( QImage , void* , int, int ) ),
       
   398         this, SLOT( thumbnailReadyImage( QImage , void* , int , int )));
       
   399 
       
   400     disconnect( wrapper_p, SIGNAL(thumbnailReady( QPixmap , void* , int, int ) ),
       
   401         this, SLOT( thumbnailReadyPixmap( QPixmap , void* , int , int )));    
       
   402 }
       
   403 
       
   404 void TestThumbnailManager::testPriorities_data()
       
   405 {
       
   406     // note: current bounds for priorites are [-100:20] (from: CActive::TPriority)
       
   407     
       
   408     QTest::addColumn<int>("priority");
       
   409     QTest::addColumn<int>("expectedPriority");
       
   410     QTest::newRow("priority 0") << 0 << 0;
       
   411     QTest::newRow("priority 10") << 10 << 10;
       
   412     QTest::newRow("priority 20") << 20 << 20;
       
   413     QTest::newRow("priority 100") << 100 << 20;
       
   414     QTest::newRow("priority 200") << 200 << 20;
       
   415     QTest::newRow("priority -10") << -10 << -10;
       
   416     QTest::newRow("priority -100") << -100 << -100;
       
   417     QTest::newRow("priority -200") << -200 << -100;
       
   418 }
       
   419 
       
   420 void TestThumbnailManager::testPriorities()
       
   421 {
       
   422     QFETCH(int, priority);
       
   423     QFETCH(int, expectedPriority);
       
   424     QCOMPARE(wrapper_p->convertPriority(priority), expectedPriority);
       
   425 }
       
   426  
       
   427 
       
   428 // --- private slots - callback  ---
       
   429 
       
   430 void TestThumbnailManager::thumbnailReady( QPixmap pixmap, void *clientData, int id, int errorCode )
       
   431 {
       
   432     //do nothing, we dont test Thumbnail Manager's functionality, we just use it
       
   433     Q_UNUSED( pixmap );
       
   434     Q_UNUSED( clientData );
       
   435     Q_UNUSED( id );
       
   436     Q_UNUSED( errorCode );
       
   437 }
       
   438 
       
   439 void TestThumbnailManager::thumbnailReady_p( QPixmap pixmap, void * clientData, int id, int errorCode )
       
   440 {
       
   441     Q_UNUSED( clientData );
       
   442     QVERIFY( pixmap.isNull() == pixmapNull );
       
   443     QVERIFY( errorCode == aerrorCode );
       
   444     QVERIFY( id == aid );
       
   445 }
       
   446 
       
   447 void TestThumbnailManager::thumbnailReadyPixmap( QPixmap pixmap, void *clientData, int id, int errorCode )
       
   448 {
       
   449     Q_UNUSED( pixmap );
       
   450     QVERIFY( errorCode == pixmapErr );
       
   451     QVERIFY( id == pixmapId );
       
   452     QVERIFY( clientData = pixmapDataPtr );
       
   453 }
       
   454 
       
   455 void TestThumbnailManager::thumbnailReadyImage( QImage image, void *clientData, int id, int errorCode )
       
   456 {
       
   457     Q_UNUSED( image );
       
   458     QVERIFY( errorCode == imageErr );
       
   459     QVERIFY( id == imageId );
       
   460     QVERIFY( clientData = imageDataPtr );
       
   461 }
       
   462 
       
   463 #ifdef _LOG_TO_C_
       
   464     int main (int argc, char* argv[]) 
       
   465     {
       
   466         QApplication app(argc, argv);
       
   467         TestThumbnailManager tc;
       
   468         int c = 3;
       
   469         char* v[] = {argv[0], "-o", "c:/test.txt"};
       
   470         return QTest::qExec(&tc, c, v);
       
   471     }
       
   472 #else
       
   473     int main (int argc, char* argv[]) 
       
   474     {
       
   475         for ( int i=0;i<argc; i++){
       
   476             if (strcmp(argv[i], "-o")==0 && i+1 <argc ){
       
   477                 //let's make sure that folder specified after -o exists
       
   478                 QDir file( QString::fromLatin1( argv[i+1] ));
       
   479                 QString s = file.absolutePath ();
       
   480                 s = s.left( s.lastIndexOf(file.dirName()) );
       
   481                 if ( !file.exists(s) ){
       
   482                     file.mkpath(s);
       
   483                 }
       
   484             }
       
   485         }
       
   486         
       
   487         QApplication app(argc, argv);
       
   488         QTEST_DISABLE_KEYPAD_NAVIGATION
       
   489         TestThumbnailManager tc;
       
   490         return QTest::qExec(&tc, argc, argv);
       
   491     }
       
   492 #endif
       
   493 	
       
   494 #include "test_qtnmwrapper.moc"
       
   495