os/ossrv/lowlevellibsandfws/apputils/initLocale/test/t_initialiselocale.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2005-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 //
    15 
    16 #include <bautils.h>
    17 #include <f32file.h>
    18 #include <e32cmn.h>
    19 #include <e32test.h>
    20 #include <e32std.h>
    21 #include <e32const.h>
    22 #include <babackup.h>
    23 #include <hal.h>
    24 #include <centralrepository.h>
    25 #include "LocaleRepository.h"
    26 #include "InitialiseLocale.h"
    27 
    28 
    29 _LIT(KInitialiseLocaleExeName, "InitialiseLocale.exe");
    30 
    31 // Symbian VendorID as specified in 
    32 // "Platform security engineering guide" Section 2.1.3.2 
    33 const TVendorId KSymbianVendorId(0x70000001);
    34 
    35 RTest TheTest(_L("T_Initialise_Locale"));
    36 
    37 // const TInt KDefaultCountryCode = 29 ;
    38 const TInt KNewCountryCode = 46 ;
    39 TInt defaultCountryCode ;
    40 
    41 TCurrencySymbol defaultCurrencySymbol;
    42 
    43 _LIT(KCurrencySymbol, "ABC") ;
    44 TBuf<8> KNewCurrencySymbol (KCurrencySymbol) ;
    45 
    46 _LIT(KLongDateFormat, "%F%*D%X %N %Y") ;
    47 TBuf<20> KDefaultLongDateFormat (KLongDateFormat) ;
    48 
    49 _LIT(KShortDateFormat, "%F%*D/%*M/%Y") ;
    50 TBuf<20> KDefaultShortDateFormat (KShortDateFormat) ;
    51 
    52 _LIT(KTimeFormat, "%F%*I:%T:%S %*A") ;
    53 TBuf<20> KDefaultTimeFormat (KTimeFormat) ;
    54 
    55 TExtendedLocale initialExtendedLocale;
    56 
    57 void GetInitialLocaleSettings()
    58 	{
    59 	initialExtendedLocale.LoadSystemSettings() ;
    60 	}
    61 
    62 void RestoreInitialLocaleSettings()
    63 	{
    64 	initialExtendedLocale.SaveSystemSettings() ;
    65 	User::After(1000000);
    66 	}
    67 
    68 //Test macros and functions
    69 
    70 LOCAL_C void Check(TInt aValue, TInt aLine)
    71 	{
    72 	if(!aValue)
    73 		{
    74 		RestoreInitialLocaleSettings();
    75 		TheTest(EFalse, aLine);
    76 		}
    77 	}
    78 
    79 LOCAL_C void Check(TInt aValue, TInt aExpected, TInt aLine)
    80 	{
    81 	if(aValue != aExpected)
    82 		{
    83 		RDebug::Print(_L("*** Expected error: %d, got: %d\r\n"), aExpected, aValue);
    84 		RestoreInitialLocaleSettings();
    85 		TheTest(EFalse, aLine);
    86 		}
    87 	}
    88 
    89 LOCAL_C void Check(TPtrC aValue, TPtrC aExpected, TInt aLine)
    90 	{
    91 	if(aValue != aExpected)
    92 		{
    93 		RDebug::Print(_L("*** Expected error: %S, got: %S\r\n"), &aExpected, &aValue);
    94 		RestoreInitialLocaleSettings();
    95 		TheTest(EFalse, aLine);
    96 		}
    97 	}
    98 
    99 #define TEST(arg) ::Check((arg), __LINE__)
   100 #define TEST2(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
   101 #define TEST3(aValue, aExpected) ::Check(aValue, aExpected, __LINE__)
   102 
   103 /**
   104 @SYMTestCaseID		SYSLIB-BAFL-CIT-0200
   105 @SYMTestCaseDesc 	Check that default locale values are loaded on startup.
   106 @SYMTestPriority 	High
   107 @SYMTestActions  	Ensure that country code in system locale data matches value in persisted data
   108 @SYMTestExpectedResults The test must not fail.
   109 @SYMREQ PREQ1003
   110 */
   111 void CheckDefaultLocaleCountryCode()
   112 	{
   113 	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-BAFL-CIT-0200 Check default country code "));
   114 
   115 	TExtendedLocale myExtendedLocale;
   116 	myExtendedLocale.LoadSystemSettings() ;
   117 	TLocale* myLocale = myExtendedLocale.GetLocale() ;
   118 	TInt countryCode = myLocale->CountryCode();
   119 	TheTest.Printf(_L("Default Country code is %d\n"), defaultCountryCode) ;
   120 	TheTest.Printf(_L("Current Country code is %d\n"), countryCode) ;
   121 
   122 	TEST2(countryCode, defaultCountryCode) ;
   123 	TheTest.End();
   124 	}
   125 
   126 /**
   127 @SYMTestCaseID		SYSLIB-BAFL-CT-3160
   128 @SYMTestCaseDesc 	Check that default locale values i.e. Currency Symbol and long date, short date & time format.
   129 @SYMTestPriority 	High
   130 @SYMTestActions  	Ensure that these values are set to the default values as defined.
   131 @SYMTestExpectedResults The test must not fail.
   132 @SYMREQ PREQ1003
   133 */
   134 void CheckDefaultLocaleValues()
   135 {
   136 	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-BAFL-CT-3160 Check default Locale Values "));
   137 	TExtendedLocale myExtendedLocale;
   138 	myExtendedLocale.LoadSystemSettings() ;
   139 
   140 	TPtrC currencySymbolPtr = myExtendedLocale.GetCurrencySymbol();
   141 	TPtrC longDateFormatPtr = myExtendedLocale.GetLongDateFormatSpec();
   142 	TPtrC shortDateFormatPtr = myExtendedLocale.GetShortDateFormatSpec();
   143 	TPtrC timeFormatPtr = myExtendedLocale.GetTimeFormatSpec();
   144 
   145 	TheTest.Printf(_L("New Currency Symbol is %S \n"), &currencySymbolPtr);
   146 	TheTest.Printf(_L("Default Long Date Format is %S \n"), &longDateFormatPtr);
   147 	TheTest.Printf(_L("Default Short Date Format is %S \n"), &shortDateFormatPtr);
   148 	TheTest.Printf(_L("Default Time Format is %S \n"), &timeFormatPtr);
   149 
   150 	TPtrC expectedCurrSymPtr(KNewCurrencySymbol);
   151 	TEST3(currencySymbolPtr, expectedCurrSymPtr);
   152 
   153 	TPtrC expectedlongDatePtr(KDefaultLongDateFormat);
   154 	TEST3(longDateFormatPtr, expectedlongDatePtr);
   155 
   156 	TPtrC expectedShortDatePtr(KDefaultShortDateFormat);
   157 	TEST3(shortDateFormatPtr, expectedShortDatePtr);
   158 
   159 	TPtrC expectedTimePtr(KDefaultTimeFormat);
   160 	TEST3(timeFormatPtr, expectedTimePtr);
   161 
   162 	TheTest.End();
   163 }
   164 
   165 /**
   166 @SYMTestCaseID		SYSLIB-BAFL-CIT-0201
   167 @SYMTestCaseDesc 	Check that locale settings can be saved to and read from system locale data through TExtendedLocale.
   168 @SYMTestPriority 	High
   169 @SYMTestActions  	Change a number of locale values, write to system data, read back from data, check result
   170 @SYMTestExpectedResults The test must not fail.
   171 @SYMREQ PREQ1003
   172 */
   173 void ChangeLocaleCountryCode()
   174 	{
   175 	TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-BAFL-CIT-0201 Define a TLocale "));
   176 	TheTest.Next(_L("Setting CountryCode"));
   177 
   178 	TExtendedLocale myExtendedLocale;
   179 	myExtendedLocale.LoadSystemSettings() ;
   180 	TLocale* myLocale = myExtendedLocale.GetLocale() ;
   181 	TheTest.Printf(_L("Setting Country code to %d\n"), KNewCountryCode) ;
   182 	myLocale->SetCountryCode(KNewCountryCode);
   183 	TheTest.Printf(_L("Saving new settings to system locale data\n")) ;
   184 	myExtendedLocale.SaveSystemSettings() ;
   185 	TheTest.Printf(_L("Loading new settings from system locale data\n")) ;
   186 	myExtendedLocale.LoadSystemSettings() ;
   187 	myLocale = myExtendedLocale.GetLocale() ;
   188 	TInt countryCode = myLocale->CountryCode() ;
   189 	TheTest.Printf(_L("New Country code is %d\n"), countryCode) ;
   190 	TEST2(countryCode, KNewCountryCode) ;
   191 	TheTest.End();
   192 	}
   193 
   194 
   195 /**
   196 @SYMTestCaseID      SYSLIB-BAFL-CIT-188
   197 @SYMTestCaseDesc    Check that locale settings are saved into central repository.
   198 @SYMTestPriority    High
   199 @SYMTestActions     Ensure that locale data stored in the central repository match the data in the system locale
   200 @SYMTestExpectedResults The test must not fail.
   201 @SYMREQ PREQ1003
   202 */
   203 void CheckRepositoryLocaleCountryCode(CRepository *repository)
   204    {
   205    TheTest.Start(_L(" @SYMTestCaseID:SYSLIB-BAFL-CIT-188 Checking persisted country code "));
   206    TInt datacageCountryCode;
   207    TheTest.Printf(_L("Getting repository data cage\n")) ;
   208    TInt err = repository->Get(KLocaleCountryCode, datacageCountryCode) ;
   209    TEST2(err, KErrNone);
   210    TheTest.Printf(_L("Country code from repository = %d\n"), datacageCountryCode) ;
   211    TEST2(datacageCountryCode, KNewCountryCode) ;
   212    TheTest.End();
   213    }
   214 
   215 /**
   216 @SYMTestCaseID  			SYSLIB-BAFL-CT-4051
   217 @SYMTestCaseDesc  			Check that the VendorID is set to 0x70000001 (Symbian's VendorID)
   218 @SYMTestPriority   			Medium
   219 @SYMTestActions 			1. Create a process for InitialiseLocale.exe
   220 							2. Compare the processes VID against the expected VID (0x70000001)
   221 @SYMTestExpectedResults  	The VIDs should be identical and so the test will pass.
   222 @SYMDEF 					INC121403
   223 */
   224 void TestVendorIdINC121403(RProcess& aProcess)
   225 	{
   226 	TheTest.Next (_L ("TestVendorIDINC121403"));
   227 	
   228 	// Test that the VendorID of InitialiseLocale.exe is set to the Symbian VendorID
   229 	TEST2(KSymbianVendorId.iId, aProcess.VendorId().iId);
   230 	}
   231 
   232 /**
   233 Sets up the Currency Symbol test. Outputs default values then sets the Currency Symbol
   234 to a test value ("ABC") and stores this value in the repository, before InitialiseLocale has run.
   235 */
   236 void ChangeLocaleCurrencySymbol(CRepository *repository, TCurrencySymbol & defaultCurrencySymbol)
   237 	{
   238 	TheTest.Start(_L("Setting CurrencySymbol\n"));
   239 
   240 	TExtendedLocale myExtendedLocale;
   241 	myExtendedLocale.LoadSystemSettings() ;
   242 
   243 	TheTest.Printf(_L("Default Currency Symbol is:  %S\n"), &defaultCurrencySymbol);
   244 
   245 	TCurrencySymbol datacageCurrencySymbol;
   246 	TInt err = repository->Get(KLocaleCurrencySymbol, datacageCurrencySymbol) ;
   247 	TEST2(err, KErrNone);
   248 	TheTest.Printf(_L("Currency Symbol from repository data cage is:  %S\n"), &datacageCurrencySymbol);
   249 
   250 	TheTest.Printf(_L("Setting Currency Symbol to:  %S\n"), &KNewCurrencySymbol);
   251 
   252 	TheTest.Printf(_L("Saving Currency Symbol to repository.\n\n"));
   253 	err = repository->Set(KLocaleCurrencySymbol, KNewCurrencySymbol);
   254 	TEST2(err, KErrNone);
   255 	User::SetCurrencySymbol(KNewCurrencySymbol);
   256 
   257 	TheTest.End();
   258 	}
   259 
   260 /**
   261 After InitialiseLocale has run. Gets the Currency Symbol back from the repository and then
   262 outputs it. This output should be the same value as the test value ("ABC") that was set to the
   263 repository in the above function.
   264 */
   265 void TestLocaleCurrencySymbol(CRepository *repository)
   266 	{
   267 	TheTest.Start(_L("Checking value in the repository after InitialiseLocale() has run.\n"));
   268 
   269 	TCurrencySymbol reposCurrencySymbol;
   270 	TInt err = repository->Get(KLocaleCurrencySymbol, reposCurrencySymbol);
   271 	TEST2(err, KErrNone);
   272 
   273 	TheTest.Printf(_L("The Currency Symbol after InitialiseLocale() has ran is: %S\n\n"), &reposCurrencySymbol);
   274 
   275 	TEST(KNewCurrencySymbol == reposCurrencySymbol);
   276 
   277 	TheTest.End();
   278 	}
   279 
   280 
   281 
   282 //
   283 // Test new locale initialisation and persistence mechanism
   284 void TestLocaleChanges()
   285 	{
   286 	RProcess process;
   287 
   288 	GetInitialLocaleSettings() ;
   289 
   290 	// Restore default contents of locale settings repository
   291 	TUid LocaleRepositoryUid ;
   292 	LocaleRepositoryUid.iUid = KLocalePersistRepositoryUid ;
   293 	CRepository* repository = CRepository::NewLC(LocaleRepositoryUid) ;
   294 	repository->Reset();
   295 
   296 	TInt err = repository->Get(KLocaleCountryCode, defaultCountryCode) ;
   297 	TEST2(err, KErrNone);
   298 	ChangeLocaleCurrencySymbol(repository, defaultCurrencySymbol);
   299 
   300 	// Initialise locale data and start persistence notifier by spawning
   301 	// process - this will be done as part of system start in actual
   302 	// release code!
   303 	TInt r = process.Create(KInitialiseLocaleExeName, KNullDesC);
   304 	if(r == KErrNone)
   305 		{
   306 		TRequestStatus stat;
   307 		process.Rendezvous(stat);
   308 		process.Resume(); // Start the process going
   309 		//wait for the locale initialisation to complete first before testing
   310 		User::WaitForRequest(stat);
   311 		TEST2((stat.Int()==KErrNone)||(stat.Int()==KErrAlreadyExists),ETrue);
   312 		TInt flag;
   313 		TUid initLocaleUid=TUid::Uid(KUidLocalePersistProperties);
   314 		r=RProperty::Get(initLocaleUid,(TUint)EInitialisationState,flag);
   315 		TEST2(r,KErrNone);
   316 		//check for the P&S flag that indicates localeinitialisation complete
   317 		TEST2(flag,ELocaleInitialisationComplete);
   318 		TestLocaleCurrencySymbol(repository);
   319 		User::After(1000000);
   320 		CheckDefaultLocaleCountryCode();
   321 		User::After(1000000);
   322 		CheckDefaultLocaleValues();
   323 		User::After(1000000);
   324 		ChangeLocaleCountryCode();
   325 		User::After(1000000);
   326 		CheckRepositoryLocaleCountryCode(repository);
   327 		User::After(1000000);
   328 		TestVendorIdINC121403(process);
   329 		process.Close();
   330 		User::After(1000000);
   331 		}
   332 	CleanupStack::PopAndDestroy(repository) ;
   333 	RestoreInitialLocaleSettings() ;
   334 	}
   335 
   336 
   337 
   338 TInt E32Main()
   339 	{
   340 	CTrapCleanup* tc = CTrapCleanup::New();
   341 	TheTest(tc != NULL);
   342 
   343 	__UHEAP_MARK;
   344 
   345 	TheTest.Start(_L("Basic Locale initialisation TheTests"));
   346 	TheTest.Title();
   347 	TRAPD(err, ::TestLocaleChanges())
   348 	TEST2(err, KErrNone);
   349 
   350 	TheTest.End();
   351 	TheTest.Close();
   352 
   353 	__UHEAP_MARKEND;
   354 
   355 	delete tc;
   356 
   357 	return(KErrNone);
   358 	}