os/ossrv/genericservices/httputils/Test/Integration/TestFileUriSuite/TestGetFileNameFromUriStep.cpp
Update contrib.
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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
22 // For File URI handler API
27 #include "TestGetFileNameFromUriStep.h"
30 Constructor. Sets the test step name
32 CTestGetFileNameFromUriStep::CTestGetFileNameFromUriStep()
34 //Call base class method to set human readable name for test step
35 SetTestStepName(KTestGetFileNameFromUriStep);
39 Does the main functionality of a test step.
40 Here, reads values from INI file and calls DoTestL
42 @see ExtractFileNameAndCompareL
44 @return EPass or EFail indicating the success or failure of the test step
46 TVerdict CTestGetFileNameFromUriStep::doTestStepL()
49 INFO_PRINTF1(_L("\n"));
50 // Get necessary information from INI file
52 TPtrC expectedFileName;
57 if(!GetStringFromConfig(ConfigSection(), KIniFileUri, fileUri ) ||
58 !GetStringFromConfig(ConfigSection(), KIniExpectedFileName, expectedFileName) ||
59 !GetStringFromConfig(ConfigSection(), KIniFileType, fileType ) ||
60 !GetIntFromConfig (ConfigSection(), KIniCharacterSet, characterSet ) ||
61 !GetStringFromConfig(ConfigSection(), KIniDrive, drive )
64 ERR_PRINTF6(_L("Problem in reading values from ini. \
65 \nExpected fields are: \n%S\n%S\n%S\n%S\n%S\n"
66 ),&KIniFileUri, &KIniExpectedFileName, &KIniFileType, &KIniCharacterSet, &KIniDrive
68 SetTestStepResult(EFail);
72 TRAPD(err, DoTestL(fileUri, expectedFileName, fileType, drive, characterSet));
75 ERR_PRINTF2(_L("Leave occured in CTestGetFileNameFromUriStep::DoTestL: %D"), err);
76 SetTestStepResult(EFail);
79 INFO_PRINTF1(_L("\n"));
81 return TestStepResult();
85 Constructs fully-qualified filename in case of a private file, checks whether the
86 drive is a removable drive. Populates the <drive> placeholder and calls
87 ExtractFileNameAndCompareL.
89 void CTestGetFileNameFromUriStep::DoTestL(const TPtrC& aFileUri, const TPtrC& aExpectedFileName, const TPtrC& aFileType, TPtrC& aDrive, const TInt& aCharacterSet)
92 TFileName fullyQualifiedName(aExpectedFileName);
93 TBool fileNotFound = EFalse;
95 if(aFileType == KFileTypePrivate)
96 {// The file is a private file
97 // In the case of a secure vesrion of the OS
98 // As the INI file contains relative file-name for private files
99 // under the ExpectedFileName field, construct the fully-qualified
100 // expected file-name
101 if((err = CTestFileUriServer::CreateFullyQualifiedName(aExpectedFileName, aDrive, fullyQualifiedName)) != KErrNone)
103 ERR_PRINTF2(_L("Error returned by CTestFileUriServer::CreateFullyQualifiedName: %D"), err);
104 SetTestStepResult(EFail);
109 // Check whether drive is removable drive
110 TDriveNumber driveNum;
111 CTestFileUriServer::GetDriveNumber(aDrive, driveNum);
113 err = CTestFileUriServer::IsRemovableDrive(driveNum, aResult);
116 ERR_PRINTF2(_L("Error occured while checking whether drive is removable: %D"), err);
117 SetTestStepResult(EFail);
122 {// The drive is a removable drive
123 INFO_PRINTF1(_L("The drive is a removable drive"));
125 // Check if any other drive contains a file of the same path and name
126 // but comes ahead in the alphabetical order. If yes, that is the correct
127 // expected file name
128 TBuf<1> correctDrive;
129 err = CTestFileUriServer::FirstRemovableDriveWithSameFileName(fullyQualifiedName, correctDrive);
132 ERR_PRINTF2(_L("Error occured in FirstRemovableDriveWithSameFileName: %D"), err);
133 SetTestStepResult(EFail);
135 else if(correctDrive == KNullDesC)
137 INFO_PRINTF1(_L("File not found on removable drive"));
138 fileNotFound = ETrue;
142 correctDrive.LowerCase();
143 TBuf<1> drive = aDrive;
145 if(correctDrive != drive)
147 fullyQualifiedName.Replace(0, 1, correctDrive);
148 INFO_PRINTF1(_L("One more removable drive found with the same file name, but is ahead in alphabetical order"));
149 INFO_PRINTF2(_L("Hence the correct expected file name is %S"), fullyQualifiedName);
152 aDrive.Set(KExtMedia());
154 HBufC16* uriWithDrive = NULL;
156 // Fill the <drive> place holder if it exists
157 TRAPD(err, uriWithDrive = CTestFileUriServer::CheckAndFillDriveNameL(aFileUri, aDrive));
161 ERR_PRINTF2(_L("Error occured while filling the drive-placeholder: %D"), err);
162 SetTestStepResult(EFail);
166 CleanupStack::PushL(uriWithDrive);
167 INFO_PRINTF2(_L("Character Set = %D"), aCharacterSet);
168 INFO_PRINTF2(_L("File URI = %S"), uriWithDrive);
169 INFO_PRINTF2(_L("Expected Filename = %S"), &fullyQualifiedName);
170 fullyQualifiedName.LowerCase();
172 // Call template function based on the characterset.
173 if(aCharacterSet == KCharSet8)
175 TUriParser16 uriParser8;
176 HBufC16* hBufC8 = NULL;
177 ExtractFileNameAndCompareL(uriParser8, hBufC8, uriWithDrive->Des(), fullyQualifiedName, aFileType, fileNotFound);
179 else if(aCharacterSet == KCharSet16)
181 TUriParser16 uriParser16;
182 HBufC16* hBufC16 = NULL;
183 ExtractFileNameAndCompareL(uriParser16, hBufC16, uriWithDrive->Des(), fullyQualifiedName, aFileType, fileNotFound);
187 ERR_PRINTF1(_L("Invalid CharacterSet"));
188 SetTestStepResult(EFail);
190 CleanupStack::PopAndDestroy(uriWithDrive);
196 Template function that calls TUriC::GetFileNameL() after processing
197 an 8-bit or 16-bit version of a URI, verifies the expected and
198 actual results and sets the test step result accordingly.
200 template <class TUriParserType, class HBufCType>
201 void CTestGetFileNameFromUriStep::ExtractFileNameAndCompareL(TUriParserType& uriParser8Or16, HBufCType*& fileUri8Or16, const TPtrC& aFileUri, const TFileName& aFullyQualifiedName, const TPtrC& aFileType, const TBool& aFileNotFound)
203 fileUri8Or16 = HBufCType::NewLC(aFileUri.Length());
204 fileUri8Or16->Des().Copy(aFileUri);
205 uriParser8Or16.Parse(*fileUri8Or16);
207 // The actual API being tested
208 HBufC16* fileName16 = NULL;
209 TRAPD(err, fileName16 = uriParser8Or16.GetFileNameL());
210 CleanupStack::PopAndDestroy(fileUri8Or16);
212 if(aFileType == KFileTypePrivate)
217 {// aFileNotFound is true when the file is not found on removable drive
218 if(err == KErrNotFound || err == KErrPathNotFound)
220 INFO_PRINTF2(_L("Leave occured = %D. This is expected if file is not found on removable drive"), err);
224 ERR_PRINTF2(_L("Unexpected result = %D when file is not found on removable drive"), err);
225 SetTestStepResult(EFail);
226 if(fileName16 != NULL)
228 INFO_PRINTF2(_L("File name returned = %S"), fileName16);
233 else if(err != KErrNone)
234 {// If it comes here, it means there's some problem. But some tests
235 // expect an error. So just set the error, and leave the decision
237 ERR_PRINTF2(_L("Leave occured: %D"), err);
238 SetTestStepError(err);
241 {// Things seem to be OK and a file-name has been returned.
244 INFO_PRINTF2(_L("Returned Filename = %S"), fileName16);
246 fileName16->Des().LowerCase();
248 // Compare and set the verdict
249 if((fileName16->Des() != aFullyQualifiedName))
251 INFO_PRINTF1(_L("The returned filename did not match the expected filename. Result = INCORRECT"));
252 SetTestStepResult(EFail);
256 INFO_PRINTF1(_L("The returned filename is the same as the expected filename. Result = CORRECT"));
261 } // ExtractFileNameAndCompareL()