os/ossrv/genericservices/httputils/Test/Integration/TestFileUriSuite/TestGetFileNameFromUriStep.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/genericservices/httputils/Test/Integration/TestFileUriSuite/TestGetFileNameFromUriStep.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,263 @@
     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 +//
    1.18 +
    1.19 +/**
    1.20 + @file
    1.21 + @internalTechnology 
    1.22 +*/
    1.23 +
    1.24 +// Epoc Includes
    1.25 +// For File URI handler API
    1.26 +#include <uri16.h>
    1.27 +#include <uri8.h>
    1.28 +
    1.29 +// User Include
    1.30 +#include "TestGetFileNameFromUriStep.h"
    1.31 +
    1.32 +/**
    1.33 +Constructor. Sets the test step name
    1.34 +*/
    1.35 +CTestGetFileNameFromUriStep::CTestGetFileNameFromUriStep()
    1.36 +	{
    1.37 +	//Call base class method to set human readable name for test step
    1.38 +	SetTestStepName(KTestGetFileNameFromUriStep);
    1.39 +	}
    1.40 +
    1.41 +/**
    1.42 +Does the main functionality of a test step.
    1.43 +Here, reads values from INI file and calls DoTestL
    1.44 +@internalTechnology 
    1.45 +@see		ExtractFileNameAndCompareL
    1.46 +@param		None
    1.47 +@return		EPass or EFail indicating the success or failure of the test step
    1.48 +*/
    1.49 +TVerdict CTestGetFileNameFromUriStep::doTestStepL()
    1.50 +	{
    1.51 +	__UHEAP_MARK;
    1.52 +	INFO_PRINTF1(_L("\n"));
    1.53 +	// Get necessary information from INI file
    1.54 +	TPtrC 	fileUri;
    1.55 +	TPtrC 	expectedFileName;
    1.56 +	TPtrC 	fileType;
    1.57 +	TPtrC 	drive;
    1.58 +	TInt 	characterSet;
    1.59 +
    1.60 +	if(!GetStringFromConfig(ConfigSection(),	KIniFileUri, 			fileUri			) ||
    1.61 +	   !GetStringFromConfig(ConfigSection(), 	KIniExpectedFileName, 	expectedFileName) ||
    1.62 +	   !GetStringFromConfig(ConfigSection(), 	KIniFileType, 			fileType		) ||
    1.63 +	   !GetIntFromConfig   (ConfigSection(), 	KIniCharacterSet, 		characterSet	) ||
    1.64 +	   !GetStringFromConfig(ConfigSection(), 	KIniDrive, 				drive			)
    1.65 +	  )
    1.66 +		{
    1.67 +		ERR_PRINTF6(_L("Problem in reading values from ini.			\
    1.68 +						\nExpected fields are: \n%S\n%S\n%S\n%S\n%S\n"
    1.69 +					  ),&KIniFileUri, &KIniExpectedFileName, &KIniFileType, &KIniCharacterSet, &KIniDrive
    1.70 +				   );
    1.71 +		SetTestStepResult(EFail);
    1.72 +		}
    1.73 +	else
    1.74 +		{
    1.75 +		TRAPD(err, DoTestL(fileUri, expectedFileName, fileType, drive, characterSet));
    1.76 +		if(err != KErrNone)
    1.77 +			{
    1.78 +			ERR_PRINTF2(_L("Leave occured in CTestGetFileNameFromUriStep::DoTestL: %D"), err);
    1.79 +			SetTestStepResult(EFail);
    1.80 +			}
    1.81 +		}
    1.82 +	INFO_PRINTF1(_L("\n"));	
    1.83 +	__UHEAP_MARKEND;
    1.84 +	return TestStepResult();
    1.85 +	}	// doTestStepL()
    1.86 +
    1.87 +/**
    1.88 +Constructs fully-qualified filename in case of a private file, checks whether the 
    1.89 +drive is a removable drive. Populates the <drive> placeholder and calls 
    1.90 +ExtractFileNameAndCompareL.
    1.91 +*/
    1.92 +void CTestGetFileNameFromUriStep::DoTestL(const TPtrC& aFileUri, const TPtrC& aExpectedFileName, const TPtrC& aFileType, TPtrC& aDrive, const TInt& aCharacterSet)
    1.93 +	{
    1.94 +	TInt err = KErrNone;
    1.95 +	TFileName fullyQualifiedName(aExpectedFileName);
    1.96 +	TBool fileNotFound = EFalse;
    1.97 +
    1.98 +	if(aFileType == KFileTypePrivate)
    1.99 +		{// The file is a private file
   1.100 +		// In the case of a secure vesrion of the OS
   1.101 +		// As the INI file contains relative file-name for private files
   1.102 +		// under the ExpectedFileName field, construct the fully-qualified
   1.103 +		// expected file-name
   1.104 +		if((err = CTestFileUriServer::CreateFullyQualifiedName(aExpectedFileName, aDrive, fullyQualifiedName)) != KErrNone)
   1.105 +			{
   1.106 +			ERR_PRINTF2(_L("Error returned by CTestFileUriServer::CreateFullyQualifiedName: %D"), err);
   1.107 +			SetTestStepResult(EFail);
   1.108 +			return;
   1.109 +			}
   1.110 +		}
   1.111 +
   1.112 +	// Check whether drive is removable drive
   1.113 +	TDriveNumber driveNum;
   1.114 +	CTestFileUriServer::GetDriveNumber(aDrive, driveNum);
   1.115 +	TBool aResult;
   1.116 +	err = CTestFileUriServer::IsRemovableDrive(driveNum, aResult);
   1.117 +	if(err != KErrNone)
   1.118 +		{
   1.119 +		ERR_PRINTF2(_L("Error occured while checking whether drive is removable: %D"), err);
   1.120 +		SetTestStepResult(EFail);
   1.121 +		}
   1.122 +	else
   1.123 +		{
   1.124 +		if(aResult)
   1.125 +			{// The drive is a removable drive
   1.126 +			INFO_PRINTF1(_L("The drive is a removable drive"));
   1.127 +
   1.128 +			// Check if any other drive contains a file of the same path and name
   1.129 +			// but comes ahead in the alphabetical order. If yes, that is the correct
   1.130 +			// expected file name
   1.131 +			TBuf<1> correctDrive;
   1.132 +			err = CTestFileUriServer::FirstRemovableDriveWithSameFileName(fullyQualifiedName, correctDrive);
   1.133 +			if(err != KErrNone)
   1.134 +				{
   1.135 +				ERR_PRINTF2(_L("Error occured in FirstRemovableDriveWithSameFileName: %D"), err);
   1.136 +				SetTestStepResult(EFail);
   1.137 +				}
   1.138 +			else if(correctDrive == KNullDesC)
   1.139 +				{
   1.140 +				INFO_PRINTF1(_L("File not found on removable drive"));
   1.141 +				fileNotFound = ETrue;
   1.142 +				}
   1.143 +			else
   1.144 +				{
   1.145 +				correctDrive.LowerCase();
   1.146 +				TBuf<1> drive = aDrive;
   1.147 +				drive.LowerCase();
   1.148 +				if(correctDrive != drive)
   1.149 +					{
   1.150 +					fullyQualifiedName.Replace(0, 1, correctDrive);
   1.151 +					INFO_PRINTF1(_L("One more removable drive found with the same file name, but is ahead in alphabetical order"));
   1.152 +					INFO_PRINTF2(_L("Hence the correct expected file name is %S"), fullyQualifiedName);
   1.153 +					}	
   1.154 +				}
   1.155 +			aDrive.Set(KExtMedia());
   1.156 +			}
   1.157 +		HBufC16* uriWithDrive = NULL;
   1.158 +		
   1.159 +		// Fill the <drive> place holder if it exists
   1.160 +		TRAPD(err, uriWithDrive = CTestFileUriServer::CheckAndFillDriveNameL(aFileUri, aDrive));
   1.161 +
   1.162 +		if(err != KErrNone)
   1.163 +			{
   1.164 +			ERR_PRINTF2(_L("Error occured while filling the drive-placeholder: %D"), err);
   1.165 +			SetTestStepResult(EFail);
   1.166 +			}
   1.167 +		else
   1.168 +			{
   1.169 +			CleanupStack::PushL(uriWithDrive);
   1.170 +			INFO_PRINTF2(_L("Character Set = %D"), aCharacterSet);
   1.171 +			INFO_PRINTF2(_L("File URI = %S"), uriWithDrive);
   1.172 +			INFO_PRINTF2(_L("Expected Filename = %S"), &fullyQualifiedName);
   1.173 +			fullyQualifiedName.LowerCase();	
   1.174 +		
   1.175 +			// Call template function based on the characterset.
   1.176 +			if(aCharacterSet == KCharSet8)
   1.177 +				{
   1.178 +				TUriParser16 uriParser8;
   1.179 +				HBufC16* hBufC8 = NULL;
   1.180 +				ExtractFileNameAndCompareL(uriParser8, hBufC8, uriWithDrive->Des(), fullyQualifiedName, aFileType, fileNotFound);
   1.181 +				}
   1.182 +			else if(aCharacterSet == KCharSet16)
   1.183 +				{
   1.184 +				TUriParser16 uriParser16;
   1.185 +				HBufC16* hBufC16 = NULL;
   1.186 +				ExtractFileNameAndCompareL(uriParser16, hBufC16, uriWithDrive->Des(), fullyQualifiedName, aFileType, fileNotFound);
   1.187 +				}
   1.188 +			else
   1.189 +				{
   1.190 +				ERR_PRINTF1(_L("Invalid CharacterSet"));
   1.191 +				SetTestStepResult(EFail);
   1.192 +				}
   1.193 +			CleanupStack::PopAndDestroy(uriWithDrive);	
   1.194 +			}
   1.195 +		}
   1.196 +	}	// DoTestL
   1.197 +
   1.198 +/**
   1.199 +Template function that calls TUriC::GetFileNameL() after processing 
   1.200 +an 8-bit or 16-bit version of a URI, verifies the expected and 
   1.201 +actual results and sets the test step result accordingly.
   1.202 +*/
   1.203 +template <class TUriParserType, class HBufCType>
   1.204 +void CTestGetFileNameFromUriStep::ExtractFileNameAndCompareL(TUriParserType& uriParser8Or16, HBufCType*& fileUri8Or16, const TPtrC& aFileUri, const TFileName& aFullyQualifiedName, const TPtrC& aFileType, const TBool& aFileNotFound)
   1.205 +	{
   1.206 +	fileUri8Or16 = HBufCType::NewLC(aFileUri.Length());
   1.207 +	fileUri8Or16->Des().Copy(aFileUri);
   1.208 +	uriParser8Or16.Parse(*fileUri8Or16);
   1.209 +	
   1.210 +	// The actual API being tested
   1.211 +	HBufC16* fileName16 = NULL;
   1.212 +	TRAPD(err, fileName16 = uriParser8Or16.GetFileNameL());
   1.213 +	CleanupStack::PopAndDestroy(fileUri8Or16);
   1.214 +	
   1.215 +	if(aFileType == KFileTypePrivate)
   1.216 +		{
   1.217 +		}
   1.218 +	
   1.219 +	if(aFileNotFound)
   1.220 +		{// aFileNotFound is true when the file is not found on removable drive
   1.221 +		if(err == KErrNotFound || err == KErrPathNotFound)
   1.222 +			{
   1.223 +			INFO_PRINTF2(_L("Leave occured = %D. This is expected if file is not found on removable drive"), err);
   1.224 +			}
   1.225 +		else
   1.226 +			{
   1.227 +			ERR_PRINTF2(_L("Unexpected result = %D when file is not found on removable drive"), err);	
   1.228 +			SetTestStepResult(EFail);
   1.229 +			if(fileName16 != NULL)
   1.230 +				{
   1.231 +				INFO_PRINTF2(_L("File name returned = %S"), fileName16);
   1.232 +				delete fileName16;
   1.233 +				}
   1.234 +			}
   1.235 +		}
   1.236 +	else if(err != KErrNone)	
   1.237 +		{// If it comes here, it means there's some problem. But some tests
   1.238 +		 // expect an error. So just set the error, and leave the decision 
   1.239 +		 // to TEF
   1.240 +		ERR_PRINTF2(_L("Leave occured: %D"), err);	
   1.241 +		SetTestStepError(err);
   1.242 +		}
   1.243 +	else
   1.244 +		{// Things seem to be OK and a file-name has been returned. 
   1.245 +		 // Do the checking
   1.246 +		
   1.247 +		INFO_PRINTF2(_L("Returned Filename = %S"), fileName16);
   1.248 +		
   1.249 +		fileName16->Des().LowerCase();
   1.250 +		
   1.251 +		// Compare and set the verdict
   1.252 +		if((fileName16->Des() != aFullyQualifiedName))
   1.253 +			{
   1.254 +			INFO_PRINTF1(_L("The returned filename did not match the expected filename. Result = INCORRECT"));
   1.255 +			SetTestStepResult(EFail); 
   1.256 +			}
   1.257 +		else
   1.258 +			{
   1.259 +			INFO_PRINTF1(_L("The returned filename is the same as the expected filename. Result = CORRECT"));
   1.260 +			}
   1.261 +		
   1.262 +		delete fileName16;
   1.263 +		}
   1.264 +	}	// ExtractFileNameAndCompareL()
   1.265 +	
   1.266 +