phonebookengines_old/contactsmodel/tsrc/T_DBASE.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 <f32file.h>
       
    18 #include <s32file.h>
       
    19 #include <cntdb.h>
       
    20 #include <cntitem.h>
       
    21 #include <cntfield.h>
       
    22 #include <cntfldst.h>
       
    23 #include "T_UTILS.H"
       
    24 
       
    25 CCntTest* CntTest=NULL;
       
    26 LOCAL_D RTest test(_L("T_DBASE"));
       
    27 
       
    28 const TPtrC KDatabaseFileName=_L("C:T_DBASE.cdb");
       
    29 
       
    30 const TInt KNumTestContacts=20;
       
    31 
       
    32 LOCAL_C void CreateDatabaseL()
       
    33 //
       
    34 // Create a database in a store
       
    35 //
       
    36 	{
       
    37 	CntTest->TestLockServerCloses();
       
    38 	CntTest->CreateDatabaseL();
       
    39 	CntTest->TestLockServerExists();
       
    40 	CntTest->DeleteAllTemplateFieldsL();
       
    41 	CntTest->CloseDatabase();
       
    42 	CntTest->TestLockServerCloses();
       
    43 	}
       
    44 	
       
    45 LOCAL_C void OpenDatabaseL()
       
    46 //
       
    47 // Open an existing database in a store
       
    48 //
       
    49 	{
       
    50 	CContactDatabase* db=CContactDatabase::OpenL(KDatabaseFileName);
       
    51 	CntTest->TestLockServerExists();
       
    52 	delete db;
       
    53 	CntTest->TestLockServerCloses();
       
    54 	}
       
    55 
       
    56 LOCAL_C TPtrC Name(CContactItem& aItem)
       
    57 	{
       
    58 	CContactItemFieldSet& fieldSet=aItem.CardFields();
       
    59 	const TInt pos=fieldSet.Find(KUidContactFieldFamilyName);
       
    60 	if (pos==KErrNotFound)
       
    61 		return _L("");
       
    62 	return fieldSet[pos].TextStorage()->Text();
       
    63 	}
       
    64 
       
    65 LOCAL_C void SetDriveErrMsg()
       
    66 	{
       
    67 	test.Printf(_L("Error: For this test to run properly "));
       
    68 #if defined(__WINS__)
       
    69 	test.Printf(_L("_EPOC_DRIVE_D must be set up to create a valid drive D:\n"));
       
    70 #else
       
    71 	test.Printf(_L("a writable D: must exist\n"));
       
    72 #endif
       
    73 	test.Getch();
       
    74 	}
       
    75 
       
    76 LOCAL_C void TestDriveSettingL()
       
    77 	{
       
    78 	RFs fs;
       
    79 	test(fs.Connect()==KErrNone);
       
    80 
       
    81 	TDriveInfo driveInfo;
       
    82 	if (fs.Drive(driveInfo,EDriveD)!=KErrNone || driveInfo.iType==EMediaNotPresent || driveInfo.iMediaAtt&KMediaAttWriteProtected)
       
    83 		SetDriveErrMsg();
       
    84 	else
       
    85 		{
       
    86 		TInt err=fs.MkDirAll(_L("D:\\___t___\\"));
       
    87 		if (err!=KErrNone)
       
    88 			SetDriveErrMsg();
       
    89 		else
       
    90 			{
       
    91 			fs.RmDir(_L("D:\\___t___\\"));
       
    92 			TUint att;
       
    93 			TFileName name;
       
    94 			CContactDatabase::GetDefaultNameL(name);
       
    95 			if (fs.Att(name,att)==KErrNotFound)
       
    96 				{
       
    97 				CContactDatabase *createDb=CContactDatabase::CreateL();
       
    98 				delete createDb;
       
    99 				}
       
   100 			TDriveUnit driveUnit;
       
   101 			CContactDatabase::DatabaseDrive(driveUnit);
       
   102 			if (driveUnit!=TDriveUnit(EDriveC))
       
   103 				CContactDatabase::SetDatabaseDriveL(TDriveUnit(EDriveC));
       
   104 		//
       
   105 		// NEEDS _EPOC_DRIVE_D SET TO POINT TO A VALID DIRECTORY NAME
       
   106 		//
       
   107 			CContactDatabase::SetDatabaseDriveL(TDriveUnit(EDriveD));
       
   108 			test(CContactDatabase::DatabaseDrive(driveUnit));
       
   109 			test(driveUnit==TDriveUnit(EDriveD));
       
   110 		// _EPOC_DRIVE_P Must not exist for the following test
       
   111 			TRAPD(err,CContactDatabase::SetDatabaseDriveL(TDriveUnit(EDriveP)));
       
   112 			test(err!=KErrNone);
       
   113 			test(CContactDatabase::DatabaseDrive(driveUnit));
       
   114 			test(driveUnit==TDriveUnit(EDriveD));
       
   115 		//
       
   116 			CContactDatabase::SetDatabaseDriveL(TDriveUnit(EDriveC));
       
   117 			test(CContactDatabase::DatabaseDrive(driveUnit));
       
   118 			test(driveUnit==TDriveUnit(EDriveC));
       
   119 
       
   120 			//
       
   121 			TDriveUnit cColon(EDriveC);
       
   122 			CContactDatabase::SetDatabaseDriveL(cColon);	// Leave it set to C: for further tests
       
   123 			test(CContactDatabase::DatabaseDrive(driveUnit));
       
   124 			test(driveUnit==cColon);
       
   125 			}
       
   126 		}
       
   127 	fs.Close();
       
   128 	CntTest->TestLockServerCloses();
       
   129 	}
       
   130 
       
   131 LOCAL_C void TestDeleteFileL()
       
   132 	{
       
   133 	TFileName defaultName;
       
   134 	CContactDatabase::GetDefaultNameL(defaultName);
       
   135 	test(CntTest->Fs().SetAtt(defaultName,0,KEntryAttReadOnly)==KErrNone);
       
   136 	CntTest->TestLockServerCloses();
       
   137 	CContactDatabase *db=CContactDatabase::ReplaceL();
       
   138 	delete db;
       
   139 	CntTest->TestLockServerCloses();
       
   140 	TUint att;
       
   141 	test(CntTest->Fs().Att(defaultName,att)==KErrNone);
       
   142 	CContactDatabase::DeleteDefaultFileL();
       
   143 	test(CntTest->Fs().Att(defaultName,att)==KErrNotFound);
       
   144 //
       
   145 	db=CContactDatabase::CreateL();
       
   146 	delete db;
       
   147 	test(CntTest->Fs().Att(defaultName,att)==KErrNone);
       
   148 	test(CntTest->Fs().SetAtt(defaultName,KEntryAttReadOnly,0)==KErrNone);
       
   149 	TRAPD(readOnlyErr,CContactDatabase::DeleteDefaultFileL());
       
   150 	test(readOnlyErr==KErrAccessDenied);
       
   151 	test(CntTest->Fs().SetAtt(defaultName,0,KEntryAttReadOnly)==KErrNone);
       
   152 	CContactDatabase::DeleteDefaultFileL();
       
   153 	}
       
   154 
       
   155 LOCAL_C void AddNewContactsL(CArrayFix<TContactItemId>* aIdList)
       
   156 //
       
   157 // Add KNumTestContacts new contacts with varying numbres of text fields
       
   158 //
       
   159 	{
       
   160 	CntTest->TestLockServerCloses();
       
   161 	CContactDatabase* db=CntTest->OpenDatabaseL();
       
   162 	CntTest->TestLockServerExists();
       
   163 	CContactCard* card=CContactCard::NewL();
       
   164 	CleanupStack::PushL(card);
       
   165 	TContactItemId id=KNullContactId;
       
   166 	TContactItemId newId=KNullContactId;
       
   167 	for (TInt ii=0;ii<KNumTestContacts;ii++)
       
   168 		{
       
   169 		TBuf<16> name;
       
   170 		name.Format(_L("NAME #%d"),ii);
       
   171 		SetNameL(*card,KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN,name,EFalse);
       
   172 		CContactItemField* field=CContactItemField::NewLC(KStorageTypeText);
       
   173 		card->AddFieldL(*field);
       
   174 		CleanupStack::Pop(); // field
       
   175 		newId=db->AddNewContactL(*card);
       
   176 		id=newId;
       
   177 		aIdList->AppendL(id);
       
   178 		}
       
   179 	CleanupStack::PopAndDestroy(); // card
       
   180 	CntTest->CloseDatabase();
       
   181 	CntTest->TestLockServerCloses();
       
   182 	}
       
   183 
       
   184 LOCAL_C void EditContactsL(CArrayFix<TContactItemId>* aIdList)
       
   185 //
       
   186 // Check then edit contact names
       
   187 //
       
   188 	{
       
   189 	CContactDatabase* db=CntTest->OpenDatabaseL();
       
   190 	TInt ii=0;
       
   191 	for (;ii<KNumTestContacts;ii++)
       
   192 		{
       
   193 		const TInt index=(*aIdList)[ii];
       
   194 		CContactItem* item=db->OpenContactL(index);
       
   195 		CleanupStack::PushL(item);
       
   196 		TBuf<16> name;
       
   197 		name.Format(_L("NAME #%d"),ii);
       
   198 		test(name==Name(*item));
       
   199 		name.Format(_L("NEW NAME #%d"),index);
       
   200 		SetNameL(*item,KUidContactFieldFamilyName,KUidContactFieldVCardMapUnusedN,name,EFalse);
       
   201 		db->CommitContactL(*item);
       
   202 		CleanupStack::PopAndDestroy(); // item;
       
   203 		}
       
   204 	for (ii=0;ii<KNumTestContacts;ii++)
       
   205 		{
       
   206 		const TInt index=(*aIdList)[ii];
       
   207 		CContactItem* item=db->ReadContactL(index);
       
   208 		TBuf<16> name;
       
   209 		name.Format(_L("NEW NAME #%d"),index);
       
   210 		test(name==Name(*item));
       
   211 		delete item;
       
   212 		}
       
   213 	for (ii=0;ii<KNumTestContacts;ii++)
       
   214 		{
       
   215 		const TInt index=(*aIdList)[ii];
       
   216 		CContactItem* item=db->OpenContactLX(index);
       
   217 		delete item;
       
   218 		CleanupStack::PopAndDestroy(); // close
       
   219 		}
       
   220 
       
   221 	// try to read a non-existant contact
       
   222 	TRAPD(err,db->ReadContactL(KNullContactId));
       
   223 	test(err!=KErrNone);
       
   224 	// try to edit a contact that hasn't been opened
       
   225 	CContactItem* item=db->ReadContactLC((*aIdList)[0]);
       
   226 	TRAP(err,db->CommitContactL(*item));
       
   227 	CleanupStack::PopAndDestroy(); // item
       
   228 	test(err==KErrAccessDenied);
       
   229 	// try to close a non-existant contact
       
   230 	TRAP(err,db->CloseContactL(KNullContactId));
       
   231 	test(err==KErrNone); // test changed from 001 (using DBMS 043) - this test makes more sense anyway
       
   232 	// try to close a contact that hasn't been opened
       
   233 	TRAP(err,db->CloseContactL((*aIdList)[0]));
       
   234 	test(err==KErrNone);
       
   235 	
       
   236 	CntTest->CloseDatabase();
       
   237 	}
       
   238 
       
   239 LOCAL_C void DeleteContactsL(CArrayFix<TContactItemId>* aIdList)
       
   240 //
       
   241 // Delete all contacts
       
   242 //
       
   243 	{
       
   244 	CContactDatabase* db=CntTest->OpenDatabaseL();
       
   245 
       
   246 	// try to delete a contact that is open
       
   247 	const TContactItemId id=(*aIdList)[0];
       
   248 	CContactItem* item=db->OpenContactL(id);
       
   249 	CleanupStack::PushL(item);
       
   250 	TRAPD(err,db->DeleteContactL(id));
       
   251 
       
   252 	CleanupStack::PopAndDestroy(); // item
       
   253 	test(err==KErrInUse);
       
   254 	db->CloseContactL(id);
       
   255 	// delete all contacts
       
   256 	for (TInt ii=0;ii<KNumTestContacts;ii++)
       
   257 		{
       
   258 		const TInt index=(*aIdList)[ii];
       
   259 		db->DeleteContactL(index);
       
   260 		}
       
   261 	// try to delete a non-existant contact
       
   262 	TRAP(err,db->DeleteContactL(KNullContactId));
       
   263 	test(err!=KErrNone);
       
   264 	CntTest->CloseDatabase();
       
   265 	}
       
   266 
       
   267 void TestErr(TInt aErr)
       
   268     {
       
   269 	if (aErr!=KErrNone)
       
   270 		{
       
   271 		test.Printf(_L("Error %d\n"),aErr);
       
   272 		test(EFalse);
       
   273 		}
       
   274 	}
       
   275 	
       
   276 /**
       
   277 
       
   278 @SYMTestCaseID     PIM-T-DBASE-0001
       
   279 
       
   280 */
       
   281 
       
   282 
       
   283 void DoTestsL()
       
   284     {
       
   285 	test.Start(_L("@SYMTESTCaseID:PIM-T-DBASE-0001 T_DBASE"));
       
   286 
       
   287 	test.Next(_L("Test drive setting"));
       
   288 
       
   289 	test.Printf(_L("The following tests will delete your default contacts file"));
       
   290 	test.Printf(_L("Press to 'd' to run the test or any other key to skip them\n"));
       
   291 	if (test.Getch()=='d')
       
   292 		{
       
   293 		test.Next(_L("Set Drive"));
       
   294 
       
   295 		TRAPD(ret,TestDriveSettingL());
       
   296 		TestErr(ret);
       
   297 		test.Next(_L("Delete database file"));
       
   298 
       
   299 		TRAP(ret,TestDeleteFileL());
       
   300 		TestErr(ret);
       
   301 		}
       
   302 	test.Next(_L("Create new database"));
       
   303 
       
   304 	TRAPD(ret,CreateDatabaseL());
       
   305 	TestErr(ret);
       
   306 	test.Next(_L("Re-open existing database"));
       
   307 
       
   308 	TRAP(ret,OpenDatabaseL());
       
   309 	TestErr(ret);
       
   310 	CArrayFix<TContactItemId>* idList=new(ELeave) CArrayFixFlat<TContactItemId>(5);
       
   311 	CleanupStack::PushL(idList);
       
   312 	test.Next(_L("Add new contacts"));
       
   313 
       
   314 	TRAP(ret,AddNewContactsL(idList));
       
   315 	TestErr(ret);
       
   316 	test.Next(_L("Edit contacts"));
       
   317 
       
   318 	TRAP(ret,EditContactsL(idList));
       
   319 	TestErr(ret);
       
   320 	test.Next(_L("Delete contacts"));
       
   321 
       
   322 	TRAP(ret,DeleteContactsL(idList));
       
   323 	CleanupStack::PopAndDestroy(); // idList
       
   324 	TestErr(ret);
       
   325 	test.Next(_L("Delete empty database"));
       
   326 
       
   327 	CntTest->DeleteDatabaseL();
       
   328     }
       
   329 
       
   330 GLDEF_C TInt E32Main()
       
   331 	{
       
   332 	__UHEAP_MARK;
       
   333 	
       
   334     CntTest=new(ELeave) CCntTest;
       
   335 	CntTest->ConstructL(test,KDatabaseFileName);
       
   336     TRAPD(err,DoTestsL());
       
   337 	CntTest->EndTestLib(err);
       
   338 	
       
   339 	__UHEAP_MARKEND;
       
   340 	
       
   341 	return KErrNone;
       
   342     }