void CDummyCalendarApp::DeleteAllTodosL()
	{
	delete iTestLib;
	iTestLib = NULL;
	
	TBuf<60> dtBuf;
	iTestLib = CCalTestLibrary::NewL();

	TRAPD(err, iTestLib->PIMTestServer().DeleteFileL(KCalendarFile2));
	if(err != KErrNotFound || err != KErrPathNotFound)
		{
		User::LeaveIfError(err);	
		}
	iTestLib->PIMTestServer().CopyFileL(KOriginalDeleteCalendar, _L("c:\\private\\10003A5B\\tcal_delete_calendar_copy"));
	iTestLib->RegisterCalFileL(KCalendarFile2);
	iTestLib->OpenFileL(KCalendarFile2);
	iTestLib->PIMTestServer().SetTimeZoneL(_L8("Europe/Helsinki"));
	
	TCalTime minTime;
	minTime.SetTimeUtcL(TCalTime::MinTime());
	TCalTime maxTime;
	maxTime.SetTimeUtcL(TCalTime::MaxTime());
	CalCommon::TCalTimeRange timeRange(minTime, maxTime);
	CalCommon::TCalViewFilter filter = CalCommon::EIncludeAll;
		
	//DeleteL has to be placed in the end of the function
	//because it is a long running operation and it needs
	//to wait for callback in order to go to the next step.
	iTestLib->SynCGetEntryViewL().DeleteL(timeRange, filter, *this);
	}
TEUCHOS_UNIT_TEST( Rythmos_TimeRange, oo ) {
  TimeRange<double> tr = timeRange(1.25,3.45);
  TEST_EQUALITY_CONST( isInRange_oo(tr, 1.25), false );
  TEST_EQUALITY_CONST( isInRange_oo(tr, 2.0), true );
  TEST_EQUALITY_CONST( isInRange_oo(tr, 3.45), false );
  TimeRange_oo<double> tr_oo(tr);
  TEST_EQUALITY_CONST( tr_oo.isInRange(1.25), false );
  TEST_EQUALITY_CONST( tr_oo.isInRange(2.0), true );
  TEST_EQUALITY_CONST( tr_oo.isInRange(3.45), false );
}
TEUCHOS_UNIT_TEST( Rythmos_TimeRange, cc ) {
  TimeRange<double> tr = timeRange(1.25,3.45);
  TEST_EQUALITY_CONST( isInRange_cc(tr, 1.25), true );
  TEST_EQUALITY_CONST( isInRange_cc(tr, 2.0), true );
  TEST_EQUALITY_CONST( isInRange_cc(tr, 3.45), true );
  TimeRange_cc<double> tr_cc(tr);
  TEST_EQUALITY_CONST( tr_cc.isInRange(1.25), true );
  TEST_EQUALITY_CONST( tr_cc.isInRange(2.0), true );
  TEST_EQUALITY_CONST( tr_cc.isInRange(3.45), true );

}
void CDummyCalendarApp::DeleteAllMemosBeginL()
	{
	test.Printf(_L("Test: DeleteAllMemosBeginL\r\n"));
	//Add 1000 entries
	test.Printf(_L("Adding 1000 Memos...\r\n"));
	
	const TInt KNumEntriesToAdd(1000);
	
	iTestLib->OpenFileL(KCalendarFile);
	
	for (TInt i(0) ; i < KNumEntriesToAdd ; ++i)
		{
		HBufC8* guid = NULL;
		CCalEntry* entry = iTestLib->CreateCalEntryL(CCalEntry::EReminder, guid);
		CleanupStack::PushL(entry);
		
		SetEntryStartAndEndTimeL(entry,
									TDateTime(2005, ESeptember, 12, 0, 0, 0, 0),
									TDateTime(2005, ESeptember, 12, 1, 0, 0, 0));
		
		RPointerArray<CCalEntry> entryArray;
		CleanupClosePushL(entryArray);
		
		entryArray.AppendL(entry);
		TInt num;
		iTestLib->SynCGetEntryViewL().StoreL(entryArray, num);
		
		CleanupStack::PopAndDestroy(&entryArray);
		CleanupStack::PopAndDestroy(entry);
		
		test.Printf(_L("."));
		}
		
	TCalTime minTime;
	minTime.SetTimeUtcL(TCalTime::MinTime());
	TCalTime maxTime;
	maxTime.SetTimeUtcL(TCalTime::MaxTime());
	CalCommon::TCalTimeRange timeRange(minTime, maxTime);
	
	iActive->SetStep(CAppActive::EDeleteAllMemosMiddle);
	
	//Attempt to delete all the entries
	test.Printf(_L("Deleting all the entries...\r\n"));
	iTestLib->SynCGetEntryViewL().DeleteL(timeRange, CCalEntry::EReminder, *this);
	CActiveScheduler::Start();
	delete iTestLib;
	iTestLib = NULL;
	}
