phonebookengines/cntactions/tsrc/mt_cntactions/testrunner.cpp
changeset 46 efe85016a067
child 81 640d30f4fb64
equal deleted inserted replaced
40:b46a585f6909 46:efe85016a067
       
     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 "testrunner.h"
       
    19 #include <QtTest/QtTest>
       
    20 #include <QDir>
       
    21 #include <stdio.h>
       
    22 
       
    23 const char testFunctionElement[] = "TestFunction";
       
    24 const char incidentElement[] = "Incident";
       
    25 const char descriptionElement[] = "Description";
       
    26 const char nameAttr[] = "name";
       
    27 const char typeAttr[] = "type";
       
    28 const char fileAttr[] = "file";
       
    29 const char lineAttr[] = "line";
       
    30 const char attrValueFail[] = "fail";
       
    31 
       
    32 
       
    33 TestRunner::TestRunner(const QString& name)
       
    34 : mTestCount(0),
       
    35   mParsingIncidentElement(false),
       
    36   mParsingDescriptionElement(false),
       
    37   mCurrentTestFailed(false),
       
    38   mCurrentTestFailureLine(0)
       
    39 {
       
    40     mTestRunParams.append(name);
       
    41     mTestRunParams.append("-xml");
       
    42     mTestRunParams.append("-o");
       
    43     mHomeDir = QDir::homePath();
       
    44     mTestRunParams.append(QString()); // Initial result file name
       
    45 
       
    46     if (!mHomeDir.endsWith(QString::fromAscii("/")))
       
    47         mHomeDir += QString::fromAscii("/");
       
    48 }
       
    49 
       
    50 TestRunner::~TestRunner()
       
    51 {
       
    52 }
       
    53 
       
    54 int TestRunner::runTests(QObject& testObject)
       
    55 {
       
    56     QString className(testObject.metaObject()->className());
       
    57     printf("Running tests for %s ... ", className.toUtf8().data());
       
    58     QString resultFileName = mHomeDir + className + ".xml";
       
    59     mTestRunParams.replace(mTestRunParams.count()-1,resultFileName);
       
    60     int errorsBefore = mErrors.count();
       
    61     int error = QTest::qExec(&testObject, mTestRunParams);
       
    62     parse(resultFileName);
       
    63     printf("Failures: %d\n",mErrors.count()-errorsBefore);
       
    64     fflush(stdout);
       
    65     return error;
       
    66 }
       
    67 
       
    68 void TestRunner::printResults()
       
    69 {
       
    70     printf("\nTests executed: %d\n",mTestCount);
       
    71     if (mErrors.count() > 0) {
       
    72         printf("Failures (%d):\n", mErrors.count());
       
    73         foreach(QString error, mErrors) {
       
    74             printf("\n%s", error.toUtf8().data());
       
    75         }
       
    76         printf("\n");
       
    77     } else {
       
    78         printf("All passed.\n\n");
       
    79     }
       
    80     fflush(stdout);
       
    81     
       
    82     //To write in file
       
    83     QFile file("C:\\TestResult.txt");
       
    84     if(file.open(QIODevice::WriteOnly)) 
       
    85     {
       
    86         QTextStream ts( &file );
       
    87         ts << "Tests executed: " << mTestCount << "\n";
       
    88         if (mErrors.count() > 0)
       
    89         {
       
    90             ts <<"Failures : " << mErrors.count() << "\n";
       
    91             foreach(QString error, mErrors)
       
    92                 {
       
    93                     ts << error.toUtf8().data();
       
    94                 }
       
    95             ts << "\n";
       
    96         }
       
    97         else
       
    98         {
       
    99             ts<< "All passed.\n\n";
       
   100         }
       
   101         
       
   102         ts << endl;
       
   103         file.close();
       
   104     } 
       
   105 }
       
   106 
       
   107 void TestRunner::parse(const QString& fileName)
       
   108 {
       
   109     QFile file(fileName);
       
   110     QXmlInputSource inputSource(&file);
       
   111     QXmlSimpleReader reader;
       
   112     reader.setContentHandler(this);
       
   113     reader.parse(inputSource);
       
   114 }
       
   115 
       
   116 bool TestRunner::startElement(
       
   117     const QString& /*namespaceURI*/, 
       
   118     const QString& /*localName*/, 
       
   119     const QString& qName, 
       
   120     const QXmlAttributes& atts)
       
   121 {
       
   122     if (qName == QString::fromAscii(testFunctionElement)) {
       
   123         mTestCount++;
       
   124         mCurrentTestName = atts.value(QString::fromAscii(nameAttr));
       
   125         return true;
       
   126     }
       
   127     if (qName == QString::fromAscii(incidentElement)) {
       
   128         mParsingIncidentElement = true;
       
   129         if (atts.value(QString::fromAscii(typeAttr)) == QString::fromAscii(attrValueFail)) {
       
   130             mCurrentTestFailed = true;
       
   131             mCurrentTestFile = atts.value(QString::fromAscii(fileAttr));
       
   132             mCurrentTestFailureLine = atts.value(QString::fromAscii(lineAttr)).toInt();
       
   133         }
       
   134         return true;
       
   135     }
       
   136     mParsingDescriptionElement =
       
   137         (qName == QString::fromAscii(descriptionElement));
       
   138     return true;
       
   139 }
       
   140 
       
   141 bool TestRunner::endElement(
       
   142     const QString& /*namespaceURI*/,
       
   143     const QString& /*localName*/,
       
   144     const QString& qName)
       
   145 {
       
   146     if (qName == QString::fromAscii(incidentElement)) {
       
   147         mParsingIncidentElement = false;
       
   148         mCurrentTestFailed = false;
       
   149         return true;
       
   150     }
       
   151     if (qName == QString::fromAscii(descriptionElement)) {
       
   152         mParsingDescriptionElement = false;
       
   153     }    
       
   154     return true;
       
   155 }
       
   156 
       
   157 bool TestRunner::characters(const QString& ch)
       
   158 {
       
   159     if (mParsingIncidentElement && 
       
   160         mParsingDescriptionElement &&
       
   161         mCurrentTestFailed) {
       
   162         QByteArray testResult = mCurrentTestName.toAscii() + " failed:\n";
       
   163         testResult += "File: ";
       
   164         testResult += mCurrentTestFile.toAscii();
       
   165         testResult += "\n";
       
   166         testResult += "Line: ";
       
   167         testResult += QByteArray::number(mCurrentTestFailureLine);
       
   168         testResult += "\n";
       
   169         testResult += "Reason: ";
       
   170         testResult += ch.toAscii();
       
   171         testResult += "\n";
       
   172         mErrors.append(QString::fromAscii(testResult.data()));
       
   173     }
       
   174     return true;
       
   175 }
       
   176