pimappservices/calendar/tsrc/tcal_backuprestore.cpp
changeset 0 f979ecb2b13e
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     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 #include <connect/sbeclient.h>
       
    16 #include <e32test.h>
       
    17 #include <caluser.h>
       
    18 #include <calsession.h>
       
    19 #include <calentry.h>
       
    20 #include <calrrule.h>
       
    21 #include <calentryview.h>
       
    22 #include <calalarm.h>
       
    23 #include <calcategory.h>
       
    24 #include <calattachment.h>
       
    25 #include <caliterator.h>
       
    26 #include <coreappstest/testserver.h>
       
    27 
       
    28 #include "caltestlib.h"
       
    29 
       
    30 class CBackRestoreTestManager : public CBase, MCalChangeCallBack2, public MCalProgressCallBack
       
    31 	{
       
    32 public:
       
    33     static CBackRestoreTestManager* NewLC();
       
    34 	~CBackRestoreTestManager();
       
    35 	void ConstructL();
       
    36 	
       
    37 	// From MCalChangeCallBack2
       
    38     void CalChangeNotification(RArray<TCalChangeEntry>& aChangeItems);
       
    39 
       
    40 	void RunAllTestsL();
       
    41 private:
       
    42     // From MCalProgressCallBack
       
    43     void Progress(TInt aPercentageCompleted);   
       
    44     void Completed(TInt aError);    
       
    45     TBool NotifyProgress();
       
    46 
       
    47     enum TBackupOrRestore
       
    48         {
       
    49         EBackup,
       
    50         ERestore
       
    51         };
       
    52  
       
    53     enum TRestoredFile
       
    54         {
       
    55         EFileVersion95,
       
    56         EFileVersion94,
       
    57         EFileNotSupported,
       
    58         EFileCurrupted
       
    59         };
       
    60 	
       
    61 	void TestBackupRestoreL(TBool aStoreEntry, TBackupOrRestore aBackupOrRestore);
       
    62 	void TestMultipleCalendarBackupRestoreL(TBackupOrRestore aBackupOrRestore);
       
    63 	void TestOperationsCanBePerformedL(TBackupOrRestore aBackupOrRestore);
       
    64 	void TestRestoreEealyVersionCalFileL(TRestoredFile aRestoredFile);
       
    65 	void TestAsynDeleteL();
       
    66 	void TestNoCrashDuringRestoreL();
       
    67 	
       
    68 	void DeleteTestFilesL();
       
    69 	void RegisterNotificationL();
       
    70 	TBool CompareEntryL(RPointerArray<CCalEntry>& entriesSource, RPointerArray<CCalEntry>& entriesTocompare);
       
    71 
       
    72 	void FetchAllEntryL(RPointerArray<CCalEntry>& aEntries);
       
    73 	void StoreEntryL(TInt aNumber, CCalEntryView& aEntryView);
       
    74     CCalEntry* CreateEntryLC(HBufC8* aGulid, TBool aAddAttachment);
       
    75 	void AddAlarmL(CCalEntry& aEntry);
       
    76 	void AddCategoriesL(CCalEntry& aEntry);
       
    77 	void AddAttendeesL(CCalEntry& aEntry);
       
    78 	void AddFileAttachmentL(CCalEntry& aEntry);
       
    79 
       
    80 private:
       
    81 	CCalTestLibrary*                       iTestLib;
       
    82 	TBool                                  iNotificationCallBackOccurred;
       
    83 	MCalChangeCallBack2::TChangeType       iNotificationChangeType;
       
    84 	TInt                                   iDeleteCompleteErrCode;
       
    85 	};
       
    86 
       
    87 
       
    88 
       
    89 static RTest test(_L("tcal_backrestore"));
       
    90 static conn::CSBEClient* BackupClient;
       
    91 static TDriveList DriveList;
       
    92 
       
    93 
       
    94 _LIT8(KGuid, "A_Global_UID");
       
    95 _LIT(KCalFile, "test_backrestore_calfile");
       
    96 _LIT(KCalSecondFile, "test_backrestore_calSecondfile");
       
    97 _LIT(KAttachFile, "c:\\calendar_attachments\\tcal_attach.jpg");
       
    98 _LIT(KCalenderServerPath, "C:\\private\\10003a5b\\");
       
    99 _LIT(KTestFilePath, "C:\\calendarbackuprestortest\\");
       
   100 
       
   101 _LIT(KCalfile95, "z:\\private\\10003a5b\\tcal_dc_95");
       
   102 _LIT(KCalfile94, "z:\\private\\10003a5b\\tcal_dc_94");
       
   103 _LIT(KCalfileNotSupported, "z:\\private\\10003a5b\\tcal_dc_81");
       
   104 _LIT(KCalfileCorrupt, "z:\\private\\10003a5b\\tcal_dc_92_corrupt");
       
   105 
       
   106 const TInt KNumerOfEntry = 100;
       
   107 
       
   108 // building block test backup and restore functions
       
   109 
       
   110 static void StartBackupL()
       
   111     {
       
   112     //Use the Secure Backup Engine API to "start" a backup.  
       
   113     test.Printf(_L("Starting Backup\n"));
       
   114     BackupClient->SetBURModeL(DriveList, conn::EBURBackupFull, conn::EBackupBase);
       
   115     }
       
   116 
       
   117 static void EndBackupL()
       
   118     {
       
   119     // Use the Secure Backup Engine API to "end" a backup.
       
   120     test.Printf(_L("Finishing Backup\n"));
       
   121     BackupClient->SetBURModeL(DriveList, conn::EBURNormal, conn::ENoBackup);
       
   122     }
       
   123 
       
   124 static void StartRestoreL()
       
   125     {
       
   126     // Use the Secure Backup Engine API to "start" a restore.      
       
   127     test.Printf(_L("Starting restore\n"));
       
   128     BackupClient->SetBURModeL(DriveList, conn::EBURRestoreFull, conn::EBackupBase);
       
   129     }
       
   130 
       
   131 static void EndRestoreL()
       
   132     {
       
   133     // Use the Secure Backup Engine API to "end" a restore.   
       
   134     test.Printf(_L("Finishing restore\n"));
       
   135     BackupClient->SetBURModeL(DriveList, conn::EBURNormal, conn::ENoBackup);
       
   136     }
       
   137 
       
   138 CBackRestoreTestManager* CBackRestoreTestManager::NewLC()
       
   139 	{
       
   140 	CBackRestoreTestManager* self = new (ELeave) CBackRestoreTestManager();
       
   141 	CleanupStack::PushL(self);
       
   142 	self->ConstructL();
       
   143 	return (self);
       
   144 	}
       
   145 	
       
   146 void CBackRestoreTestManager::ConstructL()
       
   147 	{
       
   148 	iTestLib = CCalTestLibrary::NewL();
       
   149 	iTestLib->ReplaceFileL(KCalFile);
       
   150 	iTestLib->OpenFileL(KCalFile);
       
   151 	}	
       
   152 	
       
   153 CBackRestoreTestManager::~CBackRestoreTestManager()	
       
   154 	{
       
   155 	TRAP_IGNORE(iTestLib->DeleteFileL(KAttachFile, EFalse));
       
   156 	TRAP_IGNORE(DeleteTestFilesL());
       
   157 	delete iTestLib;
       
   158 	}
       
   159 
       
   160 void CBackRestoreTestManager::Progress(TInt aPercentageCompleted)
       
   161     {
       
   162     test.Printf(_L("DeleteL is %d%% complete\n"), aPercentageCompleted);
       
   163     }
       
   164     
       
   165 void CBackRestoreTestManager::Completed(TInt aError)
       
   166     {
       
   167     test.Printf(_L("DeleteL has completed with error code %d\n"), aError);
       
   168     iDeleteCompleteErrCode = aError;
       
   169     }
       
   170     
       
   171 TBool CBackRestoreTestManager::NotifyProgress()
       
   172     {
       
   173     return ETrue;
       
   174     }
       
   175 
       
   176 void CBackRestoreTestManager::DeleteTestFilesL()
       
   177     {
       
   178     CFileMan* fileCopier=NULL;
       
   179     fileCopier = CFileMan::NewL(iTestLib->FileSession());
       
   180     fileCopier->RmDir(KTestFilePath());
       
   181     if(fileCopier)
       
   182          {
       
   183          delete fileCopier;
       
   184          }
       
   185     }
       
   186 
       
   187 void CBackRestoreTestManager::RegisterNotificationL()
       
   188     {
       
   189     RDebug::Print(_L("Register Notication" ));
       
   190     TCalTime minTime;
       
   191     minTime.SetTimeUtcL(TCalTime::MinTime());
       
   192     TCalTime maxTime;
       
   193     maxTime.SetTimeUtcL(TCalTime::MaxTime());
       
   194     CalCommon::TCalTimeRange timeRange(minTime, maxTime);
       
   195     CCalChangeNotificationFilter* filter = CCalChangeNotificationFilter::NewL(MCalChangeCallBack2::EChangeEntryAll, ETrue, timeRange);
       
   196     iTestLib->GetSession().StartChangeNotification(*this, *filter);
       
   197     delete filter;
       
   198     }
       
   199 
       
   200 void CBackRestoreTestManager::CalChangeNotification(RArray<TCalChangeEntry>& aChangeItems)
       
   201     {
       
   202     test.Printf(_L("MCalChangeCallBack2 notification happened\n") );
       
   203     iNotificationCallBackOccurred = ETrue;
       
   204 
       
   205     //There is only one change in the array and it is backup\restore notification.
       
   206     test(aChangeItems.Count() == 1);
       
   207     TCalChangeEntry change = aChangeItems[0];
       
   208     iNotificationChangeType = aChangeItems[0].iChangeType;
       
   209     CActiveScheduler::Stop();
       
   210     }
       
   211 
       
   212 void CBackRestoreTestManager::AddFileAttachmentL(CCalEntry& aEntry)
       
   213     {
       
   214 //1. copy the attachment file from z:\\calendar_attachments\\tcal_attach.jpg tp c:\\calendar_attachments\\tcal_attach.jpg
       
   215     _LIT8(KMimeType, "image/jpeg");
       
   216     _LIT(KLabel, "snowman");
       
   217     _LIT(KOriginalDrive, "z:");
       
   218     
       
   219     TInt err = iTestLib->FileSession().MkDirAll(KAttachFile);
       
   220     if (err != KErrAlreadyExists)
       
   221          {
       
   222          User::LeaveIfError(err);
       
   223          }
       
   224          
       
   225     // required for passing attachment file handles to server
       
   226     User::LeaveIfError(iTestLib->FileSession().ShareProtected()); 
       
   227     
       
   228     CFileMan* fileCopier = CFileMan::NewL(iTestLib->FileSession());
       
   229     CleanupStack::PushL(fileCopier);
       
   230     
       
   231     TFileName initialLocationOfFile = KAttachFile();
       
   232     initialLocationOfFile.Replace(0, 2, KOriginalDrive());
       
   233     
       
   234     // get current time
       
   235     TTime now;
       
   236     now.HomeTime();
       
   237     
       
   238     // clear any read only attribute if the file is present such we avoid errors at copy time
       
   239     fileCopier->Attribs(KAttachFile, 0, KEntryAttReadOnly, now);
       
   240     
       
   241     // copy the file from Z: drive to C: drive (ROM to RAM)
       
   242     User::LeaveIfError(fileCopier->Copy(initialLocationOfFile, KAttachFile));
       
   243     
       
   244     // clear any read only attribute such we avoid errors at open time (true on real hardware)
       
   245     User::LeaveIfError(fileCopier->Attribs(KAttachFile, 0, KEntryAttReadOnly, now));   
       
   246     CleanupStack::PopAndDestroy(fileCopier);
       
   247  
       
   248 //2. Set the file to the attachment
       
   249     RFile file;
       
   250     User::LeaveIfError(file.Open(iTestLib->FileSession(), KAttachFile(), EFileWrite));
       
   251     CleanupClosePushL(file);
       
   252     CCalAttachment* attachment = CCalAttachment::NewFileL(file);
       
   253     CleanupStack::PopAndDestroy(&file); // file.Close()
       
   254     CleanupStack::PushL(attachment);
       
   255     attachment->SetMimeTypeL(KMimeType());
       
   256     attachment->SetLabelL(KLabel());
       
   257  
       
   258 //3. Add attachment to the entry
       
   259     aEntry.AddAttachmentL(*attachment);
       
   260     CleanupStack::Pop(attachment);
       
   261     }
       
   262 
       
   263 void CBackRestoreTestManager::AddCategoriesL(CCalEntry& aEntry)
       
   264     {
       
   265     _LIT(KDummyCategoryName, "new category");
       
   266       
       
   267     // add categories and attendees to 1 in 10 entries
       
   268     CCalCategory* category1 = CCalCategory::NewL(KDummyCategoryName);
       
   269     CleanupStack::PushL(category1);   
       
   270     CCalCategory* category2 = CCalCategory::NewL(CCalCategory::ECalHoliday);
       
   271     CleanupStack::PushL(category2);     
       
   272     aEntry.AddCategoryL(category1); 
       
   273     aEntry.AddCategoryL(category2);    
       
   274     CleanupStack::Pop(category2);
       
   275     CleanupStack::Pop(category1);
       
   276     }
       
   277 
       
   278 void CBackRestoreTestManager::AddAttendeesL(CCalEntry& aEntry)
       
   279     {
       
   280     _LIT(KDummyAddress1, "dummy address 1");
       
   281     _LIT(KDummyAddress2, "dummy address 2");
       
   282     _LIT(KDummyAddress3, "dummy address 3");
       
   283     _LIT(KDummyAddress4, "dummy address 4");
       
   284     _LIT(KDummyAddress5, "dummy address 5");
       
   285     _LIT(KDummySentBy1, "dummy sent by 1");
       
   286     _LIT(KDummySentBy2, "dummy sent by 2");
       
   287     
       
   288     // add attendees to 1 in 10 entries
       
   289     
       
   290     CCalAttendee* attendee1 = CCalAttendee::NewL(KDummyAddress1);
       
   291     CleanupStack::PushL(attendee1);  
       
   292     CCalAttendee* attendee2 = CCalAttendee::NewL(KDummyAddress2);
       
   293     CleanupStack::PushL(attendee2);  
       
   294     CCalAttendee* attendee3 = CCalAttendee::NewL(KDummyAddress3);
       
   295     CleanupStack::PushL(attendee3); 
       
   296     CCalAttendee* attendee4 = CCalAttendee::NewL(KDummyAddress4, KDummySentBy1);
       
   297     CleanupStack::PushL(attendee4);
       
   298     CCalAttendee* attendee5 = CCalAttendee::NewL(KDummyAddress5, KDummySentBy2);
       
   299     CleanupStack::PushL(attendee5);
       
   300     aEntry.AddAttendeeL(attendee5); 
       
   301     CleanupStack::Pop(attendee5);
       
   302     aEntry.AddAttendeeL(attendee4); 
       
   303     CleanupStack::Pop(attendee4);
       
   304     aEntry.AddAttendeeL(attendee3); 
       
   305     CleanupStack::Pop(attendee3);
       
   306     aEntry.AddAttendeeL(attendee2); 
       
   307     CleanupStack::Pop(attendee2);
       
   308     aEntry.AddAttendeeL(attendee1);
       
   309     CleanupStack::Pop(attendee1);
       
   310     }
       
   311 
       
   312 void CBackRestoreTestManager::AddAlarmL(CCalEntry& aEntry)
       
   313     {
       
   314     CCalAlarm* alarm = CCalAlarm::NewL();
       
   315     CleanupStack::PushL(alarm);
       
   316     alarm->SetTimeOffset(1);
       
   317          
       
   318     CCalContent* almContent = CCalContent::NewL();
       
   319     CleanupStack::PushL(almContent);
       
   320     // Add content and mimetype for the alarm.
       
   321     _LIT8(KContent, "C:\\test.jpg");
       
   322     _LIT8(KMimeType, "mime type");
       
   323     HBufC8* content = KContent().AllocLC();
       
   324     HBufC8* mimetype = KMimeType().AllocLC();
       
   325          
       
   326     // Takes ownership of content and mimetype.
       
   327     almContent->SetContentL(content, mimetype, CCalContent::EDispositionInline);
       
   328     CleanupStack::Pop(mimetype);
       
   329     CleanupStack::Pop(content);    
       
   330    
       
   331     alarm->SetAlarmAction(almContent); // Takes ownership of almContent.
       
   332     CleanupStack::Pop(almContent); 
       
   333         
       
   334     aEntry.SetAlarmL(alarm);
       
   335          
       
   336     CleanupStack::PopAndDestroy(alarm);
       
   337     }
       
   338 
       
   339 CCalEntry* CBackRestoreTestManager::CreateEntryLC(HBufC8* aGulid, TBool aAddFileAttachment)
       
   340     {
       
   341     CleanupStack::PushL(aGulid);
       
   342     CCalEntry* entry = iTestLib->CreateCalEntryL(CCalEntry::EAppt, aGulid);
       
   343     CleanupStack::Pop(aGulid);
       
   344     CleanupStack::PushL(entry); 
       
   345   
       
   346     //Set entry properties
       
   347     _LIT(KText, "bakuprestor_test_text");
       
   348     entry->SetSummaryL(KText());
       
   349     entry->SetLocationL(KText());
       
   350     entry->SetDescriptionL(KText());
       
   351     
       
   352     TTime startTime (TDateTime(2005, EJanuary, 7, 10, 0, 0, 0)); 
       
   353     TCalTime calStartTime;
       
   354     calStartTime.SetTimeLocalL(startTime);
       
   355     TTime endTime (TDateTime(2005, EJanuary, 7, 11, 0, 0, 0)); 
       
   356     TCalTime calEndTime;
       
   357     calEndTime.SetTimeLocalL(endTime);
       
   358     entry->SetStartAndEndTimeL(calStartTime, calEndTime);
       
   359     TCalRRule rpt(TCalRRule::EDaily);
       
   360     rpt.SetDtStart(calStartTime);
       
   361     rpt.SetCount(5);
       
   362     rpt.SetInterval(1);
       
   363     entry->SetRRuleL(rpt);//take a copy of the rule
       
   364     AddAlarmL(*entry);
       
   365     AddCategoriesL(*entry);
       
   366     AddAttendeesL(*entry);
       
   367     if(aAddFileAttachment)
       
   368         {
       
   369         AddFileAttachmentL(*entry);
       
   370         }
       
   371     return entry;
       
   372     }
       
   373 
       
   374 void CBackRestoreTestManager::StoreEntryL(TInt aNumber, CCalEntryView& aEntryView)
       
   375     {
       
   376     test.Printf(_L("Store Entries\n"));
       
   377     RPointerArray<CCalEntry> entriesToAdd;
       
   378     CleanupResetAndDestroyPushL(entriesToAdd);
       
   379     
       
   380     for(TInt ii=0; ii<aNumber; ++ii)
       
   381         { 
       
   382         TBool addAttachment = EFalse;
       
   383         TBuf8<64> uid;
       
   384         uid.Copy(KGuid());
       
   385         uid.AppendNum(ii);
       
   386         HBufC8* guid = uid.AllocL();
       
   387         if(ii%20 == 0)
       
   388              {
       
   389              addAttachment = ETrue;
       
   390              }
       
   391    
       
   392        CCalEntry* entry = CreateEntryLC(guid, addAttachment);
       
   393        entriesToAdd.AppendL(entry);
       
   394        CleanupStack::Pop(entry);        
       
   395        TInt success = 0;
       
   396        aEntryView.StoreL(entriesToAdd, success);
       
   397        entriesToAdd.ResetAndDestroy();
       
   398        }
       
   399     
       
   400     CleanupStack::PopAndDestroy(&entriesToAdd); 
       
   401     }
       
   402 /*
       
   403 @SYMTestCaseID              PIM-TCAL-backuprestore-0001
       
   404 @SYMPreq                    CR2033
       
   405 @SYMTestCaseDesc            Basic backup and restore test
       
   406 @SYMTestPriority            1
       
   407 @SYMTestType                CIT
       
   408 @SYMTestStatus              Implemented
       
   409 @SYMTestActions              
       
   410   1. Make sure that the Calendar file can be copied when backup or restore happens
       
   411   2. Make sure that the observer  (this) is notified when backup or restore event happens
       
   412   3. Make sure that the restored file contains same entries as expected.
       
   413                   
       
   414 @SYMTestExpectedResults     The above as described is satisfied
       
   415 **/
       
   416 void CBackRestoreTestManager::TestBackupRestoreL(TBool aStoreEntry, TBackupOrRestore aBackupOrRestore)
       
   417     {
       
   418     test.Printf(_L("Test Backup Restore\n"));
       
   419     if(aStoreEntry)
       
   420         {
       
   421         iTestLib->CleanDatabaseL();
       
   422         StoreEntryL(KNumerOfEntry, iTestLib->SynCGetEntryViewL());
       
   423         }
       
   424     //entriesBefore are those entries stored in the Calendar file before backup or restored happens
       
   425     RPointerArray<CCalEntry> entriesBefore;
       
   426     CleanupResetAndDestroyPushL(entriesBefore);   
       
   427     FetchAllEntryL(entriesBefore);
       
   428        
       
   429     //Register so that it is notified when backup\restore starts\ends
       
   430     RegisterNotificationL();
       
   431     
       
   432     //Start backup or restore process
       
   433    if(aBackupOrRestore ==  EBackup)
       
   434         {
       
   435         StartBackupL();
       
   436         }
       
   437     else
       
   438         {
       
   439         StartRestoreL();
       
   440         }
       
   441     CActiveScheduler::Start();
       
   442     
       
   443     // Test the notification has occured
       
   444     test(iNotificationCallBackOccurred);
       
   445     if(aBackupOrRestore ==  EBackup)
       
   446         {
       
   447         test(iNotificationChangeType==MCalChangeCallBack2::EBackupStart);
       
   448         //Simulate the backup process and make sure the file can be copied.
       
   449         iTestLib->PIMTestServer().CopyDirAndFilesL(KCalenderServerPath(), KTestFilePath());    
       
   450         }
       
   451     else
       
   452         {
       
   453         test(iNotificationChangeType==MCalChangeCallBack2::ERestoreStart);
       
   454         //Simulate the restore process and make sure the file can be copied.
       
   455         iTestLib->PIMTestServer().CopyDirAndFilesL( KTestFilePath(), KCalenderServerPath());    
       
   456         }
       
   457     
       
   458     iNotificationCallBackOccurred = EFalse;
       
   459     RPointerArray<CCalEntry> entriesAfter;
       
   460     CleanupResetAndDestroyPushL(entriesAfter);   
       
   461 
       
   462     // Test that the calendar file has been locked
       
   463     TRAPD(err, FetchAllEntryL(entriesAfter));
       
   464     test(err == KErrLocked);
       
   465     test(entriesAfter.Count()==0); 
       
   466    
       
   467     //Ends the backup or restore
       
   468     if(aBackupOrRestore ==  EBackup)
       
   469         {
       
   470         EndBackupL();
       
   471         }
       
   472     else
       
   473         {
       
   474         EndRestoreL();
       
   475         }
       
   476     CActiveScheduler::Start();
       
   477     
       
   478     // Test the notification has occured
       
   479     if(aBackupOrRestore ==  EBackup)
       
   480          {
       
   481          test(iNotificationChangeType==MCalChangeCallBack2::EBackupEnd);
       
   482          }
       
   483      else
       
   484          {
       
   485          test(iNotificationChangeType==MCalChangeCallBack2::ERestoreEnd);
       
   486          }
       
   487     
       
   488     //Test the file is unlocked and entries fetched are those stored in the restored file.
       
   489     FetchAllEntryL(entriesAfter);
       
   490     test(entriesAfter.Count()==KNumerOfEntry);
       
   491     test(CompareEntryL(entriesBefore,entriesAfter));
       
   492     CleanupStack::PopAndDestroy(&entriesAfter);
       
   493     CleanupStack::PopAndDestroy(&entriesBefore);
       
   494     iTestLib->GetSession().StopChangeNotification();
       
   495     }
       
   496 
       
   497 void CBackRestoreTestManager::TestNoCrashDuringRestoreL()
       
   498     {
       
   499     test.Printf(_L("Test calendar does not crash if a second session tries to open the file being restored\n"));
       
   500     iTestLib->CleanDatabaseL();
       
   501     StoreEntryL(KNumerOfEntry, iTestLib->SynCGetEntryViewL());
       
   502     
       
   503     //Register so that it is notified when backup\restore starts\ends
       
   504     RegisterNotificationL();
       
   505     
       
   506     //Start restore process
       
   507     StartRestoreL();
       
   508     
       
   509     CActiveScheduler::Start();
       
   510     
       
   511     // Test the notification has occured
       
   512     test(iNotificationCallBackOccurred);
       
   513     
       
   514     CCalSession* secondSession = CCalSession::NewL();
       
   515     CleanupStack::PushL(secondSession);
       
   516     TRAPD(err, secondSession->OpenL(KCalFile));
       
   517     if(err != KErrLocked)
       
   518         {
       
   519         User::Leave(err);
       
   520         }
       
   521        
       
   522     EndRestoreL();
       
   523     CActiveScheduler::Start();
       
   524     
       
   525     TRAP(err, secondSession->OpenL(KCalFile));
       
   526     if(err != KErrNone)
       
   527         {
       
   528         User::Leave(err);
       
   529         }
       
   530     CleanupStack::PopAndDestroy(secondSession);
       
   531     iTestLib->GetSession().StopChangeNotification();
       
   532     }
       
   533 /**
       
   534 @SYMTestCaseID              PIM-TCAL-backuprestore-0002
       
   535 @SYMPreq                    CR2033
       
   536 @SYMTestCaseDesc            Test that asynchronous deleting is being carried out when restore is in progress
       
   537 @SYMTestPriority            1
       
   538 @SYMTestType                CIT
       
   539 @SYMTestStatus              Implemented
       
   540 @SYMTestActions              
       
   541   1. Make sure that the asynchronous operation can be interruppted by restore event.
       
   542   2. Make sure that the file can be restored when asnchronous deleting is being carried out.
       
   543                   
       
   544 @SYMTestExpectedResults     TThe above as described is satisfied
       
   545 **/
       
   546 void CBackRestoreTestManager::TestAsynDeleteL()
       
   547     {
       
   548     test.Printf(_L("Test Asynchronous Delete\n"));
       
   549   
       
   550     //Prepare the fro the test
       
   551     // - Store some entries 
       
   552     // - Fetch entries for comparison later.
       
   553     // - Save the file so that it can be restored later.
       
   554     iTestLib->CleanDatabaseL();
       
   555     StoreEntryL(KNumerOfEntry, iTestLib->SynCGetEntryViewL());
       
   556     iTestLib->CloseAgendaServer();
       
   557     iTestLib->PIMTestServer().CopyDirAndFilesL(KCalenderServerPath(), KTestFilePath());
       
   558     iTestLib->OpenFileL(KCalFile);
       
   559     RPointerArray<CCalEntry> entriesBefore;
       
   560     CleanupResetAndDestroyPushL(entriesBefore);   
       
   561     FetchAllEntryL(entriesBefore);
       
   562  
       
   563     // Carry out the asynchronous deletion operation
       
   564     TCalTime startTime;
       
   565     TCalTime endTime;
       
   566     startTime.SetTimeLocalL(TCalTime::MinTime());
       
   567     endTime.SetTimeLocalL(TCalTime::MaxTime());
       
   568     CalCommon::TCalTimeRange range(startTime,endTime);
       
   569     iTestLib->SynCGetEntryViewL().DeleteL(range, CalCommon::EIncludeAll, *this); 
       
   570 
       
   571     //Start the restore  
       
   572     RegisterNotificationL();
       
   573     StartRestoreL();
       
   574     CActiveScheduler::Start();
       
   575     test(iNotificationCallBackOccurred);
       
   576     test(iNotificationChangeType==MCalChangeCallBack2::ERestoreStart);
       
   577     //Deletion could be completed or aborted.
       
   578     test(iDeleteCompleteErrCode == KErrCancel || iDeleteCompleteErrCode == KErrNone);
       
   579     // Retore the original calendar file (with KNumerOfEntry entries)
       
   580     iTestLib->PIMTestServer().CopyDirAndFilesL( KTestFilePath(), KCalenderServerPath());
       
   581 
       
   582     RPointerArray<CCalEntry> entriesAfter;
       
   583     CleanupResetAndDestroyPushL(entriesAfter);   
       
   584    
       
   585     TRAPD(err, FetchAllEntryL(entriesAfter));
       
   586     test(err == KErrLocked);
       
   587     test(entriesAfter.Count()==0);
       
   588    
       
   589     //Ends the backup and test the notification
       
   590     EndRestoreL();
       
   591     CActiveScheduler::Start();
       
   592     test(iNotificationChangeType==MCalChangeCallBack2::ERestoreEnd);
       
   593     
       
   594     //test the file is unlocked.
       
   595     FetchAllEntryL(entriesAfter);
       
   596     test(entriesAfter.Count()==KNumerOfEntry);
       
   597     //The entries should be those in restored file.
       
   598     test(CompareEntryL(entriesBefore,entriesAfter));
       
   599     CleanupStack::PopAndDestroy(&entriesAfter);
       
   600     CleanupStack::PopAndDestroy(&entriesBefore);
       
   601     iTestLib->GetSession().StopChangeNotification();
       
   602     iNotificationCallBackOccurred = EFalse;
       
   603     }
       
   604 
       
   605 /*
       
   606 @SYMTestCaseID              PIM-TCAL-backuprestore-0003
       
   607 @SYMPreq                    CR2033
       
   608 @SYMTestCaseDesc            Test the backup or restore when there are more than one Calendar files held in the calendar server
       
   609 @SYMTestPriority            1
       
   610 @SYMTestType                CIT
       
   611 @SYMTestStatus              Implemented
       
   612 @SYMTestActions              
       
   613  * 1. Make sure that the Calendar file can be copied when backup or restore happens
       
   614  * 2. Make sure that  observer  (this) is notified when backup or restore event happens
       
   615  * 3. Make sure that the restored file contains same entries as expected.
       
   616                   
       
   617 @SYMTestExpectedResults     The above as described is satisfied
       
   618 **/
       
   619 void CBackRestoreTestManager::TestMultipleCalendarBackupRestoreL(TBackupOrRestore aBackupOrRestore)
       
   620     {
       
   621     test.Printf(_L("Test there are multiple files when backup or restore is in progress\n"));
       
   622   
       
   623   //Prepare for the test
       
   624     //Create the second file and open it
       
   625     CCalSession* secondSession = CCalSession::NewL(iTestLib->GetSession()); 
       
   626     CleanupStack::PushL(secondSession);
       
   627     TRAPD(err, secondSession->CreateCalFileL(KCalSecondFile()));
       
   628     if (err == KErrAlreadyExists)
       
   629         {
       
   630         secondSession->DeleteCalFileL(KCalSecondFile());    
       
   631         secondSession->CreateCalFileL(KCalSecondFile());
       
   632         }
       
   633     secondSession->OpenL(KCalSecondFile());
       
   634     CCalEntryView* secondView =CCalEntryView::NewL(*secondSession);
       
   635     CleanupStack::PushL(secondView);
       
   636    
       
   637     //Store some entries in both calendar files
       
   638     iTestLib->CleanDatabaseL();
       
   639     StoreEntryL(KNumerOfEntry/2, iTestLib->SynCGetEntryViewL());
       
   640     StoreEntryL(KNumerOfEntry/2, *secondView);
       
   641     
       
   642     //Fetch entries in both calendar for comparison later
       
   643     RPointerArray<CCalEntry> entriesBefore1;
       
   644     CleanupResetAndDestroyPushL(entriesBefore1);   
       
   645     FetchAllEntryL(entriesBefore1);
       
   646     
       
   647     RPointerArray<CCalEntry> entriesBefore2;
       
   648     CleanupResetAndDestroyPushL(entriesBefore2);   
       
   649     FetchAllEntryL(entriesBefore2);
       
   650     
       
   651     //Start backup or restore and test the notification
       
   652     RegisterNotificationL();
       
   653     if(aBackupOrRestore ==  EBackup)
       
   654         {
       
   655         StartBackupL();
       
   656         }
       
   657     else
       
   658         {
       
   659         StartRestoreL();
       
   660         }
       
   661     CActiveScheduler::Start();
       
   662     test(iNotificationCallBackOccurred);
       
   663     if(aBackupOrRestore == EBackup)
       
   664         {
       
   665         test(iNotificationChangeType==MCalChangeCallBack2::EBackupStart);
       
   666         // Make sure the file can be copied.
       
   667         iTestLib->PIMTestServer().CopyDirAndFilesL(KCalenderServerPath(), KTestFilePath());    
       
   668         }
       
   669     else
       
   670         {
       
   671         test(iNotificationChangeType==MCalChangeCallBack2::ERestoreStart);
       
   672         iTestLib->PIMTestServer().CopyDirAndFilesL(KTestFilePath(), KCalenderServerPath());    
       
   673         }
       
   674     iNotificationCallBackOccurred = EFalse;
       
   675     
       
   676     // test that the calendar file has been loced
       
   677     RPointerArray<CCalEntry> entriesAfter1;
       
   678     CleanupResetAndDestroyPushL(entriesAfter1);   
       
   679     RPointerArray<CCalEntry> entriesAfter2;
       
   680     CleanupResetAndDestroyPushL(entriesAfter2);   
       
   681 
       
   682     TRAP(err, FetchAllEntryL(entriesAfter1));
       
   683     test(err == KErrLocked);
       
   684     TRAP(err, FetchAllEntryL(entriesAfter2));
       
   685     test(err == KErrLocked);
       
   686     test(entriesAfter1.Count()==0);
       
   687     test(entriesAfter2.Count()==0);   
       
   688       
       
   689     //Ends the backup or restore and test the notification
       
   690     if(aBackupOrRestore ==  EBackup)
       
   691         {
       
   692         EndBackupL();
       
   693         }
       
   694     else
       
   695         {
       
   696         EndRestoreL();
       
   697         }
       
   698     CActiveScheduler::Start();
       
   699     if(aBackupOrRestore ==  EBackup)
       
   700          {
       
   701          test(iNotificationChangeType==MCalChangeCallBack2::EBackupEnd);
       
   702          }
       
   703      else
       
   704          {
       
   705          test(iNotificationChangeType==MCalChangeCallBack2::ERestoreEnd);
       
   706          }
       
   707     
       
   708     //Test the file are unlocked.
       
   709     FetchAllEntryL(entriesAfter1);
       
   710     FetchAllEntryL(entriesAfter2);
       
   711     //Entries are those in the current file (backup test) or restored file (retore test).
       
   712     test(entriesAfter1.Count()==KNumerOfEntry/2);
       
   713     test(CompareEntryL(entriesBefore1,entriesAfter1));
       
   714     test(entriesAfter2.Count()==KNumerOfEntry/2);
       
   715     test(CompareEntryL(entriesBefore2,entriesAfter2));
       
   716  
       
   717     CleanupStack::PopAndDestroy(&entriesAfter2);
       
   718     CleanupStack::PopAndDestroy(&entriesAfter1);
       
   719     CleanupStack::PopAndDestroy(&entriesBefore2);
       
   720     CleanupStack::PopAndDestroy(&entriesBefore1);
       
   721     CleanupStack::PopAndDestroy(secondView);
       
   722     secondSession->DeleteCalFileL(KCalSecondFile()); 
       
   723     CleanupStack::PopAndDestroy(secondSession);
       
   724     iTestLib->GetSession().StopChangeNotification();
       
   725     }
       
   726 /*
       
   727 @SYMTestCaseID              PIM-TCAL-backuprestore-0004
       
   728 @SYMPreq                    CR2033
       
   729 @SYMTestCaseDesc            Test the operations which are not affected by a backup or restore event
       
   730 @SYMTestPriority            1
       
   731 @SYMTestType                CIT
       
   732 @SYMTestStatus              Implemented
       
   733 @SYMTestActions              
       
   734  * Most of operations are not permitted when backup or restore happens. However, some of operations
       
   735  * such as notification request, enabling broadcast and closing calenda should
       
   736  * be permitted when backup or restore happens.
       
   737                   
       
   738 @SYMTestExpectedResults     The above as described is satisfied
       
   739 **/
       
   740 void CBackRestoreTestManager::TestOperationsCanBePerformedL(TBackupOrRestore aBackupOrRestore)
       
   741     {
       
   742     test.Printf(_L("Test the operations which can be performed when backup or restore happens\n"));
       
   743     
       
   744     RegisterNotificationL();
       
   745     if(aBackupOrRestore == EBackup)
       
   746         {
       
   747         StartBackupL();
       
   748         }
       
   749     else
       
   750         {
       
   751         StartRestoreL();
       
   752         }
       
   753     CActiveScheduler::Start();
       
   754     
       
   755     RegisterNotificationL(); 
       
   756     //The following operation should be successful when backup or restore happens
       
   757     iTestLib->GetSession().StopChangeNotification();
       
   758     iTestLib->GetSession().EnableChangeBroadcast();
       
   759     iTestLib->GetSession().DisableChangeBroadcast();
       
   760     iTestLib->CloseAgendaServer();
       
   761     
       
   762     if(aBackupOrRestore == EBackup)
       
   763         {
       
   764         EndBackupL();
       
   765         }
       
   766     else
       
   767         {
       
   768         EndRestoreL();
       
   769         }
       
   770     
       
   771     DeleteTestFilesL();
       
   772     User::After(1000000);//Make sure backup\restore ends before opening the file. We can't use the notification because the file is closed.
       
   773     iTestLib->OpenFileL(KCalFile);//Open the file so that it comes back the test condition before it starts
       
   774     }
       
   775 
       
   776 /*
       
   777 @SYMTestCaseID              PIM-TCAL-backuprestore-0005
       
   778 @SYMPreq                    CR2033
       
   779 @SYMTestCaseDesc            Test that an earlier version Calendar file is retored.
       
   780 @SYMTestPriority            1
       
   781 @SYMTestType                CIT
       
   782 @SYMTestStatus              Implemented
       
   783 @SYMTestActions              
       
   784  * 1. Make sure that a compatible early version Calendar file can be restored and opened.
       
   785  * 2. Make sure that a correct notification is received when restoring an incompatible early version of Calendar file.
       
   786  * 3. Make sure that a correct notification is received when the restored file is corrupted.
       
   787  * 4. Make sure that an non-usable session can be closed and an non-usable file can be deleted.
       
   788                   
       
   789 @SYMTestExpectedResults     The above as described is satisfied
       
   790 **/
       
   791 void CBackRestoreTestManager::TestRestoreEealyVersionCalFileL(TRestoredFile aRestoredFile)
       
   792     {
       
   793     test.Printf(_L("Test Restore an Eealy Version Calendar FileL\n"));
       
   794     
       
   795     //Prepare the test: Save the current file to the testing directory so that it can be copied back.
       
   796     DeleteTestFilesL();
       
   797     iTestLib->CloseAgendaServer();
       
   798     iTestLib->PIMTestServer().CopyDirAndFilesL(KCalenderServerPath(), KTestFilePath());
       
   799     TRAPD(err,iTestLib->OpenFileL(KCalFile));
       
   800     iTestLib->SynCGetEntryViewL();
       
   801     TFileName currentFile = KCalenderServerPath();
       
   802     currentFile.Append(KCalFile());
       
   803     
       
   804     //Start the restore event
       
   805     RegisterNotificationL();
       
   806     StartRestoreL();
       
   807     CActiveScheduler::Start();
       
   808     //copy an earlly version calendar file to the current calendar file
       
   809     TBool restoredGoodFile = ETrue;
       
   810     switch (aRestoredFile)
       
   811         {
       
   812         case EFileVersion95:
       
   813             iTestLib->PIMTestServer().CopyFileL(KCalfile95, currentFile);          
       
   814             break;
       
   815         case EFileVersion94:
       
   816             iTestLib->PIMTestServer().CopyFileL(KCalfile94, currentFile);
       
   817             break;
       
   818         case EFileNotSupported:
       
   819             iTestLib->PIMTestServer().CopyFileL(KCalfileNotSupported, currentFile);
       
   820             restoredGoodFile = EFalse;
       
   821             break;
       
   822         case EFileCurrupted:
       
   823             iTestLib->PIMTestServer().CopyFileL(KCalfileCorrupt, currentFile);
       
   824             restoredGoodFile = EFalse;
       
   825             break;
       
   826         default:
       
   827             break;
       
   828         }
       
   829       
       
   830     EndRestoreL();
       
   831     CActiveScheduler::Start();
       
   832     test(iNotificationCallBackOccurred);
       
   833     
       
   834     //Test the correct notification is received depend on whether the file is valid or not
       
   835     if(restoredGoodFile)
       
   836         {
       
   837         test(iNotificationChangeType==MCalChangeCallBack2::ERestoreEnd); 
       
   838         RPointerArray<CCalEntry> entries;
       
   839         CleanupResetAndDestroyPushL(entries);   
       
   840         FetchAllEntryL(entries);
       
   841         test(entries.Count()>0);
       
   842         CleanupStack::PopAndDestroy(&entries);
       
   843         }
       
   844     else
       
   845         {
       
   846         test(iNotificationChangeType==MCalChangeCallBack2::ERestoredFileCanNotBeOpened); 
       
   847         }
       
   848     
       
   849     // Make sure the file can be deleted and server can be closed whatever the restored file is good one or not.
       
   850     iTestLib->GetSession().DeleteCalFileL(KCalFile);
       
   851     iTestLib->CloseAgendaServer();
       
   852     
       
   853     //Copied the original file to the current file.
       
   854     iTestLib->PIMTestServer().CopyDirAndFilesL(KTestFilePath, KCalenderServerPath());
       
   855     iTestLib->OpenFileL(KCalFile);//Open the file so that it comes back the test condition before it starts
       
   856     iNotificationCallBackOccurred = EFalse;
       
   857     }
       
   858 
       
   859 void CBackRestoreTestManager::FetchAllEntryL(RPointerArray<CCalEntry>& aEntries)
       
   860     {
       
   861     CCalIter& iter = iTestLib->GetIterL();
       
   862     for (TPtrC8 Uid(iter.FirstL()); Uid != KNullDesC8(); Uid.Set(iter.NextL()))
       
   863         {
       
   864         iTestLib->SynCGetEntryViewL().FetchL(Uid, aEntries);
       
   865         }
       
   866     
       
   867    const TInt count = aEntries.Count();
       
   868    for (TInt ii=0; ii< count; ii++)
       
   869        {//Make sure offline properties are loaded 
       
   870        aEntries[ii]->SummaryL();
       
   871        aEntries[ii]->DescriptionL();
       
   872        aEntries[ii]->LocationL();
       
   873        CCalAlarm* alarm = aEntries[ii]->AlarmL();
       
   874        delete alarm;
       
   875        }
       
   876    }
       
   877 
       
   878 TBool CBackRestoreTestManager::CompareEntryL(RPointerArray<CCalEntry>& entriesSource, RPointerArray<CCalEntry>& entriesTocompare)
       
   879     {
       
   880     TInt sourceCount = entriesSource.Count();
       
   881     TInt CompareCount = entriesTocompare.Count();
       
   882     if( sourceCount!= CompareCount)
       
   883         {
       
   884         return EFalse;
       
   885         }
       
   886     //Make a copy of two entry arraies to make the comparison easier without changing the arraies past in.
       
   887     RPointerArray<CCalEntry> sourceCopy;
       
   888     CleanupClosePushL(sourceCopy);
       
   889    
       
   890     RPointerArray<CCalEntry> compareCopy;
       
   891     CleanupClosePushL(compareCopy);
       
   892    
       
   893     for (TInt count = 0; count < sourceCount; count++)
       
   894         {
       
   895         sourceCopy.AppendL( entriesSource[count]);
       
   896         compareCopy.AppendL( entriesSource[count]);
       
   897         }
       
   898    
       
   899     TBool same = EFalse;
       
   900      
       
   901     for (TInt i = 0; i < sourceCount; i++)
       
   902         {
       
   903         same = EFalse;
       
   904         TInt j = 0;
       
   905         CompareCount = compareCopy.Count();
       
   906         while (!same && j<CompareCount)
       
   907           {
       
   908           if (compareCopy[j]->CompareL(*sourceCopy[i]))
       
   909               {
       
   910               same = ETrue;
       
   911               compareCopy.Remove(j);   
       
   912               }
       
   913           ++j;
       
   914           }
       
   915         
       
   916         if(!same)
       
   917             {
       
   918             break;
       
   919             }
       
   920         }
       
   921       
       
   922     CleanupStack::PopAndDestroy(&compareCopy);
       
   923     CleanupStack::PopAndDestroy(&sourceCopy);
       
   924 
       
   925     return same;
       
   926     }
       
   927 
       
   928 void CBackRestoreTestManager::RunAllTestsL()
       
   929     { 
       
   930     //Basic tests - Store some entries and test that the entries can not be fetched when backup\retore is in progress.
       
   931     test.Printf(_L("Basic tests - test backup\n"));
       
   932     TestBackupRestoreL(ETrue, EBackup);
       
   933     test.Printf(_L("Basic tests - test restore\n"));
       
   934     TestBackupRestoreL(ETrue, ERestore);
       
   935  
       
   936     //Smilar to basic tests but both Calendar file and it's index file exist when backup\retore is in progress.
       
   937     test.Printf(_L("close the calendar server\n"));
       
   938     iTestLib->CloseAgendaServer();
       
   939     DeleteTestFilesL();
       
   940     iTestLib->OpenFileL(KCalFile);
       
   941     test.Printf(_L("test backup after close and reopen a file\n"));
       
   942     TestBackupRestoreL(EFalse, EBackup);
       
   943     test.Printf(_L("test restore after close and reopen a file\n"));
       
   944     TestBackupRestoreL(EFalse, ERestore);
       
   945     
       
   946     //Test that some operations are not affected by backup\restore.
       
   947     TestOperationsCanBePerformedL(EBackup);
       
   948     TestOperationsCanBePerformedL(ERestore);
       
   949  
       
   950     //Test that asynchronous deletion is being carried out when backup\restore is in progress.
       
   951     DeleteTestFilesL();
       
   952     TestAsynDeleteL();
       
   953  
       
   954     //Test that an earlier version Calendar file is retored.
       
   955     TestRestoreEealyVersionCalFileL(EFileVersion95);
       
   956     TestRestoreEealyVersionCalFileL(EFileVersion94);
       
   957     TestRestoreEealyVersionCalFileL(EFileCurrupted);
       
   958     TestRestoreEealyVersionCalFileL(EFileNotSupported);
       
   959     
       
   960     // Similar to the basic tests but there are more than one Calendar files are involved.
       
   961     DeleteTestFilesL();
       
   962     TestMultipleCalendarBackupRestoreL(EBackup); 
       
   963     TestMultipleCalendarBackupRestoreL(ERestore);
       
   964     TestNoCrashDuringRestoreL();
       
   965     }
       
   966 	
       
   967 static void DoTestL()
       
   968 	{
       
   969 	BackupClient = conn::CSBEClient::NewL();
       
   970 	CleanupStack::PushL(BackupClient);
       
   971 
       
   972 	CBackRestoreTestManager* testManager = CBackRestoreTestManager::NewLC();
       
   973 	TPerformanceTimer timer(test);
       
   974 
       
   975 	testManager->RunAllTestsL();	
       
   976 
       
   977 	test.Printf(_L("Done\n"));
       
   978 	
       
   979 	CleanupStack::PopAndDestroy(testManager);
       
   980 	CleanupStack::PopAndDestroy(BackupClient);
       
   981 	}
       
   982 
       
   983 TInt E32Main()
       
   984     {
       
   985 	__UHEAP_MARK;
       
   986 
       
   987 	test.Start(_L("@SYMTESTCaseID:PIM-TCAL-BACKUPRESTORE-0001 Calendar tcal_backuprestore test suite"));
       
   988 	test.Title();
       
   989 
       
   990 	CTrapCleanup* trapCleanup = CTrapCleanup::New();
       
   991 	if (!trapCleanup)
       
   992 		{
       
   993 		return KErrNoMemory;
       
   994 		}
       
   995 
       
   996 	CActiveScheduler* scheduler = new CActiveScheduler();
       
   997 	if (!scheduler)
       
   998 		{
       
   999 		delete trapCleanup;
       
  1000 		return KErrNoMemory;
       
  1001 		}
       
  1002 	CActiveScheduler::Install(scheduler);	
       
  1003 
       
  1004 	TRAPD(ret, DoTestL());
       
  1005 	test.Printf(_L("ret is:%d\n"),ret);
       
  1006 	test(ret == KErrNone);
       
  1007 	
       
  1008 	delete scheduler;
       
  1009 	delete trapCleanup;	
       
  1010 
       
  1011 	test.End();
       
  1012 	test.Close();
       
  1013 
       
  1014 	__UHEAP_MARKEND;
       
  1015 
       
  1016 	return (KErrNone);
       
  1017     }