void CDummyCalendarApp::RepeatExceptedEntryFetchInstancesL(
    RPointerArray<CCalInstance>& aInstances )
	{
	// Create a time range filter that returns all instances.
	TCalTime minTime;
	minTime.SetTimeUtcL(TCalTime::MinTime());
	TCalTime maxTime;
	maxTime.SetTimeUtcL(TCalTime::MaxTime());
	CalCommon::TCalTimeRange timeRange(minTime, maxTime);
	CalCommon::TCalViewFilter filter = CalCommon::EIncludeAll;
	
	CleanupResetAndDestroyPushL(aInstances);	
	
	// Get all instances. 
	AsynCGetInstanceViewL().FindInstanceL(aInstances, filter, timeRange);
	test(aInstances.Count() >= 2);
	CleanupStack::Pop(&aInstances);
	}
/**
Base class pure virtual.
@return		EPass or EFail indicating the result of the test step.
@test
*/
TVerdict CTestCalInterimApiTZChange::doTestStepL()	
	{
	TESTL(TestStepResult() == EPass);
	INFO_PRINTF1(_L("CTestCalInterimApiTZChange::doTestStepL() start"));
	
	iInstanceView = CCalInstanceView::NewL(GetSession(), *this);
		
	iField = KIsFloating;
	TESTL(GetBoolFromConfig(ConfigSection(), iField, iIsFloating));	
		
	CActiveScheduler::Start();

	SetTimeRangeL();
	
	CalCommon::TCalViewFilter filter = CalCommon::EIncludeAll;
	CalCommon::TCalTimeRange timeRange(iStartDate, iEndDate);
	
	RPointerArray<CCalInstance> fetchInstanceArray;
	CleanupStack::PushL(TCleanupItem(ResetAndDestroyInstanceArray, &fetchInstanceArray));
	
	TRAPD(err, iInstanceView->FindInstanceL(fetchInstanceArray, filter, timeRange));
	
	if (err == KErrNone)
		{
		for ( TInt index = 0; index < fetchInstanceArray.Count(); index++ )
			{
			INFO_PRINTF2(_L("Instance:- %d"), index);
			CheckInstancesTimesL(fetchInstanceArray, index);
			}
		INFO_PRINTF1(KInstanceTimesCorrect);
		CheckEntryTimesL(fetchInstanceArray);
		}
	else
		{
		ERR_PRINTF1(_L("Instances not fetched successfully"));
		SetTestStepResult(EFail);
		SetTestStepError(err);
		}
		
	CleanupStack::PopAndDestroy(&fetchInstanceArray); // fetchInstanceArray.reset & fetchInstanceArray.destroy;
	
	INFO_PRINTF1(_L("CTestCalInterimApiTZChange::doTestStepL() finish"));
	return TestStepResult();
	}
