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