imagehandlingutilities/thumbnailmanager/thumbnailmanagerqt/tsrc/test_qtnmwrapper.cpp
changeset 15 7197e789b953
child 17 ffa62e555b02
equal deleted inserted replaced
14:2edacbf5d3f9 15:7197e789b953
       
     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 private slots:
       
    43     void initTestCase();
       
    44     void cleanupTestCase();
       
    45     
       
    46     void createAndDestroy();
       
    47     void qualityPreference();
       
    48     void thumbnailSize();
       
    49     void thumbnailMode();
       
    50     
       
    51     void getThumbnailByName();
       
    52     void getThumbnailById();
       
    53     void setThumbnail();
       
    54     void cancelRequest();
       
    55     void changePriority();
       
    56     void deleteThumbnailsByName();
       
    57     void deleteThumbnailsById();
       
    58 
       
    59     void testThumbnailReadyError();
       
    60     void testThumbnailReadyValid();
       
    61 
       
    62 public:
       
    63     ThumbnailManager* wrapper;
       
    64     QPixmap* ipixmap;
       
    65     
       
    66     int aid; 
       
    67     int aerrorCode;
       
    68     bool pixmapNull;
       
    69     
       
    70 };
       
    71 
       
    72 enum testDataType{
       
    73     AllNull,
       
    74     BitmapValid
       
    75 };
       
    76 
       
    77 class TestThumbnailData : public MThumbnailData
       
    78 {
       
    79 public:
       
    80     TestThumbnailData( testDataType type) : bitmap(0), clientData(0)
       
    81     {
       
    82         switch( type ){
       
    83         case AllNull:
       
    84         break;
       
    85 
       
    86         case BitmapValid:
       
    87             bitmap = new CFbsBitmap();
       
    88             QVERIFY( !bitmap->Create(TSize(26,15),EColor64K) );
       
    89             QVERIFY( !bitmap->Load( _L("c:\\tnmwrapper_tsrc.mbm") ) );
       
    90         break;
       
    91         };
       
    92     
       
    93     };
       
    94 
       
    95     ~TestThumbnailData() { 
       
    96         if( bitmap )
       
    97             delete bitmap; 
       
    98         if( clientData )
       
    99             delete clientData; 
       
   100     };
       
   101     
       
   102     CFbsBitmap* Bitmap() {return bitmap;};
       
   103 
       
   104     CFbsBitmap* DetachBitmap() {return bitmap;};
       
   105 
       
   106     TAny* ClientData(){ return clientData; };
       
   107 
       
   108 public:
       
   109     CFbsBitmap* bitmap;
       
   110     TAny* clientData;
       
   111 
       
   112 };
       
   113 
       
   114 
       
   115 // ======== MEMBER FUNCTIONS ========
       
   116 void TestThumbnailManager::initTestCase()
       
   117 {
       
   118     wrapper = new ThumbnailManager();
       
   119     connect( wrapper, SIGNAL(thumbnailReady( QPixmap , void* , int, int ) ),
       
   120             this, SLOT( thumbnailReady( QPixmap , void* , int , int )));
       
   121 }
       
   122     
       
   123 void TestThumbnailManager::cleanupTestCase()
       
   124 {
       
   125     disconnect( wrapper, SIGNAL(thumbnailReady( QPixmap , void* , int, int ) ),
       
   126             this, SLOT( thumbnailReady( QPixmap , void* , int , int )));
       
   127     delete wrapper;
       
   128     wrapper = NULL;
       
   129 
       
   130     if( ipixmap ){
       
   131         delete ipixmap;
       
   132         ipixmap = NULL;
       
   133         }
       
   134 }
       
   135 
       
   136 
       
   137 void TestThumbnailManager::createAndDestroy()
       
   138 {
       
   139     //empty
       
   140 }
       
   141 
       
   142 void TestThumbnailManager::qualityPreference()
       
   143 {
       
   144     QVERIFY( wrapper->setQualityPreference( ThumbnailManager::OptimizeForQuality ) );
       
   145     QVERIFY( wrapper->setQualityPreference( ThumbnailManager::OptimizeForPerformance ) );
       
   146     QVERIFY( wrapper->qualityPreference() == ThumbnailManager::OptimizeForPerformance );
       
   147 }
       
   148 
       
   149 void TestThumbnailManager::thumbnailSize()
       
   150 {
       
   151     QVERIFY( wrapper->setThumbnailSize( ThumbnailManager::ThumbnailSmall ) );
       
   152     QVERIFY( wrapper->setThumbnailSize( ThumbnailManager::ThumbnailMedium ) );
       
   153     QVERIFY( wrapper->setThumbnailSize( ThumbnailManager::ThumbnailLarge ) );
       
   154     QVERIFY( wrapper->setThumbnailSize( QSize( 100, 100 ) ) );
       
   155     QVERIFY( wrapper->thumbnailSize() == QSize( 100, 100 ) );
       
   156 }
       
   157 
       
   158 void TestThumbnailManager::thumbnailMode()
       
   159 {
       
   160     QVERIFY( wrapper->setMode( ThumbnailManager::Default ) );
       
   161     QVERIFY( wrapper->setMode( ThumbnailManager::AllowAnySize ) );
       
   162     QVERIFY( wrapper->setMode( ThumbnailManager::DoNotCreate ) );
       
   163     
       
   164     QVERIFY( wrapper->setMode( ThumbnailManager::CropToAspectRatio ) );
       
   165     QVERIFY( wrapper->mode() == ThumbnailManager::CropToAspectRatio );
       
   166 }
       
   167 
       
   168 void TestThumbnailManager::getThumbnailByName()
       
   169 {
       
   170     wrapper->setMode( ThumbnailManager::CropToAspectRatio );
       
   171     wrapper->setThumbnailSize( QSize( 200, 50 )); 
       
   172     QVERIFY( wrapper->getThumbnail( "c:\\tnmwrapper_tsrc.png", NULL, -99 ) != -1 );
       
   173     QVERIFY( wrapper->getThumbnail( "c:/tnmwrapper_tsrc.png", NULL, -99 ) != -1 );
       
   174 }
       
   175 
       
   176 void TestThumbnailManager::getThumbnailById()
       
   177 {
       
   178     wrapper->setMode( ThumbnailManager::CropToAspectRatio );
       
   179     wrapper->setThumbnailSize( QSize( 200, 50 )); 
       
   180     QVERIFY( wrapper->getThumbnail( 2, NULL, -99 ) != -1 );
       
   181 }
       
   182 
       
   183 void TestThumbnailManager::setThumbnail()
       
   184 {
       
   185     ipixmap = new QPixmap();
       
   186     ipixmap->load( "c:\\tnmwrapper.bmp" );
       
   187     wrapper->setMode( ThumbnailManager::CropToAspectRatio );
       
   188     wrapper->setThumbnailSize(ThumbnailManager::ThumbnailMedium); 
       
   189     QVERIFY( wrapper->setThumbnail( *ipixmap, "c:\\tnmwrapper_tsrc.png" ) != -1 );
       
   190     ipixmap->fill();
       
   191     QVERIFY( wrapper->setThumbnail( *ipixmap, "c:\\tnmwrapper_tsrc.png" ) != -1 );
       
   192 
       
   193 	//QI,mage
       
   194 	QImage *img = new QImage("c:\\tnmwrapper.bmp");
       
   195     QVERIFY( wrapper->setThumbnail( *img, "c:\\tnmwrapper_tsrc.png" ) != -1 );
       
   196     img->fill(0);
       
   197     QVERIFY( wrapper->setThumbnail( *img, "c:\\tnmwrapper_tsrc.png" ) != -1 );
       
   198 	delete img;
       
   199 }
       
   200 
       
   201 void TestThumbnailManager::cancelRequest()
       
   202 {
       
   203     wrapper->setMode( ThumbnailManager::CropToAspectRatio );
       
   204     wrapper->setThumbnailSize( QSize( 200, 50 )); 
       
   205     QVERIFY( wrapper->cancelRequest( wrapper->getThumbnail( "c:\\tnmwrapper_tsrc.png", NULL, -99 ) ) );
       
   206     QVERIFY( !wrapper->cancelRequest( 123 ) ); //test request not found
       
   207 }
       
   208 
       
   209 void TestThumbnailManager::changePriority()
       
   210 {
       
   211     wrapper->setMode( ThumbnailManager::CropToAspectRatio );
       
   212     wrapper->setThumbnailSize( QSize( 200, 50 )); 
       
   213     QVERIFY( wrapper->changePriority( wrapper->getThumbnail( "c:\\tnmwrapper_tsrc.png", NULL, -99 ), -80 ) );
       
   214 }
       
   215 
       
   216 void TestThumbnailManager::deleteThumbnailsByName()
       
   217 {
       
   218     wrapper->deleteThumbnails( "c:/tnmwrapper_tsrc.png" );
       
   219     wrapper->deleteThumbnails( "c:\\tnmwrapper_tsrc.png" );
       
   220 }
       
   221 
       
   222 void TestThumbnailManager::deleteThumbnailsById()
       
   223 {
       
   224     wrapper->deleteThumbnails( 2 );
       
   225 }
       
   226 
       
   227 void TestThumbnailManager::thumbnailReady( QPixmap /*pixmap*/, void * /*clientData*/, int /*id*/, int /*errorCode*/ )
       
   228 {
       
   229     //do nothing, we dont test Thumbnail Manager's functionality, we just use it
       
   230 }
       
   231 
       
   232 void TestThumbnailManager::thumbnailReady_p( QPixmap pixmap, void * /*clientData*/, int id, int errorCode )
       
   233 {
       
   234     QVERIFY( pixmap.isNull() == pixmapNull );
       
   235     QVERIFY( errorCode == aerrorCode );
       
   236     QVERIFY( aid == id );
       
   237 }
       
   238 
       
   239 void TestThumbnailManager::testThumbnailReadyError()
       
   240 {
       
   241     ThumbnailManagerPrivate* wrapper_p = new ThumbnailManagerPrivate();
       
   242 
       
   243     QVERIFY( connect( wrapper_p, SIGNAL(thumbnailReady( QPixmap , void* , int, int ) ),
       
   244         this, SLOT( thumbnailReady_p( QPixmap , void* , int , int )), Qt::DirectConnection ) );
       
   245 
       
   246     //test bytearray not null and thumbnail error
       
   247     TestThumbnailData tdata1(AllNull);
       
   248     aid = 12; 
       
   249     aerrorCode = -1;
       
   250     pixmapNull = true;
       
   251     wrapper_p->ThumbnailReady( aerrorCode, tdata1, aid );
       
   252     
       
   253     disconnect( wrapper_p, SIGNAL(thumbnailReady( QPixmap , void* , int, int ) ),
       
   254         this, SLOT( thumbnailReady_p( QPixmap , void* , int , int )));
       
   255     delete wrapper_p;
       
   256 }
       
   257 
       
   258 void TestThumbnailManager::testThumbnailReadyValid()
       
   259 {
       
   260     ThumbnailManagerPrivate* wrapper_p = new ThumbnailManagerPrivate();
       
   261 
       
   262     QVERIFY( connect( wrapper_p, SIGNAL(thumbnailReady( QPixmap , void* , int, int ) ),
       
   263         this, SLOT( thumbnailReady_p( QPixmap , void* , int , int )), Qt::DirectConnection ) );
       
   264 
       
   265     TestThumbnailData tdata3( BitmapValid );
       
   266     aid = 10; 
       
   267     aerrorCode = 0;
       
   268     pixmapNull = false;
       
   269     wrapper_p->ThumbnailReady( aerrorCode, tdata3, aid );
       
   270 
       
   271     disconnect( wrapper_p, SIGNAL(thumbnailReady( QPixmap , void* , int, int ) ),
       
   272         this, SLOT( thumbnailReady_p( QPixmap , void* , int , int )));
       
   273     delete wrapper_p;
       
   274 }
       
   275 
       
   276 #ifdef _LOG_TO_C_
       
   277     int main (int argc, char* argv[]) 
       
   278     {
       
   279         QApplication app(argc, argv);
       
   280         TestThumbnailManager tc;
       
   281         int c = 3;
       
   282         char* v[] = {argv[0], "-o", "c:/test.txt"};
       
   283         return QTest::qExec(&tc, c, v);
       
   284     }
       
   285 #else
       
   286     QTEST_MAIN(TestThumbnailManager)
       
   287 #endif
       
   288 	
       
   289 #include "test_qtnmwrapper.moc"
       
   290