void CBadRRule::CheckInstancesL(RArray<TTime> &insTimes)
{
	RPointerArray<CCalInstance> instanceArray ;

	TCalTime startTime ;
	TCalTime endTime ;	
	startTime.SetTimeUtcL(TTime(TDateTime(2006, EJanuary, 1, 10, 1, 0, 0))) ;
	endTime.SetTimeUtcL(TTime(TDateTime(2006, EJanuary, 30, 10, 5, 0, 0))) ;		
	
	CalCommon::TCalTimeRange timeRange(startTime, endTime);
	
	CCalInstanceView &insView = iTestLib->SynCGetInstanceViewL();	
	
	insView.FindInstanceL(instanceArray, CalCommon::EIncludeAll, timeRange);		
	
	TInt count = instanceArray.Count() ;
	TInt count1 = insTimes.Count() ;
	
	test(count == count1) ;
	TInt i, j ;
	
	for(i = 0; i < count1; i++)
		{
		for(j = 0; j < count; j++)
			{
			if (instanceArray[j] == NULL)
				{
				continue ;
				}
			TTime t1 = instanceArray[j]->Time().TimeUtcL() ;		
			TDateTime d1 = t1.DateTime() ;	
			if (insTimes[i] == t1)
				{
				delete instanceArray[j] ;
				instanceArray[j] = NULL ;
				break ;
				}			
			}
			test(j < count) ;		
		}
	
	instanceArray.Close() ;		
}
void CDummyCalendarApp::DeleteAllMemosEndL()
	{
	test.Printf(_L("Test: DeleteAllMemosEndL\r\n"));
	test.Printf(_L("Interrupting the delete...\r\n"));
	
	//Get a time range
	TCalTime minTime;
	minTime.SetTimeUtcL(TCalTime::MinTime());
	TCalTime maxTime;
	maxTime.SetTimeUtcL(TCalTime::MaxTime());
	CalCommon::TCalTimeRange timeRange(minTime, maxTime);
	
	test.Printf(_L("Done!\r\n"));
	
	iTestLib = CCalTestLibrary::NewL(EFalse);
	iTestLib->OpenFileL(KCalendarFile);
	
	test.Printf(_L("Deleteing all the entiries again...\r\n"));
	iTestLib->SynCGetEntryViewL().DeleteL(timeRange, CCalEntry::EReminder, *this);
	}
// ------------------------------------------------------------------------------------------------
// CArrayPtr<CNpdItem>* CNSmlNotepadDatabase::FetchItemsLC(TBool aIncludeText,
// ------------------------------------------------------------------------------------------------
CArrayPtr<CNpdItem>* CNSmlNotepadDatabase::FetchItemsLC(TBool aIncludeText,
                                                    TInt* aKey)
    {
    _NOTEPAD_DBG_FILE("CNSmlNotepadDatabase::FetchItemsLC(): begin") ;

    CArrayPtr<CNpdItem>* res = new (ELeave) CArrayPtrFlat<CNpdItem> (8);
    CleanupPtrArrayPushL(res);
        
    if(!aKey)
        {
        _NOTEPAD_DBG_FILE("CNSmlNotepadDatabase::FetchItemsLC(): In aKey case");
        RPointerArray<CCalInstance> array;
        CleanupRPtrArrayPushL(array);
        _NOTEPAD_DBG_FILE("CNSmlNotepadDatabase::FetchItemsLC(): After cleaupstack");
        
        TCalTime startDate;
        startDate.SetTimeLocalL(TDateTime(1900, EJanuary, 1, 0, 0, 0, 0));
        TCalTime endDate;
        endDate.SetTimeLocalL(TDateTime(2100, EJanuary, 30, 0, 0, 0, 0));
        CalCommon::TCalTimeRange timeRange(startDate, endDate);
    
        iInstanceView->FindInstanceL(array,CalCommon::EIncludeNotes, timeRange);
        CNpdItem* entry = NULL;
        TInt i = 0;

        while (i < array.Count())
            {
            entry = CNpdItem::NewLC();
            entry->SetKey( array[i]->Entry().LocalUidL() );
            entry->SetModified( array[i]->Entry().LastModifiedDateL().TimeUtcL());
            if(aIncludeText)
                {
                _NOTEPAD_DBG_FILE("CNSmlNotepadDatabase::FetchItemsLC(): aIncludeText true");
                HBufC* content = HBufC::NewL( array[i]->Entry().DescriptionL().Length() );
                content->Des().Copy( array[i]->Entry().DescriptionL() );                
                entry->SetContent( content );
                }
            res->AppendL(entry);
            _NOTEPAD_DBG_FILE("CNSmlNotepadDatabase::FetchItemsLC(): After res->AppendL(entry)");
            CleanupStack::Pop(entry);
            _NOTEPAD_DBG_FILE("CNSmlNotepadDatabase::FetchItemsLC(): After popping (entry)");
            i++;
            }
        _NOTEPAD_DBG_FILE("CNSmlNotepadDatabase::FetchItemsLC(): before pop and destroy");
        CleanupStack::PopAndDestroy(&array);
        _NOTEPAD_DBG_FILE("CNSmlNotepadDatabase::FetchItemsLC(): After While Loop");
        }
    else
        {
        _NOTEPAD_DBG_FILE("CNSmlNotepadDatabase::FetchItemsLC(): In else case");
        CCalEntry* entryTobeFetched(NULL);
        entryTobeFetched = iEntryView->FetchL(*aKey);
        _NOTEPAD_DBG_FILE("CNSmlNotepadDatabase::FetchItemsLC(): After Fetching entry");
        if(entryTobeFetched)
            {
            CleanupStack::PushL(entryTobeFetched); 
            CNpdItem* entry = CNpdItem::NewLC();
            entry->SetKey( entryTobeFetched->LocalUidL() );
            entry->SetModified( entryTobeFetched->LastModifiedDateL().TimeUtcL() );
            if(aIncludeText)
                {
                HBufC* content = HBufC::NewL( entryTobeFetched->DescriptionL().Length() );
                content->Des().Copy( entryTobeFetched->DescriptionL() );                
                entry->SetContent( content );
                }
            res->AppendL(entry);
            CleanupStack::Pop(entry);
            CleanupStack::PopAndDestroy(entryTobeFetched);
            }
        }
    _NOTEPAD_DBG_FILE("CNSmlNotepadDatabase::FetchItemLC(): end");
    return res;
    }
