Add unit test for bug 378839 and remove obsolete test programs

This commit is contained in:
smontagu@smontagu.org 2007-05-09 21:58:59 -07:00
parent 20e95343df
commit 7ed4343a42
8 changed files with 69 additions and 464 deletions

View File

@ -42,34 +42,16 @@ VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
CPPSRCS = StringBundleTest.cpp
REQUIRES = xpcom \
string \
locale \
necko \
intl \
$(NULL)
MODULE = test_intl_strres
SIMPLE_PROGRAMS = $(CPPSRCS:.cpp=$(BIN_SUFFIX))
XPCSHELL_TESTS = unit
EXPORT_RESOURCE = \
$(srcdir)/strres.properties \
$(srcdir)/strres-test.html \
$(srcdir)/strres-test.xul \
$(srcdir)/strres-test.dtd \
$(srcdir)/strres-test.js \
$(NULL)
LIBS = \
$(DIST)/lib/$(LIB_PREFIX)xpcomglue_s.$(LIB_SUFFIX) \
$(XPCOM_LIBS) \
$(NSPR_LIBS) \
$(NULL)
include $(topsrcdir)/config/rules.mk
INCLUDES += -I$(srcdir)/../src
libs::
$(INSTALL) $(EXPORT_RESOURCE) $(DIST)/bin/res

View File

@ -1,163 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "nsCOMPtr.h"
#include "nsIStringBundle.h"
#include <stdio.h>
#include "nsIURL.h"
#include "nsIServiceManager.h"
#include "nsIComponentRegistrar.h"
#include "nsNetCID.h"
#include "nsStringAPI.h"
#include "nsEmbedString.h"
#include "nsXPCOM.h"
#include "nsIServiceManager.h"
#include "nsIComponentManager.h"
#include "nsComponentManagerUtils.h"
#include "nsServiceManagerUtils.h"
//
#define TEST_URL "resource://gre/res/strres.properties"
////////////////////////////////////////////////////////////////////////////////////////////////////
//
// let add some locale stuff
//
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
#include "nsILocaleService.h"
#include "nsLocaleCID.h"
//
//
//
nsresult
getCountry(const nsAString &lc_name, nsAString &aCountry)
{
const PRUnichar* data;
PRUint32 length = NS_StringGetData(lc_name, &data);
PRUint32 dash;
for (dash = 0; dash < length; ++dash) {
if (data[dash] == '-')
break;
}
if (dash == length)
return NS_ERROR_FAILURE;
aCountry.Assign(lc_name);
aCountry.Cut(dash, (lc_name.Length()-dash-1));
return NS_OK;
}
nsresult
getUILangCountry(nsAString& aUILang, nsAString& aCountry)
{
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;
}
int
main(int argc, char *argv[])
{
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;
}
PRUnichar *ptrv = nsnull;
// 123
ret = bundle->GetStringFromID(123, &ptrv);
if (NS_FAILED(ret)) {
printf("cannot get string from ID 123, ret=%d\n", ret);
return 1;
}
printf("123=\"%s\"\n", NS_ConvertUTF16toUTF8(ptrv).get());
// file
ret = bundle->GetStringFromName(NS_LITERAL_STRING("file").get(), &ptrv);
if (NS_FAILED(ret)) {
printf("cannot get string from name\n");
return 1;
}
printf("file=\"%s\"\n", NS_ConvertUTF16toUTF8(ptrv).get());
return 0;
}

View File

@ -1,5 +0,0 @@
<!ENTITTY okBtn.label "OK">
<!ENTITTY testBtn.label "Strres Test">
<!ENTITTY cancelBtn.label "Cancel">
<!ENTITTY helpBtn.label "Help">
<!ENTITTY newWindownBtn.label "Open in new window">

View File

