1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/mm/mmtestenv/mmtestfwunittest/src/tsu_mmtsth22/TSU_MmTsth22.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,237 @@
1.4 +// Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// This file contains the test steps for Unit Test Suite 22 : LogFile.cpp
1.18 +//
1.19 +//
1.20 +
1.21 +// EPOC includes
1.22 +#include <e32base.h>
1.23 +
1.24 +// Test system includes
1.25 +#include <testframework.h>
1.26 +
1.27 +// Specific includes for this test suite
1.28 +#include "TSU_MmTsthSuite22.h"
1.29 +
1.30 +// Specific includes for these test steps
1.31 +#include "TSU_MmTsth22.h"
1.32 +#include "TestFrameworkServer/LogFile.h"
1.33 +
1.34 +// --------------------------------------------
1.35 +
1.36 +// Unit Test Suite 22 : LogFile.cpp
1.37 +// Depends on : None
1.38 +
1.39 +// Tests :-
1.40 +
1.41 +// Create a file log
1.42 +// Open an existing file log
1.43 +// Write to an open file log
1.44 +// Close a file log
1.45 +// Open and close a file server session
1.46 +
1.47 +
1.48 +RTestMmTsthU2201* RTestMmTsthU2201::NewL()
1.49 + {
1.50 + RTestMmTsthU2201* self = new(ELeave) RTestMmTsthU2201;
1.51 + return self;
1.52 + }
1.53 +
1.54 +// Each test step initialises its own name.
1.55 +RTestMmTsthU2201::RTestMmTsthU2201()
1.56 + {
1.57 + iTestStepName = _L("MM-TSTH-U-2201");
1.58 + }
1.59 +
1.60 +// preamble
1.61 +TVerdict RTestMmTsthU2201::OpenL()
1.62 + {
1.63 + // ensure the directory for our test log exists
1.64 +
1.65 + // parse the filenames
1.66 + _LIT(KDefault,"C:\\");
1.67 + _LIT(KLogPath, "C:\\Logs\\TestResults\\TestFrameworkTemp\\");
1.68 + TParse fullFileName;
1.69 + TInt returnCode = fullFileName.Set(KLogPath, &KDefault, NULL);
1.70 + if (returnCode != KErrNone)
1.71 + {
1.72 + ERR_PRINTF3(_L("Preamble : failed to set file name %S, error %d"),
1.73 + &fullFileName.FullName(), returnCode);
1.74 + return iTestStepResult = EFail;
1.75 + }
1.76 +
1.77 + RFs fileSystem;
1.78 + returnCode=fileSystem.Connect();
1.79 + if (returnCode != KErrNone)
1.80 + {
1.81 + ERR_PRINTF2(_L("Preamble : failed to connect to file server, error %d"),
1.82 + returnCode);
1.83 + fileSystem.Close();
1.84 + return iTestStepResult = EFail;
1.85 + }
1.86 +
1.87 + returnCode = fileSystem.MkDir(fullFileName.DriveAndPath());
1.88 + if (returnCode != KErrNone && returnCode != KErrAlreadyExists)
1.89 + {
1.90 + ERR_PRINTF3(_L("Preamble : failed to make directory %S, error %d"),
1.91 + &fullFileName.FullName(), returnCode);
1.92 + fileSystem.Close();
1.93 + return iTestStepResult = EFail;
1.94 + }
1.95 +
1.96 + fileSystem.Close();
1.97 + return iTestStepResult = EPass;
1.98 + }
1.99 +
1.100 +// postamble
1.101 +void RTestMmTsthU2201::Close()
1.102 + {
1.103 + // clean up the test directory we created in the preamble
1.104 +
1.105 + // parse the filenames
1.106 + _LIT(KDefault,"C:\\");
1.107 + _LIT(KLogPath, "C:\\Logs\\TestResults\\TestFrameworkTemp\\");
1.108 + TParse fullFileName;
1.109 + TInt returnCode = fullFileName.Set(KLogPath, &KDefault, NULL);
1.110 + if (returnCode != KErrNone)
1.111 + {
1.112 + ERR_PRINTF3(_L("RTestMmTsthU2201::Close() failed to set file name %S, error %d"),
1.113 + &fullFileName.FullName(), returnCode);
1.114 + return;
1.115 + }
1.116 +
1.117 + RFs fileSystem;
1.118 + returnCode=fileSystem.Connect();
1.119 + if (returnCode != KErrNone)
1.120 + {
1.121 + ERR_PRINTF2(_L("RTestMmTsthU2201::Close() failed to connect to file server, error %d"),
1.122 + returnCode);
1.123 + fileSystem.Close();
1.124 + return;
1.125 + }
1.126 +
1.127 + returnCode = fileSystem.RmDir(fullFileName.DriveAndPath());
1.128 + if (returnCode != KErrNone)
1.129 + {
1.130 + ERR_PRINTF3(_L("RTestMmTsthU2201::Close() failed to remove directory %S, error %d"),
1.131 + &fullFileName.FullName(), returnCode);
1.132 + }
1.133 +
1.134 + fileSystem.Close();
1.135 + }
1.136 +
1.137 +// do the test step
1.138 +TVerdict RTestMmTsthU2201::DoTestStepL()
1.139 + {
1.140 + INFO_PRINTF1(_L("Unit test for CFileLogger"));
1.141 +
1.142 + TVerdict currentVerdict = EPass;
1.143 +
1.144 + // create a CFileLogger
1.145 + CFileLogger* theFileLogger = new CFileLogger();
1.146 + if(!theFileLogger)
1.147 + {
1.148 + ERR_PRINTF1(_L("Failed to create a CFileLogger"));
1.149 + return iTestStepResult = EFail;
1.150 + }
1.151 +
1.152 + // directory path as created in the Preamble
1.153 + _LIT(KLogName, "CFileLoggerTest");
1.154 + _LIT(KLogPath, "C:\\Logs\\TestResults\\TestFrameworkTemp");
1.155 + _LIT(KDefault, "C:\\.htm");
1.156 +
1.157 + // ensure the path exists
1.158 + TParse parseLogName;
1.159 + parseLogName.Set(KLogName, NULL, NULL);
1.160 +
1.161 + TFileName logFilePath;
1.162 + logFilePath = KLogPath;
1.163 +
1.164 + if(parseLogName.PathPresent())
1.165 + logFilePath.Append(parseLogName.Path());
1.166 + else
1.167 + logFilePath.Append(_L("\\"));
1.168 +
1.169 + // overwrite extension if supplied with .htm
1.170 + TParse logFileFullName;
1.171 + TInt returnCode = logFileFullName.Set(KDefault, &logFilePath, &KLogName());
1.172 + if (returnCode == KErrNone)
1.173 + {
1.174 + TInt ret = theFileLogger->Connect();
1.175 + if (ret == KErrNone)
1.176 + {
1.177 + theFileLogger->CreateLog(logFilePath, logFileFullName.NameAndExt());
1.178 + }
1.179 + else
1.180 + {
1.181 + ERR_PRINTF1(_L("CFileLogger could not connect to file server"));
1.182 + delete theFileLogger;
1.183 + return iTestStepResult = EFail;
1.184 + }
1.185 + }
1.186 +
1.187 + _LIT(KFileLogTestString, "CFileLoggerTest Verification");
1.188 + _LIT8(KFileLogTestString8, "CFileLoggerTest Verification");
1.189 +
1.190 + // write to it
1.191 + theFileLogger->WriteLog(KFileLogTestString);
1.192 +
1.193 + // now read it back
1.194 + RFs fileSystem;
1.195 + returnCode=fileSystem.Connect();
1.196 + if (returnCode != KErrNone)
1.197 + {
1.198 + ERR_PRINTF2(_L("Failed to connect to file server, error %d"),
1.199 + returnCode);
1.200 + theFileLogger->Close();
1.201 + delete theFileLogger;
1.202 + return iTestStepResult = EFail;
1.203 + }
1.204 +
1.205 + TFileName logFile;
1.206 + logFile.Format(_L("%S\\%S.htm"), &KLogPath(), &KLogName());
1.207 + RFile theFile;
1.208 + returnCode = theFile.Open(fileSystem, logFile, EFileRead | EFileStreamText | EFileShareAny);
1.209 +
1.210 + // NB :- We're reading in 8-bit, so need to compare against an 8-bit string literal
1.211 + TInt fileSize;
1.212 + returnCode = theFile.Size(fileSize);
1.213 + TPtr8 theString(REINTERPRET_CAST(TUint8*,User::AllocLC(fileSize)), 0, fileSize);
1.214 + theFile.Read(theString, fileSize);
1.215 +
1.216 + returnCode = theString.Find(KFileLogTestString8());
1.217 +
1.218 + // see if our string is present. if it is, we've passed.
1.219 + if(returnCode == KErrNotFound)
1.220 + {
1.221 + ERR_PRINTF1(_L("Test string not found in test log file!"));
1.222 + currentVerdict = EFail;
1.223 + }
1.224 + else
1.225 + {
1.226 + INFO_PRINTF2(_L("Read string %S found in test file"), &KFileLogTestString());
1.227 + }
1.228 +
1.229 + // close file logger session
1.230 + theFileLogger->Close();
1.231 + theFile.Close();
1.232 + // clean up our file
1.233 + fileSystem.Delete(logFile);
1.234 + fileSystem.Close();
1.235 + delete(theFileLogger);
1.236 + CleanupStack::PopAndDestroy(); // theString
1.237 +
1.238 + return iTestStepResult = currentVerdict; // should be EPass if we've got here
1.239 + }
1.240 +