phonebookengines/contactsmodel/tsrc/T_ViewNotificationError.cpp
changeset 0 e686773b3f54
child 24 0ba2181d7c28
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     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 //
       
    15 
       
    16 #include <e32test.h>
       
    17 #include <cntitem.h>
       
    18 #include <cntfldst.h>
       
    19 #include <cntviewbase.h>
       
    20 
       
    21 #include "CContactViewEventQueue.h"
       
    22 
       
    23 
       
    24 // Constants
       
    25 _LIT(KTestName, "T_ViewNotificationError");
       
    26 
       
    27 _LIT(KTestDbName, "c:T_ViewNotificationError.cdb");
       
    28 
       
    29 
       
    30 // Global RTest object
       
    31 LOCAL_D RTest test(KTestName);
       
    32 
       
    33 
       
    34 /**
       
    35  * A helper class for holding the resources needed in the tests.
       
    36  */
       
    37 class CTestResources :
       
    38         public CBase, public MContactViewObserver
       
    39     {
       
    40     public:
       
    41         static CTestResources* NewLC();
       
    42         void ConstructL();
       
    43         void CreateViewL();
       
    44         void RemoveFirstContactL();
       
    45         ~CTestResources();
       
    46 
       
    47         CContactDatabase* iDb;
       
    48         CContactViewEventQueue* iViewObserver1;
       
    49         CContactViewEventQueue* iViewObserver2;
       
    50         CContactViewEventQueue* iViewObserver3;
       
    51         RContactViewSortOrder iViewSortOrder;
       
    52         CContactLocalView* iAllContactsView;
       
    53 
       
    54     private: // From MContactViewObserver.
       
    55 	    void HandleContactViewEvent
       
    56             (const CContactViewBase& aView, const TContactViewEvent& aEvent);
       
    57     };
       
    58 
       
    59 /**
       
    60  * Creates new test resources object.
       
    61  */
       
    62 CTestResources* CTestResources::NewLC()
       
    63     {
       
    64     CTestResources* self = new(ELeave) CTestResources;
       
    65     CleanupStack::PushL(self);
       
    66     self->ConstructL();
       
    67     return self;
       
    68     }
       
    69 
       
    70 void CTestResources::ConstructL()
       
    71     {
       
    72     // Open test DB
       
    73     iDb = CContactDatabase::OpenL(KTestDbName);
       
    74     }
       
    75 
       
    76 void CTestResources::CreateViewL()
       
    77     {
       
    78     // Create the sort order
       
    79     iViewSortOrder.AppendL(KUidContactFieldFamilyName);
       
    80     iViewSortOrder.AppendL(KUidContactFieldGivenName);
       
    81     iViewSortOrder.AppendL(KUidContactFieldCompanyName);
       
    82 
       
    83     // Create a view, assign this class to it's observer
       
    84     iAllContactsView = CContactLocalView::NewL
       
    85         (*this, *iDb, iViewSortOrder, EContactsOnly);
       
    86 
       
    87     // Create three additional view observers
       
    88     iViewObserver1 = CContactViewEventQueue::NewL(iAllContactsView);
       
    89     iViewObserver2 = CContactViewEventQueue::NewL(iAllContactsView);
       
    90     iViewObserver3 = CContactViewEventQueue::NewL(iAllContactsView);
       
    91 	iViewObserver1->CloseOnItemRemoved();
       
    92 	iViewObserver2->CloseOnItemRemoved();
       
    93 	iViewObserver3->CloseOnItemRemoved();
       
    94     }
       
    95 
       
    96 void CTestResources::RemoveFirstContactL()
       
    97     {
       
    98     iDb->DeleteContactL(1);
       
    99     }
       
   100 
       
   101 void CTestResources::HandleContactViewEvent
       
   102         (const CContactViewBase& /*aView*/, const TContactViewEvent& /*aEvent*/)
       
   103     {
       
   104     // Do nothing
       
   105     }
       
   106 
       
   107 CTestResources::~CTestResources()
       
   108     {
       
   109     if (iAllContactsView) iAllContactsView->Close(*this);
       
   110     iViewSortOrder.Close();
       
   111     delete iViewObserver3;
       
   112     delete iViewObserver2;
       
   113     delete iViewObserver1;
       
   114 	User::After(1000000);
       
   115     delete iDb;
       
   116     }
       
   117 
       
   118 
       
   119 LOCAL_C TContactItemId CreateTestContactL
       
   120         (CContactDatabase& aDb, const TDesC& aFamilyName)
       
   121     {
       
   122     // Create a contact card
       
   123     CContactCard* card = CContactCard::NewLC();
       
   124     // Create a name field
       
   125     CContactItemField* field = CContactItemField::NewLC
       
   126         (KStorageTypeText, KUidContactFieldFamilyName);
       
   127     field->TextStorage()->SetTextL(aFamilyName);
       
   128     card->AddFieldL(*field);
       
   129     CleanupStack::Pop(field);
       
   130     // Add the contact to the DB
       
   131     const TContactItemId contactId = aDb.AddNewContactL(*card);
       
   132     CleanupStack::PopAndDestroy(card);
       
   133     return contactId;
       
   134     }
       
   135 
       
   136 
       
   137 LOCAL_C CContactIdArray* PopulateTestDbLC(TInt aNumContacts)
       
   138     {
       
   139     CContactIdArray* ids = CContactIdArray::NewLC();
       
   140     CContactDatabase* db = CContactDatabase::ReplaceL(KTestDbName);
       
   141     CleanupStack::PushL(db);
       
   142     TBuf<50> name;
       
   143     _LIT(KNameFormat, "Test%d");
       
   144     for (TInt i=1; i <= aNumContacts; ++i)
       
   145         {
       
   146         name.Format(KNameFormat,i);
       
   147         const TContactItemId id = CreateTestContactL(*db,name);
       
   148         ids->AddL(id);
       
   149         }
       
   150     CleanupStack::PopAndDestroy(db);
       
   151     return ids;
       
   152     }
       
   153 
       
   154 
       
   155 /**
       
   156  * This test demonstrates how a contact view fails to notify its observers
       
   157  * if some of the observers deregisters for receiving events. It does not
       
   158  * only fail to do that, it also panics the application.
       
   159  * There is a design error in CContactViewBase::NotifyObservers.
       
   160  */
       
   161 
       
   162 LOCAL_C void TestViewNotificationL()
       
   163     {
       
   164     test.Next(_L("Test contact view notification"));
       
   165 
       
   166 
       
   167     // Open the populated test DB
       
   168     CTestResources* res = CTestResources::NewLC();
       
   169 
       
   170     // Create a contact view and three additional
       
   171     // observers (please see CTestResources::CreateViewL
       
   172     // implementation for extra comments)
       
   173     res->CreateViewL();
       
   174 
       
   175     // Wait for the first observer to report the view is ready
       
   176     TContactViewEvent event;
       
   177     test(res->iViewObserver1->ListenForEvent(10,event));
       
   178     test(event.iEventType == TContactViewEvent::EReady);
       
   179 
       
   180     // ..then the second observer...
       
   181     test(res->iViewObserver2->ListenForEvent(10,event));
       
   182     test(event.iEventType == TContactViewEvent::EReady);
       
   183 
       
   184     // ..and finally the last observer reports it's ready.
       
   185     test(res->iViewObserver3->ListenForEvent(10,event));
       
   186     test(event.iEventType == TContactViewEvent::EReady);
       
   187 
       
   188     // Remove one contact from the db
       
   189     res->RemoveFirstContactL();
       
   190 
       
   191     // Wait for the first observer to receive the remove event...
       
   192     test(res->iViewObserver1->ListenForEvent(10,event));
       
   193 /** 
       
   194  *  After the above line Contact Model crashes to
       
   195  *  USER 130 panic, EBadArrayIndex.
       
   196  *
       
   197  *  The reason is that the observer deregistered itself
       
   198  *  from receiving contact view events when it got
       
   199  *  EItemRemoved event. The for loop in
       
   200  *  CContactViewBase::NotifyObservers can't handle that
       
   201  *  situation.
       
   202  */
       
   203     test(event.iEventType == TContactViewEvent::EItemRemoved);
       
   204 
       
   205     // Then second observer should receive the event
       
   206     test(res->iViewObserver2->ListenForEvent(10,event));
       
   207     test(event.iEventType == TContactViewEvent::EItemRemoved);
       
   208 
       
   209     // And finally the third observer should receive the event
       
   210     test(res->iViewObserver3->ListenForEvent(10,event));
       
   211     test(event.iEventType == TContactViewEvent::EItemRemoved);
       
   212     CleanupStack::PopAndDestroy(res);
       
   213     }
       
   214 
       
   215 /**
       
   216 
       
   217 @SYMTestCaseID     PIM-T-VIEWNOTIFICATIONERROR-0001
       
   218 
       
   219 */
       
   220 
       
   221 
       
   222 LOCAL_C void DoTestsL()
       
   223     {
       
   224     // Init
       
   225     CleanupClosePushL(test);
       
   226 	test.Start(_L("@SYMTESTCaseID:PIM-T-VIEWNOTIFICATIONERROR-0001 T_ViewNotificationError"));
       
   227 
       
   228 
       
   229     // Populate a test DB
       
   230     const TInt KNumTestContacts = 100;
       
   231     CContactIdArray* testContactIds = PopulateTestDbLC(KNumTestContacts);
       
   232 
       
   233     // Execute the tests
       
   234     TestViewNotificationL();
       
   235 
       
   236     // Cleanup
       
   237     CleanupStack::PopAndDestroy(testContactIds);
       
   238     test.End();
       
   239     CleanupStack::PopAndDestroy();  // test.Close()
       
   240     }
       
   241 
       
   242 GLDEF_C TInt E32Main()
       
   243 	{
       
   244     // Init
       
   245     __UHEAP_MARK;
       
   246     CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   247     if (!cleanupStack)
       
   248         {
       
   249         return KErrNoMemory;
       
   250         }
       
   251 
       
   252     CActiveScheduler* activeScheduler = new CActiveScheduler;
       
   253     if (!activeScheduler)
       
   254         {
       
   255         return KErrNoMemory;
       
   256         }
       
   257     CActiveScheduler::Install(activeScheduler);
       
   258 
       
   259     // Run the tests
       
   260     TRAPD(err, DoTestsL());
       
   261 
       
   262     // Cleanup
       
   263     delete activeScheduler;
       
   264     delete cleanupStack;
       
   265     __UHEAP_MARKEND;
       
   266 	return err;
       
   267     }