@ -1,55 +0,0 @@
<html>
<head>
<title>Strres Test</title>
</head>
<body>
<script>
var sample = Components.classes["@mozilla.org/sample/sample-world;1"].createInstance();
sample = sample.QueryInterface(Components.interfaces.nsISample);
dump("sample = " + sample + "\n");
function get()
{
var field = document.getElementById('Value');
field.value = sample.Value;
}
function set()
{
var field = document.getElementById('Value');
//sample.Value = field.value;
sample.WriteValue(field.value);
}
function poke()
{
var field = document.getElementById('Value');
sample.Poke(field.value);
}
function write()
{
sample.WriteValue("here is what I'm writing!");
var Bundle = JS_GetStrBundle("resource://gre/res/strres.properties", null);
var ostr1 = Bundle.GetStringFromName("file");
dump("\n--** strBundle GetStringFromName file=" + ostr1 + " **--\n");
var ostr2 = Bundle.GetStringFromID(123);
dump("\n--** strBundle GetStringFromID 123=" + ostr2 + " **--\n");
var field = document.getElementById('writeBtn');
field.value = ostr2;
}
</script>
<form name="form">
<input type="button" id="getBtn" value="Get" onclick="get();">
<input type="button" value="Set" onclick="set();">
<input type="button" value="Poke" onclick="poke();">
<input type="text" id="Value">
<input type="button" id="writeBtn" value="Write" onclick="write();">
<form>
</body>
</html>

View File

@ -1,148 +0,0 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Communicator client code, released
* March 31, 1998.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
var browser;
var dialog;
function onLoad() {
dialog = new Object;
dialog.input = document.getElementById( "dialog.input" );
dialog.ok = document.getElementById( "dialog.ok" );
dialog.test = document.getElementById( "dialog.test" );
dialog.testLabel = document.getElementById( "dialog.testLabel" );
dialog.cancel = document.getElementById( "dialog.cancel" );
dialog.help = document.getElementById( "dialog.help" );
dialog.newWindow = document.getElementById( "dialog.newWindow" );
browser = XPAppCoresManager.Find( window.arguments[0] );
if ( !browser ) {
dump( "unable to get browser app core\n" );
window.close();
return;
}
/* Give input field the focus. */
dialog.input.focus();
}
function onTyping( key ) {
// Look for enter key...
if ( key == 13 ) {
// If ok button not disabled, go for it.
if ( !dialog.ok.disabled ) {
open();
}
} else {
// Check for valid input.
if ( dialog.input.value == "" ) {
// No input, disable ok button if enabled.
if ( !dialog.ok.disabled ) {
dialog.ok.setAttribute( "disabled", "" );
}
} else {
// Input, enable ok button if disabled.
if ( dialog.ok.disabled ) {
dialog.ok.removeAttribute( "disabled" );
}
}
}
}
function open() {
if ( dialog.ok.disabled ) {
return;
}
var url = dialog.input.value;
if ( !dialog.newWindow.checked ) {
/* Load url in opener. */
browser.loadUrl( url );
} else {
/* User wants new window. */
window.openDialog( "chrome://navigator/content/", "_blank", "chrome,dialog=no,all", url );
}
/* Close dialog. */
window.close();
}
function choose() {
/* Use existing browser "open" logic. */
browser.openWindow();
window.close();
}
function cancel() {
window.close();
}
function help() {
if ( dialog.help.disabled ) {
return;
}
dump( "openLocation::help() not implemented\n" );
}
function strresTest() {
var Bundle = srGetStrBundle("resource://gre/res/strres.properties");
var ostr1 = Bundle.GetStringFromName("file");
dump("\n--** JS strBundle GetStringFromName file=" + ostr1 +
"len=" + ostr1.length + "**--\n");
var ostr2 = Bundle.GetStringFromID(123);
dump("\n--** JS strBundle GetStringFromID 123=" + ostr2 +
"len=" + ostr2.length + "**--\n");
var ostr3 = Bundle.GetStringFromName("loyal");
dump("\n--** JS strBundle GetStringFromName loyal=" + ostr3 +
"len=" + ostr3.length + "**--\n");
var ostr4 = Bundle.GetStringFromName("trout");
dump("\n--** JS strBundle GetStringFromName eutf8=" + ostr4 +
"len=" + ostr4.length + "**--\n");
var ostr5 = "\u88e6\ue3bb\u8b82"; /* 戻る */
dump("\n--** JS strBundle GetStringFromName escaped=" + ostr5 +
"len=" + ostr5.length + "**--\n");
/* utf-8 \u9cdf\u9b5a */
dialog.ok.setAttribute("value", ostr2);
dialog.test.setAttribute("value", ostr3);
dialog.cancel.setAttribute("value", ostr4);
}

View File

