phonebookengines_old/contactsmodel/tsrc/t_speeddialtest.cpp
changeset 40 b46a585f6909
equal deleted inserted replaced
37:fd64c38c277d 40:b46a585f6909
       
     1 // Copyright (c) 2000-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 <e32std.h>
       
    17 #include <e32test.h>
       
    18 #include <cntdb.h>
       
    19 #include <cntitem.h>
       
    20 #include <cntfldst.h>
       
    21 #include <cntview.h>
       
    22 #include "t_utils2.h"
       
    23 #include <coreappstest/testserver.h>
       
    24 #include "t_speeddialtest.h"
       
    25 
       
    26 //
       
    27 // Constants.
       
    28 //
       
    29 _LIT(KTestName,"T_SpeedDialTest");
       
    30 
       
    31 // Configure database locations
       
    32 _LIT(KDbFileName,"c:t_speeddialtest.cdb");
       
    33 _LIT(KLongNumSpeedDialDbName, "c:LongNumSpeedDialDb.cdb");
       
    34 
       
    35 // Configure .ini file location
       
    36 _LIT(KContactsModelIniFile,"C:\\private\\10003A73\\CntModel.ini");
       
    37 
       
    38 
       
    39 const TInt KNumSmsContacts=5;
       
    40 const TInt KSpeedDialPositionOne = 1;
       
    41 const TInt KSpeedDialPositionTwo  = 2;
       
    42 const TInt KSpeedDialContact = 3;
       
    43 const TInt KSpeedDialContactA = 4;
       
    44 const TInt KSpeedDialContactB = 5;
       
    45 
       
    46 
       
    47 //
       
    48 // Global Data
       
    49 //
       
    50 RTest g_test( KTestName );
       
    51 
       
    52 //
       
    53 // Class CLockServerTester Implementation
       
    54 //
       
    55 
       
    56 CLockServerTester* CLockServerTester::NewL()
       
    57 	{
       
    58 	CLockServerTester* self=new(ELeave) CLockServerTester();
       
    59 	CleanupStack::PushL(self);
       
    60 	self->ConstructL();
       
    61 	CleanupStack::Pop();
       
    62 	return self;
       
    63 	}
       
    64  
       
    65 CLockServerTester::~CLockServerTester()
       
    66  	{
       
    67  	DeleteProcess();
       
    68  	}
       
    69  
       
    70 CLockServerTester::CLockServerTester()
       
    71  	{
       
    72  	}
       
    73  	
       
    74 void CLockServerTester::ConstructL()
       
    75  	{
       
    76  	}
       
    77  
       
    78 void CLockServerTester::FindServerAndKillItL()
       
    79  	{
       
    80  	SetupProcessL();
       
    81  	OpenAndKillProcess();
       
    82  	}
       
    83  	
       
    84 void CLockServerTester::OpenAndKillProcess()
       
    85 	{
       
    86 	iProcess.iExecution->Open(iProcessFullName);
       
    87 	iProcess.iExecution->Kill(KErrNone);
       
    88 	}
       
    89 
       
    90 void CLockServerTester::SetupProcessL()
       
    91 	{
       
    92 	iProcess.iFind = new (ELeave) TFindProcess(_L("cntsrv*"));
       
    93 	iProcess.iExecution = new (ELeave) RProcess();
       
    94 	iProcess.iFind->Next(iProcessFullName);
       
    95 	}
       
    96 
       
    97 void CLockServerTester::DeleteProcess()
       
    98 	{
       
    99 	delete iProcess.iFind;
       
   100 	if (iProcess.iExecution != NULL) iProcess.iExecution->Close();
       
   101 	delete iProcess.iExecution;
       
   102    
       
   103 	iProcess.iFind = NULL;
       
   104 	iProcess.iExecution = NULL;
       
   105 	}
       
   106 
       
   107 //
       
   108 // Function Implementation
       
   109 //
       
   110 TBool ContactIsASpeedDial(const CContactItem& aItem)
       
   111 	{
       
   112 	TBool retval = EFalse;
       
   113 	CContactItemFieldSet& fields = aItem.CardFields();
       
   114 	const TInt max = fields.Count();
       
   115 	TInt counter = 0;
       
   116 
       
   117 	for (counter = 0; counter < max; counter++)
       
   118 		{
       
   119 		retval = fields[counter].IsSpeedDial();
       
   120 		if (retval) break;
       
   121 		}
       
   122 
       
   123 	return retval;
       
   124 	}
       
   125 	
       
   126 
       
   127 TBool ContactIsASpeedDialL(TInt aContactId, CContactDatabase& aDatabase)
       
   128     {
       
   129     CContactItem* item = NULL;
       
   130     TBool result = EFalse;
       
   131     
       
   132     item = aDatabase.OpenContactL(aContactId);
       
   133     CleanupStack::PushL(item);
       
   134     
       
   135     const CContactItemFieldSet& fields = item->CardFields();
       
   136     const TInt count = fields.Count();
       
   137     
       
   138     for (TInt index = 0; index < count; ++index)
       
   139         {
       
   140         if (fields[index].IsSpeedDial())
       
   141             {
       
   142             result = ETrue;
       
   143             break;
       
   144             }
       
   145         }
       
   146     aDatabase.CloseContactL(aContactId);        
       
   147        
       
   148     CleanupStack::PopAndDestroy(item);        
       
   149     return result;    
       
   150     }
       
   151 
       
   152 
       
   153 TInt DeleteIniFile()
       
   154 	{
       
   155 	TInt retval = KErrNone;
       
   156 	//TInt deleteSuccess;
       
   157 
       
   158 	// Delete any existing ini file so that we can be sure this test is ok
       
   159 
       
   160     RPIMTestServer serv;
       
   161     User::LeaveIfError(serv.Connect());
       
   162     
       
   163     TRAPD (deleteSuccess, serv.DeleteFileL(KContactsModelIniFile));
       
   164     
       
   165     if ( deleteSuccess != KErrNotFound && deleteSuccess != KErrNone )
       
   166     {
       
   167 		  retval = deleteSuccess;
       
   168 	}
       
   169 		
       
   170 	return retval;
       
   171 	}
       
   172 
       
   173 void AddContactsL(CRandomContactGenerator& aGenerator)
       
   174 	{
       
   175 	TInt ii;
       
   176 	for (ii=0;ii<KNumSmsContacts;ii++)
       
   177 		{
       
   178 		TInt bit=0;
       
   179 		bit |= CContactDatabase::ESmsable;
       
   180 		aGenerator.AddTypicalContactForFilterL(bit);
       
   181 		}
       
   182 	}
       
   183 
       
   184 CContactDatabase* ReplaceDatabaseAndCreateContactsLC()
       
   185 	{
       
   186 	CContactDatabase* database = CContactDatabase::ReplaceL(KDbFileName);
       
   187 	CleanupStack::PushL(database);
       
   188 
       
   189 	CreateContactsL(*database);
       
   190 
       
   191 	return database;
       
   192 	}
       
   193 
       
   194 void CreateContactsL(CContactDatabase& aDb)
       
   195 	{
       
   196 	CRandomContactGenerator* generator = CRandomContactGenerator::NewL();
       
   197 	CleanupStack::PushL( generator );
       
   198 	generator->SetDbL(aDb);
       
   199 	AddContactsL(*generator);
       
   200 	CleanupStack::PopAndDestroy( generator );
       
   201 	}
       
   202 
       
   203 CContactDatabase* SetupLC()
       
   204 	{
       
   205 	User::LeaveIfError( DeleteIniFile() );
       
   206 	return ReplaceDatabaseAndCreateContactsLC();
       
   207 	}
       
   208 
       
   209 
       
   210 void AssignSpeedDialL( TInt aContact, TInt aSpeedDialID, CContactDatabase& database )
       
   211 	{
       
   212 	CContactItem* item = NULL;
       
   213 	item = database.OpenContactL( aContact );
       
   214 	
       
   215 	CleanupStack::PushL( item );
       
   216 
       
   217 	if ( ContactIsASpeedDial( *item ) )
       
   218 		{
       
   219 		g_test.Printf( _L("Contact is a speed dial !!! \n") );
       
   220 		}
       
   221 	else
       
   222 		{
       
   223 		g_test.Printf( _L("Contact is NOT A SPEED DIAL \n") );
       
   224 		}
       
   225 
       
   226 	CContactItemFieldSet &fieldSet = item->CardFields();
       
   227     for(TInt index = fieldSet.Count() - 1; index > 0; --index)
       
   228         {
       
   229         if(fieldSet[index].StorageType() == KStorageTypeText)
       
   230             {
       
   231             //set the last text field from the fieldset as speeddial field.
       
   232         	database.SetFieldAsSpeedDialL(*item, index, aSpeedDialID );
       
   233             break;
       
   234             }
       
   235         }
       
   236         
       
   237 	g_test.Next( _L("Contact changed to speed dial") );
       
   238 	g_test(ContactIsASpeedDial( *item ));
       
   239 
       
   240 	CleanupStack::PopAndDestroy( item );
       
   241 	}
       
   242 
       
   243 void UpdateSpeedDialL(TInt aContact, TInt aSpeedDialID, CContactDatabase& database )
       
   244 	{
       
   245 	CContactItem* item = NULL;
       
   246 	item = database.OpenContactL( aContact );
       
   247 	CleanupStack::PushL( item );
       
   248 
       
   249 	if ( ContactIsASpeedDial( *item ) )
       
   250 		{
       
   251 		g_test.Printf( _L("Contact is a speed dial !!! \n") );
       
   252 		database.SetFieldAsSpeedDialL(*item, item->Id(), aSpeedDialID );
       
   253 		}
       
   254 	else
       
   255 		{
       
   256 		g_test.Printf( _L("Contact is NOT A SPEED DIAL \n") );
       
   257 		}
       
   258 
       
   259 	CleanupStack::PopAndDestroy( item );
       
   260 	}
       
   261 		
       
   262 void RemoveSpeedDialL(TInt aContact, TInt aSpeedDialId, CContactDatabase& aDatabase)
       
   263     {    
       
   264     TInt error = KErrNone;
       
   265         
       
   266     TRAP(error, aDatabase.RemoveSpeedDialFieldL(aContact, aSpeedDialId));
       
   267     g_test(error == KErrNone);           
       
   268     }    
       
   269 
       
   270 void RemoveSpeedDialWhenOpenL(TInt aContact, TInt aSpeedDialID, CContactDatabase& aDatabase)
       
   271 	{
       
   272 	CContactItem* card1 = 0;
       
   273 	TInt error = KErrNone;
       
   274 
       
   275 	// First open the contact, which locks it:
       
   276 	card1 = aDatabase.OpenContactL(aContact);
       
   277 	CleanupStack::PushL(card1);
       
   278 
       
   279 	// Now attempt to open the contact (must fail with in use):
       
   280 	error = KErrNone;
       
   281 	TRAP(error, aDatabase.OpenContactL(aContact));
       
   282 	g_test(error == KErrInUse);
       
   283 
       
   284 	// Now remove the speed dial on that contact (must fail with in use):
       
   285 	error = KErrNone;
       
   286 	TRAP(error, aDatabase.RemoveSpeedDialFieldL(aContact, aSpeedDialID));
       
   287 	g_test(error == KErrInUse);
       
   288 	// Now remove the speed dial on that contact again (must fail with in use):
       
   289 	// Defect was application crash.
       
   290 	error = KErrNone;
       
   291 	TRAP(error, aDatabase.RemoveSpeedDialFieldL(aContact, aSpeedDialID));
       
   292 	g_test(error == KErrInUse);
       
   293 
       
   294 	// Now attempt to open the contact (must fail with in use):
       
   295 	// Defect was fails with wrong error/no error.
       
   296 	error = KErrNone;
       
   297 	TRAP(error, aDatabase.OpenContactL(aContact));
       
   298 	g_test(error == KErrInUse);
       
   299 
       
   300 	// Cleanup and close:
       
   301 	CleanupStack::PopAndDestroy(card1);
       
   302 	TRAP(error, aDatabase.CloseContactL(aContact));
       
   303 	g_test(error == KErrNone);
       
   304 	}
       
   305 
       
   306 void TestDatabaseWithSpeedDialsL()
       
   307 	{
       
   308 	g_test.Next(_L("Setting up..."));
       
   309 
       
   310 	CContactDatabase* database = SetupLC();
       
   311 
       
   312 	g_test.Next(_L("First Speed dial assignment"));
       
   313 	// Assign First time ok !
       
   314 	TRAPD(error, AssignSpeedDialL( KSpeedDialContact, KSpeedDialPositionOne, *database ) );
       
   315 	g_test(error == KErrNone);
       
   316 
       
   317 	g_test.Next(_L("Second Speed dial assignment"));
       
   318 	// Assign Second time falls over !
       
   319 	TRAP(error, AssignSpeedDialL( KSpeedDialContact, KSpeedDialPositionOne, *database ) );
       
   320 	g_test(error == KErrNone);
       
   321 
       
   322 	g_test.Next(_L("Open contact, remove from speed dial, open again"));
       
   323 	// The remove from speed dial on the open contact should return KErrInUse.
       
   324 	// The second open should also return KErrInUse.
       
   325 	TRAP(error, RemoveSpeedDialWhenOpenL(KSpeedDialContact, KSpeedDialPositionOne, *database));
       
   326 	g_test(error == KErrNone);
       
   327 
       
   328 	CleanupStack::PopAndDestroy( database );
       
   329 
       
   330 	// wait until Contacts Lock Server shuts down
       
   331 	User::After(6000000);
       
   332 
       
   333 	g_test.Next( _L("Try to open db again"));
       
   334 
       
   335 	database =  CContactDatabase::OpenL(KDbFileName);
       
   336 	CleanupStack::PushL(database);
       
   337 	
       
   338 	CContactItem* item = database->ReadContactL(KSpeedDialContact);
       
   339 	CleanupStack::PushL(item);
       
   340 	
       
   341 	g_test.Next( _L("Check that Speed dial is remembered"));
       
   342 	g_test(item->Id() == KSpeedDialContact);
       
   343 	// retored item should be a SpeedDial
       
   344 	g_test(ContactIsASpeedDial( *item ));
       
   345 
       
   346 	CleanupStack::PopAndDestroy(item);
       
   347 	CleanupStack::PopAndDestroy(database);
       
   348 	}
       
   349 
       
   350 /**
       
   351 
       
   352 @SYMTestCaseID PIM-T-SPEEDDIALTEST-0001
       
   353 
       
   354 */
       
   355 
       
   356 void TestOneL()
       
   357 	{
       
   358 	g_test.Start(_L("@SYMTestCaseID:PIM-T-SPEEDDIALTEST-0001 Create Database with Speed Dials then Delete and Create database"));
       
   359 
       
   360 	TestDatabaseWithSpeedDialsL();
       
   361 
       
   362 	g_test.Next(_L("Delete database"));
       
   363 	CContactDatabase::DeleteDatabaseL(KDbFileName);
       
   364 	
       
   365 	g_test.Next( _L("open database should fail"));
       
   366 	CContactDatabase* database = NULL;
       
   367 
       
   368 	TRAPD(openError, database =  CContactDatabase::OpenL(KDbFileName));
       
   369 	g_test(openError == KErrNotFound && database == NULL);
       
   370 	
       
   371 
       
   372 	g_test.Next( _L("create replacement database"));
       
   373 	database =  CContactDatabase::CreateL(KDbFileName);
       
   374 	CleanupStack::PushL(database);
       
   375 
       
   376 	CreateContactsL(*database);
       
   377 
       
   378 	CContactItem* item = database->ReadContactL(KSpeedDialContact);
       
   379 	CleanupStack::PushL(item);
       
   380 	
       
   381 	g_test.Next( _L("Check that Speed dial is forgotten"));
       
   382 	g_test(item->Id() == KSpeedDialContact);
       
   383 	// retored item should not be a SpeedDial
       
   384 	g_test(!ContactIsASpeedDial( *item ));
       
   385 
       
   386 	CleanupStack::PopAndDestroy(item);
       
   387 	CleanupStack::PopAndDestroy(database);
       
   388 
       
   389 	g_test.End();
       
   390 	}
       
   391 
       
   392 void TestTwoL()
       
   393 	{
       
   394 	g_test.Start(_L("Create Database with Speed Dials then Replace database"));
       
   395 
       
   396 	TestDatabaseWithSpeedDialsL();
       
   397 
       
   398 	g_test.Next(_L("Replace database"));
       
   399 	CContactDatabase* database = ReplaceDatabaseAndCreateContactsLC();
       
   400 
       
   401 	CContactItem* item = database->ReadContactL(KSpeedDialContact );	
       
   402 	CleanupStack::PushL(item);
       
   403 	
       
   404 	g_test.Next(_L("Check that Speed dial is forgotten"));
       
   405 	g_test(item->Id() == KSpeedDialContact);
       
   406 	// retored item should not be a SpeedDial
       
   407 	g_test(!ContactIsASpeedDial( *item ));
       
   408 
       
   409 	CleanupStack::PopAndDestroy(item);
       
   410 	CleanupStack::PopAndDestroy(database);
       
   411 
       
   412 	g_test.End();
       
   413 	}
       
   414 
       
   415 void TestThreeL()
       
   416 	{
       
   417 	g_test.Start(_L("Check that Settings are saved - normal shutdown"));
       
   418 	
       
   419     g_test.Next(_L("Setting up..."));
       
   420 	CContactDatabase* database = SetupLC();
       
   421 
       
   422 	g_test.Next( _L("Assign Speed Dial"));
       
   423 	TInt error = KErrNone;
       
   424 	TRAP(error, AssignSpeedDialL( KSpeedDialContact, KSpeedDialPositionOne, *database ) );
       
   425 	g_test(error == KErrNone);
       
   426 	
       
   427 	g_test.Next( _L("Update Speed Dial"));
       
   428 	error = KErrNone;
       
   429 	TRAP(error, UpdateSpeedDialL( KSpeedDialContact, KSpeedDialPositionTwo, *database ) );
       
   430 	g_test(error == KErrNone);
       
   431 	
       
   432 	g_test.Next( _L("Change Database Drive"));
       
   433 	CContactDatabase::SetDatabaseDriveL(TDriveUnit(EDriveD),EFalse);
       
   434 	
       
   435 	CleanupStack::PopAndDestroy( database );
       
   436 
       
   437 	g_test.Next(_L("Wait for Contact Server to shutdown"));
       
   438 	// wait until Contacts Lock Server shuts down
       
   439 	// applicabale only in the case of transient server.
       
   440 	User::After(6000000);
       
   441 	
       
   442 	g_test.Next( _L("Try to open Database again"));
       
   443 
       
   444 	database =  CContactDatabase::OpenL(KDbFileName);
       
   445 	CleanupStack::PushL(database);
       
   446 		
       
   447 	TDriveUnit driveUnit;
       
   448 	CContactDatabase::DatabaseDrive(driveUnit);
       
   449 	g_test.Next( _L("Check that Database Drive is changed"));
       
   450 	g_test(driveUnit==TDriveUnit(EDriveD));
       
   451 	
       
   452 	CContactItem* item = database->ReadContactL(KSpeedDialContact);
       
   453 	CleanupStack::PushL(item);
       
   454 	
       
   455 	g_test.Next( _L("Check that Speed Dial is remembered"));
       
   456 	g_test(item->Id() == KSpeedDialContact);
       
   457 	// retored item should be a SpeedDial
       
   458 	g_test(ContactIsASpeedDial( *item ));
       
   459 	
       
   460 	// reset Database Drive
       
   461 	g_test.Next( _L("Reset Database Drive setting"));
       
   462 	CContactDatabase::SetDatabaseDriveL(TDriveUnit(EDriveC),EFalse);
       
   463 
       
   464 	CleanupStack::PopAndDestroy(item);
       
   465 	CleanupStack::PopAndDestroy(database);
       
   466 	
       
   467 	g_test.End();
       
   468 	}
       
   469 
       
   470 void TestFourL()
       
   471 	{
       
   472 	g_test.Start(_L("Check that Settings are saved - killing ContactLockSrv"));
       
   473 	
       
   474     g_test.Next(_L("Setting up..."));
       
   475 	CLockServerTester* lockSrvTester = CLockServerTester::NewL();
       
   476 	CleanupStack::PushL(lockSrvTester);
       
   477 	CContactDatabase* database = SetupLC();
       
   478 	
       
   479 	g_test.Next( _L("Assign Speed Dial"));
       
   480 	TInt error = KErrNone;
       
   481 	TRAP(error, AssignSpeedDialL( KSpeedDialContact, KSpeedDialPositionOne, *database ) );
       
   482 	g_test(error == KErrNone);
       
   483 	
       
   484 	g_test.Next( _L("Update Speed Dial"));
       
   485 	error = KErrNone;
       
   486 	TRAP(error, UpdateSpeedDialL( KSpeedDialContact, KSpeedDialPositionTwo, *database ) );
       
   487 	g_test(error == KErrNone);
       
   488 	
       
   489 	g_test.Next( _L("Change Database Drive"));
       
   490 	CContactDatabase::SetDatabaseDriveL(TDriveUnit(EDriveD),EFalse);
       
   491 	
       
   492 	CleanupStack::PopAndDestroy(database);
       
   493 	
       
   494 	// Kill ContactLockServer process
       
   495 	g_test.Next( _L("Kill CntServer"));
       
   496 	lockSrvTester->FindServerAndKillItL();
       
   497 	CleanupStack::PopAndDestroy( lockSrvTester );
       
   498 
       
   499 	g_test.Next( _L("Open Database"));
       
   500 	database =  CContactDatabase::OpenL(KDbFileName);
       
   501 	CleanupStack::PushL(database);
       
   502 		
       
   503 	TDriveUnit driveUnit;
       
   504 	CContactDatabase::DatabaseDrive(driveUnit);
       
   505 	g_test.Next( _L("Check that Database Drive is changed"));
       
   506 	g_test(driveUnit==TDriveUnit(EDriveD));
       
   507 	
       
   508 	CContactItem* item = database->ReadContactL(KSpeedDialContact);
       
   509 	CleanupStack::PushL(item);
       
   510 	
       
   511 	g_test.Next( _L("Check that Speed Dial is remembered"));
       
   512 	g_test(item->Id() == KSpeedDialContact);
       
   513 	// retored item should be a SpeedDial
       
   514 	g_test(ContactIsASpeedDial( *item ));
       
   515 	
       
   516 	// reset Database Drive
       
   517 	g_test.Next( _L("Reset Database Drive setting"));
       
   518 	CContactDatabase::SetDatabaseDriveL(TDriveUnit(EDriveC),EFalse);
       
   519 
       
   520 	CleanupStack::PopAndDestroy(item);
       
   521 	CleanupStack::PopAndDestroy(database);
       
   522 	
       
   523 	g_test.End();		
       
   524 	}
       
   525 	
       
   526 	
       
   527 /**
       
   528 @SYMTestCaseID PIM-T-SPEEDDIALTEST-INC097895-0001
       
   529 @SYMTestType UT
       
   530 @SYMTestPriority High
       
   531 @SYMDEF INC097895
       
   532 @SYMTestCaseDesc Pbk2 AIW: Speed dial is removed from incorrect entry 
       
   533  
       
   534 @SYMTestActions
       
   535 1. Add speed dials for contact A and B
       
   536 2. Try to reset speed dial belonging to A by using B's id
       
   537 3. Reset speed dials using the correct contacts
       
   538  
       
   539 @SYMTestExpectedResults Speed dial is still active when reset is attempted by wrong contact
       
   540 */	
       
   541 void TestFiveL()
       
   542     {
       
   543     g_test.Start(_L("@SYMTestCaseID:PIM-T-SPEEDDIALTEST-INC097895-0001 Checking that the correct contact resets the speed dial number"));
       
   544 	
       
   545    	g_test.Next(_L("Setting up..."));
       
   546 	CContactDatabase* database = SetupLC();
       
   547 
       
   548 	g_test.Next(_L("First Speed dial assignment"));	
       
   549 	TRAPD(error, AssignSpeedDialL( KSpeedDialContactA, KSpeedDialPositionOne, *database ) );
       
   550 	g_test(error == KErrNone);
       
   551 	
       
   552 	// Check that a speed dial is assigned
       
   553 	g_test(ContactIsASpeedDialL(KSpeedDialContactA, *database));
       
   554 
       
   555 	g_test.Next(_L("Second Speed dial assignment"));	
       
   556 	TRAP(error, AssignSpeedDialL( KSpeedDialContactB, KSpeedDialPositionTwo, *database ) );
       
   557 	g_test(error == KErrNone);
       
   558 	
       
   559 	// Check that a speed dial is assigned
       
   560 	g_test(ContactIsASpeedDialL(KSpeedDialContactB, *database));
       
   561 		
       
   562 	// Here, the wrong contact (B) is trying to reset the speed dial number
       
   563 	// which is set by contact A. 
       
   564 	g_test.Next(_L("Wrong contact - should be ignored"));	
       
   565 	TRAP(error, RemoveSpeedDialL(KSpeedDialContactB, KSpeedDialPositionOne, *database));
       
   566 	g_test(error == KErrNone);
       
   567 		
       
   568 	g_test.Next(_L("The first speed dial is still active"));	
       
   569 	g_test(ContactIsASpeedDialL(KSpeedDialContactA, *database));		
       
   570 	
       
   571 	g_test.Next(_L("and the second speed dial is still active"));	
       
   572 	g_test(ContactIsASpeedDialL(KSpeedDialContactB, *database));				
       
   573 	
       
   574 	g_test.Next(_L("Reset allowed by the correct contact"));	
       
   575 	TRAP(error, RemoveSpeedDialL(KSpeedDialContactA, KSpeedDialPositionOne, *database));
       
   576 	g_test(error == KErrNone);
       
   577 	
       
   578 	g_test.Next(_L("Check speed dial One has been reset"));	
       
   579 	g_test(!ContactIsASpeedDialL(KSpeedDialContactA, *database));		
       
   580 					
       
   581 	TRAP(error, RemoveSpeedDialL(KSpeedDialContactB, KSpeedDialPositionTwo, *database));
       
   582 	g_test(error == KErrNone);
       
   583 	
       
   584 	g_test.Next(_L("Check speed dial Two has been reset"));	
       
   585 	g_test(!ContactIsASpeedDialL(KSpeedDialContactB, *database));		
       
   586 	
       
   587 	CleanupStack::PopAndDestroy(database);	
       
   588 	
       
   589 	g_test.End();		
       
   590     }
       
   591 
       
   592 
       
   593 LOCAL_C void LongNumSpeedDialTestsL()
       
   594     {
       
   595 	_LIT(KLongNumSpeedDialTest, "Long Phone Number Speed Dial Test");
       
   596 	g_test.Start(KLongNumSpeedDialTest);
       
   597 
       
   598 	// create default, empty database
       
   599 	CContactDatabase* db = CContactDatabase::ReplaceL(KLongNumSpeedDialDbName);
       
   600 	CleanupStack::PushL(db);
       
   601 	
       
   602 	// create a contact and add it to the db
       
   603 	CContactItem* contact = CContactCard::NewLC();
       
   604 	CContactItemField* fname = CContactItemField::NewLC(KStorageTypeText, 
       
   605 			KUidContactFieldGivenName);
       
   606 	_LIT(KFname, "James");
       
   607 	fname->TextStorage()->SetTextL(KFname() );
       
   608 	contact->AddFieldL(*fname);
       
   609 	CleanupStack::Pop(fname);
       
   610 	CContactItemField* phone = CContactItemField::NewLC(KStorageTypeText,
       
   611 			KUidContactFieldPhoneNumber);
       
   612 	_LIT(KPhoneNum,	"01234567890123456789012345678901234567890123456789012345678901234567890123456789"); // 80 chars
       
   613 	phone->TextStorage()->SetTextL(KPhoneNum() );
       
   614 	contact->AddFieldL(*phone);
       
   615 	CleanupStack::Pop(phone);
       
   616 	const TContactItemId KContactId = db->AddNewContactL(*contact);
       
   617 	CleanupStack::PopAndDestroy(contact);
       
   618 	contact = NULL;
       
   619 
       
   620 	// retrieve contact and assign its number to speed dial #1
       
   621 	contact = db->OpenContactL(KContactId);
       
   622 	CleanupStack::PushL(contact);
       
   623 	const TInt KSpeedDial1(1);
       
   624 	const TInt KPhoneNumIndex = contact->CardFields().Find(KUidContactFieldPhoneNumber);
       
   625 	db->SetFieldAsSpeedDialL(*contact, KPhoneNumIndex, KSpeedDial1);
       
   626 	TBuf<100> speedDialNumberText;
       
   627 	TContactItemId speedDialId = db->GetSpeedDialFieldL(KSpeedDial1, speedDialNumberText);
       
   628 	_LIT(KOutputFormat, "retrieved speed dial id: %d;\nretrieved speed dial phone number: ...\n%S\n");
       
   629 	g_test.Printf(KOutputFormat, speedDialId, &speedDialNumberText);
       
   630 	db->CloseContactL(KContactId);
       
   631 	
       
   632 	// cleanup
       
   633 	CleanupStack::PopAndDestroy(2, db);	// and contact
       
   634 	CContactDatabase::DeleteDatabaseL(KLongNumSpeedDialDbName);
       
   635 
       
   636 	// Since PDEF121954, long phone numbers set as speed dial are truncated to 
       
   637 	// the length of KSpeedDialPhoneLength. So, we need to get the truncated 
       
   638 	// version of the phone number for comparison with the speed dial value.
       
   639 	TPtrC phoneNum(KPhoneNum().Mid(0, KSpeedDialPhoneLength));
       
   640 	g_test(speedDialId == KContactId && speedDialNumberText.CompareC(phoneNum) == 0);
       
   641 	g_test.End();
       
   642 	g_test.Close();
       
   643     }
       
   644 
       
   645 
       
   646 
       
   647 void DoTestL()
       
   648 	{
       
   649 	CTestRegister * TempFiles = CTestRegister::NewLC();
       
   650 	TempFiles->RegisterL(KDbFileName, EFileTypeCnt);
       
   651 	TempFiles->RegisterL(KLongNumSpeedDialDbName, EFileTypeCnt);
       
   652 	TempFiles->RegisterL(KContactsModelIniFile);
       
   653 	
       
   654 	TestOneL();
       
   655 
       
   656 	g_test.Next(_L(""));
       
   657 	TestTwoL();
       
   658 
       
   659 	// Added for DEF075241
       
   660 	g_test.Next( _L(""));
       
   661 	TestThreeL();
       
   662 
       
   663 	g_test.Next( _L(""));
       
   664 	TestFourL();
       
   665 	// End of tests for DEF075241
       
   666 		
       
   667 	// INC097895 start
       
   668 	g_test.Next(_L(""));
       
   669 	TestFiveL();
       
   670 	// INC097895 end
       
   671 
       
   672 	// PDEF121954 start
       
   673 	g_test.Next(_L(""));
       
   674 	LongNumSpeedDialTestsL();
       
   675 	// PDEF121954 end
       
   676 	
       
   677 	// Delete cntmodel.ini
       
   678 	User::LeaveIfError( DeleteIniFile() );	
       
   679 	CleanupStack::PopAndDestroy(TempFiles);
       
   680 
       
   681 	g_test.End();
       
   682 	}
       
   683 
       
   684 
       
   685 //
       
   686 // Main.
       
   687 //
       
   688 
       
   689 GLDEF_C TInt E32Main()
       
   690 	{
       
   691 	__UHEAP_MARK;
       
   692 	g_test.Start(_L("Speed dial test"));
       
   693 
       
   694 	CActiveScheduler* scheduler=new CActiveScheduler;
       
   695 	if (scheduler)
       
   696 		{
       
   697 		CActiveScheduler::Install(scheduler);
       
   698 		CTrapCleanup* cleanup=CTrapCleanup::New();
       
   699 		if (cleanup)
       
   700 			{
       
   701 			TRAPD(err,DoTestL() );
       
   702 			g_test(err == KErrNone);
       
   703 			delete cleanup;
       
   704 			}
       
   705 		delete scheduler;
       
   706 		}
       
   707 	g_test.Close();
       
   708 	__UHEAP_MARKEND;
       
   709 	return KErrNone;
       
   710     }