phonebookengines_old/contactsmodel/tsrc/t_contactdbevent.cpp
changeset 40 b46a585f6909
equal deleted inserted replaced
37:fd64c38c277d 40:b46a585f6909
       
     1 // Copyright (c) 1997-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 <cntdb.h>
       
    18 #include <cntitem.h>
       
    19 #include <cntfield.h>
       
    20 #include <cntfilt.h>
       
    21 #include <cntfldst.h>
       
    22 
       
    23 RTest test(_L("t_contactdbevent"));
       
    24 
       
    25 _LIT(KEventDbFileName,"c:t_contactdbevent.cdb");
       
    26 const TInt KTimeMicroSec = 1000000;
       
    27 
       
    28 
       
    29 class CPbObserver : public CTimer, public MContactDbObserver 
       
    30 	{
       
    31 public:
       
    32 	static CPbObserver* NewL(TUint aConnectionId);
       
    33 	void ConstructL();
       
    34 	~CPbObserver(){}
       
    35 public:
       
    36 	//From MContactDbObserver
       
    37 	void HandleDatabaseEventL(TContactDbObserverEvent aEvent);
       
    38 protected:
       
    39 	CPbObserver(TUint aConnectionId);
       
    40 	void RunL();
       
    41 protected:
       
    42 	TUint iPbConnectionId;	
       
    43 	};
       
    44 
       
    45 class CPbTester : public CBase 
       
    46 	{
       
    47 public:
       
    48 	static CPbTester* NewL();
       
    49 	~CPbTester();
       
    50 	void StartTestL();
       
    51 protected:
       
    52 	void ConstructL();
       
    53 	TContactItemId AddEntryL();
       
    54 	void EditEntryL(TContactItemId itemId);
       
    55 
       
    56 protected:
       
    57 	CPbObserver* ipbObSvr;
       
    58 	CContactDatabase* ipbDb;
       
    59 	CContactChangeNotifier* ipbNotifier;	
       
    60 	};
       
    61 
       
    62 CPbObserver* CPbObserver::NewL(TUint aConnectionId)
       
    63 	{
       
    64 	CPbObserver* self = new (ELeave) CPbObserver(aConnectionId);
       
    65 	CleanupStack::PushL(self);
       
    66 	self->ConstructL();
       
    67 	CleanupStack::Pop();
       
    68 	return self;
       
    69 	}
       
    70 
       
    71 void CPbObserver::ConstructL()
       
    72 	{
       
    73 	CTimer::ConstructL();
       
    74 	}
       
    75 
       
    76 void CPbObserver::HandleDatabaseEventL(TContactDbObserverEvent aEvent)
       
    77 	{
       
    78 	if (aEvent.iType == EContactDbObserverEventContactChanged)
       
    79 		{
       
    80 		test.Printf(_L("Received a Contact changed event"));
       
    81 		}
       
    82 	if (aEvent.iType == EContactDbObserverEventContactDeleted)
       
    83 		{
       
    84 		test.Printf(_L("Received a Contact delete event"));
       
    85 		}
       
    86 	if (aEvent.iType == EContactDbObserverEventContactAdded) 
       
    87 		{
       
    88 		test.Printf(_L("Received a Contact added event"));
       
    89 		}
       
    90 	test (aEvent.iConnectionId == iPbConnectionId);
       
    91 	}
       
    92 
       
    93 
       
    94 CPbObserver::CPbObserver(TUint aConnectionId): CTimer(EPriorityStandard), 
       
    95                                                iPbConnectionId(aConnectionId)
       
    96 	{
       
    97 	CActiveScheduler::Add(this);
       
    98 	}
       
    99 
       
   100 void CPbObserver::RunL()
       
   101 	{
       
   102 	CActiveScheduler::Stop();
       
   103 	}
       
   104 
       
   105 CPbTester* CPbTester::NewL(void)
       
   106 	{
       
   107 	CPbTester *self = new(ELeave)CPbTester;
       
   108 	CleanupStack::PushL(self);
       
   109 	self->ConstructL();
       
   110 	CleanupStack::Pop();
       
   111 	return self;
       
   112 	}
       
   113 
       
   114 void CPbTester::ConstructL(void)
       
   115 	{
       
   116 	ipbDb=CContactDatabase::ReplaceL(KEventDbFileName);
       
   117 	ipbObSvr = CPbObserver::NewL(ipbDb->ConnectionId());
       
   118 	ipbNotifier = CContactChangeNotifier::NewL(*ipbDb, ipbObSvr);
       
   119 	}
       
   120 
       
   121 CPbTester::~CPbTester(void)
       
   122 	{
       
   123 	TRAP_IGNORE(ipbDb->DeleteDatabaseL(KEventDbFileName));
       
   124 	delete ipbNotifier;
       
   125 	delete ipbObSvr;
       
   126 	delete ipbDb;
       
   127 	}
       
   128 
       
   129 void CPbTester::StartTestL()
       
   130 	{
       
   131 	TContactItemId id = AddEntryL();
       
   132 	EditEntryL(id);
       
   133 	ipbObSvr->After(KTimeMicroSec); //Allow CContactChangeNotifier to run
       
   134 	CActiveScheduler::Start();
       
   135 	}
       
   136 
       
   137 TContactItemId CPbTester::AddEntryL()
       
   138 	{
       
   139 	_LIT(KForename,"Myname"); 
       
   140 	_LIT(KSurname,"Mylastname"); 
       
   141 	_LIT(KPhoneNumber,"+447700900700"); 
       
   142 
       
   143 	// Create a  contact card to contain the data
       
   144 	CContactCard* newCard = CContactCard::NewLC();
       
   145 
       
   146 	// Create the firstName field and add the data to it
       
   147 	CContactItemField* firstName = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldGivenName);
       
   148 	firstName->TextStorage()->SetTextL(KForename);
       
   149 	newCard->AddFieldL(*firstName);
       
   150 	CleanupStack::Pop(firstName);
       
   151 
       
   152 	// Create the lastName field and add the data to it
       
   153 	CContactItemField* lastName= CContactItemField::NewLC(KStorageTypeText, KUidContactFieldFamilyName);
       
   154 	lastName ->TextStorage()->SetTextL(KSurname);
       
   155 	newCard->AddFieldL(*lastName);
       
   156 	CleanupStack::Pop(lastName);
       
   157 
       
   158 	// Create the phoneNo field and add the data to it
       
   159 	CContactItemField* phoneNumber = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldPhoneNumber);
       
   160 	phoneNumber->SetMapping(KUidContactFieldVCardMapTEL);
       
   161 	phoneNumber ->TextStorage()->SetTextL(KPhoneNumber);
       
   162 	newCard->AddFieldL(*phoneNumber);
       
   163 	CleanupStack::Pop(phoneNumber);
       
   164 
       
   165 	// Add newCard to the database
       
   166 	const TContactItemId contactId = ipbDb->AddNewContactL(*newCard);
       
   167 
       
   168 	CleanupStack::PopAndDestroy(newCard);
       
   169 
       
   170 	return contactId;
       
   171 	}
       
   172 
       
   173 void CPbTester::EditEntryL(TContactItemId itemId)
       
   174 	{
       
   175 	_LIT(KEmailAddress,"myname.mylastname@symbianfoundation.test"); 
       
   176 	CContactItem *item = ipbDb->OpenContactL(itemId);
       
   177 	CContactCard* card = NULL;
       
   178 	CleanupStack::PushL(item);
       
   179 
       
   180 	card = (CContactCard*)item;
       
   181 	// Create the emailAddress field and add the data to it
       
   182 	CContactItemField* emailAddr = CContactItemField::NewLC(KStorageTypeText, KUidContactFieldEMail);
       
   183 	emailAddr->SetMapping(KUidContactFieldVCardMapEMAILINTERNET);
       
   184 	emailAddr ->TextStorage()->SetTextL(KEmailAddress);
       
   185 	card->AddFieldL(*emailAddr);
       
   186 	CleanupStack::Pop(emailAddr);
       
   187 
       
   188 	ipbDb->CommitContactL(*item);
       
   189 	CleanupStack::PopAndDestroy(item);
       
   190 	}
       
   191 
       
   192 void RunTestL()
       
   193 	{
       
   194 	CPbTester* tester = CPbTester::NewL();
       
   195 	CleanupStack::PushL(tester);
       
   196 	tester->StartTestL();
       
   197 	CleanupStack::PopAndDestroy(tester);
       
   198 	}
       
   199 
       
   200 /**
       
   201 
       
   202 @SYMTestCaseID     PIM-T-CONTACTDBEVENT-0001
       
   203 
       
   204 */
       
   205 
       
   206 GLDEF_C TInt E32Main()
       
   207 	{
       
   208 	__UHEAP_MARK;
       
   209 	CActiveScheduler* scheduler=new CActiveScheduler;
       
   210 	CActiveScheduler::Install(scheduler);
       
   211 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
   212 	test.Start(_L("@SYMTESTCaseID:PIM-T-CONTACTDBEVENT-0001 Testing Database event"));
       
   213 
       
   214 	TRAPD(err,RunTestL());
       
   215 	test(err==KErrNone);
       
   216 	test.End();
       
   217 	test.Close();
       
   218 	delete cleanup;
       
   219     delete scheduler;
       
   220 	__UHEAP_MARKEND;
       
   221 	return KErrNone;
       
   222 	}