genericservices/httputils/Test/Integration/TestFileUriSuite/TestForAllFilesStep.cpp
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 // Copyright (c) 2004-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 // TESTGENERATEFILEURIFORALLFILESSTEP.CPP
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @internalTechnology 
       
    21 */
       
    22 
       
    23 // Epoc Includes
       
    24 // For File URI handler API
       
    25 #include <uri16.h>
       
    26 #include <uri8.h>
       
    27 #include <escapeutils.h>
       
    28 
       
    29 // User Include
       
    30 #include "TestForAllFilesStep.h"
       
    31 
       
    32 /**
       
    33 Constructor. Sets the test step name
       
    34 */
       
    35 CTestForAllFilesStep::CTestForAllFilesStep()
       
    36 	{
       
    37 	//Call base class method to set human readable name for test step
       
    38 	SetTestStepName(KTestForAllFilesStep);
       
    39 	}
       
    40 
       
    41 /**
       
    42 Does the main functionality of a test step. Here just calls DoTestL.
       
    43 @internalTechnology 
       
    44 @param		None
       
    45 @return		EPass or EFail indicating the success or failure of the test step
       
    46 */
       
    47 TVerdict CTestForAllFilesStep::doTestStepL()
       
    48 	{
       
    49 	__UHEAP_MARK;
       
    50 	INFO_PRINTF1(_L("\n"));
       
    51 	TRAPD(err, DoTestL());
       
    52 	if(err != KErrNone)
       
    53 		{
       
    54 		ERR_PRINTF2(_L("Leave occured in CTestForAllFilesStep::DoTestL: %D"), err);
       
    55 		SetTestStepResult(EFail);
       
    56 		}
       
    57 	INFO_PRINTF1(_L("\n"));	
       
    58 	__UHEAP_MARKEND;
       
    59 	return TestStepResult();	
       
    60 	}		// doTestStepL()
       
    61 
       
    62 /**
       
    63 Scans all the directories and calls TestBothWaysL that tests the
       
    64 creation of URI from a filename and vice versa for all the files.
       
    65 */
       
    66 void CTestForAllFilesStep::DoTestL()
       
    67 	{
       
    68 	RFs fs;
       
    69 	TInt err = fs.Connect();
       
    70 	if(err != KErrNone)
       
    71 		{
       
    72 		ERR_PRINTF2(_L("Error occured while connecting to file server: %D"), err);
       
    73 		SetTestStepResult(EFail);
       
    74 		}
       
    75 	else
       
    76 		{
       
    77 		TBuf<4> rootDir;
       
    78 		CDirScan *dirScan = CDirScan::NewLC(fs);
       
    79 		TInt drive;
       
    80 		for(drive = EDriveA; drive <= EDriveZ; ++drive)
       
    81 			{
       
    82 			rootDir.Format(_L("%c:\\"), static_cast<char>(KLetterA + drive));
       
    83 			dirScan->SetScanDataL(rootDir, KEntryAttNormal, ESortNone);
       
    84 			CDir* entryList = NULL;
       
    85 			FOREVER
       
    86 				{
       
    87 				TRAPD(err, dirScan->NextL(entryList));
       
    88 				if(err == KErrNotReady)
       
    89 					{
       
    90 					INFO_PRINTF2(_L("Drive %S not ready"), &rootDir);
       
    91 					break;
       
    92 					}
       
    93 				if (entryList==NULL)
       
    94 					{
       
    95 					break;
       
    96 					}
       
    97 				CleanupStack::PushL(entryList);
       
    98 				TFileName filename;
       
    99 				TInt index;
       
   100 				for(index = 0; index < entryList->Count(); ++index)
       
   101 					{
       
   102 					const TEntry& entry = (*entryList)[index];
       
   103 					filename = dirScan->FullPath();
       
   104 					filename.Append(entry.iName);
       
   105 					INFO_PRINTF1(_L("\n"));
       
   106 					INFO_PRINTF2(_L("The next file name in the dir-scan = %S"), &filename);
       
   107 					INFO_PRINTF1(_L("Calling 8-bit versions"));
       
   108 					
       
   109 					// To keep VC compiler happy as it does not support
       
   110 					// explicit template calls.Hence passing empty variables 
       
   111 					// cUri8 and tUriParser8
       
   112 					CUri8* cUri8 = NULL;
       
   113 					TUriParser8 tUriParser8;
       
   114 					TestBothWaysL(cUri8, tUriParser8, filename);
       
   115 					INFO_PRINTF1(_L("\n"));
       
   116 					INFO_PRINTF1(_L("Calling 16-bit versions"));
       
   117 					CUri16* cUri16 = NULL;
       
   118 					TUriParser16 tUriParser16;
       
   119 					TestBothWaysL(cUri16, tUriParser16, filename);
       
   120 					}
       
   121 				CleanupStack::PopAndDestroy(entryList);	
       
   122 				}	// FOREVER
       
   123 			}
       
   124 		fs.Close();
       
   125 		CleanupStack::PopAndDestroy(dirScan);	
       
   126 		}
       
   127 	}	// DoTestL
       
   128 
       
   129 /**
       
   130 Template function that calls CUri::CreateFileUriL to generate a URI and 
       
   131 calls TUriC::GetFileNameL() with this URI, to verify that the filename so got
       
   132 is the same as the original one.
       
   133 */
       
   134 template <class CUriType, class TUriParserType>
       
   135 void CTestForAllFilesStep::TestBothWaysL(CUriType*& cUri8Or16, TUriParserType& tUriParser8Or16, TFileName& aFileName)
       
   136 	{
       
   137 	TUint flags = KErrNone;
       
   138 	TDriveNumber driveNum;
       
   139 	TParse parser;
       
   140 	parser.Set(aFileName, 0, 0);
       
   141 	TBuf<1> drive = parser.Drive().Left(1);
       
   142 	drive.LowerCase();
       
   143 		
       
   144 	CTestFileUriServer::GetDriveNumber(drive, driveNum);
       
   145 	TBool aResult;
       
   146 	TInt err = CTestFileUriServer::IsRemovableDrive(driveNum, aResult);
       
   147 	if(err != KErrNone)
       
   148 		{
       
   149 		ERR_PRINTF2(_L("Error occured while checking whether drive is removable: %D"), err);
       
   150 		SetTestStepResult(EFail);
       
   151 		}
       
   152 	else
       
   153 		{
       
   154 		if(aResult)
       
   155 			{// The drive is a removable drive
       
   156 			INFO_PRINTF1(_L("The drive is a removable drive"));
       
   157 			flags = EExtMedia;
       
   158 			}
       
   159 		
       
   160 		// Generate the URI	
       
   161 		TRAPD(err, cUri8Or16 = CUriType::CreateFileUriL(aFileName, flags));
       
   162 			
       
   163 		if(err != KErrNone)
       
   164 			{
       
   165 			ERR_PRINTF2(_L("Error occured in CreateFileUriL: %D"), err);
       
   166 			SetTestStepResult(EFail);
       
   167 			}
       
   168 		else
       
   169 			{
       
   170 			CleanupStack::PushL(cUri8Or16);
       
   171 			// Just in case, create a 16-bit heap desc and print.
       
   172 			HBufC16* tempBuf = HBufC16::NewL(cUri8Or16->Uri().UriDes().Length());
       
   173 			tempBuf->Des().Copy(cUri8Or16->Uri().UriDes());
       
   174 			INFO_PRINTF2(_L("File URI returned by CreateFileUriL = %S"), tempBuf);
       
   175 			delete tempBuf;
       
   176 			
       
   177 			if(aResult)
       
   178 				{
       
   179 				TBuf<1> correctDrive;
       
   180 				err = CTestFileUriServer::FirstRemovableDriveWithSameFileName(aFileName, correctDrive);
       
   181 				if(err != KErrNone)
       
   182 					{
       
   183 					ERR_PRINTF2(_L("Error occured in FirstRemovableDriveWithSameFileName: %D"), err);
       
   184 					SetTestStepResult(EFail);
       
   185 					}
       
   186 				else
       
   187 					{
       
   188 					correctDrive.LowerCase();
       
   189 					if(correctDrive != drive)
       
   190 						{
       
   191 						aFileName.Replace(0, 1, correctDrive);
       
   192 						INFO_PRINTF1(_L("One more removable drive found with the same file name, but is ahead in alphabetical order"));
       
   193 						INFO_PRINTF2(_L("Hence the correct expected file name is %S"), aFileName);
       
   194 						}	
       
   195 					}
       
   196 				}
       
   197 				
       
   198 			// Convert the URI to filename back again
       
   199 			HBufC16* returnedFileName = NULL;
       
   200 			
       
   201 			tUriParser8Or16.Parse(cUri8Or16->Uri().UriDes());
       
   202 			TRAPD(err, returnedFileName = tUriParser8Or16.GetFileNameL());
       
   203 			CleanupStack::PopAndDestroy(cUri8Or16);
       
   204 			if(err != KErrNone)
       
   205 				{
       
   206 				ERR_PRINTF2(_L("Error occured in GetFileNameL: %D"), err);
       
   207 				SetTestStepResult(EFail);
       
   208 				}
       
   209 			else
       
   210 				{
       
   211 				INFO_PRINTF2(_L("The filename returned by GetFileNameL = %S"), returnedFileName);
       
   212 			
       
   213 				// Verify the result
       
   214 				aFileName.LowerCase();
       
   215 				returnedFileName->Des().LowerCase();
       
   216 				
       
   217 				if(returnedFileName->Compare(aFileName) != KErrNone)
       
   218 					{
       
   219 					INFO_PRINTF1(_L("The returned filename did not match the original filename. Result = INCORRECT"));
       
   220 					SetTestStepResult(EFail);
       
   221 					}
       
   222 				else
       
   223 					{
       
   224 					INFO_PRINTF1(_L("The returned filename is same as the original filename. Result = CORRECT"));
       
   225 					}	
       
   226 				delete returnedFileName;
       
   227 				}	
       
   228 			}
       
   229 		}
       
   230 	}	// TestBothWaysL
       
   231 
       
   232