genericservices/httputils/Test/Integration/TestFileUriSuite/TestGenerateFileUriStep.cpp
changeset 31 ce057bb09d0b
parent 0 e4d67989cc36
equal deleted inserted replaced
30:e20de85af2ee 31:ce057bb09d0b
       
     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 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalTechnology 
       
    19 */
       
    20 
       
    21 // Epoc Includes
       
    22 // For File URI handler API
       
    23 #include <uri16.h>
       
    24 #include <uri8.h>
       
    25 #include <escapeutils.h>
       
    26 
       
    27 // User Include
       
    28 #include "TestGenerateFileUriStep.h"
       
    29 
       
    30 /**
       
    31 Constructor. Sets the test step name
       
    32 */
       
    33 CTestGenerateFileUriStep::CTestGenerateFileUriStep()
       
    34 	{
       
    35 	//Call base class method to set human readable name for test step
       
    36 	SetTestStepName(KTestGenerateFileUriStep);
       
    37 	}
       
    38 
       
    39 /**
       
    40 Does the main functionality of a test step.
       
    41 Here, reads values from INI file and calls DoTestL
       
    42 @internalTechnology 
       
    43 @see 		GenerateFileUriAndCompareL
       
    44 @param		None
       
    45 @return		EPass or EFail indicating the success or failure of the test step
       
    46 */
       
    47 TVerdict CTestGenerateFileUriStep::doTestStepL()
       
    48 	{
       
    49 	__UHEAP_MARK;
       
    50 	INFO_PRINTF1(_L("\n"));
       
    51 	// Get necessary information from INI file
       
    52 	TPtrC fileName;
       
    53 	TPtrC expectedFileUri;
       
    54 	TPtrC fileType;
       
    55 	TInt characterSet;
       
    56 	TPtrC drive;
       
    57 	
       
    58 	if(!GetStringFromConfig(ConfigSection(), 	KIniFileName, 			fileName		) ||
       
    59 	   !GetStringFromConfig(ConfigSection(), 	KIniExpectedFileUri,	expectedFileUri	) ||
       
    60 	   !GetStringFromConfig(ConfigSection(), 	KIniFileType, 			fileType		) ||
       
    61 	   !GetIntFromConfig   (ConfigSection(), 	KIniCharacterSet, 		characterSet	) ||
       
    62 	   !GetStringFromConfig(ConfigSection(), 	KIniDrive, 				drive			)
       
    63 	  )
       
    64 		{
       
    65 		ERR_PRINTF6(_L("Problem in reading values from ini.			\
       
    66 						\nExpected fields are: \n%S\n%S\n%S\n%S\n%S\n"
       
    67 					  ),&KIniFileName, &KIniExpectedFileUri, &KIniFileType, &KIniCharacterSet, &KIniDrive
       
    68 				   );
       
    69 		SetTestStepResult(EFail);
       
    70 		}
       
    71 	else
       
    72 		{// No problem in reading values from INI file. Proceed.
       
    73 		TRAPD(err, DoTestL(fileName, expectedFileUri, fileType, drive, characterSet));
       
    74 		if(err != KErrNone)
       
    75 			{
       
    76 			ERR_PRINTF2(_L("Leave occured in CTestGenerateFileUriStep::DoTestL: %D"), err);
       
    77 			SetTestStepResult(EFail);
       
    78 			}
       
    79 		}
       
    80 	__UHEAP_MARKEND;
       
    81 	INFO_PRINTF1(_L("\n"));	
       
    82 	return TestStepResult();
       
    83 	}	// doTestStepL()
       
    84 
       
    85 /**
       
    86 Checks whether the drive is a removable drive. Populates the <drive> placeholder. 
       
    87 And calls ExtractFileNameAndCompareL.
       
    88 */
       
    89 void CTestGenerateFileUriStep::DoTestL(const TPtrC& aFileName, const TPtrC& aExpectedFileUri, const TPtrC& aFileType, const TPtrC& aDrive, const TInt& aCharacterSet)
       
    90 	{
       
    91 	INFO_PRINTF2(_L("Character Set = %D"), aCharacterSet);
       
    92 	INFO_PRINTF2(_L("File name = %S"), &aFileName);
       
    93 	
       
    94 	// Eliminate any characters like colon, slash etc.
       
    95 	TPtrC drive(aDrive.Left(1));
       
    96 
       
    97 	TUint flags = KErrNone;
       
    98 	TDriveNumber driveNum;
       
    99 	CTestFileUriServer::GetDriveNumber(drive, driveNum);
       
   100 	TBool aResult;
       
   101 	TInt err = CTestFileUriServer::IsRemovableDrive(driveNum, aResult);
       
   102 	if(err != KErrNone)
       
   103 		{
       
   104 		ERR_PRINTF2(_L("Error occured while checking whether drive is removable: %D"), err);
       
   105 		SetTestStepResult(EFail);
       
   106 		}
       
   107 	else
       
   108 		{
       
   109 		if(aResult)
       
   110 			{// The drive is a removable drive
       
   111 			INFO_PRINTF1(_L("The drive is a removable drive"));
       
   112 			flags = EExtMedia;
       
   113 			drive.Set(KExtMedia());
       
   114 			}
       
   115 		HBufC16* expectedUriWithDrive = NULL;
       
   116 		
       
   117 		// Fill the <drive> place holder if it exists
       
   118 		TRAPD(err, expectedUriWithDrive = CTestFileUriServer::CheckAndFillDriveNameL(aExpectedFileUri, drive));
       
   119 		if(err != KErrNone)
       
   120 			{
       
   121 			ERR_PRINTF2(_L("Error occured while filling the drive-placeholder: %D"), err);
       
   122 			SetTestStepResult(EFail);
       
   123 			}
       
   124 		else
       
   125 			{// All preliminary operations OK so far, go ahead and perform the test.
       
   126 			CleanupStack::PushL(expectedUriWithDrive);
       
   127 			INFO_PRINTF2(_L("Expected File URI = %S"), expectedUriWithDrive);	
       
   128 			if(aCharacterSet == KCharSet8)
       
   129 				{
       
   130 				CUri16* cUri8 = NULL;
       
   131 				GenerateFileUriAndCompareL(cUri8, aFileName, expectedUriWithDrive, flags, driveNum, aFileType);	
       
   132 				}
       
   133 			else if(aCharacterSet == KCharSet16)
       
   134 				{
       
   135 				CUri16* cUri16 = NULL;
       
   136 				GenerateFileUriAndCompareL(cUri16, aFileName, expectedUriWithDrive, flags, driveNum, aFileType);	
       
   137 				}
       
   138 			else
       
   139 				{
       
   140 				ERR_PRINTF1(_L("Invalid CharacterSet"));
       
   141 				SetTestStepResult(EFail);
       
   142 				}
       
   143 			CleanupStack::PopAndDestroy(expectedUriWithDrive);
       
   144 			}
       
   145 		}
       
   146 	}	// DoTestL
       
   147 	
       
   148 /**	
       
   149 Template function that calls the 8 or 16-bit version of 
       
   150 CreatePrivateFileUriL() or CreateFileUriL(), verifies the expected and 
       
   151 actual results and sets the test step result accordingly.
       
   152 */
       
   153 template <class CUriType>
       
   154 void CTestGenerateFileUriStep::GenerateFileUriAndCompareL(CUriType*& cUri8Or16, const TPtrC& aFileName, HBufC16*& aExpectedFileUri8Or16, const TUint& aFlags, const TInt& aDriveNum, const TPtrC& aFileType)
       
   155 	{
       
   156 	TInt err = KErrNone;
       
   157 	
       
   158 	// Get the URI object
       
   159 	if(aFileType == KFileTypePrivate)
       
   160 		{// Call private version
       
   161 		TRAP(err, cUri8Or16 = CUriType::CreatePrivateFileUriL(aFileName, static_cast<TDriveNumber>(aDriveNum), aFlags));
       
   162 		
       
   163 		// Test it in case of NON-secure version too, but in that case
       
   164 		// the expected result is a Leave with KErrNotSupported.
       
   165 		} 
       
   166 	else
       
   167 		{// Call non-private version
       
   168 		TRAP(err, cUri8Or16 = CUriType::CreateFileUriL(aFileName, aFlags));	
       
   169 		}
       
   170 		
       
   171 	if(err != KErrNone)
       
   172 		{// If it comes here, it means there's some problem. But some tests
       
   173 	 	 // expect an error. So just set the error, and leave the decision 
       
   174 	 	 // to TEF
       
   175 		ERR_PRINTF2(_L("Leave occured: %D"), err);	
       
   176 		SetTestStepError(err);
       
   177 		}
       
   178 	else
       
   179 		{// Things seem to be OK and a URI has been returned. Do the checking
       
   180 		
       
   181 		CleanupStack::PushL(cUri8Or16);
       
   182 		
       
   183 		// Construct a fully lowercase 16-bit version of the returned URI, so that we
       
   184 		// can do print, compare etc.
       
   185 		HBufC16* lowerCaseUri16 = HBufC16::NewL(cUri8Or16->Uri().UriDes().Length());
       
   186 		lowerCaseUri16->Des().Copy(cUri8Or16->Uri().UriDes());
       
   187 		CleanupStack::PopAndDestroy(cUri8Or16);
       
   188 		
       
   189 		INFO_PRINTF2(_L("The returned URI = %S"), lowerCaseUri16);
       
   190 		
       
   191 		lowerCaseUri16->Des().LowerCase();
       
   192 		
       
   193 		// Make expected URI too, fully lowercase
       
   194 		aExpectedFileUri8Or16->Des().LowerCase();
       
   195 
       
   196 		// Compare and set the verdict
       
   197 		if (lowerCaseUri16->Des() != aExpectedFileUri8Or16->Des())
       
   198 			{
       
   199 			INFO_PRINTF1(_L("The returned and expected URIs did not match. Result = INCORRECT"));
       
   200 			SetTestStepResult(EFail); 
       
   201 			}
       
   202 		else
       
   203 			{
       
   204 			INFO_PRINTF1(_L("The returned and expected URIs matched. Result = CORRECT")); 	
       
   205 			}
       
   206 		delete lowerCaseUri16;
       
   207 		}
       
   208 	}		// GenerateFileUriAndCompareL()
       
   209