cellular/telephonysettings/xqbindings/psetwrapper/tsrc/common/testutilities.h
branchRCL_3
changeset 20 987c9837762f
parent 19 7d48bed6ce0c
child 21 0a6dd2dc9970
equal deleted inserted replaced
19:7d48bed6ce0c 20:987c9837762f
     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 #ifndef TESTUTILITIES_H
       
    19 #define TESTUTILITIES_H
       
    20 
       
    21 #include <qobject.h>
       
    22 #include <qglobal.h>
       
    23 #include <qlist.h>
       
    24 #include <qmetaobject.h>
       
    25 #include <QDebug>
       
    26 
       
    27 /*! Provides services for OOM simulation and memory leak detection. */
       
    28 class MemoryAllocator : public QObject
       
    29 {
       
    30     Q_OBJECT
       
    31     
       
    32 public:
       
    33     
       
    34     /*! Activates OOM simulation. */
       
    35     static void enableOomSimulation();
       
    36     
       
    37     /*! Disables OOM simulation. */
       
    38     static void disableOomSimulation();
       
    39     
       
    40     /*! Checks whether OOM simulation is active. */
       
    41     static bool isOomSimulationEnabled();
       
    42     
       
    43     /*! Returns current alloc fail index. */
       
    44     static int currentAllocFailIndex();
       
    45     
       
    46     /*! Allocates memory. */
       
    47     static void* alloc(std::size_t sz);
       
    48     
       
    49     /*! Frees allocated memory. */
       
    50     static void free(void *memoryAddress);
       
    51     
       
    52     /*! Checks that all memory allocated through this allocator is freed. */
       
    53     static void verifyMemoryAllocations();
       
    54 
       
    55 private:
       
    56     
       
    57     static bool m_isOomSimulationEnabled;
       
    58     static int m_numOfAllocsSinceLastFail;
       
    59     static int m_allocFailIndex;
       
    60     static QList<void*> m_allocList;
       
    61 };
       
    62 
       
    63 /*! Provides services to run QTest cases with OOM simulation. */
       
    64 class OomTestExecuter : public QObject
       
    65 {
       
    66     Q_OBJECT
       
    67     
       
    68 public:
       
    69     
       
    70     /*! Runs specified test case with OOM simulation. */
       
    71     template <typename CLASS>
       
    72     static void runTest(CLASS& testObject, void(CLASS::*testMethod)())
       
    73     {
       
    74         MemoryAllocator::enableOomSimulation();
       
    75 
       
    76         bool exceptionCaught = false;
       
    77         do {
       
    78             exceptionCaught = false;
       
    79             int currentAllocFailIndex = 
       
    80                 MemoryAllocator::currentAllocFailIndex();
       
    81             
       
    82             try {
       
    83                 try {
       
    84                     QMetaObject::invokeMethod(
       
    85                         &testObject, "init", Qt::DirectConnection);
       
    86                     (testObject.*testMethod)();
       
    87                 } catch (const std::bad_alloc &ex) {
       
    88                     exceptionCaught = true;
       
    89                     QMetaObject::invokeMethod(
       
    90                         &testObject, "cleanup", Qt::DirectConnection);
       
    91                 }
       
    92             // TODO: for some reason bad_alloc exception is corrupted to 
       
    93             // unknown exception and nested catch block is needed to be able to
       
    94             // handle situation. One level try-catch does not work for some reason.
       
    95             } catch (...) {
       
    96                 exceptionCaught = true;
       
    97                 QMetaObject::invokeMethod(
       
    98                     &testObject, "cleanup", Qt::DirectConnection);
       
    99                 if (currentAllocFailIndex == MemoryAllocator::currentAllocFailIndex()) {
       
   100                     qDebug() << "OomTestExecuter::runTest, ERROR: unexpected exception!";
       
   101                     throw;
       
   102                 }
       
   103             }
       
   104         } while(exceptionCaught);
       
   105         
       
   106         QMetaObject::invokeMethod(&testObject, "cleanup", Qt::DirectConnection);
       
   107         MemoryAllocator::disableOomSimulation();
       
   108     }
       
   109 
       
   110     /*! Runs specified test case with OOM simulation. */
       
   111     static void runTest(
       
   112         QObject &testObject, 
       
   113         const char *testMethod);
       
   114     
       
   115     /*! Runs all test cases in given QTest object with OOM simulation. */
       
   116     static void runAllTests(
       
   117         QObject &testObject, 
       
   118         const char *callingTestMethod);
       
   119 
       
   120 private:
       
   121     
       
   122     static bool isValidSlot(const QMetaMethod &sl);
       
   123 };
       
   124 
       
   125 #define EXPECT_EXCEPTION(statements)    \
       
   126 {                                       \
       
   127     bool exceptionDetected = false;     \
       
   128     try {                               \
       
   129         statements                      \
       
   130     } catch (...) {                     \
       
   131         exceptionDetected = true;       \
       
   132     }                                   \
       
   133     QVERIFY(true == exceptionDetected); \
       
   134 }
       
   135 
       
   136 #define QTEST_MAIN_S60(TestObject) \
       
   137 int main(int argc, char *argv[]) \
       
   138 { \
       
   139     char *new_argv[3]; \
       
   140     QCoreApplication app(argc, argv); \
       
   141     \
       
   142     QString str = "C:\\data\\" + QFileInfo(QCoreApplication::applicationFilePath()).baseName() + ".log"; \
       
   143     QByteArray   bytes  = str.toAscii(); \
       
   144     \
       
   145     char arg1[] = "-o"; \
       
   146     \
       
   147     new_argv[0] = argv[0]; \
       
   148     new_argv[1] = arg1; \
       
   149     new_argv[2] = bytes.data(); \
       
   150     \
       
   151     TestObject tc; \
       
   152     int result = QTest::qExec(&tc, 3, new_argv); \
       
   153     \
       
   154     MemoryAllocator::verifyMemoryAllocations(); \
       
   155     \
       
   156     return result;\
       
   157 }
       
   158 
       
   159 #endif  // TESTUTILITIES_H