phonebookengines_old/contactsmodel/tsrc/t_contactsramtest.cpp
changeset 40 b46a585f6909
equal deleted inserted replaced
37:fd64c38c277d 40:b46a585f6909
       
     1 // Copyright (c) 1997-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 //
       
    15 
       
    16 #include <e32test.h>
       
    17 #include <cntdb.h>
       
    18 #include <cntitem.h>
       
    19 #include <cntviewbase.h>
       
    20 #include <cntfldst.h>
       
    21 #include <e32panic.h>
       
    22 
       
    23 _LIT(KTestName, "Ram consumption Test");
       
    24 
       
    25 LOCAL_D RTest test(KTestName);
       
    26 
       
    27 #define DELETE_SET_NULL(x) { delete x; x = NULL; }
       
    28 
       
    29 static const TInt KMaxContacts = 2000;
       
    30 static const TInt KManyFields = 8;
       
    31 static const TInt KAgentId = 2;
       
    32 _LIT(KTime,"22000101:235959.999999");// time to use for datetime storage
       
    33 _LIT(KFieldValue,"Standard length field value");
       
    34 TInt manycontacts = 0;
       
    35 
       
    36 _LIT(KContactsIniFileFullPath, "c:\\private\\10003A73\\Cntmodel.ini");
       
    37 _LIT(KContactsDbFileFullPath, "c:\\private\\10003A73\\Contacts.cdb");
       
    38 _LIT(KContactsBackupRegistrationIniFileFullPath, "c:\\private\\10003A73\\backup_registration.xml");
       
    39 
       
    40 static RFs TheFsSession;
       
    41 
       
    42 class CContactsRamTest : public CActive, public MContactViewObserver
       
    43 	{
       
    44 public:
       
    45 	static CContactsRamTest* NewLC();
       
    46 	~CContactsRamTest();
       
    47 	void SetView(const CContactViewBase *aLocalView);
       
    48 	void RamTestL();
       
    49 	void RamTest2L();
       
    50 	void Activate();
       
    51 	void CloseView();
       
    52 	
       
    53 private:
       
    54 	CContactsRamTest();
       
    55 	void ConstructL();
       
    56 	void AddContactsL(const TInt aNumber);
       
    57 	void SetFieldL(const CContactItemField &aField, const TDesC& aText);
       
    58 	void SetManyFieldsL(const TDesC& aText, const TInt aMany, CContactItemFieldSet &fields);
       
    59 	void PrintHeap();
       
    60 	
       
    61 private: // From CActive.
       
    62 	void RunL();
       
    63 	void DoCancel();
       
    64 	TInt RunError(TInt aError);
       
    65 private: // From MContactViewObserver.
       
    66 	virtual void HandleContactViewEvent(const CContactViewBase& aView, 
       
    67 										const TContactViewEvent& aEvent);
       
    68 
       
    69 private:
       
    70 	CContactDatabase* iContactsDatabase;
       
    71 	CContactRemoteView* iRemoteView;
       
    72 	RContactViewSortOrder iViewSortOrder;
       
    73 	TBool iEnd;
       
    74 	};
       
    75 
       
    76 
       
    77 CContactsRamTest* CContactsRamTest::NewLC()
       
    78 	{
       
    79 	CContactsRamTest* self = new(ELeave) CContactsRamTest();
       
    80 	CleanupStack::PushL(self);
       
    81 	self->ConstructL();
       
    82 	return self;
       
    83 	}
       
    84 	
       
    85 
       
    86 void CContactsRamTest::ConstructL()
       
    87 	{
       
    88 	CActiveScheduler::Add(this);
       
    89 	iViewSortOrder.AppendL(KUidContactFieldGivenName);	
       
    90 	iViewSortOrder.AppendL(KUidContactFieldFamilyName);
       
    91 	}
       
    92 	
       
    93 
       
    94 CContactsRamTest::~CContactsRamTest()
       
    95 	{
       
    96 	CloseView();
       
    97 	iViewSortOrder.Close();
       
    98 	delete iContactsDatabase;
       
    99 	}
       
   100 
       
   101 CContactsRamTest::CContactsRamTest() : CActive(EPriorityStandard)
       
   102 	{
       
   103 	}
       
   104 	
       
   105 	
       
   106 void CContactsRamTest::RunL()
       
   107 	{
       
   108 	if( iEnd )
       
   109 		{
       
   110 		iEnd = EFalse;
       
   111 		RamTest2L();
       
   112 		CActiveScheduler::Stop();
       
   113 		}
       
   114 	else
       
   115 		{
       
   116 		RamTestL();
       
   117 		}
       
   118 	}
       
   119 	
       
   120 void CContactsRamTest::DoCancel()
       
   121 	{
       
   122 	}
       
   123 
       
   124 TInt CContactsRamTest::RunError(TInt aError)
       
   125 	{
       
   126 	test.Printf( _L("ViewObserver:: Error in doTest runL: %d"), aError );
       
   127 	return aError;
       
   128 	}
       
   129 
       
   130 void CContactsRamTest::Activate()
       
   131 	{
       
   132 	TRequestStatus *pS=&iStatus;
       
   133 	User::RequestComplete(pS,KErrNone);
       
   134 	SetActive();
       
   135 	}
       
   136 
       
   137 void CContactsRamTest::RamTestL()
       
   138 	{
       
   139 	test.Next(_L("Initial ram test"));
       
   140 
       
   141 	test.Printf( _L("Ram test for %d contacts\n"), manycontacts);
       
   142 	DELETE_SET_NULL( iContactsDatabase );
       
   143 	test.Printf(_L("Empty Database is opened\n"));
       
   144 	iContactsDatabase = CContactDatabase::ReplaceL();
       
   145 	PrintHeap();
       
   146 	AddContactsL( manycontacts );
       
   147 	test.Printf(_L("Database is opened and contacts added\n"));
       
   148 	PrintHeap();
       
   149 	test.Printf(_L("RemoteView with first/surname order is created\n"));
       
   150 	iRemoteView = CContactRemoteView::NewL(	*this, *iContactsDatabase, iViewSortOrder, EContactsOnly);
       
   151 	PrintHeap();
       
   152 	
       
   153 	}	
       
   154 
       
   155 void CContactsRamTest::RamTest2L()
       
   156 	{
       
   157 	test.Printf(_L("RemoteView is populated\n"));
       
   158 	PrintHeap();
       
   159 	CloseView();
       
   160 	DELETE_SET_NULL( iContactsDatabase );
       
   161 	test.Printf(_L("Full Database is re-opened\n"));
       
   162 	iContactsDatabase = CContactDatabase::OpenL();
       
   163 	PrintHeap();
       
   164 	DELETE_SET_NULL( iContactsDatabase );
       
   165 	}	
       
   166 	
       
   167 void CContactsRamTest::HandleContactViewEvent(	const CContactViewBase& aView, 
       
   168 											const TContactViewEvent& aEvent)
       
   169 	{
       
   170 	if (&aView==iRemoteView)
       
   171 		{
       
   172 		switch(aEvent.iEventType)
       
   173 			{
       
   174 			case TContactViewEvent::EReady:
       
   175 				{				
       
   176 				iEnd = ETrue;
       
   177 				Activate();
       
   178 				break;
       
   179 				}
       
   180 			}
       
   181 		}
       
   182 	}
       
   183 	
       
   184 void CContactsRamTest::AddContactsL(const TInt aNumber)
       
   185 /**
       
   186 Add number of contacts to db, using the system template.
       
   187 @return None
       
   188 @param aNumber number of contacts to be added
       
   189 @pre None
       
   190 @post database now contains aNumber of empty contacts
       
   191 */
       
   192 	{
       
   193 	
       
   194 
       
   195 	//viewdef that loads all fields 	
       
   196 	CContactItemViewDef *viewAll = CContactItemViewDef::NewLC(CContactItemViewDef::EIncludeFields,CContactItemViewDef::EIncludeHiddenFields);
       
   197 	viewAll->AddL(KUidContactFieldMatchAll);
       
   198 	CContactTemplate *ttemplate = static_cast< CContactTemplate* >(
       
   199 	iContactsDatabase->ReadContactLC( iContactsDatabase->TemplateId(), *viewAll ) );
       
   200 
       
   201 	CContactCard* contact = CContactCard::NewLC(ttemplate);
       
   202 	SetManyFieldsL(KFieldValue, KManyFields, contact->CardFields());
       
   203 	for(TInt k = 0; k<aNumber; k++)
       
   204 		{		
       
   205 		iContactsDatabase->AddNewContactL(*contact);
       
   206 		}	
       
   207 	CleanupStack::PopAndDestroy(contact);
       
   208 	CleanupStack::PopAndDestroy(ttemplate);
       
   209 	CleanupStack::PopAndDestroy(viewAll);
       
   210 	}
       
   211 
       
   212 /*sets a specific contact field to a value as determined by its type.
       
   213 field at aPos in aFields is set to aText*/
       
   214 void CContactsRamTest::SetFieldL(const CContactItemField &aField, const TDesC& aText)
       
   215 	{
       
   216 	if( aField.IsTemplateLabelField() )
       
   217 		{
       
   218 		return;
       
   219 		}
       
   220 		
       
   221 	if( 0 == aText.Size() )
       
   222 		{
       
   223 		return;
       
   224 		}
       
   225 	
       
   226 	switch(aField.StorageType())
       
   227 		{
       
   228 		case KStorageTypeText:
       
   229 			aField.TextStorage()->SetTextL(aText);
       
   230 		break;
       
   231 		case KStorageTypeStore:
       
   232 			{
       
   233 			HBufC8 *buf = HBufC8::NewLC(aText.Size());
       
   234 			TPtr8 tptr8 = buf->Des();
       
   235 			tptr8.Copy(aText);
       
   236 			aField.StoreStorage()->SetThingL(*buf);
       
   237 			CleanupStack::PopAndDestroy(buf);
       
   238 			}
       
   239 		break;
       
   240 		case KStorageTypeContactItemId:
       
   241 			{
       
   242 			aField.AgentStorage()->SetAgentId( KAgentId );
       
   243 			}
       
   244 		break;
       
   245 		case KStorageTypeDateTime:
       
   246 			{
       
   247 			if( aText.Size() > 0 ) 
       
   248 				{
       
   249 				aField.DateTimeStorage()->SetTime( TTime(KTime) );
       
   250 				}
       
   251 			else
       
   252 				{
       
   253 				aField.DateTimeStorage()->SetTime( TTime(0) );
       
   254 				}
       
   255 			}
       
   256 		break;
       
   257 		default:
       
   258 			User::Panic(aText,EInvariantFalse);
       
   259 		break;
       
   260 		}
       
   261 	}
       
   262 
       
   263 /**
       
   264 sets aMany fields of a currently read / open item to aText
       
   265 */
       
   266 void CContactsRamTest::SetManyFieldsL(const TDesC& aText, const TInt aMany, CContactItemFieldSet &fields)
       
   267 	{
       
   268 	TInt count = fields.Count();
       
   269 	if( count > aMany )
       
   270 		{
       
   271 		count = aMany;
       
   272 		}
       
   273 	TInt i = 0;
       
   274 	for(; i < count; ++i)
       
   275 		{
       
   276 		SetFieldL(fields[i],aText);
       
   277 		}
       
   278 	}
       
   279 	
       
   280 void CContactsRamTest::PrintHeap()
       
   281 	{
       
   282 	TInt totalAllocSize(0);
       
   283 	User::Heap().AllocSize(totalAllocSize);
       
   284 	
       
   285 	test.Printf(_L("Client heap size: %d bytes\n"), totalAllocSize);
       
   286 	iContactsDatabase->CntServerResourceCount();
       
   287 	}
       
   288 
       
   289 void CContactsRamTest::CloseView()
       
   290 	{
       
   291 	if(iRemoteView)
       
   292 		{
       
   293 		iRemoteView->Close(*this);
       
   294 		iRemoteView = NULL;
       
   295 		}
       
   296 	}
       
   297 
       
   298 void CleanupFiles()
       
   299 	{
       
   300 	TheFsSession.Delete(KContactsIniFileFullPath);
       
   301 	TheFsSession.Delete(KContactsDbFileFullPath);
       
   302 	TheFsSession.Delete(KContactsBackupRegistrationIniFileFullPath);
       
   303 	}
       
   304 
       
   305 /**
       
   306 
       
   307 @SYMTestCaseID     PIM-T-CONTACTSRAMTEST-0001
       
   308 
       
   309 */
       
   310 	
       
   311 void DoTestsL()
       
   312     {
       
   313     test.SetLogged( ETrue );
       
   314 	test.Start(_L("@SYMTESTCaseID:PIM-T-CONTACTSRAMTEST-0001 Test Ram usage for contacts model"));
       
   315 
       
   316 	
       
   317 	test.Printf(_L("Uninitilized Client Heap count: %d bytes, Heap Size: %d bytes\n"), User::Heap().Count(), User::Heap().Size());
       
   318 
       
   319 	TInt error =TheFsSession.Connect();
       
   320 	User::LeaveIfError(error);
       
   321 
       
   322 	CleanupFiles();
       
   323 	for ( manycontacts = 500; manycontacts <= KMaxContacts; manycontacts+=500 )
       
   324 		{
       
   325 		CContactsRamTest* res = CContactsRamTest::NewLC();	
       
   326 		res->Activate();
       
   327 		CActiveScheduler::Start();
       
   328 		CleanupStack::PopAndDestroy(res);
       
   329 		}
       
   330     test.End();
       
   331 	test.Close();
       
   332 	CleanupFiles();
       
   333 	//cleanup
       
   334 	CContactDatabase::DeleteDefaultFileL();
       
   335     }
       
   336 
       
   337 GLDEF_C TInt E32Main()
       
   338 	{
       
   339 	__UHEAP_MARK;
       
   340     CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   341     if (!cleanupStack)
       
   342         {
       
   343         return KErrNoMemory;
       
   344         }
       
   345 
       
   346     CActiveScheduler* activeScheduler = new CActiveScheduler;
       
   347     if (!activeScheduler)
       
   348         {
       
   349         return KErrNoMemory;
       
   350         }
       
   351     CActiveScheduler::Install(activeScheduler);
       
   352 
       
   353     // Run the tests
       
   354     TRAPD(err, DoTestsL());
       
   355 
       
   356     // Cleanup
       
   357     delete activeScheduler;
       
   358     delete cleanupStack;
       
   359 	__UHEAP_MARKEND;
       
   360 	return err;
       
   361     }
       
   362