mmtestenv/mmtestfw/Source/TestFramework/TestFrameworkMain.cpp
changeset 0 40261b775718
child 38 4269ca484c7b
equal deleted inserted replaced
-1:000000000000 0:40261b775718
       
     1 // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "TestFrameworkMain.h"
       
    17 #include "../../recog/TestFrameworkRecog.h"
       
    18 #include "script.h"
       
    19 #include "parseline.h"
       
    20 #include "Filename.h"
       
    21 #include "config.h"
       
    22 
       
    23 // Hurricane emulator only - start all services which we require to run tests.
       
    24 // Future enhancement :- add these startups to TestUtils
       
    25 #if defined(__WINS__)
       
    26 IMPORT_C TInt FbsStartup();
       
    27 #endif
       
    28 
       
    29 /**
       
    30  *
       
    31  * Literals : program information and usage
       
    32  *
       
    33  * @xxxx
       
    34  *
       
    35  */
       
    36 _LIT(KTxtFrameworkStarting, "%S %S %S starting....");
       
    37 //_LIT(KTxtUseExample,"Usage:\nTESTFRAMEWORK [-C] [-F] <file.script> [file.ini]"); // EABI warning removal
       
    38 
       
    39 /**
       
    40  *
       
    41  * Compiler switches
       
    42  *
       
    43  * @xxxx
       
    44  *
       
    45  */
       
    46 #ifdef _WIN32
       
    47 _LIT(KTxtTarget,"WINS");
       
    48 #else 
       
    49 #ifdef __MARM_THUMB__ 
       
    50 _LIT(KTxtTarget,"THUMB");
       
    51 #else
       
    52 _LIT(KTxtTarget,"ARM4");
       
    53 #endif
       
    54 #endif
       
    55 
       
    56 #ifdef _DEBUG
       
    57 _LIT(KTxtBuild,"udeb");
       
    58 #else
       
    59 _LIT(KTxtBuild,"urel");
       
    60 #endif
       
    61 
       
    62 
       
    63 //A temporary fix: 20 seconds delay for Techview to get completely loaded.
       
    64 const TInt KTechViewLoadDelay = 20000000;
       
    65 
       
    66 /**
       
    67  *
       
    68  * max length of command line
       
    69  *
       
    70  * @xxxx
       
    71  *
       
    72  */
       
    73 const TInt KMaxLenCmdLine = 256;
       
    74 
       
    75 /**
       
    76  *
       
    77  * Test Framework startup function.
       
    78  * Creates an active scheduler for input if required, reads
       
    79  * the command line, starts the main test loop.
       
    80  *
       
    81  * @xxxx
       
    82  *
       
    83  */
       
    84 void StartupL()
       
    85 	{
       
    86 	CActiveScheduler* pA=new(ELeave) CActiveScheduler;
       
    87 	CleanupStack::PushL(pA);
       
    88 	CActiveScheduler::Install(pA);
       
    89 
       
    90 // Hurricane emulator only - start all services which we require to run tests.
       
    91 // Future enhancement :- add these startups to TestUtils
       
    92 #if defined(__WINS__)
       
    93 	#ifndef EXCLUDE_FOR_UNITTEST
       
    94 	FbsStartup();
       
    95 	#endif // EXCLUDE_FOR_UNITTEST
       
    96 #endif
       
    97 
       
    98 	// read the command line into cmd
       
    99 	TPtr16 cmd(REINTERPRET_CAST(TUint16*,User::AllocLC(KMaxLenCmdLine*2)), 0, KMaxLenCmdLine);
       
   100 	cmd.Fill('\0', KMaxLenCmdLine);
       
   101 
       
   102 	User::CommandLine(cmd);
       
   103 	cmd.UpperCase();
       
   104 
       
   105 	CTestFrameworkMain* tester = CTestFrameworkMain::NewLC();
       
   106 	tester->StartTestingL(cmd);
       
   107 
       
   108 	// NOTE. Currently there is no need to start the active scheduler, as the input console is
       
   109 	// now at the server. This will however change when AOs are implemented to replace
       
   110 	// the main client loop.
       
   111 
       
   112 	// CActiveScheduler::Start();
       
   113 
       
   114 	CleanupStack::PopAndDestroy(3);	//tester, pA, cmd
       
   115 	}
       
   116 
       
   117 
       
   118 GLDEF_C TInt E32Main()
       
   119 	{
       
   120 
       
   121 	__UHEAP_MARK;
       
   122 	CTrapCleanup* cleanup = CTrapCleanup::New(); // get clean-up stack
       
   123 	
       
   124 	// start scheduler 
       
   125 	TRAPD(error, StartupL());
       
   126 	__ASSERT_ALWAYS(!error, User::Panic(_L("TestFramework"), error));
       
   127 
       
   128 	delete cleanup; // destroy clean-up stack
       
   129 	__UHEAP_MARKEND;
       
   130 	return KErrNone;
       
   131 	}
       
   132 
       
   133 // Instructions for Console Display
       
   134 // Please add new entries to the right hand column, 
       
   135 // Precede number and text with "\t "
       
   136 // Leave \n\ at end of each line except the last line
       
   137 //_LIT(KTxtMainInstructions, "Welcome to TestFramework. Press Q to quit ");	// EABI warning removal
       
   138 
       
   139 
       
   140 /**
       
   141  *
       
   142  * CTestFrameworkMain static constructor.
       
   143  *
       
   144  * @xxxx
       
   145  *
       
   146  */
       
   147 CTestFrameworkMain* CTestFrameworkMain::NewLC()
       
   148 	{
       
   149 	CTestFrameworkMain* s = new(ELeave) CTestFrameworkMain;
       
   150 	CleanupStack::PushL(s);
       
   151 	s->ConstructL();
       
   152 	return s;
       
   153 	}
       
   154 
       
   155 /**
       
   156  *
       
   157  * CTestFrameworkMain first-phase constructor.
       
   158  *
       
   159  * @xxxx
       
   160  *
       
   161  */
       
   162 CTestFrameworkMain::CTestFrameworkMain()
       
   163 	{
       
   164 	}
       
   165 
       
   166 /**
       
   167  *
       
   168  * CTestFrameworkMain second-phase constructor.
       
   169  * Loads log client and test utils.
       
   170  *
       
   171  * @xxxx
       
   172  *
       
   173  */
       
   174 void CTestFrameworkMain::ConstructL()
       
   175 	{
       
   176 	iLogClient	  = CLog::NewL();
       
   177 	iLogMode	  = ELogToConsole | ELogToFile;
       
   178 	iTestUtils	  = CTestUtils::NewL(iLogClient);
       
   179 	iGuardTimer   = KNoGuardTimer;	// default value
       
   180 	}
       
   181 
       
   182 /**
       
   183  *
       
   184  * CTestFrameworkMain destructor.
       
   185  *
       
   186  * @xxxx
       
   187  *
       
   188  */
       
   189 CTestFrameworkMain::~CTestFrameworkMain()
       
   190 	{
       
   191 	delete iTestUtils;
       
   192 	delete iLogClient;
       
   193 	}
       
   194 
       
   195 /**
       
   196  *
       
   197  * CTestFrameworkMain - start testing.
       
   198  * Calls main test loop.
       
   199  *
       
   200  * @param "const TDesC& aCmdLine"
       
   201  *			The command line
       
   202  * 
       
   203  * @xxxx
       
   204  *
       
   205  */
       
   206 void CTestFrameworkMain::StartTestingL(const TDesC& aCmdLine)
       
   207 	{
       
   208 	RunTestScriptL(aCmdLine);
       
   209 
       
   210 	RSemaphore sem;
       
   211 	TInt err = sem.OpenGlobal(KRecogSemaphoreName);
       
   212 	if (err==KErrNone)
       
   213 		{
       
   214 		// Tell the recognizer thread that we're finished
       
   215 		sem.Signal();
       
   216 		sem.Close();
       
   217 		}
       
   218 	}
       
   219 
       
   220 /**
       
   221  *
       
   222  * Accessor : log client
       
   223  *
       
   224  * @return	"CLog*"
       
   225  *			The log client
       
   226  * 
       
   227  * @xxxx
       
   228  *
       
   229  */
       
   230 CLog* CTestFrameworkMain::LogClient() const
       
   231 	{
       
   232 	return iLogClient;
       
   233 	}
       
   234 
       
   235 /**
       
   236  *
       
   237  * Main testing loop.
       
   238  * Read a script file, parse it and execute each test step in turn.
       
   239  *
       
   240  * @param "const TDesC& aCmdLine"
       
   241  *			The command line
       
   242  * 
       
   243  * @xxxx
       
   244  *
       
   245  */
       
   246 void CTestFrameworkMain::RunTestScriptL(const TDesC& aCmdLine)
       
   247 	{
       
   248 	// use TLex to decode the cmd line
       
   249 	TLex lex(aCmdLine);
       
   250 	TPtrC token=lex.NextToken();
       
   251 	
       
   252 	// Default is to have 20 second startup delay
       
   253 	TBool delayRequired = ETrue;
       
   254 
       
   255 	// if there is no input filename on the cmd line, panic
       
   256 	if (token.Length() == 0) 
       
   257 		UsageL();
       
   258 	else
       
   259 		{
       
   260 		// Process any options
       
   261 		while(token.Length() > 1 && token[0] == '-')
       
   262 			{
       
   263 			switch(token[1])
       
   264 				{
       
   265 				case 'C':
       
   266 				case 'c':
       
   267 					// log to console ONLY
       
   268 					iLogMode = ELogToConsole;
       
   269 					break;
       
   270 				case 'A':
       
   271 				case 'a':
       
   272 					iLogMode |= ELogConsoleFull;
       
   273 					break;
       
   274 				case 'F':
       
   275 				case 'f':
       
   276 					// log to file ONLY
       
   277 					iLogMode = ELogToFile; 
       
   278 					break;
       
   279 				case 'P':
       
   280 				case 'p':
       
   281 					// log to port AS WELL AS to console / file
       
   282 					iLogMode |= ELogToPort;
       
   283 					break;
       
   284 				//This stops the emulator from thowing int 3 if a panic occurs in debug builds
       
   285 				case 'T':
       
   286 				case 't':
       
   287 					User::SetJustInTime(EFalse);
       
   288 				// -S flag removed - was for old Unit Test mode only
       
   289 				// -A 'automated mode' removed - it's always automated
       
   290 					break;
       
   291 				case 'G':
       
   292 				case 'g':
       
   293 					{
       
   294 					// ** guard timer override - get numeric value that follows
       
   295 					TPtrC val = &token[2];
       
   296 					TLex lexTimeOut(val);
       
   297 					if (lexTimeOut.Val(iGuardTimer) != KErrNone)
       
   298 						UsageL();
       
   299 					}
       
   300 					break;
       
   301 				case 'm':
       
   302 				case 'M':
       
   303 					{
       
   304 					if (token.Length()<=2)
       
   305 						{
       
   306 						// only -m found. must be -m<arg> with no space
       
   307 						UsageL();
       
   308 						}
       
   309 					TPtrC restOfLine = &token[2]; // this will be rest of command line
       
   310 					TLex argument(restOfLine);
       
   311 					TPtrC matchString = argument.NextToken(); // will be the argument itself
       
   312 					ASSERT(matchString.Length()>1);
       
   313 					iTestMatchString = matchString;
       
   314 					}
       
   315 					break;
       
   316 				case 'Q':
       
   317 				case 'q':
       
   318 
       
   319 					{
       
   320 					// Remove the default 20 second delay on startup
       
   321 					delayRequired = EFalse;
       
   322 					}
       
   323 					break;	
       
   324 				default:
       
   325 					UsageL();
       
   326 					return;
       
   327 				}
       
   328 
       
   329 			token.Set(lex.NextToken());
       
   330 			}
       
   331 
       
   332 		if(delayRequired)
       
   333 			{
       
   334 			User::After(KTechViewLoadDelay);
       
   335 			}
       
   336 
       
   337 		// save the input filename
       
   338 		CFileName* scriptFileName = CFileName::NewLC();
       
   339 		*scriptFileName = token;
       
   340 
       
   341 		// make the log file name from the script file name
       
   342 		CFileName* logFileName = CFileName::NewLC();
       
   343 		*logFileName = token;
       
   344 		RDebug::Print(_L("TestFrameWorkMain.cpp: RunTestScriptL 1"));
       
   345 		// open the log file
       
   346 		iLogClient->OpenLogFileL(logFileName->FileName(), iLogMode);
       
   347 		RDebug::Print(_L("TestFrameWorkMain.cpp: RunTestScriptL 2"));	
       
   348 		iLogClient->LogExtra(__FILE8__, __LINE__, ESevrInfo,
       
   349 				KTxtFrameworkStarting, &KTxtVersion(), &KTxtTarget(), &KTxtBuild());
       
   350 		RDebug::Print(_L("TestFrameWorkMain.cpp: RunTestScriptL 3"));
       
   351 		// create a ParseScript object
       
   352 		CScript* parseScript = CScript::NewLC(iTestUtils, 
       
   353 											  iLogClient, 
       
   354 											  iGuardTimer,
       
   355 											  iTestMatchString);
       
   356 		RDebug::Print(_L("TestFrameWorkMain.cpp: RunTestScriptL 4"));
       
   357 		// parse all scripts
       
   358 		do
       
   359 			{
       
   360 			// get the next file
       
   361 			*scriptFileName = token;
       
   362 				
       
   363 			// read in the script file
       
   364 			if ( parseScript->OpenScriptFile(scriptFileName))
       
   365 				{
       
   366 				// process it
       
   367 				parseScript->ExecuteScriptL();
       
   368 				// display results summary
       
   369 				parseScript->DisplayResults();
       
   370 				}
       
   371 			// get the next
       
   372 			token.Set(lex.NextToken());
       
   373 			} while ( token.Length()!=0 );
       
   374 		RDebug::Print(_L("TestFrameWorkMain.cpp: RunTestScriptL 5"));
       
   375 		CleanupStack::PopAndDestroy(parseScript);
       
   376 
       
   377 		// close the logging system
       
   378 		iLogClient->CloseLogFile();
       
   379 
       
   380 		CleanupStack::PopAndDestroy(logFileName);
       
   381 		CleanupStack::PopAndDestroy(scriptFileName);
       
   382 		}
       
   383 	}
       
   384 
       
   385 /**
       
   386  *
       
   387  * Display command line format.
       
   388  * 
       
   389  * @xxxx
       
   390  *
       
   391  */
       
   392 void CTestFrameworkMain::UsageL()
       
   393 	{
       
   394 	// If command line is erroneous, raise a panic. 
       
   395 	// At this point, there may be no log outputs at all.
       
   396 	User::Panic(_L("TestFramework"), 2);
       
   397 	}