phonebookengines_old/contactsmodel/tsrc/T_GroupViewUpdate.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 
       
    24 #include "CContactViewEventQueue.h"
       
    25 #include "CContactDbEventQueue.h"
       
    26 
       
    27 _LIT(KTestName, "T_GroupViewUpdate");
       
    28 
       
    29 _LIT(KTestDbName, "c:T_GroupViewUpdate.cdb");
       
    30 
       
    31 LOCAL_D RTest test(KTestName);
       
    32 
       
    33 
       
    34 class CTestResources : public CBase
       
    35     {
       
    36     public:
       
    37         static CTestResources* NewLC();
       
    38         void ConstructL();
       
    39         void CreateTestContactsL();
       
    40         ~CTestResources();
       
    41 
       
    42         CContactDatabase* iDb;
       
    43         CContactIdArray* iContacts;
       
    44         TContactItemId iGroupId;
       
    45         CContactDbEventQueue* iDbEventQueue;
       
    46         CContactViewEventQueue* iViewEventQueue;
       
    47         RContactViewSortOrder iViewSortOrder;
       
    48         CContactLocalView* iLocalView;
       
    49         CContactViewEventQueue* iGroupViewEventQueue;
       
    50         CContactGroupView* iGroupView;
       
    51     };
       
    52 
       
    53 CTestResources* CTestResources::NewLC()
       
    54     {
       
    55     CTestResources* self = new(ELeave) CTestResources;
       
    56     CleanupStack::PushL(self);
       
    57     self->ConstructL();
       
    58     return self;
       
    59     }
       
    60 
       
    61 void CTestResources::ConstructL()
       
    62     {
       
    63     iDb = CContactDatabase::ReplaceL(KTestDbName);
       
    64     iDbEventQueue = CContactDbEventQueue::NewL(iDb);
       
    65 
       
    66     CreateTestContactsL();
       
    67 
       
    68     iViewEventQueue = CContactViewEventQueue::NewL();
       
    69 
       
    70     iViewSortOrder.AppendL(KUidContactFieldFamilyName);
       
    71     iViewSortOrder.AppendL(KUidContactFieldGivenName);
       
    72     iViewSortOrder.AppendL(KUidContactFieldCompanyName);
       
    73     
       
    74     iLocalView = CContactLocalView::NewL
       
    75         (*iViewEventQueue, *iDb, iViewSortOrder, EContactsOnly);
       
    76 
       
    77     iGroupViewEventQueue = CContactViewEventQueue::NewL();
       
    78     iGroupView = CContactGroupView::NewL
       
    79         (*iDb, *iLocalView, *iGroupViewEventQueue, iGroupId, 
       
    80         CContactGroupView::EShowContactsInGroup);
       
    81 
       
    82     // Wait for group view to get ready
       
    83     TContactViewEvent event;
       
    84 	TBool eventReady = iGroupViewEventQueue->ListenForEvent(10,event);
       
    85     __ASSERT_ALWAYS(eventReady,User::Invariant());
       
    86     ASSERT(event.iEventType == TContactViewEvent::EReady);
       
    87 
       
    88     // Flush all other events
       
    89     iDbEventQueue->Flush();
       
    90     iViewEventQueue->Flush();
       
    91     iGroupViewEventQueue->Flush();
       
    92     }
       
    93 
       
    94 void CTestResources::CreateTestContactsL()
       
    95     {
       
    96     const TInt KContacts = 4;
       
    97     TInt i;
       
    98     iContacts = CContactIdArray::NewL();
       
    99 
       
   100     // Create contacts
       
   101     for (i=1; i <= KContacts; ++i)
       
   102         {
       
   103         CContactCard* card = CContactCard::NewLC();
       
   104         CContactItemField* field = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldFamilyName);
       
   105         TBuf<30> name;
       
   106         name.Format(_L("Contact%02d"), i);
       
   107         field->TextStorage()->SetTextL(name);
       
   108         card->AddFieldL(*field);
       
   109         CleanupStack::Pop(field);
       
   110         const TContactItemId contactId = iDb->AddNewContactL(*card);
       
   111         iContacts->AddL(contactId);
       
   112         CleanupStack::PopAndDestroy(card);
       
   113         // Eat away contact db events
       
   114         TContactDbObserverEvent event;
       
   115 		TBool eventReady = iDbEventQueue->ListenForEvent(10,event);
       
   116         __ASSERT_ALWAYS(eventReady,User::Invariant());        
       
   117 		}
       
   118 
       
   119     // Create a group
       
   120     CContactItem* group = iDb->CreateContactGroupLC(_L("TestGroup"));
       
   121     iGroupId = group->Id();
       
   122     CleanupStack::PopAndDestroy(group);
       
   123 
       
   124     // Connect half of the contacts to the group
       
   125     for (i=0; i < iContacts->Count(); ++i)
       
   126         {
       
   127         if (i%2 == 0)
       
   128             {
       
   129             // This call doesn't send any event from CContactDatabase, which is 
       
   130             // pretty strange...
       
   131             iDb->AddContactToGroupL((*iContacts)[i], iGroupId);            
       
   132             }
       
   133         }
       
   134     }
       
   135 
       
   136 CTestResources::~CTestResources()
       
   137     {
       
   138     if (iGroupView) iGroupView->Close(*iGroupViewEventQueue);
       
   139     delete iGroupViewEventQueue;
       
   140     if (iLocalView) iLocalView->Close(*iViewEventQueue);
       
   141     iViewSortOrder.Close();
       
   142     delete iViewEventQueue;
       
   143     delete iDbEventQueue;
       
   144     delete iContacts;
       
   145     delete iDb;
       
   146     TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KTestDbName));
       
   147     }
       
   148 
       
   149 LOCAL_C void CheckConsistentL
       
   150         (CContactDatabase& aDb,
       
   151         const CContactGroupView& aGroupView, 
       
   152         const CContactViewBase& aBaseView,
       
   153         TContactItemId aGroupId)
       
   154     {
       
   155     const TInt groupViewCount = aGroupView.CountL();
       
   156     test(groupViewCount <= aBaseView.CountL());
       
   157     CContactGroup* group = static_cast<CContactGroup*>(aDb.ReadContactLC(aGroupId));
       
   158 
       
   159     const TInt count = aBaseView.CountL();
       
   160     TInt checked = 0;
       
   161     for (TInt i=0; i < count; ++i)
       
   162         {
       
   163         const TContactItemId id = aBaseView.AtL(i);
       
   164         if (group->ContainsItem(id))
       
   165             {
       
   166             test(aGroupView.FindL(id) != KErrNotFound);
       
   167             ++checked;
       
   168             }
       
   169         }
       
   170     test(checked == groupViewCount);
       
   171     CleanupStack::PopAndDestroy(group);
       
   172     }
       
   173 
       
   174 LOCAL_C void CheckConsistentL(CTestResources& aRes)
       
   175     {
       
   176     CheckConsistentL(*aRes.iDb, *aRes.iGroupView, *aRes.iLocalView, aRes.iGroupId);
       
   177     }
       
   178 
       
   179 LOCAL_C void TestAddContactToGroupL()
       
   180     {
       
   181     test.Next(_L("Add a contact to the group"));
       
   182 
       
   183     CTestResources* res = CTestResources::NewLC();
       
   184 
       
   185     CheckConsistentL(*res);
       
   186 
       
   187     // Need to open and commit the group to get any events from CContactDatabase!
       
   188     CContactGroup* group = static_cast<CContactGroup*>(res->iDb->OpenContactLX(res->iGroupId));
       
   189     CleanupStack::PushL(group);
       
   190     // Find a contact to add into group
       
   191     for (TInt i=0; i < res->iContacts->Count(); ++i)
       
   192         {
       
   193         const TContactItemId contactId = (*res->iContacts)[i];
       
   194         if (!group->ContainsItem(contactId))
       
   195             {
       
   196             // Item found, add it to the group
       
   197             res->iDb->AddContactToGroupL(contactId, group->Id());
       
   198             // Commit the group to get group changed event from Contact Db
       
   199             res->iDb->CommitContactL(*group);
       
   200             
       
   201             // Listen to the event from the DB
       
   202             TContactDbObserverEvent dbEvent;
       
   203             test(res->iDbEventQueue->ListenForEvent(2,dbEvent));
       
   204             test(dbEvent.iType == EContactDbObserverEventGroupChanged);
       
   205             test(dbEvent.iContactId == group->Id());
       
   206 
       
   207             // Listen to the event from the group view
       
   208             TContactViewEvent groupViewEvent;
       
   209             test(res->iGroupViewEventQueue->ListenForEvent(2,groupViewEvent));
       
   210             //test(groupViewEvent.iEventType == TContactViewEvent::EGroupChanged);
       
   211             //test(groupViewEvent.iContactId == group->Id());
       
   212             test(groupViewEvent.iEventType == TContactViewEvent::ESortOrderChanged);
       
   213             break;
       
   214             }
       
   215         }
       
   216     CleanupStack::PopAndDestroy(2);  // group, lock
       
   217 
       
   218     // Eat away all events from queue to give iGroupView a chance to update
       
   219     // itself
       
   220     // BUG: This loop receives a second EGroupChanged event from the group view!
       
   221     TContactViewEvent groupViewEvent;
       
   222     while (res->iGroupViewEventQueue->ListenForEvent(2,groupViewEvent))
       
   223         {
       
   224         }
       
   225     
       
   226     CheckConsistentL(*res);
       
   227     CleanupStack::PopAndDestroy(res);
       
   228     }
       
   229 
       
   230 
       
   231 LOCAL_C void TestRemoveContactFromGroupL()
       
   232     {
       
   233     test.Next(_L("Remove a contact from the group"));
       
   234 
       
   235     CTestResources* res = CTestResources::NewLC();
       
   236 
       
   237     CheckConsistentL(*res);
       
   238 
       
   239     // Need to open and commit the group to get any events from CContactDatabase!
       
   240     CContactGroup* group = static_cast<CContactGroup*>(res->iDb->OpenContactLX(res->iGroupId));
       
   241     CleanupStack::PushL(group);
       
   242 
       
   243     const TContactItemId contactId = (*group->ItemsContained())[0];
       
   244     res->iDb->RemoveContactFromGroupL(contactId, group->Id());
       
   245     // Commit the group to get group changed event from Contact Db
       
   246     res->iDb->CommitContactL(*group);
       
   247             
       
   248     // Listen to the event from the DB
       
   249     TContactDbObserverEvent dbEvent;
       
   250     test(res->iDbEventQueue->ListenForEvent(2,dbEvent));
       
   251     test(dbEvent.iType == EContactDbObserverEventGroupChanged);
       
   252     test(dbEvent.iContactId == group->Id());
       
   253 
       
   254     // Listen to the event from the group view
       
   255     TContactViewEvent groupViewEvent;
       
   256     test(res->iGroupViewEventQueue->ListenForEvent(2,groupViewEvent));
       
   257     //test(groupViewEvent.iEventType == TContactViewEvent::EGroupChanged);
       
   258     //test(groupViewEvent.iContactId == group->Id());
       
   259     test(groupViewEvent.iEventType == TContactViewEvent::ESortOrderChanged);
       
   260 
       
   261     CleanupStack::PopAndDestroy(2);  // group, lock
       
   262 
       
   263     // Eat away all events from queue to give iGroupView a chance to update
       
   264     // itself
       
   265     // BUG: This loop receives a second EGroupChanged event from the group view!
       
   266     while (res->iGroupViewEventQueue->ListenForEvent(2,groupViewEvent))
       
   267         {
       
   268         }
       
   269     
       
   270     // This test fails: the group view still contains contactId
       
   271     CheckConsistentL(*res);
       
   272     CleanupStack::PopAndDestroy(res);
       
   273     }
       
   274 
       
   275 /**
       
   276 
       
   277 @SYMTestCaseID     PIM-T-GROUPVIEWUPDATE-0001
       
   278 
       
   279 */
       
   280 
       
   281 void DoTestsL()
       
   282     {
       
   283 	test.Start(_L("@SYMTESTCaseID:PIM-T-GROUPVIEWUPDATE-0001 T_GroupViewUpdate"));
       
   284 
       
   285     TRAPD(err,TestAddContactToGroupL());
       
   286     test(err == KErrNone);
       
   287     TRAP(err,TestRemoveContactFromGroupL());
       
   288     test(err == KErrNone);
       
   289     test.End();
       
   290     test.Close();   
       
   291   	}
       
   292 
       
   293 GLDEF_C TInt E32Main()
       
   294 	{
       
   295     // Init
       
   296     __UHEAP_MARK; 
       
   297     CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   298     if (!cleanupStack)
       
   299         {
       
   300         return KErrNoMemory;
       
   301         }
       
   302 
       
   303     CActiveScheduler* activeScheduler = new CActiveScheduler;
       
   304     if (!activeScheduler)
       
   305         {
       
   306         return KErrNoMemory;
       
   307         }
       
   308     CActiveScheduler::Install(activeScheduler);
       
   309 
       
   310     // Run the tests
       
   311     TRAPD(err, DoTestsL());
       
   312 
       
   313     // Cleanup
       
   314     delete activeScheduler;
       
   315     delete cleanupStack;
       
   316     __UHEAP_MARKEND;
       
   317 	return err;
       
   318     }