phonebookengines_old/contactsmodel/tsrc/T_RecoverView.cpp
changeset 40 b46a585f6909
equal deleted inserted replaced
37:fd64c38c277d 40:b46a585f6909
       
     1 // Copyright (c) 2003-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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // RecoverView Test module
       
    15 // 
       
    16 //
       
    17 
       
    18 
       
    19 #include <e32test.h>
       
    20 #include <cntviewbase.h>
       
    21 #include <cntitem.h>
       
    22 #include <cntfldst.h>
       
    23 
       
    24 #include "CContactViewEventQueue.h"
       
    25 
       
    26 _LIT(KTestName, "T_RecoverView");
       
    27 
       
    28 _LIT(KTestDbName, "c:T_RecoverView.cdb");
       
    29 
       
    30 LOCAL_D RTest test(KTestName);
       
    31 
       
    32 
       
    33 class CTestResources : public CBase
       
    34     {
       
    35     public:
       
    36         static CTestResources* NewLC(TBool aDamaged);
       
    37         void ConstructL(TBool aDamaged);
       
    38         ~CTestResources();
       
    39 
       
    40         CContactDatabase* iDb;
       
    41         CContactViewEventQueue* iViewEventQueue;
       
    42         RContactViewSortOrder iViewSortOrder;
       
    43         CContactLocalView* iLocalView;
       
    44     };
       
    45 
       
    46 /**
       
    47  * Creates new test resources object.
       
    48  *
       
    49  * @param aDamaged  if true creates the database as initially damaged.
       
    50  */
       
    51 CTestResources* CTestResources::NewLC(TBool aDamaged)
       
    52     {
       
    53     CTestResources* self = new(ELeave) CTestResources;
       
    54     CleanupStack::PushL(self);
       
    55     self->ConstructL(aDamaged);
       
    56     return self;
       
    57     }
       
    58 
       
    59 /**
       
    60  * Creates a new, nondamaged DB.
       
    61  */
       
    62 void CTestResources::ConstructL(TBool aDamaged)
       
    63     {
       
    64     if (aDamaged)
       
    65         {
       
    66         // Create an empty DB...
       
    67         CContactDatabase* db = CContactDatabase::ReplaceL(KTestDbName);
       
    68         CleanupStack::PushL(db);
       
    69         // Damage the DB...
       
    70         db->DamageDatabaseL(0x666);
       
    71         // Close the DB.
       
    72         CleanupStack::PopAndDestroy(db);
       
    73         // Open the damaged database. CContactDatabase::OpenL() does recovery
       
    74         // by calling RecoverL().
       
    75 		iDb = CContactDatabase::OpenL(KTestDbName);
       
    76         }
       
    77     else
       
    78         {
       
    79         // Just create an empty DB
       
    80         iDb = CContactDatabase::ReplaceL(KTestDbName);
       
    81         }
       
    82 
       
    83     iViewEventQueue = CContactViewEventQueue::NewL();
       
    84 
       
    85     iViewSortOrder.AppendL(KUidContactFieldFamilyName);
       
    86     iViewSortOrder.AppendL(KUidContactFieldGivenName);
       
    87     iViewSortOrder.AppendL(KUidContactFieldCompanyName);
       
    88     
       
    89     iLocalView = CContactLocalView::NewL
       
    90         (*iViewEventQueue, *iDb, iViewSortOrder, EContactsOnly);
       
    91     }
       
    92 
       
    93 CTestResources::~CTestResources()
       
    94     {
       
    95     if (iLocalView) iLocalView->Close(*iViewEventQueue);
       
    96     iViewSortOrder.Close();
       
    97     delete iViewEventQueue;
       
    98     delete iDb;
       
    99     TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KTestDbName));
       
   100     }
       
   101 
       
   102 #if defined(_DEBUG)
       
   103 
       
   104 LOCAL_C void TestRecoverWhenViewReadyL()
       
   105     {
       
   106 	test.Next(_L("Recover database when view is ready"));
       
   107 
       
   108     CTestResources* res = CTestResources::NewLC(EFalse);
       
   109 	
       
   110 	//Creating a contact to test recovery
       
   111 	_LIT(KPhoneNumLabel,"Phone Number");
       
   112 	_LIT(KPhoneNum,		"+447700900012");
       
   113 
       
   114 	CContactCard* contact		= CContactCard::NewLC();
       
   115 	CContactItemField* field	= CContactItemField::NewLC(KStorageTypeText,KUidContactFieldPhoneNumber);
       
   116 	
       
   117 	field->SetLabelL(KPhoneNumLabel());
       
   118 	field->TextStorage()->SetTextL(KPhoneNum());
       
   119 	
       
   120 	contact->AddFieldL(*field);
       
   121 	
       
   122 	TContactItemId aContactId = res->iDb->AddNewContactL(*contact);
       
   123 	
       
   124 	CleanupStack::Pop(field);
       
   125 
       
   126     // Wait 5 seconds for the view to get ready
       
   127     TContactViewEvent event;
       
   128     test(res->iViewEventQueue->ListenForEvent(5,event));
       
   129     test(event.iEventType == TContactViewEvent::EReady);
       
   130 
       
   131     // Damage the DB
       
   132 	test.Next(_L("Damage the open database"));
       
   133 
       
   134     res->iDb->DamageDatabaseL(0x666);
       
   135     test(res->iDb->IsDamaged());
       
   136    
       
   137     // Damaging should not send view events
       
   138     test(!res->iViewEventQueue->ListenForEvent(5,event));
       
   139 
       
   140 	//Attempt to read contact from the damaged database - this should fail with KErrNotReady
       
   141 	test.Next(_L("Attempting to read from damaged database"));
       
   142 
       
   143 	CContactItem* testCard = NULL;
       
   144 	TRAPD(ret,testCard = res->iDb->ReadContactLC(aContactId));
       
   145 	test(ret == KErrNotReady);
       
   146 		
       
   147 	// Recover the DB
       
   148 	test.Next(_L("Recover the damaged database"));
       
   149 
       
   150     res->iDb->RecoverL();
       
   151 		
       
   152     // View should be unavailable here
       
   153     test(res->iViewEventQueue->ListenForEvent(5,event));
       
   154     test(event.iEventType == TContactViewEvent::EUnavailable);
       
   155 
       
   156 	//View should now be ready for use again
       
   157 	test(res->iViewEventQueue->ListenForEvent(5,event));
       
   158 	test(event.iEventType == TContactViewEvent::EReady);
       
   159 
       
   160 	//Line below included to fix ARMV5 minor build warnings.
       
   161 	testCard = res->iDb->ReadContactLC(aContactId);
       
   162 	test(testCard != NULL);
       
   163 	
       
   164 	CleanupStack::PopAndDestroy(testCard);
       
   165 	CleanupStack::PopAndDestroy(contact);
       
   166     CleanupStack::PopAndDestroy(res);
       
   167     }
       
   168 
       
   169 LOCAL_C void TestOpenDamagedDbL()
       
   170     {
       
   171     test.Next(_L("Open damaged database"));
       
   172 
       
   173     CTestResources* res = CTestResources::NewLC(ETrue);
       
   174 
       
   175     // Wait 10 seconds for the view to get ready
       
   176     TContactViewEvent event;
       
   177     test(res->iViewEventQueue->ListenForEvent(10,event));
       
   178     test(event.iEventType == TContactViewEvent::EReady);
       
   179     CleanupStack::PopAndDestroy(res);
       
   180     }
       
   181 
       
   182 LOCAL_C void TestOpenAndCloseTablesL()
       
   183     {
       
   184     test.Next(_L("Open and close tables"));
       
   185 
       
   186     CTestResources* res = CTestResources::NewLC(EFalse);
       
   187 
       
   188     // Wait 10 seconds for the view to get ready
       
   189     TContactViewEvent event;
       
   190     test(res->iViewEventQueue->ListenForEvent(5,event));
       
   191     test(event.iEventType == TContactViewEvent::EReady);
       
   192 
       
   193     // Close tables
       
   194     res->iDb->CloseTables();
       
   195    
       
   196     // View should get unavailable
       
   197     test(res->iViewEventQueue->ListenForEvent(5,event));
       
   198     test(event.iEventType == TContactViewEvent::EUnavailable);
       
   199 
       
   200     // Reopen tables
       
   201     res->iDb->OpenTablesL();
       
   202 
       
   203     // Wait 10 seconds for the view to get ready
       
   204     test(res->iViewEventQueue->ListenForEvent(10,event));
       
   205     test(event.iEventType == TContactViewEvent::EReady);
       
   206     CleanupStack::PopAndDestroy(res);
       
   207     }
       
   208 
       
   209 #endif
       
   210 
       
   211 /**
       
   212 
       
   213 @SYMTestCaseID     PIM-T-RECOVERVIEW-0001
       
   214 
       
   215 */
       
   216 
       
   217 LOCAL_C void DoTestsL()
       
   218     {
       
   219 	test.Start(_L("@SYMTESTCaseID:PIM-T-RECOVERVIEW-0001 T_RecoverView"));
       
   220 
       
   221 
       
   222 #if defined(_DEBUG)
       
   223 	TestRecoverWhenViewReadyL();
       
   224 	TestOpenDamagedDbL();
       
   225 	TestOpenAndCloseTablesL();
       
   226 #else
       
   227 	test.Printf(_L("T_RecoverView only perform functionality testing in UDEB build"));
       
   228 
       
   229 	// reference CntModel DLL to remove linker warning
       
   230     TRAP_IGNORE(CContactDatabase::DefaultContactDatabaseExistsL());
       
   231 #endif
       
   232 
       
   233     test.End();
       
   234 	test.Close();
       
   235     }
       
   236 
       
   237 GLDEF_C TInt E32Main()
       
   238 	{
       
   239 	__UHEAP_MARK;
       
   240     // Init
       
   241     CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   242     if (!cleanupStack)
       
   243         {
       
   244         return KErrNoMemory;
       
   245         }
       
   246 
       
   247     CActiveScheduler* activeScheduler = new CActiveScheduler;
       
   248     if (!activeScheduler)
       
   249         {
       
   250         return KErrNoMemory;
       
   251         }
       
   252     CActiveScheduler::Install(activeScheduler);
       
   253 
       
   254     // Run the tests
       
   255     TRAPD(err, DoTestsL());
       
   256 	User::LeaveIfError(err);
       
   257 
       
   258     // Cleanup
       
   259     delete activeScheduler;
       
   260     delete cleanupStack;
       
   261 	__UHEAP_MARKEND;
       
   262 	return err;
       
   263     }