pimappservices/calendar/tsrc/tcal_fileaccess.cpp
changeset 0 f979ecb2b13e
equal deleted inserted replaced
-1:000000000000 0:f979ecb2b13e
       
     1 // Copyright (c) 2005-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 "caltestlib.h"
       
    17 
       
    18 #include <e32test.h>
       
    19 #include <calsession.h>
       
    20 
       
    21 _LIT(KTestName, "TCal_FileAccess");
       
    22 RTest test(KTestName);
       
    23 _LIT(KFileName, "C:TestFile_FileAccess");
       
    24 _LIT(KSecondFileName, "C:TestFile_FileAccess_2");
       
    25 _LIT(KThirdFileName, "C:TestFile_FileAccess_3");
       
    26 
       
    27 
       
    28 class CFileAccessTestManager : public CBase
       
    29 	{
       
    30 public:
       
    31     static CFileAccessTestManager* NewLC();
       
    32 	void RunAllTestsL();
       
    33 	~CFileAccessTestManager();	
       
    34 	
       
    35 private:
       
    36 	void ConstructL();
       
    37 	void TestDefaultFileNameL();
       
    38 	void TestCreateAndOpenFileL();
       
    39 	void TestListAgendaFilesL();
       
    40 	void TestDeleteFileL();
       
    41 	void TestCreateBadFileNameL();
       
    42 	void TestCreateLongFileNameL();
       
    43 	
       
    44 private:	
       
    45 	CCalSession* iModel;
       
    46 	CCalTestLibrary* iTestLib;
       
    47 	};
       
    48 
       
    49 CFileAccessTestManager* CFileAccessTestManager::NewLC()
       
    50 	{
       
    51 	CFileAccessTestManager* self = new (ELeave) CFileAccessTestManager();
       
    52 	
       
    53 	CleanupStack::PushL(self);
       
    54 	self->ConstructL();
       
    55 
       
    56 	return (self);
       
    57 	}
       
    58 
       
    59 CFileAccessTestManager::~CFileAccessTestManager()
       
    60 	{
       
    61 	delete iTestLib;
       
    62 	}
       
    63 
       
    64 void CFileAccessTestManager::ConstructL()
       
    65 	{
       
    66 	iTestLib = CCalTestLibrary::NewL();	
       
    67 	iModel = &(iTestLib->GetSession());
       
    68 	}
       
    69 
       
    70 void CFileAccessTestManager::TestDefaultFileNameL()
       
    71 	{
       
    72 	_LIT(KDefaultFileName, "C:Calendar");
       
    73 	
       
    74 	const TPtrC16 KName = iModel->DefaultFileNameL();
       
    75 	test(KName == KDefaultFileName);
       
    76 	
       
    77 	test.Printf(iModel->DefaultFileNameL());
       
    78 	test.Printf(_L("\n"));
       
    79 	}
       
    80 
       
    81 
       
    82 void CFileAccessTestManager::RunAllTestsL()
       
    83 	{
       
    84 	
       
    85 	test.Next(_L("GetDefaultName"));
       
    86 	TestDefaultFileNameL();
       
    87 
       
    88 	test.Next(_L("CreateAndOpenFile"));
       
    89 	TestCreateAndOpenFileL();
       
    90 
       
    91 	test.Next(_L("ListsAgendaFiles"));
       
    92 	TestListAgendaFilesL();
       
    93 	
       
    94 	test.Next(_L("CloseAndDeleteFile"));
       
    95 	TestDeleteFileL();
       
    96 
       
    97 	test.Next(_L("CreateBadFileName"));
       
    98 	TestCreateBadFileNameL(); 
       
    99 	
       
   100 	test.Next(_L("CreateLongFileName"));
       
   101 	TestCreateLongFileNameL();
       
   102 	}
       
   103 	
       
   104 // Test the Opening of a non-existent File
       
   105 // Test Creating a File
       
   106 // Test The Opening of the newly created file	
       
   107 void CFileAccessTestManager::TestCreateAndOpenFileL()
       
   108 	{
       
   109 	_LIT(KBadName, "C:NonExistentFile");
       
   110 	TRAPD(expectedErr, iModel->OpenL(KBadName));
       
   111 	test(expectedErr == KErrNotFound);
       
   112 	
       
   113 	_LIT(KFullPathName, "C:\\private\\10003a5b\\TestFile_FileAccess");
       
   114 	TRAP(expectedErr, iModel->OpenL(KFullPathName));
       
   115 	test(expectedErr == KErrNotSupported);
       
   116 	
       
   117 	TRAPD(err, iModel->CreateCalFileL(KFileName));
       
   118 	test(err == KErrAlreadyExists || err == KErrNone);
       
   119 		
       
   120 	iModel->OpenL(KFileName);
       
   121 	}
       
   122 	
       
   123 // Test that three created files are Listing in ListCalFilesL
       
   124 // (Other files will exist in the Cal private directory due to other tests) 	
       
   125 void CFileAccessTestManager::TestListAgendaFilesL()
       
   126 	{
       
   127 	TBool foundFirstFile((EFalse));
       
   128 	TBool foundSecondFile((EFalse));
       
   129 	TBool foundThirdFile((EFalse));
       
   130 	
       
   131 	// create Three files and check they can be listed
       
   132 	TRAPD(err, iModel->CreateCalFileL(KFileName));
       
   133 	test(err == KErrAlreadyExists);  // created in first test step
       
   134 	
       
   135 	TRAPD(errSecondCreate, iModel->CreateCalFileL(KSecondFileName));
       
   136 	test(errSecondCreate == KErrNone);	
       
   137 	
       
   138 	TRAPD(errThirdCreate, iModel->CreateCalFileL(KThirdFileName));
       
   139 	test(errThirdCreate == KErrNone);	
       
   140 	
       
   141 	CDesCArray* agendaFiles(iModel->ListCalFilesL()); 
       
   142 	CleanupStack::PushL(agendaFiles);
       
   143 	
       
   144 	for(TInt i = 0; i < agendaFiles->MdcaCount(); ++i)  // there may be other files from other tests
       
   145 		{
       
   146 		test.Printf(agendaFiles->MdcaPoint(i));
       
   147 		test.Printf(_L("\n"));
       
   148 		
       
   149 		foundFirstFile = foundFirstFile || ((*agendaFiles)[i]) == KFileName;
       
   150 		foundSecondFile = foundSecondFile || ((*agendaFiles)[i]) == KSecondFileName;
       
   151 		foundThirdFile = foundThirdFile || ((*agendaFiles)[i]) == KThirdFileName;		
       
   152 		}
       
   153 		
       
   154 	test(foundFirstFile);
       
   155 	test(foundSecondFile);
       
   156 	test(foundThirdFile);		
       
   157 		
       
   158 	CleanupStack::PopAndDestroy(agendaFiles);
       
   159 	}
       
   160 	
       
   161 // Delete 2 of the 3 Files and check they are not listed in ListCalFilesL and the other file is	
       
   162 // Delete a non-existent file
       
   163 void CFileAccessTestManager::TestDeleteFileL()
       
   164 	{	
       
   165 	iModel->DeleteCalFileL(KFileName);
       
   166 	iModel->DeleteCalFileL(KSecondFileName);
       
   167 	
       
   168 	TBool foundFirstFile((EFalse));
       
   169 	TBool foundSecondFile((EFalse));
       
   170 	TBool foundThirdFile((EFalse));	
       
   171 	
       
   172 	CDesCArray* agendaFiles(iModel->ListCalFilesL()); 
       
   173 	CleanupStack::PushL(agendaFiles);
       
   174 	
       
   175 	for(TInt i = 0; i < agendaFiles->MdcaCount(); ++i)  // there may be other files from other tests
       
   176 		{		
       
   177 		foundFirstFile = foundFirstFile || ((*agendaFiles)[i]) == KFileName;
       
   178 		foundSecondFile = foundSecondFile || ((*agendaFiles)[i]) == KSecondFileName;
       
   179 		foundThirdFile = foundThirdFile || ((*agendaFiles)[i]) == KThirdFileName;		
       
   180 		}
       
   181 		
       
   182 	test(!foundFirstFile);
       
   183 	test(!foundSecondFile);
       
   184 	test(foundThirdFile);		
       
   185 		
       
   186 	CleanupStack::PopAndDestroy(agendaFiles);
       
   187 	iModel->DeleteCalFileL(KThirdFileName);	
       
   188 	
       
   189 	TRAPD(deleteErr, iModel->DeleteCalFileL(KThirdFileName));
       
   190 	test(deleteErr == KErrNotFound);	
       
   191 	}
       
   192 
       
   193 void CFileAccessTestManager::TestCreateBadFileNameL()
       
   194 	{
       
   195 	// Test for INC122832
       
   196  	TBuf<239> nameExceeds238Chars;
       
   197  	nameExceeds238Chars.Append(_L("C:"));
       
   198  	nameExceeds238Chars.AppendFill('a', 237);
       
   199  	TRAPD(err2, iModel->CreateCalFileL(nameExceeds238Chars));
       
   200  	test(err2 == KErrBadName);
       
   201  	
       
   202  	TBuf<237> nameExceeds236CharsWithNoDrive;
       
   203  	nameExceeds236CharsWithNoDrive.AppendFill('a', 237);
       
   204  	TRAPD(err3, iModel->CreateCalFileL(nameExceeds236CharsWithNoDrive));
       
   205  	test(err3 == KErrBadName);
       
   206  	}
       
   207 
       
   208 
       
   209 void CFileAccessTestManager::TestCreateLongFileNameL()
       
   210 	{
       
   211 	// Test for DEF113074
       
   212  	TBuf<200> maxLengthFilename;
       
   213  	maxLengthFilename.Append(_L("C:"));
       
   214  	maxLengthFilename.AppendFill('a', 198);
       
   215  	TRAPD(err, iModel->CreateCalFileL(maxLengthFilename));
       
   216  	test(err == KErrNone);   
       
   217  	//Test to create same existing file
       
   218  	TRAPD(err1, iModel->CreateCalFileL(maxLengthFilename));
       
   219  	test(err1 == KErrAlreadyExists || err1 == KErrNone); 	
       
   220  	iModel->OpenL(maxLengthFilename);  	
       
   221  	iModel->DeleteCalFileL(maxLengthFilename);
       
   222  	
       
   223  	
       
   224  	//Test to create longfile name with invalid chars
       
   225  	TBuf<201> maxLengthInvalidFilename;
       
   226  	maxLengthInvalidFilename.Append(_L("C:"));
       
   227  	maxLengthInvalidFilename.AppendFill('*', 199);
       
   228  	TRAPD(err2, iModel->CreateCalFileL(maxLengthInvalidFilename));
       
   229  	test(err2 == KErrBadName);
       
   230  	
       
   231  	
       
   232  	//Test to create no drive name
       
   233  	TBuf<200> maxLengthNoDriveFilename;
       
   234  	maxLengthNoDriveFilename.AppendFill('a', 200);
       
   235  	TRAPD(err3, iModel->CreateCalFileL(maxLengthNoDriveFilename));
       
   236  	test(err3 == KErrNone);
       
   237  	iModel->DeleteCalFileL(maxLengthNoDriveFilename);
       
   238  	
       
   239  	//Test to create long filename with different chars
       
   240  	TBuf<200> maxLengthFilenameDiffChars;
       
   241  	maxLengthFilenameDiffChars.Append(_L("C:"));
       
   242  	maxLengthFilenameDiffChars.AppendFill('i', 50);
       
   243  	maxLengthFilenameDiffChars.AppendFill('2', 50);
       
   244  	maxLengthFilenameDiffChars.AppendFill('@', 48);
       
   245  	maxLengthFilenameDiffChars.AppendFill('x', 40);
       
   246  	maxLengthFilenameDiffChars.AppendFill('w', 10);
       
   247  	TRAPD(err4, iModel->CreateCalFileL(maxLengthFilenameDiffChars));
       
   248  	test(err4 == KErrNone);
       
   249  	iModel->OpenL(maxLengthFilenameDiffChars); 
       
   250  	iModel->DeleteCalFileL(maxLengthFilenameDiffChars);
       
   251  	
       
   252  	//Test to create file name exceed max length
       
   253  	TBuf<201> maxLengthExceedFilename;
       
   254  	maxLengthExceedFilename.Append(_L("C:"));
       
   255  	maxLengthExceedFilename.AppendFill('g', 199);
       
   256  	TRAPD(err5, iModel->CreateCalFileL(maxLengthExceedFilename));
       
   257  	test(err5 == KErrBadName);
       
   258  	
       
   259  	}
       
   260 
       
   261 
       
   262 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
       
   263  * DoTestL()
       
   264  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
       
   265 	
       
   266 static void DoTestL()
       
   267 	{
       
   268 	CFileAccessTestManager* testManager = CFileAccessTestManager::NewLC();
       
   269 
       
   270 
       
   271 	TPerformanceTimer timer(test);
       
   272 	timer.Start();
       
   273 
       
   274 
       
   275 	// Run the test suite
       
   276 
       
   277 	testManager->RunAllTestsL();
       
   278 	
       
   279 	
       
   280 	timer.Stop();
       
   281 	test.Printf(_L("Done\n"));
       
   282 	// printout performance time
       
   283 	timer.PrintOut();
       
   284 
       
   285 
       
   286 	CleanupStack::PopAndDestroy(testManager);
       
   287 	}
       
   288 
       
   289 
       
   290 /**
       
   291 @SYMTestCaseID     PIM-TCAL-FILEACCESS-0001
       
   292 */
       
   293 TInt E32Main()
       
   294     {
       
   295 	__UHEAP_MARK;
       
   296 
       
   297 	test.Start(_L("@SYMTESTCaseID:PIM-TCAL-FILEACCESS-0001 Calendar Interim API File Access test suite"));
       
   298 
       
   299 	test.Title();
       
   300 
       
   301 	CTrapCleanup* trapCleanup = CTrapCleanup::New();
       
   302 	if (!trapCleanup)
       
   303 		{
       
   304 		return KErrNoMemory;
       
   305 		}
       
   306 
       
   307 	CActiveScheduler* scheduler = new CActiveScheduler();
       
   308 	if (!scheduler)
       
   309 		{
       
   310 		delete trapCleanup;
       
   311 		return KErrNoMemory;
       
   312 		}
       
   313 	CActiveScheduler::Install(scheduler);	
       
   314 
       
   315 	TRAPD(ret, DoTestL());
       
   316 	test(ret == KErrNone);
       
   317 	
       
   318 	delete scheduler;
       
   319 	delete trapCleanup;	
       
   320 
       
   321 	test.End();
       
   322 	test.Close();
       
   323 
       
   324 	__UHEAP_MARKEND;
       
   325 
       
   326 	return (KErrNone);
       
   327     }
       
   328