Back to index

lightning-sunbird  0.9+nobinonly
Defines | Functions
StringBundleTest.cpp File Reference
#include "nsCOMPtr.h"
#include "nsReadableUtils.h"
#include "nsIStringBundle.h"
#include "nsIEventQueueService.h"
#include <stdio.h>
#include "nsIURL.h"
#include "nsIServiceManager.h"
#include "nsIComponentRegistrar.h"
#include "nsNetCID.h"
#include "nsString.h"
#include "nsXPCOM.h"
#include "nsIComponentManager.h"
#include "nsILocaleService.h"
#include "nsLocaleCID.h"

Go to the source code of this file.

Defines

#define TEST_URL   "resource://gre/res/strres.properties"
#define TEST_397093_URL   "resource://gre/res/397093.properties"

Functions

nsresult getCountry (const nsAString &lc_name, nsAString &aCountry)
nsresult getUILangCountry (nsAString &aUILang, nsAString &aCountry)
int main (int argc, char *argv[])

Define Documentation

#define TEST_397093_URL   "resource://gre/res/397093.properties"

Definition at line 56 of file StringBundleTest.cpp.

#define TEST_URL   "resource://gre/res/strres.properties"

Definition at line 55 of file StringBundleTest.cpp.


Function Documentation

nsresult getCountry ( const nsAString &  lc_name,
nsAString &  aCountry 
)

Definition at line 71 of file StringBundleTest.cpp.

{
  PRInt32   dash = lc_name.FindChar('-');
  if (dash > 0) {
    aCountry = lc_name;
    aCountry.Cut(dash, (lc_name.Length()-dash-1));
  }
  else
    return NS_ERROR_FAILURE;

  return NS_OK;
}

Here is the caller graph for this function:

nsresult getUILangCountry ( nsAString &  aUILang,
nsAString &  aCountry 
)

Definition at line 85 of file StringBundleTest.cpp.

{
       nsresult       result;
       // get a locale service 
       nsCOMPtr<nsILocaleService> localeService = do_GetService(NS_LOCALESERVICE_CONTRACTID, &result);
       NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: get locale service failed");

  result = localeService->GetLocaleComponentForUserAgent(aUILang);
  NS_ASSERTION(NS_SUCCEEDED(result),"nsLocaleTest: get locale component failed");
  result = getCountry(aUILang, aCountry);
  return result;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( int  argc,
char *  argv[] 
)

Definition at line 100 of file StringBundleTest.cpp.

{
  nsresult ret;

  nsCOMPtr<nsIServiceManager> servMan;
  NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
  nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
  NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
  registrar->AutoRegister(nsnull);
  
  nsCOMPtr<nsIStringBundleService> service =
      do_GetService(NS_STRINGBUNDLE_CONTRACTID);
  if (!service) {
    printf("cannot create service\n");
    return 1;
  }

  nsAutoString uiLang;
  nsAutoString country;
  ret = getUILangCountry(uiLang, country);
#if DEBUG_tao
  printf("\n uiLang=%s, country=%s\n\n",
         NS_LossyConvertUTF16toASCII(uiLang).get(),
         NS_LossyConvertUTF16toASCII(country).get());
#endif

  nsIStringBundle* bundle = nsnull;

  ret = service->CreateBundle(TEST_URL, &bundle);

  if (NS_FAILED(ret)) {
    printf("cannot create instance\n");
    return 1;
  }

  nsAutoString v;
  PRUnichar *ptrv = nsnull;
  char *value = nsnull;

  // 123
  ret = bundle->GetStringFromID(123, &ptrv);
  if (NS_FAILED(ret)) {
    printf("cannot get string from ID 123, ret=%d\n", ret);
    return 1;
  }
  v = ptrv;
  value = ToNewCString(v);
  printf("123=\"%s\"\n", value);

  // file
  nsString strfile;
  strfile.AssignLiteral("file");
  const PRUnichar *ptrFile = strfile.get();
  ret = bundle->GetStringFromName(ptrFile, &ptrv);
  if (NS_FAILED(ret)) {
    printf("cannot get string from name\n");
    return 1;
  }
  v = ptrv;
  value = ToNewCString(v);
  printf("file=\"%s\"\n", value);

  // Test for bug 397093
  nsIStringBundle* bundle397093 = nsnull;

  ret = service->CreateBundle(TEST_397093_URL, &bundle397093);

  if (NS_FAILED(ret)) {
    printf("cannot create bundle for 397093\n");
    return 1;
  }

  nsXPIDLString valueASCII, valueUTF8, valueLatin1;
  
  ret = bundle397093->GetStringFromName(NS_LITERAL_STRING("asciiProperty").get(),
                                  getter_Copies(valueASCII));
  if (NS_FAILED(ret)) {
    printf("failed to get string for asciiProperty\n");
    return 1;
  }
  if (!valueASCII.Equals(NS_LITERAL_STRING("Foo"))) {
    printf("incorrect value for asciiProperty\n");
    return 1;
  }
  
  ret = bundle397093->GetStringFromName(NS_LITERAL_STRING("utf8Property").get(),
                                  getter_Copies(valueUTF8));
  if (NS_FAILED(ret)) {
    printf("failed to get string for utf8Property\n");
    return 1;
  }
  if (!valueUTF8.Equals(NS_LITERAL_STRING("Fòò"))) {
    printf("incorrect value for utf8Property\n");
    return 1;
  }

  ret = bundle397093->GetStringFromName(NS_LITERAL_STRING("latin1Property").get(),
                                  getter_Copies(valueLatin1));
  if (NS_FAILED(ret)) {
    printf("failed to get string for latin1Property\n");
    return 1;
  }
  if (!valueLatin1.Equals(NS_LITERAL_STRING("F"))) {
    printf("incorrect value for latin1Property\n");
    return 1;
  }
  printf("test for bug 397093 passed\n");

  return 0;
}

Here is the call graph for this function: