testfwuis/symbianunittestui/qt/testrunner_s60.cpp
changeset 2 453d490c84a5
equal deleted inserted replaced
1:753e33780645 2:453d490c84a5
       
     1 /*
       
     2  * Copyright (c) 2010 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: Symbian C++ test runner Class.
       
    15  *
       
    16  */
       
    17 #include "testrunner_s60.h"
       
    18 #include "symbianunittestrunner.h"
       
    19 #include "widgettestrunner.h"
       
    20 _LIT( KOutputFileName, "SymbianUnitTestResults" );
       
    21 _LIT( KDefaultOutputFormat, "html" );
       
    22 
       
    23 TestRunnerPrivate* TestRunnerPrivate::NewLC(WidgetTestRunner* aPublicApi)
       
    24     {
       
    25     TestRunnerPrivate* self = new (ELeave) TestRunnerPrivate(aPublicApi);
       
    26     CleanupStack::PushL(self);
       
    27     self->ConstructL();
       
    28     return self;
       
    29     }
       
    30 
       
    31 TestRunnerPrivate* TestRunnerPrivate::NewL(WidgetTestRunner* aPublicApi)
       
    32     {
       
    33     TestRunnerPrivate* self = TestRunnerPrivate::NewLC(aPublicApi);
       
    34     CleanupStack::Pop(self);
       
    35     return self;
       
    36     }
       
    37 
       
    38 TestRunnerPrivate::TestRunnerPrivate(WidgetTestRunner* aPublicApi) :
       
    39     CActive(EPriorityStandard), d_ptr(aPublicApi), iTimeout(
       
    40             KSymbianUnitTestDefaultTimeout)
       
    41     {
       
    42     }
       
    43 
       
    44 void TestRunnerPrivate::ConstructL()
       
    45     {
       
    46     User::SetJustInTime(EFalse); // Do not stop on test case panics
       
    47 
       
    48     iTestDllNames = new (ELeave) CDesCArrayFlat(1);
       
    49     iTestCaseNames = new (ELeave) CDesCArrayFlat(1);
       
    50     iOutputFormat = KDefaultOutputFormat().AllocL();
       
    51     CActiveScheduler::Add(this);
       
    52     }
       
    53 
       
    54 TestRunnerPrivate::~TestRunnerPrivate()
       
    55     {
       
    56     Cancel();
       
    57     delete iTestDllNames;
       
    58     delete iTestCaseNames;
       
    59     delete iOutputFormat;
       
    60     }
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // From MSymbianUnitTestUiCallBack
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 void TestRunnerPrivate::InfoMsg(const TDesC& aMessage)
       
    67     {
       
    68     QString msg = QString::fromUtf16(aMessage.Ptr(), aMessage.Length());
       
    69     emit d_ptr->infoMsgReceived(msg);
       
    70     }
       
    71 
       
    72 int TestRunnerPrivate::SetTestDllNames(const QStringList& aDllNames)
       
    73     {
       
    74     TRAPD(err, SetTestDllNamesL(aDllNames));
       
    75     return err;
       
    76     }
       
    77 
       
    78 int TestRunnerPrivate::SetTestCaseNames(const QStringList& aCaseNames)
       
    79     {
       
    80     TRAPD(err, SetTestCaseNamesL(aCaseNames));
       
    81     return err;
       
    82     }
       
    83 
       
    84 int TestRunnerPrivate::SetOutputFormat(const QString& aFmt)
       
    85     {
       
    86     TRAPD(err, SetOutputFormatL(aFmt));
       
    87     return err;
       
    88     }
       
    89 
       
    90 int TestRunnerPrivate::ListTestCases(const QString& aDllName, QStringList& aCaseNames)
       
    91     {
       
    92     TRAPD(err, ListTestCasesL(aDllName, aCaseNames));
       
    93     return err;
       
    94     }
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // From MSymbianUnitTestUiCallBack
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 void TestRunnerPrivate::InfoMsg(const TDesC& aFormat, const TDesC& aMessage)
       
   101     {
       
   102     //    iConsole->Printf( aFormat, &aMessage );
       
   103     }
       
   104 
       
   105 void TestRunnerPrivate::TestsCount()
       
   106     {
       
   107 
       
   108     }
       
   109 
       
   110 void TestRunnerPrivate::TestsExecuted()
       
   111     {
       
   112     emit d_ptr->testExecuted(iPassedTestsCount + iFailedTestsCount);
       
   113     }
       
   114 
       
   115 void TestRunnerPrivate::SetTestDllNamesL(const QStringList& aDllNames)
       
   116     {
       
   117     if (!aDllNames.isEmpty())
       
   118         {
       
   119         CDesCArray* testDllNames = new (ELeave) CDesCArrayFlat(1);
       
   120         CleanupStack::PushL(testDllNames);
       
   121         foreach (QString tmp, aDllNames)
       
   122                 {
       
   123                 TPtrC myDescriptor(static_cast<const TUint16*> (tmp.utf16()),
       
   124                         tmp.length());
       
   125                 HBufC* buffer = myDescriptor.AllocL();
       
   126                 testDllNames->AppendL(buffer->Des());
       
   127                 }
       
   128         delete iTestDllNames;
       
   129         iTestDllNames = testDllNames;
       
   130         CleanupStack::Pop(testDllNames);
       
   131         }
       
   132     }
       
   133 
       
   134 void TestRunnerPrivate::SetTestCaseNamesL(const QStringList& aNames)
       
   135     {
       
   136     if (!aNames.isEmpty())
       
   137         {
       
   138         CDesCArray* testCaseNames = new (ELeave) CDesCArrayFlat(1);
       
   139         CleanupStack::PushL(testCaseNames);
       
   140         foreach (QString tmp, aNames)
       
   141                 {
       
   142                 TPtrC myDescriptor(static_cast<const TUint16*> (tmp.utf16()),
       
   143                         tmp.length());
       
   144                 HBufC* buffer = myDescriptor.AllocL();
       
   145                 testCaseNames->AppendL(buffer->Des());
       
   146                 }
       
   147         delete iTestCaseNames;
       
   148         iTestCaseNames = testCaseNames;
       
   149         CleanupStack::Pop(testCaseNames);
       
   150         }
       
   151     }
       
   152 
       
   153 void TestRunnerPrivate::SetAllocFailureSimulation(const TBool aSimulation)
       
   154     {
       
   155     iAllocFailureSimulation = aSimulation;
       
   156     }
       
   157 
       
   158 void TestRunnerPrivate::SetTimeout(const TInt aTimeout)
       
   159     {
       
   160     iTimeout = aTimeout;
       
   161     }
       
   162 
       
   163 void TestRunnerPrivate::SetOutputFormatL(const QString& aFormat)
       
   164     {
       
   165     TPtrC outputFormatPtr(aFormat.utf16());
       
   166     HBufC* outputFormat = outputFormatPtr.AllocL();
       
   167     delete iOutputFormat;
       
   168     iOutputFormat = outputFormat;
       
   169     }
       
   170 
       
   171 void TestRunnerPrivate::RunL()
       
   172     {
       
   173     iTestRunner = CSymbianUnitTestRunner::NewL(*this);
       
   174     iPassedTestsCount = 0;
       
   175     iFailedTestsCount = 0;
       
   176     TRAPD(err, iTestRunner->ExecuteTestsL(*iTestDllNames,
       
   177                     iAllocFailureSimulation,
       
   178                     KOutputFileName,
       
   179                     *iOutputFormat,
       
   180                     *iTestCaseNames,
       
   181                     iTimeout));
       
   182     if (err != KErrNone)
       
   183         {
       
   184         emit d_ptr->errorOccurred(err);
       
   185         }
       
   186     delete iTestRunner;
       
   187     iTestRunner = NULL;
       
   188     }
       
   189 
       
   190 // -----------------------------------------------------------------------------
       
   191 // From CActive
       
   192 // -----------------------------------------------------------------------------
       
   193 //
       
   194 void TestRunnerPrivate::DoCancel()
       
   195     {
       
   196     }
       
   197 
       
   198 void TestRunnerPrivate::RunTests()
       
   199     {
       
   200     // Set ready to run immediately
       
   201     TRequestStatus *status = &iStatus;
       
   202     User::RequestComplete(status, KErrNone);
       
   203     if (!IsActive())
       
   204         {
       
   205         SetActive();
       
   206         }
       
   207     }
       
   208 
       
   209 void TestRunnerPrivate::ListTestCasesL(const QString& aDllName,
       
   210         QStringList& aTestCases)
       
   211     {
       
   212     iTestRunner = CSymbianUnitTestRunner::NewL(*this);
       
   213     TPtrC dllName(static_cast<const TUint16*> (aDllName.utf16()),
       
   214             aDllName.length());
       
   215     CDesCArray* testCaseNames = new (ELeave) CDesCArrayFlat(1);
       
   216     CleanupStack::PushL(testCaseNames);
       
   217     iTestRunner->TestCaseNamesL(dllName, *testCaseNames);
       
   218     for (int i = 0; i < testCaseNames->Count(); ++i)
       
   219         {
       
   220         aTestCases << QString::fromUtf16((*testCaseNames)[i].Ptr(),
       
   221                 (*testCaseNames)[i].Length());
       
   222         }
       
   223     CleanupStack::PopAndDestroy(testCaseNames);
       
   224     delete iTestRunner;
       
   225     iTestRunner = NULL;
       
   226     }
       
   227 
       
   228 // -----------------------------------------------------------------------------
       
   229 // From MSymbianUnitTestUiCallBack
       
   230 // -----------------------------------------------------------------------------
       
   231 //
       
   232 void TestRunnerPrivate::TestPass(const TDesC& aTestCaseName)
       
   233     {
       
   234     QString caseName = QString::fromUtf16(aTestCaseName.Ptr(),
       
   235             aTestCaseName.Length());
       
   236     emit
       
   237     d_ptr->infoMsgReceived(caseName);
       
   238     emit
       
   239     d_ptr->testPassed(++iPassedTestsCount);
       
   240     TestsExecuted();
       
   241     }
       
   242 
       
   243 // -----------------------------------------------------------------------------
       
   244 // From MSymbianUnitTestUiCallBack
       
   245 // -----------------------------------------------------------------------------
       
   246 //
       
   247 void TestRunnerPrivate::TestFailed(const TDesC& aTestCaseName,
       
   248         const TDesC8& aErrMsg)
       
   249     {
       
   250     QString caseName = QString::fromUtf16(aTestCaseName.Ptr(),
       
   251             aTestCaseName.Length());
       
   252     QString reason = QString::fromUtf8(
       
   253             reinterpret_cast<const char*> (aErrMsg.Ptr()), aErrMsg.Length());
       
   254     emit
       
   255     d_ptr->testFailed(caseName, reason, ++iFailedTestsCount);
       
   256     TestsExecuted();
       
   257     }