phonebookengines_old/contactsmodel/tsrc/T_viewcontactid.cpp
changeset 40 b46a585f6909
equal deleted inserted replaced
37:fd64c38c277d 40:b46a585f6909
       
     1 // Copyright (c) 2001-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 <cntdef.h>
       
    18 #include <cntdb.h>
       
    19 #include <cntitem.h>
       
    20 #include <cntfield.h>
       
    21 #include <cntfldst.h>
       
    22 #include <cntviewbase.h>
       
    23 #include <cntview.h>
       
    24 
       
    25 #include <coreappstest/testserver.h>
       
    26 
       
    27 
       
    28 #include "CContactViewEventQueue.h"
       
    29 #include "cfindtext.h"
       
    30 #include "t_utils2.h"
       
    31 
       
    32 //
       
    33 // Uncomment the following macro to run the large view creation test.
       
    34 // This takes a long time to run. Also, it requires the database file
       
    35 // SQLite__T_ViewContactId_10000_Contacts.cdb. This is *NOT* listed in
       
    36 // the bld.inf file and so is not available unless manually moved into
       
    37 // the emulator's z:\core-apps\app-engines\cntmodel\databases folder
       
    38 // in the epoc32 tree.
       
    39 // 
       
    40 // Also, this test cannot easily be run on hardware as the db file is 
       
    41 // too large to fit on the ROM. It is not included in the IBY file.
       
    42 //
       
    43 //
       
    44 //#define __DO_CREATE_LARGE_VIEW_TEST__
       
    45 //
       
    46 //
       
    47 
       
    48 _LIT(KTestName, "T_ViewContactId");
       
    49 
       
    50 _LIT(KTestDbName, "c:T_ViewContactId.cdb");
       
    51 
       
    52 const TInt KOneHundredContacts  =   100;
       
    53 #if (defined __SYMBIAN_CNTMODEL_USE_SQLITE__ && defined __DO_CREATE_LARGE_VIEW_TEST__ )
       
    54 const TInt KTenThousandContacts = 10000;
       
    55 #endif
       
    56 
       
    57 LOCAL_D RTest test(KTestName);
       
    58 
       
    59 
       
    60 class CTestResources : public CBase
       
    61     {
       
    62 public:
       
    63 	static CTestResources* NewLC(TInt aNumContactsToCreate);
       
    64 	static CTestResources* NewLC(const TDesC& aDatabaseName);
       
    65 	~CTestResources();
       
    66 	void GetLocalContactIdsLC(const CArrayFixFlat<TInt>& aIndexes, TInt aExpectedCount);
       
    67 	void GetRemoteContactIdsLC(const CArrayFixFlat<TInt>& aIndexes, TInt aExpectedCount);
       
    68 	inline CContactLocalView* LocalView() { return iLocalView; }
       
    69 	inline CContactRemoteView* RemoteView() { return iRemoteView; }
       
    70 	void CreateViewL();
       
    71 private:
       
    72 	void ConstructL(TInt aNumContactsToCreate);
       
    73 	void ConstructL(const TDesC& aDatabaseName);
       
    74 	void CreateTestContactsL(TInt aNumContactsToCreate);
       
    75 private:
       
    76 	CContactDatabase* iDb;
       
    77 	CContactViewEventQueue* iViewEventQueue;
       
    78 	RContactViewSortOrder iViewSortOrder;
       
    79 	CContactLocalView* iLocalView;
       
    80 	CContactRemoteView* iRemoteView;
       
    81 	};
       
    82 
       
    83 CTestResources* CTestResources::NewLC(TInt aNumContactsToCreate)
       
    84     {
       
    85     CTestResources* self = new(ELeave) CTestResources;
       
    86     CleanupStack::PushL(self);
       
    87     self->ConstructL(aNumContactsToCreate);
       
    88     return self;
       
    89     }
       
    90 
       
    91 CTestResources* CTestResources::NewLC(const TDesC& aDatabaseName)
       
    92     {
       
    93     CTestResources* self = new(ELeave) CTestResources;
       
    94     CleanupStack::PushL(self);
       
    95     self->ConstructL(aDatabaseName);
       
    96     return self;
       
    97     }
       
    98 
       
    99 void CTestResources::ConstructL(TInt aNumContactsToCreate)
       
   100     {
       
   101     iDb = CContactDatabase::ReplaceL(KTestDbName);
       
   102 	CreateTestContactsL(aNumContactsToCreate);
       
   103     }
       
   104 
       
   105 void CTestResources::ConstructL(const TDesC& aDatabaseName)
       
   106     {
       
   107     iDb = CContactDatabase::OpenL(aDatabaseName);
       
   108     TInt count(iDb->CountL());
       
   109     }
       
   110 
       
   111 void CTestResources::CreateViewL()
       
   112 	{
       
   113 	iViewEventQueue = CContactViewEventQueue::NewL(NULL,4000);
       
   114 
       
   115     iViewSortOrder.AppendL(KUidContactFieldFamilyName);
       
   116     iViewSortOrder.AppendL(KUidContactFieldGivenName);
       
   117     iViewSortOrder.AppendL(KUidContactFieldCompanyName);
       
   118     
       
   119     const TContactViewPreferences prefs = static_cast<TContactViewPreferences>(EContactsOnly);
       
   120     iLocalView = CContactLocalView::NewL(*iViewEventQueue, *iDb, iViewSortOrder, prefs);
       
   121 
       
   122 #ifndef __SYMBIAN_CNTMODEL_USE_SQLITE__
       
   123 	const TInt KTimeOutInSeconds(10);
       
   124 #else
       
   125 	const TInt KTimeOutInSeconds(420);  // 7 minutes.
       
   126 #endif
       
   127 
       
   128    // Wait for view to get ready
       
   129     TContactViewEvent event;
       
   130     __ASSERT_ALWAYS(iViewEventQueue->ListenForEvent(KTimeOutInSeconds, event), User::Invariant());
       
   131     __ASSERT_ALWAYS(event.iEventType == TContactViewEvent::EReady, User::Invariant());
       
   132 
       
   133 	iViewEventQueue->Flush();
       
   134 	iRemoteView = CContactRemoteView::NewL(*iViewEventQueue, *iDb, iViewSortOrder, prefs);
       
   135 	__ASSERT_ALWAYS(iViewEventQueue->ListenForEvent(KTimeOutInSeconds, event), User::Invariant());
       
   136 	__ASSERT_ALWAYS(event.iEventType == TContactViewEvent::EReady, User::Invariant());
       
   137     }
       
   138 
       
   139 void CTestResources::CreateTestContactsL(TInt aNumContactsToCreate)
       
   140 	{
       
   141 	CRandomContactGenerator* generator = CRandomContactGenerator::NewL();
       
   142 	CleanupStack::PushL(generator);
       
   143 	generator->SetDbL(*iDb);
       
   144 	for (TInt i = 0; i < aNumContactsToCreate; ++i)
       
   145 		{
       
   146 		generator->AddTypicalRandomContactL();
       
   147 		}
       
   148 	iDb->CompactL();
       
   149 	CleanupStack::PopAndDestroy(generator);
       
   150 
       
   151 	}
       
   152 
       
   153 CTestResources::~CTestResources()
       
   154     {
       
   155 	if (iRemoteView) iRemoteView->Close(*iViewEventQueue);
       
   156     if (iLocalView) iLocalView->Close(*iViewEventQueue);
       
   157     iViewSortOrder.Close();
       
   158     delete iViewEventQueue;
       
   159     delete iDb;
       
   160 	TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KTestDbName));
       
   161     }
       
   162 
       
   163 void CTestResources::GetLocalContactIdsLC(const CArrayFixFlat<TInt>& aIndexes, TInt aExpectedCount)
       
   164 	{
       
   165 	CContactIdArray* array = CContactIdArray::NewLC();
       
   166 	//Pack the parameters to GetContactIdsL in the class TVirtualFunction1Params 
       
   167 	//and pass it to CContactViewBase_Reserved_1.
       
   168 	CContactViewBase::TVirtualFunction1Params structOfParams(&aIndexes,array);
       
   169 	//Make a call to CContactViewBase_Reserved_1() instead of GetContactIdsL().
       
   170 	iLocalView->CContactViewBase_Reserved_1(CContactViewBase::ECContactViewBaseVirtualFunction1, 
       
   171 		&structOfParams);
       
   172 
       
   173 	__ASSERT_ALWAYS(array->Count()==aExpectedCount, User::Invariant());
       
   174 	CleanupStack::PopAndDestroy(array);
       
   175 	}
       
   176 
       
   177 
       
   178 void CTestResources::GetRemoteContactIdsLC(const CArrayFixFlat<TInt>& aIndexes, TInt aExpectedCount)
       
   179 	{
       
   180 	CContactIdArray* array = CContactIdArray::NewLC();
       
   181 	//Pack the parameters to GetContactIdsL in the class TVirtualFunction1Params 
       
   182 	//and pass it to CContactViewBase_Reserved_1.
       
   183 	CContactViewBase::TVirtualFunction1Params structOfParams(&aIndexes,array);
       
   184 	//Make a call to CContactViewBase_Reserved_1() instead of GetContactIdsL().
       
   185 	iRemoteView->CContactViewBase_Reserved_1(CContactViewBase::ECContactViewBaseVirtualFunction1, 
       
   186 		&structOfParams);
       
   187 
       
   188 	__ASSERT_ALWAYS(array->Count()==aExpectedCount, User::Invariant());
       
   189 	CleanupStack::PopAndDestroy(array);
       
   190 	}
       
   191 
       
   192 
       
   193 LOCAL_C void TestGetContactIdsL()
       
   194 	{
       
   195 	CTestResources* res = CTestResources::NewLC(KOneHundredContacts);
       
   196 	res->CreateViewL();
       
   197 
       
   198 	//Empty index array
       
   199 	CArrayFixFlat<TInt>* emptyIndexes = new(ELeave) CArrayFixFlat<TInt>(8);	
       
   200 	CleanupStack::PushL(emptyIndexes);
       
   201 	res->GetLocalContactIdsLC(*emptyIndexes,0);
       
   202 	res->GetRemoteContactIdsLC(*emptyIndexes,0);
       
   203 	CleanupStack::PopAndDestroy(emptyIndexes);
       
   204 
       
   205 	//Valid index array
       
   206 	CArrayFixFlat<TInt>* indexes = new(ELeave) CArrayFixFlat<TInt>(8);	
       
   207 	CleanupStack::PushL(indexes);
       
   208 	indexes->AppendL(1);
       
   209 	indexes->AppendL(3);
       
   210 	indexes->AppendL(5);
       
   211 	indexes->AppendL(7);
       
   212 	res->GetLocalContactIdsLC(*indexes,4);
       
   213 	res->GetRemoteContactIdsLC(*indexes,4);
       
   214 	CleanupStack::PopAndDestroy(indexes);
       
   215 	
       
   216 	//Invalid index array
       
   217 	CArrayFixFlat<TInt>* invalid = new(ELeave) CArrayFixFlat<TInt>(8);	
       
   218 	CleanupStack::PushL(indexes);
       
   219 	indexes->AppendL(1003);
       
   220 	indexes->AppendL(666);
       
   221 	res->GetLocalContactIdsLC(*invalid,0);
       
   222 	res->GetRemoteContactIdsLC(*invalid,0);
       
   223 	CleanupStack::PopAndDestroy(invalid);
       
   224 
       
   225 	//Partially valid array
       
   226 	CArrayFixFlat<TInt>* partial = new(ELeave) CArrayFixFlat<TInt>(8);	
       
   227 	CleanupStack::PushL(partial);
       
   228 	partial->AppendL(0);
       
   229 	partial->AppendL(1000);
       
   230 	partial->AppendL(1);
       
   231 	partial->AppendL(666);
       
   232 	res->GetLocalContactIdsLC(*partial,2);
       
   233 	res->GetRemoteContactIdsLC(*partial,2);
       
   234 	CleanupStack::PopAndDestroy(partial);
       
   235 
       
   236 	//Whole View
       
   237 	CArrayFixFlat<TInt>* allIndexes = new(ELeave) CArrayFixFlat<TInt>(8);	
       
   238 	CleanupStack::PushL(allIndexes);
       
   239 	const TInt count = res->LocalView()->CountL();
       
   240 	for (TInt i=0; i<count; ++i)
       
   241 		{
       
   242 		allIndexes->AppendL(i);
       
   243 		}
       
   244 	res->GetLocalContactIdsLC(*allIndexes,count);
       
   245 	res->GetRemoteContactIdsLC(*allIndexes,count);
       
   246 
       
   247 	//Check data returned matches view
       
   248 	CContactIdArray* array = CContactIdArray::NewLC();
       
   249 	//Pack the parameters to GetContactIdsL in the class TVirtualFunction1Params 
       
   250 	//and pass it to CContactViewBase_Reserved_1.
       
   251 	CContactViewBase::TVirtualFunction1Params structOfParams(allIndexes,array);
       
   252 	//Make a call to CContactViewBase_Reserved_1() instead of GetContactIdsL().
       
   253 	res->LocalView()->CContactViewBase_Reserved_1(CContactViewBase::ECContactViewBaseVirtualFunction1, 
       
   254 		&structOfParams);
       
   255 
       
   256 	const TInt arrayCount = array->Count();
       
   257 	__ASSERT_ALWAYS(arrayCount==count, User::Invariant());
       
   258 	
       
   259 	for (TInt j=0; j<arrayCount; ++j)
       
   260 		{
       
   261 		test(res->LocalView()->AtL(j)==(*array)[j]);
       
   262 		}
       
   263 	CleanupStack::PopAndDestroy(array);
       
   264 	CleanupStack::PopAndDestroy(allIndexes);
       
   265 
       
   266 	CleanupStack::PopAndDestroy(res);
       
   267 	}
       
   268 
       
   269 
       
   270 #if (defined __SYMBIAN_CNTMODEL_USE_SQLITE__ && defined __DO_CREATE_LARGE_VIEW_TEST__ )
       
   271 LOCAL_C void TestBigDatabaseL()
       
   272 	{
       
   273 	_LIT(K1000ContactDbSourcePath, 
       
   274 		"z:\\core-apps\\app-engines\\cntmodel\\databases\\SQLite__T_ViewContactId_10000_Contacts.cdb");
       
   275 	_LIT(K1000ContactDbDestinationPath, 
       
   276 		"c:\\private\\10003a73\\SQLite__T_ViewContactId_10000_Contacts.cdb");
       
   277 	_LIT(K1000ContactDbUserFriendlyName, 
       
   278 		"c:T_ViewContactId_10000_Contacts.cdb");
       
   279 
       
   280     RPIMTestServer serv;
       
   281 	CleanupClosePushL(serv);	
       
   282     User::LeaveIfError(serv.Connect());
       
   283     serv.DeleteFileL(K1000ContactDbDestinationPath());
       
   284     serv.CopyFileL(K1000ContactDbSourcePath, K1000ContactDbDestinationPath());
       
   285     CleanupStack::PopAndDestroy(&serv);
       
   286 	
       
   287 	CTestResources* res = CTestResources::NewLC(K1000ContactDbUserFriendlyName());
       
   288 	res->CreateViewL();
       
   289 	const TInt KNumContactsInLocalView = res->LocalView()->CountL();
       
   290 	const TInt KNumContactsInRemoteView = res->RemoteView()->CountL();
       
   291 	test(KNumContactsInLocalView == KTenThousandContacts && 
       
   292 		KNumContactsInRemoteView == KTenThousandContacts);
       
   293 	test.Printf(_L("Number contacts in views -- local: %d; remote: %d.\n"), 
       
   294 		KNumContactsInLocalView, KNumContactsInRemoteView);
       
   295 
       
   296 	// Access the penultimate contact view item
       
   297 	const TInt KPenultimateContactViewIndex(KNumContactsInRemoteView - 2);
       
   298 	const CViewContact& viewContact = res->RemoteView()->ContactAtL(KPenultimateContactViewIndex);
       
   299 	const TInt KFieldCount = viewContact.FieldCount();
       
   300 	for (TInt i = 0; i < KFieldCount; ++i)
       
   301 		{
       
   302 		TPtrC fieldText = viewContact.Field(i);
       
   303 		test.Printf(_L("Remote view contact item %d; field %d of %d: %S\n"), 
       
   304 			KPenultimateContactViewIndex + 1, i + 1, KFieldCount, &fieldText);
       
   305 		}
       
   306 	CleanupStack::PopAndDestroy(res);
       
   307 	}
       
   308 #endif
       
   309 
       
   310 /**
       
   311 
       
   312 @SYMTestCaseID     PIM-T-VIEWCONTACTID-0001
       
   313 
       
   314 */
       
   315 
       
   316 void DoTestsL()
       
   317     {
       
   318 	_LIT(KTestTitle, "@SYMTESTCaseID:PIM-T-VIEWCONTACTID-0001 T_ViewContactId: test contact view item IDs");
       
   319 	test.Start(KTestTitle);
       
   320 
       
   321 	TestGetContactIdsL();
       
   322 
       
   323 #if (defined __SYMBIAN_CNTMODEL_USE_SQLITE__ && defined __DO_CREATE_LARGE_VIEW_TEST__ )
       
   324 	// This will only work after the migration to SQLite and the re-architected views 
       
   325 	// in v9.5 and then only if the __DO_CREATE_LARGE_VIEW_TEST__ macro is turned on.
       
   326 	_LIT(KBigDbTestTitle, "Test creating a view from a big database (10,000 contacts)");
       
   327 	test.Next(_L("Test creating a view from a big database (10,000 contacts)"));
       
   328 
       
   329 	TestBigDatabaseL();
       
   330 #endif
       
   331 
       
   332     test.End();
       
   333     }
       
   334 
       
   335 GLDEF_C TInt E32Main()
       
   336 	{
       
   337     // Init
       
   338 	__UHEAP_MARK;
       
   339     CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   340     if (!cleanupStack)
       
   341         {
       
   342         return KErrNoMemory;
       
   343         }
       
   344 
       
   345     CActiveScheduler* activeScheduler = new CActiveScheduler;
       
   346     if (!activeScheduler)
       
   347         {
       
   348         return KErrNoMemory;
       
   349         }
       
   350     CActiveScheduler::Install(activeScheduler);
       
   351 
       
   352 	//Get rid of the warnings.
       
   353 	//T_utils2 uses efsrv.dll, but we don't use this functions in this test code.
       
   354 	//So we pretend to use efsrv to keep T_utils2.obj and linker happy
       
   355 	RFs fs;
       
   356 	fs.Connect();
       
   357 	fs.Close();
       
   358 
       
   359     // Run the tests
       
   360     TRAPD(err, DoTestsL());
       
   361 
       
   362     // Cleanup
       
   363     delete activeScheduler;
       
   364     delete cleanupStack;
       
   365 	test.Close();
       
   366 	__UHEAP_MARKEND;
       
   367 	return err;
       
   368     }