Back to index

lightning-sunbird  0.9+nobinonly
StringBundleTest.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 
00038 #include "nsCOMPtr.h"
00039 #include "nsReadableUtils.h"
00040 #include "nsIStringBundle.h"
00041 #include "nsIEventQueueService.h"
00042 #include <stdio.h>
00043 
00044 #include "nsIURL.h"
00045 #include "nsIServiceManager.h"
00046 #include "nsIComponentRegistrar.h"
00047 #include "nsNetCID.h"
00048 
00049 #include "nsString.h"
00050 
00051 #include "nsXPCOM.h"
00052 #include "nsIServiceManager.h"
00053 #include "nsIComponentManager.h"
00054 //
00055 #define TEST_URL "resource://gre/res/strres.properties"
00056 #define TEST_397093_URL "resource://gre/res/397093.properties"
00057 
00059 //
00060 // let add some locale stuff
00061 //
00064 #include "nsILocaleService.h"
00065 #include "nsLocaleCID.h"
00066 
00067 //
00068 //
00069 //
00070 nsresult
00071 getCountry(const nsAString &lc_name, nsAString &aCountry)
00072 {
00073   PRInt32   dash = lc_name.FindChar('-');
00074   if (dash > 0) {
00075     aCountry = lc_name;
00076     aCountry.Cut(dash, (lc_name.Length()-dash-1));
00077   }
00078   else
00079     return NS_ERROR_FAILURE;
00080 
00081   return NS_OK;
00082 }
00083 
00084 nsresult
00085 getUILangCountry(nsAString& aUILang, nsAString& aCountry)
00086 {
00087        nsresult       result;
00088        // get a locale service 
00089        nsCOMPtr<nsILocaleService> localeService = do_GetService(NS_LOCALESERVICE_CONTRACTID, &result);
00090        NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: get locale service failed");
00091 
00092   result = localeService->GetLocaleComponentForUserAgent(aUILang);
00093   NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: get locale component failed");
00094   result = getCountry(aUILang, aCountry);
00095   return result;
00096 }
00097 
00098 
00099 int
00100 main(int argc, char *argv[])
00101 {
00102   nsresult ret;
00103 
00104   nsCOMPtr<nsIServiceManager> servMan;
00105   NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
00106   nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
00107   NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
00108   registrar->AutoRegister(nsnull);
00109   
00110   nsCOMPtr<nsIStringBundleService> service =
00111       do_GetService(NS_STRINGBUNDLE_CONTRACTID);
00112   if (!service) {
00113     printf("cannot create service\n");
00114     return 1;
00115   }
00116 
00117   nsAutoString uiLang;
00118   nsAutoString country;
00119   ret = getUILangCountry(uiLang, country);
00120 #if DEBUG_tao
00121   printf("\n uiLang=%s, country=%s\n\n",
00122          NS_LossyConvertUTF16toASCII(uiLang).get(),
00123          NS_LossyConvertUTF16toASCII(country).get());
00124 #endif
00125 
00126   nsIStringBundle* bundle = nsnull;
00127 
00128   ret = service->CreateBundle(TEST_URL, &bundle);
00129 
00130   if (NS_FAILED(ret)) {
00131     printf("cannot create instance\n");
00132     return 1;
00133   }
00134 
00135   nsAutoString v;
00136   PRUnichar *ptrv = nsnull;
00137   char *value = nsnull;
00138 
00139   // 123
00140   ret = bundle->GetStringFromID(123, &ptrv);
00141   if (NS_FAILED(ret)) {
00142     printf("cannot get string from ID 123, ret=%d\n", ret);
00143     return 1;
00144   }
00145   v = ptrv;
00146   value = ToNewCString(v);
00147   printf("123=\"%s\"\n", value);
00148 
00149   // file
00150   nsString strfile;
00151   strfile.AssignLiteral("file");
00152   const PRUnichar *ptrFile = strfile.get();
00153   ret = bundle->GetStringFromName(ptrFile, &ptrv);
00154   if (NS_FAILED(ret)) {
00155     printf("cannot get string from name\n");
00156     return 1;
00157   }
00158   v = ptrv;
00159   value = ToNewCString(v);
00160   printf("file=\"%s\"\n", value);
00161 
00162   // Test for bug 397093
00163   nsIStringBundle* bundle397093 = nsnull;
00164 
00165   ret = service->CreateBundle(TEST_397093_URL, &bundle397093);
00166 
00167   if (NS_FAILED(ret)) {
00168     printf("cannot create bundle for 397093\n");
00169     return 1;
00170   }
00171 
00172   nsXPIDLString valueASCII, valueUTF8, valueLatin1;
00173   
00174   ret = bundle397093->GetStringFromName(NS_LITERAL_STRING("asciiProperty").get(),
00175                                   getter_Copies(valueASCII));
00176   if (NS_FAILED(ret)) {
00177     printf("failed to get string for asciiProperty\n");
00178     return 1;
00179   }
00180   if (!valueASCII.Equals(NS_LITERAL_STRING("Foo"))) {
00181     printf("incorrect value for asciiProperty\n");
00182     return 1;
00183   }
00184   
00185   ret = bundle397093->GetStringFromName(NS_LITERAL_STRING("utf8Property").get(),
00186                                   getter_Copies(valueUTF8));
00187   if (NS_FAILED(ret)) {
00188     printf("failed to get string for utf8Property\n");
00189     return 1;
00190   }
00191   if (!valueUTF8.Equals(NS_LITERAL_STRING("Fòò"))) {
00192     printf("incorrect value for utf8Property\n");
00193     return 1;
00194   }
00195 
00196   ret = bundle397093->GetStringFromName(NS_LITERAL_STRING("latin1Property").get(),
00197                                   getter_Copies(valueLatin1));
00198   if (NS_FAILED(ret)) {
00199     printf("failed to get string for latin1Property\n");
00200     return 1;
00201   }
00202   if (!valueLatin1.Equals(NS_LITERAL_STRING("F"))) {
00203     printf("incorrect value for latin1Property\n");
00204     return 1;
00205   }
00206   printf("test for bug 397093 passed\n");
00207 
00208   return 0;
00209 }