sl@0: /* sl@0: * Copyright (c) 1997-2010 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include sl@0: #include sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #include sl@0: #include sl@0: #include "TXTMRTSR.H" sl@0: sl@0: #include "../incp/T_PMLPAR.H" sl@0: #include "T_PICRES.h" sl@0: sl@0: LOCAL_D CTestStep *pTestStep = NULL; sl@0: #define test(cond) \ sl@0: { \ sl@0: TBool __bb = (cond); \ sl@0: pTestStep->TEST(__bb); \ sl@0: if (!__bb) \ sl@0: { \ sl@0: pTestStep->ERR_PRINTF1(_L("ERROR: Test Failed")); \ sl@0: User::Leave(1); \ sl@0: } \ sl@0: } sl@0: #undef INFO_PRINTF1 sl@0: #undef INFO_PRINTF2 sl@0: // copy from tefexportconst.h sl@0: #define INFO_PRINTF1(p1) pTestStep->Logger().LogExtra(((TText8*)__FILE__), __LINE__, ESevrInfo, (p1)) sl@0: #define INFO_PRINTF2(p1, p2) pTestStep->Logger().LogExtra(((TText8*)__FILE__), __LINE__, ESevrInfo, (p1), (p2)) sl@0: sl@0: sl@0: #define UNUSED_VAR(a) a = a sl@0: sl@0: const TInt KTestCleanupStack=0x40; sl@0: sl@0: class CContainer : public CBase, public MRichTextStoreResolver sl@0: { sl@0: public: sl@0: static CContainer* NewL(); sl@0: ~CContainer(); sl@0: // sl@0: // Mixin sl@0: // sl@0: virtual const CStreamStore& StreamStoreL(TInt aPos)const; sl@0: // Methods sl@0: TStreamId StoreL(CStreamStore& aStore)const; sl@0: void RestoreL(const CStreamStore& aStore,TStreamId aId,MPictureFactory* aFctry); sl@0: protected: sl@0: CContainer(); sl@0: void ConstructL(); sl@0: public: sl@0: CRichText* iText; sl@0: const CParaFormatLayer* iGlobalParaFormatLayer; sl@0: const CCharFormatLayer* iGlobalCharFormatLayer; sl@0: }; sl@0: sl@0: LOCAL_D CTrapCleanup* TheTrapCleanup; sl@0: LOCAL_D RFs TheFs; // the file server sl@0: LOCAL_D RFile TheFile; // the data file sl@0: LOCAL_D CFileStore* TheStore; // concrete CStreamStore sl@0: LOCAL_D CParser* TheParser; sl@0: LOCAL_D CContainer* TheContainer; sl@0: LOCAL_D CStreamStore* TheDeferredPictureStore; sl@0: sl@0: CContainer* CContainer::NewL() sl@0: // Create new container & set its components. sl@0: // sl@0: { sl@0: CContainer* self=new(ELeave) CContainer; sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(); sl@0: CleanupStack::Pop(); sl@0: return self; sl@0: } sl@0: sl@0: sl@0: CContainer::CContainer() sl@0: { sl@0: } sl@0: sl@0: sl@0: void CContainer::ConstructL() sl@0: { sl@0: TFileName theFileName = _L("z:\\test\\app-framework\\etext\\t_picres.pml"); sl@0: TheParser=CParser::NewL(); sl@0: CleanupStack::PushL(TheParser); sl@0: iText=TheParser->ParseL(theFileName); sl@0: CleanupStack::PopAndDestroy(); sl@0: iGlobalParaFormatLayer=iText->GlobalParaFormatLayer(); sl@0: iGlobalCharFormatLayer=iText->GlobalCharFormatLayer(); sl@0: } sl@0: sl@0: sl@0: CContainer::~CContainer() sl@0: { sl@0: delete iText; sl@0: delete (CParaFormatLayer*)iGlobalParaFormatLayer; sl@0: delete (CCharFormatLayer*)iGlobalCharFormatLayer; sl@0: } sl@0: sl@0: sl@0: const CStreamStore& CContainer::StreamStoreL(TInt /*aPos*/)const sl@0: // Return the deferred picture store. sl@0: // In this instance, the deferred picture store does not vary with document position. sl@0: // sl@0: {return *TheDeferredPictureStore;} sl@0: sl@0: sl@0: TStreamId CContainer::StoreL(CStreamStore& aStore)const sl@0: // Store this component sl@0: // sl@0: { sl@0: CStoreMap* map=CStoreMap::NewLC(aStore); sl@0: iText->StoreComponentsL(aStore,*map); sl@0: // sl@0: RStoreWriteStream stream(*map); sl@0: TStreamId id=stream.CreateLC(aStore); sl@0: iGlobalParaFormatLayer->ExternalizeL(stream); sl@0: iGlobalCharFormatLayer->ExternalizeL(stream); sl@0: stream<< *iText; sl@0: stream.CommitL(); sl@0: // sl@0: map->Reset(); sl@0: CleanupStack::PopAndDestroy(2); sl@0: return id; sl@0: } sl@0: sl@0: sl@0: void CContainer::RestoreL(const CStreamStore& aStore,TStreamId aId,MPictureFactory* aFactory) sl@0: // Restore this component sl@0: // sl@0: { sl@0: RStoreReadStream stream; sl@0: stream.OpenLC(aStore,aId); sl@0: iGlobalParaFormatLayer=CParaFormatLayer::NewL(stream); sl@0: iGlobalCharFormatLayer=CCharFormatLayer::NewL(stream); sl@0: iText=CRichText::NewL(iGlobalParaFormatLayer,iGlobalCharFormatLayer); sl@0: iText->SetPictureFactory(aFactory,this); sl@0: stream>> *iText; sl@0: // sl@0: CleanupStack::PopAndDestroy(); sl@0: // sl@0: iText->RestoreComponentsL(aStore); sl@0: } sl@0: sl@0: _LIT(KOutputFile, "c:\\etext\\t_word.doc"); sl@0: LOCAL_C void testPictureRestorer(TBool aDeferPictureLoad=ETrue) sl@0: // Test Picture persistance. sl@0: // sl@0: { sl@0: // sl@0: TheFs.Connect(); sl@0: // sl@0: TheFs.Delete(KOutputFile); sl@0: TheFs.MkDirAll(KOutputFile); sl@0: TheStore=CDirectFileStore::CreateL(TheFs,KOutputFile,EFileRead|EFileWrite); sl@0: TheDeferredPictureStore=TheStore; sl@0: CleanupStack::PushL(TheStore); sl@0: TheStore->SetTypeL(KDirectFileStoreLayoutUid); sl@0: // sl@0: // Create concrete picture factory. sl@0: MDemPictureFactory* factory=new(ELeave) MDemPictureFactory; sl@0: sl@0: TheContainer->iText->Reset(); sl@0: TheContainer->iText->InsertL(0,_L("Hello Duncan how")); sl@0: sl@0: TheContainer->iText->SetPictureFactory(factory,TheContainer); sl@0: // Create some pictures. sl@0: CXzePicture* pic1=CXzePicture::NewL('x'); sl@0: CleanupStack::PushL(pic1); sl@0: CXzePicture* pic2=CXzePicture::NewL('z'); sl@0: CleanupStack::PushL(pic2); sl@0: CXzePicture* pic3=CXzePicture::NewL('e'); sl@0: CleanupStack::PushL(pic3); sl@0: // sl@0: // Create the picture headers sl@0: TPictureHeader hdr1; sl@0: TPictureHeader hdr2; sl@0: TPictureHeader hdr3; sl@0: // sl@0: TSize size; sl@0: pic1->GetSizeInTwips(size); sl@0: hdr1.iPictureType=KUidXzePictureType; sl@0: hdr1.iPicture=pic1; sl@0: hdr2.iPictureType=KUidXzePictureType; sl@0: hdr2.iPicture=pic2; sl@0: hdr3.iPictureType=KUidXzePictureType; sl@0: hdr3.iPicture=pic3; sl@0: // sl@0: // Insert the pictures into the rich text sl@0: TBool hasMarkupData=TheContainer->iText->HasMarkupData(); sl@0: test(!hasMarkupData); sl@0: TheContainer->iText->CancelInsertCharFormat(); sl@0: TheContainer->iText->InsertL(0,hdr1); sl@0: TheContainer->iText->InsertL(5,hdr2); sl@0: TheContainer->iText->InsertL(7,hdr3); sl@0: TheContainer->iText->InsertL(0,CEditableText::EParagraphDelimiter); sl@0: TheContainer->iText->CancelInsertCharFormat(); sl@0: TheContainer->iText->InsertL(2,CEditableText::EParagraphDelimiter); sl@0: CleanupStack::Pop(3); sl@0: hasMarkupData=TheContainer->iText->HasMarkupData(); sl@0: test(hasMarkupData); sl@0: // sl@0: // High level Store context sl@0: TStreamId id=TheContainer->StoreL(*TheStore); sl@0: // sl@0: delete TheContainer->iText; sl@0: delete (CParaFormatLayer*)TheContainer->iGlobalParaFormatLayer; sl@0: delete (CCharFormatLayer*)TheContainer->iGlobalCharFormatLayer; sl@0: // sl@0: // sl@0: // Now restore the container with rich text sl@0: TheContainer->RestoreL(*TheStore,id,factory); sl@0: if (!aDeferPictureLoad) sl@0: TheContainer->iText->DetachFromStoreL(CPicture::EDetachFull); sl@0: // sl@0: hasMarkupData=TheContainer->iText->HasMarkupData(); sl@0: test(hasMarkupData); sl@0: test(TheContainer->iText->ParagraphCount()==3); sl@0: test(TheContainer->iText->DocumentLength()==21); sl@0: TPtrC view; sl@0: TCharFormat format; sl@0: CPicture* picture; sl@0: // sl@0: // TEST THE PICTURE HEADERS, DEPENDING ON WHETHER DEFERRED LOADING IS SET OR NOT sl@0: TPictureHeader hdrA=TheContainer->iText->PictureHeader(1); sl@0: test(hdrA.iPictureType==KUidXzePictureType); sl@0: if (aDeferPictureLoad) sl@0: { sl@0: test(hdrA.iPicture.IsId()); sl@0: } sl@0: else sl@0: { sl@0: test(hdrA.iPicture!=NULL); sl@0: test(hdrA.iPicture.IsPtr()); sl@0: test(((CXzePicture*)hdrA.iPicture.AsPtr())->iLabel=='x'); sl@0: } sl@0: TPictureHeader hdrB=TheContainer->iText->PictureHeader(7); sl@0: test(hdrB.iPictureType==KUidXzePictureType); sl@0: if (aDeferPictureLoad) sl@0: { sl@0: test(hdrB.iPicture.IsId()); sl@0: } sl@0: else sl@0: { sl@0: test(hdrB.iPicture!=NULL); sl@0: test(hdrB.iPicture.IsPtr()); sl@0: test(((CXzePicture*)hdrB.iPicture.AsPtr())->iLabel=='z'); sl@0: } sl@0: TPictureHeader hdrC=TheContainer->iText->PictureHeader(9); sl@0: test(hdrC.iPictureType==KUidXzePictureType); sl@0: if (aDeferPictureLoad) sl@0: { sl@0: test(hdrC.iPicture.IsId()); sl@0: } sl@0: else sl@0: { sl@0: test(hdrC.iPicture!=NULL); sl@0: test(hdrC.iPicture.IsPtr()); sl@0: test(((CXzePicture*)hdrC.iPicture.AsPtr())->iLabel=='e'); sl@0: } sl@0: TPictureHeader hdrD=TheContainer->iText->PictureHeader(0); // This is not a picture character sl@0: test(hdrD.iPictureType==KNullUid); sl@0: test(hdrD.iPicture==NULL); sl@0: TSize dummySize; sl@0: test(hdrD.iSize==dummySize); sl@0: // sl@0: TheContainer->iText->GetChars(view,format,1); sl@0: test(view[0]==CEditableText::EPictureCharacter); sl@0: picture=TheContainer->iText->PictureHandleL(1); sl@0: test(((CXzePicture*)picture)->iLabel=='x'); sl@0: sl@0: TheContainer->iText->GetChars(view,format,7); sl@0: test(view[0]==CEditableText::EPictureCharacter); sl@0: picture=TheContainer->iText->PictureHandleL(7); sl@0: test(((CXzePicture*)picture)->iLabel=='z'); sl@0: sl@0: TheContainer->iText->GetChars(view,format,9); sl@0: test(view[0]==CEditableText::EPictureCharacter); sl@0: picture=TheContainer->iText->PictureHandleL(9); sl@0: test(((CXzePicture*)picture)->iLabel=='e'); sl@0: sl@0: delete factory; sl@0: CleanupStack::PopAndDestroy(); // TheStore sl@0: TheFs.Close(); sl@0: } sl@0: sl@0: _LIT(KOutputFile1, "c:\\etext\\t_word1.doc"); sl@0: LOCAL_C void testPictureRestorer2(TBool aAlwaysFailToLoad=EFalse) sl@0: // Test Picture persistance. sl@0: // sl@0: { sl@0: // sl@0: TheFs.Connect(); sl@0: // sl@0: TheFs.Delete(KOutputFile1); sl@0: TheFs.MkDirAll(KOutputFile1); sl@0: TheStore=CDirectFileStore::CreateL(TheFs,KOutputFile1,EFileRead|EFileWrite); sl@0: TheDeferredPictureStore=TheStore; sl@0: CleanupStack::PushL(TheStore); sl@0: TheStore->SetTypeL(KDirectFileStoreLayoutUid); sl@0: // sl@0: // Create concrete picture factory. sl@0: MDemPictureFactory* factory=new(ELeave) MDemPictureFactory; sl@0: sl@0: TheContainer->iText->Reset(); sl@0: TheContainer->iText->InsertL(0,_L("Hello Duncan how")); sl@0: sl@0: TheContainer->iText->SetPictureFactory(factory,TheContainer); sl@0: // Create some pictures. sl@0: CXzeDoor* pic1=CXzeDoor::NewL('x',aAlwaysFailToLoad); sl@0: CleanupStack::PushL(pic1); sl@0: CXzeDoor* pic2=CXzeDoor::NewL('z',aAlwaysFailToLoad); sl@0: CleanupStack::PushL(pic2); sl@0: CXzePicture* pic3=CXzePicture::NewL('e'); // Control: will always load. sl@0: CleanupStack::PushL(pic3); sl@0: // sl@0: // Create the picture headers sl@0: TPictureHeader hdr1; sl@0: TPictureHeader hdr2; sl@0: TPictureHeader hdr3; sl@0: // sl@0: TSize size; sl@0: pic1->GetSizeInTwips(size); sl@0: hdr1.iPictureType=KUidXzeDoorType; sl@0: hdr1.iPicture=pic1; sl@0: hdr2.iPictureType=KUidXzeDoorType; sl@0: hdr2.iPicture=pic2; sl@0: hdr3.iPictureType=KUidXzePictureType; sl@0: hdr3.iPicture=pic3; sl@0: // sl@0: // Insert the pictures into the rich text sl@0: TBool hasMarkupData=TheContainer->iText->HasMarkupData(); sl@0: test(!hasMarkupData); sl@0: TheContainer->iText->CancelInsertCharFormat(); sl@0: TheContainer->iText->InsertL(0,hdr1); sl@0: TheContainer->iText->InsertL(5,hdr2); sl@0: TheContainer->iText->InsertL(7,hdr3); sl@0: TheContainer->iText->InsertL(0,CEditableText::EParagraphDelimiter); sl@0: TheContainer->iText->CancelInsertCharFormat(); sl@0: TheContainer->iText->InsertL(2,CEditableText::EParagraphDelimiter); sl@0: CleanupStack::Pop(3); // pic1,2,3 - ownership transferred to rich text sl@0: hasMarkupData=TheContainer->iText->HasMarkupData(); sl@0: test(hasMarkupData); sl@0: // sl@0: // High level Store context - all pictures currently in memory sl@0: TStreamId id=TheContainer->StoreL(*TheStore); sl@0: // sl@0: delete TheContainer->iText; sl@0: delete (CParaFormatLayer*)TheContainer->iGlobalParaFormatLayer; sl@0: delete (CCharFormatLayer*)TheContainer->iGlobalCharFormatLayer; sl@0: // sl@0: // sl@0: // Now restore the container with rich text sl@0: TheContainer->RestoreL(*TheStore,id,factory); sl@0: // sl@0: // sl@0: // Now store the stuff again sl@0: TRAPD(ret, sl@0: TheContainer->iText->DetachFromStoreL(CPicture::EDetachFull)); sl@0: if (ret==KErrNotSupported) sl@0: INFO_PRINTF1(_L(" SIMULATION: Some picture data is not supported by the current factory.")); sl@0: // if (aAlwaysFailToLoad) sl@0: // test(error==KErrNotFound); sl@0: // else sl@0: // test(error==KErrNone); sl@0: id=KNullStreamId; sl@0: TRAP(ret, sl@0: id=TheContainer->StoreL(*TheStore)); sl@0: test(ret==KErrNone); sl@0: // sl@0: // ...and restore it to check what we have got. sl@0: delete TheContainer->iText; sl@0: delete (CParaFormatLayer*)TheContainer->iGlobalParaFormatLayer; sl@0: delete (CCharFormatLayer*)TheContainer->iGlobalCharFormatLayer; sl@0: TheContainer->RestoreL(*TheStore,id,factory); sl@0: TInt pictureCount=TheContainer->iText->PictureCount(); sl@0: if (aAlwaysFailToLoad) sl@0: { sl@0: test(pictureCount==1); sl@0: } sl@0: else sl@0: { sl@0: test(pictureCount==3); sl@0: } sl@0: // sl@0: delete factory; sl@0: CleanupStack::PopAndDestroy(); // TheStore sl@0: TheFs.Close(); sl@0: } sl@0: sl@0: sl@0: LOCAL_C void GoL() sl@0: // Run the tests sl@0: // sl@0: { sl@0: INFO_PRINTF1(_L(" @SYMTestCaseID:SYSLIB-TTEXT-LEGACY-T_PICRES-0001 RichText Storing - with pictures deferred loading ")); sl@0: TheContainer=CContainer::NewL(); sl@0: TRAPD(r, sl@0: testPictureRestorer()); sl@0: test(r==KErrNone); sl@0: delete TheContainer; // deletes the rich text, which deletes the contained pictures. sl@0: // sl@0: // sl@0: INFO_PRINTF1(_L("RichText Storing - with pictures auto loading")); sl@0: TheContainer=CContainer::NewL(); sl@0: TRAP(r, sl@0: testPictureRestorer(EFalse)); // DO NOT DEFER PICTURE LOADING sl@0: test(r==KErrNone); sl@0: delete TheContainer; // deletes the rich text, which deletes the contained pictures. sl@0: // sl@0: // sl@0: // sl@0: INFO_PRINTF1(_L("Testing no missing picture app's")); sl@0: TheContainer=CContainer::NewL(); sl@0: TRAP(r, sl@0: testPictureRestorer2()); sl@0: test(r==KErrNone); sl@0: delete TheContainer; // deletes the rich text, which deletes the contained pictures. sl@0: // sl@0: // sl@0: /* sl@0: TEST NOW REDUNDANT AS OF NEW PICTURE CONTAINMENT MECHANISM AS AT RELEASE 073 sl@0: sl@0: INFO_PRINTF1(_L("Testing missing picture app's")); sl@0: TheContainer=CContainer::NewL(); sl@0: TRAP(r, sl@0: testPictureRestorer2(ETrue)); // ALWAYS FAIL TO DETACH FROM STORE sl@0: test(r==KErrNone); sl@0: delete TheContainer; // deletes the rich text, which deletes the contained pictures. sl@0: */ sl@0: } sl@0: sl@0: sl@0: LOCAL_C void setupCleanup() sl@0: // sl@0: // Initialise the cleanup stack. sl@0: // sl@0: { sl@0: sl@0: TheTrapCleanup=CTrapCleanup::New(); sl@0: TRAPD(r,\ sl@0: {\ sl@0: for (TInt i=KTestCleanupStack;i>0;i--)\ sl@0: CleanupStack::PushL((TAny*)1);\ sl@0: test(r==KErrNone);\ sl@0: CleanupStack::Pop(KTestCleanupStack);\ sl@0: }); sl@0: } sl@0: sl@0: sl@0: LOCAL_C void DeleteDataFile(const TDesC& aFullName) sl@0: { sl@0: RFs fsSession; sl@0: TInt err = fsSession.Connect(); sl@0: if(err == KErrNone) sl@0: { sl@0: TEntry entry; sl@0: if(fsSession.Entry(aFullName, entry) == KErrNone) sl@0: { sl@0: RDebug::Print(_L("Deleting \"%S\" file.\n"), &aFullName); sl@0: err = fsSession.SetAtt(aFullName, 0, KEntryAttReadOnly); sl@0: if(err != KErrNone) sl@0: { sl@0: RDebug::Print(_L("Error %d changing \"%S\" file attributes.\n"), err, &aFullName); sl@0: } sl@0: err = fsSession.Delete(aFullName); sl@0: if(err != KErrNone) sl@0: { sl@0: RDebug::Print(_L("Error %d deleting \"%S\" file.\n"), err, &aFullName); sl@0: } sl@0: } sl@0: fsSession.Close(); sl@0: } sl@0: else sl@0: { sl@0: RDebug::Print(_L("Error %d connecting file session. File: %S.\n"), err, &aFullName); sl@0: } sl@0: } sl@0: sl@0: CT_PICRES::CT_PICRES() sl@0: { sl@0: SetTestStepName(KTestStep_T_PICRES); sl@0: pTestStep = this; sl@0: } sl@0: sl@0: TVerdict CT_PICRES::doTestStepL() sl@0: { sl@0: SetTestStepResult(EFail); sl@0: sl@0: INFO_PRINTF1(_L("Testing Picture Restorer mechanism")); sl@0: __UHEAP_MARK; sl@0: setupCleanup(); sl@0: TRAPD(r,GoL()); sl@0: sl@0: delete TheTrapCleanup; sl@0: sl@0: __UHEAP_MARKEND; sl@0: sl@0: ::DeleteDataFile(KOutputFile); //deletion of data files must be before call to End() - DEF047652 sl@0: ::DeleteDataFile(KOutputFile1); sl@0: sl@0: if (r == KErrNone) sl@0: { sl@0: SetTestStepResult(EPass); sl@0: } sl@0: sl@0: return TestStepResult(); sl@0: }