phonebookengines_old/contactsmodel/tsrc/t_groupviewpanic.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 // GroupViewPanic Test module
       
    15 // 
       
    16 //
       
    17 
       
    18 
       
    19 #include <e32test.h>
       
    20 #include <cntdef.h>
       
    21 #include <cntdb.h>
       
    22 #include <cntitem.h>
       
    23 #include <cntfield.h>
       
    24 #include <cntfldst.h>
       
    25 #include <cntviewbase.h>
       
    26 
       
    27 #include "CContactViewEventQueue.h"
       
    28 
       
    29 _LIT(KTestName, "T_GroupViewPanic");
       
    30 
       
    31 _LIT(KTestDbName, "c:T_GroupViewPanic.cdb");
       
    32 
       
    33 
       
    34 LOCAL_D RTest test(KTestName);
       
    35 
       
    36 
       
    37 class CRemoteViewTestResources : public CBase
       
    38     {
       
    39     public:
       
    40         static CRemoteViewTestResources* NewLC();
       
    41         void ConstructL();
       
    42         ~CRemoteViewTestResources();
       
    43         void CreateTestGroupsL();
       
    44 
       
    45         CContactDatabase* iDb;
       
    46         CContactIdArray* iContacts;
       
    47         CContactViewEventQueue* iViewEventQueue;
       
    48         RContactViewSortOrder iViewSortOrder;
       
    49         CContactRemoteView* iRemoteView;
       
    50     };
       
    51    
       
    52 CRemoteViewTestResources* CRemoteViewTestResources::NewLC()
       
    53     {
       
    54     CRemoteViewTestResources* self = new(ELeave) CRemoteViewTestResources;
       
    55     CleanupStack::PushL(self);
       
    56     self->ConstructL();
       
    57     return self;
       
    58     }
       
    59 
       
    60 void CRemoteViewTestResources::ConstructL()
       
    61     {          
       
    62     iDb = CContactDatabase::ReplaceL(KTestDbName);
       
    63 	iContacts = CContactIdArray::NewL();
       
    64     iViewEventQueue = CContactViewEventQueue::NewL();
       
    65     iViewSortOrder.AppendL(KUidContactFieldTemplateLabel);
       
    66 
       
    67     const TContactViewPreferences prefs = static_cast<TContactViewPreferences>
       
    68     			(EGroupsOnly |ESingleWhiteSpaceIsEmptyField | EUnSortedAtEnd);       
       
    69     iRemoteView = CContactRemoteView::NewL(*iViewEventQueue, *iDb, iViewSortOrder, prefs);
       
    70     
       
    71     // Wait for view to get ready
       
    72     TContactViewEvent event;
       
    73 	
       
    74 	TBool eventReady = iViewEventQueue->ListenForEvent(10,event);
       
    75     __ASSERT_ALWAYS(eventReady,User::Invariant());
       
    76     ASSERT(event.iEventType == TContactViewEvent::EReady);
       
    77     }
       
    78 
       
    79 CRemoteViewTestResources::~CRemoteViewTestResources()
       
    80     {
       
    81     if (iRemoteView)
       
    82     	{
       
    83     	iRemoteView->Close(*iViewEventQueue);	
       
    84     	delete iViewEventQueue;
       
    85     	}
       
    86     iViewSortOrder.Close();    
       
    87     delete iContacts;
       
    88     delete iDb;
       
    89     TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KTestDbName));
       
    90     }
       
    91     
       
    92 class CTestResources : public CBase
       
    93     {
       
    94     public:
       
    95         static CTestResources* NewLC();
       
    96         void ConstructL();
       
    97         void CreateTestContactsL();
       
    98         void CreateTestGroupsL();
       
    99         ~CTestResources();
       
   100 
       
   101         CContactDatabase* iDb;
       
   102         CContactIdArray* iContacts;
       
   103         CContactViewEventQueue* iViewEventQueue;
       
   104         RContactViewSortOrder iViewSortOrder;
       
   105         CContactLocalView* iLocalView;
       
   106     };
       
   107     
       
   108 CTestResources* CTestResources::NewLC()
       
   109     {
       
   110     CTestResources* self = new(ELeave) CTestResources;
       
   111     CleanupStack::PushL(self);
       
   112     self->ConstructL();
       
   113     return self;
       
   114     }
       
   115 
       
   116 
       
   117 
       
   118 
       
   119 void CTestResources::ConstructL()
       
   120     {
       
   121     iDb = CContactDatabase::ReplaceL(KTestDbName);
       
   122     iContacts = CContactIdArray::NewL();
       
   123 
       
   124     // Create some contacts into the database (don't create any groups)
       
   125     CreateTestContactsL();
       
   126 
       
   127     // Close and reopen the DB (this does not help with the bug)
       
   128     /*
       
   129     delete iDb;
       
   130     iDb = NULL;
       
   131     iDb = CContactDatabase::OpenL(KTestDbName);
       
   132     */
       
   133 
       
   134     iViewEventQueue = CContactViewEventQueue::NewL();
       
   135 
       
   136     iViewSortOrder.AppendL(KUidContactFieldTemplateLabel);
       
   137     
       
   138     // Create a groups only contact view
       
   139     const TContactViewPreferences prefs = 
       
   140         static_cast<TContactViewPreferences>(EGroupsOnly);
       
   141     iLocalView = CContactLocalView::NewL
       
   142         (*iViewEventQueue, *iDb, iViewSortOrder, prefs);
       
   143 #if defined (_DEBUG)
       
   144     // Wait for view to get ready
       
   145     TContactViewEvent event;
       
   146 #endif
       
   147     ASSERT(iViewEventQueue->ListenForEvent(10,event));
       
   148     ASSERT(event.iEventType == TContactViewEvent::EReady);
       
   149     }
       
   150 
       
   151 LOCAL_C void AddFieldToContactL
       
   152         (CContactItem& aContact, TFieldType aFieldType, const TDesC& aText)
       
   153     {
       
   154     CContactItemField* field = CContactItemField::NewLC(KStorageTypeText, aFieldType);
       
   155     field->TextStorage()->SetTextL(aText);
       
   156     aContact.AddFieldL(*field);
       
   157     CleanupStack::Pop(field);
       
   158     }
       
   159 
       
   160 LOCAL_C TContactItemId CreateTestContactL
       
   161         (CContactDatabase& aDb,
       
   162         const TDesC& aFamilyName, 
       
   163         const TDesC& aGivenName, 
       
   164         const TDesC& aCompanyName)
       
   165     {
       
   166     CContactCard* card = CContactCard::NewLC();
       
   167     AddFieldToContactL(*card, KUidContactFieldFamilyName, aFamilyName);
       
   168     AddFieldToContactL(*card, KUidContactFieldGivenName, aGivenName);
       
   169     AddFieldToContactL(*card, KUidContactFieldCompanyName, aCompanyName);
       
   170     const TContactItemId id = aDb.AddNewContactL(*card);
       
   171     CleanupStack::PopAndDestroy(card);
       
   172     return id;
       
   173     }
       
   174 
       
   175 // Creates a few test contacts.
       
   176 void CTestResources::CreateTestContactsL()
       
   177     {
       
   178     TContactItemId id;
       
   179     id = CreateTestContactL(*iDb, _L("Example1"), _L("test"), _L("Name1"));
       
   180     iContacts->AddL(id);
       
   181     id = CreateTestContactL(*iDb, _L("Example2"), _L("test1"), _L("Name2"));
       
   182     iContacts->AddL(id);
       
   183     id = CreateTestContactL(*iDb, _L("Example12"), _L("test0"), _L("Name3"));
       
   184     iContacts->AddL(id);
       
   185     id = CreateTestContactL(*iDb, _L("Example13"), _L("test3"), _L("Name4"));
       
   186     iContacts->AddL(id);
       
   187     }
       
   188 
       
   189 CTestResources::~CTestResources()
       
   190     {
       
   191     if (iLocalView)
       
   192     	{
       
   193     	iLocalView->Close(*iViewEventQueue);	
       
   194     	delete iViewEventQueue;
       
   195     	}
       
   196     iViewSortOrder.Close();    
       
   197     delete iContacts;
       
   198     delete iDb;
       
   199     TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KTestDbName));
       
   200     }
       
   201 
       
   202 LOCAL_C void TestDeleteContactsL()
       
   203     {
       
   204     // Create test resources
       
   205     CTestResources* res = CTestResources::NewLC();
       
   206 
       
   207     // Delete all contacts in the DB
       
   208     for (TInt i=0; i < res->iContacts->Count(); ++i)
       
   209         {
       
   210         res->iDb->DeleteContactL((*res->iContacts)[i]);
       
   211         }
       
   212 
       
   213     CleanupStack::PopAndDestroy(res);
       
   214     }
       
   215 
       
   216 void CheckEvent(const TContactViewEvent& event, 
       
   217 				TContactViewEvent::TEventType expectedEventType,
       
   218 				TInt expectedUnderlyingIndex,
       
   219 				TContactItemId expectedItemId)
       
   220 	{	
       
   221 	test(event.iEventType == expectedEventType);
       
   222 	test(event.iInt == expectedUnderlyingIndex);
       
   223 	test(event.iContactId == expectedItemId);
       
   224 	}
       
   225 
       
   226 
       
   227 void CRemoteViewTestResources::CreateTestGroupsL()
       
   228     {
       
   229 	test.Next(_L("Test for creating a remote view and validating that queued events are received in the correct order."));
       
   230 
       
   231 	TContactViewEvent event;
       
   232 
       
   233 	// Create A then "unnanmed", then rename "unnamed" to B
       
   234 	test.Next(_L("Test for Create group A, then create group B"));
       
   235 
       
   236 	CContactItem* group1 = iDb->CreateContactGroupLC(_L("A"));	
       
   237 	iViewEventQueue->ListenForEvent(2,event);
       
   238 	CheckEvent(event, TContactViewEvent::EItemAdded, 0, group1->Id());
       
   239 
       
   240 	CContactItem* group0 = iDb->CreateContactGroupLC(_L(""));
       
   241 	iViewEventQueue->ListenForEvent(2,event);
       
   242 	CheckEvent(event, TContactViewEvent::EItemAdded, 1, group0->Id());
       
   243 	 	
       
   244 	CContactItem *group00 = iDb->OpenContactL(group0->Id());
       
   245 	static_cast<CContactGroup*> (group00)->SetGroupLabelL(_L("B"));
       
   246 	iDb->CommitContactL(*group00);
       
   247 		
       
   248 	// Received 3 events, grp changed, removed and added
       
   249 	iViewEventQueue->ListenForEvent(2,event);
       
   250 	CheckEvent(event, TContactViewEvent::EGroupChanged, 0, group00->Id());
       
   251 	iViewEventQueue->ListenForEvent(2,event);
       
   252 	CheckEvent(event, TContactViewEvent::EItemRemoved, 1, group00->Id());
       
   253 	iViewEventQueue->ListenForEvent(2,event);
       
   254 	CheckEvent(event, TContactViewEvent::EItemAdded, 1, group00->Id()); 	
       
   255 
       
   256 	// Create D then "unnamed", then rename "unnamed" to C
       
   257 	test.Next(_L("Test for Create group D, then create group C"));
       
   258 
       
   259 	CContactItem* group2 = iDb->CreateContactGroupLC(_L("D"));	
       
   260 	iViewEventQueue->ListenForEvent(2,event);
       
   261 	CheckEvent(event, TContactViewEvent::EItemAdded, 2, group2->Id());
       
   262 
       
   263 	CContactItem* group3 = iDb->CreateContactGroupLC(_L(""));
       
   264 	iViewEventQueue->ListenForEvent(2,event);
       
   265 	CheckEvent(event, TContactViewEvent::EItemAdded, 3, group3->Id());
       
   266 	 	
       
   267 	CContactItem *group4 = iDb->OpenContactL(group3->Id());
       
   268 	static_cast<CContactGroup*> (group4)->SetGroupLabelL(_L("C"));
       
   269 	iDb->CommitContactL(*group4);
       
   270 		
       
   271 	// Received 3 events, grp changed, removed and added
       
   272 	iViewEventQueue->ListenForEvent(2,event);
       
   273 	CheckEvent(event, TContactViewEvent::EGroupChanged, 0, group4->Id());
       
   274 	iViewEventQueue->ListenForEvent(2,event);
       
   275 	CheckEvent(event, TContactViewEvent::EItemRemoved, 3, group4->Id());
       
   276 	iViewEventQueue->ListenForEvent(2,event);
       
   277 	CheckEvent(event, TContactViewEvent::EItemAdded, 2, group4->Id()); 
       
   278 
       
   279 	// More complex set of add and deletes.
       
   280 	test.Next(_L("Test-Addgroup E, Addgroup F, Addgroup G , RemoveGroup G Addgroup H RenameGroup H as EA"));
       
   281 
       
   282 	CContactItem* group5 = iDb->CreateContactGroupLC(_L("E"));	   
       
   283 	CContactItem* group6 = iDb->CreateContactGroupLC(_L("F"));	    
       
   284 	CContactItem* group7 = iDb->CreateContactGroupLC(_L("G"));    
       
   285 	iDb->DeleteContactL(group7->Id());	    
       
   286 	CContactItem* group8 = iDb->CreateContactGroupLC(_L("H"));	
       
   287 
       
   288 	CContactItem *group01 = iDb->OpenContactL(group8->Id());
       
   289 	static_cast<CContactGroup*> (group01)->SetGroupLabelL(_L("EA"));
       
   290 	iDb->CommitContactL(*group01);
       
   291 
       
   292 	// Should receive events for operations above, order is not important 
       
   293 	iViewEventQueue->ListenForEvent(2,event);
       
   294 	CheckEvent(event, TContactViewEvent::EItemAdded, 4, group5->Id());
       
   295 	iViewEventQueue->ListenForEvent(2,event);
       
   296 	CheckEvent(event, TContactViewEvent::EItemAdded, 5, group6->Id());
       
   297 	iViewEventQueue->ListenForEvent(2,event);
       
   298 	CheckEvent(event, TContactViewEvent::EItemRemoved, 6, group7->Id());
       
   299 	iViewEventQueue->ListenForEvent(2,event);
       
   300 	CheckEvent(event, TContactViewEvent::EGroupChanged, 0, group8->Id());
       
   301 
       
   302 	// Cleanup
       
   303 	delete group01;
       
   304 	delete group00;
       
   305 	delete group4;    
       
   306 	CleanupStack::PopAndDestroy(group8);
       
   307 	CleanupStack::PopAndDestroy(group7);
       
   308 	CleanupStack::PopAndDestroy(group6);
       
   309 	CleanupStack::PopAndDestroy(group5);
       
   310 	CleanupStack::PopAndDestroy(group3);
       
   311 	CleanupStack::PopAndDestroy(group2);     
       
   312 	CleanupStack::PopAndDestroy(group0);
       
   313 	CleanupStack::PopAndDestroy(group1);
       
   314     }    
       
   315 
       
   316 /**
       
   317 @SYMTestCaseID PIM-T-GROUPVIEWPANIC-0001
       
   318 @SYMTestType UT
       
   319 @SYMTestPriority Urgent
       
   320 @SYMDEF INC097928
       
   321 @SYMTestCaseDesc Check the sequence of events when groups are added, removed or edited
       
   322 @SYMTestActions
       
   323 @SYMTestExpectedResults The sequence of events should be proper.
       
   324 */    
       
   325 LOCAL_C void TestEventsForGroupCreationL()
       
   326     {
       
   327     // Create test resources
       
   328     CRemoteViewTestResources * pRes = CRemoteViewTestResources::NewLC();
       
   329     pRes->CreateTestGroupsL();  
       
   330     CleanupStack::PopAndDestroy(pRes);  
       
   331     }   
       
   332 
       
   333 LOCAL_C void DoTestsL()
       
   334     {
       
   335 	test.Start(_L("@SYMTESTCaseID:PIM-T-GROUPVIEWPANIC-0001 T_GroupViewPanic"));
       
   336 
       
   337     TestDeleteContactsL();
       
   338     TestEventsForGroupCreationL();    
       
   339     test.End();
       
   340     test.Close();
       
   341     }
       
   342 
       
   343 GLDEF_C TInt E32Main()
       
   344 	{
       
   345     // Init
       
   346     CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   347     if (!cleanupStack)
       
   348         {
       
   349         return KErrNoMemory;
       
   350         }
       
   351 
       
   352     CActiveScheduler* activeScheduler = new CActiveScheduler;
       
   353     if (!activeScheduler)
       
   354         {
       
   355         return KErrNoMemory;
       
   356         }
       
   357     CActiveScheduler::Install(activeScheduler);
       
   358 
       
   359     // Run the tests
       
   360     __UHEAP_MARK;
       
   361     TRAPD(err, DoTestsL());
       
   362     __UHEAP_MARKEND;
       
   363 
       
   364     // Cleanup
       
   365     delete activeScheduler;
       
   366     delete cleanupStack;
       
   367 
       
   368 	return err;
       
   369     }