os/ossrv/genericservices/httputils/Test/Integration/TestFileUriSuite/TestForAllFilesStep.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/genericservices/httputils/Test/Integration/TestFileUriSuite/TestForAllFilesStep.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,232 @@
1.4 +// Copyright (c) 2004-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 +// TESTGENERATEFILEURIFORALLFILESSTEP.CPP
1.18 +//
1.19 +//
1.20 +
1.21 +/**
1.22 + @file
1.23 + @internalTechnology
1.24 +*/
1.25 +
1.26 +// Epoc Includes
1.27 +// For File URI handler API
1.28 +#include <uri16.h>
1.29 +#include <uri8.h>
1.30 +#include <escapeutils.h>
1.31 +
1.32 +// User Include
1.33 +#include "TestForAllFilesStep.h"
1.34 +
1.35 +/**
1.36 +Constructor. Sets the test step name
1.37 +*/
1.38 +CTestForAllFilesStep::CTestForAllFilesStep()
1.39 + {
1.40 + //Call base class method to set human readable name for test step
1.41 + SetTestStepName(KTestForAllFilesStep);
1.42 + }
1.43 +
1.44 +/**
1.45 +Does the main functionality of a test step. Here just calls DoTestL.
1.46 +@internalTechnology
1.47 +@param None
1.48 +@return EPass or EFail indicating the success or failure of the test step
1.49 +*/
1.50 +TVerdict CTestForAllFilesStep::doTestStepL()
1.51 + {
1.52 + __UHEAP_MARK;
1.53 + INFO_PRINTF1(_L("\n"));
1.54 + TRAPD(err, DoTestL());
1.55 + if(err != KErrNone)
1.56 + {
1.57 + ERR_PRINTF2(_L("Leave occured in CTestForAllFilesStep::DoTestL: %D"), err);
1.58 + SetTestStepResult(EFail);
1.59 + }
1.60 + INFO_PRINTF1(_L("\n"));
1.61 + __UHEAP_MARKEND;
1.62 + return TestStepResult();
1.63 + } // doTestStepL()
1.64 +
1.65 +/**
1.66 +Scans all the directories and calls TestBothWaysL that tests the
1.67 +creation of URI from a filename and vice versa for all the files.
1.68 +*/
1.69 +void CTestForAllFilesStep::DoTestL()
1.70 + {
1.71 + RFs fs;
1.72 + TInt err = fs.Connect();
1.73 + if(err != KErrNone)
1.74 + {
1.75 + ERR_PRINTF2(_L("Error occured while connecting to file server: %D"), err);
1.76 + SetTestStepResult(EFail);
1.77 + }
1.78 + else
1.79 + {
1.80 + TBuf<4> rootDir;
1.81 + CDirScan *dirScan = CDirScan::NewLC(fs);
1.82 + TInt drive;
1.83 + for(drive = EDriveA; drive <= EDriveZ; ++drive)
1.84 + {
1.85 + rootDir.Format(_L("%c:\\"), static_cast<char>(KLetterA + drive));
1.86 + dirScan->SetScanDataL(rootDir, KEntryAttNormal, ESortNone);
1.87 + CDir* entryList = NULL;
1.88 + FOREVER
1.89 + {
1.90 + TRAPD(err, dirScan->NextL(entryList));
1.91 + if(err == KErrNotReady)
1.92 + {
1.93 + INFO_PRINTF2(_L("Drive %S not ready"), &rootDir);
1.94 + break;
1.95 + }
1.96 + if (entryList==NULL)
1.97 + {
1.98 + break;
1.99 + }
1.100 + CleanupStack::PushL(entryList);
1.101 + TFileName filename;
1.102 + TInt index;
1.103 + for(index = 0; index < entryList->Count(); ++index)
1.104 + {
1.105 + const TEntry& entry = (*entryList)[index];
1.106 + filename = dirScan->FullPath();
1.107 + filename.Append(entry.iName);
1.108 + INFO_PRINTF1(_L("\n"));
1.109 + INFO_PRINTF2(_L("The next file name in the dir-scan = %S"), &filename);
1.110 + INFO_PRINTF1(_L("Calling 8-bit versions"));
1.111 +
1.112 + // To keep VC compiler happy as it does not support
1.113 + // explicit template calls.Hence passing empty variables
1.114 + // cUri8 and tUriParser8
1.115 + CUri8* cUri8 = NULL;
1.116 + TUriParser8 tUriParser8;
1.117 + TestBothWaysL(cUri8, tUriParser8, filename);
1.118 + INFO_PRINTF1(_L("\n"));
1.119 + INFO_PRINTF1(_L("Calling 16-bit versions"));
1.120 + CUri16* cUri16 = NULL;
1.121 + TUriParser16 tUriParser16;
1.122 + TestBothWaysL(cUri16, tUriParser16, filename);
1.123 + }
1.124 + CleanupStack::PopAndDestroy(entryList);
1.125 + } // FOREVER
1.126 + }
1.127 + fs.Close();
1.128 + CleanupStack::PopAndDestroy(dirScan);
1.129 + }
1.130 + } // DoTestL
1.131 +
1.132 +/**
1.133 +Template function that calls CUri::CreateFileUriL to generate a URI and
1.134 +calls TUriC::GetFileNameL() with this URI, to verify that the filename so got
1.135 +is the same as the original one.
1.136 +*/
1.137 +template <class CUriType, class TUriParserType>
1.138 +void CTestForAllFilesStep::TestBothWaysL(CUriType*& cUri8Or16, TUriParserType& tUriParser8Or16, TFileName& aFileName)
1.139 + {
1.140 + TUint flags = KErrNone;
1.141 + TDriveNumber driveNum;
1.142 + TParse parser;
1.143 + parser.Set(aFileName, 0, 0);
1.144 + TBuf<1> drive = parser.Drive().Left(1);
1.145 + drive.LowerCase();
1.146 +
1.147 + CTestFileUriServer::GetDriveNumber(drive, driveNum);
1.148 + TBool aResult;
1.149 + TInt err = CTestFileUriServer::IsRemovableDrive(driveNum, aResult);
1.150 + if(err != KErrNone)
1.151 + {
1.152 + ERR_PRINTF2(_L("Error occured while checking whether drive is removable: %D"), err);
1.153 + SetTestStepResult(EFail);
1.154 + }
1.155 + else
1.156 + {
1.157 + if(aResult)
1.158 + {// The drive is a removable drive
1.159 + INFO_PRINTF1(_L("The drive is a removable drive"));
1.160 + flags = EExtMedia;
1.161 + }
1.162 +
1.163 + // Generate the URI
1.164 + TRAPD(err, cUri8Or16 = CUriType::CreateFileUriL(aFileName, flags));
1.165 +
1.166 + if(err != KErrNone)
1.167 + {
1.168 + ERR_PRINTF2(_L("Error occured in CreateFileUriL: %D"), err);
1.169 + SetTestStepResult(EFail);
1.170 + }
1.171 + else
1.172 + {
1.173 + CleanupStack::PushL(cUri8Or16);
1.174 + // Just in case, create a 16-bit heap desc and print.
1.175 + HBufC16* tempBuf = HBufC16::NewL(cUri8Or16->Uri().UriDes().Length());
1.176 + tempBuf->Des().Copy(cUri8Or16->Uri().UriDes());
1.177 + INFO_PRINTF2(_L("File URI returned by CreateFileUriL = %S"), tempBuf);
1.178 + delete tempBuf;
1.179 +
1.180 + if(aResult)
1.181 + {
1.182 + TBuf<1> correctDrive;
1.183 + err = CTestFileUriServer::FirstRemovableDriveWithSameFileName(aFileName, correctDrive);
1.184 + if(err != KErrNone)
1.185 + {
1.186 + ERR_PRINTF2(_L("Error occured in FirstRemovableDriveWithSameFileName: %D"), err);
1.187 + SetTestStepResult(EFail);
1.188 + }
1.189 + else
1.190 + {
1.191 + correctDrive.LowerCase();
1.192 + if(correctDrive != drive)
1.193 + {
1.194 + aFileName.Replace(0, 1, correctDrive);
1.195 + INFO_PRINTF1(_L("One more removable drive found with the same file name, but is ahead in alphabetical order"));
1.196 + INFO_PRINTF2(_L("Hence the correct expected file name is %S"), aFileName);
1.197 + }
1.198 + }
1.199 + }
1.200 +
1.201 + // Convert the URI to filename back again
1.202 + HBufC16* returnedFileName = NULL;
1.203 +
1.204 + tUriParser8Or16.Parse(cUri8Or16->Uri().UriDes());
1.205 + TRAPD(err, returnedFileName = tUriParser8Or16.GetFileNameL());
1.206 + CleanupStack::PopAndDestroy(cUri8Or16);
1.207 + if(err != KErrNone)
1.208 + {
1.209 + ERR_PRINTF2(_L("Error occured in GetFileNameL: %D"), err);
1.210 + SetTestStepResult(EFail);
1.211 + }
1.212 + else
1.213 + {
1.214 + INFO_PRINTF2(_L("The filename returned by GetFileNameL = %S"), returnedFileName);
1.215 +
1.216 + // Verify the result
1.217 + aFileName.LowerCase();
1.218 + returnedFileName->Des().LowerCase();
1.219 +
1.220 + if(returnedFileName->Compare(aFileName) != KErrNone)
1.221 + {
1.222 + INFO_PRINTF1(_L("The returned filename did not match the original filename. Result = INCORRECT"));
1.223 + SetTestStepResult(EFail);
1.224 + }
1.225 + else
1.226 + {
1.227 + INFO_PRINTF1(_L("The returned filename is same as the original filename. Result = CORRECT"));
1.228 + }
1.229 + delete returnedFileName;
1.230 + }
1.231 + }
1.232 + }
1.233 + } // TestBothWaysL
1.234 +
1.235 +