Update contrib.
2 * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
28 TBool __bb = (cond); \
32 ERR_PRINTF1(_L("ERROR: Test Failed")); \
37 const TInt KTestCleanupStack=0x20;
39 LOCAL_D CTrapCleanup* TheTrapCleanup;
40 LOCAL_D CTextFieldSet* TheFieldSetOriginal;
41 LOCAL_D CTextFieldSet* TheFieldSetCopy;
42 LOCAL_D TTestFieldFactory* TheFactory;
44 _LIT(KTFieldOutputFile, "c:\\etext\\tfield.tst");
45 // Test Picture persistance.
46 void CT_STREAM::testStoreRestore(CTextFieldSet* aCopy,const CTextFieldSet* aOriginal)
51 theFs.Delete(KTFieldOutputFile);
52 theFs.MkDirAll(KTFieldOutputFile);
53 CFileStore* theStore=CDirectFileStore::CreateL(theFs, KTFieldOutputFile, EFileRead | EFileWrite);
54 CleanupStack::PushL(theStore);
55 theStore->SetTypeL(KDirectFileStoreLayoutUid);
59 TRAPD(ret,id=aOriginal->StoreL(*theStore));
62 // restore into the copy
63 TRAP(ret,aCopy->RestoreL(*theStore,id));
67 CleanupStack::PopAndDestroy(); // theStore
74 void CT_STREAM::testCopyPaste(T* aCopy, T* aOriginal,TInt aCopyPos,TInt aLen,TInt aPastePos,TInt aPasteLen)
75 // Copy part of anOriginal to aCopy using memory-based streams.
81 CFileStore* theStore=CDirectFileStore::ReplaceL(theFs,KTFieldOutputFile,EFileRead|EFileWrite);
82 CleanupStack::PushL(theStore);
83 theStore->SetTypeL(KDirectFileStoreLayoutUid);
85 // Copy anOriginal out to the buffer
87 TRAPD(ret,id=aOriginal->CopyToStoreL(*theStore,aCopyPos,aLen));
90 // paste into the copy
91 TRAP(ret,aCopy->PasteFromStoreL(*theStore,id,aPastePos,aPasteLen));
95 CleanupStack::PopAndDestroy(); // theStore
101 TBool CT_STREAM::IsEqual(const CTextFieldSet* aCopy,const CTextFieldSet* anOriginal)
103 // Returns true if aCopy contents matches anOriginal contents.
106 // test the num chars and num fields
107 TInt charCount = anOriginal->CharCount();
108 TInt fieldCount = anOriginal->FieldCount();
109 test(charCount==aCopy->CharCount());
110 test(fieldCount==aCopy->FieldCount());
112 // test each entry in the array
113 for (TInt pos=0 ; pos<charCount ;)
115 TFindFieldInfo infoOrig,infoCopy;
116 anOriginal->FindFields(infoOrig,pos,charCount-pos);
117 aCopy->FindFields(infoCopy,pos,charCount-pos);
118 test(infoOrig==infoCopy);
119 pos += infoOrig.iFirstFieldPos+infoOrig.iFirstFieldLen;
120 if (infoOrig.iFieldCountInRange==0)
128 TBool CT_STREAM::UpdateField(TInt aPos,CTextFieldSet* aSet)
130 // find out which field aPos is in
133 ret=aSet->FindFields(info,aPos);
136 HBufC* buf = HBufC::NewL(5);
137 CleanupStack::PushL(buf);
138 TInt err=aSet->NewFieldValueL(buf,aPos);
140 // Notify FieldSet of update
141 aSet->NotifyFieldUpdate(aPos,buf->Length());
143 CleanupStack::PopAndDestroy();
148 void CT_STREAM::test2()
149 // Streams an empty field set
152 INFO_PRINTF1(_L("- streaming of a default FieldSet (no fields)"));
153 testStoreRestore(TheFieldSetCopy,TheFieldSetOriginal);
154 test(IsEqual(TheFieldSetCopy,TheFieldSetOriginal));
155 TheFieldSetOriginal->Reset();
156 TheFieldSetCopy->Reset();
160 void CT_STREAM::test3()
161 // Streams a field set containing 100 chars & 1 field
164 INFO_PRINTF1(_L("- streaming with a field"));
166 // insert a block of text into the original
167 TheFieldSetOriginal->NotifyInsertion(0,100); // pos=0, len=100
168 // insert a field into the original
169 CTextField* field=TheFieldSetOriginal->NewFieldL(KDummyFieldUid);
171 TInt ret=TheFieldSetOriginal->InsertFieldL(10,field,KDummyFieldUid); // pos=10
173 ret=UpdateField(10,TheFieldSetOriginal);
177 testStoreRestore(TheFieldSetCopy,TheFieldSetOriginal);
178 test(IsEqual(TheFieldSetCopy,TheFieldSetOriginal));
179 TheFieldSetOriginal->Reset();
180 TheFieldSetCopy->Reset();
184 void CT_STREAM::test4()
185 // Streams a field set into a non-empty target
188 INFO_PRINTF1(_L("- streaming into a non-empty target"));
190 // insert a block of text into the original
191 TheFieldSetOriginal->NotifyInsertion(0,100); // pos=0, len=100
192 // insert a field into the original
193 CTextField* field=TheFieldSetOriginal->NewFieldL(KDummyFieldUid);
195 TInt ret=TheFieldSetOriginal->InsertFieldL(10,field,KDummyFieldUid); // pos=10
197 ret=UpdateField(10,TheFieldSetOriginal);
200 // insert a block of text into the copy
201 TheFieldSetCopy->NotifyInsertion(0,40); // pos=0, len=40
202 // insert two fields into the copy
203 CTextField* field2 = TheFieldSetCopy->NewFieldL(KDummyFieldUid);
204 CTextField* field3 = TheFieldSetCopy->NewFieldL(KDummyFieldUid);
205 ret=TheFieldSetCopy->InsertFieldL(20,field2,KDummyFieldUid);
207 ret=TheFieldSetCopy->InsertFieldL(30,field3,KDummyFieldUid);
209 ret=UpdateField(20,TheFieldSetCopy);
211 ret=UpdateField(33,TheFieldSetCopy);
215 testStoreRestore(TheFieldSetCopy,TheFieldSetOriginal);
216 test(IsEqual(TheFieldSetCopy,TheFieldSetOriginal));
217 TheFieldSetOriginal->Reset();
218 TheFieldSetCopy->Reset();
222 void CT_STREAM::test5()
223 // Streams a field set containing an out of date field
226 INFO_PRINTF1(_L("- streaming an out of date field"));
228 // insert a block of text into the original
229 TheFieldSetOriginal->NotifyInsertion(0,100); // pos=0, len=100
230 // insert a field into the original
231 CTextField* field=TheFieldSetOriginal->NewFieldL(KDummyFieldUid);
233 TInt ret=TheFieldSetOriginal->InsertFieldL(10,field,KDummyFieldUid); // pos=10
235 ret=UpdateField(10,TheFieldSetOriginal);
237 // invalidate the field
238 TheFieldSetOriginal->NotifyInsertion(11,7); // pos=11, len=7
241 testStoreRestore(TheFieldSetCopy,TheFieldSetOriginal);
242 test(IsEqual(TheFieldSetCopy,TheFieldSetOriginal));
243 TheFieldSetOriginal->Reset();
244 TheFieldSetCopy->Reset();
248 void CT_STREAM::test6()
249 // Tests that copy/paste methods exist
252 INFO_PRINTF1(_L("- testing copy/paste methods with empty array"));
253 testCopyPaste(TheFieldSetCopy,TheFieldSetOriginal,0,0,0); // copyPos,Len,PastePos
254 test(IsEqual(TheFieldSetCopy,TheFieldSetOriginal));
255 TheFieldSetOriginal->Reset();
256 TheFieldSetCopy->Reset();
260 void CT_STREAM::test7()
261 // Tests copy/paste methods in detail
264 INFO_PRINTF1(_L("- testing copy/paste methods in detail"));
265 // insert a block of text into the original
266 TheFieldSetOriginal->NotifyInsertion(0,100); // pos=0, len=100
267 // insert a field into the original
268 CTextField* field=TheFieldSetOriginal->NewFieldL(KDummyFieldUid);
270 TInt ret=TheFieldSetOriginal->InsertFieldL(10,field,KDummyFieldUid); // pos=10
272 TBool ok=UpdateField(10,TheFieldSetOriginal);
274 test(TheFieldSetOriginal->CharCount()==103);
275 // copy out of that into an empty array
276 testCopyPaste(TheFieldSetCopy,TheFieldSetOriginal,5,15,0); // copyPos,Len,PastePos
277 // check that the contents of the array are as expected
279 TheFieldSetCopy->FindFields(info,0,15);
280 test(TheFieldSetCopy->CharCount()==15);
281 test(TheFieldSetCopy->FieldCount()==1);
282 test(info.iFieldCountInRange==1);
283 test(info.iFirstFieldPos==5);
284 test(info.iFirstFieldLen==3);
285 TheFieldSetCopy->Reset();
286 // test copying part of a field
287 testCopyPaste(TheFieldSetCopy,TheFieldSetOriginal,5,6,0); // copyPos,Len,PastePos
288 TheFieldSetCopy->FindFields(info,0,6);
289 test(TheFieldSetCopy->CharCount()==6);
290 test(TheFieldSetCopy->FieldCount()==0);
291 test(info.iFieldCountInRange==0);
292 TheFieldSetCopy->Reset();
293 // test copying exactly one field
294 testCopyPaste(TheFieldSetCopy,TheFieldSetOriginal,10,3,0); // copyPos,Len,PastePos
295 TheFieldSetCopy->FindFields(info,0,3);
296 test(TheFieldSetCopy->CharCount()==3);
297 test(TheFieldSetCopy->FieldCount()==1);
298 test(info.iFieldCountInRange==1);
299 test(info.iFirstFieldPos==0);
300 test(info.iFirstFieldLen==3);
301 TheFieldSetCopy->Reset();
302 // test pasting into text in a non-empty array
303 TheFieldSetCopy->NotifyInsertion(0,50); // pos=0, len=50
304 testCopyPaste(TheFieldSetCopy,TheFieldSetOriginal,5,15,10); // copyPos,Len,PastePos
305 TheFieldSetCopy->FindFields(info,0,65);
306 test(TheFieldSetCopy->CharCount()==65);
307 test(TheFieldSetCopy->FieldCount()==1);
308 test(info.iFieldCountInRange==1);
309 test(info.iFirstFieldPos==15);
310 test(info.iFirstFieldLen==3);
311 TheFieldSetCopy->Reset();
312 // test pasting into a field
313 TheFieldSetCopy->NotifyInsertion(0,50); // pos=0, len=50
314 field=TheFieldSetCopy->NewFieldL(KDummyFieldUid);
316 ret=TheFieldSetCopy->InsertFieldL(10,field,KDummyFieldUid); // pos=10
318 ok=UpdateField(10,TheFieldSetCopy);
320 testCopyPaste(TheFieldSetCopy,TheFieldSetOriginal,5,15,11); // copyPos,Len,PastePos
321 TheFieldSetCopy->FindFields(info,0,68);
322 test(TheFieldSetCopy->CharCount()==68);
323 test(TheFieldSetCopy->FieldCount()==1);
324 test(info.iFieldCountInRange==1);
325 test(info.iFirstFieldPos==10);
326 test(info.iFirstFieldLen==18);
327 TheFieldSetCopy->Reset();
328 // test pasting at the start & end of a field
329 TheFieldSetCopy->NotifyInsertion(0,10); // pos=0, len=10
330 field=TheFieldSetCopy->NewFieldL(KDummyFieldUid);
332 ret=TheFieldSetCopy->InsertFieldL(5,field,KDummyFieldUid); // pos=5
334 ok=UpdateField(5,TheFieldSetCopy);
336 testCopyPaste(TheFieldSetCopy,TheFieldSetOriginal,10,3,8); // copyPos,Len,PastePos
337 testCopyPaste(TheFieldSetCopy,TheFieldSetOriginal,10,3,5); // copyPos,Len,PastePos
338 TheFieldSetCopy->FindFields(info,0,19);
339 test(TheFieldSetCopy->CharCount()==19);
340 test(TheFieldSetCopy->FieldCount()==3);
341 test(info.iFieldCountInRange==3);
342 test(info.iFirstFieldPos==5);
343 test(info.iFirstFieldLen==3);
344 TheFieldSetCopy->FindFields(info,8,11);
345 test(info.iFieldCountInRange==2);
346 test(info.iFirstFieldPos==8);
347 test(info.iFirstFieldLen==3);
348 TheFieldSetCopy->FindFields(info,11,8);
349 test(info.iFieldCountInRange==1);
350 test(info.iFirstFieldPos==11);
351 test(info.iFirstFieldLen==3);
353 TheFieldSetOriginal->Reset();
354 TheFieldSetCopy->Reset();
358 void CT_STREAM::test8()
359 // Tests paste method with restricted target length
362 INFO_PRINTF1(_L("- testing paste method with restricted length"));
363 // insert a block of text into the original
364 TheFieldSetOriginal->NotifyInsertion(0,100); // pos=0, len=100
365 // insert 2 fields into the original
367 CTextField* field=TheFieldSetOriginal->NewFieldL(KDummyFieldUid);
369 TInt ret=TheFieldSetOriginal->InsertFieldL(10,field,KDummyFieldUid); // pos=10
371 TBool ok=UpdateField(10,TheFieldSetOriginal);
374 field=TheFieldSetOriginal->NewFieldL(KDummyFieldUid);
376 ret=TheFieldSetOriginal->InsertFieldL(20,field,KDummyFieldUid); // pos=20
378 ok=UpdateField(20,TheFieldSetOriginal);
380 // paste part of original into copy with length greater than required
381 testCopyPaste(TheFieldSetCopy,TheFieldSetOriginal,10,5,0,7); // copyPos,Len,PastePos,PasteLen
383 TheFieldSetCopy->FindFields(info,0,5);
384 test(TheFieldSetCopy->CharCount()==5);
385 test(TheFieldSetCopy->FieldCount()==1);
386 test(info.iFieldCountInRange==1);
387 test(info.iFirstFieldPos==0);
388 test(info.iFirstFieldLen==3);
389 TheFieldSetCopy->Reset();
390 // paste part of original into copy with length equal to that required
391 testCopyPaste(TheFieldSetCopy,TheFieldSetOriginal,10,5,0,5); // copyPos,Len,PastePos,PasteLen
392 TheFieldSetCopy->FindFields(info,0,5);
393 test(TheFieldSetCopy->CharCount()==5);
394 test(TheFieldSetCopy->FieldCount()==1);
395 test(info.iFieldCountInRange==1);
396 test(info.iFirstFieldPos==0);
397 test(info.iFirstFieldLen==3);
398 TheFieldSetCopy->Reset();
399 // paste part of original into copy with length zero
400 testCopyPaste(TheFieldSetCopy,TheFieldSetOriginal,10,5,0,0); // copyPos,Len,PastePos,PasteLen
401 test(TheFieldSetCopy->CharCount()==0);
402 test(TheFieldSetCopy->FieldCount()==0);
403 TheFieldSetCopy->Reset();
404 // paste part of original into copy with length st field is chopped
405 testCopyPaste(TheFieldSetCopy,TheFieldSetOriginal,5,10,0,6); // copyPos,Len,PastePos,PasteLen
406 TheFieldSetCopy->FindFields(info,0,6);
407 test(TheFieldSetCopy->CharCount()==6);
408 test(TheFieldSetCopy->FieldCount()==0);
409 test(info.iFieldCountInRange==0);
410 TheFieldSetCopy->Reset();
411 // paste part of original into copy with length st one field is left off, the other pasted in
412 testCopyPaste(TheFieldSetCopy,TheFieldSetOriginal,5,20,0,10); // copyPos,Len,PastePos,PasteLen
413 TheFieldSetCopy->FindFields(info,0,10);
414 test(TheFieldSetCopy->CharCount()==10);
415 test(TheFieldSetCopy->FieldCount()==1);
416 test(info.iFieldCountInRange==1);
417 test(info.iFirstFieldPos==5);
418 test(info.iFirstFieldLen==3);
420 TheFieldSetOriginal->Reset();
421 TheFieldSetCopy->Reset();
425 void CT_STREAM::test9()
426 // Tests paste into set with no field factory
427 // Should convert all pasted fields to text...
430 INFO_PRINTF1(_L("- testing paste into set with no field factory"));
431 TheFieldSetCopy->SetFieldFactory(NULL);
432 // insert a block of text into the original
433 TheFieldSetOriginal->NotifyInsertion(0,100); // pos=0, len=100
434 // insert a field into the original
435 CTextField* field=TheFieldSetOriginal->NewFieldL(KDummyFieldUid);
437 TInt ret=TheFieldSetOriginal->InsertFieldL(10,field,KDummyFieldUid); // pos=10
439 TBool ok=UpdateField(10,TheFieldSetOriginal);
441 // paste part of original into copy
442 testCopyPaste(TheFieldSetCopy,TheFieldSetOriginal,5,25,0); // copyPos,Len,PastePos
444 TheFieldSetCopy->FindFields(info,0,25);
445 test(TheFieldSetCopy->CharCount()==25);
446 test(TheFieldSetCopy->FieldCount()==0);
448 TheFieldSetOriginal->Reset();
449 TheFieldSetCopy->Reset();
450 TheFieldSetCopy->SetFieldFactory(TheFactory);
454 void CT_STREAM::test10()
455 // Streams a field set containing 100 chars & 1 field
458 INFO_PRINTF1(_L("- streaming CDateTimeField"));
460 // insert a block of text into the original
461 TheFieldSetOriginal->NotifyInsertion(0,100); // pos=0, len=100
462 // insert a field into the original
463 CTextField* field=TheFieldSetOriginal->NewFieldL(KDateTimeFieldUid);
465 TBuf<14> buff = _L("%-A%*I%:1%T%+A");
466 ((CDateTimeField*)field)->SetFormat(buff); // 01:53pm
467 TInt ret=TheFieldSetOriginal->InsertFieldL(10,field,KDateTimeFieldUid); // pos=10
469 ret=UpdateField(10,TheFieldSetOriginal);
472 HBufC* miniBuf = HBufC::NewL(5);
473 CleanupStack::PushL(miniBuf);
474 ret=TheFieldSetOriginal->NewFieldValueL(miniBuf,10); // pos=10
476 TPtr buf = miniBuf->Des();
477 INFO_PRINTF1(_L(" Value before streaming: "));
479 INFO_PRINTF1(_L("\n"));
481 testStoreRestore(TheFieldSetCopy,TheFieldSetOriginal);
482 test(IsEqual(TheFieldSetCopy,TheFieldSetOriginal));
484 ret=TheFieldSetCopy->NewFieldValueL(miniBuf,10); // pos=10
486 buf = miniBuf->Des();
487 INFO_PRINTF1(_L(" Value after streaming: "));
489 INFO_PRINTF1(_L("\n"));
490 CleanupStack::PopAndDestroy(); // miniBuf
491 TheFieldSetOriginal->Reset();
492 TheFieldSetCopy->Reset();
496 void CT_STREAM::TestStreamingL()
498 // Test streaming of PrintSetup info.
499 // Stream from one copy to another, then compare
502 // create the PrintSetups
503 INFO_PRINTF1(_L("Streaming CTextFieldSet"));
505 // instantiate the factory and FieldSet
506 TheFactory = new(ELeave) TTestFieldFactory();
507 TheFieldSetOriginal = CTextFieldSet::NewL();
508 TheFieldSetOriginal->SetFieldFactory(TheFactory);
509 TheFieldSetCopy = CTextFieldSet::NewL();
510 TheFieldSetCopy->SetFieldFactory(TheFactory);
512 // Use "original" with default settings & test stream
515 // change data in original and test stream again
518 // Stream a field set into a non-empty target
521 // Stream a field set containing an out of date field
524 // Test that copy/paste methods exist
527 // Test copy/paste methods in detail
530 // Test paste with limited target area
533 // Test paste into set with no field factory
536 // Test streaming of built-in types
540 delete TheFieldSetOriginal;
541 delete TheFieldSetCopy;
546 void CT_STREAM::setupCleanup()
548 // Initialise the cleanup stack.
552 TheTrapCleanup=CTrapCleanup::New();
555 for (TInt i=KTestCleanupStack;i>0;i--)\
556 CleanupStack::PushL((TAny*)1);\
558 CleanupStack::Pop(KTestCleanupStack);\
563 void CT_STREAM::DeleteDataFile(const TDesC& aFullName)
566 TInt err = fsSession.Connect();
570 if(fsSession.Entry(aFullName, entry) == KErrNone)
572 RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
573 err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
576 RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
578 err = fsSession.Delete(aFullName);
581 RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
588 RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
593 CT_STREAM::CT_STREAM()
595 SetTestStepName(KTestStep_T_STREAM);
598 TVerdict CT_STREAM::doTestStepL()
600 INFO_PRINTF1(_L("T_STREAM - Fields Persistence"));
601 SetTestStepResult(EFail);
607 TRAPD(error1, TestStreamingL());
609 delete TheTrapCleanup;
612 DeleteDataFile(KTFieldOutputFile); //deletion of data files must be before call to End() - DEF047652
614 if(error1 == KErrNone)
616 SetTestStepResult(EPass);
619 return TestStepResult();