Branch libreoffice-5-0-4
[LibreOffice.git] / qadevOOo / tests / java / ifc / i18n / _XExtendedCalendar.java
blob99d26e7b9a0b5bc6ba28bc03631d6df6067faf32
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package ifc.i18n;
20 import com.sun.star.i18n.CalendarDisplayCode;
21 import com.sun.star.i18n.NativeNumberMode;
22 import com.sun.star.i18n.XExtendedCalendar;
23 import com.sun.star.i18n.XLocaleData;
24 import com.sun.star.lang.Locale;
25 import com.sun.star.uno.UnoRuntime;
26 import java.text.SimpleDateFormat;
27 import java.util.Calendar;
28 import java.util.Date;
29 import java.util.GregorianCalendar;
30 import lib.MultiMethodTest;
32 /**
35 public class _XExtendedCalendar extends MultiMethodTest {
36 public XExtendedCalendar oObj = null;
37 boolean useUSENLocale = false;
38 /**
39 * Load a calendar
41 @Override
42 public void before() {
43 Locale[] installed_locales = null;
44 XLocaleData locData = null;
45 try {
46 locData = UnoRuntime.queryInterface(
47 XLocaleData.class,
48 tParam.getMSF().createInstance(
49 "com.sun.star.i18n.LocaleData"));
50 } catch (com.sun.star.uno.Exception e) {
53 installed_locales = locData.getAllInstalledLocaleNames();
54 // use first Locale as fallback, if US-English is not found
55 Locale lo = installed_locales[0];
56 for (int i=0; i<installed_locales.length; i++) {
57 // search for "en" and "US"
58 if (installed_locales[i].Language.equals("en") &&
59 installed_locales[i].Country.equals("US")) {
60 lo = installed_locales[i];
61 useUSENLocale = true;
64 log.println("Choose Locale: '" + lo.Language + "', '" + lo.Country + "'");
65 oObj.loadDefaultCalendar(lo);
69 public void _getDisplayString() {
70 // against regression: the current state is the right one.
71 boolean result = true;
72 String[] displayString = new String[6];
73 // build the defaults with the Java Calendar functions
74 String[] expectedStringResult = new String[6];
75 Calendar cal = new GregorianCalendar();
76 Date actualDate = cal.getTime();
78 SimpleDateFormat sdf = getSDF("yy");
79 expectedStringResult[0] = "AD" + sdf.format(actualDate);
81 sdf = getSDF("yyyy");
82 expectedStringResult[1] = "AD" + sdf.format(actualDate);
84 sdf = getSDF("MM");
85 expectedStringResult[2] = sdf.format(actualDate);
87 int month = cal.get(Calendar.MONTH) + 1;
88 String quarter = "Q1";
89 String longQuarter = "1st quarter";
90 if (month > 3 && month < 7) { quarter = "Q2"; longQuarter = "2nd quarter"; }
91 else if (month > 6 && month < 10) { quarter = "Q3"; longQuarter = "3rd quarter"; }
92 else if (month > 10 && month < 13) {quarter = "Q4"; longQuarter = "4th quarter"; }
93 expectedStringResult[3] = quarter;
94 expectedStringResult[4] = longQuarter;
96 sdf = getSDF("MMMM");
97 expectedStringResult[5] = sdf.format(actualDate);
99 displayString[0] = oObj.getDisplayString(CalendarDisplayCode.SHORT_YEAR_AND_ERA, NativeNumberMode.NATNUM0);
100 displayString[1] = oObj.getDisplayString(CalendarDisplayCode.LONG_YEAR_AND_ERA, NativeNumberMode.NATNUM0);
101 displayString[2] = oObj.getDisplayString(CalendarDisplayCode.LONG_MONTH, NativeNumberMode.NATNUM0);
102 displayString[3] = oObj.getDisplayString(CalendarDisplayCode.SHORT_QUARTER, NativeNumberMode.NATNUM0);
103 displayString[4] = oObj.getDisplayString(CalendarDisplayCode.LONG_QUARTER, NativeNumberMode.NATNUM0);
104 displayString[5] = oObj.getDisplayString(CalendarDisplayCode.LONG_MONTH_NAME, NativeNumberMode.NATNUM0);
106 for (int i=0; i<displayString.length; i++) {
107 boolean locResult = false;
108 if (useUSENLocale) {
109 locResult = displayString[i].equals(expectedStringResult[i]);
110 if (!locResult)
111 log.println("getDisplayString() result " + i + ": '" + displayString[i]
112 + "', expected: '" + expectedStringResult[i] + "'");
113 result &= locResult;
115 else { // no defaults for other locales, just expect a String
116 locResult &= displayString[i] != null;
117 if (!locResult)
118 log.println("getDisplayString() result " + i + " was 'null'");
119 result &= locResult;
122 tRes.tested("getDisplayString()", result);
125 private SimpleDateFormat getSDF(String format){
126 if (useUSENLocale) return new SimpleDateFormat(format, java.util.Locale.US);
127 return new SimpleDateFormat(format);