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