os/mm/mmtestenv/mmtestfwunittest/src/tsu_mmtsth22/TSU_MmTsth22.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 // This file contains the test steps for Unit Test Suite 22 : LogFile.cpp
    15 // 
    16 //
    17 
    18 // EPOC includes
    19 #include <e32base.h>
    20 
    21 // Test system includes
    22 #include <testframework.h>
    23 
    24 // Specific includes for this test suite
    25 #include "TSU_MmTsthSuite22.h"
    26 
    27 // Specific includes for these test steps
    28 #include "TSU_MmTsth22.h"
    29 #include "TestFrameworkServer/LogFile.h"
    30 
    31 // --------------------------------------------
    32 
    33 // Unit Test Suite 22 : LogFile.cpp
    34 // Depends on : None
    35 
    36 // Tests :-
    37 
    38 // Create a file log
    39 // Open an existing file log
    40 // Write to an open file log
    41 // Close a file log
    42 // Open and close a file server session
    43 
    44 
    45 RTestMmTsthU2201* RTestMmTsthU2201::NewL()
    46 	{
    47 	RTestMmTsthU2201* self = new(ELeave) RTestMmTsthU2201;
    48 	return self;
    49 	}
    50 
    51 // Each test step initialises its own name.
    52 RTestMmTsthU2201::RTestMmTsthU2201()
    53 	{
    54 	iTestStepName = _L("MM-TSTH-U-2201");
    55 	}
    56 
    57 // preamble
    58 TVerdict RTestMmTsthU2201::OpenL()
    59 	{
    60 	// ensure the directory for our test log exists
    61 
    62 	// parse the filenames
    63 	_LIT(KDefault,"C:\\"); 
    64 	_LIT(KLogPath, "C:\\Logs\\TestResults\\TestFrameworkTemp\\");
    65 	TParse fullFileName;
    66 	TInt returnCode = fullFileName.Set(KLogPath, &KDefault, NULL);
    67 	if (returnCode != KErrNone)
    68 		{
    69 		ERR_PRINTF3(_L("Preamble : failed to set file name %S, error %d"), 
    70 			&fullFileName.FullName(), returnCode);
    71 		return iTestStepResult = EFail;
    72 		}
    73 
    74 	RFs fileSystem;
    75 	returnCode=fileSystem.Connect();
    76 	if (returnCode != KErrNone)
    77 		{
    78 		ERR_PRINTF2(_L("Preamble : failed to connect to file server, error %d"), 
    79 			returnCode);
    80 		fileSystem.Close();
    81 		return iTestStepResult = EFail;
    82 		}
    83 
    84 	returnCode = fileSystem.MkDir(fullFileName.DriveAndPath()); 
    85 	if (returnCode != KErrNone && returnCode != KErrAlreadyExists)
    86 		{
    87 		ERR_PRINTF3(_L("Preamble : failed to make directory %S, error %d"), 
    88 			&fullFileName.FullName(), returnCode);
    89 		fileSystem.Close();
    90 		return iTestStepResult = EFail;
    91 		}
    92 
    93 	fileSystem.Close();
    94 	return iTestStepResult = EPass;
    95 	}
    96 
    97 // postamble
    98 void RTestMmTsthU2201::Close()
    99 	{
   100 	// clean up the test directory we created in the preamble
   101 
   102 	// parse the filenames
   103 	_LIT(KDefault,"C:\\"); 
   104 	_LIT(KLogPath, "C:\\Logs\\TestResults\\TestFrameworkTemp\\");
   105 	TParse fullFileName;
   106 	TInt returnCode = fullFileName.Set(KLogPath, &KDefault, NULL);
   107 	if (returnCode != KErrNone)
   108 		{
   109 		ERR_PRINTF3(_L("RTestMmTsthU2201::Close() failed to set file name %S, error %d"), 
   110 			&fullFileName.FullName(), returnCode);
   111 		return;
   112 		}
   113 
   114 	RFs fileSystem;
   115 	returnCode=fileSystem.Connect();
   116 	if (returnCode != KErrNone)
   117 		{
   118 		ERR_PRINTF2(_L("RTestMmTsthU2201::Close() failed to connect to file server, error %d"), 
   119 			returnCode);
   120 		fileSystem.Close();
   121 		return;
   122 		}
   123 
   124 	returnCode = fileSystem.RmDir(fullFileName.DriveAndPath()); 
   125 	if (returnCode != KErrNone)
   126 		{
   127 		ERR_PRINTF3(_L("RTestMmTsthU2201::Close() failed to remove directory %S, error %d"), 
   128 			&fullFileName.FullName(), returnCode);
   129 		}
   130 
   131 	fileSystem.Close();
   132 	}
   133 
   134 // do the test step
   135 TVerdict RTestMmTsthU2201::DoTestStepL()
   136 	{
   137 	INFO_PRINTF1(_L("Unit test for CFileLogger"));
   138 
   139 	TVerdict currentVerdict = EPass;
   140 
   141 	// create a CFileLogger
   142 	CFileLogger* theFileLogger = new CFileLogger();
   143 	if(!theFileLogger)
   144 	{
   145 		ERR_PRINTF1(_L("Failed to create a CFileLogger"));
   146 		return iTestStepResult = EFail;
   147 	}
   148 
   149 	// directory path as created in the Preamble
   150 	_LIT(KLogName, "CFileLoggerTest");
   151 	_LIT(KLogPath, "C:\\Logs\\TestResults\\TestFrameworkTemp");
   152 	_LIT(KDefault, "C:\\.htm"); 
   153 
   154 	// ensure the path exists
   155 	TParse parseLogName;
   156 	parseLogName.Set(KLogName, NULL, NULL); 
   157 
   158 	TFileName logFilePath;
   159 	logFilePath = KLogPath;
   160 
   161 	if(parseLogName.PathPresent())
   162 		logFilePath.Append(parseLogName.Path());
   163 	else
   164 		logFilePath.Append(_L("\\"));
   165 
   166 	// overwrite extension if supplied with .htm
   167 	TParse logFileFullName;
   168 	TInt returnCode = logFileFullName.Set(KDefault, &logFilePath, &KLogName());
   169 	if (returnCode == KErrNone)
   170 		{
   171 		TInt ret = theFileLogger->Connect();
   172 		if (ret == KErrNone)
   173 			{
   174 			theFileLogger->CreateLog(logFilePath, logFileFullName.NameAndExt());
   175 			}
   176 		else
   177 			{
   178 			ERR_PRINTF1(_L("CFileLogger could not connect to file server"));
   179 			delete theFileLogger;
   180 			return iTestStepResult = EFail;
   181 			}
   182 		}
   183 
   184 	_LIT(KFileLogTestString, "CFileLoggerTest Verification");
   185 	_LIT8(KFileLogTestString8, "CFileLoggerTest Verification");
   186 
   187 	// write to it
   188 	theFileLogger->WriteLog(KFileLogTestString);
   189 
   190 	// now read it back
   191 	RFs fileSystem;
   192 	returnCode=fileSystem.Connect();
   193 	if (returnCode != KErrNone)
   194 		{
   195 		ERR_PRINTF2(_L("Failed to connect to file server, error %d"), 
   196 			returnCode);
   197 		theFileLogger->Close();
   198 		delete theFileLogger;
   199 		return iTestStepResult = EFail;
   200 		}
   201 
   202 	TFileName logFile;
   203 	logFile.Format(_L("%S\\%S.htm"), &KLogPath(), &KLogName());
   204 	RFile theFile;
   205 	returnCode = theFile.Open(fileSystem, logFile, EFileRead | EFileStreamText | EFileShareAny);
   206 
   207 	// NB :- We're reading in 8-bit, so need to compare against an 8-bit string literal
   208 	TInt fileSize;
   209 	returnCode = theFile.Size(fileSize);
   210 	TPtr8 theString(REINTERPRET_CAST(TUint8*,User::AllocLC(fileSize)), 0, fileSize);
   211 	theFile.Read(theString, fileSize);
   212 
   213 	returnCode = theString.Find(KFileLogTestString8());
   214 
   215 	// see if our string is present. if it is, we've passed.
   216 	if(returnCode == KErrNotFound)
   217 		{
   218 		ERR_PRINTF1(_L("Test string not found in test log file!"));
   219 		currentVerdict = EFail;
   220 		}
   221 	else
   222 		{
   223 		INFO_PRINTF2(_L("Read string %S found in test file"), &KFileLogTestString());
   224 		}
   225 
   226 	// close file logger session
   227 	theFileLogger->Close();
   228 	theFile.Close();
   229 	// clean up our file
   230 	fileSystem.Delete(logFile);
   231 	fileSystem.Close();
   232 	delete(theFileLogger);
   233 	CleanupStack::PopAndDestroy();	// theString
   234 
   235 	return iTestStepResult = currentVerdict; // should be EPass if we've got here
   236 	}
   237