|
1 /* |
|
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "datawrapperbase.h" |
|
20 |
|
21 _LIT(KPrefixHex, "0x"); |
|
22 _LIT(KPrefixOctal, "0"); |
|
23 _LIT(KSuffixBinary, "b"); |
|
24 _LIT(KIncludeSection, "include"); |
|
25 _LIT(KFile, "file%d"); |
|
26 _LIT(KMatch, "*{*,*}*"); |
|
27 _LIT(KStart, "{"); |
|
28 _LIT(KSeparator, ","); |
|
29 _LIT(KEnd, "}"); |
|
30 _LIT(KDataRead, "INI READ : %S %S %S"); |
|
31 _LIT(KLogMissingParameter, "Missing parameter '%S'"); |
|
32 _LIT(KExpected, "expected"); |
|
33 |
|
34 CDataWrapperBase::CDataWrapperBase():CDataWrapper() |
|
35 { |
|
36 } |
|
37 |
|
38 CDataWrapperBase::~CDataWrapperBase() |
|
39 { |
|
40 iInclude.ResetAndDestroy(); |
|
41 iBuffer.ResetAndDestroy(); |
|
42 iFs.Close(); |
|
43 } |
|
44 |
|
45 void CDataWrapperBase::InitialiseL() |
|
46 { |
|
47 iTimer.CreateLocal(); |
|
48 CDataWrapper::InitialiseL(); |
|
49 TBuf<KMaxTestExecuteCommandLength> tempStore; |
|
50 TPtrC fileName; |
|
51 TBool moreData = ETrue; |
|
52 TBool index = 0; |
|
53 while( moreData ) |
|
54 { |
|
55 tempStore.Format(KFile(), ++index); |
|
56 moreData=GetStringFromConfig(KIncludeSection, tempStore, fileName); |
|
57 if( moreData ) |
|
58 { |
|
59 CIniData* iniData=CIniData::NewL(fileName); |
|
60 CleanupStack::PushL(iniData); |
|
61 iInclude.Append(iniData); |
|
62 CleanupStack::Pop(iniData); |
|
63 } |
|
64 } |
|
65 User::LeaveIfError(iFs.Connect()); |
|
66 } |
|
67 |
|
68 /** |
|
69 * Reads the value present from the test steps ini file within the mentioned section name and key name |
|
70 * Copies the value to the TBool reference passed in possible values TRUE, FALSE |
|
71 * @param aSectName - Section within the test steps ini file |
|
72 * @param aKeyName - Name of a key within a section |
|
73 * @return aResult - The value of the boolean |
|
74 * @return TBool - ETrue for found, EFalse for not found |
|
75 */ |
|
76 TBool CDataWrapperBase::GetBoolFromConfig(const TDesC& aSectName,const TDesC& aKeyName,TBool& aResult) |
|
77 { |
|
78 TBool ret = EFalse; |
|
79 TPtrC result; |
|
80 TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result)); |
|
81 if( err != KErrNone ) |
|
82 { |
|
83 ret = EFalse; |
|
84 } |
|
85 if( ret ) |
|
86 { |
|
87 _LIT(KTrue,"true"); |
|
88 aResult = (result.FindF(KTrue) != KErrNotFound); |
|
89 } |
|
90 return ret; |
|
91 } |
|
92 |
|
93 /** |
|
94 * Reads the value present from the test steps ini file within the mentioned section name and key name |
|
95 * Copies the value to the TInt reference passed in |
|
96 * @param aSectName - Section within the test steps ini file |
|
97 * @param aKeyName - Name of a key within a section |
|
98 * @return aResult - The value of the integer |
|
99 * @return TBool - ETrue for found, EFalse for not found |
|
100 */ |
|
101 TBool CDataWrapperBase::GetIntFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TInt& aResult) |
|
102 { |
|
103 TPtrC result; |
|
104 TBool ret=EFalse; |
|
105 TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result)); |
|
106 if ( err != KErrNone ) |
|
107 { |
|
108 ret = EFalse; |
|
109 } |
|
110 if ( ret ) |
|
111 { |
|
112 TLex lex(result); |
|
113 ret = ( lex.Val(aResult)==KErrNone ); |
|
114 } |
|
115 return ret; |
|
116 } |
|
117 |
|
118 /** |
|
119 * Reads the value present from the test steps ini file within the mentioned section name and key name |
|
120 * Copies the value to the TReal reference passed in |
|
121 * @param aSectName - Section within the test steps ini file |
|
122 * @param aKeyName - Name of a key within a section |
|
123 * @return aResult - The value of the real |
|
124 * @return TBool - ETrue for found, EFalse for not found |
|
125 */ |
|
126 TBool CDataWrapperBase::GetRealFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TReal& aResult) |
|
127 { |
|
128 TPtrC result; |
|
129 TBool ret=EFalse; |
|
130 TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result)); |
|
131 if ( err != KErrNone ) |
|
132 { |
|
133 ret = EFalse; |
|
134 } |
|
135 if ( ret ) |
|
136 { |
|
137 TLex lex(result); |
|
138 ret = ( lex.Val(aResult)==KErrNone ); |
|
139 } |
|
140 return ret; |
|
141 } |
|
142 |
|
143 /** |
|
144 * Reads the value present from the test steps ini file within the mentioned section name and key name |
|
145 * Copies the value to the TPtrC reference passed in |
|
146 * @param aSectName - Section within the test steps ini file |
|
147 * @param aKeyName - Name of a key within a section |
|
148 * @return aResult - Reference to the string on the heap |
|
149 * @return TBool - ETrue for found, EFalse for not found |
|
150 */ |
|
151 TBool CDataWrapperBase::GetStringFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TPtrC& aResult) |
|
152 { |
|
153 TBool ret=EFalse; |
|
154 TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, aResult)); |
|
155 if ( err != KErrNone ) |
|
156 { |
|
157 ret=EFalse; |
|
158 } |
|
159 return ret; |
|
160 } |
|
161 |
|
162 /** |
|
163 * Reads the value present from the test steps ini file within the mentioned section name and key name |
|
164 * Copies the value to the TInt reference passed in. The value can optionally be prefixed with 0x |
|
165 * @param aSectName - Section within the test steps ini file |
|
166 * @param aKeyName - Name of a key within a section |
|
167 * @return aResult - The integer value of the Hex input |
|
168 * @return TBool - ETrue for found, EFalse for not found |
|
169 */ |
|
170 TBool CDataWrapperBase::GetHexFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TInt& aResult) |
|
171 { |
|
172 TPtrC result; |
|
173 TBool ret=EFalse; |
|
174 TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result)); |
|
175 if ( err != KErrNone ) |
|
176 { |
|
177 ret=EFalse; |
|
178 } |
|
179 if ( ret ) |
|
180 { |
|
181 TLex lex; |
|
182 if( result.FindC(KPrefixHex)==KErrNone ) |
|
183 { |
|
184 lex = result.Mid(KPrefixHex().Length()); |
|
185 } |
|
186 else |
|
187 { |
|
188 lex = result; |
|
189 } |
|
190 ret = ( lex.Val((TUint &)aResult, EHex)==KErrNone ); |
|
191 } |
|
192 return ret; |
|
193 } |
|
194 |
|
195 /** |
|
196 * Reads the value present from the test steps ini file within the mentioned section name and key name |
|
197 * Copies the value to the TUint reference passed in. |
|
198 * If the value is prefixed with 0x the value is read as a hexadecimal value |
|
199 * If the value is suffixed with b the value is read as a binary value |
|
200 * If the value is prefixed with a 0 the value is read as an octal value |
|
201 * If it does not match the above it is read in as an integer |
|
202 * @param aSectName - Section within the test steps ini file |
|
203 * @param aKeyName - Name of a key within a section |
|
204 * @return aResult - The integer value of the Hex input |
|
205 * @return TBool - ETrue for found, EFalse for not found |
|
206 */ |
|
207 TBool CDataWrapperBase::GetUintFromConfig(const TDesC& aSectName, const TDesC& aKeyName, TUint& aResult) |
|
208 { |
|
209 TPtrC result; |
|
210 TBool ret = EFalse; |
|
211 TRAPD(err, ret=GetCommandStringParameterL(aSectName, aKeyName, result)); |
|
212 if ( err != KErrNone ) |
|
213 { |
|
214 ret=EFalse; |
|
215 } |
|
216 if ( ret ) |
|
217 { |
|
218 TLex lex(result); |
|
219 if( result.FindC(KPrefixHex)==KErrNone ) |
|
220 { |
|
221 lex = result.Mid( KPrefixHex().Length() ); |
|
222 ret = ( lex.Val(aResult, EHex)==KErrNone ); |
|
223 } |
|
224 else |
|
225 { |
|
226 TInt binarySuffixPosition=result.Length()-KSuffixBinary().Length(); |
|
227 if ( result.FindC(KSuffixBinary)==binarySuffixPosition ) |
|
228 { |
|
229 lex = result.Left(binarySuffixPosition); |
|
230 ret = (lex.Val(aResult, EBinary)==KErrNone); |
|
231 } |
|
232 else |
|
233 { |
|
234 if( result.FindC(KPrefixOctal)==KErrNone ) |
|
235 { |
|
236 ret = ( lex.Val(aResult, EOctal)==KErrNone ); |
|
237 } |
|
238 else |
|
239 { |
|
240 TInt intResult; |
|
241 ret =(lex.Val(intResult)==KErrNone); |
|
242 if ( ret ) |
|
243 { |
|
244 aResult = (TUint)intResult; |
|
245 } |
|
246 } |
|
247 } |
|
248 } |
|
249 } |
|
250 return ret; |
|
251 } |
|
252 |
|
253 TBool CDataWrapperBase::GetCommandStringParameterL(const TDesC& aSectName, const TDesC& aKeyName, TPtrC& aResult) |
|
254 { |
|
255 TBool ret=EFalse; |
|
256 |
|
257 if ( aSectName.Length()!=0 ) |
|
258 { |
|
259 ret=CDataWrapper::GetStringFromConfig(aSectName, aKeyName, aResult); |
|
260 |
|
261 for ( TInt index=iInclude.Count(); (index>0) && (!ret); ) |
|
262 { |
|
263 ret=iInclude[--index]->FindVar(aSectName, aKeyName, aResult); |
|
264 } |
|
265 } |
|
266 |
|
267 if ( ret ) |
|
268 { |
|
269 if ( aResult.Match(KMatch)!=KErrNotFound ) |
|
270 { |
|
271 /* |
|
272 * We have an entry of the format entry =*{section,entry}* where * |
|
273 * is one or more characters. We need to construct this from other |
|
274 * data in the ini file replacing {*,*} with the data from |
|
275 * [section] entry = some_value |
|
276 */ |
|
277 |
|
278 HBufC* buffer=HBufC::NewLC(aResult.Length()); |
|
279 buffer->Des().Copy(aResult); |
|
280 |
|
281 TInt startLength=KStart().Length(); |
|
282 TInt sparatorLength=KSeparator().Length(); |
|
283 TInt endLength=KEnd().Length(); |
|
284 TInt bufferLength; |
|
285 TInt start; |
|
286 TInt sparator; |
|
287 TInt end; |
|
288 TPtrC remaining; |
|
289 TLex lex; |
|
290 |
|
291 do |
|
292 { |
|
293 bufferLength=buffer->Length(); |
|
294 start=buffer->Find(KStart); |
|
295 |
|
296 remaining.Set(buffer->Des().Right(bufferLength-start-startLength)); |
|
297 sparator=remaining.Find(KSeparator); |
|
298 remaining.Set(remaining.Right(remaining.Length()-sparator-sparatorLength)); |
|
299 sparator += (start + startLength); |
|
300 |
|
301 end=remaining.Find(KEnd) + sparator + sparatorLength; |
|
302 |
|
303 TPtrC sectionName(buffer->Ptr()+start+startLength, sparator-start-startLength); |
|
304 TPtrC keyName(buffer->Ptr()+sparator+sparatorLength, end-sparator-sparatorLength); |
|
305 sectionName.Set(TLex(sectionName).NextToken()); |
|
306 keyName.Set(TLex(keyName).NextToken()); |
|
307 |
|
308 TInt entrySize=0; |
|
309 TPtrC entryData; |
|
310 TBool found=CDataWrapper::GetStringFromConfig(sectionName, keyName, entryData); |
|
311 for ( TInt index=iInclude.Count(); (index>0) && (!found); ) |
|
312 { |
|
313 found=iInclude[--index]->FindVar(sectionName, keyName, entryData); |
|
314 } |
|
315 if ( found ) |
|
316 { |
|
317 entrySize=entryData.Length(); |
|
318 } |
|
319 |
|
320 TInt newLength=start + bufferLength - end - endLength + entrySize; |
|
321 HBufC* bufferNew=HBufC::NewLC(newLength); |
|
322 bufferNew->Des().Copy(buffer->Ptr(), start); |
|
323 if ( entrySize>0 ) |
|
324 { |
|
325 bufferNew->Des().Append(entryData); |
|
326 } |
|
327 bufferNew->Des().Append(buffer->Ptr() + end + endLength, bufferLength - end - endLength); |
|
328 CleanupStack::Pop(bufferNew); |
|
329 CleanupStack::PopAndDestroy(buffer); |
|
330 buffer=bufferNew; |
|
331 CleanupStack::PushL(buffer); |
|
332 } |
|
333 while ( buffer->Match(KMatch)!=KErrNotFound ); |
|
334 iBuffer.Append(buffer); |
|
335 CleanupStack::Pop(buffer); |
|
336 aResult.Set(*buffer); |
|
337 INFO_PRINTF4(KDataRead, &aSectName, &aKeyName , &aResult); |
|
338 } |
|
339 } |
|
340 return ret; |
|
341 } |
|
342 |
|
343 /** |
|
344 *Utility function to produce time delay |
|
345 * @param aTimeoutInSecs Times in micro seconds |
|
346 */ |
|
347 void CDataWrapperBase::Timedelay(TInt aTimeoutInSecs) |
|
348 { |
|
349 TRequestStatus status; |
|
350 iTimer.After(status, aTimeoutInSecs); |
|
351 User::WaitForRequest(status); |
|
352 } |
|
353 |
|
354 /** Validates the returned valua as a Bool. Compares aValue with the expected result in the ini file and Sets the block result to EFail if not equal. |
|
355 * @param aSection - Section within the ini file to read the expected value |
|
356 * @param aValue - The value we need to validate |
|
357 * @param aForceValidation - ETrue forces validation if there's a missing parameter. |
|
358 * @param aErrorMessage - Contains a descriptor with a custom error message |
|
359 * @return - Print error messages to log file |
|
360 * @return - Sets test block result |
|
361 */ |
|
362 void CDataWrapperBase::GetValidationBool(/*CDataWrapperBase* aThis,*/ const TDesC& aSection, TInt aValue, TBool aForceValidation, const TDesC& aErrorMessage) |
|
363 { |
|
364 TBool expectedValue; |
|
365 TBool ret=GetBoolFromConfig(aSection, KExpected(), expectedValue); |
|
366 if ( !ret ) |
|
367 { |
|
368 if ( aForceValidation ) |
|
369 { |
|
370 ERR_PRINTF2(KLogMissingParameter, & KExpected); |
|
371 SetBlockResult(EFail); |
|
372 } |
|
373 } |
|
374 else |
|
375 { |
|
376 if ( aValue!=expectedValue ) |
|
377 { |
|
378 SetBlockResult(EFail); |
|
379 ERR_PRINTF1(aErrorMessage); |
|
380 ERR_PRINTF3(_L("Retrived Value(%d) != expectedValue(%d)"), aValue, expectedValue); |
|
381 } |
|
382 } |
|
383 } |
|
384 |
|
385 /** Validates the returned valua as a TInt. Compares aValue with the expected result in the ini file and Sets the block result to EFail if not equal. |
|
386 * @param aSection - Section within the ini file to read the expected value |
|
387 * @param aValue - The value we need to validate |
|
388 * @param aForceValidation - ETrue forces validation if there's a missing parameter. |
|
389 * @param aErrorMessage - Contains a descriptor with a custom error message |
|
390 * @return - Print error messages to log file |
|
391 * @return - Sets test block result |
|
392 */ |
|
393 void CDataWrapperBase::GetValidationInt(const TDesC& aSection, TInt aValue, TBool aForceValidation, const TDesC& aErrorMessage) |
|
394 { |
|
395 TInt expectedValue; |
|
396 TBool ret = GetIntFromConfig(aSection, KExpected(), expectedValue); |
|
397 if ( !ret ) |
|
398 { |
|
399 if ( aForceValidation ) |
|
400 { |
|
401 ERR_PRINTF2(KLogMissingParameter, &KExpected()); |
|
402 SetBlockResult(EFail); |
|
403 } |
|
404 } |
|
405 else |
|
406 { |
|
407 if ( aValue!=expectedValue ) |
|
408 { |
|
409 SetBlockResult(EFail); |
|
410 ERR_PRINTF1(aErrorMessage); |
|
411 ERR_PRINTF3(_L("Retrived Value(%d) != expectedValue(%d)"), aValue, expectedValue); |
|
412 } |
|
413 } |
|
414 } |
|
415 |
|
416 /** Validates the returned valua as a TUint. Compares aValue with the expected result in the ini file and Sets the block result to EFail if not equal. |
|
417 * @param aSection - Section within the ini file to read the expected value |
|
418 * @param aValue - The value we need to validate |
|
419 * @param aForceValidation - ETrue forces validation if there's a missing parameter. |
|
420 * @param aErrorMessage - Contains a descriptor with a custom error message |
|
421 * @return - Print error messages to log file |
|
422 * @return - Sets test block result |
|
423 */ |
|
424 void CDataWrapperBase::GetValidationUint(const TDesC& aSection, TInt aValue, TBool aForceValidation, const TDesC& aErrorMessage) |
|
425 { |
|
426 TUint expectedValue; |
|
427 TBool ret=GetUintFromConfig(aSection, KExpected(), expectedValue); |
|
428 if ( !ret ) |
|
429 { |
|
430 if ( aForceValidation ) |
|
431 { |
|
432 ERR_PRINTF2(KLogMissingParameter, &KExpected()); |
|
433 SetBlockResult(EFail); |
|
434 } |
|
435 } |
|
436 else |
|
437 { |
|
438 if ( aValue!=expectedValue ) |
|
439 { |
|
440 SetBlockResult(EFail); |
|
441 ERR_PRINTF1(aErrorMessage); |
|
442 ERR_PRINTF3(_L("Retrived Value(%d) != expectedValue(%d)"), aValue, expectedValue); |
|
443 } |
|
444 } |
|
445 } |
|
446 |
|
447 /** |
|
448 * Return array of string parameters i.e. key=a1,a2,a3 returns array which contains |
|
449 * String a1, a2 and a3. |
|
450 * @return ret - EFalse if can't get a String parameter from Config file. ETrue if KErrNone |
|
451 */ |
|
452 TBool CDataWrapperBase::GetArrayFromConfig(const TDesC& aSectName, const TDesC& aKeyName, RPointerArray<HBufC>& aResult) |
|
453 { |
|
454 TBool ret = EFalse; |
|
455 TPtrC completeArray; |
|
456 |
|
457 TRAPD(err, ret = GetCommandStringParameterL(aSectName, aKeyName, completeArray)); |
|
458 if (err != KErrNone) |
|
459 { |
|
460 ret=EFalse; |
|
461 } |
|
462 |
|
463 TLex16 lex(completeArray); |
|
464 TBuf<256> buf; |
|
465 TChar chr; |
|
466 |
|
467 while(!lex.Eos()) |
|
468 { |
|
469 chr = lex.Get(); |
|
470 if (chr == ',') |
|
471 { |
|
472 HBufC* param = buf.AllocLC(); |
|
473 buf.Zero(); |
|
474 aResult.Append(param); |
|
475 CleanupStack::Pop(param); |
|
476 } |
|
477 else |
|
478 { |
|
479 buf.Append(chr); |
|
480 } |
|
481 } |
|
482 HBufC* param = buf.AllocLC(); |
|
483 aResult.Append(param); |
|
484 CleanupStack::Pop(param); |
|
485 |
|
486 return ret; |
|
487 } |
|
488 |
|
489 /* Validates the returned value is an array. Compares aValue with each of the expected results in the ini file and sets the block result to EFail if not equal. |
|
490 * @param aSection - Section within the ini file to read if there's a missing parameter |
|
491 * @param aValue - The value we need to validate |
|
492 * @param aForceValidation - ETrue forces validation if there's a missing parameter |
|
493 * @return - Print error messages to log file |
|
494 * @return - Sets test block result |
|
495 * */ |
|
496 void CDataWrapperBase::GetValidationIntArray(/*CDataWrapperBase* aThis,*/ const TDesC& aSection, TInt aValue, TBool aForceValidation, const TDesC& aErrorMessage) |
|
497 { |
|
498 RPointerArray<HBufC> expectedArray; |
|
499 TBool ret = GetArrayFromConfig(aSection,KExpected(),expectedArray); |
|
500 |
|
501 if(!ret) |
|
502 { |
|
503 ERR_PRINTF1(_L(">>> Could not get expected values")); |
|
504 SetBlockResult(EFail); |
|
505 } |
|
506 else |
|
507 { |
|
508 TBool equals = EFalse; |
|
509 TBuf<256> buf; |
|
510 buf.AppendNum(aValue); |
|
511 |
|
512 for( TInt i=0 ; i<expectedArray.Count() ; i++ ) |
|
513 { |
|
514 if( (*(expectedArray[i])).Compare(buf) == 0) |
|
515 { |
|
516 equals = ETrue; |
|
517 } |
|
518 } |
|
519 |
|
520 if( !equals ) |
|
521 { |
|
522 if( aForceValidation ) |
|
523 { |
|
524 ERR_PRINTF2(KLogMissingParameter, &KExpected()); |
|
525 ERR_PRINTF1(aErrorMessage); |
|
526 SetBlockResult(EFail); |
|
527 } |
|
528 } |
|
529 } |
|
530 } |
|
531 |
|
532 TBool CDataWrapperBase::GetEnumFromConfig(const TDesC& aSectName, const TDesC& aKeyName, const TEnumEntryTable* aTable, TInt& aResult) |
|
533 { |
|
534 TPtrC str; |
|
535 TBool ret=GetStringFromConfig(aSectName, aKeyName, str); |
|
536 |
|
537 if ( ret ) |
|
538 { |
|
539 TBool found=EFalse; |
|
540 TInt index=0; |
|
541 while ( (aTable[index].iValue!=-1) && !found ) |
|
542 { |
|
543 if ( aTable[index].iString==str ) |
|
544 { |
|
545 found=ETrue; |
|
546 aResult=aTable[index].iValue; |
|
547 } |
|
548 else |
|
549 { |
|
550 ++index; |
|
551 } |
|
552 } |
|
553 |
|
554 if ( !found ) |
|
555 { |
|
556 ret=GetIntFromConfig(aSectName, aKeyName, aResult); |
|
557 } |
|
558 } |
|
559 |
|
560 return ret; |
|
561 } |
|
562 |