lowlevellibsandfws/apputils/tsrc/T_RSREAD.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 // Tests resource reader
       
    15 // NB, prior to version 050 of BAFL, the resource compiler did not generate
       
    16 // unicode text and so this code used to make a special 8/16 bit text
       
    17 // comparison where appropriate and the resource binary file could be common
       
    18 // between the 8 and 16-bit builds.  This is now no-longer the case, which means
       
    19 // that the resource binary used for this text MUST be compiled as Unicode
       
    20 // text for the unicode build and 8-bit text for the 8-bit build.
       
    21 // - PNJ, January 1997.
       
    22 // 
       
    23 //
       
    24 
       
    25 #include <e32std.h>
       
    26 #include <e32base.h>
       
    27 #include <e32test.h>
       
    28 #include <f32file.h>
       
    29 #include <barsc.h>
       
    30 #include <barsread.h>
       
    31 #include <barsread2.h>
       
    32 #include <badesca.h>
       
    33 #include <trsc.rsg>
       
    34 #include "T_RSC.H"
       
    35 
       
    36 RTest test(_L("T_RSREAD"));
       
    37 RFs TheFs;
       
    38 RResourceFile TheResourceFile;
       
    39 
       
    40 _LIT(KPanicThread1,"panicThread1");
       
    41 _LIT(KPanicThread2,"panicThread2");
       
    42 _LIT(KPanicThread3,"panicThread3");
       
    43 _LIT(KPanicThread4,"panicThread4");
       
    44 
       
    45 class TRsReadTester
       
    46 	{
       
    47 public:
       
    48 	void TestButtonL();
       
    49 	void TestArrayL();
       
    50 	void TestFlPtEdL();
       
    51 	void TestMenuBarL();
       
    52     void TestAlignment1L();
       
    53     void TestAlignment2L();
       
    54     void TimingTestsL();
       
    55     TInt PanicTests();
       
    56 private:
       
    57     void DumpBytes(const TAny* aPtr,TInt aLen);
       
    58     TUint8 DumpByte(TUint aVal);
       
    59     void CreateResourceReaderLC(TResourceReader& aReader,TInt aResourceId) const;
       
    60     void PanicTResImp1();
       
    61     void PanicTResImp2();
       
    62     void PanicTResImp3();
       
    63     void PanicTResImp4();
       
    64 	};
       
    65 
       
    66 void TRsReadTester::CreateResourceReaderLC(TResourceReader& aReader,TInt aResourceId) const
       
    67     {
       
    68     HBufC8* resource=TheResourceFile.AllocReadLC(aResourceId);
       
    69     aReader.SetBuffer(resource);
       
    70     }
       
    71 
       
    72 /**
       
    73 @SYMTestCaseID          SYSLIB-BAFL-CT-0433
       
    74 @SYMTestCaseDesc        Tests for TResourceReader::ReadInt16(),TResourceReader::ReadUint16() functions
       
    75 @SYMTestPriority        Medium
       
    76 @SYMTestActions         Attempt to read a BUTTON resource
       
    77 @SYMTestExpectedResults Tests must not fail
       
    78 @SYMREQ                 REQ0000
       
    79 */
       
    80 void TRsReadTester::TestButtonL()
       
    81 	{
       
    82 	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0433 Test reading BUTTON resource "));
       
    83     TResourceReader reader;
       
    84     CreateResourceReaderLC(reader,SYS_BUTTON_ONE);
       
    85     test(reader.ReadInt16()==3);
       
    86     test(reader.ReadUint16()==5);
       
    87     TPtrC ptr=reader.ReadTPtrC();
       
    88     test(ptr==_L("Text"));
       
    89     ptr.Set(reader.ReadTPtrC());
       
    90     test(ptr.Length()==0);
       
    91     HBufC* hBuf=reader.ReadHBufCL();
       
    92     test(*hBuf==_L("Bitmap placeholder"));
       
    93     delete(hBuf);
       
    94     CleanupStack::PopAndDestroy();
       
    95 	}
       
    96 
       
    97 /**
       
    98 @SYMTestCaseID          SYSLIB-BAFL-CT-0434
       
    99 @SYMTestCaseDesc        Tests for TResourceReader::ReadDesCArrayL() function
       
   100 @SYMTestPriority        Medium
       
   101 @SYMTestActions         Attempt to read an array resource
       
   102 @SYMTestExpectedResults Tests must not fail
       
   103 @SYMREQ                 REQ0000
       
   104 */
       
   105 void TRsReadTester::TestArrayL()
       
   106 	{
       
   107 	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0434 Test reading ARRAY resource "));
       
   108     TResourceReader reader;
       
   109     CreateResourceReaderLC(reader,SYS_ARRAY_ONE);
       
   110     CDesCArray* desArray=reader.ReadDesCArrayL();
       
   111     CleanupStack::PopAndDestroy();
       
   112     test(desArray->Count()==5);
       
   113 
       
   114     test((*desArray)[0]==_L("Esc"));
       
   115     test((*desArray)[1]==_L("Enter"));
       
   116     test((*desArray)[2]==_L("Tab"));
       
   117     test((*desArray)[3]==_L("Del"));
       
   118     test((*desArray)[4]==_L("Space"));
       
   119 
       
   120     delete(desArray);
       
   121 	}
       
   122 
       
   123 /**
       
   124 @SYMTestCaseID          SYSLIB-BAFL-CT-0435
       
   125 @SYMTestCaseDesc        Tests for TResourceReader::ReadInt16(),TResourceReader::ReadInt64() function
       
   126 @SYMTestPriority        High
       
   127 @SYMTestActions         Attempt for reading FLPTED resource
       
   128 @SYMTestExpectedResults Tests must not fail
       
   129 @SYMREQ                 REQ0000
       
   130 */
       
   131 void TRsReadTester::TestFlPtEdL()
       
   132 	{
       
   133 	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0435 Test reading FLPTED resource "));
       
   134     TResourceReader reader;
       
   135     CreateResourceReaderLC(reader,SYS_FLPTED_ONE);
       
   136     test(reader.ReadInt16()==18);
       
   137     TReal little=reader.ReadReal64();
       
   138     test(little==0.0);
       
   139     TReal big=reader.ReadReal64();
       
   140     test(big>9.89e99);
       
   141     test(big<9.91e99);
       
   142     CleanupStack::PopAndDestroy(1);
       
   143 	}
       
   144 
       
   145 /**
       
   146 @SYMTestCaseID          SYSLIB-BAFL-CT-0436
       
   147 @SYMTestCaseDesc        TResourceReader class functionality test
       
   148 						Test for TResourceReader::ReadInt32 (),TResourceReader::ReadTPtrC() functions
       
   149 @SYMTestPriority        High
       
   150 @SYMTestActions         Attempt for reading MENU_BAR resource
       
   151 @SYMTestExpectedResults Tests must not fail
       
   152 @SYMREQ                 REQ0000
       
   153 */
       
   154 void TRsReadTester::TestMenuBarL()
       
   155 	{
       
   156 	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0436 Test reading MENU_BAR resource "));
       
   157     TResourceReader reader;
       
   158     CreateResourceReaderLC(reader,SYS_MENUBAR_ONE);
       
   159     test(reader.ReadInt16()==8); // how many items following
       
   160     TPtrC ref=_L("abcdefgh");
       
   161     for (TInt ii=1; ii<=8; ii++)
       
   162         {
       
   163         test(reader.ReadInt32()==ii);
       
   164         test(reader.ReadTPtrC()==ref.Left(ii));
       
   165         }
       
   166     CleanupStack::PopAndDestroy(1);
       
   167 	}
       
   168 
       
   169 TInt horrors[] = {
       
   170 	SYS_ALIGNMENT_HORROR0, SYS_ALIGNMENT_HORROR1,
       
   171 	SYS_ALIGNMENT_HORROR2, SYS_ALIGNMENT_HORROR3,
       
   172 	0
       
   173 	};
       
   174 
       
   175 /**
       
   176 @SYMTestCaseID          SYSLIB-BAFL-CT-0437
       
   177 @SYMTestCaseDesc        Reading ALIGNMENT_HORROR resources test
       
   178                         Test for TResourceReader::ReadTPtrC8(),TResourceReader::ReadTPtrC16() function
       
   179 @SYMTestPriority        High
       
   180 @SYMTestActions         Tests for reading descriptors and checks for alignment
       
   181 @SYMTestExpectedResults Tests must not fail
       
   182 @SYMREQ                 REQ0000
       
   183 */
       
   184 void TRsReadTester::TestAlignment1L()
       
   185 	{
       
   186 	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0437 Test reading ALIGNMENT_HORROR resources "));
       
   187 	TPtrC8  ref8 =_L8("xyz");
       
   188 	TPtrC16 ref16=_L16("xyz");
       
   189     TResourceReader reader;
       
   190 	for (TInt rr=0; horrors[rr]!=0; rr++)
       
   191 		{
       
   192 		CreateResourceReaderLC(reader,horrors[rr]);
       
   193 		test(reader.ReadTPtrC8() ==ref8.Left(rr));
       
   194 		test(reader.ReadTPtrC16()==ref16.Left(rr));
       
   195 		CleanupStack::PopAndDestroy(1);
       
   196 		}
       
   197 	}
       
   198 
       
   199 /**
       
   200 @SYMTestCaseID          SYSLIB-BAFL-CT-0438
       
   201 @SYMTestCaseDesc        Reading ALIGNMENT_HORROR_ARRAY resource test
       
   202 @SYMTestPriority        High
       
   203 @SYMTestActions         Tests for reading descriptors and checks for alignment
       
   204 @SYMTestExpectedResults Tests must not fail
       
   205 @SYMREQ                 REQ0000
       
   206 */
       
   207 void TRsReadTester::TestAlignment2L()
       
   208 	{
       
   209 	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0438 Test reading ALIGNMENT_HORROR_ARRAY resource "));
       
   210     TResourceReader reader;
       
   211     CreateResourceReaderLC(reader,SYS_ALIGNMENT_HORROR_ARRAY);
       
   212     test(reader.ReadInt16()==7); // how many items following
       
   213     TPtrC8  ref8 =_L8("abcdef");
       
   214     TPtrC16 ref16=_L16("abcdef");
       
   215     for (TInt ii=0; ii<=6; ii++)
       
   216         {
       
   217         test(reader.ReadTPtrC8() ==ref8.Left(ii));
       
   218         test(reader.ReadTPtrC16()==ref16.Mid(ii));
       
   219         }
       
   220     CleanupStack::PopAndDestroy(1);
       
   221 	}
       
   222 
       
   223 /**
       
   224 @SYMTestCaseID          SYSLIB-BAFL-CT-0439
       
   225 @SYMTestCaseDesc        Timing Tests
       
   226 @SYMTestPriority        High
       
   227 @SYMTestActions         Check for the time loads needed to repeatedly load a resource
       
   228 @SYMTestExpectedResults Tests must not fail
       
   229 @SYMREQ                 REQ0000
       
   230 */
       
   231 void TRsReadTester::TimingTestsL()
       
   232 	{
       
   233 	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0439 Timing tests "));
       
   234 	test.Next(_L("BUTTON and FLPTED"));
       
   235 	TUint time = User::TickCount();
       
   236 	for (TInt ii=0; ii<100; ii++)
       
   237 		{
       
   238     	TResourceReader reader;
       
   239     	CreateResourceReaderLC(reader,SYS_BUTTON_ONE);
       
   240 	    CleanupStack::PopAndDestroy();
       
   241 	    CreateResourceReaderLC(reader,SYS_FLPTED_ONE);
       
   242     	CleanupStack::PopAndDestroy();
       
   243 		}
       
   244 	time = User::TickCount() - time;
       
   245     test.Printf(_L("Time to load 100 times: %d\n"),time);
       
   246 
       
   247 	test.Next(_L("BUTTON and ARRAY"));
       
   248 	time = User::TickCount();
       
   249 	for (TInt jj=0; jj<100; jj++)
       
   250 		{
       
   251     	TResourceReader reader;
       
   252     	CreateResourceReaderLC(reader,SYS_BUTTON_ONE);
       
   253 	    CleanupStack::PopAndDestroy();
       
   254 	    CreateResourceReaderLC(reader,SYS_ARRAY_ONE);
       
   255     	CleanupStack::PopAndDestroy();
       
   256 		}
       
   257 	time = User::TickCount() - time;
       
   258     test.Printf(_L("Time to load 100 times: %d\n"),time);
       
   259 	}
       
   260 
       
   261 
       
   262 TUint8 TRsReadTester::DumpByte(TUint aVal)
       
   263     {
       
   264     return(aVal>9? TUint8(aVal-10+'a'): TUint8(aVal+'0'));
       
   265     }
       
   266 
       
   267 void TRsReadTester::DumpBytes(const TAny* aPtr,TInt aLen)
       
   268     {
       
   269     TUint8* src=(TUint8*)aPtr;
       
   270     TBuf<100> whole;
       
   271     TUint8* tar=(TUint8*)whole.Ptr();
       
   272     TInt len=0;
       
   273     while (aLen--)
       
   274         {
       
   275         TUint val=(*src++);
       
   276         TUint top=val/16;
       
   277         TUint bottom=val%16;
       
   278         *tar++=DumpByte(top);
       
   279         *tar++=DumpByte(bottom);
       
   280         *tar++=' ';
       
   281         len+=3;
       
   282         }
       
   283     whole.SetLength(len);
       
   284     test.Printf(whole);
       
   285     test.Printf(_L("\r\n"));
       
   286     }
       
   287 
       
   288 TInt ThreadFunc1(TAny*)
       
   289   	{
       
   290   	CTrapCleanup* cleanup=CTrapCleanup::New();
       
   291   	RResourceReader reader;
       
   292   	reader.Close();// iCurrentPtr=NULL
       
   293   	TRAPD(err,reader.AdvanceL(0));//calls TResourceReaderImpl::AdvanceL()
       
   294   	delete cleanup;
       
   295   	return err;
       
   296   	}
       
   297 
       
   298 /**
       
   299 @SYMTestCaseID          SYSLIB-BAFL-UT-1791
       
   300 @SYMTestCaseDesc        Testing a panic on TResourceReaderImpl class(JustInTimeDebug is disabled)
       
   301 @SYMTestPriority        Low
       
   302 @SYMTestActions         Test that panics, when the condition inside __ASSERT is made false, by calling AdvanceL, after iCurrentPtr is made NULL using RResourceReader::close()
       
   303 @SYMTestExpectedResults Test must panic
       
   304 @SYMREQ                 REQ0000
       
   305 */
       
   306 
       
   307 void TRsReadTester::PanicTResImp1()
       
   308   	{
       
   309 	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-UT-1791 "));
       
   310   	TRequestStatus threadStatus;
       
   311   	RThread thread1;
       
   312   	TInt rc;
       
   313   	rc = thread1.Create(KPanicThread1, ThreadFunc1,
       
   314   		KDefaultStackSize, KMinHeapSize, KMinHeapSize*4,NULL);
       
   315   	test(KErrNone == rc);
       
   316   	thread1.Logon(threadStatus);
       
   317   	thread1.Resume();
       
   318   	User::WaitForRequest(threadStatus);
       
   319 
       
   320   	test(thread1.ExitType() == EExitPanic);
       
   321   	thread1.Close();
       
   322   	}
       
   323 
       
   324 TInt ThreadFunc2(TAny*)
       
   325 	{
       
   326   	CTrapCleanup* cleanup=CTrapCleanup::New();
       
   327   	RResourceReader reader;
       
   328   	reader.Close();// iCurrentPtr=NULL
       
   329   	TRAPD(err,reader.ReadInt8L());//calls TResourceReaderImpl::ReadInt8L()
       
   330   	delete cleanup;
       
   331   	return err;
       
   332   	}
       
   333 
       
   334 /**
       
   335 @SYMTestCaseID          SYSLIB-BAFL-UT-1792
       
   336 @SYMTestCaseDesc        Testing a panic on TResourceReaderImpl class(JustInTimeDebug is disabled)
       
   337 @SYMTestPriority        Low
       
   338 @SYMTestActions         Test that panics, when the condition inside __ASSERT is made false, by calling ReadInt8L, after iCurrentPtr is made NULL using RResourceReader::close()
       
   339 @SYMTestExpectedResults Test must panic
       
   340 @SYMREQ                 REQ0000
       
   341 */
       
   342 void TRsReadTester::PanicTResImp2()
       
   343  	{
       
   344 	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-UT-1792 "));
       
   345   	TRequestStatus threadStatus;
       
   346   	RThread thread2;
       
   347   	TInt rc;
       
   348   	rc = thread2.Create(KPanicThread2, ThreadFunc2,
       
   349   		KDefaultStackSize, KMinHeapSize, KMinHeapSize*4,NULL);
       
   350  	test(KErrNone == rc);
       
   351   	thread2.Logon(threadStatus);
       
   352   	thread2.Resume();
       
   353   	User::WaitForRequest(threadStatus);
       
   354 
       
   355   	test(thread2.ExitType() == EExitPanic);
       
   356   	thread2.Close();
       
   357   	}
       
   358 
       
   359 TInt ThreadFunc3(TAny*)
       
   360   	{
       
   361   	CTrapCleanup* cleanup=CTrapCleanup::New();
       
   362   	RResourceReader reader;
       
   363   	reader.Close();
       
   364   	TRAPD(err,reader.ReadTPtrC8L());//calls TResourceReaderImpl::ReadTPtrC8L()
       
   365   	delete cleanup;
       
   366   	return err;
       
   367   	}
       
   368 
       
   369 /**
       
   370 @SYMTestCaseID          SYSLIB-BAFL-UT-1793
       
   371 @SYMTestCaseDesc        Testing a panic on TResourceReaderImpl class(JustInTimeDebug is disabled)
       
   372 @SYMTestPriority        Low
       
   373 @SYMTestActions         Test that panics, when the condition inside __ASSERT is made false, by calling ReadTPtrC8L, after iCurrentPtr is made NULL using RResourceReader::close()
       
   374 @SYMTestExpectedResults Test must panic
       
   375 @SYMREQ                 REQ0000
       
   376 */
       
   377 void TRsReadTester::PanicTResImp3()
       
   378  	{
       
   379 	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-UT-1793 "));
       
   380   	TRequestStatus threadStatus;
       
   381   	RThread thread3;
       
   382   	TInt rc;
       
   383   	rc = thread3.Create(KPanicThread3, ThreadFunc3,
       
   384   		KDefaultStackSize, KMinHeapSize, KMinHeapSize*4,NULL);
       
   385   	test(KErrNone == rc);
       
   386   	thread3.Logon(threadStatus);
       
   387   	thread3.Resume();
       
   388   	User::WaitForRequest(threadStatus);
       
   389 
       
   390   	test(thread3.ExitType() == EExitPanic);
       
   391   	thread3.Close();
       
   392   	}
       
   393 
       
   394 TInt ThreadFunc4(TAny*)
       
   395   	{
       
   396   	CTrapCleanup* cleanup=CTrapCleanup::New();
       
   397   	RResourceReader reader;
       
   398   	reader.Close();
       
   399   	TRAPD(err,reader.ReadUint8L());//calls TResourceReaderImpl::ReadUint8L()
       
   400   	delete cleanup;
       
   401   	return err;
       
   402   	}
       
   403 
       
   404 /**
       
   405 @SYMTestCaseID          SYSLIB-BAFL-UT-1794
       
   406 @SYMTestCaseDesc        Testing a panic on TResourceReaderImpl class(JustInTimeDebug is disabled)
       
   407 @SYMTestPriority        Low
       
   408 @SYMTestActions         Test that panics, when the condition inside __ASSERT is made false, by calling ReadUint8L, after iCurrentPtr is made NULL using RResourceReader::close()
       
   409 @SYMTestExpectedResults Test must panic
       
   410 @SYMREQ                 REQ0000
       
   411 */
       
   412 void TRsReadTester::PanicTResImp4()
       
   413  	{
       
   414 	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-UT-1794 "));
       
   415  	TRequestStatus threadStatus;
       
   416   	RThread thread4;
       
   417   	TInt rc;
       
   418   	rc = thread4.Create(KPanicThread4, ThreadFunc4,
       
   419   		KDefaultStackSize, KMinHeapSize, KMinHeapSize*4,NULL);
       
   420   	test(KErrNone == rc);
       
   421   	thread4.Logon(threadStatus);
       
   422   	thread4.Resume();
       
   423   	User::WaitForRequest(threadStatus);
       
   424 
       
   425   	test(thread4.ExitType() == EExitPanic);
       
   426   	thread4.Close();
       
   427   	}
       
   428 
       
   429 TInt TRsReadTester::PanicTests()
       
   430   	{
       
   431   	TBool jit;
       
   432   	jit = User::JustInTime();
       
   433   	User::SetJustInTime(EFalse);
       
   434 
       
   435   	//Tests for panics in TResourceReaderImpl class, through RResourceReader class
       
   436   	PanicTResImp1();
       
   437   	PanicTResImp2();
       
   438   	PanicTResImp3();
       
   439   	PanicTResImp4();
       
   440 
       
   441   	User::SetJustInTime(jit);
       
   442   	return KErrNone;
       
   443   	}
       
   444 
       
   445 /**
       
   446 @SYMTestCaseID          SYSLIB-BAFL-CT-0440
       
   447 @SYMTestCaseDesc        Resource reader test
       
   448 @SYMTestPriority        High
       
   449 @SYMTestActions         Executes all the tests related to resource read
       
   450 @SYMTestExpectedResults Tests must not fail
       
   451 @SYMREQ                 REQ0000
       
   452 */
       
   453 void RunTests()
       
   454     {
       
   455 	test.Start(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0440 Resource reader "));
       
   456     TheFs.Connect();
       
   457     TheResourceFile.OpenL(TheFs,KRomResourceFile);
       
   458 	TRsReadTester lt;
       
   459 	TRAPD(errCode, lt.TestButtonL());
       
   460 	test(errCode==KErrNone);
       
   461 	TRAP(errCode, lt.TestArrayL());
       
   462 	test(errCode==KErrNone);
       
   463 	TRAP(errCode, lt.TestFlPtEdL());
       
   464 	test(errCode==KErrNone);
       
   465 	TRAP(errCode, lt.TestMenuBarL());
       
   466 	test(errCode==KErrNone);
       
   467 	TRAP(errCode, lt.TestAlignment1L());
       
   468 	test(errCode==KErrNone);
       
   469 	TRAP(errCode, lt.TestAlignment2L());
       
   470 	test(errCode==KErrNone);
       
   471     TRAP(errCode, lt.TimingTestsL());
       
   472 	test(errCode==KErrNone);
       
   473     TheResourceFile.Close();
       
   474     //Test that raises panics
       
   475 	test(KErrNone==lt.PanicTests());
       
   476 
       
   477     TheFs.Close();
       
   478 	test.End();
       
   479 	test.Close();
       
   480     }
       
   481 
       
   482 TInt E32Main()
       
   483 	{
       
   484 	__UHEAP_MARK;
       
   485     CTrapCleanup *cleanup=CTrapCleanup::New();
       
   486 	test.Title();
       
   487     TRAPD(err,RunTests());
       
   488 	test(!err);
       
   489 	test.Close();
       
   490     delete(cleanup);
       
   491 	__UHEAP_MARKEND;
       
   492     return(0);
       
   493 	}