Back to index

lightning-sunbird  0.9+nobinonly
nsLocaleTest.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #include <stdlib.h>
00038 #include "nsILocale.h"
00039 #include "nsILocaleFactory.h"
00040 #include "nsLocaleCID.h"
00041 #include "nsIComponentManager.h"
00042 #include "nsILocaleService.h"
00043 
00044 #if defined(XP_WIN) || defined(XP_OS2)
00045 #include "nsIWin32Locale.h"
00046 #include <windows.h>
00047 #endif
00048 #ifdef XP_UNIX
00049 #include "nsIPosixLocale.h"
00050 #endif
00051 #ifdef XP_MAC
00052 #include "nsIMacLocale.h"
00053 #endif
00054 
00055 NS_DEFINE_CID(kLocaleFactoryCID, NS_LOCALEFACTORY_CID);
00056 NS_DEFINE_IID(kILocaleFactoryIID, NS_ILOCALEFACTORY_IID);
00057 NS_DEFINE_IID(kILocaleServiceIID,NS_ILOCALESERVICE_IID);
00058 NS_DEFINE_CID(kLocaleServiceCID,NS_LOCALESERVICE_CID);
00059 NS_DEFINE_IID(kIFactoryIID, NS_IFACTORY_IID);
00060 NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
00061 
00062 #if defined(XP_WIN) || defined(XP_OS2)
00063 NS_DEFINE_CID(kWin32LocaleFactoryCID, NS_WIN32LOCALEFACTORY_CID);
00064 NS_DEFINE_IID(kIWin32LocaleIID, NS_IWIN32LOCALE_IID);
00065 
00066 #define USER_DEFINED_PRIMARYLANG   0x0200
00067 #define USER_DEFINED_SUBLANGUAGE   0x20
00068 
00069 #endif
00070 #ifdef XP_UNIX
00071 NS_DEFINE_CID(kPosixLocaleFactoryCID, NS_POSIXLOCALEFACTORY_CID);
00072 NS_DEFINE_IID(kIPosixLocaleIID, NS_IPOSIXLOCALE_IID);
00073 #endif
00074 #ifdef XP_MAC
00075 NS_DEFINE_CID(kMacLocaleFactoryCID, NS_MACLOCALEFACTORY_CID);
00076 NS_DEFINE_IID(kIMacLocaleIID, NS_IMACLOCALE_IID);
00077 #endif
00078 
00079 char* localeCategoryList[6] = { "NSILOCALE_TIME",
00080                                                         "NSILOCALE_COLLATE",
00081                                                         "NSILOCALE_CTYPE",
00082                                                         "NSILOCALE_MONETARY",
00083                                                         "NSILOCALE_MESSAGES",
00084                                                         "NSILOCALE_NUMERIC"
00085 };
00086 
00087 void
00088 serivce_create_interface(void)
00089 {
00090        nsresult                    result;
00091        nsILocaleService*    localeService;
00092 
00093        result = CallCreateInstance(kLocaleServiceCID, &localeService);
00094        NS_ASSERTION(localeService!=NULL,"nsLocaleTest: service_create_interface failed.");
00095        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: service_create_interface failed");
00096 
00097        localeService->Release();
00098 }
00099 
00100 void
00101 factory_create_interface(void)
00102 {
00103        nsresult                    result;
00104        nsILocaleFactory*    localeFactory;
00105        nsIFactory*                 genericFactory;
00106 
00107        result = CallCreateInstance(kLocaleFactoryCID, &localeFactory);
00108        NS_ASSERTION(localeFactory!=NULL,"nsLocaleTest: factory_create_interface failed.");
00109        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_create_interface failed");
00110 
00111        localeFactory->Release();
00112 
00113        result = CallCreateInstance(kLocaleFactoryCID, &genericFactory);
00114        NS_ASSERTION(localeFactory!=NULL,"nsLocaleTest: factory_create_interface failed.");
00115        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_create_interface failed");
00116 
00117        genericFactory->Release();
00118 }
00119 
00120 void 
00121 factory_test_isupports(void)
00122 {
00123        nsresult                    result;
00124        nsCOMPtr<nsILocaleFactory>  localeFactory;
00125        nsCOMPtr<nsISupports>              genericInterface1, genericInterface2;
00126        nsCOMPtr<nsIFactory> genericFactory1, genericFactory2;
00127 
00128   localeFactory = do_GetClassObject(kLocaleFactoryCID, &result);
00129        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_create_interface failed");
00130 
00131        //
00132        // test generic interface
00133        //
00134        result = localeFactory->QueryInterface(kISupportsIID, getter_AddRefs(genericInterface1));
00135        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_test_isupports failed.");
00136 
00137        result = localeFactory->QueryInterface(kISupportsIID, getter_AddRefs(genericInterface2));
00138        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_test_isupports failed.");
00139        NS_ASSERTION(genericInterface1==genericInterface2,"nsLocaleTest: factory_test_isupports failed.");
00140 
00141        //
00142        // test generic factory
00143        //
00144        result = localeFactory->QueryInterface(kIFactoryIID, getter_AddRefs(genericFactory1));
00145        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_test_isupports failed.");
00146 
00147        result = localeFactory->QueryInterface(kIFactoryIID, getter_AddRefs(genericFactory2));
00148        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_test_isupports failed.");
00149        NS_ASSERTION(genericFactory1==genericFactory2,"nsLocaleTest: factory_test_isupports failed.");
00150 
00151 }
00152 
00153 void
00154 factory_new_locale(void)
00155 {
00156        nsresult                    result;
00157        nsCOMPtr<nsILocaleFactory>  localeFactory;
00158        nsCOMPtr<nsILocale>  locale;
00159        nsString*                   *category, *value;
00160        int    i;
00161        nsString**                  categoryList, **valueList;
00162        PRUnichar *lc_name_unichar;
00163 
00164   localeFactory = do_GetClassObject(kLocaleFactoryCID, &result);
00165        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_create_interface failed");
00166 
00167 
00168        //
00169        // test NewLocale
00170        //
00171        nsAutoString localeName(NS_LITERALSTRING("ja-JP"));
00172        result = localeFactory->NewLocale(localeName, getter_AddRefs(locale));
00173        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_new_interface failed");
00174        NS_ASSERTION(locale!=NULL,"nsLocaleTest: factory_new_interface failed");
00175 
00176        for(i=0;i<6;i++)
00177        {
00178               category = new nsString(localeCategoryList[i]);
00179               value = new nsString();
00180 
00181               result = locale->GetCategory(category, &lc_name_unichar);
00182               NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_new_interface failed");
00183        
00184     value->SetString(lc_name_unichar);
00185               delete category;
00186               delete value;
00187        }
00188 
00189        categoryList = new nsStringArray(6);
00190        valueList = new nsStringArray(6);
00191 
00192        for(i=0;i<6;i++)
00193        {
00194               categoryList.InsertStringAt(localeCategoryList[i], i);
00195               valueList[i] = InsertStringAt(NS_LITERAL_STRING("x-netscape"), i);
00196        }
00197 
00198        result = localeFactory->NewLocale(categoryList,valueList,6,&locale);
00199        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_new_interface failed");
00200        NS_ASSERTION(locale!=NULL,"nsLocaleTest: factory_new_interface failed");
00201 
00202        for(i=0;i<6;i++)
00203        {
00204               value = new nsString();
00205               result = locale->GetCategory(categoryList[i], &lc_name_unichar);
00206               NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_new_interface failed");
00207 
00208     value->SetString(lc_name_unichar);
00209               delete value;
00210        }
00211 
00212        for(i=0;i<6;i++)
00213        {
00214               delete categoryList.RemoveStringAt(i);
00215               delete valueList.RemoveStringAt(i);
00216        }
00217 
00218        delete  categoryList;
00219        delete  valueList;
00220 }
00221 
00222 
00223 void
00224 factory_get_locale(void)
00225 {
00226        nsresult                    result;
00227        nsCOMPtr<nsILocaleFactory>  localeFactory;
00228        nsCOMPtr<nsILocale>  locale;
00229        nsString*                   category;
00230        nsString*                   value;
00231        const char*                 acceptLangString = "ja;q=0.9,en;q=1.0,*";
00232        PRUnichar *lc_name_unichar;
00233 
00234   localeFactory = do_GetClassObject(kLocaleFactoryCID, &result);
00235        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_create_interface failed");
00236 
00237        //
00238        // get the application locale
00239        //
00240        result = localeFactory->GetApplicationLocale(getter_AddRefs(locale));
00241        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_get_locale failed");
00242 
00243        //
00244        // test and make sure the locale is a valid Interface
00245        //
00246        locale->AddRef();
00247 
00248        category = new nsString("NSILOCALE_CTYPE");
00249        value = new nsString();
00250 
00251        result = locale->GetCategory(category, &lc_name_unichar);
00252        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_get_locale failed");
00253 
00254   value->SetString(lc_name_unichar);
00255 
00256        delete category;
00257        delete value;
00258 
00259        //
00260        // test GetSystemLocale
00261        //
00262        result = localeFactory->GetSystemLocale(getter_AddRefs(locale));
00263        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_get_locale failed");
00264        NS_ASSERTION(locale!=NULL,"nsLocaleTest: factory_get_locale failed");
00265 
00266        //
00267        // test and make sure the locale is a valid Interface
00268        //
00269 
00270        category = new nsString("NSILOCALE_CTYPE");
00271        value = new nsString();
00272 
00273        result = locale->GetCategory(category->get(),&lc_name_unichar);
00274        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_get_locale failed");
00275 
00276   value->SetString(lc_name_unichar);
00277 
00278        delete category;
00279        delete value;
00280 
00281        //
00282        // test GetLocaleFromAcceptLanguage
00283        //
00284        result = localeFactory->GetLocaleFromAcceptLanguage(acceptLangString,getter_AddRefs(locale));
00285        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_get_locale failed");
00286 
00287        //
00288        // test and make sure the locale is a valid Interface
00289        //
00290        category = new nsString("NSILOCALE_CTYPE");
00291        value = new nsString();
00292 
00293        result = locale->GetCategory(category, &lc_name_unichar);
00294        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_get_locale failed");
00295 
00296   value->SetString(lc_name_unichar);
00297 
00298        delete category;
00299        delete value;
00300 
00301 }
00302 
00303 
00304 #if defined(XP_WIN) || defined(XP_OS2)
00305 
00306 void
00307 win32factory_create_interface(void)
00308 {
00309        nsresult                    result;
00310        nsIFactory*                 factory;
00311        nsIWin32Locale*             win32Locale;
00312 
00313        result = CallCreateInstance(kWin32LocaleFactoryCID, &factory);
00314        NS_ASSERTION(factory!=NULL,"nsLocaleTest: factory_create_interface failed.");
00315        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_create_interface failed");
00316 
00317        factory->Release();
00318 
00319        result = CallCreateInstance(kWin32LocaleFactoryCID, &win32Locale);
00320        NS_ASSERTION(win32Locale!=NULL,"nsLocaleTest: factory_create_interface failed.");
00321        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_create_interface failed");
00322 
00323        win32Locale->Release();
00324 }
00325 
00326 void
00327 win32locale_test(void)
00328 {
00329        nsresult                    result;
00330        nsIWin32Locale*             win32Locale;
00331        nsAutoString         locale;
00332        LCID                        loc_id;
00333 
00334        //
00335        // test with a simple locale
00336        //
00337     locale.AssignLiteral("en-US");
00338        loc_id = 0;
00339 
00340        result = CallCreateInstance(kWin32LocaleFactoryCID, &win32Locale);
00341        NS_ASSERTION(win32Locale!=NULL,"nsLocaleTest: factory_create_interface failed.");
00342        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_create_interface failed");
00343 
00344        result = win32Locale->GetPlatformLocale(locale,&loc_id);
00345        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00346        NS_ASSERTION(loc_id==MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT),
00347               "nsLocaleTest: GetPlatformLocale failed.");
00348 
00349 
00350        //
00351        // test with a not so simple locale
00352        //
00353        locale.AssignLiteral("x-netscape");
00354        loc_id = 0;
00355 
00356        result = win32Locale->GetPlatformLocale(locale,&loc_id);
00357        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00358        NS_ASSERTION(loc_id==MAKELCID(MAKELANGID(USER_DEFINED_PRIMARYLANG,USER_DEFINED_SUBLANGUAGE),SORT_DEFAULT),
00359               "nsLocaleTest: GetPlatformLocale failed.");
00360 
00361 
00362        locale.AssignLiteral("en");
00363        loc_id = 0;
00364 
00365        result = win32Locale->GetPlatformLocale(locale,&loc_id);
00366        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00367        NS_ASSERTION(loc_id==MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT),SORT_DEFAULT),
00368               "nsLocaleTest: GetPlatformLocale failed.");
00369 
00370        win32Locale->Release();
00371 }
00372 
00373 void
00374 win32locale_conversion_test(void)
00375 {
00376        nsresult                    result;
00377        nsIWin32Locale*             win32Locale;
00378        nsString*                   locale;
00379        LCID                        loc_id;
00380 
00381        result = CallCreateInstance(kWin32LocaleFactoryCID, &win32Locale);
00382        NS_ASSERTION(win32Locale!=NULL,"nsLocaleTest: factory_create_interface failed.");
00383        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_create_interface failed");
00384 
00385        //
00386        // check english variants
00387        //
00388        locale.AssignLiteral("en"); // generic english
00389        loc_id = 0;
00390 
00391        result = win32Locale->GetPlatformLocale(locale,&loc_id);
00392        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00393        NS_ASSERTION(loc_id==MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT),SORT_DEFAULT),
00394               "nsLocaleTest: GetPlatformLocale failed.");
00395 
00396        locale.AssignLiteral("en-US");     // US english
00397        loc_id = 0;
00398 
00399        result = win32Locale->GetPlatformLocale(locale,&loc_id);
00400        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00401        NS_ASSERTION(loc_id==MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),SORT_DEFAULT),
00402               "nsLocaleTest: GetPlatformLocale failed.");
00403 
00404        locale.AssignLiteral("en-GB");     // UK english
00405        loc_id = 0;
00406 
00407        result = win32Locale->GetPlatformLocale(locale,&loc_id);
00408        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00409        NS_ASSERTION(loc_id==MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_UK),SORT_DEFAULT),
00410               "nsLocaleTest: GetPlatformLocale failed.");
00411 
00412        locale.AssignLiteral("en-CA");     // Canadian english
00413        loc_id = 0;
00414 
00415        result = win32Locale->GetPlatformLocale(locale,&loc_id);
00416        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00417        NS_ASSERTION(loc_id==MAKELCID(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_CAN),SORT_DEFAULT),
00418               "nsLocaleTest: GetPlatformLocale failed.");
00419 
00420        //
00421        // japanese
00422        //
00423        locale.AssignLiteral("ja");
00424        loc_id = 0;
00425 
00426        result = win32Locale->GetPlatformLocale(locale,&loc_id);
00427        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00428        NS_ASSERTION(loc_id==MAKELCID(MAKELANGID(LANG_JAPANESE,SUBLANG_DEFAULT),SORT_DEFAULT),
00429               "nsLocaleTest: GetPlatformLocale failed.");
00430 
00431        locale.AssignLiteral("ja-JP");
00432        loc_id = 0;
00433 
00434        result = win32Locale->GetPlatformLocale(locale,&loc_id);
00435        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00436        NS_ASSERTION(loc_id==MAKELCID(MAKELANGID(LANG_JAPANESE,SUBLANG_DEFAULT),SORT_DEFAULT),
00437               "nsLocaleTest: GetPlatformLocale failed.");
00438 
00439        //
00440        // chinese Locales
00441        //
00442        locale.AssignLiteral("zh");
00443        loc_id = 0;
00444 
00445        result = win32Locale->GetPlatformLocale(locale,&loc_id);
00446        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00447        NS_ASSERTION(loc_id==MAKELCID(MAKELANGID(LANG_CHINESE,SUBLANG_DEFAULT),SORT_DEFAULT),
00448               "nsLocaleTest: GetPlatformLocale failed.");
00449 
00450        locale.AssignLiteral("zh-CN");
00451        loc_id = 0;
00452 
00453        result = win32Locale->GetPlatformLocale(locale,&loc_id);
00454        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00455        NS_ASSERTION(loc_id==MAKELCID(MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_SIMPLIFIED),SORT_DEFAULT),
00456               "nsLocaleTest: GetPlatformLocale failed.");
00457 
00458        locale.AssignLiteral("zh-TW");
00459        loc_id = 0;
00460 
00461        result = win32Locale->GetPlatformLocale(locale,&loc_id);
00462        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00463        NS_ASSERTION(loc_id==MAKELCID(MAKELANGID(LANG_CHINESE,SUBLANG_CHINESE_TRADITIONAL),SORT_DEFAULT),
00464               "nsLocaleTest: GetPlatformLocale failed.");
00465 
00466        //
00467        // german and variants
00468        //
00469        locale.AssignLiteral("de");
00470        loc_id = 0;
00471 
00472        result = win32Locale->GetPlatformLocale(locale,&loc_id);
00473        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00474        NS_ASSERTION(loc_id==MAKELCID(MAKELANGID(LANG_GERMAN,SUBLANG_DEFAULT),SORT_DEFAULT),
00475               "nsLocaleTest: GetPlatformLocale failed.");
00476 
00477        locale.AssignLiteral("de-DE");
00478        loc_id = 0;
00479 
00480        result = win32Locale->GetPlatformLocale(locale,&loc_id);
00481        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00482        NS_ASSERTION(loc_id==MAKELCID(MAKELANGID(LANG_GERMAN,SUBLANG_GERMAN),SORT_DEFAULT),
00483               "nsLocaleTest: GetPlatformLocale failed.");
00484 
00485        locale.AssignLiteral("de-AT");
00486        loc_id = 0;
00487 
00488        result = win32Locale->GetPlatformLocale(locale,&loc_id);
00489        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00490        NS_ASSERTION(loc_id==MAKELCID(MAKELANGID(LANG_GERMAN,SUBLANG_GERMAN_AUSTRIAN),SORT_DEFAULT),
00491               "nsLocaleTest: GetPlatformLocale failed.");
00492 
00493        //
00494        // french and it's variants
00495        //
00496        locale.AssignLiteral("fr");
00497        loc_id = 0;
00498 
00499        result = win32Locale->GetPlatformLocale(locale,&loc_id);
00500        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00501        NS_ASSERTION(loc_id==MAKELCID(MAKELANGID(LANG_FRENCH,SUBLANG_DEFAULT),SORT_DEFAULT),
00502               "nsLocaleTest: GetPlatformLocale failed.");
00503 
00504        locale.AssignLiteral("fr-FR");
00505        loc_id = 0;
00506 
00507        result = win32Locale->GetPlatformLocale(locale,&loc_id);
00508        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00509        NS_ASSERTION(loc_id==MAKELCID(MAKELANGID(LANG_FRENCH,SUBLANG_FRENCH),SORT_DEFAULT),
00510               "nsLocaleTest: GetPlatformLocale failed.");
00511 
00512        locale.AssignLiteral("fr-CA");
00513        loc_id = 0;
00514 
00515        result = win32Locale->GetPlatformLocale(locale,&loc_id);
00516        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00517        NS_ASSERTION(loc_id==MAKELCID(MAKELANGID(LANG_FRENCH,SUBLANG_FRENCH_CANADIAN),SORT_DEFAULT),
00518               "nsLocaleTest: GetPlatformLocale failed.");
00519 
00520        //
00521        // delete the XPCOM inteface
00522        //
00523        win32Locale->Release();
00524 }
00525 
00526 void
00527 win32locale_reverse_conversion_test(void)
00528 {
00529        nsresult                    result;
00530        nsIWin32Locale*             win32Locale;
00531 
00532        result = CallCreateInstance(kWin32LocaleFactoryCID, &win32Locale);
00533        NS_ASSERTION(win32Locale!=NULL,"nsLocaleTest: factory_create_interface failed.");
00534        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_create_interface failed");
00535 
00536        //
00537        // english and variants
00538        //
00539        win32Locale->Release();
00540 }
00541 
00542 void
00543 win32_test_special_locales(void)
00544 {
00545        nsresult                    result;
00546        nsCOMPtr<nsIWin32Locale>    win32Locale;
00547        nsCOMPtr<nsILocaleFactory>  xp_locale_factory;
00548        nsCOMPtr<nsILocale>  xp_locale;
00549        nsString*                   locale, *result_locale, *category;
00550        LCID                        sys_lcid, user_lcid;
00551        PRUnichar *lc_name_unichar;
00552 
00553   win32Locale = do_CreateInstance(kWin32LocaleFactoryCID, &result);
00554        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_create_interface failed");
00555 
00556   xp_locale_factory = do_GetClassObject(kLocaleFactoryCID, &result);
00557        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_create_interface failed");
00558 
00559        category = new nsString(localeCategoryList[0]);
00560 
00561        //
00562        // derive a system locale
00563        //
00564        result  = xp_locale_factory->GetSystemLocale(getter_AddRefs(xp_locale));
00565        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_create_interface failed");
00566 
00567        sys_lcid = GetSystemDefaultLCID();
00568        locale = new nsString();
00569        result_locale = new nsString();
00570 
00571        result = win32Locale->GetXPLocale(sys_lcid,locale);
00572        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_create_interface failed");
00573        result = xp_locale->GetCategory(category, &lc_name_unichar);
00574        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_create_interface failed");
00575 
00576   result_locale->SetString(lc_name_unichar);
00577        delete result_locale;
00578 
00579        //
00580        // derive a system locale
00581        //
00582        result  = xp_locale_factory->GetApplicationLocale(getter_AddRefs(xp_locale));
00583        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_create_interface failed");
00584 
00585        user_lcid = GetUserDefaultLCID();
00586        locale = new nsString();
00587        result_locale = new nsString();
00588 
00589        result = win32Locale->GetXPLocale(user_lcid,locale);
00590        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_create_interface failed");
00591        result = xp_locale->GetCategory(category, &lc_name_unichar);
00592        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_create_interface failed");
00593 
00594  result_locale->SetString(lc_name_unichar);
00595        delete result_locale;
00596 
00597        delete category;
00598 
00599 }
00600        
00601 #endif
00602 
00603 #ifdef XP_UNIX
00604 void
00605 posixfactory_create_interface(void)
00606 {
00607        nsresult                      result;
00608        nsIFactory*                 factory;
00609        nsIPosixLocale*      posix_locale;
00610 
00611        result = CallCreateInstance(kPosixLocaleFactoryCID, &factory);
00612        NS_ASSERTION(factory!=NULL,"nsLocaleTest: factory_create_interface failed.");
00613        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_create_interface failed");
00614 
00615        factory->Release();
00616 
00617        result = CallCreateInstance(kPosixLocaleFactoryCID, &posix_locale);
00618        NS_ASSERTION(posix_locale!=NULL,"nsLocaleTest: factory_create_interface failed.");
00619        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_create_interface failed");
00620 
00621        posix_locale->Release();
00622 }
00623 
00624 void
00625 posixlocale_test(void)
00626 {
00627        nsresult                        result;
00628        nsIPosixLocale*             posix_locale;
00629        nsAutoString      locale;
00630        char              posix_locale_string[9];
00631 
00632   //
00633   // create the locale object
00634   //
00635        result = CallCreateInstance(kPosixLocaleFactoryCID, &posix_locale);
00636        NS_ASSERTION(posix_locale!=NULL,"nsLocaleTest: create interface failed.\n");
00637        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: create interface failed\n");
00638 
00639        //
00640        // test with a simple locale
00641        //
00642        locale.AssignLiteral("en-US");
00643        result = posix_locale->GetPlatformLocale(locale,posix_locale_string,9);
00644        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.\n");
00645   NS_ASSERTION(strcmp("en_US",posix_locale_string)==0,"nsLocaleTest: GetPlatformLocale failed.\n");
00646 
00647        //
00648        // test with a not so simple locale
00649        //
00650        locale.AssignLiteral("x-netscape");
00651        result = posix_locale->GetPlatformLocale(locale,posix_locale_string,9);
00652        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.\n");
00653   NS_ASSERTION(strcmp("C",posix_locale_string)==0,"nsLocaleTest: GetPlatformLocale failed.\n");
00654 
00655   //
00656   // test with a generic locale
00657   //
00658        locale.AssignLiteral("en");
00659        result = posix_locale->GetPlatformLocale(locale,posix_locale_string,9);
00660        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.\n");
00661   NS_ASSERTION(strcmp("en",posix_locale_string)==0,"nsLocaleTest: GetPlatformLocale failed.\n");
00662 
00663 
00664   //
00665   // release the locale interface
00666   //
00667   posix_locale->Release();
00668 }
00669 
00670 void
00671 posixlocale_conversion_test()
00672 {
00673        nsresult                        result;
00674        nsIPosixLocale*             posix_locale;
00675        nsString*                       locale;
00676   char              posix_locale_result[9];
00677 
00678        result = CallCreateInstance(kPosixLocaleFactoryCID, &posix_locale);
00679        NS_ASSERTION(posix_locale!=NULL,"nsLocaleTest: factory_create_interface failed.");
00680        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_create_interface failed");
00681 
00682        //
00683        // check english variants
00684        //
00685        locale.AssignLiteral("en"); // generic english
00686        result = posix_locale->GetPlatformLocale(locale,posix_locale_result,9);
00687        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00688        NS_ASSERTION(strcmp("en",posix_locale_result)==0,"nsLocaleTest: GetPlatformLocale failed.\n");
00689 
00690        locale.AssignLiteral("en-US");     // US english
00691        result = posix_locale->GetPlatformLocale(locale,posix_locale_result,9);
00692        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00693        NS_ASSERTION(strcmp("en_US",posix_locale_result)==0,"nsLocaleTest: GetPlatformLocale failed.\n");
00694 
00695        locale.AssignLiteral("en-GB");     // UK english
00696        result = posix_locale->GetPlatformLocale(locale,posix_locale_result,9);
00697        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00698        NS_ASSERTION(strcmp("en_GB",posix_locale_result)==0,"nsLocaleTest: GetPlatformLocale failed.\n");
00699 
00700        locale.AssignLiteral("en-CA");     // Canadian english
00701        result = posix_locale->GetPlatformLocale(locale,posix_locale_result,9);
00702        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00703        NS_ASSERTION(strcmp("en_CA",posix_locale_result)==0,"nsLocaleTest: GetPlatformLocale failed.\n");
00704 
00705        //
00706        // japanese
00707        //
00708        locale.AssignLiteral("ja");
00709        result = posix_locale->GetPlatformLocale(locale,posix_locale_result,9);
00710        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00711        NS_ASSERTION(strcmp("ja",posix_locale_result)==0,"nsLocaleTest: GetPlatformLocale failed.\n");
00712 
00713        locale.AssignLiteral("ja-JP");
00714        result = posix_locale->GetPlatformLocale(locale,posix_locale_result,9);
00715        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00716        NS_ASSERTION(strcmp("ja_JP",posix_locale_result)==0,"nsLocaleTest: GetPlatformLocale failed.\n");
00717 
00718        //
00719        // chinese Locales
00720        //
00721        locale.AssignLiteral("zh");
00722        result = posix_locale->GetPlatformLocale(locale,posix_locale_result,9);
00723        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00724        NS_ASSERTION(strcmp("zh",posix_locale_result)==0,"nsLocaleTest: GetPlatformLocale failed.\n");
00725 
00726        locale.AssignLiteral("zh-CN");
00727        result = posix_locale->GetPlatformLocale(locale,posix_locale_result,9);
00728        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00729        NS_ASSERTION(strcmp("zh_CN",posix_locale_result)==0,"nsLocaleTest: GetPlatformLocale failed.\n");
00730 
00731        locale.AssignLiteral("zh-TW");
00732        result = posix_locale->GetPlatformLocale(locale,posix_locale_result,9);
00733        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00734        NS_ASSERTION(strcmp("zh_TW",posix_locale_result)==0,"nsLocaleTest: GetPlatformLocale failed.\n");
00735 
00736        //
00737        // german and variants
00738        //
00739        locale.AssignLiteral("de");
00740        result = posix_locale->GetPlatformLocale(locale,posix_locale_result,9);
00741        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00742        NS_ASSERTION(strcmp("de",posix_locale_result)==0,"nsLocaleTest: GetPlatformLocale failed.\n");
00743 
00744        locale.AssignLiteral("de-DE");
00745        result = posix_locale->GetPlatformLocale(locale,posix_locale_result,9);
00746        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00747        NS_ASSERTION(strcmp("de_DE",posix_locale_result)==0,"nsLocaleTest: GetPlatformLocale failed.\n");
00748 
00749        locale.AssignLiteral("de-AT");
00750        result = posix_locale->GetPlatformLocale(locale,posix_locale_result,9);
00751        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00752        NS_ASSERTION(strcmp("de_AT",posix_locale_result)==0,"nsLocaleTest: GetPlatformLocale failed.\n");
00753 
00754        //
00755        // french and it's variants
00756        //
00757        locale.AssignLiteral("fr");
00758        result = posix_locale->GetPlatformLocale(locale,posix_locale_result,9);
00759        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00760        NS_ASSERTION(strcmp("fr",posix_locale_result)==0,"nsLocaleTest: GetPlatformLocale failed.\n");
00761 
00762        locale.AssignLiteral("fr-FR");
00763        result = posix_locale->GetPlatformLocale(locale,posix_locale_result,9);
00764        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00765        NS_ASSERTION(strcmp("fr_FR",posix_locale_result)==0,"nsLocaleTest: GetPlatformLocale failed.\n");
00766 
00767        locale.AssignLiteral("fr-CA");
00768        result = posix_locale->GetPlatformLocale(locale,posix_locale_result,9);
00769        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetPlatformLocale failed.");
00770        NS_ASSERTION(strcmp("fr_CA",posix_locale_result)==0,"nsLocaleTest: GetPlatformLocale failed.\n");
00771 
00772        //
00773        // delete the XPCOM inteface
00774        //
00775        posix_locale->Release();
00776 }
00777 
00778 void
00779 posixlocale_reverse_conversion_test()
00780 {
00781        nsresult                        result;
00782        nsIPosixLocale*             posix_locale;
00783        nsAutoString             locale;
00784 
00785   //
00786   // create the locale object
00787   //
00788        result = CallCreateInstance(kPosixLocaleFactoryCID, &posix_locale);
00789        NS_ASSERTION(posix_locale!=NULL,"nsLocaleTest: create interface failed.\n");
00790        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: create interface failed\n");
00791 
00792        //
00793        // test with a simple locale
00794        //
00795        result = posix_locale->GetXPLocale("en_US",locale);
00796        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetXPLocale failed.\n");
00797        NS_ASSERTION(locale.EqualsLiteral("en-US"),"nsLocaleTest: GetXPLocale failed.\n");
00798 
00799        result = posix_locale->GetXPLocale("C",locale);
00800        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetXPLocale failed.\n");
00801        NS_ASSERTION(locale.EqualsLiteral("en"),"nsLocaleTest: GetXPLocale failed.\n");
00802 
00803        result = posix_locale->GetXPLocale("en",locale);
00804        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetXPLocale failed.\n");
00805   NS_ASSERTION(locale.EqualsLiteral("en"),"nsLocaleTest: GetXPLocale failed.\n");
00806 
00807   posix_locale->Release();
00808 
00809 }
00810 
00811 void
00812 posixlocale_test_special(void)
00813 {
00814        nsresult                        result;
00815        nsCOMPtr<nsILocaleFactory>  xp_factory;
00816        nsCOMPtr<nsILocale>  xp_locale;
00817   const PRUnichar *lc_name_unichar;
00818        nsString*                       locale, *result_locale;
00819   nsString*         lc_message;
00820 
00821   //
00822   // create the locale objects
00823   //
00824   xp_factory = do_GetClassObject(kLocaleFactoryCID, &result);
00825        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_create_interface failed");
00826 
00827   //
00828   // settup strings
00829   //
00830   locale.AssignLiteral("en");
00831   result_locale = new nsString();
00832   lc_message = new nsString("NSILOCALE_MESSAGES");
00833 
00834        //
00835        // test GetSystemLocale
00836        //
00837   result = xp_factory->GetSystemLocale(getter_AddRefs(xp_locale));
00838   NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetSystemLocale failed.\n");
00839   
00840   result = xp_locale->GetCategory(lc_message, &lc_name_unichar);
00841   NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetSystemLocale failed.\n");
00842   
00843   result_locale->SetString(lc_name_unichar);
00844 
00845   result = xp_factory->GetApplicationLocale(getter_AddRefs(xp_locale));
00846   NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetApplicationLocale failed.\n");
00847   
00848   
00849   result = xp_locale->GetCategory(lc_message, &lc_name_unichar);
00850   NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: GetSystemLocale failed.\n");
00851   result_locale->SetString(lc_name_unichar);
00852 
00853   //
00854   // delete strings
00855   //
00856   delete result_locale;
00857   delete lc_message;
00858 
00859 
00860 }
00861   
00862 #endif
00863 
00864 #ifdef XP_MAC
00865 
00866 void
00867 macfactory_create_interface(void)
00868 {
00869        nsresult                      result;
00870        nsIFactory*                   factory;
00871        nsIMacLocale*          mac_locale;
00872 
00873        result = CallCreateInstance(kMacLocaleFactoryCID, &factory);
00874        NS_ASSERTION(factory!=NULL,"nsLocaleTest: factory_create_interface failed.");
00875        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_create_interface failed");
00876 
00877        factory->Release();
00878 
00879        result = CallCreateInstance(kMacLocaleFactoryCID, &mac_locale);
00880        NS_ASSERTION(posix_locale!=NULL,"nsLocaleTest: factory_create_interface failed.");
00881        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: factory_create_interface failed");
00882 
00883        mac_locale->Release();
00884 }
00885 
00886 void
00887 maclocale_test(void)
00888 {
00889        nsresult                        result;
00890        nsIMacLocale*               mac_locale;
00891        nsString*                       locale;
00892        short                       script_code;
00893 
00894   //
00895   // create the locale object
00896   //
00897        result = CallCreateInstance(kMacLocaleFactoryCID, &mac_locale);
00898        NS_ASSERTION(posix_locale!=NULL,"nsLocaleTest: create interface failed.\n");
00899        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: create interface failed\n");
00900 
00901        //
00902        // release the locale interface
00903        //
00904        mac_locale->Release();
00905 }
00906 
00907 
00908 #endif
00909 
00910 int
00911 main(int argc, char** argv)
00912 {
00913 
00914        //
00915        // what are we doing?
00916        //
00917        printf("Starting nsLocaleTest\n");
00918        printf("---------------------\n");
00919        printf("This test has completed successfully if no error messages are printed.\n");
00920 
00921        //
00922        // run the nsILocaleFactory tests (nsILocale gets tested in the prcoess)
00923        //
00924        factory_create_interface();
00925        factory_get_locale();
00926        factory_new_locale();
00927 
00928 #if defined(XP_WIN) || defined(XP_OS2)
00929 
00930        //
00931        // run the nsIWin32LocaleFactory tests
00932        //
00933        win32factory_create_interface();
00934        win32locale_test();
00935        win32locale_conversion_test();
00936        win32locale_reverse_conversion_test();
00937        win32_test_special_locales();
00938 #elif defined(XP_UNIX)
00939 
00940   //
00941   // do the younicks tests
00942   //
00943   posixfactory_create_interface();
00944   posixlocale_test();
00945   posixlocale_conversion_test();
00946   posixlocale_reverse_conversion_test();
00947   posixlocale_test_special();
00948 
00949 #elif defined(XP_MAC)
00950 
00951   //
00952   // do the Mac specific tests
00953   //
00954   macfactory_create_interface();
00955   maclocale_test();
00956   
00957 #endif
00958 
00959        //
00960        // we done
00961        //
00962        printf("---------------------\n");
00963        printf("Finished nsLocaleTest\n");
00964 
00965        return 0;
00966 }