pimappservices/calendar/tsrc/unit/src/TestCalInterimApiUserInt.cpp
changeset 0 f979ecb2b13e
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 // Copyright (c) 2007-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 "TestCalInterimApiUserInt.h"
       
    17 #include "TestCalInterimApiDefines.h"
       
    18 
       
    19 #include <calentry.h>
       
    20 #include <calsession.h>
       
    21 #include <calinstance.h>
       
    22 #include <caldataexchange.h>
       
    23 #include <caldataformat.h>
       
    24 
       
    25 _LIT( KUserIntTestCal, "c:userinttest.dat" );
       
    26 _LIT( KExportFile,     "c:\\userintExportTest.vcs" );
       
    27 _LIT( KInvalidVcsFile, "\\CITCalInterimApi\\InvalidUserInt.vcs" );
       
    28 _LIT8( KEntryUid,  "EntryOne" );
       
    29 _LIT8( KEntryUid2, "EntryTwo" );
       
    30 
       
    31 TFileName GetFullFileName(const TDesC& aPath)
       
    32 	{
       
    33 	TFileName fileName;
       
    34 #ifdef __WINS__
       
    35 	fileName.Append(_L("c:"));
       
    36 #else
       
    37 	TFileName fullName = RProcess().FileName();
       
    38 	TParsePtrC parse(fullName);
       
    39 	fileName.Append(parse.Drive());
       
    40 #endif
       
    41 	fileName.Append(aPath);
       
    42 	return fileName;
       
    43 	}
       
    44 
       
    45 CUserIntTestStep::CUserIntTestStep()
       
    46  	{
       
    47  	SetTestStepName( KUserIntTestStepName );
       
    48  	}
       
    49 
       
    50 CUserIntTestStep::~CUserIntTestStep()
       
    51 	{
       
    52 	delete iEntryView;
       
    53 	delete iDataExchange;
       
    54 	delete iSession;
       
    55 	delete iScheduler;
       
    56 	}
       
    57 
       
    58 TVerdict CUserIntTestStep::doTestStepPreambleL()
       
    59     {
       
    60 	TVerdict ret = CTestStep::doTestStepPreambleL();
       
    61 
       
    62 	iScheduler = new(ELeave) CActiveScheduler;
       
    63 	CActiveScheduler::Install( iScheduler );
       
    64     
       
    65     iSession = CCalSession::NewL();
       
    66     OpenCalendarFileL();
       
    67 
       
    68     iEntryView = CCalEntryView::NewL( *iSession );
       
    69     iDataExchange = CCalDataExchange::NewL( *iSession );
       
    70 
       
    71     return ret;
       
    72 	}	
       
    73 
       
    74 TVerdict CUserIntTestStep::doTestStepL()
       
    75     {
       
    76     // Tests storage and retrieval, importing, exporting with positive user 
       
    77     // integer.
       
    78     TUint32 userInt1 = 0xffffffff;
       
    79     StoreAndRetrieveEntryL( KEntryUid(), userInt1 );
       
    80     ExportEntryL( KEntryUid() );
       
    81     ImportEntryL( KExportFile(), userInt1 );
       
    82     
       
    83     // Change the user integer of the entry just added.
       
    84     TInt newUserInt = 1000;
       
    85     StoreAndRetrieveEntryL( KEntryUid(), newUserInt );
       
    86 
       
    87     // Tests storage and retrieval, importing, exporting with negative user
       
    88     // integer. The exported value should be unsigned. Versit only supports 
       
    89     // signed ints so the user int is exported as a string instead. Versit 
       
    90     // doesn't support importing values with a '-' sign even though the type 
       
    91     // is a signed int, and if a negative value is imported, the value gets 
       
    92     // set to 0. The value is imported as an integer and implicitely converted
       
    93     // to a signed int. This tests that negative values aren't exported.
       
    94     TInt negUserInt = -500;
       
    95     StoreAndRetrieveEntryL( KEntryUid2(), negUserInt );
       
    96     ExportEntryL( KEntryUid2() );
       
    97     ImportEntryL( KExportFile(), negUserInt );
       
    98     
       
    99     // Imports an existing vcs with a negative user integer. Negative values
       
   100     // are invalid and the user int should remain the default value of 0.
       
   101     
       
   102     
       
   103     
       
   104     ImportEntryL( GetFullFileName(KInvalidVcsFile), 0 );
       
   105 
       
   106     // Test copying of entries.
       
   107     TestCopyEntriesL();
       
   108 
       
   109     // Test comparing entries.
       
   110     TestCompareL();
       
   111 
       
   112     return TestStepResult();
       
   113 	}
       
   114 
       
   115 TVerdict CUserIntTestStep::doTestStepPostambleL()
       
   116 	{
       
   117 	return TestStepResult();
       
   118 	}
       
   119 
       
   120 void CUserIntTestStep::OpenCalendarFileL( TBool aReplaceIfExists )
       
   121 	{
       
   122 	TBuf<256> buffer;
       
   123 	TRAPD( err, iSession->CreateCalFileL( KUserIntTestCal ) );
       
   124 	if (err == KErrAlreadyExists && aReplaceIfExists)
       
   125 		{
       
   126 		iSession->DeleteCalFileL( KUserIntTestCal );
       
   127 		iSession->CreateCalFileL( KUserIntTestCal );	
       
   128 		}
       
   129 	iSession->OpenL( KUserIntTestCal() );
       
   130 	}
       
   131 
       
   132 /**
       
   133 Store and retrieve user integer.  A calendar entry's user integer is set and 
       
   134 the entry is stored. The same entry is then retrieved from the database. The 
       
   135 user integer of the retrieved entry should match the entry that was set.
       
   136 
       
   137 @param aUid The uid of the entry to store. To test changing an entry, the uid 
       
   138   of an already stored entry is specified for aUid.
       
   139 @param aUserIntVal The user integer value for the entry.
       
   140 */
       
   141 void CUserIntTestStep::StoreAndRetrieveEntryL( const TDesC8& aUid, 
       
   142     TUint32 aUserIntVal )
       
   143     {
       
   144     INFO_PRINTF2( _L("Creating and storing an entry. User int=%u"),
       
   145         aUserIntVal);
       
   146 
       
   147     CCalEntry* entry = CreateEntryL( aUid, aUserIntVal );
       
   148     CleanupStack::PushL( entry );
       
   149 
       
   150     // Store the entry.
       
   151     RPointerArray<CCalEntry> entriesToStore;
       
   152     entriesToStore.Append( entry );
       
   153     TInt numEntriesStored = 0;
       
   154     iEntryView->StoreL( entriesToStore, numEntriesStored );
       
   155     ASSERT( numEntriesStored == 1 );
       
   156 
       
   157     CleanupStack::PopAndDestroy( entry );
       
   158     
       
   159     ReOpenSession();
       
   160 
       
   161     // Verify entry was stored by retrieving it.    
       
   162     RetrieveEntryL( aUid, aUserIntVal );
       
   163     }
       
   164 
       
   165 /** 
       
   166 Tests that the user integer was stored in the calendar database and
       
   167 can be retrieved again. The entry is first stored using 
       
   168 CreateAndStoreEntryL(). The user integer is checked against the expected
       
   169 value once retrieved.
       
   170 
       
   171 @param aUid The uid of the entry to retrieve and was previously stored.
       
   172 @param aUserIntVal The expected user integer value of the entry.
       
   173 */
       
   174 void CUserIntTestStep::RetrieveEntryL( const TDesC8& aUid, 
       
   175     TUint32 aExpectedUserInt )
       
   176     {
       
   177     INFO_PRINTF2( _L("Retrieving entry. Expected user int = %u"), 
       
   178         aExpectedUserInt);
       
   179 
       
   180     // Retrieve the entry.
       
   181     RPointerArray<CCalEntry> entriesStored;
       
   182     CleanupStack::PushL( &entriesStored );
       
   183     iEntryView->FetchL( aUid, entriesStored );
       
   184     ASSERT( entriesStored.Count() == 1 );
       
   185 
       
   186     // Check the user integer.
       
   187     CCalEntry* entry = entriesStored[0];
       
   188     TUint32 restoredUserInt = entry->UserIntL();
       
   189     if (restoredUserInt != aExpectedUserInt )
       
   190         {
       
   191         INFO_PRINTF1( _L("Test failed: Retrieved user int not expected \
       
   192             value." ) );
       
   193         SetTestStepResult( EFail );
       
   194         }
       
   195 
       
   196     CleanupStack::Pop( &entriesStored );
       
   197     entriesStored.ResetAndDestroy();
       
   198     }
       
   199 
       
   200 /**
       
   201 An entry from the calendar database is retrieved and then exported.This is used
       
   202 with ImportEntryL() to fully verify the entry was exported.
       
   203 
       
   204 @param aUid The uid of an entry existing in the database to be retrieved
       
   205     and then exported.
       
   206 */
       
   207 void CUserIntTestStep::ExportEntryL( const TDesC8& aUid )
       
   208     {
       
   209     INFO_PRINTF1( _L("Exporting entry.") );
       
   210     
       
   211     // Retrieve the entry.
       
   212     RPointerArray<CCalEntry> entriesStored;
       
   213     CleanupStack::PushL( &entriesStored );
       
   214     iEntryView->FetchL( aUid, entriesStored );
       
   215     ASSERT( entriesStored.Count() == 1 );
       
   216 
       
   217     // Create the export file.
       
   218     RFs fs;
       
   219     CleanupClosePushL( fs );
       
   220     User::LeaveIfError( fs.Connect() );
       
   221 	RFile outFile;
       
   222 	TInt err = outFile.Replace( fs, KExportFile, EFileWrite );
       
   223 	User::LeaveIfError( err );
       
   224 
       
   225     // Export the entry that was retrieved.
       
   226     RFileWriteStream writeStream( outFile );
       
   227     CleanupClosePushL( writeStream );
       
   228     iDataExchange->ExportL( KUidVCalendar, writeStream, entriesStored );
       
   229     writeStream.CommitL();
       
   230 
       
   231     CleanupStack::PopAndDestroy( &writeStream );
       
   232     CleanupStack::PopAndDestroy( &fs );
       
   233     CleanupStack::Pop( &entriesStored );
       
   234     entriesStored.ResetAndDestroy();
       
   235     }
       
   236 
       
   237 /**
       
   238 An exported vCal that has an X-SYMBIAN-USERINT property is  imported and the 
       
   239 user integer of the entry is checked. The user integer of the entry should 
       
   240 match the value of the exported X-SYMBIAN-USERINT property.
       
   241  
       
   242 @param aExpectedUserInt The expected value of the imported user integer.
       
   243 */
       
   244 void CUserIntTestStep::ImportEntryL( const TDesC& aFileName, 
       
   245 	TUint32 aExpectedUserInt )
       
   246     {
       
   247     INFO_PRINTF2( _L("Importing entry. Expected user int = %u"), 
       
   248         aExpectedUserInt );
       
   249     
       
   250     RFs fs;
       
   251     User::LeaveIfError( fs.Connect() );	
       
   252     CleanupClosePushL( fs );
       
   253     
       
   254     // Open the file to import.
       
   255 	RFile inFile;
       
   256 	inFile.Open( fs, aFileName, EFileRead );
       
   257     RFileReadStream readStream( inFile );
       
   258     CleanupClosePushL( readStream );
       
   259     RPointerArray<CCalEntry> importedEntries;
       
   260     CleanupStack::PushL( &importedEntries );
       
   261 
       
   262     // Import the file that was previously exported.
       
   263     iDataExchange->ImportL( KUidVCalendar, readStream, importedEntries );
       
   264     ASSERT( importedEntries.Count() == 1 );
       
   265     
       
   266     // Verify that the user integer is the expected value.
       
   267     CCalEntry* importedEntry = importedEntries[0];
       
   268     TUint32 userInt = importedEntry->UserIntL();
       
   269     if (userInt != aExpectedUserInt)
       
   270         {
       
   271         INFO_PRINTF1( _L("Test failed: Imported user int not expected \
       
   272             value." ) );
       
   273         SetTestStepResult( EFail );
       
   274         }
       
   275 
       
   276     CleanupStack::Pop( &importedEntries );
       
   277     importedEntries.ResetAndDestroy();
       
   278     CleanupStack::PopAndDestroy( &readStream );
       
   279     CleanupStack::PopAndDestroy( &fs );
       
   280     }
       
   281 
       
   282 /**
       
   283 Creates a new entry. The only configurable field is the user integer.
       
   284 
       
   285 @param aUserInt The value of the user integer.
       
   286 @return The entry that was created.
       
   287 */   
       
   288 CCalEntry* CUserIntTestStep::CreateEntryL( const TDesC8& aUid, TUint32 aUserInt )
       
   289     {
       
   290 	HBufC8* guid = aUid.AllocLC();
       
   291 
       
   292     CCalEntry* entry = CCalEntry::NewL( CCalEntry::EEvent, guid,
       
   293         CCalEntry::EMethodNone, 0 );
       
   294 	CleanupStack::Pop( guid );
       
   295 	CleanupStack::PushL( entry );
       
   296 
       
   297     TTime startTime(TDateTime(2007, EFebruary, 1, 9, 0, 0, 0));
       
   298     TCalTime calStartTime;
       
   299     calStartTime.SetTimeLocalL( startTime );
       
   300     entry->SetStartAndEndTimeL( calStartTime, calStartTime );
       
   301     entry->SetSummaryL( _L("user int test entry") );
       
   302 
       
   303     // Set the user integer.
       
   304     entry->SetUserIntL( aUserInt );
       
   305 
       
   306     CleanupStack::Pop( entry );
       
   307     return entry;
       
   308     }
       
   309 
       
   310 /**
       
   311 Copy an entry. An entry is created and the user integer set. The entry is 
       
   312 then copied to a new entry.The entries should not be considered the same.
       
   313 */
       
   314 void CUserIntTestStep::TestCopyEntriesL()
       
   315     {
       
   316     TUint32 userInt = 9999999;
       
   317     INFO_PRINTF2( _L("Testing CopyFromL. User int = %u"), userInt );
       
   318     
       
   319     // Create two entries with different user integer values.
       
   320     CCalEntry* srcEntry = CreateEntryL( KEntryUid(), userInt );
       
   321     CleanupStack::PushL( srcEntry );
       
   322     CCalEntry* destEntry = CreateEntryL( KEntryUid(), 0 );
       
   323     CleanupStack::PushL( destEntry );
       
   324 
       
   325     // User integer values should be different.
       
   326     TUint32 srcUserInt = srcEntry->UserIntL();
       
   327     TUint32 destUserInt = destEntry->UserIntL();
       
   328     ASSERT( srcUserInt != destUserInt );
       
   329 
       
   330     // Copy destination entry to the source and compare the values. They
       
   331     // should now be the same.
       
   332     destEntry->CopyFromL( *srcEntry );
       
   333     srcUserInt = srcEntry->UserIntL();
       
   334     destUserInt = destEntry->UserIntL();
       
   335     
       
   336     if (srcUserInt != destUserInt)
       
   337         {
       
   338         INFO_PRINTF1( _L("Compare entries failed. User int values on src/dest \
       
   339             entries not equal." ) );
       
   340         SetTestStepResult( EFail );
       
   341         }
       
   342 
       
   343     CleanupStack::PopAndDestroy( destEntry );
       
   344     CleanupStack::PopAndDestroy( srcEntry );
       
   345     }
       
   346 
       
   347 /**
       
   348 Compare entries with different user integer values. An entry with a previously 
       
   349 set user integer value is copied to another entry. The user integer of the 
       
   350 destination entry is then changed and the entries are compared. The entries 
       
   351 should not be considered the same.
       
   352 */
       
   353 void CUserIntTestStep::TestCompareL()
       
   354     {
       
   355     TUint32 srcUserInt = 999999;    
       
   356     INFO_PRINTF2( _L("Testing CompareL: User int=%u"),  srcUserInt );
       
   357 
       
   358     // Create two entries with different user integer values.
       
   359     CCalEntry* srcEntry = CreateEntryL( KEntryUid(), srcUserInt );
       
   360     CleanupStack::PushL( srcEntry );
       
   361     CCalEntry* destEntry = CreateEntryL( KEntryUid(), 0 );
       
   362     CleanupStack::PushL( destEntry );
       
   363     
       
   364     // Compare src and dest. They should not be equal.
       
   365     TBool isDestEqual = srcEntry->CompareL( *destEntry );
       
   366     if (isDestEqual)
       
   367         {
       
   368         INFO_PRINTF1( _L("Compare test failed. Entries should not be equal") );
       
   369         SetTestStepResult( EFail );
       
   370         }
       
   371 
       
   372     // Make user integer same in both src and dest entries then compare.
       
   373     destEntry->SetUserIntL( srcUserInt );
       
   374     isDestEqual = srcEntry->CompareL( *destEntry );
       
   375     if (!isDestEqual)
       
   376         {
       
   377         INFO_PRINTF1( _L("Compare test failed. Entries should be equal") );
       
   378         SetTestStepResult( EFail );
       
   379         }
       
   380 
       
   381     CleanupStack::PopAndDestroy( destEntry );
       
   382     CleanupStack::PopAndDestroy( srcEntry );
       
   383     }
       
   384     
       
   385 /**
       
   386 Re-opens the session to the agenda server to ensure that the server restarts.
       
   387 */
       
   388 void CUserIntTestStep::ReOpenSession()
       
   389 	{
       
   390 	delete iEntryView;
       
   391 	delete iDataExchange;
       
   392 	delete iSession;
       
   393 	iEntryView = NULL;
       
   394 	iDataExchange = NULL; 
       
   395 	iSession = NULL;
       
   396 	
       
   397     iSession = CCalSession::NewL();
       
   398     
       
   399     // Open calendar file again but don't delete it.
       
   400     OpenCalendarFileL( EFalse );
       
   401 
       
   402     iEntryView = CCalEntryView::NewL( *iSession );
       
   403     iDataExchange = CCalDataExchange::NewL( *iSession );
       
   404 	}