camerauis/cameraxui/cxengine/tsrc/unit/unittest_cxeimagedataitemsymbian/unittest_cxeimagedataitemsymbian.cpp
changeset 48 42ba2d16bf40
parent 37 64817133cd1d
equal deleted inserted replaced
37:64817133cd1d 48:42ba2d16bf40
     1 /*
     1 /*
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    14 * Description:
    14 * Description:
    15 *
    15 *
    16 */
    16 */
    17 #include <QDate>
    17 #include <QDate>
    18 #include <QTest>
    18 #include <QTest>
       
    19 #include <QDir>
    19 #include <QDebug>
    20 #include <QDebug>
    20 #include <QSignalSpy>
    21 #include <QSignalSpy>
    21 
    22 
    22 #include <bitdev.h>
    23 #include <bitdev.h>
    23 
    24 
    24 #include "unittest_cxeimagedataitemsymbian.h"
    25 #include "unittest_cxeimagedataitemsymbian.h"
    25 #include "cxeimagedataitemunit.h"
    26 #include "cxeimagedataitemunit.h"
    26 #include "cxestillimagesymbian.h"
    27 #include "cxestillimagesymbian.h"
    27 #include "cxutils.h"
    28 #include "cxutils.h"
       
    29 #include "cxeerror.h"
    28 
    30 
    29 
    31 
    30 UnitTestCxeImageDataItemSymbian::UnitTestCxeImageDataItemSymbian()
    32 UnitTestCxeImageDataItemSymbian::UnitTestCxeImageDataItemSymbian()
    31 : mImageDataItem(NULL)
    33 : mImageDataItem(NULL),
    32 {
    34   mImageCounter(-1)
       
    35 {
       
    36     qRegisterMetaType<CxeError::Id>("CxeError::Id");
    33 }
    37 }
    34 
    38 
    35 UnitTestCxeImageDataItemSymbian::~UnitTestCxeImageDataItemSymbian()
    39 UnitTestCxeImageDataItemSymbian::~UnitTestCxeImageDataItemSymbian()
    36 {
    40 {
    37     delete mImageDataItem;
    41     delete mImageDataItem;
    38 }
    42 }
    39 
    43 
       
    44 /*!
       
    45 * Initializes resources.
       
    46 */
    40 void UnitTestCxeImageDataItemSymbian::init()
    47 void UnitTestCxeImageDataItemSymbian::init()
    41 {
    48 {
    42     CX_DEBUG_ENTER_FUNCTION();
    49     CX_DEBUG_ENTER_FUNCTION();
    43     TInt index = 0;
    50     mFilename = generateImageFileName();
    44     mPath = generateImageFileName(index);
       
    45     QByteArray data = "1234";
    51     QByteArray data = "1234";
    46 
    52     mImageDataItem = new CxeImageDataItemUnit(mImageCounter, data, mFilename, false);
    47     mImageDataItem = new CxeImageDataItemUnit(index, data, mPath, false);
       
    48 
       
    49     QVERIFY(mImageDataItem->state() == CxeImageDataItem::SavePending);
    53     QVERIFY(mImageDataItem->state() == CxeImageDataItem::SavePending);
    50     CX_DEBUG_EXIT_FUNCTION();
    54 	QCOMPARE(mImageDataItem->isLocationEnabled(), false);
    51 }
    55 
    52 
    56     CX_DEBUG_EXIT_FUNCTION();
       
    57 }
       
    58 
       
    59 
       
    60 /*!
       
    61 * cleans up resources for each test case
       
    62 */
    53 void UnitTestCxeImageDataItemSymbian::cleanup()
    63 void UnitTestCxeImageDataItemSymbian::cleanup()
    54 {
    64 {
    55     CX_DEBUG_ENTER_FUNCTION();
    65     CX_DEBUG_ENTER_FUNCTION();
       
    66     
    56     delete mImageDataItem;
    67     delete mImageDataItem;
    57     mImageDataItem = NULL;
    68     mImageDataItem = NULL;
    58     CX_DEBUG_EXIT_FUNCTION();
    69 
    59 }
    70     CX_DEBUG_EXIT_FUNCTION();
    60 
    71 }
       
    72 
       
    73 
       
    74 
       
    75 /*!
       
    76 * Initializes resources before any test cases.
       
    77 */
       
    78 void UnitTestCxeImageDataItemSymbian::initTestCase()
       
    79 {
       
    80     CX_DEBUG_ENTER_FUNCTION();
       
    81 
       
    82     // create folder for storing temporary image files
       
    83     QDir dir;
       
    84     dir.mkpath(path());
       
    85 
       
    86     CX_DEBUG_EXIT_FUNCTION();
       
    87 }
       
    88 
       
    89 
       
    90 /*!
       
    91 * cleans up resources after last test case
       
    92 */
       
    93 void UnitTestCxeImageDataItemSymbian::cleanupTestCase()
       
    94 {
       
    95     CX_DEBUG_ENTER_FUNCTION();
       
    96 
       
    97     // remove temporary image files and folder
       
    98     QDir dir;
       
    99     QString filepath = path();
       
   100     dir.setPath(filepath);
       
   101 
       
   102     QStringList filters;
       
   103     filters << "*.jpg";
       
   104     dir.setNameFilters(filters);
       
   105     
       
   106     QStringList filenames = dir.entryList(filters);
       
   107     
       
   108     // delete temporary created files
       
   109     foreach(const QString &file, filenames) {
       
   110         dir.remove(filepath + file);
       
   111     }
       
   112     
       
   113     // delete the directory created for temporary saving image files.
       
   114     dir.rmpath(filepath);
       
   115 
       
   116     CX_DEBUG_EXIT_FUNCTION();
       
   117 }
       
   118 
       
   119 
       
   120 
       
   121 /*!
       
   122 * UnitTestCxeImageDataItemSymbian::testState
       
   123 */
    61 void UnitTestCxeImageDataItemSymbian::testSave()
   124 void UnitTestCxeImageDataItemSymbian::testSave()
    62 {
   125 {
    63     CX_DEBUG_ENTER_FUNCTION();
   126     CX_DEBUG_ENTER_FUNCTION();
    64     const int id = mImageDataItem->id();
   127     const int id = mImageDataItem->id();
    65 
   128 
    66     QSignalSpy deviceStateSpy(mImageDataItem, SIGNAL(imageSaved(CxeError::Id, const QString&, int)));
   129     QSignalSpy deviceStateSpy(mImageDataItem, SIGNAL(imageSaved(CxeError::Id, const QString&, int)));
    67     QVERIFY(deviceStateSpy.isValid());
   130     QVERIFY(deviceStateSpy.isValid());
    68 
   131 
    69     int returnValue = mImageDataItem->save();
   132     CxeError::Id err = mImageDataItem->save();
    70 
   133 
    71     QVERIFY(mImageDataItem->state() == CxeImageDataItem::Saved);
   134     QVERIFY(mImageDataItem->state() == CxeImageDataItem::Saved);
    72     QVERIFY(returnValue == KErrNone);
   135     QCOMPARE(err, CxeError::None);
    73 
   136 
    74     QCOMPARE( deviceStateSpy.count(), 1 );
   137     QCOMPARE( deviceStateSpy.count(), 1 );
    75     if (deviceStateSpy.count() > 0) {
   138     if (deviceStateSpy.count() > 0) {
    76         QList<QVariant> initModeArguments = deviceStateSpy.takeFirst();
   139         QList<QVariant> initModeArguments = deviceStateSpy.takeFirst();
    77         QVERIFY( initModeArguments.at(0).toInt() == CxeError::None );
   140         QCOMPARE(initModeArguments.at(0).value<CxeError::Id>(), CxeError::None);
    78         QVERIFY( initModeArguments.at(1).toString().compare(mPath) == 0);
   141         QVERIFY( initModeArguments.at(1).toString().compare(mFilename) == 0);
    79         QVERIFY( initModeArguments.at(2).toInt() == id);
   142         QVERIFY( initModeArguments.at(2).toInt() == id);
    80     }
   143     }
    81     CX_DEBUG_EXIT_FUNCTION();
   144 
    82 }
   145     // try to test possible fail cases.
    83 
   146     QString filename = NULL;
    84 
       
    85 void UnitTestCxeImageDataItemSymbian::testSaveFail()
       
    86 {
       
    87     CX_DEBUG_ENTER_FUNCTION();
       
    88     int returnValue;
       
    89     int index = 1;
       
    90     QString path = NULL;
       
    91     QByteArray data = "";
   147     QByteArray data = "";
    92 
   148 
    93     returnValue = mImageDataItem->save();
   149     CX_DEBUG(("UnitTestCxeImageDataItemSymbian::testSave <> Invalid arguments"));
       
   150     cleanup();
       
   151     mImageDataItem = new CxeImageDataItemUnit(mImageCounter, data, filename, false);
       
   152     err = mImageDataItem->save();
       
   153     QVERIFY(mImageDataItem->state() == CxeImageDataItem::SaveFailed);
       
   154     
       
   155     // Arugments are not valid, leaves with KErrArgument which is mapped internally to CxError::General.
       
   156     QCOMPARE(err, CxeError::General);
       
   157 
       
   158     CX_DEBUG(("UnitTestCxeImageDataItemSymbian::testSave <> Invalid path"));
       
   159     cleanup();
       
   160     filename = dummyPath("C");
       
   161     mImageDataItem = new CxeImageDataItemUnit(mImageCounter, data, filename, false);
       
   162     err = mImageDataItem->save();
       
   163     QVERIFY(mImageDataItem->state() == CxeImageDataItem::SaveFailed);
       
   164     QVERIFY(err != CxeError::None);
       
   165 
       
   166     CX_DEBUG(("UnitTestCxeImageDataItemSymbian::testSave <> invalid drive"));
       
   167     cleanup();
       
   168     filename = dummyPath("");
       
   169     mImageDataItem = new CxeImageDataItemUnit(mImageCounter, data, filename, false);
       
   170     err = mImageDataItem->save();
       
   171     QVERIFY(mImageDataItem->state() == CxeImageDataItem::SaveFailed);
       
   172     QVERIFY(err != CxeError::None);
       
   173 
       
   174     CX_DEBUG(("UnitTestCxeImageDataItemSymbian::testSave <> invalid drive - 2"));
       
   175     cleanup();
       
   176     filename = dummyPath("12");
       
   177     mImageDataItem = new CxeImageDataItemUnit(mImageCounter, data, filename, false);
       
   178     err = mImageDataItem->save();
       
   179     QVERIFY(mImageDataItem->state() == CxeImageDataItem::SaveFailed);
       
   180     QVERIFY(err != CxeError::None);
       
   181 
       
   182     CX_DEBUG(("UnitTestCxeImageDataItemSymbian::testSave <> invalid drive - 3"));
       
   183     cleanup();
       
   184     filename = dummyPath("Edata");
       
   185     mImageDataItem = new CxeImageDataItemUnit(mImageCounter, data, filename, false);
       
   186     err = mImageDataItem->save();
       
   187     QVERIFY(mImageDataItem->state() == CxeImageDataItem::SaveFailed);
       
   188     QVERIFY(err != CxeError::None);
       
   189 
       
   190     CX_DEBUG_EXIT_FUNCTION();
       
   191 }
       
   192 
       
   193 void UnitTestCxeImageDataItemSymbian::testPath()
       
   194 {
       
   195     CX_DEBUG_ENTER_FUNCTION();
       
   196     QVERIFY(mImageDataItem->path().compare(mFilename) == 0);
       
   197     CX_DEBUG_EXIT_FUNCTION();
       
   198 }
       
   199 
       
   200 
       
   201 
       
   202 
       
   203 /*!
       
   204 * UnitTestCxeImageDataItemSymbian::testState
       
   205 */
       
   206 void UnitTestCxeImageDataItemSymbian::testState()
       
   207 {
       
   208     CX_DEBUG_ENTER_FUNCTION();
       
   209     mImageDataItem->save();
    94     QVERIFY(mImageDataItem->state() == CxeImageDataItem::Saved);
   210     QVERIFY(mImageDataItem->state() == CxeImageDataItem::Saved);
    95     QVERIFY(returnValue == KErrArgument);
   211     CX_DEBUG_EXIT_FUNCTION();
    96 
   212 }
    97     delete mImageDataItem;
   213 
    98     mImageDataItem = NULL;
   214 
    99     mImageDataItem = new CxeImageDataItemUnit( index, data, path, false );
   215 
   100     returnValue = mImageDataItem->save();
   216 /*!
   101     QVERIFY(mImageDataItem->state() == CxeImageDataItem::SaveFailed);
   217 * UnitTestCxeImageDataItemSymbian::testId
   102     QVERIFY(returnValue == KErrArgument);
   218 */
   103 
   219 void UnitTestCxeImageDataItemSymbian::testId()
   104     delete mImageDataItem;
   220 {
   105     mImageDataItem = NULL;
   221     CX_DEBUG_ENTER_FUNCTION();
   106     QString filename = generateImageFileNameWithLetter(++index, "C");
   222 	// checking if initialization of image data item is successful.
   107     mImageDataItem = new CxeImageDataItemUnit( index, data, filename, false );
   223     QCOMPARE(mImageDataItem->id(), mImageCounter);
   108     returnValue = mImageDataItem->save();
   224     mImageDataItem->save();
   109     QVERIFY(mImageDataItem->state() == CxeImageDataItem::SaveFailed);
   225     QCOMPARE(mImageDataItem->id(), mImageCounter);
   110     QVERIFY(returnValue != KErrNone);
   226     CX_DEBUG_EXIT_FUNCTION();
   111 
   227 }
   112     qDebug() << "UnitTestCxeImageDataItemSymbian::testSaveFail3 =>";
   228 
   113     delete mImageDataItem;
   229 
   114     mImageDataItem = NULL;
   230 /*!
   115     filename = generateImageFileNameWithLetter(++index, "");
   231 * UnitTestCxeImageDataItemSymbian::testIsLocationEnabled
   116     mImageDataItem = new CxeImageDataItemUnit( index, data, filename, false );
   232 */
   117     returnValue = mImageDataItem->save();
   233 void UnitTestCxeImageDataItemSymbian::testIsLocationEnabled()
   118     QVERIFY(mImageDataItem->state() == CxeImageDataItem::SaveFailed);
   234 {
   119     QVERIFY(returnValue != KErrNone);
   235     CX_DEBUG_ENTER_FUNCTION();
   120 
   236 
   121     qDebug() << "UnitTestCxeImageDataItemSymbian::testSaveFail4 =>";
   237 	// spl case to check if location is enabled	
   122     delete mImageDataItem;
   238 	cleanup();
   123     mImageDataItem = NULL;
   239     bool enableLocation(true);
   124     filename = generateImageFileNameWithLetter(++index, "12");
   240     QByteArray data = "1234";
   125     mImageDataItem = new CxeImageDataItemUnit( index, data, filename, false );
   241     QString filename = generateImageFileName();
   126     returnValue = mImageDataItem->save();
   242     mImageDataItem = new CxeImageDataItemUnit(mImageCounter, data, filename, enableLocation);
   127     QVERIFY(mImageDataItem->state() == CxeImageDataItem::SaveFailed);
   243     QCOMPARE(mImageDataItem->isLocationEnabled(), enableLocation);
   128     QVERIFY(returnValue != KErrNone);
   244 
   129 
   245     CX_DEBUG_EXIT_FUNCTION();
   130     qDebug() << "UnitTestCxeImageDataItemSymbian::testSaveFail5 =>";
   246 }
   131     delete mImageDataItem;
   247 
   132     mImageDataItem = NULL;
   248 
   133     filename = generateImageFileNameWithLetter(++index, "Edata");
   249 /*!
   134     mImageDataItem = new CxeImageDataItemUnit( index, data, filename, false );
   250 * Generates filename.
   135     returnValue = mImageDataItem->save();
   251 * @param counter is associated with image file name.
   136     QVERIFY(mImageDataItem->state() == CxeImageDataItem::SaveFailed);
   252 */
   137     QVERIFY(returnValue != KErrNone);
   253 QString UnitTestCxeImageDataItemSymbian::generateImageFileName()
   138 
   254 {
   139     CX_DEBUG_EXIT_FUNCTION();
   255     CX_DEBUG_ENTER_FUNCTION();
   140 }
   256     
   141 
   257     mImageCounter++;
   142 void UnitTestCxeImageDataItemSymbian::testPath()
       
   143 {
       
   144     CX_DEBUG_ENTER_FUNCTION();
       
   145     QVERIFY(mImageDataItem->path().compare(mPath) == 0);
       
   146 }
       
   147 
       
   148 
       
   149 QString UnitTestCxeImageDataItemSymbian::generateImageFileName(int counter)
       
   150 {
       
   151     CX_DEBUG_ENTER_FUNCTION();
       
   152     QString monthName = QDate::currentDate().toString("yyyyMM");
       
   153     QString filename;
   258     QString filename;
   154     filename.sprintf("E:\\Images\\Camera\\%s\\%sA0\\%04d_Nokia5800.jpg",
   259     filename.sprintf("%04d_Nokia.jpg", mImageCounter);
   155             monthName.toAscii().data(),
   260     filename = path() + filename;
   156             monthName.toAscii().data(),
   261 
   157             counter);
       
   158     CX_DEBUG_EXIT_FUNCTION();
   262     CX_DEBUG_EXIT_FUNCTION();
   159     return filename;
   263     return filename;
   160 }
   264 }
   161 
   265 
   162 QString UnitTestCxeImageDataItemSymbian::generateImageFileNameWithLetter(int counter, QString letter)
   266 /*!
   163 {
   267 * Create path for saving images to be used in testcases.
   164     CX_DEBUG_ENTER_FUNCTION();
   268 */
   165     QString monthName = QDate::currentDate().toString("yyyyMM");
   269 QString UnitTestCxeImageDataItemSymbian::path()
       
   270 {
       
   271     CX_DEBUG_ENTER_FUNCTION();
       
   272     
       
   273     QString path;
       
   274 
       
   275 #ifdef __WINSCW__
       
   276     path.sprintf("c:\\data\\testimages\\");
       
   277 #else
       
   278     path.sprintf("e:\\testimages\\");
       
   279 #endif
       
   280 
       
   281     CX_DEBUG_EXIT_FUNCTION();
       
   282     
       
   283     return path;
       
   284 }
       
   285 
       
   286 
       
   287 
       
   288 /*!
       
   289 * Create a dummy invalid path for testcases.
       
   290 */
       
   291 QString UnitTestCxeImageDataItemSymbian::dummyPath(const QString &drive)
       
   292 {
       
   293     CX_DEBUG_ENTER_FUNCTION();
       
   294     mImageCounter++;
   166     QString filename;
   295     QString filename;
   167     filename.sprintf("%s:\\Images\\Camera\\%s\\%sA0\\%04d_Nokia5800.jpg",
   296     filename.sprintf("%s:\\Dummy\\Camera\\%04d_Nokia.jpg",
   168             letter.toAscii().data(),
   297             drive.toAscii().data(),
   169             monthName.toAscii().data(),
   298             mImageCounter);
   170             monthName.toAscii().data(),
       
   171             counter);
       
   172     CX_DEBUG_EXIT_FUNCTION();
   299     CX_DEBUG_EXIT_FUNCTION();
   173     return filename;
   300     return filename;
   174 }
   301 }
   175 
   302 
   176 // main() function non-GUI testing
   303 // main() function non-GUI testing