phonebookengines_old/contactsmodel/tsrc/t_GroupViewEvents.cpp
changeset 40 b46a585f6909
equal deleted inserted replaced
37:fd64c38c277d 40:b46a585f6909
       
     1 // Copyright (c) 2000-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 <e32std.h>
       
    17 #include <e32test.h>
       
    18 #include <cntdb.h>
       
    19 #include <cntitem.h>
       
    20 #include <cntfldst.h>
       
    21 #include "t_utils2.h"
       
    22 #include "t_GroupViewEvents.h"
       
    23 
       
    24 // Test Macro
       
    25 LOCAL_D RTest test(_L("t_GroupViewEvents"));
       
    26 
       
    27 //
       
    28 // Constants.
       
    29 //
       
    30 _LIT(KTestName,    "@SYMTESTCaseID:PIM-T-GROUPVIEWEVENTS-0001 t_GroupViewEvents" );
       
    31 _LIT(KLogFileName, "t_GroupViewEvents.log" );
       
    32 _LIT(KDbFileName,  "c:t_GroupViewEvents.cdb" );
       
    33 _LIT(KTextDefSeparator, "");
       
    34 
       
    35 const TInt KNumContacts = 100;
       
    36 const TInt KNumContactsInGroupOne = 11;
       
    37 _LIT(KGroupOneName,"GroupOne");
       
    38 
       
    39 //
       
    40 // CTestConductor.
       
    41 //
       
    42 
       
    43 CTestConductor* CTestConductor::NewL()
       
    44 	{
       
    45 	CTestConductor* self=new(ELeave) CTestConductor();
       
    46 	CleanupStack::PushL(self);
       
    47 	self->ConstructL();
       
    48 	self->RunTestsL();
       
    49 	CleanupStack::Pop();
       
    50 	return self;
       
    51 	}
       
    52 
       
    53 CTestConductor::~CTestConductor()
       
    54 	{
       
    55 	delete iLog;
       
    56 	delete iDb;
       
    57 	delete iRandomGenerator;
       
    58     TRAP_IGNORE(CContactDatabase::DeleteDatabaseL( KDbFileName ) );
       
    59 	iFs.Close();
       
    60 	}
       
    61 
       
    62 CTestConductor::CTestConductor()
       
    63 	{
       
    64 	}
       
    65 
       
    66 void CTestConductor::ConstructL()
       
    67 	{
       
    68 	User::LeaveIfError( iFs.Connect() );
       
    69 	iLog = CLog::NewL( test, KLogFileName );
       
    70 	iDb = CContactDatabase::ReplaceL( KDbFileName );
       
    71 	iRandomGenerator = CRandomContactGenerator::NewL();
       
    72 	iRandomGenerator->SetDbL( *iDb );
       
    73 	AddContactsL();
       
    74 	}
       
    75 
       
    76 TContactItemId CTestConductor::AddRandomContactL()
       
    77     {
       
    78 	TContactItemId id = iRandomGenerator->AddTypicalRandomContactL();
       
    79 
       
    80 	iTotalContacts++;
       
    81     return id;
       
    82     }
       
    83 
       
    84 void CTestConductor::AddContactsL()
       
    85 	{
       
    86 	iTotalContacts=0;
       
    87 	TInt ii;
       
    88 	for (ii=0; ii < KNumContacts; ii++)
       
    89 		{
       
    90 		iRandomGenerator->AddTypicalRandomContactL();
       
    91 		iTotalContacts++;
       
    92 		test.Printf(_L("Adding %d "),ii);
       
    93 		}
       
    94 	}
       
    95 
       
    96 void CTestConductor::RunTestsL()
       
    97 	{
       
    98 	CGroupViewTester* tester=CGroupViewTester::NewL(*iLog,this,*iDb);
       
    99 	CleanupStack::PushL(tester);
       
   100 	CActiveScheduler::Start();
       
   101 	CleanupStack::Pop(tester); 
       
   102 	// error from CGroupViewTester?
       
   103 	User::LeaveIfError(iTestError);
       
   104 	}
       
   105 
       
   106 void CTestConductor::SetTestError(TInt aTestError)
       
   107 	{
       
   108 	iTestError = aTestError;
       
   109 	}
       
   110 
       
   111 
       
   112 //
       
   113 // CGroupViewTester.
       
   114 //
       
   115 
       
   116 CGroupViewTester* CGroupViewTester::NewL(CLog& aLog,CTestConductor* aTestConductor,CContactDatabase& aDb)
       
   117 	{
       
   118 	CGroupViewTester* self=new(ELeave) CGroupViewTester(aLog,aTestConductor,aDb);
       
   119 	CleanupStack::PushL(self);
       
   120 	self->ConstructL();
       
   121 	CleanupStack::Pop();
       
   122 	return self;
       
   123 	}
       
   124 
       
   125 CGroupViewTester::~CGroupViewTester()
       
   126 	{
       
   127     iGroupViewTwo->Close(*this);
       
   128 	iGroupViewOne->Close(*this);
       
   129 	iLocalView->Close(*this);
       
   130 	iSortOrder_1.Close();
       
   131 	delete iTextDef;
       
   132 	delete iIdsInGroupViewOne;
       
   133 	delete iGroupOne;
       
   134 	}
       
   135 
       
   136 CGroupViewTester::CGroupViewTester( CLog& aLog ,CTestConductor* aTestConductor,
       
   137     CContactDatabase& aDb) :
       
   138 	CActive(EPriorityStandard), 
       
   139     iLog( aLog ), 
       
   140     iTestConductor( aTestConductor ),
       
   141     iDb( aDb ), 
       
   142     iCurrentTest( -1 ),
       
   143 	iLastContactID(-1),
       
   144 	iSecondLastContactID(-1),
       
   145 	iThirdLastContactID(-1)     
       
   146 	{
       
   147 	CActiveScheduler::Add( this );
       
   148 	}
       
   149 
       
   150 void CGroupViewTester::ConstructL()
       
   151 	{
       
   152 	iSortOrder_1.AppendL(KUidContactFieldGivenName);
       
   153 	iSortOrder_1.AppendL(KUidContactFieldFamilyName);
       
   154 	iSortOrder_1.AppendL(KUidContactFieldCompanyName);
       
   155 
       
   156 	iTextDef=CContactTextDef::NewL();
       
   157 	iTextDef->AppendL(TContactTextDefItem(KUidContactFieldGivenName,KTextDefSeparator));
       
   158 	iTextDef->AppendL(TContactTextDefItem(KUidContactFieldFamilyName,KTextDefSeparator));
       
   159 	iTextDef->AppendL(TContactTextDefItem(KUidContactFieldCompanyName,KTextDefSeparator));
       
   160 	CreateGroupTestDataL();
       
   161 	NextTest();
       
   162 	}
       
   163 
       
   164 void CGroupViewTester::CreateGroupTestDataL()
       
   165 	{
       
   166 	iGroupOne = static_cast<CContactGroup*>( iDb.CreateContactGroupL( 
       
   167         KGroupOneName ) );
       
   168 
       
   169 	for (TInt ii=1;ii<=KNumContactsInGroupOne;++ii)
       
   170 		{
       
   171 		iDb.AddContactToGroupL(ii,iGroupOne->Id());
       
   172 		}
       
   173 
       
   174 	iGroupOneId = iGroupOne->Id();
       
   175 	delete iGroupOne;
       
   176 	iGroupOne = NULL;
       
   177 	iGroupOne = STATIC_CAST(CContactGroup*,iDb.ReadContactL(iGroupOneId));
       
   178 	iIdsInGroupViewOne = iGroupOne->ItemsContainedLC();
       
   179 	CleanupStack::Pop( iIdsInGroupViewOne );
       
   180 	}
       
   181 
       
   182 void CGroupViewTester::RunL()
       
   183 	{
       
   184 	switch (iCurrentTest)
       
   185 		{
       
   186 		case ECreateLocalView:
       
   187 			iLog.LogLine(_L("Creating local view"));
       
   188 			iLocalView = CContactLocalView::NewL( *this, iDb, iSortOrder_1, 
       
   189                 EContactAndGroups );
       
   190 			break;
       
   191 
       
   192 		case ECreateGroupOneView:
       
   193 			iLog.LogLine(_L("Creating group view one."));
       
   194             
       
   195             // Create a version 2 object.
       
   196 			iGroupViewOne = CContactGroupView::NewL( *iLocalView, iDb, *this,
       
   197                 iGroupOne->Id(), CContactGroupView::EShowContactsInGroup );
       
   198 			break;
       
   199 
       
   200         case ECreateGroupTwoView:
       
   201             {
       
   202 			iLog.LogLine(_L("Creating group view two."));
       
   203 			iGroupViewTwo = CContactGroupView::NewL( *iLocalView, iDb, *this,
       
   204                 KNullContactId, CContactGroupView::EShowContactsNotInAnyGroup );
       
   205             }
       
   206             break;
       
   207 
       
   208         case EDeleteItemsFromView:
       
   209             {
       
   210             iLog.LogLine(_L("Deleting contacts from group one."));
       
   211 
       
   212             TInt id1 = 1;
       
   213             TInt index = iGroupViewOne->FindL( id1 );
       
   214             iLog.LogLine( _L("Deleting contact id %d. index = %d"), id1, index );
       
   215 
       
   216             TInt id2 = 2;
       
   217             index = iGroupViewOne->FindL( 2 );
       
   218             iLog.LogLine( _L("Deleting contact id %d. index = %d"), id2, index );
       
   219 
       
   220             // Delete two contacts using a bulk delete.
       
   221             TInt numContactsInGroup = iGroupViewOne->CountL();
       
   222             CContactIdArray* itemsToDelete = CContactIdArray::NewL();
       
   223             CleanupStack::PushL( itemsToDelete );
       
   224             itemsToDelete->AddL( id1 );
       
   225             itemsToDelete->AddL( id2 );
       
   226             iDb.DeleteContactsL( *itemsToDelete );
       
   227             CleanupStack::PopAndDestroy( itemsToDelete );
       
   228 
       
   229             break;
       
   230             }
       
   231 
       
   232         case EAddItemToGroup:
       
   233             {
       
   234             iLog.LogLine(_L("Adding contact to database and group one."));
       
   235             // Create a new item and add it to group one.
       
   236             iIdAdded = iTestConductor->AddRandomContactL();
       
   237             iDb.AddContactToGroupL( iIdAdded, iGroupOne->Id() );
       
   238             break;
       
   239             }
       
   240 
       
   241 		case ENumTests:
       
   242             // Tests complete.
       
   243             iLog.LogLine(_L("All tests complete."));
       
   244 			CActiveScheduler::Stop();
       
   245 			delete this;
       
   246 			break;
       
   247 
       
   248 		default:
       
   249 			ASSERT( EFalse );
       
   250 			break;
       
   251 		}
       
   252 	}
       
   253 
       
   254 TInt CGroupViewTester::RunError(TInt aError)
       
   255 	{
       
   256 	// propagate error
       
   257 	iTestConductor->SetTestError(aError);
       
   258 
       
   259 	switch (iCurrentTest)
       
   260 		{
       
   261 		case ECreateLocalView: 
       
   262             test.Printf(_L("Test failed at step CreateLocalView (%i) with error %i"), 
       
   263                 iCurrentTest, aError); 
       
   264             break;
       
   265 
       
   266 		case ECreateGroupOneView: 
       
   267             test.Printf(_L("Test failed at step ECreateGroupOneView (%i) with error %i"), 
       
   268                 iCurrentTest, aError); 
       
   269             break;
       
   270 
       
   271 
       
   272 		case ECreateGroupTwoView: 
       
   273             test.Printf(_L("Test failed at step ECreateGroupTwoView (%i) with error %i"), 
       
   274                 iCurrentTest, aError); 
       
   275             break;
       
   276 
       
   277         case EDeleteItemsFromView:
       
   278             test.Printf(_L("Test failed at step EDeleteItemsFromView (%i) with error %i"), 
       
   279                 iCurrentTest, aError);     
       
   280             break;
       
   281 
       
   282         case EAddItemToGroup:
       
   283             test.Printf(_L("Test failed at step EAddItemToGroup (%i) with error %i"), 
       
   284                 iCurrentTest, aError);     
       
   285             break;
       
   286 
       
   287 		default: 
       
   288             test.Printf(_L("Test failed at step %i with error %i"), 
       
   289                 iCurrentTest, aError); 
       
   290             break;
       
   291 		}
       
   292 
       
   293 	CActiveScheduler::Stop();
       
   294 	return KErrNone;
       
   295 	}
       
   296 
       
   297 void CGroupViewTester::HandleContactViewEvent( const CContactViewBase& aView,
       
   298     const TContactViewEvent& aEvent )
       
   299 	{
       
   300     // Log events from both underlying view and group view.
       
   301     LogEvent( const_cast<TContactViewEvent&>( aEvent ) );
       
   302 
       
   303 	switch (iCurrentTest)
       
   304 		{
       
   305 		case ECreateLocalView:
       
   306 			test(iLocalView==&aView);
       
   307 			test(aEvent.iEventType==TContactViewEvent::EReady);
       
   308 			break;
       
   309 
       
   310 		case ECreateGroupOneView:
       
   311 			{
       
   312 			test(iGroupViewOne == &aView);
       
   313 			test(aEvent.iEventType == TContactViewEvent::EReady);
       
   314 
       
   315 			//Reserve the IDs of last three contacts in the group
       
   316 			TInt index = KNumContactsInGroupOne-1;
       
   317             TRAPD(err, iLastContactID = iGroupViewOne->ContactAtL(index).Id() );
       
   318 			if (err)
       
   319 				{
       
   320 				_LIT(KErrMsg, "Contact view error in CGroupViewTester::HandleContactViewEvent() assigning iLastContactID\n");
       
   321 				test.Printf(KErrMsg);
       
   322 				User::Invariant();
       
   323 				}
       
   324 
       
   325             --index;
       
   326             TRAP(err, iSecondLastContactID = iGroupViewOne->ContactAtL(index).Id() );
       
   327 			if (err)
       
   328 				{
       
   329 				_LIT(KErrMsg, "Contact view error in CGroupViewTester::HandleContactViewEvent() assigning iSecondLastContactID\n");
       
   330 				test.Printf(KErrMsg);
       
   331 				User::Invariant();
       
   332 				}
       
   333 
       
   334             --index;
       
   335             TRAP(err, iThirdLastContactID = iGroupViewOne->ContactAtL(index).Id() );
       
   336 			if (err)
       
   337 				{
       
   338 				_LIT(KErrMsg, "Contact view error in CGroupViewTester::HandleContactViewEvent() assigning iThirdLastContactID\n");
       
   339 				test.Printf(KErrMsg);
       
   340 				User::Invariant();
       
   341 				}
       
   342 
       
   343 			}
       
   344 			break;
       
   345 
       
   346         case ECreateGroupTwoView:
       
   347 			test( iGroupViewTwo == &aView );
       
   348 			test( aEvent.iEventType == TContactViewEvent::EReady );
       
   349             break;
       
   350 
       
   351         case EDeleteItemsFromView:
       
   352             {
       
   353             // Bulk delete complete when the last Ready is received from the 
       
   354             // base view. This comes after the group view ready event. Break to 
       
   355             // run next test.
       
   356             TBool lastEvent = (aEvent.iEventType == TContactViewEvent::EReady) &&
       
   357                 (&aView == iLocalView);
       
   358             if (lastEvent) 
       
   359                 { 
       
   360                 break; 
       
   361                 }
       
   362 
       
   363             if (aEvent.iEventType == TContactViewEvent::EItemRemoved)
       
   364                 {
       
   365                 // Only proces events from group view.
       
   366                 if (&aView == iGroupViewOne)
       
   367                     {
       
   368                     // Ensure that only 1 contact is removed per notification.
       
   369                      iNumRemovedEvents++;
       
   370                     TInt numItemsExpected = KNumContactsInGroupOne - iNumRemovedEvents;
       
   371 		            
       
   372 		            TInt numItems(0);
       
   373 		            TRAPD(err, numItems = iGroupViewOne->CountL() );
       
   374 					if (err)
       
   375 						{
       
   376 						_LIT(KErrMsg, "Contact view error in CGroupViewTester::HandleContactViewEvent() assigning numItems\n");
       
   377 						test.Printf(KErrMsg);
       
   378 						User::Invariant();
       
   379 						}
       
   380 		            test(numItems == numItemsExpected );
       
   381 
       
   382                     // Verify the contacts were deleted as expected.
       
   383                     TBool testVal(EFalse);
       
   384                     TInt index = numItemsExpected - 1;
       
   385 					TRAP(err, testVal = (iGroupViewOne->ContactAtL(index).Id() == iLastContactID) );
       
   386 					if (err)
       
   387 						{
       
   388 						_LIT(KErrMsg, "Contact view error in CGroupViewTester::HandleContactViewEvent() assigning testVal for iLastContactID\n");
       
   389 						test.Printf(KErrMsg);
       
   390 						User::Invariant();
       
   391 						}
       
   392 					test(testVal);
       
   393 
       
   394                     --index;
       
   395 					TRAP(err, testVal = (iGroupViewOne->ContactAtL(index).Id() == iSecondLastContactID) );
       
   396 					if (err)
       
   397 						{
       
   398 						_LIT(KErrMsg, "Contact view error in CGroupViewTester::HandleContactViewEvent() assigning testVal for iSecondLastContactID\n");
       
   399 						test.Printf(KErrMsg);
       
   400 						User::Invariant();
       
   401 						}
       
   402 					test(testVal);
       
   403 
       
   404                     --index;
       
   405 					TRAP(err, testVal = (iGroupViewOne->ContactAtL(index).Id() == iThirdLastContactID) );
       
   406 					if (err)
       
   407 						{
       
   408 						_LIT(KErrMsg, "Contact view error in CGroupViewTester::HandleContactViewEvent() assigning testVal for iThirdLastContactID\n");
       
   409 						test.Printf(KErrMsg);
       
   410 						User::Invariant();
       
   411 						}
       
   412 					test(testVal);
       
   413                     }
       
   414 
       
   415                 // More events expected, don't run next test.
       
   416                 return;
       
   417                 }
       
   418             else
       
   419                 {
       
   420                 // Ignore all other events.
       
   421                 return;
       
   422                 }
       
   423             }
       
   424 
       
   425         case EAddItemToGroup:
       
   426             {
       
   427             // Break to run next test if this is the last expected event. The last
       
   428             // event is the group being added from the base view. The local view
       
   429             // removes and then adds the group id whenever the group changes. These
       
   430             // events are meaningless.
       
   431             TBool lastEvent = (&aView == iLocalView) && 
       
   432                 (aEvent.iEventType == TContactViewEvent::EItemAdded) &&
       
   433                 (aEvent.iContactId == iGroupOneId);
       
   434             if (lastEvent)
       
   435                 {
       
   436                 break; // Run next test.
       
   437                 }
       
   438 
       
   439             // Verify contact has been added.
       
   440             if (aEvent.iEventType == TContactViewEvent::EItemAdded && 
       
   441                 &aView == iGroupViewOne)
       
   442                 {
       
   443                 // Check if contact was added.
       
   444                 test( aEvent.iContactId == iIdAdded );
       
   445 
       
   446                 TInt index = iGroupViewOne->FindL( iIdAdded );
       
   447                 test (index != KErrNotFound);
       
   448 
       
   449                 // More events expected, don't run next test.
       
   450                 return;
       
   451                 }
       
   452             else
       
   453                 {
       
   454                 // Ignore all other events.
       
   455                 return;
       
   456                 }
       
   457             }
       
   458 
       
   459 		default:
       
   460 			test( EFalse );
       
   461 			break;
       
   462 		}
       
   463 
       
   464     // Run the next test.
       
   465 	if (--iNumNotificationExpected <= 0)
       
   466 		{
       
   467 		NextTest();
       
   468 		iNumNotificationExpected = 0;
       
   469 		}
       
   470 	}
       
   471 
       
   472 void CGroupViewTester::NextTest()
       
   473 	{
       
   474 	++iCurrentTest;
       
   475 	TRequestStatus *pS = &iStatus;
       
   476 	User::RequestComplete( pS, KErrNone );
       
   477 	SetActive();
       
   478 	}
       
   479 
       
   480 void CGroupViewTester::DoCancel()
       
   481 	{
       
   482 	}
       
   483 
       
   484 void CGroupViewTester::LogEvent( TContactViewEvent& aEvent )
       
   485     {
       
   486     _LIT( KLogLine, "Event occurred. Type: %S, iInt: %d, contact id: %d" );
       
   487 
       
   488     TBuf<256> buf; 
       
   489     _LIT( KUnavailable,      "EUnavailable" );
       
   490     _LIT( KReady,            "EReady" );
       
   491     _LIT( KSortOrderChanged, "ESortOrderChanged" );
       
   492     _LIT( KSortError,        "ESortError" );
       
   493     _LIT( KServerError,      "EServerError" );
       
   494     _LIT( KIndexingError,    "EIndexingError" );
       
   495     _LIT( KItemAdded,        "EItemAdded" );
       
   496     _LIT( KItemRemoved,      "EItemRemoved" );
       
   497     _LIT( KGroupChanged,     "EGroupChanged" );
       
   498     _LIT( KUnknown,          "Unknown" );
       
   499 
       
   500     const TDesC16* eventString = 0;
       
   501     switch (aEvent.iEventType)
       
   502         {
       
   503         case TContactViewEvent::EUnavailable:
       
   504             eventString = &KUnavailable;
       
   505             break;
       
   506         case TContactViewEvent::EReady:
       
   507             eventString = &KReady;
       
   508             break;
       
   509         case TContactViewEvent::ESortOrderChanged:
       
   510             eventString = &KSortOrderChanged;
       
   511             break;
       
   512         case TContactViewEvent::ESortError:
       
   513             eventString = &KSortError;
       
   514             break;
       
   515         case TContactViewEvent::EServerError:
       
   516             eventString = &KServerError;
       
   517             break;
       
   518         case TContactViewEvent::EIndexingError:
       
   519             eventString = &KIndexingError;
       
   520             break;
       
   521         case TContactViewEvent::EItemAdded:
       
   522             eventString = &KItemAdded;
       
   523             break;
       
   524         case TContactViewEvent::EItemRemoved:
       
   525             eventString = &KItemRemoved;
       
   526             break;
       
   527         case TContactViewEvent::EGroupChanged:
       
   528             eventString = &KGroupChanged;
       
   529             break;
       
   530         default:
       
   531             eventString = &KUnknown;
       
   532             break;
       
   533         };
       
   534 
       
   535     buf.Format( KLogLine, eventString, aEvent.iInt, aEvent.iContactId );
       
   536     test.Printf( buf );
       
   537     }
       
   538 
       
   539 //
       
   540 // Main.
       
   541 //
       
   542 
       
   543 /**
       
   544 
       
   545 @SYMTestCaseID     PIM-T-GROUPVIEWEVENTS-0001
       
   546 
       
   547 */
       
   548 
       
   549 GLDEF_C TInt E32Main()
       
   550 	{
       
   551 	__UHEAP_MARK;
       
   552 	CActiveScheduler* scheduler=new CActiveScheduler;
       
   553 	if (scheduler)
       
   554 		{
       
   555 		CActiveScheduler::Install(scheduler);
       
   556 		CTrapCleanup* cleanup=CTrapCleanup::New();
       
   557 		if (cleanup)
       
   558 			{
       
   559 			CTestConductor* testConductor=NULL;
       
   560 			test.Start(KTestName);
       
   561 
       
   562 			TRAPD(err,testConductor=CTestConductor::NewL());
       
   563 			test(err == KErrNone);
       
   564 			test.End();
       
   565 			delete testConductor;
       
   566 			test.Close();
       
   567 			delete cleanup;
       
   568 			}
       
   569 		delete scheduler;
       
   570 		}
       
   571 	__UHEAP_MARKEND;
       
   572 	return KErrNone;
       
   573     }