os/ossrv/lowlevellibsandfws/apputils/tsrc/T_RSC.CPP
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/lowlevellibsandfws/apputils/tsrc/T_RSC.CPP	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,396 @@
     1.4 +// Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.5 +// All rights reserved.
     1.6 +// This component and the accompanying materials are made available
     1.7 +// under the terms of "Eclipse Public License v1.0"
     1.8 +// which accompanies this distribution, and is available
     1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.10 +//
    1.11 +// Initial Contributors:
    1.12 +// Nokia Corporation - initial contribution.
    1.13 +//
    1.14 +// Contributors:
    1.15 +//
    1.16 +// Description:
    1.17 +// Started by MJB, May 1995
    1.18 +// Updated Nov 1996 by PNJ for Unicode.
    1.19 +// Tests RResourceFile class
    1.20 +// 
    1.21 +//
    1.22 +
    1.23 +#include <e32test.h>
    1.24 +#include <bautils.h>
    1.25 +#include <barsc.h>
    1.26 +#include <barsc2.h>
    1.27 +#include <trsc.rsg>
    1.28 +
    1.29 +#include "T_RSC.H"
    1.30 +
    1.31 +LOCAL_D RTest test(_L("T_RSC"));
    1.32 +LOCAL_D RFs TheFs;
    1.33 +
    1.34 +_LIT(KRamResourceFile, "c:\\T_RSC.RSC");
    1.35 +_LIT(KRamResourceFile2, "c:\\NewRscFormat.RSC");
    1.36 +_LIT(KFailResourceFile,"z:\\system\\data\\nonexist.rsc");
    1.37 +
    1.38 +LOCAL_C TInt compare( const TDesC8 &aresbuf, const TDesC &abuf, TBool aUnicode )
    1.39 +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    1.40 +//
    1.41 +// Compare a buffer taken from a resource file with one that was defined in
    1.42 +// source code.  All resources read at this level are read as pure data.
    1.43 +// However, the resource file entries for this test code are defined as
    1.44 +// generic text, which may be 8 or 16 bit codes, depending on how the
    1.45 +// resource was compiled.  The third parameter indicates which is expected.
    1.46 +//
    1.47 +//
    1.48 +// Return a true value if the text matches, else false.
    1.49 +//
    1.50 +// ----------------------------------------------------------------------------
    1.51 +    {
    1.52 +
    1.53 +    TInt count = abuf.Length();
    1.54 +
    1.55 +    // exit immediately of the strings are of different length.
    1.56 +    if ( abuf.Size() != aresbuf.Size() ) return FALSE;
    1.57 +
    1.58 +    // loop through character by character and exit if any comparison fails.
    1.59 +
    1.60 +    if ( aUnicode )
    1.61 +        {
    1.62 +        while ( count > 0 )
    1.63 +            {
    1.64 +            TInt uch;
    1.65 +            TInt bi; // byte index
    1.66 +            count -=1;
    1.67 +            bi = count*2;
    1.68 +            uch = (aresbuf[bi+1]<<8) | aresbuf[bi];
    1.69 +            if ( uch != abuf[count] ) return FALSE;
    1.70 +            }
    1.71 +        }
    1.72 +    else
    1.73 +        {
    1.74 +        while ( count > 0 )
    1.75 +            {
    1.76 +            count -=1;
    1.77 +            if ( aresbuf[count] != abuf[count] ) return FALSE;
    1.78 +            }
    1.79 +        }
    1.80 +
    1.81 +    return TRUE;
    1.82 +
    1.83 +    } // end of compare function.
    1.84 +
    1.85 +
    1.86 +LOCAL_C void CopyFileL(const TDesC& aFrom,const TDesC& aTo)
    1.87 +//
    1.88 +// helper function
    1.89 +//
    1.90 +	{
    1.91 +	TheFs.Delete(aTo);
    1.92 +	CFileMan* man=CFileMan::NewL(TheFs);
    1.93 +	TInt r=man->Copy(aFrom,aTo);
    1.94 +	delete man;
    1.95 +	User::LeaveIfError(r);
    1.96 +	User::LeaveIfError(TheFs.SetAtt(aTo,0,KEntryAttReadOnly)); // clear RO
    1.97 +	}
    1.98 +
    1.99 +LOCAL_C void DeleteFile(const TDesC& aFileName)
   1.100 +//
   1.101 +// helper function
   1.102 +//
   1.103 +	{
   1.104 +	// make sure the file is read/write
   1.105 +	TInt err = TheFs.SetAtt(aFileName, 0, KEntryAttReadOnly);
   1.106 +	if(err != KErrNone)
   1.107 +		{
   1.108 +		RDebug::Print(_L("error changing attributes file = %d"),err);
   1.109 +		}
   1.110 +	// delete the file
   1.111 +	err = BaflUtils::DeleteFile(TheFs, aFileName);
   1.112 +	if(err != KErrNone)
   1.113 +		{
   1.114 +		RDebug::Print(_L("error deleting file = %d"),err);
   1.115 +		}
   1.116 +	}
   1.117 +
   1.118 +LOCAL_C TInt FileSizeL(const TDesC& aFileName)
   1.119 +	{
   1.120 +	RFile file;
   1.121 +	User::LeaveIfError(file.Open(TheFs, aFileName, EFileRead));
   1.122 +	CleanupClosePushL(file);
   1.123 +	TInt size = 0;
   1.124 +	User::LeaveIfError(file.Size(size));
   1.125 +	CleanupStack::PopAndDestroy(&file);
   1.126 +	return size;
   1.127 +	}
   1.128 +
   1.129 +template <class T> class TestRsc
   1.130 +	{
   1.131 +protected:
   1.132 +	void TestReadL(T* aRscFile);
   1.133 +    };
   1.134 +
   1.135 +class TestRRsc : public TestRsc<RResourceFile>
   1.136 +	{
   1.137 +public:
   1.138 +	void TestReadROldL(const TDesC &aTitle, const TDesC &aFileName);
   1.139 +	void TestReadRNewL(const TDesC &aTitle, const TDesC &aFileName, TUint aFileOffset, TUint aFileSize);
   1.140 +	void TestOpenR();
   1.141 +    };
   1.142 +
   1.143 +class TestCRsc : public TestRsc<CResourceFile>
   1.144 +	{
   1.145 +public:
   1.146 +	void TestReadCNewL(const TDesC &aTitle, const TDesC &aFileName, TUint aFileOffset, TUint aFileSize);
   1.147 +	void TestOpenC();
   1.148 +    };
   1.149 +
   1.150 +/**
   1.151 +@SYMTestCaseID          SYSLIB-BAFL-CT-0427
   1.152 +@SYMTestCaseDesc        RResourceFile class functionality test
   1.153 +@SYMTestPriority        Medium
   1.154 +@SYMTestActions         Attempts to read from an 8 and 16 bit resource file
   1.155 +@SYMTestExpectedResults Tests must not fail
   1.156 +@SYMREQ                 REQ0000
   1.157 +*/
   1.158 +void TestRRsc::TestReadROldL(const TDesC &aTitle, const TDesC &aFileName)
   1.159 +	{
   1.160 +	test.Start(aTitle);
   1.161 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0427 "));
   1.162 +	RResourceFile rsc;
   1.163 +	CleanupClosePushL(rsc);
   1.164 +	rsc.OpenL(TheFs, aFileName);
   1.165 +	TestReadL(&rsc);
   1.166 +	CleanupStack::PopAndDestroy();
   1.167 +	test.End();
   1.168 +	}
   1.169 +
   1.170 +/**
   1.171 +@SYMTestCaseID          SYSLIB-BAFL-CT-0428
   1.172 +@SYMTestCaseDesc        Tests for RResourceFile::OpenL(,,,) function
   1.173 +@SYMTestPriority        Medium
   1.174 +@SYMTestActions         Attempt to read from a resource file
   1.175 +@SYMTestExpectedResults Tests must not fail
   1.176 +@SYMREQ                 REQ0000
   1.177 +*/
   1.178 +void TestRRsc::TestReadRNewL(const TDesC &aTitle, const TDesC &aFileName, TUint aFileOffset, TUint aFileSize)
   1.179 +	{
   1.180 +	test.Start(aTitle);
   1.181 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0428 "));
   1.182 +	RResourceFile rsc;
   1.183 +	CleanupClosePushL(rsc);
   1.184 +	rsc.OpenL(TheFs, aFileName, aFileOffset, aFileSize);
   1.185 +	TestReadL(&rsc);
   1.186 +	CleanupStack::PopAndDestroy();
   1.187 +	test.End();
   1.188 +	}
   1.189 +
   1.190 +/**
   1.191 +@SYMTestCaseID          SYSLIB-BAFL-CT-0429
   1.192 +@SYMTestCaseDesc        Testing error recovery - R file
   1.193 +@SYMTestPriority        Medium
   1.194 +@SYMTestActions         Attempt to read a resouce file,should recover on error.
   1.195 +@SYMTestExpectedResults Tests must not fail
   1.196 +@SYMREQ                 REQ0000
   1.197 +*/
   1.198 +void TestRRsc::TestOpenR()
   1.199 +	{
   1.200 +	test.Start(_L("Testing error recovery - R file"));
   1.201 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0429 "));
   1.202 +	RResourceFile rsc_file;
   1.203 +    TRAPD(err, rsc_file.OpenL(TheFs, KFailResourceFile));
   1.204 +    test(err != KErrNone);
   1.205 +	test.End();
   1.206 +	}
   1.207 +
   1.208 +/**
   1.209 +@SYMTestCaseID          SYSLIB-BAFL-CT-0430
   1.210 +@SYMTestCaseDesc        Tests for RResourceFile::NewL() function
   1.211 +@SYMTestPriority        Medium
   1.212 +@SYMTestActions         Tests for reading the new resource file
   1.213 +@SYMTestExpectedResults Tests must not fail
   1.214 +@SYMREQ                 REQ0000
   1.215 +*/
   1.216 +void TestCRsc::TestReadCNewL(const TDesC &aTitle, const TDesC &aFileName, TUint aFileOffset, TUint aFileSize)
   1.217 +	{
   1.218 +	test.Start(aTitle);
   1.219 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0430 "));
   1.220 +	CResourceFile* rsc = CResourceFile::NewL(TheFs, aFileName, aFileOffset, aFileSize);
   1.221 +	CleanupStack::PushL(rsc);
   1.222 +	TestReadL(rsc);
   1.223 +	CleanupStack::PopAndDestroy(rsc);
   1.224 +	test.End();
   1.225 +	}
   1.226 +
   1.227 +/**
   1.228 +@SYMTestCaseID          SYSLIB-BAFL-CT-0431
   1.229 +@SYMTestCaseDesc        Testing error recovery - C file
   1.230 +@SYMTestPriority        Medium
   1.231 +@SYMTestActions         Tests for new open resource file,should recover on error
   1.232 +@SYMTestExpectedResults Tests must not fail
   1.233 +@SYMREQ                 REQ0000
   1.234 +*/
   1.235 +void TestCRsc::TestOpenC()
   1.236 +	{
   1.237 +	test.Start(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0431 Testing error recovery - C file "));
   1.238 +	CResourceFile* rsc_file = NULL;
   1.239 +    TRAPD(err, rsc_file = CResourceFile::NewL(TheFs, KFailResourceFile, 0, 0));
   1.240 +    test(err != KErrNone);
   1.241 +	delete rsc_file;
   1.242 +	test.End();
   1.243 +	}
   1.244 +
   1.245 +template <class T> void TestRsc<T>::TestReadL(T* aRscFile)
   1.246 +	{
   1.247 +	TInt ii;
   1.248 +    //
   1.249 +    // First test to see if we have an 8 or 16-bit text resource file.
   1.250 +    // This is done by looking at a signature resource that is defined
   1.251 +    // as an LTEXT item and seeing how its length computes.
   1.252 +    //
   1.253 +
   1.254 +    test.Next(_L("Testing 8 or 16-bit signature"));
   1.255 +
   1.256 +
   1.257 +    TBool isUnicode = EFalse;
   1.258 +
   1.259 +    HBufC8 *ps = aRscFile->AllocReadL(R_TEXT_SIGNATURE);
   1.260 +    TPtr8 sig = ps->Des();
   1.261 +
   1.262 +    TInt l = sig.Length();		// length of the raw resource data
   1.263 +    TInt sig_count = sig[0];	// count byte at the start of the LTEXT
   1.264 +
   1.265 +    if ( l == (1 + sig_count) )	// count + TText8 data
   1.266 +		{
   1.267 +		test.Printf(_L("8-bit resource text\n"));
   1.268 +		}
   1.269 +    else
   1.270 +	if ( l == (1+1+(sig_count*2)) )		// count + padding + TText16 data
   1.271 +		{
   1.272 +		isUnicode = ETrue;
   1.273 +		test.Printf(_L("Unicode resource text...\n"));
   1.274 +		test(sig[1]==0xAB);				// check for the defined padding byte
   1.275 +		}
   1.276 +	else
   1.277 +		test.Printf(_L("Invalid signature found\n"));
   1.278 +
   1.279 +    User::Free(ps);
   1.280 +
   1.281 +	for (ii=SYS_SPECIAL_CHARACTERS;ii<=SYS_PAGE_IS;ii++)
   1.282 +		{
   1.283 +		TPtrC des=TPtrC(S_Resource[ii]);
   1.284 +		TBuf<256> testbuf;
   1.285 +		testbuf.Format(_L("Testing ReadL for resource: \"%S\""),&des);
   1.286 +		test.Next(testbuf);
   1.287 +		TBuf8<256> buf;
   1.288 +		aRscFile->ReadL(buf,ii);
   1.289 +        test(compare(buf,des,isUnicode));
   1.290 +		}
   1.291 +
   1.292 +	for (ii=SYS_SPECIAL_CHARACTERS;ii<=SYS_PAGE_IS;ii++)
   1.293 +		{
   1.294 +		TPtrC des=TPtrC(S_Resource[ii]);
   1.295 +		TBuf<256> testbuf;
   1.296 +		testbuf.Format(_L("Testing AllocReadL for resource: \"%S\""),&des);
   1.297 +		test.Next(testbuf);
   1.298 +		HBufC8 *p=aRscFile->AllocReadL(ii);
   1.299 +		TPtr8 wp=p->Des();
   1.300 +		test(compare(wp,des,isUnicode));
   1.301 +		User::Free(p);
   1.302 +		}
   1.303 +
   1.304 +	for (ii=SYS_SPECIAL_CHARACTERS;ii<=SYS_PAGE_IS;ii++)
   1.305 +		{
   1.306 +		TPtrC des=TPtrC(S_Resource[ii]);
   1.307 +		TBuf<256> testbuf;
   1.308 +		testbuf.Format(_L("Testing AllocReadLC for resource: \"%S\""),&des);
   1.309 +		test.Next(testbuf);
   1.310 +		HBufC8 *p=aRscFile->AllocReadLC(ii);
   1.311 +		TPtr8 wp=p->Des();
   1.312 +		test(compare(wp,des,isUnicode));
   1.313 +		CleanupStack::PopAndDestroy();
   1.314 +		}
   1.315 +    }
   1.316 +
   1.317 +/**
   1.318 +@SYMTestCaseID          SYSLIB-BAFL-CT-0432
   1.319 +@SYMTestCaseDesc        Testing RResourceFile & CResourceFile classes
   1.320 +@SYMTestPriority        High
   1.321 +@SYMTestActions         Wrapper function, calls up test execute functions
   1.322 +@SYMTestExpectedResults Tests must not fail
   1.323 +@SYMREQ                 REQ0000
   1.324 +*/
   1.325 +LOCAL_C void DoTestsL()
   1.326 +    {
   1.327 +	test.Next(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-0432 "));
   1.328 +	CleanupClosePushL(TheFs);
   1.329 +	User::LeaveIfError(TheFs.Connect());
   1.330 +
   1.331 +	test.Printf(_L("Copying resource files to RAM\r\n"));
   1.332 +	CopyFileL(KRomResourceFile,KRamResourceFile);
   1.333 +	CopyFileL(KRomResourceFile2,KRamResourceFile2);
   1.334 +
   1.335 +	//The new resource format rsc file is compiled from RscHeader.bin, TRsc.rsc and 16RAMC.mbm.
   1.336 +	//So I want to know the offset and size of TRsc.rsc.
   1.337 +	TInt header_size = ::FileSizeL(KRomResourceFileHeader);
   1.338 +	TInt rsc_size1 = ::FileSizeL(KRomResourceFile);
   1.339 +	TInt rsc_size2 = ::FileSizeL(KRamResourceFile);
   1.340 +
   1.341 +	TestRRsc t1;
   1.342 +
   1.343 +	t1.TestReadROldL(_L("Testing ROM Resource file T_RSC.RSC"),KRomResourceFile);
   1.344 +
   1.345 +	t1.TestReadRNewL(_L("Testing ROM Resource file NewRscFormat.RSC"),
   1.346 +							KRomResourceFile2,
   1.347 +							header_size,
   1.348 +							rsc_size1);
   1.349 +
   1.350 +	t1.TestReadROldL(_L("Testing RAM Resource file T_RSC.RSC"),KRamResourceFile);
   1.351 +
   1.352 +	t1.TestReadRNewL(_L("Testing RAM Resource file NewRscFormat.RSC"),
   1.353 +						KRamResourceFile2,
   1.354 +						header_size,
   1.355 +						rsc_size2);
   1.356 +
   1.357 +	t1.TestOpenR();
   1.358 +
   1.359 +	TestCRsc t2;
   1.360 +
   1.361 +	t2.TestReadCNewL(_L("Testing ROM Resource file T_RSC.RSC"),KRomResourceFile, 0, 0);
   1.362 +
   1.363 +	t2.TestReadCNewL(_L("Testing ROM Resource file NewRscFormat.RSC"),
   1.364 +						KRomResourceFile2,
   1.365 +						header_size,
   1.366 +						rsc_size1);
   1.367 +
   1.368 +	t2.TestReadCNewL(_L("Testing RAM Resource file T_RSC.RSC"),KRamResourceFile, 0, 0);
   1.369 +
   1.370 +	t2.TestReadCNewL(_L("Testing RAM Resource file NewRscFormat.RSC"),
   1.371 +						KRamResourceFile2,
   1.372 +						header_size,
   1.373 +						rsc_size2);
   1.374 +
   1.375 +	t2.TestOpenC();
   1.376 +
   1.377 +   	// tidy up
   1.378 +	DeleteFile(KRamResourceFile);
   1.379 +	DeleteFile(KRamResourceFile2);
   1.380 +
   1.381 +	CleanupStack::PopAndDestroy(1, &TheFs);
   1.382 +    }
   1.383 +
   1.384 +GLDEF_C TInt E32Main()
   1.385 +	{
   1.386 +    __UHEAP_MARK;
   1.387 +    CTrapCleanup *cleanup=CTrapCleanup::New();
   1.388 +	test.Title();
   1.389 +	test.Start(_L("Testing RResourceFile & CResourceFile"));
   1.390 +    TRAPD(err,DoTestsL());
   1.391 +    test.Printf(_L("Error code is %d\n"),err);
   1.392 +    test(err==KErrNone);
   1.393 +    test.Next(_L("/n"));
   1.394 +	test.End();
   1.395 +    test.Close();
   1.396 +    delete cleanup;
   1.397 +    __UHEAP_MARKEND;
   1.398 +	return(0);
   1.399 +    }