os/ossrv/lowlevellibsandfws/apputils/initLocale/test/t_initialiselocale.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/lowlevellibsandfws/apputils/initLocale/test/t_initialiselocale.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,358 @@
     1.4 +// Copyright (c) 2005-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 +//
    1.18 +
    1.19 +#include <bautils.h>
    1.20 +#include <f32file.h>
    1.21 +#include <e32cmn.h>
    1.22 +#include <e32test.h>
    1.23 +#include <e32std.h>
    1.24 +#include <e32const.h>
    1.25 +#include <babackup.h>
    1.26 +#include <hal.h>
    1.27 +#include <centralrepository.h>
    1.28 +#include "LocaleRepository.h"
    1.29 +#include "InitialiseLocale.h"
    1.30 +
    1.31 +
    1.32 +_LIT(KInitialiseLocaleExeName, "InitialiseLocale.exe");
    1.33 +
    1.34 +// Symbian VendorID as specified in 
    1.35 +// "Platform security engineering guide" Section 2.1.3.2 
    1.36 +const TVendorId KSymbianVendorId(0x70000001);
    1.37 +
    1.38 +RTest TheTest(_L("T_Initialise_Locale"));
    1.39 +
    1.40 +// const TInt KDefaultCountryCode = 29 ;
    1.41 +const TInt KNewCountryCode = 46 ;
    1.42 +TInt defaultCountryCode ;
    1.43 +
    1.44 +TCurrencySymbol defaultCurrencySymbol;
    1.45 +
    1.46 +_LIT(KCurrencySymbol, "ABC") ;
    1.47 +TBuf<8> KNewCurrencySymbol (KCurrencySymbol) ;
    1.48 +
    1.49 +_LIT(KLongDateFormat, "%F%*D%X %N %Y") ;
    1.50 +TBuf<20> KDefaultLongDateFormat (KLongDateFormat) ;
    1.51 +
    1.52 +_LIT(KShortDateFormat, "%F%*D/%*M/%Y") ;
    1.53 +TBuf<20> KDefaultShortDateFormat (KShortDateFormat) ;
    1.54 +
    1.55 +_LIT(KTimeFormat, "%F%*I:%T:%S %*A") ;
    1.56 +TBuf<20> KDefaultTimeFormat (KTimeFormat) ;
    1.57 +
    1.58 +TExtendedLocale initialExtendedLocale;
    1.59 +
    1.60 +void GetInitialLocaleSettings()
    1.61 +	{
    1.62 +	initialExtendedLocale.LoadSystemSettings() ;
    1.63 +	}
    1.64 +
    1.65 +void RestoreInitialLocaleSettings()
    1.66 +	{
    1.67 +	initialExtendedLocale.SaveSystemSettings() ;
    1.68 +	User::After(1000000);
    1.69 +	}
    1.70 +
    1.71 +//Test macros and functions
    1.72 +
    1.73 +LOCAL_C void Check(TInt aValue, TInt aLine)
    1.74 +	{
    1.75 +	if(!aValue)
    1.76 +		{
    1.77 +		RestoreInitialLocaleSettings();
    1.78 +		TheTest(EFalse, aLine);
    1.79 +		}
    1.80 +	}
    1.81 +
    1.82 +LOCAL_C void Check(TInt aValue, TInt aExpected, TInt aLine)
    1.83 +	{
    1.84 +	if(aValue != aExpected)
    1.85 +		{
    1.86 +		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
    1.87 +		RestoreInitialLocaleSettings();
    1.88 +		TheTest(EFalse, aLine);
    1.89 +		}
    1.90 +	}
    1.91 +
    1.92 +LOCAL_C void Check(TPtrC aValue, TPtrC aExpected, TInt aLine)
    1.93 +	{
    1.94 +	if(aValue != aExpected)
    1.95 +		{
    1.96 +		RDebug::Print(_L("*** Expected error: %S, got: %S\r\n"), &aExpected, &aValue);
    1.97 +		RestoreInitialLocaleSettings();
    1.98 +		TheTest(EFalse, aLine);
    1.99 +		}
   1.100 +	}
   1.101 +
   1.102 +#define TEST(arg) ::Check((arg), __LINE__)
   1.103 +#define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
   1.104 +#define TEST3(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
   1.105 +
   1.106 +/**
   1.107 +@SYMTestCaseID		SYSLIB-BAFL-CIT-0200
   1.108 +@SYMTestCaseDesc 	Check that default locale values are loaded on startup.
   1.109 +@SYMTestPriority 	High
   1.110 +@SYMTestActions  	Ensure that country code in system locale data matches value in persisted data
   1.111 +@SYMTestExpectedResults The test must not fail.
   1.112 +@SYMREQ PREQ1003
   1.113 +*/
   1.114 +void CheckDefaultLocaleCountryCode()
   1.115 +	{
   1.116 +	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-BAFL-CIT-0200 Check default country code "));
   1.117 +
   1.118 +	TExtendedLocale myExtendedLocale;
   1.119 +	myExtendedLocale.LoadSystemSettings() ;
   1.120 +	TLocale* myLocale = myExtendedLocale.GetLocale() ;
   1.121 +	TInt countryCode = myLocale->CountryCode();
   1.122 +	TheTest.Printf(_L("Default Country code is %d\n"), defaultCountryCode) ;
   1.123 +	TheTest.Printf(_L("Current Country code is %d\n"), countryCode) ;
   1.124 +
   1.125 +	TEST2(countryCode, defaultCountryCode) ;
   1.126 +	TheTest.End();
   1.127 +	}
   1.128 +
   1.129 +/**
   1.130 +@SYMTestCaseID		SYSLIB-BAFL-CT-3160
   1.131 +@SYMTestCaseDesc 	Check that default locale values i.e. Currency Symbol and long date, short date & time format.
   1.132 +@SYMTestPriority 	High
   1.133 +@SYMTestActions  	Ensure that these values are set to the default values as defined.
   1.134 +@SYMTestExpectedResults The test must not fail.
   1.135 +@SYMREQ PREQ1003
   1.136 +*/
   1.137 +void CheckDefaultLocaleValues()
   1.138 +{
   1.139 +	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-3160 Check default Locale Values "));
   1.140 +	TExtendedLocale myExtendedLocale;
   1.141 +	myExtendedLocale.LoadSystemSettings() ;
   1.142 +
   1.143 +	TPtrC currencySymbolPtr = myExtendedLocale.GetCurrencySymbol();
   1.144 +	TPtrC longDateFormatPtr = myExtendedLocale.GetLongDateFormatSpec();
   1.145 +	TPtrC shortDateFormatPtr = myExtendedLocale.GetShortDateFormatSpec();
   1.146 +	TPtrC timeFormatPtr = myExtendedLocale.GetTimeFormatSpec();
   1.147 +
   1.148 +	TheTest.Printf(_L("New Currency Symbol is %S \n"), &currencySymbolPtr);
   1.149 +	TheTest.Printf(_L("Default Long Date Format is %S \n"), &longDateFormatPtr);
   1.150 +	TheTest.Printf(_L("Default Short Date Format is %S \n"), &shortDateFormatPtr);
   1.151 +	TheTest.Printf(_L("Default Time Format is %S \n"), &timeFormatPtr);
   1.152 +
   1.153 +	TPtrC expectedCurrSymPtr(KNewCurrencySymbol);
   1.154 +	TEST3(currencySymbolPtr, expectedCurrSymPtr);
   1.155 +
   1.156 +	TPtrC expectedlongDatePtr(KDefaultLongDateFormat);
   1.157 +	TEST3(longDateFormatPtr, expectedlongDatePtr);
   1.158 +
   1.159 +	TPtrC expectedShortDatePtr(KDefaultShortDateFormat);
   1.160 +	TEST3(shortDateFormatPtr, expectedShortDatePtr);
   1.161 +
   1.162 +	TPtrC expectedTimePtr(KDefaultTimeFormat);
   1.163 +	TEST3(timeFormatPtr, expectedTimePtr);
   1.164 +
   1.165 +	TheTest.End();
   1.166 +}
   1.167 +
   1.168 +/**
   1.169 +@SYMTestCaseID		SYSLIB-BAFL-CIT-0201
   1.170 +@SYMTestCaseDesc 	Check that locale settings can be saved to and read from system locale data through TExtendedLocale.
   1.171 +@SYMTestPriority 	High
   1.172 +@SYMTestActions  	Change a number of locale values, write to system data, read back from data, check result
   1.173 +@SYMTestExpectedResults The test must not fail.
   1.174 +@SYMREQ PREQ1003
   1.175 +*/
   1.176 +void ChangeLocaleCountryCode()
   1.177 +	{
   1.178 +	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-BAFL-CIT-0201 Define a TLocale "));
   1.179 +	TheTest.Next(_L("Setting CountryCode"));
   1.180 +
   1.181 +	TExtendedLocale myExtendedLocale;
   1.182 +	myExtendedLocale.LoadSystemSettings() ;
   1.183 +	TLocale* myLocale = myExtendedLocale.GetLocale() ;
   1.184 +	TheTest.Printf(_L("Setting Country code to %d\n"), KNewCountryCode) ;
   1.185 +	myLocale->SetCountryCode(KNewCountryCode);
   1.186 +	TheTest.Printf(_L("Saving new settings to system locale data\n")) ;
   1.187 +	myExtendedLocale.SaveSystemSettings() ;
   1.188 +	TheTest.Printf(_L("Loading new settings from system locale data\n")) ;
   1.189 +	myExtendedLocale.LoadSystemSettings() ;
   1.190 +	myLocale = myExtendedLocale.GetLocale() ;
   1.191 +	TInt countryCode = myLocale->CountryCode() ;
   1.192 +	TheTest.Printf(_L("New Country code is %d\n"), countryCode) ;
   1.193 +	TEST2(countryCode, KNewCountryCode) ;
   1.194 +	TheTest.End();
   1.195 +	}
   1.196 +
   1.197 +
   1.198 +/**
   1.199 +@SYMTestCaseID      SYSLIB-BAFL-CIT-188
   1.200 +@SYMTestCaseDesc    Check that locale settings are saved into central repository.
   1.201 +@SYMTestPriority    High
   1.202 +@SYMTestActions     Ensure that locale data stored in the central repository match the data in the system locale
   1.203 +@SYMTestExpectedResults The test must not fail.
   1.204 +@SYMREQ PREQ1003
   1.205 +*/
   1.206 +void CheckRepositoryLocaleCountryCode(CRepository *repository)
   1.207 +   {
   1.208 +   TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-BAFL-CIT-188 Checking persisted country code "));
   1.209 +   TInt datacageCountryCode;
   1.210 +   TheTest.Printf(_L("Getting repository data cage\n")) ;
   1.211 +   TInt err = repository->Get(KLocaleCountryCode, datacageCountryCode) ;
   1.212 +   TEST2(err, KErrNone);
   1.213 +   TheTest.Printf(_L("Country code from repository = %d\n"), datacageCountryCode) ;
   1.214 +   TEST2(datacageCountryCode, KNewCountryCode) ;
   1.215 +   TheTest.End();
   1.216 +   }
   1.217 +
   1.218 +/**
   1.219 +@SYMTestCaseID  			SYSLIB-BAFL-CT-4051
   1.220 +@SYMTestCaseDesc  			Check that the VendorID is set to 0x70000001 (Symbian's VendorID)
   1.221 +@SYMTestPriority   			Medium
   1.222 +@SYMTestActions 			1. Create a process for InitialiseLocale.exe
   1.223 +							2. Compare the processes VID against the expected VID (0x70000001)
   1.224 +@SYMTestExpectedResults  	The VIDs should be identical and so the test will pass.
   1.225 +@SYMDEF 					INC121403
   1.226 +*/
   1.227 +void TestVendorIdINC121403(RProcess& aProcess)
   1.228 +	{
   1.229 +	TheTest.Next (_L ("TestVendorIDINC121403"));
   1.230 +	
   1.231 +	// Test that the VendorID of InitialiseLocale.exe is set to the Symbian VendorID
   1.232 +	TEST2(KSymbianVendorId.iId, aProcess.VendorId().iId);
   1.233 +	}
   1.234 +
   1.235 +/**
   1.236 +Sets up the Currency Symbol test. Outputs default values then sets the Currency Symbol
   1.237 +to a test value ("ABC") and stores this value in the repository, before InitialiseLocale has run.
   1.238 +*/
   1.239 +void ChangeLocaleCurrencySymbol(CRepository *repository, TCurrencySymbol & defaultCurrencySymbol)
   1.240 +	{
   1.241 +	TheTest.Start(_L("Setting CurrencySymbol\n"));
   1.242 +
   1.243 +	TExtendedLocale myExtendedLocale;
   1.244 +	myExtendedLocale.LoadSystemSettings() ;
   1.245 +
   1.246 +	TheTest.Printf(_L("Default Currency Symbol is:  %S\n"), &defaultCurrencySymbol);
   1.247 +
   1.248 +	TCurrencySymbol datacageCurrencySymbol;
   1.249 +	TInt err = repository->Get(KLocaleCurrencySymbol, datacageCurrencySymbol) ;
   1.250 +	TEST2(err, KErrNone);
   1.251 +	TheTest.Printf(_L("Currency Symbol from repository data cage is:  %S\n"), &datacageCurrencySymbol);
   1.252 +
   1.253 +	TheTest.Printf(_L("Setting Currency Symbol to:  %S\n"), &KNewCurrencySymbol);
   1.254 +
   1.255 +	TheTest.Printf(_L("Saving Currency Symbol to repository.\n\n"));
   1.256 +	err = repository->Set(KLocaleCurrencySymbol, KNewCurrencySymbol);
   1.257 +	TEST2(err, KErrNone);
   1.258 +	User::SetCurrencySymbol(KNewCurrencySymbol);
   1.259 +
   1.260 +	TheTest.End();
   1.261 +	}
   1.262 +
   1.263 +/**
   1.264 +After InitialiseLocale has run. Gets the Currency Symbol back from the repository and then
   1.265 +outputs it. This output should be the same value as the test value ("ABC") that was set to the
   1.266 +repository in the above function.
   1.267 +*/
   1.268 +void TestLocaleCurrencySymbol(CRepository *repository)
   1.269 +	{
   1.270 +	TheTest.Start(_L("Checking value in the repository after InitialiseLocale() has run.\n"));
   1.271 +
   1.272 +	TCurrencySymbol reposCurrencySymbol;
   1.273 +	TInt err = repository->Get(KLocaleCurrencySymbol, reposCurrencySymbol);
   1.274 +	TEST2(err, KErrNone);
   1.275 +
   1.276 +	TheTest.Printf(_L("The Currency Symbol after InitialiseLocale() has ran is: %S\n\n"), &reposCurrencySymbol);
   1.277 +
   1.278 +	TEST(KNewCurrencySymbol == reposCurrencySymbol);
   1.279 +
   1.280 +	TheTest.End();
   1.281 +	}
   1.282 +
   1.283 +
   1.284 +
   1.285 +//
   1.286 +// Test new locale initialisation and persistence mechanism
   1.287 +void TestLocaleChanges()
   1.288 +	{
   1.289 +	RProcess process;
   1.290 +
   1.291 +	GetInitialLocaleSettings() ;
   1.292 +
   1.293 +	// Restore default contents of locale settings repository
   1.294 +	TUid LocaleRepositoryUid ;
   1.295 +	LocaleRepositoryUid.iUid = KLocalePersistRepositoryUid ;
   1.296 +	CRepository* repository = CRepository::NewLC(LocaleRepositoryUid) ;
   1.297 +	repository->Reset();
   1.298 +
   1.299 +	TInt err = repository->Get(KLocaleCountryCode, defaultCountryCode) ;
   1.300 +	TEST2(err, KErrNone);
   1.301 +	ChangeLocaleCurrencySymbol(repository, defaultCurrencySymbol);
   1.302 +
   1.303 +	// Initialise locale data and start persistence notifier by spawning
   1.304 +	// process - this will be done as part of system start in actual
   1.305 +	// release code!
   1.306 +	TInt r = process.Create(KInitialiseLocaleExeName, KNullDesC);
   1.307 +	if(r == KErrNone)
   1.308 +		{
   1.309 +		TRequestStatus stat;
   1.310 +		process.Rendezvous(stat);
   1.311 +		process.Resume(); // Start the process going
   1.312 +		//wait for the locale initialisation to complete first before testing
   1.313 +		User::WaitForRequest(stat);
   1.314 +		TEST2((stat.Int()==KErrNone)||(stat.Int()==KErrAlreadyExists),ETrue);
   1.315 +		TInt flag;
   1.316 +		TUid initLocaleUid=TUid::Uid(KUidLocalePersistProperties);
   1.317 +		r=RProperty::Get(initLocaleUid,(TUint)EInitialisationState,flag);
   1.318 +		TEST2(r,KErrNone);
   1.319 +		//check for the P&S flag that indicates localeinitialisation complete
   1.320 +		TEST2(flag,ELocaleInitialisationComplete);
   1.321 +		TestLocaleCurrencySymbol(repository);
   1.322 +		User::After(1000000);
   1.323 +		CheckDefaultLocaleCountryCode();
   1.324 +		User::After(1000000);
   1.325 +		CheckDefaultLocaleValues();
   1.326 +		User::After(1000000);
   1.327 +		ChangeLocaleCountryCode();
   1.328 +		User::After(1000000);
   1.329 +		CheckRepositoryLocaleCountryCode(repository);
   1.330 +		User::After(1000000);
   1.331 +		TestVendorIdINC121403(process);
   1.332 +		process.Close();
   1.333 +		User::After(1000000);
   1.334 +		}
   1.335 +	CleanupStack::PopAndDestroy(repository) ;
   1.336 +	RestoreInitialLocaleSettings() ;
   1.337 +	}
   1.338 +
   1.339 +
   1.340 +
   1.341 +TInt E32Main()
   1.342 +	{
   1.343 +	CTrapCleanup* tc = CTrapCleanup::New();
   1.344 +	TheTest(tc != NULL);
   1.345 +
   1.346 +	__UHEAP_MARK;
   1.347 +
   1.348 +	TheTest.Start(_L("Basic Locale initialisation TheTests"));
   1.349 +	TheTest.Title();
   1.350 +	TRAPD(err, ::TestLocaleChanges())
   1.351 +	TEST2(err, KErrNone);
   1.352 +
   1.353 +	TheTest.End();
   1.354 +	TheTest.Close();
   1.355 +
   1.356 +	__UHEAP_MARKEND;
   1.357 +
   1.358 +	delete tc;
   1.359 +
   1.360 +	return(KErrNone);
   1.361 +	}