os/ossrv/lowlevellibsandfws/apputils/tsrc/t_bafllang.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 // Copyright (c) 2004-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 <f32file.h>
    17 #include <e32test.h>
    18 #include <bautils.h>
    19 #include <hal.h>
    20 #include <e32std.h>
    21 
    22 LOCAL_D CTrapCleanup*		TheTrapCleanup=NULL;
    23 
    24 LOCAL_D RTest				TheTest (_L("T_BaflLang"));
    25 LOCAL_D RFs					TheFs;
    26 
    27 // Our test diectory and files...
    28 
    29 _LIT (KNullFilename, "");
    30 _LIT (KRscFilename,  "C:\\abc.rsc");
    31 _LIT (KRscFilenameNoSuffix,  "C:\\abc.");
    32 
    33 _LIT (KAmericanLang, "C:\\abc.r10");
    34 _LIT (KFrenchLang,   "C:\\abc.r02");
    35 _LIT (KJapanEnglishLang, "C:\\abc.r160");
    36 _LIT (KEnglishLang, "C:\\abc.r01");
    37 
    38 LOCAL_C void DeleteTestFiles()
    39 {
    40 	User::LeaveIfError (TheFs.Connect ());
    41 	::CleanupClosePushL (TheFs);
    42 
    43 	BaflUtils::DeleteFile (TheFs, KRscFilename);
    44 	BaflUtils::DeleteFile (TheFs, KAmericanLang);
    45 	BaflUtils::DeleteFile (TheFs, KFrenchLang);
    46 	BaflUtils::DeleteFile (TheFs, KJapanEnglishLang);
    47 	BaflUtils::DeleteFile (TheFs, KEnglishLang);
    48 
    49 	CleanupStack::PopAndDestroy (&::TheFs);
    50 }
    51 
    52 //
    53 //Test macroses and functions
    54 LOCAL_C void Check(TInt aValue, TInt aLine)
    55 	{
    56 	if(!aValue)
    57 		{
    58 		::DeleteTestFiles();
    59 		TheTest(EFalse, aLine);
    60 		}
    61 	}
    62 
    63 #define TEST(arg) ::Check((arg), __LINE__)
    64 
    65 LOCAL_C void CreateTestFiles()
    66 {
    67 	User::LeaveIfError (TheFs.Connect ());
    68 	::CleanupClosePushL (TheFs);
    69 	TEST (BaflUtils::FileExists (TheFs, KRscFilename) == EFalse);
    70 	TEST (BaflUtils::FileExists (TheFs, KAmericanLang) == EFalse);
    71 	TEST (BaflUtils::FileExists (TheFs, KFrenchLang) == EFalse);
    72 	TEST (BaflUtils::FileExists (TheFs, KJapanEnglishLang) == EFalse);
    73 	TEST (BaflUtils::FileExists (TheFs, KEnglishLang) == EFalse);
    74 
    75 	// Create the files...
    76 	RFile rFile;
    77 	// KFilename1 can't be created
    78 	rFile.Create (TheFs, KRscFilename, EFileRead);
    79 	rFile.Close ();
    80 	rFile.Create (TheFs, KAmericanLang, EFileRead);
    81 	rFile.Close ();
    82 	rFile.Create (TheFs, KFrenchLang, EFileRead);
    83 	rFile.Close ();
    84 
    85 	CleanupStack::PopAndDestroy (&::TheFs);
    86 
    87 }
    88 
    89 /**
    90 @SYMTestCaseID SYSLIB-BAFL-CT-0070
    91 @SYMTestCaseDesc Tests the configuration of the ideal language.
    92 @SYMTestPriority High
    93 @SYMTestActions  Set the ideal language and look for nearest language files.
    94 @SYMTestExpectedResults The test must not fail.
    95 @SYMREQ 3770: Configurable "ideal language" in BaflUtils::NearestLanguageFile.
    96 */
    97 void REQ3770L()
    98 	{
    99     TheTest.Next (_L (" @SYMTestCaseID SYSLIB-BAFL-CT-0070 REQ3770L "));
   100 
   101     __UHEAP_MARK;
   102 
   103 	User::LeaveIfError (TheFs.Connect ());
   104 	::CleanupClosePushL (TheFs);
   105 
   106 	TBuf <256> filename;
   107 	TLanguage language;
   108 
   109 	// Test ideal language code
   110 
   111 	// Check initial value of ideal language
   112 
   113 	language=BaflUtils::IdealLanguage();
   114 	TEST(language == ELangNone);
   115 
   116 	// Set and read back the ideal language
   117 
   118 	BaflUtils::SetIdealLanguage( ELangGerman);
   119 	language=BaflUtils::IdealLanguage();
   120 	TEST(language == ELangGerman);
   121 
   122 	BaflUtils::SetIdealLanguage( ELangAmerican);
   123 	language=BaflUtils::IdealLanguage();
   124 	TEST(language == ELangAmerican);
   125 
   126 	// Test NearestLanguageFile with empty name
   127 	TFileName resPath;
   128 	BaflUtils::NearestLanguageFile (TheFs, resPath);
   129 	TEST(resPath.Length()==0);
   130 	TheTest.Printf (_L ("The NearestLanguageFile for the null file - %S - is - %S -\n"), &KNullFilename, &filename);
   131 
   132 	// Test NearestLanguageFile with no suffix
   133 	filename.Copy (KRscFilenameNoSuffix);
   134 	BaflUtils::NearestLanguageFile (TheFs, filename);
   135 	TEST(filename==KRscFilenameNoSuffix);
   136 	TheTest.Printf (_L ("The NearestLanguageFile for %S is %S\n"), &KRscFilenameNoSuffix, &filename);
   137 
   138 	// Test NearestLanguageFile
   139 	filename.Copy (KRscFilename);
   140 	BaflUtils::NearestLanguageFile (TheFs, filename);
   141 	TheTest.Printf (_L ("The NearestLanguageFile for %S is %S\n"), &KRscFilename, &filename);
   142 	TEST(filename == KAmericanLang);
   143 
   144 	// Set ideal language to one we don't have
   145 	BaflUtils::SetIdealLanguage( ELangGerman);
   146 	language=BaflUtils::IdealLanguage();
   147 	TEST(language == ELangGerman);
   148 
   149 	// Test NearestLanguageFile
   150 	filename.Copy (KRscFilename);
   151 	BaflUtils::NearestLanguageFile (TheFs, filename);
   152 	TheTest.Printf (_L ("The NearestLanguageFile for %S is %S\n"), &KRscFilename, &filename);
   153 	TEST(filename == KRscFilename);
   154 
   155 	// Restore ideal language to default
   156 	BaflUtils::SetIdealLanguage( ELangNone);
   157 	language=BaflUtils::IdealLanguage();
   158 	TEST(language == ELangNone);
   159 
   160 	// Test NearestLanguageFile
   161 	filename.Copy (KRscFilename);
   162 	BaflUtils::NearestLanguageFile (TheFs, filename);
   163 	TheTest.Printf (_L ("The NearestLanguageFile for %S is %S\n"), &KRscFilename, &filename);
   164 	TEST(filename == KRscFilename);
   165 
   166 	// Set ideal language to French
   167 
   168 	BaflUtils::SetIdealLanguage( ELangFrench);
   169 	language=BaflUtils::IdealLanguage();
   170 	TEST(language == ELangFrench);
   171 
   172 	// Test NearestLanguageFile
   173 	filename.Copy (KRscFilename);
   174 	BaflUtils::NearestLanguageFile (TheFs, filename);
   175 	TheTest.Printf (_L ("The NearestLanguageFile for %S is %S\n"), &KRscFilename, &filename);
   176 	TEST(filename == KFrenchLang);
   177 
   178 	// Restore ideal language to default
   179 	BaflUtils::SetIdealLanguage( ELangNone);
   180 	language=BaflUtils::IdealLanguage();
   181 	TEST(language == ELangNone);
   182 
   183 	// Release ideal language - free's TLS
   184 
   185 	BaflUtils::ReleaseIdealLanguage();
   186 
   187 	CleanupStack::PopAndDestroy (&::TheFs);
   188 
   189 	__UHEAP_MARKEND;
   190 	}
   191 
   192 /**
   193 @SYMTestCaseID SYSLIB-BAFL-CT-0071
   194 @SYMTestCaseDesc Tests the overload of BaflUtils::NearestLanguageFile.
   195 @SYMTestPriority High
   196 @SYMTestActions  Calls the overload of BaflUtils::NearestLanguageFile and checks the returned language.
   197 @SYMTestExpectedResults The test must not fail.
   198 @SYMREQ 3785: Overload of BaflUtils::NearestLanguageFile.
   199 */
   200 void REQ3785L()
   201 	{
   202     TheTest.Next (_L (" @SYMTestCaseID SYSLIB-BAFL-CT-0071 REQ3785L "));
   203 
   204     __UHEAP_MARK;
   205 
   206 	User::LeaveIfError (TheFs.Connect ());
   207 	::CleanupClosePushL (TheFs);
   208 
   209 	TBuf <256> filename;
   210 	TLanguage language;
   211 
   212 	// Ensure that ideal language is default
   213 	BaflUtils::SetIdealLanguage( ELangNone);
   214 	language=BaflUtils::IdealLanguage();
   215 	TEST(language == ELangNone);
   216 
   217 	// Test NearestLanguageFile overload with language returned
   218 	language=ELangAmerican;
   219 	filename.Copy (KRscFilename);
   220 	BaflUtils::NearestLanguageFile (TheFs, filename, language);
   221 	TheTest.Printf (_L ("The NearestLanguageFile for %S is %S, language = %d\n"), &KRscFilename, &filename, language);
   222 	TEST(language == ELangNone);
   223 	TEST(filename == KRscFilename);
   224 
   225 	// Set ideal language to American
   226 
   227 	BaflUtils::SetIdealLanguage( ELangAmerican);
   228 	language=BaflUtils::IdealLanguage();
   229 	TEST(language == ELangAmerican);
   230 
   231 	// Test NearestLanguageFile overload with language returned
   232 	language=ELangNone;
   233 	filename.Copy (KRscFilename);
   234 	BaflUtils::NearestLanguageFile (TheFs, filename, language);
   235 	TheTest.Printf (_L ("The NearestLanguageFile for %S is %S, language = %d\n"), &KRscFilename, &filename, language);
   236 	TEST(language == ELangAmerican);
   237 	TEST(filename == KAmericanLang);
   238 
   239 	// Set ideal language to French
   240 
   241 	BaflUtils::SetIdealLanguage( ELangFrench);
   242 	language=BaflUtils::IdealLanguage();
   243 	TEST(language == ELangFrench);
   244 
   245 	// Test NearestLanguageFile overload with language returned
   246 	language=ELangNone;
   247 	filename.Copy (KRscFilename);
   248 	BaflUtils::NearestLanguageFile (TheFs, filename, language);
   249 	TheTest.Printf (_L ("The NearestLanguageFile for %S is %S, language = %d\n"), &KRscFilename, &filename, language);
   250 	TEST(language == ELangFrench);
   251 	TEST(filename == KFrenchLang);
   252 
   253 	// Set ideal language to one we don't have
   254 	BaflUtils::SetIdealLanguage( ELangGerman);
   255 	language=BaflUtils::IdealLanguage();
   256 	TEST(language == ELangGerman);
   257 
   258 	// Test NearestLanguageFile overload with language returned
   259 	language=ELangAmerican;
   260 	filename.Copy (KRscFilename);
   261 	BaflUtils::NearestLanguageFile (TheFs, filename, language);
   262 	TheTest.Printf (_L ("The NearestLanguageFile for %S is %S, language = %d\n"), &KRscFilename, &filename, language);
   263 	TEST(language == ELangNone);
   264 	TEST(filename == KRscFilename);
   265 
   266 	// Restore ideal language to default
   267 	BaflUtils::SetIdealLanguage( ELangNone);
   268 	language=BaflUtils::IdealLanguage();
   269 	TEST(language == ELangNone);
   270 
   271 	// Release ideal language - free's TLS
   272 
   273 	BaflUtils::ReleaseIdealLanguage();
   274 
   275 	CleanupStack::PopAndDestroy (&::TheFs);
   276 
   277 	__UHEAP_MARKEND;
   278 	}
   279 
   280 /**
   281 @SYMTestCaseID SYSLIB-BAFL-CT-3158
   282 @SYMTestCaseDesc Test the downgrade path from ELangMalay_Apac to ELangMalay
   283 @SYMTestPriority High
   284 @SYMTestActions Calls the BaflUtils::GetDowngradePathL function to check that ELangMalay_Apac downgrades to ELangMalay
   285 @SYMTestExpectedResults The test must not fail.
   286 @SYMDEF DEF094490
   287 */
   288 void DEF094490L()
   289 	{
   290     TheTest.Next (_L (" @SYMTestCaseID SYSLIB-BAFL-CT-3158 DEF094490L "));
   291 
   292 	__UHEAP_MARK;
   293 
   294 	User::LeaveIfError (TheFs.Connect ());
   295 	::CleanupClosePushL (TheFs);
   296 	RArray<TLanguage> aLanguageArray;
   297 
   298 	//Test GetDowngradePathL to see if ELangMalay is included in the path
   299 	BaflUtils::GetDowngradePathL(TheFs,ELangMalay_Apac,aLanguageArray);
   300 	TEST(aLanguageArray.Find(ELangMalay)!= KErrNotFound);
   301 
   302 	aLanguageArray.Close();
   303 
   304 	CleanupStack::PopAndDestroy (&::TheFs);
   305 
   306 	__UHEAP_MARKEND;
   307 	}
   308 
   309 /**
   310 @SYMTestCaseID SYSLIB-BAFL-CT-3359
   311 @SYMTestCaseDesc Test correct and incorrect usage of NearestLanguageFile as documented in the developer library
   312 @SYMTestPriority High
   313 @SYMTestActions Calls the BaflUtils::NearestLanguageFile function with different contructed
   314 language downgrade path cases along with different input paths
   315 @SYMTestExpectedResults The test must not fail.
   316 @SYMDEF DEF096421
   317 */
   318 void DEF096421L()
   319 	{
   320     TheTest.Next (_L (" @SYMTestCaseID SYSLIB-BAFL-CT-3359 DEF096421 "));
   321 	__UHEAP_MARK;
   322 
   323 	TBuf <256> path;
   324 	TLanguage language;
   325 	RFile rFile;
   326 
   327 	//number of characters in the suffix is less than KInvNameAndMinSuffixLength(2)
   328 	_LIT (KInvalidSuffix1, "C:\\abc.r");
   329 
   330 	//number of digits in the suffix is more than KMaxSuffixLength(5)
   331 	_LIT (KInvalidSuffix2, "C:\\abc.r123456");
   332 
   333 	//no proceeding alphabetical characters in the suffix
   334 	_LIT (KInvalidSuffix3, "C:\\abc.10");
   335 
   336 	//no drive letters supplied in the path
   337 	_LIT (KRscFilenameNoDrive, "\\abc.rsc");
   338 
   339 	_LIT (KRscFileInZDrive, "\\system\\data\\T_CALYPSO_TEST_RESOURCE_FILE_1.RSC");
   340 	_LIT (KRScFullFile, "Z:\\system\\data\\T_CALYPSO_TEST_RESOURCE_FILE_1.RSC");
   341 
   342 	User::LeaveIfError (TheFs.Connect ());
   343 	::CleanupClosePushL (TheFs);
   344 
   345 	rFile.Create (TheFs, KJapanEnglishLang, EFileRead);
   346 	rFile.Close ();
   347 	rFile.Create (TheFs, KEnglishLang, EFileRead);
   348 	rFile.Close ();
   349 
   350 	BaflUtils::SetIdealLanguage( ELangAmerican);
   351 	language=BaflUtils::IdealLanguage();
   352 	TEST(language == ELangAmerican);
   353 
   354 	//Test match for the ideal language
   355 	path.Copy(KRscFilename);
   356 	BaflUtils::NearestLanguageFile (TheFs, path, language);
   357 	TEST(path == KAmericanLang);
   358 	TEST(language == ELangAmerican);
   359 	TheTest.Printf (_L ("The NearestLanguageFile for %S is %S\n"), &KRscFilename, &path);
   360 
   361 	//Test match when supplied with the language file
   362 	path.Copy(KAmericanLang);
   363 	BaflUtils::NearestLanguageFile (TheFs, path, language);
   364 	TEST(path == KAmericanLang);
   365 	TEST(language == ELangAmerican);
   366 	TheTest.Printf (_L ("The NearestLanguageFile for %S is %S\n"), &KAmericanLang, &path);
   367 
   368 	//Test when the no. of characters in the suffix is less than KInvNameAndMinSuffixLength(2)
   369 	path.Copy(KInvalidSuffix1);
   370 	BaflUtils::NearestLanguageFile (TheFs, path, language);
   371 	TEST(path == KInvalidSuffix1);
   372 	TEST(language == ELangNone);
   373 	TheTest.Printf (_L ("The NearestLanguageFile for %S is %S\n"), &KInvalidSuffix1, &path);
   374 
   375 	//Test when filename ends with a period
   376 	path.Copy(KRscFilenameNoSuffix);
   377 	BaflUtils::NearestLanguageFile (TheFs, path, language);
   378 	TEST(path == KRscFilenameNoSuffix);
   379 	TEST(language == ELangNone);
   380 	TheTest.Printf (_L ("The NearestLanguageFile for %S is %S\n"), &KRscFilenameNoSuffix, &path);
   381 
   382 	//Test when the no. of digits in the suffix is more than KMaxSuffixLength(5)
   383 	path.Copy(KInvalidSuffix2);
   384 	BaflUtils::NearestLanguageFile (TheFs, path, language);
   385 	TEST(path == KInvalidSuffix2);
   386 	TEST(language == ELangNone);
   387 	TheTest.Printf (_L ("The NearestLanguageFile for %S is %S\n"), &KInvalidSuffix2, &path);
   388 
   389 	//Test when theres no proceeding alphabetical characters in the suffix
   390 	path.Copy(KInvalidSuffix3);
   391 	BaflUtils::NearestLanguageFile (TheFs, path, language);
   392 	TEST(path == KInvalidSuffix3);
   393 	TEST(language == ELangNone);
   394 	TheTest.Printf (_L ("The NearestLanguageFile for %S is %S\n"), &KInvalidSuffix3, &path);
   395 
   396 	//Test when no drive letter is supplied in the path and theres no corresponding file in Z:
   397 	path.Copy(KRscFilenameNoDrive);
   398 	BaflUtils::NearestLanguageFile (TheFs, path, language);
   399 	TEST(path == KRscFilenameNoDrive);
   400 	TEST(language == ELangNone);
   401 	TheTest.Printf (_L ("The NearestLanguageFile for %S is %S\n"), &KRscFilenameNoDrive, &path);
   402 
   403 	BaflUtils::SetIdealLanguage(ELangEnglish_Japan);
   404 	TheTest.Printf(_L("The ideal langauge set to ELangEnglish_Japan\n"));
   405 
   406 	//Test when the matching language file is 3 digits long
   407 	path.Copy(KRscFilename);
   408 	BaflUtils::NearestLanguageFile (TheFs, path, language);
   409 	TEST(path == KJapanEnglishLang);
   410 	TEST(language == ELangEnglish_Japan);
   411 	TheTest.Printf (_L ("The NearestLanguageFile for %S is %S\n"), &KRscFilename, &path);
   412 
   413 	BaflUtils::DeleteFile (TheFs, KJapanEnglishLang);
   414 	TheTest.Printf(_L("Deleting C:\\abc.r160\n"));
   415 
   416 	//Test match for the current locale (2nd best match)
   417 	path.Copy(KRscFilename);
   418 	BaflUtils::NearestLanguageFile (TheFs, path, language);
   419 	TEST(path == KEnglishLang);
   420 	TEST(language == ELangEnglish);
   421 	TheTest.Printf (_L ("The NearestLanguageFile for %S is %S\n"), &KRscFilename, &path);
   422 
   423 	BaflUtils::DeleteFile (TheFs, KEnglishLang);
   424 	TheTest.Printf(_L("Deleting C:\\abc.r01\n"));
   425 
   426 	//Test when theres no matching language file
   427 	path.Copy(KRscFilename);
   428 	BaflUtils::NearestLanguageFile (TheFs, path, language);
   429 	TEST(path == KRscFilename);
   430 	TEST(language == ELangNone);
   431 	TheTest.Printf (_L ("The NearestLanguageFile for %S is %S\n"), &KRscFilename, &path);
   432 
   433 	//Test when no drive letter is supplied in the
   434 	//path but theres a corresponding file in Z:
   435 	path.Copy(KRscFileInZDrive);
   436 	BaflUtils::NearestLanguageFile (TheFs, path, language);
   437 	TEST(path == KRScFullFile);
   438 	TEST(language == ELangNone);
   439 	TheTest.Printf (_L ("The NearestLanguageFile for %S is %S\n"), &KRscFileInZDrive, &path);
   440 
   441 	CleanupStack::PopAndDestroy (&::TheFs);
   442 
   443 	BaflUtils::ReleaseIdealLanguage();
   444 	__UHEAP_MARKEND;
   445 
   446 	}
   447 //===============================================================================
   448 
   449 /**
   450 Initialise the cleanup stack
   451 */
   452 LOCAL_C void SetupL ()
   453     {
   454 	TheTrapCleanup = CTrapCleanup::New ();
   455 	User::LeaveIfNull (TheTrapCleanup);
   456 	}
   457 
   458 /**
   459 Cleanup
   460 */
   461 LOCAL_C void CleanupL ()
   462     {
   463 	delete TheTrapCleanup;
   464 	}
   465 
   466 /**
   467 Invoke the tests
   468 */
   469 LOCAL_C void DoTestsL ()
   470     {
   471     CreateTestFiles();
   472 	REQ3770L ();
   473 	REQ3785L ();
   474 	DEF094490L();
   475 	DEF096421L();
   476 	DeleteTestFiles();
   477 	}
   478 
   479 
   480 GLDEF_C TInt E32Main ()
   481 	{
   482 	__UHEAP_MARK;
   483 
   484 	TheTest.Printf (_L ("\n"));
   485 	TheTest.Title ();
   486 	TheTest.Start (_L("Ideal language Tests "));
   487 
   488 	TRAPD (err, SetupL ());
   489 	TheTest (err == KErrNone);
   490 
   491 	TRAP (err, DoTestsL ());
   492 	TheTest (err == KErrNone);
   493 
   494 	CleanupL ();
   495 
   496 	TheTest.End ();
   497 	TheTest.Close ();
   498 
   499 	__UHEAP_MARKEND;
   500 	return (KErrNone);
   501 	}