use of android.icu.util.GlobalizationPreferences in project j2objc by google.
the class GlobalizationPreferencesTest method TestCollator.
@Test
public void TestCollator() {
GlobalizationPreferences gp = new GlobalizationPreferences();
// Set locale - tr
logln("Set locale - tr");
gp.setLocale(new ULocale("tr"));
Collator coll = gp.getCollator();
String locStr = coll.getLocale(ULocale.VALID_LOCALE).toString();
if (!locStr.equals("tr")) {
errln("FAIL: Collator locale is " + locStr + " Expected: tr");
}
// Unsupported collator locale - zun
logln("Set locale - zun");
gp.setLocale(new ULocale("zun"));
coll = gp.getCollator();
locStr = coll.getLocale(ULocale.VALID_LOCALE).toString();
if (!locStr.equals("")) {
errln("FAIL: Collator locale is \"" + locStr + "\" Expected: \"\"(empty)");
}
// Set locales - en_JP, fr, en_US, fr_FR
logln("Set locale - en_JP, fr, en_US, fr_FR");
ULocale[] locales = new ULocale[4];
locales[0] = new ULocale("en_JP");
locales[1] = new ULocale("fr");
locales[2] = new ULocale("en_US");
locales[3] = new ULocale("fr_FR");
gp.setLocales(locales);
coll = gp.getCollator();
locStr = coll.getLocale(ULocale.VALID_LOCALE).toString();
if (!locStr.equals("fr")) {
errln("FAIL: Collator locale is " + locStr + " Expected: fr");
}
// Set explicit Collator
Collator coll1 = Collator.getInstance(new ULocale("it"));
coll1.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
logln("Set collator for it in canonical deconposition mode");
gp.setCollator(coll1);
coll1.setStrength(Collator.IDENTICAL);
coll = gp.getCollator();
locStr = coll.getLocale(ULocale.VALID_LOCALE).toString();
if (!locStr.equals("it")) {
errln("FAIL: Collator locale is " + locStr + " Expected: it");
}
if (coll1.equals(coll)) {
errln("FAIL: setCollator must use a safe copy of a Collator");
}
// Freeze
logln("Freeze this object");
boolean isFrozen = false;
gp.freeze();
try {
gp.setCollator(coll1);
} catch (UnsupportedOperationException uoe) {
logln("setCollator is blocked");
isFrozen = true;
}
if (!isFrozen) {
errln("FAIL: setCollator must be blocked after freeze");
}
// Modifiable clone
logln("cloneAsThawed");
GlobalizationPreferences gp1 = (GlobalizationPreferences) gp.cloneAsThawed();
coll = gp1.getCollator();
locStr = coll.getLocale(ULocale.VALID_LOCALE).toString();
if (!locStr.equals("it")) {
errln("FAIL: Collator locale is " + locStr + " Expected: it");
}
if (coll.getDecomposition() != Collator.CANONICAL_DECOMPOSITION) {
errln("FAIL: Decomposition mode is not CANONICAL_DECOMPOSITION");
}
// Set custom collator again
gp1.setCollator(coll1);
coll = gp1.getCollator();
if (coll.getStrength() != Collator.IDENTICAL) {
errln("FAIL: Strength is not IDENTICAL");
}
}
use of android.icu.util.GlobalizationPreferences in project j2objc by google.
the class GlobalizationPreferencesTest method TestResourceBundle.
@Test
public void TestResourceBundle() {
String baseName = "android.icu.dev.data.resources.TestDataElements";
ResourceBundle rb;
logln("Get a resource bundle " + baseName + " using GlobalizationPreferences initialized by locales - en_GB, en_US");
GlobalizationPreferences gp = new GlobalizationPreferences();
ULocale[] locales = new ULocale[2];
locales[0] = new ULocale("en_GB");
locales[1] = new ULocale("en_US");
gp.setLocales(locales);
try {
rb = gp.getResourceBundle(baseName, Thread.currentThread().getContextClassLoader());
String str = rb.getString("from_en_US");
if (!str.equals("This data comes from en_US")) {
errln("FAIL: from_en_US is not from en_US bundle");
}
} catch (MissingResourceException mre) {
errln("FAIL: Missing resouces");
}
gp.reset();
logln("Get a resource bundle " + baseName + " using GlobalizationPreferences initialized by locales - ja, en_US_California");
locales = new ULocale[2];
locales[0] = new ULocale("ja");
locales[1] = new ULocale("en_US_California");
gp.setLocales(locales);
try {
rb = gp.getResourceBundle(baseName, Thread.currentThread().getContextClassLoader());
String str = rb.getString("from_en_US");
if (!str.equals("This data comes from en_US")) {
errln("FAIL: from_en_US is not from en_US bundle");
}
} catch (MissingResourceException mre) {
errln("FAIL: Missing resouces");
}
logln("Get a resource bundle which does not exist");
boolean bException = false;
try {
rb = gp.getResourceBundle("foo.bar.XXX", Thread.currentThread().getContextClassLoader());
} catch (MissingResourceException mre) {
logln("Missing resource exception for getting resource bundle - foo.bar.XXX");
bException = true;
}
if (!bException) {
errln("FAIL: MissingResourceException must be thrown for RB - foo.bar.XXX");
}
}
use of android.icu.util.GlobalizationPreferences in project j2objc by google.
the class GlobalizationPreferencesTest method TestTimeZone.
@Test
public void TestTimeZone() {
GlobalizationPreferences gp = new GlobalizationPreferences();
// Set locale - zh_CN
logln("Set locale - zh_CN");
gp.setLocale(new ULocale("zh_CN"));
TimeZone tz = gp.getTimeZone();
String tzid = tz.getID();
if (!tzid.equals("Asia/Shanghai")) {
errln("FAIL: Time zone ID is " + tzid + " Expected: Asia/Shanghai");
}
// Set locale - en
logln("Set locale - en");
gp.setLocale(new ULocale("en"));
tz = gp.getTimeZone();
tzid = tz.getID();
if (!tzid.equals("America/New_York")) {
errln("FAIL: Time zone ID is " + tzid + " Expected: America/New_York");
}
// Set territory - GB
logln("Set territory - GB");
gp.setTerritory("GB");
tz = gp.getTimeZone();
tzid = tz.getID();
if (!tzid.equals("Europe/London")) {
errln("FAIL: Time zone ID is " + tzid + " Expected: Europe/London");
}
// Check if getTimeZone returns a safe clone
tz.setID("Bad_ID");
tz = gp.getTimeZone();
tzid = tz.getID();
if (!tzid.equals("Europe/London")) {
errln("FAIL: Time zone ID is " + tzid + " Expected: Europe/London");
}
// Set explicit time zone
TimeZone jst = TimeZone.getTimeZone("Asia/Tokyo");
String customJstId = "Japan_Standard_Time";
jst.setID(customJstId);
gp.setTimeZone(jst);
tz = gp.getTimeZone();
tzid = tz.getID();
if (!tzid.equals(customJstId)) {
errln("FAIL: Time zone ID is " + tzid + " Expected: " + customJstId);
}
// Freeze
logln("Freeze this object");
TimeZone cst = TimeZone.getTimeZone("Europe/Paris");
boolean bFrozen = false;
gp.freeze();
try {
gp.setTimeZone(cst);
} catch (UnsupportedOperationException uoe) {
logln("setTimeZone is blocked");
bFrozen = true;
}
if (!bFrozen) {
errln("FAIL: setTimeZone must be blocked");
}
// Modifiable clone
logln("cloneAsThawed");
GlobalizationPreferences gp1 = (GlobalizationPreferences) gp.cloneAsThawed();
tz = gp1.getTimeZone();
tzid = tz.getID();
if (!tzid.equals(customJstId)) {
errln("FAIL: Time zone ID is " + tzid + " Expected: " + customJstId);
}
// Set explicit time zone
gp1.setTimeZone(cst);
tz = gp1.getTimeZone();
tzid = tz.getID();
if (!tzid.equals(cst.getID())) {
errln("FAIL: Time zone ID is " + tzid + " Expected: " + cst.getID());
}
}
use of android.icu.util.GlobalizationPreferences in project j2objc by google.
the class GlobalizationPreferencesTest method TestDateFormat.
@Test
public void TestDateFormat() {
GlobalizationPreferences gp = new GlobalizationPreferences();
String pattern;
DateFormat df;
// Set unsupported locale - ach
logln("Set locale - ach");
gp.setLocale(new ULocale("ach"));
// Date - short
df = gp.getDateFormat(GlobalizationPreferences.DF_SHORT, GlobalizationPreferences.DF_NONE);
pattern = ((SimpleDateFormat) df).toPattern();
// root pattern must be used
if (!pattern.equals("y-MM-dd")) {
errln("FAIL: SHORT date pattern is " + pattern + " Expected: y-MM-dd");
}
// Set locale - fr, fr_CA, fr_FR
ArrayList lcls = new ArrayList(3);
lcls.add(new ULocale("fr"));
lcls.add(new ULocale("fr_CA"));
lcls.add(new ULocale("fr_FR"));
logln("Set locales - fr, fr_CA, fr_FR");
gp.setLocales(lcls);
// Date - short
df = gp.getDateFormat(GlobalizationPreferences.DF_SHORT, GlobalizationPreferences.DF_NONE);
pattern = ((SimpleDateFormat) df).toPattern();
// fr_CA pattern must be used
if (!pattern.equals("yy-MM-dd")) {
errln("FAIL: SHORT date pattern is " + pattern + " Expected: yy-MM-dd");
}
// Set locale - en_GB
logln("Set locale - en_GB");
gp.setLocale(new ULocale("en_GB"));
// Date - full
df = gp.getDateFormat(GlobalizationPreferences.DF_FULL, GlobalizationPreferences.DF_NONE);
pattern = ((SimpleDateFormat) df).toPattern();
if (!pattern.equals("EEEE, d MMMM y")) {
errln("FAIL: FULL date pattern is " + pattern + " Expected: EEEE, d MMMM y");
}
// Date - long
df = gp.getDateFormat(GlobalizationPreferences.DF_LONG, GlobalizationPreferences.DF_NONE);
pattern = ((SimpleDateFormat) df).toPattern();
if (!pattern.equals("d MMMM y")) {
errln("FAIL: LONG date pattern is " + pattern + " Expected: d MMMM y");
}
// Date - medium
df = gp.getDateFormat(GlobalizationPreferences.DF_MEDIUM, GlobalizationPreferences.DF_NONE);
pattern = ((SimpleDateFormat) df).toPattern();
if (!pattern.equals("d MMM y")) {
errln("FAIL: MEDIUM date pattern is " + pattern + " Expected: d MMM y");
}
// Date - short
df = gp.getDateFormat(GlobalizationPreferences.DF_SHORT, GlobalizationPreferences.DF_NONE);
pattern = ((SimpleDateFormat) df).toPattern();
if (!pattern.equals("dd/MM/y")) {
errln("FAIL: SHORT date pattern is " + pattern + " Expected: dd/MM/y");
}
// Time - full
df = gp.getDateFormat(GlobalizationPreferences.DF_NONE, GlobalizationPreferences.DF_FULL);
pattern = ((SimpleDateFormat) df).toPattern();
if (!pattern.equals("HH:mm:ss zzzz")) {
errln("FAIL: FULL time pattern is " + pattern + " Expected: HH:mm:ss zzzz");
}
// Time - long
df = gp.getDateFormat(GlobalizationPreferences.DF_NONE, GlobalizationPreferences.DF_LONG);
pattern = ((SimpleDateFormat) df).toPattern();
if (!pattern.equals("HH:mm:ss z")) {
errln("FAIL: LONG time pattern is " + pattern + " Expected: HH:mm:ss z");
}
// Time - medium
df = gp.getDateFormat(GlobalizationPreferences.DF_NONE, GlobalizationPreferences.DF_MEDIUM);
pattern = ((SimpleDateFormat) df).toPattern();
if (!pattern.equals("HH:mm:ss")) {
errln("FAIL: MEDIUM time pattern is " + pattern + " Expected: HH:mm:ss");
}
// Time - short
df = gp.getDateFormat(GlobalizationPreferences.DF_NONE, GlobalizationPreferences.DF_SHORT);
pattern = ((SimpleDateFormat) df).toPattern();
if (!pattern.equals("HH:mm")) {
errln("FAIL: SHORT time pattern is " + pattern + " Expected: HH:mm");
}
// Date/Time - full
df = gp.getDateFormat(GlobalizationPreferences.DF_FULL, GlobalizationPreferences.DF_FULL);
pattern = ((SimpleDateFormat) df).toPattern();
if (!pattern.equals("EEEE, d MMMM y 'at' HH:mm:ss zzzz")) {
errln("FAIL: FULL date/time pattern is " + pattern + " Expected: EEEE, d MMMM y 'at' HH:mm:ss zzzz");
}
// Invalid style
boolean illegalArg = false;
try {
df = gp.getDateFormat(-1, GlobalizationPreferences.DF_NONE);
} catch (IllegalArgumentException iae) {
logln("Illegal date style -1");
illegalArg = true;
}
if (!illegalArg) {
errln("FAIL: getDateFormat() must throw IllegalArgumentException for dateStyle -1");
}
illegalArg = false;
try {
df = gp.getDateFormat(GlobalizationPreferences.DF_NONE, GlobalizationPreferences.DF_NONE);
} catch (IllegalArgumentException iae) {
logln("Illegal style - dateStyle:DF_NONE / timeStyle:DF_NONE");
illegalArg = true;
}
if (!illegalArg) {
errln("FAIL: getDateFormat() must throw IllegalArgumentException for dateStyle:DF_NONE/timeStyle:DF_NONE");
}
// Set explicit time zone
logln("Set timezone - America/Sao_Paulo");
TimeZone tz = TimeZone.getTimeZone("America/Sao_Paulo");
gp.setTimeZone(tz);
df = gp.getDateFormat(GlobalizationPreferences.DF_LONG, GlobalizationPreferences.DF_MEDIUM);
String tzid = df.getTimeZone().getID();
if (!tzid.equals("America/Sao_Paulo")) {
errln("FAIL: The DateFormat instance must use timezone America/Sao_Paulo");
}
// Set explicit calendar
logln("Set calendar - japanese");
Calendar jcal = new JapaneseCalendar();
jcal.setTimeZone(TimeZone.getTimeZone("Asia/Tokyo"));
gp.setCalendar(jcal);
df = gp.getDateFormat(GlobalizationPreferences.DF_SHORT, GlobalizationPreferences.DF_SHORT);
Calendar dfCal = df.getCalendar();
if (!(dfCal instanceof JapaneseCalendar)) {
errln("FAIL: The DateFormat instance must use Japanese calendar");
}
// TimeZone must be still America/Sao_Paulo
tzid = df.getTimeZone().getID();
if (!tzid.equals("America/Sao_Paulo")) {
errln("FAIL: The DateFormat instance must use timezone America/Sao_Paulo");
}
// Set explicit DateFormat
logln("Set explicit date format - full date");
DateFormat customFD = DateFormat.getDateInstance(new IslamicCalendar(), DateFormat.FULL, new ULocale("ar_SA"));
customFD.setTimeZone(TimeZone.getTimeZone("Asia/Riyadh"));
gp.setDateFormat(GlobalizationPreferences.DF_FULL, GlobalizationPreferences.DF_NONE, customFD);
df = gp.getDateFormat(GlobalizationPreferences.DF_FULL, GlobalizationPreferences.DF_NONE);
dfCal = df.getCalendar();
if (!(dfCal instanceof IslamicCalendar)) {
errln("FAIL: The DateFormat instance must use Islamic calendar");
}
// TimeZone in the custom DateFormat is overridden by GP's timezone setting
tzid = df.getTimeZone().getID();
if (!tzid.equals("America/Sao_Paulo")) {
errln("FAIL: The DateFormat instance must use timezone America/Sao_Paulo");
}
// Freeze
logln("Freeze this object");
gp.freeze();
DateFormat customLD = DateFormat.getDateInstance(new BuddhistCalendar(), DateFormat.LONG, new ULocale("th"));
customLD.setTimeZone(TimeZone.getTimeZone("Asia/Bangkok"));
boolean isFrozen = false;
try {
gp.setDateFormat(GlobalizationPreferences.DF_LONG, GlobalizationPreferences.DF_NONE, customLD);
} catch (UnsupportedOperationException uoe) {
logln("setDateFormat is blocked");
isFrozen = true;
}
if (!isFrozen) {
errln("FAIL: setDateFormat must be blocked after frozen");
}
// Modifiable clone
logln("cloneAsThawed");
GlobalizationPreferences gp1 = (GlobalizationPreferences) gp.cloneAsThawed();
gp1.setDateFormat(GlobalizationPreferences.DF_LONG, GlobalizationPreferences.DF_NONE, customLD);
df = gp1.getDateFormat(GlobalizationPreferences.DF_SHORT, GlobalizationPreferences.DF_SHORT);
dfCal = df.getCalendar();
if (!(dfCal instanceof JapaneseCalendar)) {
errln("FAIL: The DateFormat instance must use Japanese calendar");
}
// TimeZone must be still America/Sao_Paulo
tzid = df.getTimeZone().getID();
if (!tzid.equals("America/Sao_Paulo")) {
errln("FAIL: The DateFormat instance must use timezone America/Sao_Paulo");
}
df = gp1.getDateFormat(GlobalizationPreferences.DF_LONG, GlobalizationPreferences.DF_NONE);
dfCal = df.getCalendar();
if (!(dfCal instanceof BuddhistCalendar)) {
errln("FAIL: The DateFormat instance must use Buddhist calendar");
}
// TimeZone must be still America/Sao_Paulo
tzid = df.getTimeZone().getID();
if (!tzid.equals("America/Sao_Paulo")) {
errln("FAIL: The DateFormat instance must use timezone America/Sao_Paulo");
}
}
use of android.icu.util.GlobalizationPreferences in project j2objc by google.
the class GlobalizationPreferencesTest method TestBreakIterator.
@Test
public void TestBreakIterator() {
GlobalizationPreferences gp = new GlobalizationPreferences();
// Unsupported break iterator locale - aar
logln("Set locale - aar");
gp.setLocale(new ULocale("aar"));
BreakIterator brk = gp.getBreakIterator(GlobalizationPreferences.BI_LINE);
String locStr = brk.getLocale(ULocale.VALID_LOCALE).toString();
if (!locStr.equals("root")) {
errln("FAIL: Line break iterator locale is " + locStr + " Expected: root");
}
// Set locale - es
logln("Set locale - es");
gp.setLocale(new ULocale("es"));
brk = gp.getBreakIterator(GlobalizationPreferences.BI_CHARACTER);
locStr = brk.getLocale(ULocale.VALID_LOCALE).toString();
if (!locStr.equals("es")) {
errln("FAIL: Character break iterator locale is " + locStr + " Expected: es");
}
// Set explicit break sentence iterator
logln("Set break iterator for sentence using locale hu_HU");
BreakIterator brk1 = BreakIterator.getSentenceInstance(new ULocale("hu_HU"));
gp.setBreakIterator(GlobalizationPreferences.BI_SENTENCE, brk1);
brk = gp.getBreakIterator(GlobalizationPreferences.BI_SENTENCE);
/* TODO: JB#5210
locStr = brk.getLocale(ULocale.VALID_LOCALE).toString();
if (!locStr.equals("hu_HU")) {
errln("FAIL: Sentence break locale is " + locStr + " Expected: hu_HU");
}
*/
brk.setText("This is a test case. Is this a new instance?");
brk.next();
if (brk1.current() == brk.current()) {
errln("FAIL: getBreakIterator must return a new instance");
}
// Illegal argument
logln("Get break iterator type 100");
boolean illegalArg = false;
try {
brk = gp.getBreakIterator(100);
} catch (IllegalArgumentException iae) {
logln("Break iterator type 100 is illegal");
illegalArg = true;
}
if (!illegalArg) {
errln("FAIL: getBreakIterator must throw IllegalArgumentException for type 100");
}
logln("Set break iterator type -1");
illegalArg = false;
try {
gp.setBreakIterator(-1, brk1);
} catch (IllegalArgumentException iae) {
logln("Break iterator type -1 is illegal");
illegalArg = true;
}
if (!illegalArg) {
errln("FAIL: getBreakIterator must throw IllegalArgumentException for type -1");
}
// Freeze
logln("Freeze this object");
BreakIterator brk2 = BreakIterator.getTitleInstance(new ULocale("es_MX"));
boolean isFrozen = false;
gp.freeze();
try {
gp.setBreakIterator(GlobalizationPreferences.BI_TITLE, brk2);
} catch (UnsupportedOperationException uoe) {
logln("setBreakIterator is blocked");
isFrozen = true;
}
if (!isFrozen) {
errln("FAIL: setBreakIterator must be blocked after frozen");
}
// Modifiable clone
logln("cloneAsThawed");
GlobalizationPreferences gp1 = (GlobalizationPreferences) gp.cloneAsThawed();
brk = gp1.getBreakIterator(GlobalizationPreferences.BI_WORD);
/* TODO: JB#5383
locStr = brk.getLocale(ULocale.VALID_LOCALE).toString();
if (!locStr.equals("es")) {
errln("FAIL: Word break iterator locale is " + locStr + " Expected: es");
}
*/
ULocale frFR = new ULocale("fr_FR");
BreakIterator brkC = BreakIterator.getCharacterInstance(frFR);
BreakIterator brkW = BreakIterator.getWordInstance(frFR);
BreakIterator brkL = BreakIterator.getLineInstance(frFR);
BreakIterator brkS = BreakIterator.getSentenceInstance(frFR);
BreakIterator brkT = BreakIterator.getTitleInstance(frFR);
gp1.setBreakIterator(GlobalizationPreferences.BI_CHARACTER, brkC);
gp1.setBreakIterator(GlobalizationPreferences.BI_WORD, brkW);
gp1.setBreakIterator(GlobalizationPreferences.BI_LINE, brkL);
gp1.setBreakIterator(GlobalizationPreferences.BI_SENTENCE, brkS);
gp1.setBreakIterator(GlobalizationPreferences.BI_TITLE, brkT);
/* TODO: JB#5210
locStr = brkC.getLocale(ULocale.VALID_LOCALE).toString();
if (!locStr.equals("ja_JP")) {
errln("FAIL: Character break iterator locale is " + locStr + " Expected: fr_FR");
}
locStr = brkW.getLocale(ULocale.VALID_LOCALE).toString();
if (!locStr.equals("ja_JP")) {
errln("FAIL: Word break iterator locale is " + locStr + " Expected: fr_FR");
}
locStr = brkL.getLocale(ULocale.VALID_LOCALE).toString();
if (!locStr.equals("ja_JP")) {
errln("FAIL: Line break iterator locale is " + locStr + " Expected: fr_FR");
}
locStr = brkS.getLocale(ULocale.VALID_LOCALE).toString();
if (!locStr.equals("ja_JP")) {
errln("FAIL: Sentence break iterator locale is " + locStr + " Expected: fr_FR");
}
locStr = brkT.getLocale(ULocale.VALID_LOCALE).toString();
if (!locStr.equals("ja_JP")) {
errln("FAIL: Title break iterator locale is " + locStr + " Expected: fr_FR");
}
*/
}
Aggregations