graphicstest/graphicstestharness/src/T_RebootTests.cpp
changeset 0 5d03bc08d59c
equal deleted inserted replaced
-1:000000000000 0:5d03bc08d59c
       
     1 // T_RebootTests.cpp
       
     2 // Copyright (c) 1995-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 #include <e32base.h>
       
    17 #include <e32cons.h>
       
    18 #include <bacline.h>
       
    19 #include <hal.h>
       
    20 #include <hal_data.h>
       
    21 #include <f32file.h>
       
    22 #include <s32file.h>
       
    23 #include "T_RebootTests.h"
       
    24 
       
    25 GLDEF_C TInt E32Main();
       
    26 
       
    27 const TInt8 KMemoryCardDrive = 0x65; //e
       
    28 
       
    29 const TInt8 KSystemDrive = 0x63; //c
       
    30 
       
    31 _LIT(KCurrTestFilePath, "%c:%Scurrtest.txt");
       
    32 
       
    33 _LIT(KCurrentTestBatPath, "%c:\\thistest.script");
       
    34 
       
    35 _LIT(KTestScriptList, "z:\\autoexec.txt");
       
    36 
       
    37 _LIT(KScriptFileFlag, "cd test");
       
    38 
       
    39 _LIT(KDummyLogFile, "z:\\reboot\\dummylog.htm");
       
    40 
       
    41 _LIT(KRebootTestsPanic, "REBOOTTEST");
       
    42 
       
    43 const TInt KPathBufLen = 36;
       
    44 
       
    45 //Global variables to contain formatted paths for file locations.
       
    46 TBuf<KPathBufLen> KCurrTestFile;
       
    47 TBuf<KPathBufLen> KCurrentTestBat;
       
    48 
       
    49 CTRebootTestHelper::CTRebootTestHelper()
       
    50 	{
       
    51 	}
       
    52 
       
    53 CTRebootTestHelper::~CTRebootTestHelper()
       
    54 	{
       
    55 	iFs.Close();
       
    56 	}
       
    57 
       
    58 CTRebootTestHelper* CTRebootTestHelper::NewLC()
       
    59 	{
       
    60 	CTRebootTestHelper* self = new(ELeave) CTRebootTestHelper();
       
    61 	CleanupStack::PushL(self);
       
    62 	self->ConstructL();
       
    63 	return self;
       
    64 	}
       
    65 
       
    66 void CTRebootTestHelper::ConstructL()
       
    67 	{
       
    68 	User::LeaveIfError(iFs.Connect());
       
    69 	}
       
    70 
       
    71 /**
       
    72  Generate a command script to run the specified test from the test script list.
       
    73  
       
    74  @param aFileIn The source script file
       
    75  @param aFileOut The target batch file
       
    76  @param aPos The test case from the source file to be placed in the destination
       
    77  
       
    78  @leave One of the system wide error codes
       
    79  */
       
    80 void CTRebootTestHelper::BuildCommandScriptL(const TDesC& aFileIn, const TDesC& aFileOut, TInt aTestCase)
       
    81 	{
       
    82 	aTestCase = aTestCase+2;
       
    83 	RFile f;
       
    84 	User::LeaveIfError(f.Open(iFs, aFileIn, EFileRead));
       
    85 	CleanupClosePushL(f);
       
    86 	RFileReadStream frs;
       
    87 	CleanupClosePushL(frs);
       
    88 	frs.Attach(f);
       
    89 	
       
    90 	TInt err = KErrNone;
       
    91 	TBuf<256> thisLine;
       
    92 	
       
    93 	TInt offset = -1; //Flag = 0, Init Script = 1, first script = 2
       
    94 	
       
    95 	TChar thisChar = ' ';
       
    96 
       
    97 	while (err == KErrNone)
       
    98 		{
       
    99 		TRAP(err, thisChar = (TChar) frs.ReadInt8L());
       
   100 		
       
   101 		if ( (thisChar != '\n') && (thisChar != '\r') )
       
   102 			thisLine.Append(thisChar);
       
   103 		
       
   104 		//This line is complete
       
   105 		if (thisChar == '\n')
       
   106 			{
       
   107 			
       
   108 			if ( (offset == -1) && (thisLine.CompareF(KScriptFileFlag) == 0) )
       
   109 				{
       
   110 				offset++;
       
   111 				}
       
   112 			
       
   113 			if (offset >= 0)
       
   114 				{
       
   115 				offset++;
       
   116 				}
       
   117 			
       
   118 			if (offset == aTestCase)
       
   119 				{
       
   120 				RDebug::Printf("The next test script to be run will be...");
       
   121 				RDebug::RawPrint(thisLine);
       
   122 				break;
       
   123 				}
       
   124 			thisLine.Delete(0, thisLine.Length());
       
   125 			}
       
   126 		}	
       
   127 
       
   128 	RFile fo;
       
   129 	TInt ret = fo.Replace(iFs, aFileOut, EFileWrite);
       
   130 	if (ret != KErrNone)
       
   131 			ret = fo.Open(iFs, aFileOut, EFileWrite);
       
   132 	
       
   133 	User::LeaveIfError(ret);
       
   134 	
       
   135 	TFileText fto;
       
   136 	fto.Set(fo);
       
   137 	//Add the TEF command to execute the test correctly
       
   138 	thisLine.Insert(0,_L("RUN_PROGRAM -1 "));
       
   139 	fo.Write(0, thisLine.Collapse(), thisLine.Length());
       
   140 	
       
   141 	CleanupStack::PopAndDestroy(2, &f);
       
   142 	}
       
   143 /**
       
   144  Retrieve the previous test run on the previous boot.
       
   145  
       
   146  @param aFile The full path of the file containing the test count
       
   147  @param aValue The current test being run (-1 if none have been).
       
   148  
       
   149  @return TInt8 The previous test cases index
       
   150  */
       
   151 TInt8 CTRebootTestHelper::FindLastTestL(const TDesC& aFile)
       
   152 	{
       
   153 	RFileReadStream fr;
       
   154 	CleanupClosePushL(fr);
       
   155 	
       
   156 	TInt8 currtest = -1;
       
   157 
       
   158 	if (fr.Open(iFs, aFile, EFileRead) == KErrNone)
       
   159 		{
       
   160 		currtest = fr.ReadInt8L();
       
   161 		RDebug::Printf("Restart Test Manager >> Retrieved %i from file", currtest);
       
   162 		}
       
   163 	else
       
   164 		{
       
   165 		RDebug::Printf("Restart Test Manager >> Beginning Tests");
       
   166 		}
       
   167 
       
   168 	CleanupStack::PopAndDestroy(&fr);
       
   169 	
       
   170   return currtest;
       
   171 	}
       
   172 /**
       
   173 Update the file containing the current test being run.
       
   174 
       
   175 @param aFile The full path of the file containing the test count
       
   176 @param aValue The value to be written to the file.
       
   177  */
       
   178 void CTRebootTestHelper::WriteTestOutL(const TDesC& aFile, TInt8 aValue)
       
   179 	{
       
   180 	RFileWriteStream fw;
       
   181 	CleanupClosePushL(fw);
       
   182 	
       
   183 	if (fw.Replace(iFs, aFile, EFileWrite) != KErrNone)
       
   184 		{
       
   185 		User::LeaveIfError(fw.Create(iFs, aFile, EFileWrite));
       
   186 		}
       
   187 	
       
   188 	fw.WriteInt8L(aValue);
       
   189 	RDebug::Printf("Wrote %i to file", aValue);
       
   190 	fw.CommitL();
       
   191 	CleanupStack::PopAndDestroy(&fw);
       
   192 	
       
   193 	}
       
   194 
       
   195 /**
       
   196 The rebooting harness prevents the script that initiates it from displaying
       
   197 any results in DABS; this function generates this file so that "No Tests To Run"
       
   198 is displayed in the ONB logs. The initiating script should not run any test code
       
   199 that results are expected from.
       
   200 
       
   201 @param aScriptName The name of the script file minus its file extension.
       
   202 @param aLogFile The file containing the dummy log file. All instances of <!LOGFILE!> will be replaced with aScriptName
       
   203 */
       
   204 void CTRebootTestHelper::WriteDummyLogFileL(const TDesC& aScriptName, const TDesC& aLogFile)
       
   205     {
       
   206     RFile f;
       
   207     User::LeaveIfError(f.Open(iFs, aLogFile, EFileRead));
       
   208     CleanupClosePushL(f);
       
   209     RFileReadStream frs;
       
   210     CleanupClosePushL(frs);
       
   211     frs.Attach(f);
       
   212     
       
   213     TInt err = KErrNone;
       
   214     TBuf<256> thisLine;
       
   215     TChar thisChar = ' ';
       
   216 
       
   217     while (err == KErrNone)
       
   218         {
       
   219         TRAP(err, thisChar = (TChar) frs.ReadInt8L());
       
   220         
       
   221         if (err == KErrNone)
       
   222             {
       
   223             thisLine.Append(thisChar);
       
   224             
       
   225             if (thisLine.Length() > 1)
       
   226                 {
       
   227                 //Replace occurrences of %S with aScriptName
       
   228                 if (thisLine.Right(2).Compare(_L("%S")) == 0)
       
   229                     {
       
   230                     thisLine.Delete(thisLine.Length()-2, 2);
       
   231                     thisLine.Append(aScriptName);
       
   232                     thisLine.Append(_L(".htm"));
       
   233                     }
       
   234                 }
       
   235             
       
   236             if (thisChar == '\n')
       
   237                 {
       
   238                 RDebug::RawPrint(thisLine);
       
   239                 thisLine.Delete(0, thisLine.Length());
       
   240                 }
       
   241             }
       
   242         }   
       
   243 
       
   244     CleanupStack::PopAndDestroy(2, &f);
       
   245 
       
   246     }
       
   247 
       
   248 /**
       
   249 Runs the rebooting test harness. Not providing any command line parameters will set up
       
   250 the test runs; or execute the next test depening on the availability of the test data files.
       
   251 Passing restart as the parameter will force a reboot. 
       
   252  */
       
   253 LOCAL_C void MainL()
       
   254 	{
       
   255 	
       
   256 	CCommandLineArguments* args = CCommandLineArguments::NewLC();
       
   257 	CTRebootTestHelper* testHelper = CTRebootTestHelper::NewLC();
       
   258 
       
   259 	//private path length (includes slashes)
       
   260 	TBuf<21> privPath;
       
   261 	testHelper->iFs.PrivatePath(privPath);
       
   262 	
       
   263 	KCurrTestFile.Format(KCurrTestFilePath, KMemoryCardDrive, &privPath);
       
   264 	KCurrentTestBat.Format(KCurrentTestBatPath, KSystemDrive);
       
   265 	
       
   266 	if ((args->Count() == 2) && (args->Arg(1).CompareF(_L("r")) != 0))
       
   267 		{
       
   268 		TRAPD(dirErr,testHelper->iFs.MkDirAll(KCurrTestFile));
       
   269 		
       
   270 		if (dirErr != KErrAlreadyExists)
       
   271 			{
       
   272 			User::LeaveIfError(dirErr);
       
   273 			}
       
   274 		
       
   275 		//Retrieve the previous test from the configuation file.
       
   276 		TInt8 testCase = testHelper->FindLastTestL(KCurrTestFile);
       
   277 
       
   278 		if (testCase == -1)
       
   279 			{
       
   280 			//If there was no configuration file set the test to be the first one
       
   281 			testCase = 1;
       
   282 			}
       
   283 
       
   284 		//Write out the dummy log file for DABS
       
   285 		testHelper->WriteDummyLogFileL(args->Arg(1), KDummyLogFile);
       
   286 		
       
   287 		//Construct the TEF script to run the current test case.
       
   288 		testHelper->BuildCommandScriptL(KTestScriptList, KCurrentTestBat, testCase);
       
   289 		
       
   290 		//Write out the current state to MMC
       
   291 		testCase++;
       
   292 		testHelper->WriteTestOutL(KCurrTestFile, testCase);
       
   293 
       
   294 		
       
   295 		}
       
   296 	else if (args->Count() == 2)
       
   297 		{
       
   298 		//If the app is run with restart in the command line then force a reboot
       
   299 		if (args->Arg(1).CompareF(_L("r"))==0)
       
   300 			{
       
   301 			RDebug::Printf("Restart Test Manager >> Batch File Requested Restart");
       
   302 			testHelper->iFs.Delete(KCurrentTestBat);
       
   303 			HAL::Set(HALData::ECustomRestart, 25);
       
   304 			}
       
   305 		}
       
   306 	CleanupStack::PopAndDestroy(2,args); 
       
   307 	}
       
   308 
       
   309 LOCAL_C void DoStartL()
       
   310 	{
       
   311 	CActiveScheduler* scheduler = new (ELeave) CActiveScheduler();
       
   312 	CleanupStack::PushL(scheduler);
       
   313 	CActiveScheduler::Install(scheduler);
       
   314 	TRAPD(ret, MainL());
       
   315 	
       
   316 	//If a leave has occurred then prevent the tests from continuing
       
   317 	if (ret != KErrNone)
       
   318 		{
       
   319 		RDebug::Printf("Restart Test Manager >> About to panic; MainL exited with error %i", ret);
       
   320 		User::SetCritical(User::ESystemCritical);
       
   321 		User::Panic(KRebootTestsPanic, 0);
       
   322 		}
       
   323 
       
   324 	CleanupStack::PopAndDestroy(scheduler);
       
   325 	}
       
   326 
       
   327 
       
   328 GLDEF_C TInt E32Main()
       
   329 	{
       
   330 	__UHEAP_MARK;
       
   331 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
   332 	TRAPD(mainError, DoStartL());
       
   333 	if (mainError)
       
   334 		RDebug::Printf("Restart Test Manager >> Error %i", mainError);
       
   335 	delete cleanup;
       
   336 	__UHEAP_MARKEND;
       
   337 	return KErrNone;
       
   338 	}
       
   339