sl@0: // Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // This file contains the test steps for Unit Test Suite 22 : LogFile.cpp sl@0: // sl@0: // sl@0: sl@0: // EPOC includes sl@0: #include sl@0: sl@0: // Test system includes sl@0: #include sl@0: sl@0: // Specific includes for this test suite sl@0: #include "TSU_MmTsthSuite22.h" sl@0: sl@0: // Specific includes for these test steps sl@0: #include "TSU_MmTsth22.h" sl@0: #include "TestFrameworkServer/LogFile.h" sl@0: sl@0: // -------------------------------------------- sl@0: sl@0: // Unit Test Suite 22 : LogFile.cpp sl@0: // Depends on : None sl@0: sl@0: // Tests :- sl@0: sl@0: // Create a file log sl@0: // Open an existing file log sl@0: // Write to an open file log sl@0: // Close a file log sl@0: // Open and close a file server session sl@0: sl@0: sl@0: RTestMmTsthU2201* RTestMmTsthU2201::NewL() sl@0: { sl@0: RTestMmTsthU2201* self = new(ELeave) RTestMmTsthU2201; sl@0: return self; sl@0: } sl@0: sl@0: // Each test step initialises its own name. sl@0: RTestMmTsthU2201::RTestMmTsthU2201() sl@0: { sl@0: iTestStepName = _L("MM-TSTH-U-2201"); sl@0: } sl@0: sl@0: // preamble sl@0: TVerdict RTestMmTsthU2201::OpenL() sl@0: { sl@0: // ensure the directory for our test log exists sl@0: sl@0: // parse the filenames sl@0: _LIT(KDefault,"C:\\"); sl@0: _LIT(KLogPath, "C:\\Logs\\TestResults\\TestFrameworkTemp\\"); sl@0: TParse fullFileName; sl@0: TInt returnCode = fullFileName.Set(KLogPath, &KDefault, NULL); sl@0: if (returnCode != KErrNone) sl@0: { sl@0: ERR_PRINTF3(_L("Preamble : failed to set file name %S, error %d"), sl@0: &fullFileName.FullName(), returnCode); sl@0: return iTestStepResult = EFail; sl@0: } sl@0: sl@0: RFs fileSystem; sl@0: returnCode=fileSystem.Connect(); sl@0: if (returnCode != KErrNone) sl@0: { sl@0: ERR_PRINTF2(_L("Preamble : failed to connect to file server, error %d"), sl@0: returnCode); sl@0: fileSystem.Close(); sl@0: return iTestStepResult = EFail; sl@0: } sl@0: sl@0: returnCode = fileSystem.MkDir(fullFileName.DriveAndPath()); sl@0: if (returnCode != KErrNone && returnCode != KErrAlreadyExists) sl@0: { sl@0: ERR_PRINTF3(_L("Preamble : failed to make directory %S, error %d"), sl@0: &fullFileName.FullName(), returnCode); sl@0: fileSystem.Close(); sl@0: return iTestStepResult = EFail; sl@0: } sl@0: sl@0: fileSystem.Close(); sl@0: return iTestStepResult = EPass; sl@0: } sl@0: sl@0: // postamble sl@0: void RTestMmTsthU2201::Close() sl@0: { sl@0: // clean up the test directory we created in the preamble sl@0: sl@0: // parse the filenames sl@0: _LIT(KDefault,"C:\\"); sl@0: _LIT(KLogPath, "C:\\Logs\\TestResults\\TestFrameworkTemp\\"); sl@0: TParse fullFileName; sl@0: TInt returnCode = fullFileName.Set(KLogPath, &KDefault, NULL); sl@0: if (returnCode != KErrNone) sl@0: { sl@0: ERR_PRINTF3(_L("RTestMmTsthU2201::Close() failed to set file name %S, error %d"), sl@0: &fullFileName.FullName(), returnCode); sl@0: return; sl@0: } sl@0: sl@0: RFs fileSystem; sl@0: returnCode=fileSystem.Connect(); sl@0: if (returnCode != KErrNone) sl@0: { sl@0: ERR_PRINTF2(_L("RTestMmTsthU2201::Close() failed to connect to file server, error %d"), sl@0: returnCode); sl@0: fileSystem.Close(); sl@0: return; sl@0: } sl@0: sl@0: returnCode = fileSystem.RmDir(fullFileName.DriveAndPath()); sl@0: if (returnCode != KErrNone) sl@0: { sl@0: ERR_PRINTF3(_L("RTestMmTsthU2201::Close() failed to remove directory %S, error %d"), sl@0: &fullFileName.FullName(), returnCode); sl@0: } sl@0: sl@0: fileSystem.Close(); sl@0: } sl@0: sl@0: // do the test step sl@0: TVerdict RTestMmTsthU2201::DoTestStepL() sl@0: { sl@0: INFO_PRINTF1(_L("Unit test for CFileLogger")); sl@0: sl@0: TVerdict currentVerdict = EPass; sl@0: sl@0: // create a CFileLogger sl@0: CFileLogger* theFileLogger = new CFileLogger(); sl@0: if(!theFileLogger) sl@0: { sl@0: ERR_PRINTF1(_L("Failed to create a CFileLogger")); sl@0: return iTestStepResult = EFail; sl@0: } sl@0: sl@0: // directory path as created in the Preamble sl@0: _LIT(KLogName, "CFileLoggerTest"); sl@0: _LIT(KLogPath, "C:\\Logs\\TestResults\\TestFrameworkTemp"); sl@0: _LIT(KDefault, "C:\\.htm"); sl@0: sl@0: // ensure the path exists sl@0: TParse parseLogName; sl@0: parseLogName.Set(KLogName, NULL, NULL); sl@0: sl@0: TFileName logFilePath; sl@0: logFilePath = KLogPath; sl@0: sl@0: if(parseLogName.PathPresent()) sl@0: logFilePath.Append(parseLogName.Path()); sl@0: else sl@0: logFilePath.Append(_L("\\")); sl@0: sl@0: // overwrite extension if supplied with .htm sl@0: TParse logFileFullName; sl@0: TInt returnCode = logFileFullName.Set(KDefault, &logFilePath, &KLogName()); sl@0: if (returnCode == KErrNone) sl@0: { sl@0: TInt ret = theFileLogger->Connect(); sl@0: if (ret == KErrNone) sl@0: { sl@0: theFileLogger->CreateLog(logFilePath, logFileFullName.NameAndExt()); sl@0: } sl@0: else sl@0: { sl@0: ERR_PRINTF1(_L("CFileLogger could not connect to file server")); sl@0: delete theFileLogger; sl@0: return iTestStepResult = EFail; sl@0: } sl@0: } sl@0: sl@0: _LIT(KFileLogTestString, "CFileLoggerTest Verification"); sl@0: _LIT8(KFileLogTestString8, "CFileLoggerTest Verification"); sl@0: sl@0: // write to it sl@0: theFileLogger->WriteLog(KFileLogTestString); sl@0: sl@0: // now read it back sl@0: RFs fileSystem; sl@0: returnCode=fileSystem.Connect(); sl@0: if (returnCode != KErrNone) sl@0: { sl@0: ERR_PRINTF2(_L("Failed to connect to file server, error %d"), sl@0: returnCode); sl@0: theFileLogger->Close(); sl@0: delete theFileLogger; sl@0: return iTestStepResult = EFail; sl@0: } sl@0: sl@0: TFileName logFile; sl@0: logFile.Format(_L("%S\\%S.htm"), &KLogPath(), &KLogName()); sl@0: RFile theFile; sl@0: returnCode = theFile.Open(fileSystem, logFile, EFileRead | EFileStreamText | EFileShareAny); sl@0: sl@0: // NB :- We're reading in 8-bit, so need to compare against an 8-bit string literal sl@0: TInt fileSize; sl@0: returnCode = theFile.Size(fileSize); sl@0: TPtr8 theString(REINTERPRET_CAST(TUint8*,User::AllocLC(fileSize)), 0, fileSize); sl@0: theFile.Read(theString, fileSize); sl@0: sl@0: returnCode = theString.Find(KFileLogTestString8()); sl@0: sl@0: // see if our string is present. if it is, we've passed. sl@0: if(returnCode == KErrNotFound) sl@0: { sl@0: ERR_PRINTF1(_L("Test string not found in test log file!")); sl@0: currentVerdict = EFail; sl@0: } sl@0: else sl@0: { sl@0: INFO_PRINTF2(_L("Read string %S found in test file"), &KFileLogTestString()); sl@0: } sl@0: sl@0: // close file logger session sl@0: theFileLogger->Close(); sl@0: theFile.Close(); sl@0: // clean up our file sl@0: fileSystem.Delete(logFile); sl@0: fileSystem.Close(); sl@0: delete(theFileLogger); sl@0: CleanupStack::PopAndDestroy(); // theString sl@0: sl@0: return iTestStepResult = currentVerdict; // should be EPass if we've got here sl@0: } sl@0: