textrendering/texthandling/ttext/T_PICRES.CPP
changeset 32 8b9155204a54
parent 0 1fb32624e06b
child 51 a7c938434754
equal deleted inserted replaced
31:b9ad20498fb4 32:8b9155204a54
       
     1 /*
       
     2 * Copyright (c) 1997-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 <e32std.h>
       
    20 #include <e32base.h>
       
    21 
       
    22 #include <gdi.h>
       
    23 #include <conpics.h>
       
    24 #include <s32file.h>
       
    25 #include <e32test.h>
       
    26 
       
    27 #include <txtrich.h>
       
    28 #include <txtfmlyr.h>
       
    29 #include "TXTMRTSR.H"
       
    30 
       
    31 #include "../incp/T_PMLPAR.H"
       
    32 
       
    33 #define UNUSED_VAR(a) a = a
       
    34 
       
    35 const TInt KTestCleanupStack=0x40;
       
    36 
       
    37 class CContainer : public CBase, public MRichTextStoreResolver
       
    38 	{
       
    39 public:
       
    40 	static CContainer* NewL();
       
    41 	~CContainer();
       
    42 	//
       
    43 	// Mixin
       
    44 	//
       
    45 	virtual const CStreamStore& StreamStoreL(TInt aPos)const;
       
    46 	// Methods
       
    47 	TStreamId StoreL(CStreamStore& aStore)const;
       
    48 	void RestoreL(const CStreamStore& aStore,TStreamId aId,MPictureFactory* aFctry);
       
    49 protected:
       
    50 	CContainer();
       
    51 	void ConstructL();
       
    52 public:
       
    53 	CRichText* iText;
       
    54 	const CParaFormatLayer* iGlobalParaFormatLayer;
       
    55 	const CCharFormatLayer* iGlobalCharFormatLayer;
       
    56 	};
       
    57 
       
    58 
       
    59 LOCAL_D RTest test(_L("Testing Picture Restorer mechanism"));
       
    60 LOCAL_D CTrapCleanup* TheTrapCleanup;
       
    61 LOCAL_D RFs TheFs;  // the file server
       
    62 LOCAL_D RFile TheFile;  // the data file
       
    63 LOCAL_D CFileStore* TheStore;  // concrete CStreamStore
       
    64 LOCAL_D CParser* TheParser;
       
    65 LOCAL_D CContainer* TheContainer;
       
    66 LOCAL_D CStreamStore* TheDeferredPictureStore;
       
    67 
       
    68 CContainer* CContainer::NewL()
       
    69 // Create new container & set its components.
       
    70 //
       
    71 	{
       
    72 	CContainer* self=new(ELeave) CContainer;
       
    73 	CleanupStack::PushL(self);
       
    74 	self->ConstructL();
       
    75 	CleanupStack::Pop();
       
    76 	return self;
       
    77 	}
       
    78 
       
    79 
       
    80 CContainer::CContainer()
       
    81 	{
       
    82 	}
       
    83 
       
    84 
       
    85 void CContainer::ConstructL()
       
    86 	{
       
    87 	TFileName theFileName = _L("z:\\test\\app-framework\\etext\\t_picres.pml");
       
    88 	TheParser=CParser::NewL();
       
    89 	CleanupStack::PushL(TheParser);
       
    90 	iText=TheParser->ParseL(theFileName);
       
    91 	CleanupStack::PopAndDestroy();
       
    92 	iGlobalParaFormatLayer=iText->GlobalParaFormatLayer();
       
    93 	iGlobalCharFormatLayer=iText->GlobalCharFormatLayer();
       
    94 	}
       
    95 
       
    96 
       
    97 CContainer::~CContainer()
       
    98 	{
       
    99 	delete iText;
       
   100 	delete (CParaFormatLayer*)iGlobalParaFormatLayer;
       
   101 	delete (CCharFormatLayer*)iGlobalCharFormatLayer;
       
   102 	}
       
   103 
       
   104 
       
   105 const CStreamStore& CContainer::StreamStoreL(TInt /*aPos*/)const
       
   106 // Return the deferred picture store.
       
   107 // In this instance, the deferred picture store does not vary with document position.
       
   108 //
       
   109 	{return *TheDeferredPictureStore;}
       
   110 
       
   111 
       
   112 TStreamId CContainer::StoreL(CStreamStore& aStore)const
       
   113 // Store this component
       
   114 //
       
   115 	{
       
   116 	CStoreMap* map=CStoreMap::NewLC(aStore);
       
   117 	iText->StoreComponentsL(aStore,*map);
       
   118 	//
       
   119 	RStoreWriteStream stream(*map);
       
   120 	TStreamId id=stream.CreateLC(aStore);
       
   121 	iGlobalParaFormatLayer->ExternalizeL(stream);
       
   122 	iGlobalCharFormatLayer->ExternalizeL(stream);
       
   123 	stream<< *iText;
       
   124 	stream.CommitL();
       
   125 	//
       
   126 	map->Reset();
       
   127 	CleanupStack::PopAndDestroy(2);
       
   128 	return id;
       
   129 	}
       
   130 
       
   131 
       
   132 void CContainer::RestoreL(const CStreamStore& aStore,TStreamId aId,MPictureFactory* aFactory)
       
   133 // Restore this component
       
   134 //
       
   135 	{
       
   136 	RStoreReadStream stream;
       
   137 	stream.OpenLC(aStore,aId);
       
   138 	iGlobalParaFormatLayer=CParaFormatLayer::NewL(stream);
       
   139 	iGlobalCharFormatLayer=CCharFormatLayer::NewL(stream);
       
   140 	iText=CRichText::NewL(iGlobalParaFormatLayer,iGlobalCharFormatLayer);
       
   141 	iText->SetPictureFactory(aFactory,this);
       
   142 	stream>> *iText;
       
   143 	//
       
   144 	CleanupStack::PopAndDestroy();
       
   145 	//
       
   146 	iText->RestoreComponentsL(aStore);
       
   147 	}
       
   148 
       
   149 _LIT(KOutputFile, "c:\\etext\\t_word.doc");
       
   150 LOCAL_C void testPictureRestorer(TBool aDeferPictureLoad=ETrue)
       
   151 // Test Picture persistance.
       
   152 //
       
   153     {
       
   154 	//
       
   155 	TheFs.Connect();
       
   156 	//
       
   157 	TheFs.Delete(KOutputFile);
       
   158 	TheFs.MkDirAll(KOutputFile);
       
   159 	TheStore=CDirectFileStore::CreateL(TheFs,KOutputFile,EFileRead|EFileWrite);
       
   160 	TheDeferredPictureStore=TheStore;
       
   161 	CleanupStack::PushL(TheStore);
       
   162 	TheStore->SetTypeL(KDirectFileStoreLayoutUid);
       
   163 	//
       
   164 	// Create concrete picture factory.
       
   165 	MDemPictureFactory* factory=new(ELeave) MDemPictureFactory;
       
   166 
       
   167 	TheContainer->iText->Reset();
       
   168 	TheContainer->iText->InsertL(0,_L("Hello Duncan how"));
       
   169 
       
   170 	TheContainer->iText->SetPictureFactory(factory,TheContainer);
       
   171 	// Create some pictures.
       
   172 	CXzePicture* pic1=CXzePicture::NewL('x');
       
   173 		CleanupStack::PushL(pic1);
       
   174 	CXzePicture* pic2=CXzePicture::NewL('z');
       
   175 		CleanupStack::PushL(pic2);
       
   176 	CXzePicture* pic3=CXzePicture::NewL('e');
       
   177 		CleanupStack::PushL(pic3);
       
   178 	//
       
   179 	// Create the picture headers
       
   180 	TPictureHeader hdr1;
       
   181 	TPictureHeader hdr2;
       
   182 	TPictureHeader hdr3;
       
   183 	//
       
   184 	TSize size;
       
   185 	pic1->GetSizeInTwips(size);
       
   186 	hdr1.iPictureType=KUidXzePictureType;
       
   187 	hdr1.iPicture=pic1;
       
   188 	hdr2.iPictureType=KUidXzePictureType;
       
   189 	hdr2.iPicture=pic2;
       
   190 	hdr3.iPictureType=KUidXzePictureType;
       
   191 	hdr3.iPicture=pic3;
       
   192 	//
       
   193 	// Insert the pictures into the rich text
       
   194 	TBool hasMarkupData=TheContainer->iText->HasMarkupData();
       
   195 	test(!hasMarkupData);
       
   196 	TheContainer->iText->CancelInsertCharFormat();
       
   197 	TheContainer->iText->InsertL(0,hdr1);
       
   198 	TheContainer->iText->InsertL(5,hdr2);
       
   199 	TheContainer->iText->InsertL(7,hdr3);
       
   200 	TheContainer->iText->InsertL(0,CEditableText::EParagraphDelimiter);
       
   201 	TheContainer->iText->CancelInsertCharFormat();
       
   202 	TheContainer->iText->InsertL(2,CEditableText::EParagraphDelimiter);
       
   203 	CleanupStack::Pop(3);
       
   204 	hasMarkupData=TheContainer->iText->HasMarkupData();
       
   205 	test(hasMarkupData);
       
   206 	//
       
   207 	// High level Store context
       
   208 	TStreamId id=TheContainer->StoreL(*TheStore);
       
   209 //
       
   210 	delete TheContainer->iText;
       
   211 	delete (CParaFormatLayer*)TheContainer->iGlobalParaFormatLayer;
       
   212 	delete (CCharFormatLayer*)TheContainer->iGlobalCharFormatLayer;
       
   213 //
       
   214 //
       
   215 //	Now restore the container with rich text
       
   216 	TheContainer->RestoreL(*TheStore,id,factory);
       
   217 	if (!aDeferPictureLoad)
       
   218 		TheContainer->iText->DetachFromStoreL(CPicture::EDetachFull);
       
   219 	//
       
   220 	hasMarkupData=TheContainer->iText->HasMarkupData();
       
   221 	test(hasMarkupData);
       
   222 	test(TheContainer->iText->ParagraphCount()==3);
       
   223 	test(TheContainer->iText->DocumentLength()==21);
       
   224 	TPtrC view;
       
   225 	TCharFormat format;
       
   226 	CPicture* picture;
       
   227 	//
       
   228 	// TEST THE PICTURE HEADERS, DEPENDING ON WHETHER DEFERRED LOADING IS SET OR NOT
       
   229 	TPictureHeader hdrA=TheContainer->iText->PictureHeader(1);
       
   230 	test(hdrA.iPictureType==KUidXzePictureType);
       
   231 	if (aDeferPictureLoad)
       
   232 		{
       
   233 		test(hdrA.iPicture.IsId());
       
   234 		}
       
   235 	else
       
   236 		{
       
   237 		test(hdrA.iPicture!=NULL);
       
   238 		test(hdrA.iPicture.IsPtr());
       
   239 		test(((CXzePicture*)hdrA.iPicture.AsPtr())->iLabel=='x');
       
   240 		}
       
   241 	TPictureHeader hdrB=TheContainer->iText->PictureHeader(7);
       
   242 	test(hdrB.iPictureType==KUidXzePictureType);
       
   243 	if (aDeferPictureLoad)
       
   244 		{
       
   245 		test(hdrB.iPicture.IsId());
       
   246 		}
       
   247 	else
       
   248 		{
       
   249 		test(hdrB.iPicture!=NULL);
       
   250 		test(hdrB.iPicture.IsPtr());
       
   251 		test(((CXzePicture*)hdrB.iPicture.AsPtr())->iLabel=='z');
       
   252 		}
       
   253 	TPictureHeader hdrC=TheContainer->iText->PictureHeader(9);
       
   254 	test(hdrC.iPictureType==KUidXzePictureType);
       
   255 	if (aDeferPictureLoad)
       
   256 		{
       
   257 		test(hdrC.iPicture.IsId());
       
   258 		}
       
   259 	else
       
   260 		{
       
   261 		test(hdrC.iPicture!=NULL);
       
   262 		test(hdrC.iPicture.IsPtr());
       
   263 		test(((CXzePicture*)hdrC.iPicture.AsPtr())->iLabel=='e');
       
   264 		}
       
   265 	TPictureHeader hdrD=TheContainer->iText->PictureHeader(0);  // This is not a picture character
       
   266 	test(hdrD.iPictureType==KNullUid);
       
   267 	test(hdrD.iPicture==NULL);
       
   268 	TSize dummySize;
       
   269 	test(hdrD.iSize==dummySize);
       
   270 	//
       
   271 	TheContainer->iText->GetChars(view,format,1);
       
   272 	test(view[0]==CEditableText::EPictureCharacter);
       
   273 	picture=TheContainer->iText->PictureHandleL(1);
       
   274 	test(((CXzePicture*)picture)->iLabel=='x');
       
   275 	
       
   276 	TheContainer->iText->GetChars(view,format,7);
       
   277 	test(view[0]==CEditableText::EPictureCharacter);
       
   278 	picture=TheContainer->iText->PictureHandleL(7);
       
   279 	test(((CXzePicture*)picture)->iLabel=='z');
       
   280 	
       
   281 	TheContainer->iText->GetChars(view,format,9);
       
   282 	test(view[0]==CEditableText::EPictureCharacter);
       
   283 	picture=TheContainer->iText->PictureHandleL(9);
       
   284 	test(((CXzePicture*)picture)->iLabel=='e');
       
   285 
       
   286 	delete factory;
       
   287 	CleanupStack::PopAndDestroy();  // TheStore
       
   288 	TheFs.Close();
       
   289     }
       
   290 
       
   291 _LIT(KOutputFile1, "c:\\etext\\t_word1.doc");
       
   292 LOCAL_C void testPictureRestorer2(TBool aAlwaysFailToLoad=EFalse)
       
   293 // Test Picture persistance.
       
   294 //
       
   295     {
       
   296 	//
       
   297 	TheFs.Connect();
       
   298 	//
       
   299 	TheFs.Delete(KOutputFile1);
       
   300 	TheFs.MkDirAll(KOutputFile1);
       
   301 	TheStore=CDirectFileStore::CreateL(TheFs,KOutputFile1,EFileRead|EFileWrite);
       
   302 	TheDeferredPictureStore=TheStore;
       
   303 	CleanupStack::PushL(TheStore);
       
   304 	TheStore->SetTypeL(KDirectFileStoreLayoutUid);
       
   305 	//
       
   306 	// Create concrete picture factory.
       
   307 	MDemPictureFactory* factory=new(ELeave) MDemPictureFactory;
       
   308 
       
   309 	TheContainer->iText->Reset();
       
   310 	TheContainer->iText->InsertL(0,_L("Hello Duncan how"));
       
   311 
       
   312 	TheContainer->iText->SetPictureFactory(factory,TheContainer);
       
   313 	// Create some pictures.
       
   314 	CXzeDoor* pic1=CXzeDoor::NewL('x',aAlwaysFailToLoad);
       
   315 		CleanupStack::PushL(pic1);
       
   316 	CXzeDoor* pic2=CXzeDoor::NewL('z',aAlwaysFailToLoad);
       
   317 		CleanupStack::PushL(pic2);
       
   318 	CXzePicture* pic3=CXzePicture::NewL('e');  // Control: will always load.
       
   319 		CleanupStack::PushL(pic3);
       
   320 	//
       
   321 	// Create the picture headers
       
   322 	TPictureHeader hdr1;
       
   323 	TPictureHeader hdr2;
       
   324 	TPictureHeader hdr3;
       
   325 	//
       
   326 	TSize size;
       
   327 	pic1->GetSizeInTwips(size);
       
   328 	hdr1.iPictureType=KUidXzeDoorType;
       
   329 	hdr1.iPicture=pic1;
       
   330 	hdr2.iPictureType=KUidXzeDoorType;
       
   331 	hdr2.iPicture=pic2;
       
   332 	hdr3.iPictureType=KUidXzePictureType;
       
   333 	hdr3.iPicture=pic3;
       
   334 	//
       
   335 	// Insert the pictures into the rich text
       
   336 	TBool hasMarkupData=TheContainer->iText->HasMarkupData();
       
   337 	test(!hasMarkupData);
       
   338 	TheContainer->iText->CancelInsertCharFormat();
       
   339 	TheContainer->iText->InsertL(0,hdr1);
       
   340 	TheContainer->iText->InsertL(5,hdr2);
       
   341 	TheContainer->iText->InsertL(7,hdr3);
       
   342 	TheContainer->iText->InsertL(0,CEditableText::EParagraphDelimiter);
       
   343 	TheContainer->iText->CancelInsertCharFormat();
       
   344 	TheContainer->iText->InsertL(2,CEditableText::EParagraphDelimiter);
       
   345 	CleanupStack::Pop(3);  // pic1,2,3 - ownership transferred to rich text
       
   346 	hasMarkupData=TheContainer->iText->HasMarkupData();
       
   347 	test(hasMarkupData);
       
   348 	//
       
   349 	// High level Store context - all pictures currently in memory
       
   350 	TStreamId id=TheContainer->StoreL(*TheStore);
       
   351 //
       
   352 	delete TheContainer->iText;
       
   353 	delete (CParaFormatLayer*)TheContainer->iGlobalParaFormatLayer;
       
   354 	delete (CCharFormatLayer*)TheContainer->iGlobalCharFormatLayer;
       
   355 //
       
   356 //
       
   357 //	Now restore the container with rich text
       
   358 	TheContainer->RestoreL(*TheStore,id,factory);
       
   359 //
       
   360 //
       
   361 //  Now store the stuff again
       
   362 	TRAPD(ret,
       
   363 	TheContainer->iText->DetachFromStoreL(CPicture::EDetachFull));
       
   364 	if (ret==KErrNotSupported)
       
   365 		test.Printf(_L("   SIMULATION: Some picture data is not supported by the current factory."));
       
   366 //	if (aAlwaysFailToLoad)
       
   367 //		test(error==KErrNotFound);
       
   368 //	else
       
   369 //		test(error==KErrNone);
       
   370 	id=KNullStreamId;
       
   371 	TRAP(ret,
       
   372 	id=TheContainer->StoreL(*TheStore));
       
   373 	test(ret==KErrNone);
       
   374 //
       
   375 // ...and restore it to check what we have got.
       
   376 	delete TheContainer->iText;
       
   377 	delete (CParaFormatLayer*)TheContainer->iGlobalParaFormatLayer;
       
   378 	delete (CCharFormatLayer*)TheContainer->iGlobalCharFormatLayer;
       
   379     TheContainer->RestoreL(*TheStore,id,factory);
       
   380 	TInt pictureCount=TheContainer->iText->PictureCount();
       
   381 	if (aAlwaysFailToLoad)
       
   382 		test(pictureCount==1);
       
   383 	else
       
   384 		test(pictureCount==3);
       
   385 //
       
   386 	delete factory;
       
   387 	CleanupStack::PopAndDestroy();  // TheStore
       
   388 	TheFs.Close();
       
   389     }
       
   390 
       
   391 
       
   392 LOCAL_C  void GoL()
       
   393 // Run the tests
       
   394 //
       
   395 	{
       
   396 	test.Start(_L(" @SYMTestCaseID:SYSLIB-TTEXT-LEGACY-T_PICRES-0001 RichText Storing - with pictures deferred loading "));
       
   397 	TheContainer=CContainer::NewL();
       
   398 	TRAPD(r,
       
   399 	testPictureRestorer());
       
   400 	test(r==KErrNone);
       
   401 	delete TheContainer;  // deletes the rich text, which deletes the contained pictures.
       
   402 	//
       
   403 	//
       
   404 	test.Next(_L("RichText Storing - with pictures auto loading"));
       
   405 	TheContainer=CContainer::NewL();
       
   406 	TRAP(r,
       
   407 	testPictureRestorer(EFalse));  // DO NOT DEFER PICTURE LOADING
       
   408 	test(r==KErrNone);
       
   409 	delete TheContainer;  // deletes the rich text, which deletes the contained pictures.
       
   410 //
       
   411 //
       
   412 //
       
   413 	test.Next(_L("Testing no missing picture app's"));
       
   414 	TheContainer=CContainer::NewL();
       
   415 	TRAP(r,
       
   416 	testPictureRestorer2());
       
   417 	test(r==KErrNone);
       
   418 	delete TheContainer;  // deletes the rich text, which deletes the contained pictures.
       
   419 	//
       
   420 	//
       
   421 /*
       
   422 	TEST NOW REDUNDANT AS OF NEW PICTURE CONTAINMENT MECHANISM AS AT RELEASE 073
       
   423 
       
   424 	test.Next(_L("Testing missing picture app's"));
       
   425 	TheContainer=CContainer::NewL();
       
   426 	TRAP(r,
       
   427 	testPictureRestorer2(ETrue));  // ALWAYS FAIL TO DETACH FROM STORE
       
   428 	test(r==KErrNone);
       
   429 	delete TheContainer;  // deletes the rich text, which deletes the contained pictures.
       
   430 */
       
   431 	}
       
   432 
       
   433 
       
   434 LOCAL_C void setupCleanup()
       
   435 //
       
   436 // Initialise the cleanup stack.
       
   437 //
       
   438     {
       
   439 
       
   440 	TheTrapCleanup=CTrapCleanup::New();
       
   441 	TRAPD(r,\
       
   442 		{\
       
   443 		for (TInt i=KTestCleanupStack;i>0;i--)\
       
   444 			CleanupStack::PushL((TAny*)1);\
       
   445 		test(r==KErrNone);\
       
   446 		CleanupStack::Pop(KTestCleanupStack);\
       
   447 		});
       
   448 	}
       
   449 
       
   450 
       
   451 LOCAL_C void DeleteDataFile(const TDesC& aFullName)
       
   452 	{
       
   453 	RFs fsSession;
       
   454 	TInt err = fsSession.Connect();
       
   455 	if(err == KErrNone)
       
   456 		{
       
   457 		TEntry entry;
       
   458 		if(fsSession.Entry(aFullName, entry) == KErrNone)
       
   459 			{
       
   460 			RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName);
       
   461 			err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly);
       
   462 			if(err != KErrNone) 
       
   463 				{
       
   464 				RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName);
       
   465 				}
       
   466 			err = fsSession.Delete(aFullName);
       
   467 			if(err != KErrNone) 
       
   468 				{
       
   469 				RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName);
       
   470 				}
       
   471 			}
       
   472 		fsSession.Close();
       
   473 		}
       
   474 	else
       
   475 		{
       
   476 		RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName);
       
   477 		}
       
   478 	}
       
   479 
       
   480 GLDEF_C TInt E32Main()
       
   481 //
       
   482 // Test the streaming framework.
       
   483 //
       
   484     {
       
   485 
       
   486 	test.Title();
       
   487     __UHEAP_MARK;
       
   488 	setupCleanup();
       
   489 	TRAPD(r,GoL());
       
   490     test(r == KErrNone);
       
   491 
       
   492 	delete TheTrapCleanup;
       
   493 	
       
   494 	__UHEAP_MARKEND;
       
   495 	
       
   496 	::DeleteDataFile(KOutputFile);		//deletion of data files must be before call to End() - DEF047652
       
   497 	::DeleteDataFile(KOutputFile1);	
       
   498 	
       
   499 	test.End();
       
   500 	test.Close();
       
   501 
       
   502 	return 0;
       
   503     }