@ -1,67 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!-- DON'T localize: strres test cases -->
<!DOCTYPE window>
<!-- padding 0px is just a workaround for windows not displaying paddings correctly
take it out once this works! -->
<window style=" padding: 0px; width: 100%; height: 100%"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
title="Open File or Location"
onload="onLoad()"
class="dialog">
<html:script type="application/x-javascript" src="chrome://global/content/strres.js" />
<!--html:script type="application/x-javascript" src="resource:/res/strres-test.js" charset="Shift_JIS" / -->
<html:script type="application/x-javascript" src="resource:/res/strres-test.js" />
<hbox flex="100%" style="width:30em;">
<html:div style="width: 45px; margin: 5px;">
<html:img src="chrome://navigator/skin/questionl.gif" width="32" height="32" border="0"/>
</html:div>
<vbox flex="100%" style="height: 100%;">
<html:div style="width: 100%;">Enter the World Wide Web location (URL) you would like to open or
</html:div>
<html:div style="width: 100%; margin-bottom: 0.5em;">use the Choose File... button to select a local file:
</html:div>
<hbox>
<html:input id="dialog.input" flex="100%" onkeyup="onTyping(event.keyCode)"/>
<button onclick="choose()" label="Choose File..." align="left"/>
</hbox>
<hbox style="margin-bottom: 1em;">
<html:input type="checkbox" id="dialog.newWindow"/>
<html:label for="dialog.newWindow">Open in new window</html:label>
<spacer flex="100%"/>
</hbox>
<hbox style="margin-bottom: 1em;">
<spacer flex="100%"/>
<html:label id = "labelPrompt">Strres Test: </html:label>
<html:label id = "testLabel">to be set!!</html:label>
<spacer flex="100%"/>
</hbox>
<spacer flex="100%"/>
<hbox>
<spacer flex="100%"/>
<button id="dialog.ok" label="ok" onclick="open()" align="left"/>
<button id="dialog.test" value="test" onclick="strresTest()" align="left"/>
<button id="dialog.cancel" label="cancel: ??" onclick="cancel()" align="left"/>
<spacer flex="100%"/>
</hbox>
</vbox>
</hbox>
</window>

View File

@ -35,12 +35,12 @@
# ***** END LICENSE BLOCK *****
file=File
loyal=\u5fe0\u5fc3
trout=\u9cdf\u9b5a
edit=Edit
view=View
go=Go
message=Message
trout=鳟魚
edit= Edit
view=View
go=\u0020Go
message=Message\u0020
communicator=Communicator
help=Help
123=onetwothree
hello=Hello %S

View File

@ -0,0 +1,61 @@
/* Tests getting properties from string bundles
*/
const name_file = "file";
const value_file = "File";
const name_loyal = "loyal";
const value_loyal = "\u5fe0\u5fc3"; // tests escaped Unicode
const name_trout = "trout";
const value_trout = "\u9cdf\u9b5a"; // tests UTF-8
const name_edit = "edit";
const value_edit = "Edit"; // tests literal leading spaces are stripped
const name_view = "view";
const value_view = "View"; // tests literal trailing spaces are stripped
const name_go = "go";
const value_go = " Go"; // tests escaped leading spaces are not stripped
const name_message = "message";
const value_message = "Message "; // tests escaped trailing spaces are not stripped
const name_hello = "hello";
const var_hello = "World";
const value_hello = "Hello World"; // tests formatStringFromName with parameter
function run_test() {
var StringBundle =
Components.classes["@mozilla.org/intl/stringbundle;1"]
.getService(Components.interfaces.nsIStringBundleService);
var bundle = StringBundle.createBundle("resource://gre/res/strres.properties");
var bundle_file = bundle.GetStringFromName(name_file);
do_check_eq(bundle_file, value_file);
var bundle_loyal = bundle.GetStringFromName(name_loyal);
do_check_eq(bundle_loyal, value_loyal);
var bundle_trout = bundle.GetStringFromName(name_trout);
do_check_eq(bundle_trout, value_trout);
var bundle_edit = bundle.GetStringFromName(name_edit);
do_check_eq(bundle_edit, value_edit);
var bundle_view = bundle.GetStringFromName(name_view);
do_check_eq(bundle_view, value_view);
var bundle_go = bundle.GetStringFromName(name_go);
do_check_eq(bundle_go, value_go);
var bundle_message = bundle.GetStringFromName(name_message);
do_check_eq(bundle_message, value_message);
var bundle_hello = bundle.formatStringFromName(name_hello, [var_hello], 1);
do_check_eq(bundle_hello, value_hello);
}