// Helper method
// Extract all instances given by Guids in iCurrentTestData
void CDummyCalendarApp::ExtractInstancesL()
	{
	TCalTime startDate;
	startDate.SetTimeLocalL(iCurrentTestData->iStartDate);
	TCalTime endDate;
	endDate.SetTimeLocalL(iCurrentTestData->iEndDate);

	CalCommon::TCalTimeRange timeRange(startDate, endDate);
	
	RPointerArray<CCalInstance> instanceArray;
	
	if (iCurrentTestData->iSearchString)
		{
		CCalInstanceView::TCalSearchParams searchParams(*iCurrentTestData->iSearchString, iCurrentTestData->iSearchBehaviour);
		
		iInstanceView->FindInstanceL(instanceArray, 
									(CalCommon::TCalViewFilter)iCurrentTestData->iFilter, 
									timeRange, 
									searchParams);
		}
	else
		{
		iInstanceView->FindInstanceL(instanceArray, 
							(CalCommon::TCalViewFilter)iCurrentTestData->iFilter, 
							timeRange);		
		}
	
	TBuf<26> startBuf;
	TBuf<26> endBuf;
	startDate.TimeLocalL().FormatL(startBuf,KFormatDate());
	endDate.TimeLocalL().FormatL(endBuf,KFormatDate());
	
	test.Printf(_L("Checking instances between %S and %S\n"), &startBuf, &endBuf);
	
	if (instanceArray.Count() == 0)
		{
		test.Printf(_L("No instances found\n"));
		}
	else
		{
		TInt instanceCount = 0;
		TTime instTime;
		TBuf<26> instBuf;

		while (instanceCount < instanceArray.Count())
			{
			CCalInstance* inst = instanceArray[instanceCount];
			CleanupStack::PushL(inst);

			instTime = inst->Time().TimeLocalL();
			instTime.FormatL(instBuf,KFormatDate());
	
			TBuf<62> rptType;
			TCalRRule rptDef;
			if (!inst->Entry().GetRRuleL(rptDef))
				{
				rptType.Copy(_L("One off"));
				}
			else
				{
				switch (rptDef.Type())
					{
					case TCalRRule::EDaily:
						rptType.Format(_L("Daily every %d days"), rptDef.Interval());
						break;
					case TCalRRule::EWeekly:
						rptType.Format(_L("Weekly every %d weeks"), rptDef.Interval());
						break;
					case TCalRRule::EMonthly:
						rptType.Format(_L("MonthlyByDates every %d months"), rptDef.Interval());
						break;
					case TCalRRule::EYearly:
						rptType.Format(_L("YearlyByDate every %d years"), rptDef.Interval());
						break;
					default:
						ASSERT(0);
						break;
					}
				}
				
			test.Printf(_L("One instance is on %S, %d:%d - %S"), &instBuf, instTime.DateTime().Hour(), instTime.DateTime().Minute(), &rptType);
			
			if (iCurrentTestData->iTimes.Find(instTime) == KErrNotFound)
				{
				test.Printf(_L("THIS ENTRY WAS NOT EXPECTED\n"));
				test(0);
				}
				
			instanceCount++;
			CleanupStack::Pop(inst);
			}
			
		if (iCurrentTestData->iFunction)
			ASSERT(iCurrentTestData->iFunction->CallBack() == 0);
		}

	if (instanceArray.Count() != iCurrentTestData->iTimes.Count())
		{
		test.Printf(_L("Some expected entries were not found!\n"));
		test(0);	
		}
		
	instanceArray.ResetAndDestroy();
	}
