os/ossrv/lowlevellibsandfws/apputils/tsrc/T_RSREAD2.CPP
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2003-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 // 
    16 //
    17 
    18 #include <e32std.h>
    19 #include <e32base.h>
    20 #include <e32test.h>
    21 #include <f32file.h>
    22 #include <barsc2.h>
    23 #include <barsread2.h>
    24 #include <badesca.h>
    25 #include <trsc.rsg>
    26 #include "T_RSC.H"
    27 
    28 RTest TheTest(_L("T_RSREAD2"));
    29 RFs TheFs;
    30 CResourceFile* TheResourceFile = NULL;
    31 
    32 //
    33 //
    34 //Test macroses and functions
    35 LOCAL_C void Check(TInt aValue, TInt aLine)
    36 	{
    37 	if(!aValue)
    38 		{
    39 		TheTest(EFalse, aLine);
    40 		}
    41 	}
    42 LOCAL_C void Check(TInt aValue, TInt aExpected, TInt aLine)
    43 	{
    44 	if(aValue != aExpected)
    45 		{
    46 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
    47 		TheTest(EFalse, aLine);
    48 		}
    49 	}
    50 #define TEST(arg) ::Check((arg), __LINE__)
    51 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
    52 
    53 //
    54 //
    55 
    56 //Opens aReader using RResourceReader::OpenLC(CResourceFile*, TInt)
    57 static void OpenResourceReaderLC(RResourceReader& aReader, TInt aResourceId)
    58     {
    59     TEST(TheResourceFile != NULL);
    60     aReader.OpenLC(TheResourceFile, aResourceId);
    61     }
    62 
    63 //Opens aReader using RResourceReader::OpenL(const TDesC8&)
    64 static void OpenResourceReader2LC(RResourceReader& aReader, TInt aResourceId)
    65     {
    66     TEST(TheResourceFile != NULL);
    67     HBufC8* rscData = TheResourceFile->AllocReadLC(aResourceId);
    68     aReader.OpenL(rscData->Des());
    69     CleanupStack::PopAndDestroy(rscData);
    70     CleanupClosePushL(aReader);
    71     }
    72 
    73 //This enum describes how to obtain the resource buffer for
    74 //RResourceReader objects: from the resource file or from a buffer
    75 enum TRscBufAllocation
    76     {
    77     EFromRscFile,
    78     EFromRscBuf,
    79     ERscBufLast
    80     };
    81 
    82 typedef void (*FTOpenResourceReaderLC)(RResourceReader& aReader, TInt aResourceId);
    83 
    84 static FTOpenResourceReaderLC OpenResourceReaderFuncLC[ERscBufLast] =
    85     {
    86     &OpenResourceReaderLC,
    87     &OpenResourceReader2LC
    88     };
    89 
    90 //Opens aReader either from a resource file ot from a resource buffer
    91 static void OpenResourceReaderLC(RResourceReader& aReader, TInt aResourceId, TRscBufAllocation aRscBufAllocation)
    92     {
    93     OpenResourceReaderFuncLC[aRscBufAllocation](aReader, aResourceId);
    94     }
    95 
    96 class TRsReadTester
    97 	{
    98 public:
    99 	void TestButtonL(RResourceReader& aReader);
   100 	void TestArrayL(RResourceReader& aReader);
   101 	void TestFlPtEdL(RResourceReader& aReader);
   102 	void TestMenuBarL(RResourceReader& aReader);
   103     void TestAlignment1L(RResourceReader& aReader, TInt aIndex);
   104     void TestAlignment2L(RResourceReader& aReader);
   105     void TimingTestsL(TRscBufAllocation aRscBufAllocation);
   106 private:
   107     void DumpBytes(const TAny* aPtr,TInt aLen);
   108     TUint8 DumpByte(TUint aVal);
   109 	};
   110 
   111 /**
   112 @SYMTestCaseID          SYSLIB-BAFL-CT-1289
   113 @SYMTestCaseDesc        Tests for reading data from a BUTTON resource file
   114 @SYMTestPriority        High
   115 @SYMTestActions         Tests for RResourceReader::ReadInt16L(),RResourceReader::ReadUInt16L(),
   116                         RResourceReader::ReadTPtrCL() functions
   117 @SYMTestExpectedResults Tests must not fail
   118 @SYMREQ                 REQ0000
   119 */
   120 void TRsReadTester::TestButtonL(RResourceReader& aReader)
   121 	{
   122 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-1289 Test reading BUTTON resource "));
   123     TEST(aReader.ReadInt16L()==3);
   124     TEST(aReader.ReadUint16L()==5);
   125     TPtrC ptr=aReader.ReadTPtrCL();
   126     TEST(ptr==_L("Text"));
   127     ptr.Set(aReader.ReadTPtrCL());
   128     TEST(ptr.Length()==0);
   129     HBufC* hBuf=aReader.ReadHBufCL();
   130     TEST(*hBuf==_L("Bitmap placeholder"));
   131     delete(hBuf);
   132 	}
   133 
   134 /**
   135 @SYMTestCaseID          SYSLIB-BAFL-CT-1290
   136 @SYMTestCaseDesc        Tests for RResourceReader::ReadDesCArrayL() function
   137 @SYMTestPriority        High
   138 @SYMTestActions         Attempt to interpret data from an ARRAY resource file
   139 @SYMTestExpectedResults Tests must not fail
   140 @SYMREQ                 REQ0000
   141 */
   142 void TRsReadTester::TestArrayL(RResourceReader& aReader)
   143 	{
   144 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-1290 Test reading ARRAY resource "));
   145     CDesCArray* desArray=aReader.ReadDesCArrayL();
   146     TEST(desArray->Count()==5);
   147 
   148     TEST((*desArray)[0]==_L("Esc"));
   149     TEST((*desArray)[1]==_L("Enter"));
   150     TEST((*desArray)[2]==_L("Tab"));
   151     TEST((*desArray)[3]==_L("Del"));
   152     TEST((*desArray)[4]==_L("Space"));
   153 
   154     delete(desArray);
   155 	}
   156 
   157 /**
   158 @SYMTestCaseID          SYSLIB-BAFL-CT-1291
   159 @SYMTestCaseDesc        Tests for reading FLPTED resource
   160 @SYMTestPriority        High
   161 @SYMTestActions         Tests for RResourceReader::ReadReal64L() function
   162 @SYMTestExpectedResults Tests must not fail
   163 @SYMREQ                 REQ0000
   164 */
   165 void TRsReadTester::TestFlPtEdL(RResourceReader& aReader)
   166 	{
   167 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-1291 Test reading FLPTED resource "));
   168     TEST(aReader.ReadInt16L()==18);
   169     TReal little=aReader.ReadReal64L();
   170     TEST(little==0.0);
   171     TReal big=aReader.ReadReal64L();
   172     TEST(big>9.89e99);
   173     TEST(big<9.91e99);
   174 	}
   175 
   176 /**
   177 @SYMTestCaseID          SYSLIB-BAFL-CT-1292
   178 @SYMTestCaseDesc        Tests for reading MENU_BAR resource
   179 @SYMTestPriority        High
   180 @SYMTestActions         Tests for RResourceReader::ReadInt16L() function
   181 @SYMTestExpectedResults Tests must not fail
   182 @SYMREQ                 REQ0000
   183 */
   184 void TRsReadTester::TestMenuBarL(RResourceReader& aReader)
   185 	{
   186 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-1292 Test reading MENU_BAR resource "));
   187     TEST(aReader.ReadInt16L()==8); // how many items following
   188     TPtrC ref=_L("abcdefgh");
   189     for (TInt ii=1; ii<=8; ii++)
   190         {
   191         TEST(aReader.ReadInt32L()==ii);
   192         TEST(aReader.ReadTPtrCL()==ref.Left(ii));
   193         }
   194 	}
   195 
   196 /**
   197 @SYMTestCaseID          SYSLIB-BAFL-CT-1293
   198 @SYMTestCaseDesc        Tests for reading ALIGNMENT_HORROR resources
   199 @SYMTestPriority        High
   200 @SYMTestActions         Tests for RResourceReader::ReadTPtrC8L(),RResourceReader::ReadTPtrC16L() functions
   201 @SYMTestExpectedResults Tests must not fail
   202 @SYMREQ                 REQ0000
   203 */
   204 void TRsReadTester::TestAlignment1L(RResourceReader& aReader, TInt aIndex)
   205 	{
   206 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-1293 Test reading ALIGNMENT_HORROR resources "));
   207 	TPtrC8  ref8 =_L8("xyz");
   208 	TPtrC16 ref16=_L16("xyz");
   209 	TEST(aReader.ReadTPtrC8L() == ref8.Left(aIndex));
   210 	TEST(aReader.ReadTPtrC16L()== ref16.Left(aIndex));
   211 	}
   212 
   213 /**
   214 @SYMTestCaseID          SYSLIB-BAFL-CT-1294
   215 @SYMTestCaseDesc        Tests for reading ALIGNMENT_HORROR_ARRAY resource
   216 @SYMTestPriority        High
   217 @SYMTestActions         Tests for RResourceReader::ReadTPtrC8L(),RResourceReader::ReadTPtrC16L() functions
   218 @SYMTestExpectedResults Tests must not fail
   219 @SYMREQ                 REQ0000
   220 */
   221 void TRsReadTester::TestAlignment2L(RResourceReader& aReader)
   222 	{
   223 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-1294 Test reading ALIGNMENT_HORROR_ARRAY resource "));
   224     TEST(aReader.ReadInt16L()==7); // how many items following
   225     TPtrC8  ref8 =_L8("abcdef");
   226     TPtrC16 ref16=_L16("abcdef");
   227     for (TInt ii=0; ii<=6; ii++)
   228         {
   229         TEST(aReader.ReadTPtrC8L() ==ref8.Left(ii));
   230         TEST(aReader.ReadTPtrC16L()==ref16.Mid(ii));
   231         }
   232     }
   233 
   234 /**
   235 @SYMTestCaseID          SYSLIB-BAFL-CT-1295
   236 @SYMTestCaseDesc        Tests for time required to load a resource file around 100 times
   237 @SYMTestPriority        High
   238 @SYMTestActions         Tests for time taken to load resource files repeatedly
   239 @SYMTestExpectedResults Tests must not fail
   240 @SYMREQ                 REQ0000
   241 */
   242 void TRsReadTester::TimingTestsL(TRscBufAllocation aRscBufAllocation)
   243 	{
   244 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-1295 Timing tests "));
   245 	TheTest.Next(_L("BUTTON and FLPTED"));
   246 	TUint time = User::TickCount();
   247 	for (TInt ii=0; ii<100; ii++)
   248 		{
   249     	RResourceReader reader;
   250         ::OpenResourceReaderLC(reader, SYS_BUTTON_ONE, aRscBufAllocation);
   251 	    CleanupStack::PopAndDestroy(&reader);
   252         ::OpenResourceReaderLC(reader, SYS_FLPTED_ONE, aRscBufAllocation);
   253     	CleanupStack::PopAndDestroy(&reader);
   254 		}
   255 	time = User::TickCount() - time;
   256 	TheTest.Printf(_L("Time to load 100 times: %d\n"),time);
   257 
   258 	TheTest.Next(_L("BUTTON and ARRAY"));
   259 	time = User::TickCount();
   260 	for (TInt jj=0; jj<100; jj++)
   261 		{
   262     	RResourceReader reader;
   263         ::OpenResourceReaderLC(reader, SYS_BUTTON_ONE, aRscBufAllocation);
   264 	    CleanupStack::PopAndDestroy(&reader);
   265         ::OpenResourceReaderLC(reader, SYS_ARRAY_ONE, aRscBufAllocation);
   266     	CleanupStack::PopAndDestroy(&reader);
   267 		}
   268 	time = User::TickCount() - time;
   269 	TheTest.Printf(_L("Time to load 100 times: %d\n"),time);
   270 	}
   271 
   272 
   273 TUint8 TRsReadTester::DumpByte(TUint aVal)
   274     {
   275     return(aVal>9? TUint8(aVal-10+'a'): TUint8(aVal+'0'));
   276     }
   277 
   278 void TRsReadTester::DumpBytes(const TAny* aPtr,TInt aLen)
   279     {
   280     TUint8* src=(TUint8*)aPtr;
   281     TBuf<100> whole;
   282     TUint8* tar=(TUint8*)whole.Ptr();
   283     TInt len=0;
   284     while (aLen--)
   285         {
   286         TUint val=(*src++);
   287         TUint top=val/16;
   288         TUint bottom=val%16;
   289         *tar++=DumpByte(top);
   290         *tar++=DumpByte(bottom);
   291         *tar++=' ';
   292         len+=3;
   293         }
   294     whole.SetLength(len);
   295     TheTest.Printf(whole);
   296     TheTest.Printf(_L("\r\n"));
   297     }
   298 
   299 /**
   300 @SYMTestCaseID          SYSLIB-BAFL-CT-0474
   301 @SYMTestCaseDesc        Tests for the functionality of RResourceReader class
   302 @SYMTestPriority        High
   303 @SYMTestActions         Open a resource file and test for the reading the data
   304 @SYMTestExpectedResults Tests must not fail
   305 @SYMREQ                 REQ0000
   306 */
   307 static void TestButtonL(TRsReadTester& aTest, TRscBufAllocation aRscBufAllocation)
   308     {
   309 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0474 "));
   310     RResourceReader reader;
   311     ::OpenResourceReaderLC(reader, SYS_BUTTON_ONE, aRscBufAllocation);
   312 	TRAPD(errCode, aTest.TestButtonL(reader));
   313     CleanupStack::PopAndDestroy(&reader);
   314 	TEST2(errCode, KErrNone);
   315     }
   316 
   317 /**
   318 @SYMTestCaseID          SYSLIB-BAFL-CT-0475
   319 @SYMTestCaseDesc        Tests for the functionality of RResourceReader class
   320 @SYMTestPriority        High
   321 @SYMTestActions         Tests for reading descriptor array.
   322 @SYMTestExpectedResults Tests must not fail
   323 @SYMREQ                 REQ0000
   324 */
   325 static void TestArrayL(TRsReadTester& aTest, TRscBufAllocation aRscBufAllocation)
   326     {
   327 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0475 "));
   328     RResourceReader reader;
   329     ::OpenResourceReaderLC(reader, SYS_ARRAY_ONE, aRscBufAllocation);
   330 	TRAPD(errCode, aTest.TestArrayL(reader));
   331     CleanupStack::PopAndDestroy(&reader);
   332 	TEST2(errCode, KErrNone);
   333     }
   334 
   335 /**
   336 @SYMTestCaseID          SYSLIB-BAFL-CT-0476
   337 @SYMTestCaseDesc        Tests for the functionality of RResourceReader class
   338 @SYMTestPriority        High
   339 @SYMTestActions         Tests for reading real values
   340 @SYMTestExpectedResults Tests must not fail
   341 @SYMREQ                 REQ0000
   342 */
   343 static void TestFlPtEdL(TRsReadTester& aTest, TRscBufAllocation aRscBufAllocation)
   344     {
   345 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0476 "));
   346     RResourceReader reader;
   347     ::OpenResourceReaderLC(reader, SYS_FLPTED_ONE, aRscBufAllocation);
   348 	TRAPD(errCode, aTest.TestFlPtEdL(reader));
   349     CleanupStack::PopAndDestroy(&reader);
   350 	TEST2(errCode, KErrNone);
   351     }
   352 
   353 /**
   354 @SYMTestCaseID          SYSLIB-BAFL-CT-0477
   355 @SYMTestCaseDesc        Tests for the functionality of RResourceReader class
   356 @SYMTestPriority        High
   357 @SYMTestActions         Tests for reading menu_bar resource
   358 @SYMTestExpectedResults Tests must not fail
   359 @SYMREQ                 REQ0000
   360 */
   361 static void TestMenuBarL(TRsReadTester& aTest, TRscBufAllocation aRscBufAllocation)
   362     {
   363 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0477 "));
   364     RResourceReader reader;
   365     ::OpenResourceReaderLC(reader, SYS_MENUBAR_ONE, aRscBufAllocation);
   366 	TRAPD(errCode, aTest.TestMenuBarL(reader));
   367     CleanupStack::PopAndDestroy(&reader);
   368 	TEST2(errCode, KErrNone);
   369     }
   370 
   371 /**
   372 @SYMTestCaseID          SYSLIB-BAFL-CT-0478
   373 @SYMTestCaseDesc        Tests for the functionality of RResourceReader class
   374 @SYMTestPriority        High
   375 @SYMTestActions         Tests for reading descriptors and checks for alignment
   376 @SYMTestExpectedResults Tests must not fail
   377 @SYMREQ                 REQ0000
   378 */
   379 static void TestAlignment1L(TRsReadTester& aTest, TRscBufAllocation aRscBufAllocation)
   380     {
   381 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0478 "));
   382     const TInt TheHorrors[] =
   383         {
   384 	    SYS_ALIGNMENT_HORROR0, SYS_ALIGNMENT_HORROR1,
   385 	    SYS_ALIGNMENT_HORROR2, SYS_ALIGNMENT_HORROR3,
   386 	    0
   387 	    };
   388 
   389 	for(TInt i=0;TheHorrors[i]!=0;++i)
   390         {
   391         RResourceReader reader;
   392         ::OpenResourceReaderLC(reader, TheHorrors[i], aRscBufAllocation);
   393 	    TRAPD(errCode, aTest.TestAlignment1L(reader, i));
   394         CleanupStack::PopAndDestroy(&reader);
   395 	    TEST2(errCode, KErrNone);
   396         }
   397     }
   398 
   399 /**
   400 @SYMTestCaseID          SYSLIB-BAFL-CT-0479
   401 @SYMTestCaseDesc        Tests for the functionality of RResourceReader class
   402 @SYMTestPriority        High
   403 @SYMTestActions         Tests for reading descriptors and checks for alignment
   404 @SYMTestExpectedResults Tests must not fail
   405 @SYMREQ                 REQ0000
   406 */
   407 static void TestAlignment2L(TRsReadTester& aTest, TRscBufAllocation aRscBufAllocation)
   408     {
   409 	TheTest.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0479 "));
   410     RResourceReader reader;
   411     ::OpenResourceReaderLC(reader, SYS_ALIGNMENT_HORROR_ARRAY, aRscBufAllocation);
   412 	TRAPD(errCode, aTest.TestAlignment2L(reader));
   413     CleanupStack::PopAndDestroy(&reader);
   414 	TEST2(errCode, KErrNone);
   415     }
   416 
   417 static void RunTestsL()
   418     {
   419 	TheTest.Start(_L("Resource reader"));
   420 	CleanupClosePushL(TheFs);
   421 	User::LeaveIfError(TheFs.Connect());
   422     TheResourceFile = CResourceFile::NewLC(TheFs,KRomResourceFile, 0, 0);
   423 
   424 	TRsReadTester lt;
   425 
   426     ::TestButtonL(lt, EFromRscFile);
   427     ::TestButtonL(lt, EFromRscBuf);
   428 
   429     ::TestArrayL(lt, EFromRscFile);
   430     ::TestArrayL(lt, EFromRscBuf);
   431 
   432     ::TestFlPtEdL(lt, EFromRscFile);
   433     ::TestFlPtEdL(lt, EFromRscBuf);
   434 
   435     ::TestMenuBarL(lt, EFromRscFile);
   436     ::TestMenuBarL(lt, EFromRscBuf);
   437 
   438     ::TestAlignment1L(lt, EFromRscFile);
   439     ::TestAlignment1L(lt, EFromRscBuf);
   440 
   441     ::TestAlignment2L(lt, EFromRscFile);
   442     ::TestAlignment2L(lt, EFromRscBuf);
   443 
   444     TRAPD(errCode, lt.TimingTestsL(EFromRscFile));
   445 	TEST2(errCode, KErrNone);
   446 
   447     TRAP(errCode, lt.TimingTestsL(EFromRscBuf));
   448 	TEST2(errCode, KErrNone);
   449 
   450 	CleanupStack::PopAndDestroy(TheResourceFile);
   451 	CleanupStack::PopAndDestroy(1, &TheFs);
   452     }
   453 
   454 TInt E32Main()
   455 	{
   456 	__UHEAP_MARK;
   457     CTrapCleanup *cleanup=CTrapCleanup::New();
   458 	TheTest.Title();
   459     TRAPD(err,::RunTestsL());
   460 	TEST2(err, KErrNone);
   461 	TheTest.Next(_L("/n"));
   462 	TheTest.End();
   463 	TheTest.Close();
   464     delete(cleanup);
   465 	__UHEAP_MARKEND;
   466     return(0);
   467 	}