phonebookengines/contactsmodel/tsrc/t_subview.cpp
branchRCL_3
changeset 20 f4a778e096c2
parent 0 e686773b3f54
equal deleted inserted replaced
19:5b6f26637ad3 20:f4a778e096c2
       
     1 // Copyright (c) 2008-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 // Summary:
       
    15 // A test to exercise the contacts model subview object and associated 
       
    16 // functionality. The test includes creating, resorting and searching a view. 
       
    17 // Actions:
       
    18 // -- Create a new database;
       
    19 // -- Populate database with randomly generated data;
       
    20 // -- Create a local named view sorted by lname, fname, company and telephone;
       
    21 // -- Create a subview based on the local view;
       
    22 // -- Verify the subview has been created properly;
       
    23 // -- Resort the local view;
       
    24 // -- Verify the subview has been updated properly after resort;
       
    25 // -- Clean up.
       
    26 // Associated defect:
       
    27 // DEF125393 PIM Sphinx QP: Improve test coverage of Cntmodel Subviews
       
    28 // Written by:
       
    29 // James Clarke
       
    30 // 
       
    31 //
       
    32 
       
    33 #include "t_subview.h"
       
    34 
       
    35 
       
    36 //
       
    37 // CSubViewTest methods
       
    38 //
       
    39 
       
    40 CSubViewTest* CSubViewTest::NewLC()
       
    41 	{
       
    42 	CSubViewTest* self = new (ELeave) CSubViewTest();
       
    43 	CleanupStack::PushL(self);
       
    44 	self->ConstructL();
       
    45 	return self;
       
    46 	}
       
    47 
       
    48 
       
    49 CSubViewTest::CSubViewTest() :
       
    50 	CActive(CActive::EPriorityStandard)
       
    51 	{
       
    52 	}
       
    53 	
       
    54 	
       
    55 void CSubViewTest::ConstructL()
       
    56 	{
       
    57 	CActiveScheduler::Add(this);
       
    58 
       
    59 	// create a database populated with randomly generated contacts 
       
    60 	iContactsDb = DbCreator::CreateDbL(KDbName, KNumContacts);
       
    61 	}
       
    62 
       
    63 
       
    64 CSubViewTest::~CSubViewTest()
       
    65 	{
       
    66 	Cancel();
       
    67 	Cleanup();
       
    68 	}
       
    69 
       
    70 
       
    71 void CSubViewTest::Cleanup()
       
    72 	{
       
    73 	if (iNamedLocalView)
       
    74 		{
       
    75 		iNamedLocalView->Close(*this);
       
    76 		iNamedLocalView = NULL;
       
    77 		}
       
    78 	if (iSubView)
       
    79 		{
       
    80 		iSubView->Close(*this);
       
    81 		iSubView = NULL;
       
    82 		}
       
    83 	delete iContactsDb;
       
    84 	TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KDbName));
       
    85 	}
       
    86 
       
    87 
       
    88 void CSubViewTest::RunL()
       
    89 	{
       
    90 	switch (iState)
       
    91 		{
       
    92 		case ETestStarted:
       
    93 			CreateNamedLocalViewL();
       
    94 			iState = ENamedLocalViewCreated;
       
    95 			break;
       
    96 		case ENamedLocalViewCreated: 
       
    97 			test.Printf(_L("Local view created"));
       
    98 			CreateSubViewL();
       
    99 			iState = ESubViewCreated;
       
   100 			break;
       
   101 		case ESubViewCreated:
       
   102 			test.Printf(_L("Sub view created"));
       
   103 			PreResortTestL();
       
   104 			ResortNamedLocalViewL();
       
   105 			iState = EResorting;
       
   106 			break;
       
   107 		case ENamedLocalViewResorted: 
       
   108 		case ESubViewUpdated: 
       
   109 			break;
       
   110 		case ESortingComplete:
       
   111 			test.Printf(_L("Local view resorted"));
       
   112 			test.Printf(_L("Sub view updated after local view resort."));
       
   113 			PostResortTestL();
       
   114 			CActiveScheduler::Stop();
       
   115 			break;
       
   116 		default:
       
   117 			test.Printf(_L("Invalid state."));
       
   118 			test(EFalse);
       
   119 		}
       
   120 	}
       
   121 
       
   122 
       
   123 
       
   124 void CSubViewTest::CreateNamedLocalViewL()
       
   125 	{
       
   126 	test.Next(_L("Creating local view."));
       
   127 
       
   128 	
       
   129 	RContactViewSortOrder sortOrder;
       
   130 	CleanupClosePushL(sortOrder);
       
   131 	sortOrder.AppendL(KUidContactFieldFamilyName);
       
   132 	sortOrder.AppendL(KUidContactFieldGivenName);
       
   133 	sortOrder.AppendL(KUidContactFieldCompanyName);
       
   134 	sortOrder.AppendL(KUidContactFieldPhoneNumber);
       
   135 
       
   136 	// set the field indexes to reflect the order above
       
   137 	iLnameIdx = 0;
       
   138 	iFnameIdx = 1;
       
   139 	iCnameIdx = 2;
       
   140 	iNumIdx   = 3;
       
   141 	
       
   142 	iNamedLocalView = CContactNamedLocalView::NewL(*this, KTestName(), *iContactsDb, sortOrder, EContactsOnly);
       
   143 	
       
   144 	CleanupStack::PopAndDestroy(&sortOrder);
       
   145 	}
       
   146 
       
   147 
       
   148 void CSubViewTest::DoCancel()
       
   149 	{
       
   150 	}
       
   151 
       
   152 
       
   153 void CSubViewTest::CreateSubViewL()
       
   154 	{
       
   155 	test.Next(_L("Creating subview."));
       
   156 
       
   157 
       
   158 	iSubView = CContactSubView::NewL(*iNamedLocalView, *iContactsDb, *this, KLowBoundary(), KHighBoundary());
       
   159 	}
       
   160 
       
   161 
       
   162 void CSubViewTest::HandleContactViewEvent(const CContactViewBase& aView, const TContactViewEvent& aEvent)
       
   163 	{
       
   164 
       
   165 	if (aEvent.iEventType == TContactViewEvent::ESortOrderChanged)
       
   166 		{
       
   167 			if (iState == EResorting)
       
   168 				{
       
   169 				if (&aView == iNamedLocalView)
       
   170 					{
       
   171 					iState = ENamedLocalViewResorted;
       
   172 					}
       
   173 				else
       
   174 					{
       
   175 					iState = ESubViewUpdated; 
       
   176 					}
       
   177 				}
       
   178 			else if (iState == ENamedLocalViewResorted || iState == ESubViewUpdated)
       
   179 				{
       
   180 				iState = ESortingComplete;
       
   181 				}
       
   182 			else
       
   183 				{
       
   184 				test.Printf(_L("Ooops! Something's gone wrong with the view-resorting states..."));
       
   185 				test(EFalse);
       
   186 				}
       
   187 		}
       
   188 
       
   189 	if (aEvent.iEventType == TContactViewEvent::EReady || iState == ESortingComplete)
       
   190 		{
       
   191 		TRequestStatus* pStat = &iStatus;
       
   192 	  	User::RequestComplete(pStat, KErrNone);
       
   193 	   	SetActive();
       
   194 		}
       
   195 	}
       
   196 
       
   197 
       
   198 void CSubViewTest::PrintSubViewL() const
       
   199 	{
       
   200 	const TInt KLocalCount(iNamedLocalView->CountL());
       
   201 	const TInt KSubCount(iSubView->CountL());
       
   202 	test.Printf(_L("Subview contains %d of the %d contacts in the NamedLocalView."), KSubCount, KLocalCount);
       
   203 	
       
   204 	for(TInt i = 0; i < KSubCount; ++i)
       
   205 		{
       
   206 		const TPtrC fname = iSubView->ContactAtL(i).Field(iFnameIdx);
       
   207 		const TPtrC lname = iSubView->ContactAtL(i).Field(iLnameIdx);
       
   208 		const TPtrC cname = iSubView->ContactAtL(i).Field(iCnameIdx);
       
   209 		const TPtrC num = iSubView->ContactAtL(i).Field(iNumIdx);
       
   210 		test.Printf(_L("Contact #%d -- Person: %S %S; Company: %S; Phone number: %S."), 
       
   211 			i + 1, &fname, &lname, &cname, &num);
       
   212 		}
       
   213 	}
       
   214 
       
   215 
       
   216 void CSubViewTest::PreResortTestL() const
       
   217 	{
       
   218 	test.Next(_L("Verifying subview is correct after creation."));
       
   219 
       
   220 	SubAndLocalViewCompareTestL();
       
   221 	CheckSortOrderL();
       
   222 	}
       
   223 
       
   224 void CSubViewTest::PostResortTestL() const
       
   225 	{
       
   226 	test.Next(_L("Verifying subview is correct after resorting underlying view."));
       
   227 
       
   228 	SubAndLocalViewCompareTestL();
       
   229 	CheckSortOrderL();
       
   230 	}
       
   231 
       
   232 
       
   233 void CSubViewTest::SubAndLocalViewCompareTestL() const
       
   234 	{
       
   235 	test.Printf(_L("Testing subview is accurate compared to the local view."));
       
   236 
       
   237 	const TInt KSubViewCount = iSubView->CountL();
       
   238 	const TInt KLocalViewCount = iNamedLocalView->CountL();
       
   239 	const TInt KFirstFieldIdx(0);
       
   240 	
       
   241 	TInt subViewIdx(0);
       
   242 	TBool foundFirstInSubView(EFalse);
       
   243 	TBool foundLastInSubView(EFalse);
       
   244 	
       
   245 	TInt subCntItemId(iSubView->AtL(subViewIdx));
       
   246 	TInt localCntItemId(-1); // initialise with a value that can't be a real Id 
       
   247 	for(TInt localViewIdx = 0; localViewIdx < KLocalViewCount && !foundLastInSubView; ++localViewIdx)
       
   248 		{
       
   249 		localCntItemId = iNamedLocalView->AtL(localViewIdx);
       
   250 
       
   251 		// already found the first item in the sub view within
       
   252 		// the underlying local view.
       
   253 		if (foundFirstInSubView)
       
   254 			{
       
   255 			++subViewIdx;
       
   256 			subCntItemId = iSubView->AtL(subViewIdx);
       
   257 			// check that the contact ids in the sub view and local view match
       
   258 			if (subCntItemId != localCntItemId)
       
   259 				{
       
   260 				test.Printf(_L("Mismatch of contacts between subview and local view"));
       
   261 				test(EFalse);
       
   262 				}
       
   263 
       
   264 			// if we've found the last sub view item, do boundary checking to 
       
   265 			// ensure we have the right things from the local view in the sub view. 
       
   266 			if (subViewIdx == KSubViewCount - 1)
       
   267 				{
       
   268 				foundLastInSubView = ETrue;
       
   269 
       
   270 				// Haven't checked localViewIdx < KLocalViewCount but it's not
       
   271 				// likely that the last contact in the sub view will legitimately 
       
   272 				// be the same as the last one in the local view.    
       
   273 				const TPtrC nextLocalName = iNamedLocalView->ContactAtL(localViewIdx + 1).Field(KFirstFieldIdx);
       
   274 				const TPtrC subName = iSubView->ContactAtL(subViewIdx).Field(KFirstFieldIdx);
       
   275 
       
   276 				// check that the first character of the last contact in the subview
       
   277 				// is within the proper range and that the next contact in the
       
   278 				// local view is outside the range.
       
   279 				test.Printf(_L("Found last contact from subview in local view. Testing for errors."));
       
   280 				test(subName.Left(1).CompareF(KHighChar) <= 0 ||
       
   281 						nextLocalName.Left(1).CompareF(KHighChar) > 0);
       
   282 				test.Printf(_L("-- check okay."));
       
   283 				}
       
   284 			}
       
   285 
       
   286 		// we've found the first contact item from the sub view in the local
       
   287 		// view. check 
       
   288 		else if (localCntItemId == subCntItemId && !foundFirstInSubView)
       
   289 			{
       
   290 			foundFirstInSubView = ETrue;
       
   291 
       
   292 			// Haven't checked localViewIdx != 0 but it is not likely that the 
       
   293 			// first contact in the local view will legitimately be the same as 
       
   294 			// the first one in the sub view.
       
   295 			const TPtrC prevLocalName = iNamedLocalView->ContactAtL(localViewIdx - 1).Field(KFirstFieldIdx);
       
   296 			const TPtrC subName = iSubView->ContactAtL(subViewIdx).Field(KFirstFieldIdx);
       
   297 						
       
   298 			// check that the first character of the first contact in the subview
       
   299 			// is within the proper range and that the previous contact in the
       
   300 			// local view is outside the range.
       
   301 			test.Printf(_L("Found first contact from subview in local view. Testing for errors."));
       
   302 			test(subName.Left(1).CompareF(KLowChar) >= 0 &&
       
   303 					prevLocalName.Left(1).CompareF(KLowChar) < 0);
       
   304 			test.Printf(_L("-- check okay."));
       
   305 			
       
   306 			// check that we can find an expected contact item in the subview
       
   307 			// and cannot find something that should not be there.
       
   308 			test.Printf(_L("Testing FindL() functionality."));
       
   309 			test(iSubView->FindL(subCntItemId) == subViewIdx &&
       
   310 					iSubView->FindL(iNamedLocalView->AtL(localViewIdx - 1)) == KErrNotFound);
       
   311 			test.Printf(_L("-- check okay."));
       
   312 			
       
   313 			// check the AllFieldsLC functionality
       
   314 			test.Printf(_L("Testing AllFieldsLC() functionality."));
       
   315 			
       
   316 			_LIT(KDelimiter, ";");
       
   317 			HBufC* allFields = iSubView->AllFieldsLC(subViewIdx, KDelimiter());
       
   318 			TPtrC allFieldsPtr = allFields->Des();
       
   319 
       
   320 			TBufC<16> field0 = iSubView->ContactAtL(subViewIdx).Field(0);
       
   321 			TBufC<16> field1 = iSubView->ContactAtL(subViewIdx).Field(1);
       
   322 			TBufC<16> field2 = iSubView->ContactAtL(subViewIdx).Field(2);
       
   323 			TBufC<16> field3 = iSubView->ContactAtL(subViewIdx).Field(3);
       
   324 			
       
   325 			TBuf<48> fieldsBuf;
       
   326 			_LIT(KFormat, "%S;%S;%S;%S");
       
   327 			fieldsBuf.AppendFormat(KFormat(), &field0, &field1, &field2, &field3);
       
   328 			
       
   329 			test(fieldsBuf.Compare(allFieldsPtr) == 0); // they should be the same
       
   330 			test.Printf(_L("-- check okay."));
       
   331 			CleanupStack::PopAndDestroy(allFields);
       
   332 			}
       
   333 		}
       
   334 	}
       
   335 
       
   336 
       
   337 void CSubViewTest::CheckSortOrderL() const
       
   338 	{
       
   339 	test.Printf(_L("Checking sort order"));
       
   340 	RContactViewSortOrder sortOrder = iSubView->SortOrderL(); // don't take ownership
       
   341 	test(
       
   342 			sortOrder[iLnameIdx] == KUidContactFieldFamilyName &&
       
   343 			sortOrder[iFnameIdx] == KUidContactFieldGivenName &&
       
   344 			sortOrder[iCnameIdx] == KUidContactFieldCompanyName &&
       
   345 			sortOrder[iNumIdx]   == KUidContactFieldPhoneNumber &&
       
   346 			iSubView->ContactViewPreferences() == EContactsOnly
       
   347 		);
       
   348 	}
       
   349 
       
   350 
       
   351 void CSubViewTest::ResortNamedLocalViewL()
       
   352 	{
       
   353 	test.Next(_L("Resorting local view."));
       
   354 
       
   355 
       
   356 	RContactViewSortOrder sortOrder;
       
   357 	CleanupClosePushL(sortOrder);
       
   358 	sortOrder.AppendL(KUidContactFieldCompanyName);
       
   359 	sortOrder.AppendL(KUidContactFieldGivenName);
       
   360 	sortOrder.AppendL(KUidContactFieldFamilyName);
       
   361 	sortOrder.AppendL(KUidContactFieldPhoneNumber);
       
   362 
       
   363 	// set the field indexes to reflect the order above
       
   364 	iCnameIdx = 0;
       
   365 	iFnameIdx = 1;
       
   366 	iLnameIdx = 2;
       
   367 	iNumIdx   = 3;
       
   368 	
       
   369 	iNamedLocalView->ChangeSortOrderL(sortOrder);
       
   370 
       
   371 	CleanupStack::PopAndDestroy(&sortOrder);
       
   372 	}
       
   373 
       
   374 
       
   375 void CSubViewTest::DoTestL()
       
   376 	{
       
   377 	TRequestStatus* status = &iStatus;
       
   378 	User::RequestComplete(status, KErrNone);
       
   379 	SetActive();
       
   380 	iState = ETestStarted;
       
   381 	CActiveScheduler::Start();
       
   382 	}
       
   383 
       
   384 
       
   385 //
       
   386 // main test functions
       
   387 //
       
   388 
       
   389 /**
       
   390 
       
   391 @SYMTestCaseID     PIM-T-SUBVIEW-0001
       
   392 
       
   393 */
       
   394 
       
   395 void DoTestsL()
       
   396     {
       
   397 	test.Start(_L("@SYMTESTCaseID:PIM-T-SUBVIEW-0001 "));
       
   398 
       
   399 	CSubViewTest* myTest = CSubViewTest::NewLC();
       
   400 	myTest->DoTestL();
       
   401 
       
   402 	// do some stuff here...
       
   403 
       
   404 	CleanupStack::PopAndDestroy(myTest);
       
   405     test.End();
       
   406     test.Close();
       
   407     }
       
   408 
       
   409 GLDEF_C TInt E32Main()
       
   410 	{
       
   411     // Init
       
   412     __UHEAP_MARK; 
       
   413     CTrapCleanup* cleanupStack = CTrapCleanup::New();
       
   414     if (!cleanupStack)
       
   415         {
       
   416         return KErrNoMemory;
       
   417         }
       
   418 
       
   419     CActiveScheduler* activeScheduler = new CActiveScheduler;
       
   420     if (!activeScheduler)
       
   421         {
       
   422         return KErrNoMemory;
       
   423         }
       
   424     CActiveScheduler::Install(activeScheduler);
       
   425 
       
   426     // Run the tests
       
   427     TRAPD(err, DoTestsL());
       
   428     if (err)
       
   429     	{
       
   430     	TRAP_IGNORE(CContactDatabase::DeleteDatabaseL(KDbName) );
       
   431     	test(EFalse);
       
   432     	}
       
   433     
       
   434     // Cleanup
       
   435     delete activeScheduler;
       
   436     delete cleanupStack;
       
   437     __UHEAP_MARKEND;
       
   438 	return err;
       
   439     }