void CDummyCalendarApp::DeleteWhileAnotherThreadAccessesCalendarL()
	{
	test.Printf(_L("Adding 1000 entries"));
	
	const TInt KNumEntriesToAdd(1000);
	
	for (TInt i(0) ; i < KNumEntriesToAdd ; ++i)
		{
		HBufC8* guid = NULL;
		CCalEntry* entry = CreateCalEntryL(CCalEntry::EAppt, guid);
		CleanupStack::PushL(entry);
		
		SetEntryStartAndEndTimeL(entry,
									TDateTime(2005, ESeptember, 12, 0, 0, 0, 0),
									TDateTime(2005, ESeptember, 12, 1, 0, 0, 0));
		
		RPointerArray<CCalEntry> entryArray;
		CleanupClosePushL(entryArray);
		
		entryArray.AppendL(entry);
		TInt num;
		SynCGetEntryViewL().StoreL(entryArray, num);
		
		CleanupStack::PopAndDestroy(&entryArray);
		CleanupStack::PopAndDestroy(entry);
		
		test.Printf(_L("."));
		}
	
	// start the second thread and wait for it to signal
	// that it is ready to recieve notifications
	RThread thread;
	CleanupClosePushL(thread);
	User::LeaveIfError(thread.Create(_L("SecondThread"), SecondThreadMain,
															KDefaultStackSize,
															KDefaultStackSize * 20, // this is the minimum heap size
															KDefaultStackSize * 40, // this is the maximum heap size
															NULL));
	thread.SetPriority(EPriorityMore);
	thread.Resume();
	
	// wait untill the second thread is ready
	TRequestStatus status;
	thread.Rendezvous(status);
	User::WaitForRequest(status);
		
	TCalTime start;
	start.SetTimeLocalL(TDateTime(2005, ESeptember, 0, 0, 0, 0, 0));
	TCalTime end;
	end.SetTimeLocalL(TDateTime(2006, EJanuary, 0, 0, 0, 0, 0));
	
	CalCommon::TCalTimeRange timeRange(start, end);
	
	CProgressCallBack* progress = new(ELeave) CProgressCallBack;
	CleanupStack::PushL(progress);
	
	test.Printf(_L("Start DeleteL\n"));
	SynCGetEntryViewL().DeleteL(timeRange, CalCommon::EIncludeAll, *progress);
	thread.Resume();
	// signal to the other thread that we have called delete
	// and it should start testing that the file is locked
	CActiveScheduler::Start();
	thread.Resume();
	
	CleanupStack::PopAndDestroy(progress);
	
	// Wait for the other thread to exit
	// and test that the exit reason was KErrNone
	thread.Logon(status);
	User::WaitForRequest(status);
	
	// If the other thread's testing was unsuccessful
	// the exit reason will be KErrGeneral
	test(status.Int() == KErrNone);
	
	CleanupStack::PopAndDestroy(&thread);
	}
