src/testlib/qtestcase.cpp
changeset 30 5dc02b23752f
parent 19 fcece45ef507
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
   844 
   844 
   845     static int keyDelay = -1;
   845     static int keyDelay = -1;
   846     static int mouseDelay = -1;
   846     static int mouseDelay = -1;
   847     static int eventDelay = -1;
   847     static int eventDelay = -1;
   848     static int keyVerbose = -1;
   848     static int keyVerbose = -1;
       
   849 #if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN)
       
   850     static bool noCrashHandler = false;
       
   851 #endif
   849 
   852 
   850 void filter_unprintable(char *str)
   853 void filter_unprintable(char *str)
   851 {
   854 {
   852     char *idx = str;
   855     char *idx = str;
   853     while (*idx) {
   856     while (*idx) {
   870     str[size - 1] = '\0';
   873     str[size - 1] = '\0';
   871 
   874 
   872     filter_unprintable(str);
   875     filter_unprintable(str);
   873 
   876 
   874     return res;
   877     return res;
       
   878 }
       
   879 
       
   880 /*! \internal
       
   881     Invoke a method of the object without generating warning if the method does not exist
       
   882  */
       
   883 static void invokeMethod(QObject *obj, const char *methodName)
       
   884 {
       
   885     const QMetaObject *metaObject = obj->metaObject();
       
   886     int funcIndex = metaObject->indexOfMethod(methodName);
       
   887     if (funcIndex >= 0) {
       
   888         QMetaMethod method = metaObject->method(funcIndex);
       
   889         method.invoke(obj, Qt::DirectConnection);
       
   890     }
   875 }
   891 }
   876 
   892 
   877 bool Q_TESTLIB_EXPORT defaultKeyVerbose()
   893 bool Q_TESTLIB_EXPORT defaultKeyVerbose()
   878 {
   894 {
   879     if (keyVerbose == -1) {
   895     if (keyVerbose == -1) {
   974          " -keydelay ms      : Set default delay for keyboard simulation to ms milliseconds\n"
   990          " -keydelay ms      : Set default delay for keyboard simulation to ms milliseconds\n"
   975          " -mousedelay ms    : Set default delay for mouse simulation to ms milliseconds\n"
   991          " -mousedelay ms    : Set default delay for mouse simulation to ms milliseconds\n"
   976          " -keyevent-verbose : Turn on verbose messages for keyboard simulation\n"
   992          " -keyevent-verbose : Turn on verbose messages for keyboard simulation\n"
   977          " -maxwarnings n    : Sets the maximum amount of messages to output.\n"
   993          " -maxwarnings n    : Sets the maximum amount of messages to output.\n"
   978          "                     0 means unlimited, default: 2000\n"
   994          "                     0 means unlimited, default: 2000\n"
       
   995 #if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN)
       
   996          " -nocrashhandler   : Disables the crash handler\n"
       
   997 #endif
   979          "\n"
   998          "\n"
   980          " Benchmark related options:\n"
   999          " Benchmark related options:\n"
   981 #ifdef QTESTLIB_USE_VALGRIND
  1000 #ifdef QTESTLIB_USE_VALGRIND
   982         " -callgrind      : Use callgrind to time benchmarks\n"
  1001         " -callgrind      : Use callgrind to time benchmarks\n"
   983 #endif
  1002 #endif
  1054                 printf("-maxwarnings needs an extra parameter with the amount of warnings\n");
  1073                 printf("-maxwarnings needs an extra parameter with the amount of warnings\n");
  1055                 exit(1);
  1074                 exit(1);
  1056             } else {
  1075             } else {
  1057                 QTestLog::setMaxWarnings(qToInt(argv[++i]));
  1076                 QTestLog::setMaxWarnings(qToInt(argv[++i]));
  1058             }
  1077             }
       
  1078 #if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN)
       
  1079         } else if (strcmp(argv[i], "-nocrashhandler") == 0) {
       
  1080             QTest::noCrashHandler = true;
       
  1081 #endif
  1059         } else if (strcmp(argv[i], "-keyevent-verbose") == 0) {
  1082         } else if (strcmp(argv[i], "-keyevent-verbose") == 0) {
  1060             QTest::keyVerbose = 1;
  1083             QTest::keyVerbose = 1;
  1061 #ifdef QTESTLIB_USE_VALGRIND
  1084 #ifdef QTESTLIB_USE_VALGRIND
  1062         } else if (strcmp(argv[i], "-callgrind") == 0) {
  1085         } else if (strcmp(argv[i], "-callgrind") == 0) {
  1063             if (QBenchmarkValgrindUtils::haveValgrind())
  1086             if (QBenchmarkValgrindUtils::haveValgrind())
  1201 
  1224 
  1202         /* Benchmarking: for each accumulation iteration*/
  1225         /* Benchmarking: for each accumulation iteration*/
  1203         bool invokeOk;
  1226         bool invokeOk;
  1204         do {
  1227         do {
  1205             QTestResult::setCurrentTestLocation(QTestResult::InitFunc);
  1228             QTestResult::setCurrentTestLocation(QTestResult::InitFunc);
  1206             QMetaObject::invokeMethod(QTest::currentTestObject, "init");
  1229             invokeMethod(QTest::currentTestObject, "init()");
  1207             if (QTestResult::skipCurrentTest())
  1230             if (QTestResult::skipCurrentTest())
  1208                 break;
  1231                 break;
  1209 
  1232 
  1210             QTestResult::setCurrentTestLocation(QTestResult::Func);
  1233             QTestResult::setCurrentTestLocation(QTestResult::Func);
  1211 
  1234 
  1221                                                  Qt::DirectConnection);
  1244                                                  Qt::DirectConnection);
  1222             if (!invokeOk)
  1245             if (!invokeOk)
  1223                 QTestResult::addFailure("Unable to execute slot", __FILE__, __LINE__);
  1246                 QTestResult::addFailure("Unable to execute slot", __FILE__, __LINE__);
  1224 
  1247 
  1225             QTestResult::setCurrentTestLocation(QTestResult::CleanupFunc);
  1248             QTestResult::setCurrentTestLocation(QTestResult::CleanupFunc);
  1226             QMetaObject::invokeMethod(QTest::currentTestObject, "cleanup");
  1249             invokeMethod(QTest::currentTestObject, "cleanup()");
  1227             QTestResult::setCurrentTestLocation(QTestResult::NoWhere);
  1250             QTestResult::setCurrentTestLocation(QTestResult::NoWhere);
  1228 
  1251 
  1229             // If this test method has a benchmark, repeat until all measurements are
  1252             // If this test method has a benchmark, repeat until all measurements are
  1230             // acceptable.
  1253             // acceptable.
  1231             // The QBENCHMARK macro increases the number of iterations for each run until
  1254             // The QBENCHMARK macro increases the number of iterations for each run until
  1288         if (!gTable->isEmpty())
  1311         if (!gTable->isEmpty())
  1289             QTestResult::setCurrentGlobalTestData(gTable->testData(curGlobalDataIndex));
  1312             QTestResult::setCurrentGlobalTestData(gTable->testData(curGlobalDataIndex));
  1290 
  1313 
  1291         if (curGlobalDataIndex == 0) {
  1314         if (curGlobalDataIndex == 0) {
  1292             QTestResult::setCurrentTestLocation(QTestResult::DataFunc);
  1315             QTestResult::setCurrentTestLocation(QTestResult::DataFunc);
  1293             QTest::qt_snprintf(member, 512, "%s_data", slot);
  1316             QTest::qt_snprintf(member, 512, "%s_data()", slot);
  1294             QMetaObject::invokeMethod(QTest::currentTestObject, member, Qt::DirectConnection);
  1317             invokeMethod(QTest::currentTestObject, member);
       
  1318 
  1295             // if we encounter a SkipAll in the _data slot, we skip the whole
  1319             // if we encounter a SkipAll in the _data slot, we skip the whole
  1296             // testfunction, no matter how much global data exists
  1320             // testfunction, no matter how much global data exists
  1297             if (QTestResult::skipCurrentTest()) {
  1321             if (QTestResult::skipCurrentTest()) {
  1298                 QTestResult::setCurrentGlobalTestData(0);
  1322                 QTestResult::setCurrentGlobalTestData(0);
  1299                 break;
  1323                 break;
  1454     QTestLog::startLogging();
  1478     QTestLog::startLogging();
  1455 
  1479 
  1456     QTestResult::setCurrentTestFunction("initTestCase");
  1480     QTestResult::setCurrentTestFunction("initTestCase");
  1457     QTestResult::setCurrentTestLocation(QTestResult::DataFunc);
  1481     QTestResult::setCurrentTestLocation(QTestResult::DataFunc);
  1458     QTestTable::globalTestTable();
  1482     QTestTable::globalTestTable();
  1459     QMetaObject::invokeMethod(testObject, "initTestCase_data", Qt::DirectConnection);
  1483     invokeMethod(testObject, "initTestCase_data()");
  1460 
  1484 
  1461     if (!QTestResult::skipCurrentTest() && !QTest::currentTestFailed()) {
  1485     if (!QTestResult::skipCurrentTest() && !QTest::currentTestFailed()) {
  1462         QTestResult::setCurrentTestLocation(QTestResult::InitFunc);
  1486         QTestResult::setCurrentTestLocation(QTestResult::InitFunc);
  1463         QMetaObject::invokeMethod(testObject, "initTestCase");
  1487         invokeMethod(testObject, "initTestCase()");
  1464 
  1488 
  1465         // finishedCurrentTestFunction() resets QTestResult::testFailed(), so use a local copy.
  1489         // finishedCurrentTestFunction() resets QTestResult::testFailed(), so use a local copy.
  1466         const bool previousFailed = QTestResult::testFailed();
  1490         const bool previousFailed = QTestResult::testFailed();
  1467         QTestResult::finishedCurrentTestFunction();
  1491         QTestResult::finishedCurrentTestFunction();
  1468 
  1492 
  1486             }
  1510             }
  1487         }
  1511         }
  1488 
  1512 
  1489         QTestResult::setSkipCurrentTest(false);
  1513         QTestResult::setSkipCurrentTest(false);
  1490         QTestResult::setCurrentTestFunction("cleanupTestCase");
  1514         QTestResult::setCurrentTestFunction("cleanupTestCase");
  1491         QMetaObject::invokeMethod(testObject, "cleanupTestCase");
  1515         invokeMethod(testObject, "cleanupTestCase()");
  1492     }
  1516     }
  1493     QTestResult::finishedCurrentTestFunction();
  1517     QTestResult::finishedCurrentTestFunction();
  1494     QTestResult::setCurrentTestFunction(0);
  1518     QTestResult::setCurrentTestFunction(0);
  1495     QTestTable::clearGlobalTestTable();
  1519     QTestTable::clearGlobalTestTable();
  1496 
  1520 
  1539     for (int i = 0; fatalSignals[i]; ++i) {
  1563     for (int i = 0; fatalSignals[i]; ++i) {
  1540         sigaction(fatalSignals[i], &act, &oldact);
  1564         sigaction(fatalSignals[i], &act, &oldact);
  1541 #ifndef Q_WS_QWS
  1565 #ifndef Q_WS_QWS
  1542         // Don't overwrite any non-default handlers
  1566         // Don't overwrite any non-default handlers
  1543         // however, we need to replace the default QWS handlers
  1567         // however, we need to replace the default QWS handlers
  1544         if (oldact.sa_flags & SA_SIGINFO || oldact.sa_handler != SIG_DFL) {
  1568         if (
       
  1569 #ifdef SA_SIGINFO
       
  1570             oldact.sa_flags & SA_SIGINFO ||
       
  1571 #endif
       
  1572             oldact.sa_handler != SIG_DFL) {
  1545             sigaction(fatalSignals[i], &oldact, 0);
  1573             sigaction(fatalSignals[i], &oldact, 0);
  1546         } else
  1574         } else
  1547 #endif
  1575 #endif
  1548         {
  1576         {
  1549             sigaddset(&handledSignals, fatalSignals[i]);
  1577             sigaddset(&handledSignals, fatalSignals[i]);
  1679 
  1707 
  1680     } else
  1708     } else
  1681 #endif
  1709 #endif
  1682     {
  1710     {
  1683 #if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN)
  1711 #if defined(Q_OS_UNIX) && !defined(Q_OS_SYMBIAN)
  1684         FatalSignalHandler handler;
  1712         QScopedPointer<FatalSignalHandler> handler;
       
  1713         if (!noCrashHandler)
       
  1714             handler.reset(new FatalSignalHandler);
  1685 #endif
  1715 #endif
  1686         qInvokeTestMethods(testObject);
  1716         qInvokeTestMethods(testObject);
  1687     }
  1717     }
  1688 
  1718 
  1689 #ifndef QT_NO_EXCEPTIONS
  1719 #ifndef QT_NO_EXCEPTIONS