lowlevellibsandfws/apputils/tsrc/T_RSC.CPP
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     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 // Started by MJB, May 1995
       
    15 // Updated Nov 1996 by PNJ for Unicode.
       
    16 // Tests RResourceFile class
       
    17 // 
       
    18 //
       
    19 
       
    20 #include <e32test.h>
       
    21 #include <bautils.h>
       
    22 #include <barsc.h>
       
    23 #include <barsc2.h>
       
    24 #include <trsc.rsg>
       
    25 
       
    26 #include "T_RSC.H"
       
    27 
       
    28 LOCAL_D RTest test(_L("T_RSC"));
       
    29 LOCAL_D RFs TheFs;
       
    30 
       
    31 _LIT(KRamResourceFile, "c:\\T_RSC.RSC");
       
    32 _LIT(KRamResourceFile2, "c:\\NewRscFormat.RSC");
       
    33 _LIT(KFailResourceFile,"z:\\system\\data\\nonexist.rsc");
       
    34 
       
    35 LOCAL_C TInt compare( const TDesC8 &aresbuf, const TDesC &abuf, TBool aUnicode )
       
    36 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       
    37 //
       
    38 // Compare a buffer taken from a resource file with one that was defined in
       
    39 // source code.  All resources read at this level are read as pure data.
       
    40 // However, the resource file entries for this test code are defined as
       
    41 // generic text, which may be 8 or 16 bit codes, depending on how the
       
    42 // resource was compiled.  The third parameter indicates which is expected.
       
    43 //
       
    44 //
       
    45 // Return a true value if the text matches, else false.
       
    46 //
       
    47 // ----------------------------------------------------------------------------
       
    48     {
       
    49 
       
    50     TInt count = abuf.Length();
       
    51 
       
    52     // exit immediately of the strings are of different length.
       
    53     if ( abuf.Size() != aresbuf.Size() ) return FALSE;
       
    54 
       
    55     // loop through character by character and exit if any comparison fails.
       
    56 
       
    57     if ( aUnicode )
       
    58         {
       
    59         while ( count > 0 )
       
    60             {
       
    61             TInt uch;
       
    62             TInt bi; // byte index
       
    63             count -=1;
       
    64             bi = count*2;
       
    65             uch = (aresbuf[bi+1]<<8) | aresbuf[bi];
       
    66             if ( uch != abuf[count] ) return FALSE;
       
    67             }
       
    68         }
       
    69     else
       
    70         {
       
    71         while ( count > 0 )
       
    72             {
       
    73             count -=1;
       
    74             if ( aresbuf[count] != abuf[count] ) return FALSE;
       
    75             }
       
    76         }
       
    77 
       
    78     return TRUE;
       
    79 
       
    80     } // end of compare function.
       
    81 
       
    82 
       
    83 LOCAL_C void CopyFileL(const TDesC& aFrom,const TDesC& aTo)
       
    84 //
       
    85 // helper function
       
    86 //
       
    87 	{
       
    88 	TheFs.Delete(aTo);
       
    89 	CFileMan* man=CFileMan::NewL(TheFs);
       
    90 	TInt r=man->Copy(aFrom,aTo);
       
    91 	delete man;
       
    92 	User::LeaveIfError(r);
       
    93 	User::LeaveIfError(TheFs.SetAtt(aTo,0,KEntryAttReadOnly)); // clear RO
       
    94 	}
       
    95 
       
    96 LOCAL_C void DeleteFile(const TDesC& aFileName)
       
    97 //
       
    98 // helper function
       
    99 //
       
   100 	{
       
   101 	// make sure the file is read/write
       
   102 	TInt err = TheFs.SetAtt(aFileName, 0, KEntryAttReadOnly);
       
   103 	if(err != KErrNone)
       
   104 		{
       
   105 		RDebug::Print(_L("error changing attributes file = %d"),err);
       
   106 		}
       
   107 	// delete the file
       
   108 	err = BaflUtils::DeleteFile(TheFs, aFileName);
       
   109 	if(err != KErrNone)
       
   110 		{
       
   111 		RDebug::Print(_L("error deleting file = %d"),err);
       
   112 		}
       
   113 	}
       
   114 
       
   115 LOCAL_C TInt FileSizeL(const TDesC& aFileName)
       
   116 	{
       
   117 	RFile file;
       
   118 	User::LeaveIfError(file.Open(TheFs, aFileName, EFileRead));
       
   119 	CleanupClosePushL(file);
       
   120 	TInt size = 0;
       
   121 	User::LeaveIfError(file.Size(size));
       
   122 	CleanupStack::PopAndDestroy(&file);
       
   123 	return size;
       
   124 	}
       
   125 
       
   126 template <class T> class TestRsc
       
   127 	{
       
   128 protected:
       
   129 	void TestReadL(T* aRscFile);
       
   130     };
       
   131 
       
   132 class TestRRsc : public TestRsc<RResourceFile>
       
   133 	{
       
   134 public:
       
   135 	void TestReadROldL(const TDesC &aTitle, const TDesC &aFileName);
       
   136 	void TestReadRNewL(const TDesC &aTitle, const TDesC &aFileName, TUint aFileOffset, TUint aFileSize);
       
   137 	void TestOpenR();
       
   138     };
       
   139 
       
   140 class TestCRsc : public TestRsc<CResourceFile>
       
   141 	{
       
   142 public:
       
   143 	void TestReadCNewL(const TDesC &aTitle, const TDesC &aFileName, TUint aFileOffset, TUint aFileSize);
       
   144 	void TestOpenC();
       
   145     };
       
   146 
       
   147 /**
       
   148 @SYMTestCaseID          SYSLIB-BAFL-CT-0427
       
   149 @SYMTestCaseDesc        RResourceFile class functionality test
       
   150 @SYMTestPriority        Medium
       
   151 @SYMTestActions         Attempts to read from an 8 and 16 bit resource file
       
   152 @SYMTestExpectedResults Tests must not fail
       
   153 @SYMREQ                 REQ0000
       
   154 */
       
   155 void TestRRsc::TestReadROldL(const TDesC &aTitle, const TDesC &aFileName)
       
   156 	{
       
   157 	test.Start(aTitle);
       
   158 	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0427 "));
       
   159 	RResourceFile rsc;
       
   160 	CleanupClosePushL(rsc);
       
   161 	rsc.OpenL(TheFs, aFileName);
       
   162 	TestReadL(&rsc);
       
   163 	CleanupStack::PopAndDestroy();
       
   164 	test.End();
       
   165 	}
       
   166 
       
   167 /**
       
   168 @SYMTestCaseID          SYSLIB-BAFL-CT-0428
       
   169 @SYMTestCaseDesc        Tests for RResourceFile::OpenL(,,,) function
       
   170 @SYMTestPriority        Medium
       
   171 @SYMTestActions         Attempt to read from a resource file
       
   172 @SYMTestExpectedResults Tests must not fail
       
   173 @SYMREQ                 REQ0000
       
   174 */
       
   175 void TestRRsc::TestReadRNewL(const TDesC &aTitle, const TDesC &aFileName, TUint aFileOffset, TUint aFileSize)
       
   176 	{
       
   177 	test.Start(aTitle);
       
   178 	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0428 "));
       
   179 	RResourceFile rsc;
       
   180 	CleanupClosePushL(rsc);
       
   181 	rsc.OpenL(TheFs, aFileName, aFileOffset, aFileSize);
       
   182 	TestReadL(&rsc);
       
   183 	CleanupStack::PopAndDestroy();
       
   184 	test.End();
       
   185 	}
       
   186 
       
   187 /**
       
   188 @SYMTestCaseID          SYSLIB-BAFL-CT-0429
       
   189 @SYMTestCaseDesc        Testing error recovery - R file
       
   190 @SYMTestPriority        Medium
       
   191 @SYMTestActions         Attempt to read a resouce file,should recover on error.
       
   192 @SYMTestExpectedResults Tests must not fail
       
   193 @SYMREQ                 REQ0000
       
   194 */
       
   195 void TestRRsc::TestOpenR()
       
   196 	{
       
   197 	test.Start(_L("Testing error recovery - R file"));
       
   198 	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0429 "));
       
   199 	RResourceFile rsc_file;
       
   200     TRAPD(err, rsc_file.OpenL(TheFs, KFailResourceFile));
       
   201     test(err != KErrNone);
       
   202 	test.End();
       
   203 	}
       
   204 
       
   205 /**
       
   206 @SYMTestCaseID          SYSLIB-BAFL-CT-0430
       
   207 @SYMTestCaseDesc        Tests for RResourceFile::NewL() function
       
   208 @SYMTestPriority        Medium
       
   209 @SYMTestActions         Tests for reading the new resource file
       
   210 @SYMTestExpectedResults Tests must not fail
       
   211 @SYMREQ                 REQ0000
       
   212 */
       
   213 void TestCRsc::TestReadCNewL(const TDesC &aTitle, const TDesC &aFileName, TUint aFileOffset, TUint aFileSize)
       
   214 	{
       
   215 	test.Start(aTitle);
       
   216 	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0430 "));
       
   217 	CResourceFile* rsc = CResourceFile::NewL(TheFs, aFileName, aFileOffset, aFileSize);
       
   218 	CleanupStack::PushL(rsc);
       
   219 	TestReadL(rsc);
       
   220 	CleanupStack::PopAndDestroy(rsc);
       
   221 	test.End();
       
   222 	}
       
   223 
       
   224 /**
       
   225 @SYMTestCaseID          SYSLIB-BAFL-CT-0431
       
   226 @SYMTestCaseDesc        Testing error recovery - C file
       
   227 @SYMTestPriority        Medium
       
   228 @SYMTestActions         Tests for new open resource file,should recover on error
       
   229 @SYMTestExpectedResults Tests must not fail
       
   230 @SYMREQ                 REQ0000
       
   231 */
       
   232 void TestCRsc::TestOpenC()
       
   233 	{
       
   234 	test.Start(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0431 Testing error recovery - C file "));
       
   235 	CResourceFile* rsc_file = NULL;
       
   236     TRAPD(err, rsc_file = CResourceFile::NewL(TheFs, KFailResourceFile, 0, 0));
       
   237     test(err != KErrNone);
       
   238 	delete rsc_file;
       
   239 	test.End();
       
   240 	}
       
   241 
       
   242 template <class T> void TestRsc<T>::TestReadL(T* aRscFile)
       
   243 	{
       
   244 	TInt ii;
       
   245     //
       
   246     // First test to see if we have an 8 or 16-bit text resource file.
       
   247     // This is done by looking at a signature resource that is defined
       
   248     // as an LTEXT item and seeing how its length computes.
       
   249     //
       
   250 
       
   251     test.Next(_L("Testing 8 or 16-bit signature"));
       
   252 
       
   253 
       
   254     TBool isUnicode = EFalse;
       
   255 
       
   256     HBufC8 *ps = aRscFile->AllocReadL(R_TEXT_SIGNATURE);
       
   257     TPtr8 sig = ps->Des();
       
   258 
       
   259     TInt l = sig.Length();		// length of the raw resource data
       
   260     TInt sig_count = sig[0];	// count byte at the start of the LTEXT
       
   261 
       
   262     if ( l == (1 + sig_count) )	// count + TText8 data
       
   263 		{
       
   264 		test.Printf(_L("8-bit resource text\n"));
       
   265 		}
       
   266     else
       
   267 	if ( l == (1+1+(sig_count*2)) )		// count + padding + TText16 data
       
   268 		{
       
   269 		isUnicode = ETrue;
       
   270 		test.Printf(_L("Unicode resource text...\n"));
       
   271 		test(sig[1]==0xAB);				// check for the defined padding byte
       
   272 		}
       
   273 	else
       
   274 		test.Printf(_L("Invalid signature found\n"));
       
   275 
       
   276     User::Free(ps);
       
   277 
       
   278 	for (ii=SYS_SPECIAL_CHARACTERS;ii<=SYS_PAGE_IS;ii++)
       
   279 		{
       
   280 		TPtrC des=TPtrC(S_Resource[ii]);
       
   281 		TBuf<256> testbuf;
       
   282 		testbuf.Format(_L("Testing ReadL for resource: \"%S\""),&des);
       
   283 		test.Next(testbuf);
       
   284 		TBuf8<256> buf;
       
   285 		aRscFile->ReadL(buf,ii);
       
   286         test(compare(buf,des,isUnicode));
       
   287 		}
       
   288 
       
   289 	for (ii=SYS_SPECIAL_CHARACTERS;ii<=SYS_PAGE_IS;ii++)
       
   290 		{
       
   291 		TPtrC des=TPtrC(S_Resource[ii]);
       
   292 		TBuf<256> testbuf;
       
   293 		testbuf.Format(_L("Testing AllocReadL for resource: \"%S\""),&des);
       
   294 		test.Next(testbuf);
       
   295 		HBufC8 *p=aRscFile->AllocReadL(ii);
       
   296 		TPtr8 wp=p->Des();
       
   297 		test(compare(wp,des,isUnicode));
       
   298 		User::Free(p);
       
   299 		}
       
   300 
       
   301 	for (ii=SYS_SPECIAL_CHARACTERS;ii<=SYS_PAGE_IS;ii++)
       
   302 		{
       
   303 		TPtrC des=TPtrC(S_Resource[ii]);
       
   304 		TBuf<256> testbuf;
       
   305 		testbuf.Format(_L("Testing AllocReadLC for resource: \"%S\""),&des);
       
   306 		test.Next(testbuf);
       
   307 		HBufC8 *p=aRscFile->AllocReadLC(ii);
       
   308 		TPtr8 wp=p->Des();
       
   309 		test(compare(wp,des,isUnicode));
       
   310 		CleanupStack::PopAndDestroy();
       
   311 		}
       
   312     }
       
   313 
       
   314 /**
       
   315 @SYMTestCaseID          SYSLIB-BAFL-CT-0432
       
   316 @SYMTestCaseDesc        Testing RResourceFile & CResourceFile classes
       
   317 @SYMTestPriority        High
       
   318 @SYMTestActions         Wrapper function, calls up test execute functions
       
   319 @SYMTestExpectedResults Tests must not fail
       
   320 @SYMREQ                 REQ0000
       
   321 */
       
   322 LOCAL_C void DoTestsL()
       
   323     {
       
   324 	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0432 "));
       
   325 	CleanupClosePushL(TheFs);
       
   326 	User::LeaveIfError(TheFs.Connect());
       
   327 
       
   328 	test.Printf(_L("Copying resource files to RAM\r\n"));
       
   329 	CopyFileL(KRomResourceFile,KRamResourceFile);
       
   330 	CopyFileL(KRomResourceFile2,KRamResourceFile2);
       
   331 
       
   332 	//The new resource format rsc file is compiled from RscHeader.bin, TRsc.rsc and 16RAMC.mbm.
       
   333 	//So I want to know the offset and size of TRsc.rsc.
       
   334 	TInt header_size = ::FileSizeL(KRomResourceFileHeader);
       
   335 	TInt rsc_size1 = ::FileSizeL(KRomResourceFile);
       
   336 	TInt rsc_size2 = ::FileSizeL(KRamResourceFile);
       
   337 
       
   338 	TestRRsc t1;
       
   339 
       
   340 	t1.TestReadROldL(_L("Testing ROM Resource file T_RSC.RSC"),KRomResourceFile);
       
   341 
       
   342 	t1.TestReadRNewL(_L("Testing ROM Resource file NewRscFormat.RSC"),
       
   343 							KRomResourceFile2,
       
   344 							header_size,
       
   345 							rsc_size1);
       
   346 
       
   347 	t1.TestReadROldL(_L("Testing RAM Resource file T_RSC.RSC"),KRamResourceFile);
       
   348 
       
   349 	t1.TestReadRNewL(_L("Testing RAM Resource file NewRscFormat.RSC"),
       
   350 						KRamResourceFile2,
       
   351 						header_size,
       
   352 						rsc_size2);
       
   353 
       
   354 	t1.TestOpenR();
       
   355 
       
   356 	TestCRsc t2;
       
   357 
       
   358 	t2.TestReadCNewL(_L("Testing ROM Resource file T_RSC.RSC"),KRomResourceFile, 0, 0);
       
   359 
       
   360 	t2.TestReadCNewL(_L("Testing ROM Resource file NewRscFormat.RSC"),
       
   361 						KRomResourceFile2,
       
   362 						header_size,
       
   363 						rsc_size1);
       
   364 
       
   365 	t2.TestReadCNewL(_L("Testing RAM Resource file T_RSC.RSC"),KRamResourceFile, 0, 0);
       
   366 
       
   367 	t2.TestReadCNewL(_L("Testing RAM Resource file NewRscFormat.RSC"),
       
   368 						KRamResourceFile2,
       
   369 						header_size,
       
   370 						rsc_size2);
       
   371 
       
   372 	t2.TestOpenC();
       
   373 
       
   374    	// tidy up
       
   375 	DeleteFile(KRamResourceFile);
       
   376 	DeleteFile(KRamResourceFile2);
       
   377 
       
   378 	CleanupStack::PopAndDestroy(1, &TheFs);
       
   379     }
       
   380 
       
   381 GLDEF_C TInt E32Main()
       
   382 	{
       
   383     __UHEAP_MARK;
       
   384     CTrapCleanup *cleanup=CTrapCleanup::New();
       
   385 	test.Title();
       
   386 	test.Start(_L("Testing RResourceFile & CResourceFile"));
       
   387     TRAPD(err,DoTestsL());
       
   388     test.Printf(_L("Error code is %d\n"),err);
       
   389     test(err==KErrNone);
       
   390     test.Next(_L("/n"));
       
   391 	test.End();
       
   392     test.Close();
       
   393     delete cleanup;
       
   394     __UHEAP_MARKEND;
       
   395 	return(0);
       
   396     }