void CSecondThreadApp::TestFileLockL(TInt aError)
	{
	TCalTime from;
	from.SetTimeLocalL(TDateTime(2005, ESeptember, 11, 0, 0, 0, 0));
	TCalTime resultTime;
	
	// NextInstanceL
	RDebug::Printf("Test NextInstanceL");
	TInt error(KErrNone);
	TRAP(error, resultTime = iCalTestLib->SynCGetInstanceViewL().NextInstanceL(CalCommon::EIncludeAll, from));
	if (error != aError) 
		{
		User::Leave(KErrGeneral);
		}
	
	// PreviousInstanceL
	RDebug::Printf("Test PreviousInstanceL");
	error = KErrNone;
	TRAP(error, resultTime = iCalTestLib->SynCGetInstanceViewL().PreviousInstanceL(CalCommon::EIncludeAll, from));
	if (error != aError) 
		{
		User::Leave(KErrGeneral);
		}
	
	// GetIdsModifiedSinceDateL
	RDebug::Printf("Test GetIdsModifiedSinceDateL");
	RArray<TCalLocalUid> uids;
	CleanupClosePushL(uids);
	error = KErrNone;
	TRAP(error, iCalTestLib->SynCGetEntryViewL().GetIdsModifiedSinceDateL(from, uids));
	if (error != aError) 
		{
		User::Leave(KErrGeneral);
		}
	if (aError != KErrNone && uids.Count() != 0)
		{
		User::Leave(KErrGeneral);
		}
	CleanupStack::PopAndDestroy(&uids);
	
	// FindInstanceL
	RDebug::Printf("Test FindInstanceL");
	RPointerArray<CCalInstance> instanceList;
	CleanupResetAndDestroyPushL(instanceList);
	
	TCalTime start;
	start.SetTimeLocalL(TDateTime(2004, EMay, 12, 10, 30, 0, 0));
	TCalTime end;
	end.SetTimeLocalL(TDateTime(2007, EMay, 12, 10, 30, 0, 0));
	
	CalCommon::TCalTimeRange timeRange(start, end);
	
	error = KErrNone;
	TRAP(error, iCalTestLib->SynCGetInstanceViewL().FindInstanceL(instanceList, CalCommon::EIncludeAll, timeRange));
	if (error != aError)
		{
		User::Leave(KErrGeneral);
		}
	if (aError != KErrNone && instanceList.Count() != 0)
		{
		User::Leave(KErrGeneral);
		}
		
	CleanupStack::PopAndDestroy(&instanceList);
	
	// StoreL
	RPointerArray<CCalEntry> entryArray;
	CleanupResetAndDestroyPushL(entryArray);
	
	HBufC8* guid = NULL;
	CCalEntry* entry = iCalTestLib->CreateCalEntryL(CCalEntry::EAppt, guid);
	CleanupStack::PushL(entry);
	iCalTestLib->SetEntryStartAndEndTimeL(entry, TDateTime(2005, EMay, 23, 10, 30, 0, 0),
													TDateTime(2005, EMay, 23, 11, 0, 0, 0));
	entryArray.AppendL(entry);
	CleanupStack::Pop(entry);
	TInt numSuc;
	error = KErrNone;
	TRAP(error, iCalTestLib->SynCGetEntryViewL().StoreL(entryArray, numSuc));
	if (error != aError)
		{
		User::Leave(KErrGeneral);
		}
	
	// UpdateL
	error = KErrNone;
	TRAP(error, iCalTestLib->SynCGetEntryViewL().UpdateL(entryArray, numSuc));
	if (error != aError && error != KErrNotFound)
		{
		User::Leave(KErrGeneral);
		}
	// FetchL
	error = KErrNone;
	TRAP(error, iCalTestLib->SynCGetEntryViewL().FetchL(*guid, entryArray));
	if (error != aError)
		{
		User::Leave(KErrGeneral);
		}
		
	// DeleteL
	error = KErrNone;
	TRAP(error, iCalTestLib->SynCGetEntryViewL().DeleteL(*entry));
	if (error != aError)
		{
		User::Leave(KErrGeneral);
		}
	
	CleanupStack::PopAndDestroy(&entryArray);
	
	// CategoryManager
	CCalCategoryManager* categoryManager = CCalCategoryManager::NewL(iCalTestLib->GetSession());
	CleanupStack::PushL(categoryManager);
	
	CCalCategory* funCategory = CCalCategory::NewL(_L("Fun"));
	CleanupStack::PushL(funCategory);
	error = KErrNone;
	TRAP(error, categoryManager->AddCategoryL(*funCategory));
	if (error != aError)
		{
		User::Leave(KErrGeneral);
		}
	CleanupStack::PopAndDestroy(funCategory);
	
	error = KErrNone;
	TRAP(error, delete categoryManager->CategoryL(0));
	if (error != aError)
		{
		User::Leave(KErrGeneral);
		}
	
	CleanupStack::PopAndDestroy(categoryManager);
	
	// CCalIter
	CCalIter* calIter = CCalIter::NewL(iCalTestLib->GetSession());
	CleanupStack::PushL(calIter);
	
	error = KErrNone;
	TRAP(error, const TDesC8& guidRefFirst = calIter->FirstL());
	if (error != aError)
		{
		User::Leave(KErrGeneral);
		}
	
	error = KErrNone;
	TRAP(error, const TDesC8& guidRefNext = calIter->NextL());
	if (error != aError && error != KErrCorrupt)
		{
		User::Leave(KErrGeneral);
		}
		
	CleanupStack::PopAndDestroy(calIter);
	//	
	}
