phonebookengines_old/contactsmodel/tsrc/T_LocalViewRemove.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 // LocalViewRemove 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 #include "CContactDbEventQueue.h"
       
    29 
       
    30 _LIT(KTestName, "T_LocalViewRemove");
       
    31 
       
    32 _LIT(KTestDbName, "c:T_LocalViewRemove.cdb");
       
    33 
       
    34 LOCAL_D RTest test(KTestName);
       
    35 
       
    36 class CTestResources : public CBase
       
    37     {
       
    38     public:
       
    39         static CTestResources* NewLC();
       
    40         void ConstructL();
       
    41         TContactItemId CreateTestContactL(const TDesC& aFamilyName, TBool aWithPhoneNumber);
       
    42         ~CTestResources();
       
    43 
       
    44         CContactDatabase* iDb;
       
    45         TContactItemId iGroupId;
       
    46         CContactDbEventQueue* iDbEventQueue;
       
    47         CContactViewEventQueue* iViewEventQueue;
       
    48         RContactViewSortOrder iViewSortOrder;
       
    49         CContactLocalView* iLocalView;
       
    50         TContactItemId iTestContactId;
       
    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     iViewEventQueue = CContactViewEventQueue::NewL();
       
    67 
       
    68     iViewSortOrder.AppendL(KUidContactFieldFamilyName);
       
    69     iViewSortOrder.AppendL(KUidContactFieldGivenName);
       
    70     iViewSortOrder.AppendL(KUidContactFieldCompanyName);
       
    71 
       
    72 	// Create contact view preferences, UNSORTED AT THE END!
       
    73 	TContactViewPreferences preferences =  static_cast<TContactViewPreferences>
       
    74         (EContactsOnly | EUnSortedAtEnd | ESingleWhiteSpaceIsEmptyField);
       
    75 
       
    76     // Create the view
       
    77     iLocalView = CContactLocalView::NewL
       
    78         (*iViewEventQueue, *iDb, iViewSortOrder, preferences);
       
    79 
       
    80     // Wait for view to get ready
       
    81     TContactViewEvent event;
       
    82     test(iViewEventQueue->ListenForEvent(10,event));
       
    83     test(event.iEventType == TContactViewEvent::EReady);
       
    84 
       
    85     // Flush all other events
       
    86     iDbEventQueue->Flush();
       
    87     iViewEventQueue->Flush();
       
    88     }
       
    89 
       
    90 TContactItemId CTestResources::CreateTestContactL
       
    91         (const TDesC& aFamilyName, TBool aWithName)
       
    92     {
       
    93     // Create a contact card
       
    94     CContactCard* card = CContactCard::NewLC();
       
    95     
       
    96 	// Create a name field
       
    97 	if (aWithName)
       
    98 		{
       
    99 		CContactItemField* field = CContactItemField::NewLC(KStorageTypeText,
       
   100 			KUidContactFieldFamilyName);
       
   101 		field->TextStorage()->SetTextL(aFamilyName);
       
   102 		card->AddFieldL(*field);
       
   103 		CleanupStack::Pop(field);
       
   104 		}
       
   105 
       
   106     // Create a phone number field
       
   107 	CContactItemField* field = CContactItemField::NewLC(KStorageTypeText,
       
   108 		KUidContactFieldPhoneNumber);
       
   109     field->TextStorage()->SetTextL(_L("1013"));
       
   110     card->AddFieldL(*field);
       
   111     CleanupStack::Pop(field);
       
   112 
       
   113     // Add the contact to the DB
       
   114     const TContactItemId contactId = iDb->AddNewContactL(*card);
       
   115     CleanupStack::PopAndDestroy(card);
       
   116 
       
   117     // Eat away contact db events
       
   118     TContactDbObserverEvent event;
       
   119     while 
       
   120         (iDbEventQueue->ListenForEvent(10,event) && 
       
   121         (event.iType != EContactDbObserverEventContactAdded ||
       
   122         event.iContactId != contactId))
       
   123         {
       
   124         }
       
   125     iTestContactId = contactId;
       
   126     return contactId;
       
   127     }
       
   128 
       
   129 CTestResources::~CTestResources()
       
   130     {
       
   131     if (iLocalView) iLocalView->Close(*iViewEventQueue);
       
   132     iViewSortOrder.Close();
       
   133     delete iViewEventQueue;
       
   134     delete iDbEventQueue;
       
   135     delete iDb;
       
   136     TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KTestDbName));
       
   137     }
       
   138 
       
   139 LOCAL_C void DoAddContactL(CTestResources* aRes, const TDesC& aFamilyName)
       
   140     {
       
   141 	if (aFamilyName.Compare(KNullDesC) == 0)
       
   142 		{
       
   143 		aRes->CreateTestContactL(aFamilyName, EFalse);
       
   144 		}
       
   145 	else
       
   146 		{
       
   147 		aRes->CreateTestContactL(aFamilyName, ETrue);
       
   148 		}
       
   149     }
       
   150 
       
   151 LOCAL_C void AddContactWithNameL(CTestResources* aRes)
       
   152     {
       
   153     DoAddContactL(aRes, _L("Mulder"));
       
   154     }
       
   155 
       
   156 LOCAL_C void AddContactWithoutNameL(CTestResources* aRes)
       
   157     {
       
   158 	DoAddContactL(aRes, KNullDesC);
       
   159     }
       
   160 
       
   161 // This test passes
       
   162 
       
   163 LOCAL_C void TestRemoveContactWithNameL(CTestResources* aRes)
       
   164     {
       
   165     test.Next(_L("Remove a contact with name"));
       
   166 
       
   167 
       
   168 	// First add the contact which will be soon deleted.
       
   169 	// Let's use name Scully, which will be ordered to
       
   170 	// be the 11th name in the view (there are 10 Mulders and
       
   171 	// 5 unnamed contacts)
       
   172     DoAddContactL(aRes, _L("Scully"));
       
   173 
       
   174 	// Flush the view event queue
       
   175     aRes->iViewEventQueue->Flush();
       
   176 
       
   177     // Now delete the added contact
       
   178     aRes->iDb->DeleteContactL(aRes->iTestContactId);
       
   179 
       
   180     // Then wait for the local view event
       
   181     TContactViewEvent viewEvent;
       
   182     test(aRes->iViewEventQueue->ListenForEvent(10,viewEvent));
       
   183 
       
   184     test(viewEvent.iEventType == TContactViewEvent::EItemRemoved);
       
   185     test(viewEvent.iContactId == aRes->iTestContactId);
       
   186 	// Now the important test: was the deleted contact 11th contact
       
   187 	// in the view (the iInt index of the contact should be 10)?
       
   188     test(viewEvent.iInt == 10);								// <---- success!
       
   189     }
       
   190 
       
   191 // This test fails
       
   192 
       
   193 LOCAL_C void TestRemoveContactWithoutNameL(CTestResources* aRes)
       
   194     {
       
   195     test.Next(_L("Remove a contact without name"));
       
   196 
       
   197 
       
   198 	// First add the contact which will be soon deleted.
       
   199 	// The names list contains now 10 Mulders and 5 unnamed
       
   200 	// contacts. There's no real sort order for unnamed contacts,
       
   201 	// this contact will be 11th - 16th contact in the view,
       
   202 	// because unsorted contacts are placed to the end of the
       
   203 	// view (see CTestResources::ConstructL)
       
   204     DoAddContactL(aRes, KNullDesC);
       
   205 
       
   206 	// Flush the view event queue
       
   207     aRes->iViewEventQueue->Flush();
       
   208 
       
   209     // Now delete the added contact
       
   210     aRes->iDb->DeleteContactL(aRes->iTestContactId);
       
   211 
       
   212     // Then wait for the local view event
       
   213     TContactViewEvent viewEvent;
       
   214     test(aRes->iViewEventQueue->ListenForEvent(10,viewEvent));
       
   215     test(viewEvent.iEventType == TContactViewEvent::EItemRemoved);
       
   216     test(viewEvent.iContactId == aRes->iTestContactId);
       
   217 	// Now the important test: was the deleted contact 11th, 12th,
       
   218 	// 13th, 14th, 15th, or 16th contact in the view? Each one of
       
   219 	// these values is acceptable, since unnamed contacts are not
       
   220 	// sorted but are placed at the end of the view
       
   221 	// (EUnSortedAtEnd preference is in use). This means, that
       
   222 	// the index of the deleted contact should be in range of 10-15.
       
   223     test((viewEvent.iInt >= 10) && (viewEvent.iInt <= 15));		// <---- fail!
       
   224 
       
   225 	// Test fails, because the iInt index is 5. So according to the
       
   226 	// received event the deleted unnamed contact was 6th contact
       
   227 	// in a view which contained 10 named contacts and 6 unnamed
       
   228 	// contacts and where the unnamed contacts were at the end
       
   229 	// of the view (EUnSortedAtEnd). Which is not true, the event
       
   230 	// is wrong.
       
   231     }
       
   232 
       
   233 LOCAL_C void TestUnsortedAtBeginningL(CTestResources* aRes)
       
   234 	{
       
   235 	// Now, switch the unsorted to the beginning, and make sure that we
       
   236 	// get a good response when we remove a name (both sorted and unsorted).
       
   237 	// Create contact view preferences, UNSORTED AT THE BEGINNING!
       
   238 	TContactViewPreferences preferences =  static_cast<TContactViewPreferences>
       
   239         (EContactsOnly | EUnSortedAtBeginning | ESingleWhiteSpaceIsEmptyField);
       
   240     // Create the view
       
   241     if (aRes->iLocalView) aRes->iLocalView->Close(*(aRes->iViewEventQueue));
       
   242     aRes->iLocalView = CContactLocalView::NewL
       
   243         (*(aRes->iViewEventQueue), *(aRes->iDb), aRes->iViewSortOrder, preferences);
       
   244     // Wait for view to get ready
       
   245     TContactViewEvent event;
       
   246     test(aRes->iViewEventQueue->ListenForEvent(10,event));
       
   247     test(event.iEventType == TContactViewEvent::EReady);
       
   248     // Flush all other events
       
   249     aRes->iDbEventQueue->Flush();
       
   250     aRes->iViewEventQueue->Flush();
       
   251 	// Now remove a Mulder.
       
   252     DoAddContactL(aRes, _L("Scully"));
       
   253 
       
   254 	// Flush the view event queue
       
   255     aRes->iViewEventQueue->Flush();
       
   256 
       
   257     // Now delete the added contact
       
   258     aRes->iDb->DeleteContactL(aRes->iTestContactId);
       
   259 
       
   260     // Then wait for the local view event
       
   261     TContactViewEvent viewEvent;
       
   262     test(aRes->iViewEventQueue->ListenForEvent(10,viewEvent));
       
   263     test(viewEvent.iEventType == TContactViewEvent::EItemRemoved);
       
   264     test(viewEvent.iContactId == aRes->iTestContactId);
       
   265     test(viewEvent.iInt > 4);
       
   266 
       
   267 	// Now remove one without a name
       
   268     DoAddContactL(aRes, KNullDesC);
       
   269 
       
   270 	// Flush the view event queue
       
   271     aRes->iViewEventQueue->Flush();
       
   272 
       
   273     // Now delete the added contact
       
   274     aRes->iDb->DeleteContactL(aRes->iTestContactId);
       
   275 
       
   276     // Then wait for the local view event
       
   277     test(aRes->iViewEventQueue->ListenForEvent(10,viewEvent));
       
   278     test(viewEvent.iEventType == TContactViewEvent::EItemRemoved);
       
   279     test(viewEvent.iContactId == aRes->iTestContactId);
       
   280 	}
       
   281 
       
   282 /**
       
   283 
       
   284 @SYMTestCaseID     PIM-T-LOCALVIEWREMOVE-0001
       
   285 
       
   286 */
       
   287 
       
   288 void DoTestsL()
       
   289     {
       
   290 	test.Start(_L("@SYMTESTCaseID:PIM-T-LOCALVIEWREMOVE-0001 T_LocalViewRemove"));
       
   291 
       
   292 
       
   293 	// Create test resources
       
   294     CTestResources* res = CTestResources::NewLC();
       
   295 
       
   296 	// First add 10 contacts with name Mulder
       
   297 	for (TInt i=0; i<10; ++i)
       
   298 	{
       
   299 		AddContactWithNameL(res);
       
   300 	}
       
   301 
       
   302 	// Then add 5 contacts without name
       
   303 	for (TInt j=0; j<5; ++j)
       
   304 	{
       
   305 	    AddContactWithoutNameL(res);
       
   306 	}
       
   307 
       
   308 	// Do the tests
       
   309     TestRemoveContactWithNameL(res);	// passes
       
   310     TestRemoveContactWithoutNameL(res);	// fails
       
   311 	TestUnsortedAtBeginningL(res);
       
   312 
       
   313 	CleanupStack::PopAndDestroy(res);
       
   314 
       
   315     test.End();
       
   316     test.Close(); 
       
   317     }
       
   318 
       
   319 GLDEF_C TInt E32Main()
       
   320 	{
       
   321     // Init
       
   322     __UHEAP_MARK; 
       
   323     CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   324     if (!cleanupStack)
       
   325         {
       
   326         return KErrNoMemory;
       
   327         }
       
   328 
       
   329     CActiveScheduler* activeScheduler = new CActiveScheduler;
       
   330     if (!activeScheduler)
       
   331         {
       
   332         return KErrNoMemory;
       
   333         }
       
   334     CActiveScheduler::Install(activeScheduler);
       
   335 
       
   336     // Run the tests
       
   337     TRAPD(err, DoTestsL());
       
   338 
       
   339     // Cleanup
       
   340     delete activeScheduler;
       
   341     delete cleanupStack;
       
   342     __UHEAP_MARKEND;
       
   343 	return err;
       
   344     }