First public contribution.
1 // Copyright (c) 1997-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 the License "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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // f32test\server\t_dlocl.cpp
15 // Tests UserSvr::ChangeLocale() function
19 #define __E32TEST_EXTENSION__
22 #include <e32std_private.h>
30 LOCAL_D TFindLibrary fLib;
32 typedef TInt (*TLibEntry)(TInt);
33 typedef TInt (*TLibEntry2)();
35 const TBool KEY_DOWN=EFalse;
36 const TBool EXPECT_KEY_PRESS=ETrue;
38 RTest test(_L("T_DLOCL"));
40 _LIT(ELOCL_DEFAULT, "");
41 _LIT(ELOCLGE, "T_LOCLGE");
42 _LIT(ELOCLUS, "T_LOCLUS");
43 _LIT(ELOCLUS1, "T_LOCLUS1");
44 _LIT(ELOCLUS2, "T_LOCLUS2");
45 _LIT(EKDATA, "EKDATA");
46 _LIT(DUMMYDLL, "EDISP");
47 _LIT(KColonColon, "::");
48 _LIT(KDLLExtension, ".DLL");
50 GLDEF_D CKeyTranslator *KeyTranslator=CKeyTranslator::New();
51 LOCAL_D CCaptureKeys *CaptureKeys;
53 void testConv(const TDesC& aDes,TBool aKeyup,TBool aRet,TUint aScanCode,TUint aKeyCode,TInt aModifiers)
57 TBool ret=KeyTranslator->TranslateKey(aScanCode, aKeyup,*CaptureKeys,keyData);
60 test((keyData.iKeyCode==aKeyCode));
61 test((keyData.iModifiers==aModifiers));
64 void testChangeKeyData()
66 TName pn(RProcess().Name());
69 CaptureKeys=new CCaptureKeys();
70 CaptureKeys->Construct();
71 test.Printf(_L("Test default key data\n"));
72 testConv(_L("\nFirst Special key down"),KEY_DOWN,EXPECT_KEY_PRESS,ESpecialKeyBase,ESpecialKeyBase,0);
75 TInt res=testLib.Load(EKDATA);
77 THandleInfo handleInfo;
78 testLib.HandleInfo(&handleInfo);
79 test(handleInfo.iNumOpenInThread==2);
82 test.Printf(_L("Change to unknown dll \n")); // Test with non keydata type dll
83 res=KeyTranslator->ChangeKeyData(DUMMYDLL);
84 test(res==KErrArgument);
86 res=testLib.Load(EKDATA);
88 testLib.HandleInfo(&handleInfo);
89 test(handleInfo.iNumOpenInThread==2);
92 res=testLib.Load(DUMMYDLL);
94 testLib.HandleInfo(&handleInfo);
95 test(handleInfo.iNumOpenInThread==1);
99 while (fLib.Next(n)==KErrNone)
103 test.Printf(_L(" %S\n"),&n);
104 test(n.FindF(findname) == KErrNotFound);
107 test.Printf(_L("Change to EKDATA.dll\n"));
108 res=KeyTranslator->ChangeKeyData(EKDATA);
111 res=testLib.Load(EKDATA);
113 testLib.HandleInfo(&handleInfo);
114 test(handleInfo.iNumOpenInThread==2);
116 res=testLib.Load(DUMMYDLL);
118 testLib.HandleInfo(&handleInfo);
119 test(handleInfo.iNumOpenInThread==1);
123 while (fLib.Next(n)==KErrNone)
127 test.Printf(_L(" %S\n"),&n);
128 if(n.FindF(findname) != KErrNotFound)
131 //The test below fails if we use Secure APIs
132 //test(n.FindF(EKDATA) == KErrNotSupported);
133 testConv(_L("\nFirst Special key down"),KEY_DOWN,EXPECT_KEY_PRESS,ESpecialKeyBase,ESpecialKeyBase,0);
135 test.Printf(_L("Change back to Default KeyData\n"));
136 res=KeyTranslator->ChangeKeyData(_L(""));
139 res=testLib.Load(EKDATA);
141 testLib.HandleInfo(&handleInfo);
142 test(handleInfo.iNumOpenInThread==2);
145 res=testLib.Load(DUMMYDLL);
147 testLib.HandleInfo(&handleInfo);
148 test(handleInfo.iNumOpenInThread==1);
152 while (fLib.Next(n)==KErrNone)
156 test.Printf(_L(" %S\n"),&n);
157 test(n.FindF(findname) == KErrNotFound);
159 testConv(_L("\nFirst Special key down"),KEY_DOWN,EXPECT_KEY_PRESS,ESpecialKeyBase,ESpecialKeyBase,0);
163 void testUS(const TLocale& aLocale)
166 test(aLocale.CountryCode()==1);
167 test(aLocale.DateFormat()==EDateAmerican);
168 test(aLocale.TimeFormat()==ETime12);
169 test(aLocale.CurrencySymbolPosition()==ELocaleBefore);
170 test(aLocale.CurrencySpaceBetween()==FALSE);
171 test(aLocale.CurrencyDecimalPlaces()==2);
172 test(aLocale.CurrencyNegativeInBrackets()==EFalse);
173 test(aLocale.CurrencyTriadsAllowed()==TRUE);
174 test(aLocale.ThousandsSeparator()==',');
175 test(aLocale.DecimalSeparator()=='.');
176 test(aLocale.DateSeparator(0)==0);
177 test(aLocale.DateSeparator(1)=='/');
178 test(aLocale.DateSeparator(2)=='/');
179 test(aLocale.DateSeparator(3)==0);
180 test(aLocale.TimeSeparator(0)==0);
181 test(aLocale.TimeSeparator(1)==':');
182 test(aLocale.TimeSeparator(2)==':');
183 test(aLocale.TimeSeparator(3)==0);
184 test(aLocale.AmPmSymbolPosition()==TRUE);
185 test(aLocale.AmPmSpaceBetween()==TRUE);
186 test(aLocale.HomeDaylightSavingZone()==EDstNorthern);
187 test(aLocale.WorkDays()==0x1f);
188 test(aLocale.StartOfWeek()==ESunday);
189 test(aLocale.ClockFormat()==EClockAnalog);
190 test(aLocale.UnitsGeneral()==EUnitsImperial);
191 test(aLocale.UnitsDistanceShort()==EUnitsImperial);
192 test(aLocale.UnitsDistanceLong()==EUnitsImperial);
197 void testUK(const TLocale& aLocale)
200 test(aLocale.CountryCode()==44);
201 test(aLocale.DateFormat()==EDateEuropean);
202 test(aLocale.TimeFormat()==ETime12);
203 test(aLocale.CurrencySymbolPosition()==ELocaleBefore);
204 test(aLocale.CurrencySpaceBetween()==FALSE);
205 test(aLocale.CurrencyDecimalPlaces()==2);
206 test(aLocale.CurrencyNegativeInBrackets()==EFalse);
207 test(aLocale.CurrencyTriadsAllowed()==TRUE);
208 test(aLocale.ThousandsSeparator()==',');
209 test(aLocale.DecimalSeparator()=='.');
210 test(aLocale.DateSeparator(0)==0);
211 test(aLocale.DateSeparator(1)=='/');
212 test(aLocale.DateSeparator(2)=='/');
213 test(aLocale.DateSeparator(3)==0);
214 test(aLocale.TimeSeparator(0)==0);
215 test(aLocale.TimeSeparator(1)==':');
216 test(aLocale.TimeSeparator(2)==':');
217 test(aLocale.TimeSeparator(3)==0);
218 test(aLocale.AmPmSymbolPosition()==TRUE);
219 test(aLocale.AmPmSpaceBetween()==TRUE);
220 test(aLocale.HomeDaylightSavingZone()==EDstEuropean);
221 test(aLocale.WorkDays()==0x1f);
222 test(aLocale.StartOfWeek()==EMonday);
223 test(aLocale.ClockFormat()==EClockAnalog);
224 test(aLocale.UnitsGeneral()==EUnitsImperial);
225 test(aLocale.UnitsDistanceShort()==EUnitsImperial);
226 test(aLocale.UnitsDistanceLong()==EUnitsImperial);
230 void testGE(const TLocale& aLocale)
233 test(aLocale.CountryCode()==49);
234 test(aLocale.DateFormat()==EDateEuropean);
235 test(aLocale.TimeFormat()==ETime24);
236 test(aLocale.CurrencySymbolPosition()==ELocaleAfter);
237 test(aLocale.CurrencySpaceBetween()==TRUE);
238 test(aLocale.CurrencyDecimalPlaces()==2);
239 test(aLocale.CurrencyNegativeInBrackets()==TRUE);
240 test(aLocale.CurrencyTriadsAllowed()==TRUE);
241 test(aLocale.ThousandsSeparator()=='.');
242 test(aLocale.DecimalSeparator()==',');
243 test(aLocale.DateSeparator(0)==0);
244 test(aLocale.DateSeparator(1)=='.');
245 test(aLocale.DateSeparator(2)=='.');
246 test(aLocale.DateSeparator(3)==0);
248 test(aLocale.TimeSeparator(0)==0);
249 test(aLocale.TimeSeparator(1)==':');
250 test(aLocale.TimeSeparator(2)==':');
251 test(aLocale.TimeSeparator(3)==0);
252 test(aLocale.AmPmSymbolPosition()==TRUE);
253 test(aLocale.AmPmSpaceBetween()==TRUE);
254 test(aLocale.HomeDaylightSavingZone()==EDstEuropean);
255 test(aLocale.WorkDays()==0x1f);
256 test(aLocale.StartOfWeek()==EMonday);
257 test(aLocale.ClockFormat()==EClockDigital);
258 test(aLocale.UnitsGeneral()==EUnitsMetric);
259 test(aLocale.UnitsDistanceShort()==EUnitsMetric);
260 test(aLocale.UnitsDistanceLong()==EUnitsMetric);
267 Subscribe to a system event.
269 @param aNotifier notifier
270 @param aStatus request status that is used with the notifier
271 @param aEventMask Specifies the event. See TChanges
273 static void SubscribeToSystemChangeNotification(RChangeNotifier& aNotifier, TRequestStatus& aStatus, TUint32 aEventMask)
276 const TInt KMaxAttempts = 100;
278 for(i=0; i<KMaxAttempts; ++i)
280 test.Printf(_L("SubscribeToSystemChangeNotification(0x%x), attempt:%d\n"), aEventMask, i);
282 TInt nRes = aNotifier.Logon(aStatus);
283 test(nRes==KErrNone);
285 if(aStatus.Int() == KRequestPending)
288 //-- some other system-wide event can just happen; re-subscribe
289 test( !(aStatus.Int() & aEventMask));
292 test(i<KMaxAttempts);
296 Wait for the event(s) specified in aEventMask to happen
298 @param aNotifier notifier
299 @param aStatus request status that is used with the notifier
300 @param aEventMask Specifies the event. See TChanges
302 static void WaitForSystemChange(RChangeNotifier& aNotifier, TRequestStatus& aStatus, TUint32 aEventMask)
304 //-- it would be nice to have here a timeout processing in order not to wait forever.. but later.
307 User::WaitForRequest(aStatus);
308 if(aStatus.Int() & aEventMask)
311 //-- some other system-wide unexpected event happened, we need to resubscribe
312 test.Printf(_L("WaitForSystemChange(0x%x), happened:0x%x, resubscribing...\n"), aEventMask, aStatus.Int());
313 SubscribeToSystemChangeNotification(aNotifier, aStatus, aEventMask);
319 void testChangeLocale()
323 RChangeNotifier notifier;
324 TInt res=notifier.Create();
328 res=notifier.Logon(stat);
330 // initial value of stat already tested by t_chnot
332 SubscribeToSystemChangeNotification(notifier, stat, EChangesLocale);
334 test.Printf(_L("Change to US Locale\n"));
335 res=UserSvr::ChangeLocale(ELOCLUS);
336 test.Printf(_L("res=%d\n"),res);
339 WaitForSystemChange(notifier, stat, EChangesLocale);
340 test(stat.Int() & EChangesLocale);
342 //-------------------
343 SubscribeToSystemChangeNotification(notifier, stat, EChangesLocale);
346 // let's not test localisation data details now that the internationalisation
347 // team rather than us release the localisation dlls (changed 9/6/98)
350 test.Printf(_L("Change to GE Locale\n"));
351 res=UserSvr::ChangeLocale(ELOCLGE);
352 test.Printf(_L("res=%d\n"),res);
355 WaitForSystemChange(notifier, stat, EChangesLocale);
356 test(stat.Int() & EChangesLocale);
358 //-------------------
360 SubscribeToSystemChangeNotification(notifier, stat, EChangesLocale);
363 // let's not test localisation data details now that the internationalisation
364 // team rather than us release the localisation dlls (changed 9/6/98)
367 test.Printf(_L("Load non ELOCL type DLL\n"));
368 res=UserSvr::ChangeLocale(DUMMYDLL);
369 test.Printf(_L("res=%d\n"),res);
370 test(res == KErrNotSupported);
372 //-- ensure that there wasn't locale change
373 const TInt KMaxAttempts = 100;
375 for(i=0; i<KMaxAttempts; ++i)
377 if(stat ==KRequestPending)
380 //-- check that if something system-wide happened, it wasn't a locale change
381 test(!(stat.Int() & EChangesLocale));
382 SubscribeToSystemChangeNotification(notifier, stat, EChangesLocale);
384 test(i<KMaxAttempts);
388 // let's not test localisation data details now that the internationalisation
389 // team rather than us release the localisation dlls (changed 9/6/98)
392 //This test using US1 locale is for identifying different collation table
393 //used for matching and comparing. To specify a table to be used in the
394 //matching we just need to add the TCollationTable::EMatchingTable flag
395 //inside the TCollationMethods::iFlags
396 test.Printf(_L("Change to US1 Locale\n"));
397 res=UserSvr::ChangeLocale(ELOCLUS1);
398 test.Printf(_L("res=%d\n"),res);
401 WaitForSystemChange(notifier, stat, EChangesLocale);
402 test(stat.Int() & EChangesLocale);
405 //-------------------
406 SubscribeToSystemChangeNotification(notifier, stat, EChangesLocale);
410 //Testing the different collation table used for Matching and Comparing
411 //after loading this new locale using T_LOCLUS1.DLL.
412 //test.Printf(_L("Test for differnt collation table for Matching and Comparing\n"));
413 //TCollationMethod m=*Mem::GetDefaultMatchingTable();
414 //test(m.iFlags & TCollationMethod::EMatchingTable);
416 //The collation table t_locl.cpp can be found in \locl\t_locl.cpp
417 //Expected collation override for MatchC A==B and a==b
418 //Expected collation override for CompareC A>B and a>b
419 //Collation tables cannot be changed at runtime
420 /* TBuf16<1> a_cap(_L("A"));
421 TBuf16<1> a_small(_L("a"));
423 //Test using the locale independent matching and comparing
424 test(a_cap.Match(_L("B"))!=0);
425 test(a_cap.Compare(_L("B"))<0);
426 test(a_small.Match(_L("b"))!=0);
427 test(a_small.Compare(_L("b"))<0);
428 //Test that the default collation table(1st table) is selected here
429 test(a_cap.CompareC(_L("B"))!=0);
430 test(a_small.CompareC(_L("b"))!=0);
431 test(a_cap.CompareC(_L("B"))>0);
432 test(a_small.CompareC(_L("b"))>0);
433 //Test that the Matching collation table(3rd table) is selected here
434 test(a_cap.MatchC(_L("B"))==0);
435 test(a_small.MatchC(_L("b"))==0);
437 test.Printf(_L("Back to default UK Locale\n"));
438 res=UserSvr::ChangeLocale(ELOCL_DEFAULT);
439 test.Printf(_L("res=%d\n"),res);
442 WaitForSystemChange(notifier, stat, EChangesLocale);
443 test(stat.Int() & EChangesLocale);
445 //-------------------
447 SubscribeToSystemChangeNotification(notifier, stat, EChangesLocale);
452 //Test for locale which does not have a matching collation table
453 TCollationMethod m1=*Mem::GetDefaultMatchingTable();
454 test((m1.iFlags & TCollationMethod::EMatchingTable)==0);
456 //************************************************
457 test.Printf(_L("Change to US Locale with file open on drive\n"));
459 _LIT(KTestFile, "TEST.TXT");
461 res = file.Replace(TheFs, KTestFile, 0);
462 test(res == KErrNone);
465 res=UserSvr::ChangeLocale(ELOCLUS);
466 test.Printf(_L("res=%d\n"),res);
470 WaitForSystemChange(notifier, stat, EChangesLocale);
471 test(stat.Int() & EChangesLocale);
474 //-------------------
475 SubscribeToSystemChangeNotification(notifier, stat, EChangesLocale);
478 _LIT8(KTestData, "Arsenal");
479 res = file.Write(KTestData);
483 res = file.Open(TheFs, KTestFile, 0);
484 test(res == KErrNone);
486 res = TheFs.Delete(KTestFile);
489 //************************************************
491 //-- close the notifier
493 User::WaitForRequest(stat);
495 //-- n.b. it's actually a bad idea to expect _exact_ event completion from the RChangeNotifier, because
496 //-- this is a system-wide events observer and any of the TChanges events can happen, not only EChangesLocale.
497 //-- so this test is a bit flawed.
499 //test(stat==KErrGeneral);
502 const TText * const DateSuffixTable[KMaxSuffixes] =
504 _S("st"),_S("nd"),_S("rd"),_S("th"),_S("th"),
505 _S("th"),_S("th"),_S("th"),_S("th"),_S("th"),
506 _S("th"),_S("th"),_S("th"),_S("th"),_S("th"),
507 _S("th"),_S("th"),_S("th"),_S("th"),_S("th"),
508 _S("st"),_S("nd"),_S("rd"),_S("th"),_S("th"),
509 _S("th"),_S("th"),_S("th"),_S("th"),_S("th"),
513 const TText * const DayTable[KMaxDays] =
524 const TText * const DayAbbTable[KMaxDays] =
535 const TText * const MonthTable[KMaxMonths] =
551 const TText * const MonthAbbTable[KMaxMonths] =
567 const TText * const AmPmTable[KMaxAmPms] = {_S("am"),_S("pm")};
569 void testExtendedUS(TUint aAspect, TExtendedLocale& aLocale)
571 TLocale* loc = aLocale.GetLocale();
574 if(aAspect & ELocaleLanguageSettings)
576 TLanguage tl = User::Language();
577 test(tl == ELangAmerican);
579 TDateSuffix datesuffix;
581 for(i=0;i<KMaxSuffixes;++i)
584 test(datesuffix.Compare(TPtrC(DateSuffixTable[i])) == 0);
588 for(i=0;i<KMaxDays;++i)
590 dayname.Set((TDay)i);
591 test(dayname.Compare(TPtrC(DayTable[i])) == 0);
594 TDayNameAbb daynameabb;
595 for(i=0;i<KMaxDays;++i)
597 daynameabb.Set((TDay)i);
598 test(daynameabb.Compare(TPtrC(DayAbbTable[i])) == 0);
601 TMonthName monthname;
602 for(i=0;i<KMaxMonths;++i)
604 monthname.Set((TMonth)i);
605 test(monthname.Compare(TPtrC(MonthTable[i])) == 0);
608 TMonthNameAbb monthnameabb;
609 for(i=0;i<KMaxMonths;++i)
611 monthnameabb.Set((TMonth)i);
612 test(monthnameabb.Compare(TPtrC(MonthAbbTable[i])) == 0);
616 for(i=0;i<KMaxAmPms;++i)
618 ampmname.Set((TAmPm)i);
619 test(ampmname.Compare(TPtrC(AmPmTable[i])) == 0);
623 if(aAspect & ELocaleLocaleSettings)
625 TCurrencySymbol symbol;
627 test(symbol.Compare(TPtrC(_S("$"))) == 0);
630 if(aAspect & ELocaleTimeDateSettings)
632 TShortDateFormatSpec shortdate;
634 test(shortdate.Compare(TPtrC(_S("%F%*M/%*D/%Y"))) == 0);
636 TLongDateFormatSpec longdate;
638 test(longdate.Compare(TPtrC(_S("%F%*D%X %N %Y"))) == 0);
640 TTimeFormatSpec spec;
642 test(spec.Compare(TPtrC(_S("%F%*I:%T:%S %*A"))) == 0);
648 void testExtendedLocale()
650 TExtendedLocale locale;
651 locale.LoadLocale(ELOCLUS);
652 TInt r = locale.SaveSystemSettings();
654 testExtendedUS(ELocaleLanguageSettings | ELocaleCollateSetting | ELocaleLocaleSettings | ELocaleTimeDateSettings, locale);
656 r = locale.SetCurrencySymbol(TPtrC(_S("Leu")));
658 TCurrencySymbol symbol;
660 test(symbol.Compare(TPtrC(_S("Leu"))) == 0);
662 User::SetCurrencySymbol(TPtrC(_S("Le")));
664 test(symbol.Compare(TPtrC(_S("Le"))) == 0);
668 eloclus.Copy(ELOCLUS);
669 eloclus.Append(TPtrC(KDLLExtension));
670 r = locale.GetLocaleDllName(ELocaleLanguageSettings, dllName);
672 test.Printf(_L("dllName looking for %s (%s)\n"), dllName.Ptr(), eloclus.Ptr());
673 test(dllName.Find(eloclus) != KErrNotFound);
677 r = locale.GetLocaleDllName(ELocaleCollateSetting, dllName);
679 test(dllName.Find(eloclus) != KErrNotFound);
683 r = locale.GetLocaleDllName(ELocaleLocaleSettings, dllName);
685 test(dllName.Find(eloclus) != KErrNotFound);
689 r = locale.GetLocaleDllName(ELocaleTimeDateSettings, dllName);
691 test(dllName.Find(eloclus) != KErrNotFound);
695 r = locale.LoadLocaleAspect(ELocaleLocaleSettings | ELocaleTimeDateSettings, ELOCLGE);
698 r = locale.SaveSystemSettings();
701 testExtendedUS(ELocaleLanguageSettings | ELocaleCollateSetting, locale);
704 test(symbol.Compare(TPtrC(_S("DM"))) == 0);
706 TShortDateFormatSpec shortdate;
708 test(shortdate.Compare(TPtrC(_S("%F%*D.%*M.%Y"))) == 0);
710 TLongDateFormatSpec longdate;
712 test(longdate.Compare(TPtrC(_S("%F%*D%X %N %Y"))) == 0);
714 TTimeFormatSpec spec;
716 test(spec.Compare(TPtrC(_S("%F%H:%T:%S"))) == 0);
719 eloclge.Copy(ELOCLGE);
720 eloclge.Append(KDLLExtension);
721 r = locale.GetLocaleDllName(ELocaleLanguageSettings, dllName);
723 test(dllName.Find(eloclus) != KErrNotFound);
727 r = locale.GetLocaleDllName(ELocaleCollateSetting, dllName);
729 test(dllName.Find(eloclus) != KErrNotFound);
733 r = locale.GetLocaleDllName(ELocaleLocaleSettings, dllName);
735 test(dllName.Find(eloclge) != KErrNotFound);
739 r = locale.GetLocaleDllName(ELocaleTimeDateSettings, dllName);
741 test(dllName.Find(eloclge) != KErrNotFound);
747 void testDigitsInMonthsNames()
749 test.Next(_L("Testing date parsing when month names include digits"));
752 _LIT(KMonthJan, "30 Th1 2006 14:31:50");
753 test_Equal(KErrArgument, time.Parse(KMonthJan));
754 test_KErrNone(UserSvr::ChangeLocale(ELOCLUS2));
755 test_Equal(EParseDatePresent | EParseTimePresent, time.Parse(KMonthJan));
756 test_KErrNone(UserSvr::ChangeLocale(ELOCL_DEFAULT));
759 GLDEF_C void CallTestsL(void)
763 test.Start(_L("Starting T_DLOCL tests"));
767 testExtendedLocale();
768 testDigitsInMonthsNames();
769 test.Next(_L("Close connection to the file server\n"));