void CAppActive2::RunL()
	{
	RTest test(KTestName);
	iCount++;
	switch (iStep)
		{
		case EStepCreateView:
			{
		// Step 1
		// 1. Open the existing file which is created by the main thread
		// 2. Create a view which result in building indexes
		// 3. For the first few times, iStep is set to step 2 so that the building index process is started and cancelled repeatedly.
		// Later on, the iStep is set to step 3 and wait for the index building is completed.
			if(iCancelBuildIndex)
				{
				test(iApp->Status()==CMultiThreadTestApp2::EBuildIndexCancel);
				}
			iApp->OpenFileL(KTestFile);
			iApp->GetSession().OpenL(KTestFile);
			iStep = EStepMutipleOperation1;
			iInstanceView = CCalInstanceView::NewL(iApp->GetSession(), *iApp);
			if(iCount<50)
				{//Test that both threads build index and cancel it spotenously
				iStep = EStepDeleteView;			
				Start();
				}
			}
			break;
		case EStepDeleteView:
			{
		// Step 2
		// Delete the view which result in cancelling the index building
			iCancelBuildIndex = ETrue;
			delete iInstanceView;
			iInstanceView=NULL;
			iStep = EStepCreateView;
			}
			break;
		case EStepMutipleOperation1:
			{
			//Step 3
			//The other thread cancel the building index request which shouldn't affect the server to build index for this thread
			// Test the index is built and it is able to use it to find instances.

			test(iApp->Status()==CMultiThreadTestApp2::EBuildIndexComplete);
			RPointerArray<CCalInstance>instArray;
			CleanupResetAndDestroyPushL(instArray);
			TCalTime calStartTime;
			TCalTime calEndTime;
			calStartTime.SetTimeLocalL(TCalTime::MinTime());
			calEndTime.SetTimeLocalL(TCalTime::MaxTime());
			CalCommon::TCalTimeRange timeRange(calStartTime, calEndTime);
			iInstanceView->FindInstanceL(instArray, CalCommon::EIncludeAll, timeRange);
			test(instArray.Count() == KNumberOfEntriesToAdd); 
			CleanupStack::PopAndDestroy(&instArray); 
			
			// Wait for the first thread is ready to make sure operations are carried spontaneously
			// (Synch Point 1)
			iApp->SynchWithOtherThread();
			iEntryView = CCalEntryView::NewL(iApp->GetSession());
			iApp->AddEntryL(*iEntryView, KNumberOfEntriesToAdd);
			
			// (Synch Point 2)
			iApp->SynchWithOtherThread();
			RPointerArray<CCalEntry> entries;
			CleanupResetAndDestroyPushL(entries);
			iApp->FetchEntryL(*iEntryView, entries);
			CleanupStack::PopAndDestroy(&entries);
			
			// (Synch Point 3)
			iApp->SynchWithOtherThread();
			TRAPD(err,iApp->AddEntryL(*iEntryView,KNumberOfEntriesToAdd));	
			test.Printf(_L("Synch Point 3 err is: %d\n"),err);
			test(err==KErrLocked || err == KErrNone);
			// (Synch Point 4)
			iApp->SynchWithOtherThread();
			iApp->SetStatus(CMultiThreadTestApp::ESynchentry);
			iApp->SynchEntryL(*iEntryView);
			iStep = EEnd;
			break;
			}
		case EEnd:
			{
			iApp->Close();
			}
			break;
		default:
			User::Leave(KErrNotFound);
		}
	test.Close();
	}
TEUCHOS_UNIT_TEST( Rythmos_TimeRange, nonMemberConstructor ) {
  TimeRange<double> tr = timeRange(1.25,3.45);
  TEST_EQUALITY_CONST( tr.isValid(), true );
  TEST_EQUALITY_CONST( tr.lower(), 1.25 );
  TEST_EQUALITY_CONST( tr.upper(), 3.45 );
}