Update ooo320-m1
[ooovba.git] / qadevOOo / tests / java / ifc / i18n / _XCalendar.java
blob6a4d70fdd1c0d75411b9fd8b08e81dfdd171ca52
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: _XCalendar.java,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package ifc.i18n;
33 import lib.MultiMethodTest;
35 import com.sun.star.i18n.CalendarDisplayIndex;
36 import com.sun.star.i18n.CalendarFieldIndex;
37 import com.sun.star.i18n.CalendarItem;
38 import com.sun.star.i18n.XCalendar;
39 import com.sun.star.i18n.XLocaleData;
40 import com.sun.star.lang.Locale;
41 import com.sun.star.lang.XMultiServiceFactory;
42 import com.sun.star.uno.UnoRuntime;
44 /**
45 * Testing <code>com.sun.star.i18n.XCalendar</code>
46 * interface methods :
47 * <ul>
48 * <li><code> loadDefaultCalendar()</code></li>
49 * <li><code> loadCalendar()</code></li>
50 * <li><code> getLoadedCalendar()</code></li>
51 * <li><code> getAllCalendars()</code></li>
52 * <li><code> getUniqueID()</code></li>
53 * <li><code> setDateTime()</code></li>
54 * <li><code> getDateTime()</code></li>
55 * <li><code> setValue()</code></li>
56 * <li><code> getValue()</code></li>
57 * <li><code> isValid()</code></li>
58 * <li><code> addValue()</code></li>
59 * <li><code> getFirstDayOfWeek()</code></li>
60 * <li><code> setFirstDayOfWeek()</code></li>
61 * <li><code> setMinimumNumberOfDaysForFirstWeek()</code></li>
62 * <li><code> getMinimumNumberOfDaysForFirstWeek()</code></li>
63 * <li><code> getNumberOfMonthsInYear()</code></li>
64 * <li><code> getNumberOfDaysInWeek()</code></li>
65 * <li><code> getMonths()</code></li>
66 * <li><code> getDays()</code></li>
67 * <li><code> getDisplayName()</code></li>
68 * </ul> <p>
69 * Test is <b> NOT </b> multithread compilant. <p>
70 * @see com.sun.star.i18n.XCalendar
72 public class _XCalendar extends MultiMethodTest {
73 private boolean debug = false;
74 public XCalendar oObj = null;
75 public String[][] calendars;
76 public int[] count;
77 public double newDTime = 1000.75;
78 public short newValue = 2;
79 public short firstDay = 2;
80 public short mdfw = 3;
81 double aOriginalDTime = 0;
82 Locale[] installed_locales;
84 public void before() {
85 XLocaleData locData = null;
86 try {
87 locData = (XLocaleData) UnoRuntime.queryInterface(
88 XLocaleData.class,
89 ((XMultiServiceFactory)tParam.getMSF()).createInstance(
90 "com.sun.star.i18n.LocaleData"));
91 } catch (com.sun.star.uno.Exception e) {
94 installed_locales = locData.getAllInstalledLocaleNames();
95 calendars = new String[installed_locales.length][];
96 count = new int[installed_locales.length];
97 oObj.loadDefaultCalendar(installed_locales[0]);
98 aOriginalDTime = oObj.getDateTime();
100 debug = tParam.getBool("DebugIsActive");
104 * Restore the changed time during the test to the original value of the
105 * machine: has to be correct for the following interface tests.
107 public void after() {
108 oObj.loadDefaultCalendar(installed_locales[0]);
109 oObj.setDateTime(aOriginalDTime);
113 * Loads default calendar for different locales. <p>
114 * Has <b> OK </b> status if method loads calendar, that is
115 * default for a given locale.
117 public void _loadDefaultCalendar() {
118 boolean res = true;
120 for (int i=0; i<installed_locales.length; i++) {
121 String lang = "Language: "+installed_locales[i].Language +
122 ", Country: "+ installed_locales[i].Country +
123 ", Variant: "+ installed_locales[i].Country;
124 oObj.loadDefaultCalendar(installed_locales[i]);
125 if (oObj.getLoadedCalendar().Default) {
126 //log.println(lang + " ... OK");
127 } else {
128 log.println(lang + " ... FAILED");
130 res &= oObj.getLoadedCalendar().Default;
133 tRes.tested("loadDefaultCalendar()", res);
137 * Tries to obtain calendars for a number of locales. <p>
138 * Has <b> OK </b> status if the method returns more than zero calendars for
139 * every locale.
141 public void _getAllCalendars() {
142 boolean res = true;
144 for (int i=0; i<installed_locales.length; i++) {
145 String lang = "Language: "+installed_locales[i].Language +
146 ", Country: "+ installed_locales[i].Country +
147 ", Variant: "+ installed_locales[i].Country;
148 calendars[i] = oObj.getAllCalendars(installed_locales[i]);
149 count[i] = calendars[i].length-1;
150 if (calendars[i].length > 0) {
151 //log.println(lang + " ... OK");
152 } else {
153 log.println(lang + " ... FAILED");
155 res &= (calendars[i].length > 0);
157 tRes.tested("getAllCalendars()", res);
161 * Loads calendars for a number of locales. <p>
162 * Has <b> OK </b> status if loaded calendar names are equal to gotten
163 * calendar names after loading.<p>
164 * The following method tests are to be completed successfully before :
165 * <ul>
166 * <li> <code> getAllCalendars() </code> : gets all calendars for a given
167 * locale </li>
168 * </ul>
170 public void _loadCalendar() {
171 boolean res = true;
172 requiredMethod("getAllCalendars()");
174 for (int i=0; i<installed_locales.length; i++) {
175 String lang = "Language: "+installed_locales[i].Language +
176 ", Country: "+ installed_locales[i].Country +
177 ", Variant: "+ installed_locales[i].Country;
178 oObj.loadCalendar(calendars[i][0], installed_locales[i]);
179 if (calendars[i][0].equals(oObj.getLoadedCalendar().Name)) {
180 //log.println(lang + " ... OK");
181 } else {
182 log.println(lang + " ... FAILED");
184 res &= calendars[i][0].equals(oObj.getLoadedCalendar().Name);
187 tRes.tested("loadCalendar()", res);
191 * Test calls the method, then result is checked. <p>
192 * Has <b> OK </b> status if loaded calendar names are equal to gotten
193 * calendar names after loading.<p>
194 * The following method tests are to be completed successfully before :
195 * <ul>
196 * <li> <code> loadCalendar() </code> : loads calendar using a given name
197 * and locale </li>
198 * </ul>
200 public void _getLoadedCalendar() {
201 boolean res = true;
203 requiredMethod("loadCalendar()");
204 for (int i=0; i<installed_locales.length; i++) {
205 String lang = "Language: "+installed_locales[i].Language +
206 ", Country: "+ installed_locales[i].Country +
207 ", Variant: "+ installed_locales[i].Country;
208 oObj.loadCalendar(calendars[i][0], installed_locales[i]);
209 if (calendars[i][0].equals(oObj.getLoadedCalendar().Name)) {
210 //log.println(lang + " ... OK");
211 } else {
212 log.println(lang + " ... FAILED");
214 res &= calendars[i][0].equals(oObj.getLoadedCalendar().Name);
216 tRes.tested("getLoadedCalendar()", res);
220 * Test calls the method, then result is checked. <p>
221 * Has <b> OK </b> status if the method returns value that's equal to a
222 * calendar name. <p>
223 * The following method tests are to be completed successfully before :
224 * <ul>
225 * <li> <code> loadCalendar() </code> : loads calendar using a given name
226 * and locale </li>
227 * </ul>
229 public void _getUniqueID() {
230 boolean res = true;
231 for (int i=0; i<installed_locales.length; i++) {
232 String lang = "Language: "+installed_locales[i].Language +
233 ", Country: "+ installed_locales[i].Country +
234 ", Variant: "+ installed_locales[i].Country;
235 oObj.loadCalendar(calendars[i][0], installed_locales[i]);
236 String uID = oObj.getUniqueID();
237 if (uID.equals(calendars[i][0])) {
238 //log.println(lang + " ... OK");
239 } else {
240 log.println(lang + " ... FAILED");
242 res &= uID.equals(calendars[i][0]);
245 tRes.tested("getUniqueID()",res);
249 * Test calls the method, then result is checked. <p>
250 * Has <b> OK </b> status if the method returns value, that's equal to
251 * value set before. <p>
254 public void _setDateTime() {
255 boolean res = true;
257 for (int i=0; i<installed_locales.length; i++) {
258 String lang = "Language: "+installed_locales[i].Language +
259 ", Country: "+ installed_locales[i].Country +
260 ", Variant: "+ installed_locales[i].Country;
261 oObj.setDateTime(newDTime);
262 double aDTime = oObj.getDateTime();
263 if (aDTime == newDTime) {
264 //log.println(lang + " ... OK");
265 } else {
266 log.println(lang + " ... FAILED");
268 res &= (aDTime == newDTime);
271 tRes.tested("setDateTime()", res);
275 * Test calls the method, then result is checked. <p>
276 * Has <b> OK </b> status if the method returns value, that's equal to
277 * value set before. <p>
280 public void _getDateTime() {
281 boolean res = true;
283 for (int i=0; i<installed_locales.length; i++) {
284 String lang = "Language: "+installed_locales[i].Language +
285 ", Country: "+ installed_locales[i].Country +
286 ", Variant: "+ installed_locales[i].Country;
287 oObj.setDateTime(newDTime);
288 double aDTime = oObj.getDateTime();
289 if (aDTime == newDTime) {
290 //log.println(lang + " ... OK");
291 } else {
292 log.println(lang + " ... FAILED");
294 res &= (aDTime == newDTime);
296 tRes.tested("getDateTime()", res);
300 * Test calls the method, then result is checked. <p>
301 * Has <b> OK </b> status if the method returns value, that's equal to
302 * value set before. <p>
305 public void _setValue() {
306 boolean res = true;
307 for (int i=0; i<installed_locales.length; i++) {
308 String error = "";
309 String lang = "Language: "+installed_locales[i].Language +
310 ", Country: "+ installed_locales[i].Country +
311 ", Variant: "+ installed_locales[i].Variant +
312 ", Name: "+calendars[i][count[i]];
313 String[] names = new String[]{"DAY_OF_MONTH",
314 "HOUR","MINUTE","SECOND","MILLISECOND",
315 "YEAR","MONTH"};
316 oObj.loadCalendar(calendars[i][count[i]],installed_locales[i]);
317 short[] fields = new short[]{CalendarFieldIndex.DAY_OF_MONTH,
318 CalendarFieldIndex.HOUR,
319 CalendarFieldIndex.MINUTE,
320 CalendarFieldIndex.SECOND,
321 CalendarFieldIndex.MILLISECOND,
322 CalendarFieldIndex.YEAR,
323 CalendarFieldIndex.MONTH
325 for (int k=0; k<fields.length;k++) {
327 oObj.setDateTime(0.0);
329 // save the current values for debug purposes
330 short[] oldValues = new short[fields.length];
331 for (int n=0; n < oldValues.length; n++){
332 oldValues[n] = oObj.getValue(fields[n]);
335 short set = oObj.getValue(fields[k]);
336 if (fields[k] == CalendarFieldIndex.MONTH) set = newValue;
337 oObj.setValue(fields[k],set);
338 short get = oObj.getValue(fields[k]);
339 if (get != set) {
340 if (debug)
341 log.println("ERROR occure: tried to set " + names[k] + " to value " + set);
342 log.println("list of values BEFORE set " + names[k] + " to value " + set + ":");
343 for (int n=0; n < oldValues.length; n++){
344 log.println(names[n] + ":" + oldValues[n]);
346 log.println("list of values AFTER set " + names[k] + " to value " + set + ":");
347 for (int n=0; n < fields.length;n++){
348 log.println(names[n] + ":" + oObj.getValue(fields[n]));
351 error += "failed for "+names[k]+" expected "+
352 set+" gained "+get+" ; \n";
355 if (error.equals("")) {
356 log.println(lang + " ... OK");
357 } else {
358 log.println("*** "+lang + " ... FAILED ***");
359 log.println(error);
361 res &= (error.equals(""));
364 tRes.tested("setValue()", res);
368 * Test calls the method, then result is checked. <p>
369 * Has <b> OK </b> status if the method returns value, that's equal to
370 * value set before. <p>
373 public void _getValue() {
374 boolean res = true;
376 requiredMethod("setValue()");
377 short aValue = oObj.getValue(CalendarFieldIndex.MONTH);
378 res &= (aValue == newValue);
379 if (!res){
380 log.println("the returned value is not the expected value:");
381 log.println("expexted: " + newValue + " returned value: " + aValue);
383 tRes.tested("getValue()", res);
387 * Test calls the method, then result is checked. <p>
388 * Has <b> OK </b> status if value, added by the method is greater than
389 * previously defined "newValue".
390 * <p>
391 * The following method tests are to be completed successfully before :
392 * <ul>
393 * <li> <code> getValue() </code> : gets the value of a field </li>
394 * </ul>
396 public void _addValue() {
397 boolean res = true;
399 requiredMethod("getValue()");
400 oObj.addValue(CalendarFieldIndex.MONTH, 1);
401 short aValue = oObj.getValue(CalendarFieldIndex.MONTH);
402 res &= (aValue > newValue);
403 if (!res){
404 log.println("the returned value is not the expected value:");
405 log.println("expexted: " + newValue + " returned value: " + aValue);
407 tRes.tested("addValue()", res);
411 * Test calls the method. <p>
412 * Has <b> OK </b> status if the method successfully returns
413 * and no exceptions were thrown.
415 public void _setFirstDayOfWeek() {
416 boolean res = true;
418 oObj.setFirstDayOfWeek(firstDay);
419 res &= true;
420 tRes.tested("setFirstDayOfWeek()", res);
424 * Test calls the method, then result is checked. <p>
425 * Has <b> OK </b> status if the method returns value that is equal to
426 * value set before. <p>
427 * The following method tests are to be completed successfully before :
428 * <ul>
429 * <li> <code> setFirstDayOfWeek() </code> : set the first day of a
430 * week</li>
431 * </ul>
433 public void _getFirstDayOfWeek() {
434 boolean res = true;
436 requiredMethod("setFirstDayOfWeek()");
437 short aFirstDayOfWeek = oObj.getFirstDayOfWeek();
438 res &= (aFirstDayOfWeek == firstDay);
439 tRes.tested("getFirstDayOfWeek()", res);
443 * Test calls the method. <p>
444 * Has <b> OK </b> status if the method successfully returns
445 * and no exceptions were thrown.
447 public void _setMinimumNumberOfDaysForFirstWeek() {
448 boolean res = true;
450 oObj.setMinimumNumberOfDaysForFirstWeek(mdfw);
451 res &= true;
452 tRes.tested("setMinimumNumberOfDaysForFirstWeek()", res);
456 * Test calls the method, then result is checked. <p>
457 * Has <b> OK </b> status if the method returns value that is equal to
458 * value set before. <p>
459 * The following method tests are to be completed successfully before :
460 * <ul>
461 * <li> <code> setMinimumNumberOfDaysForFirstWeek() </code> : sets how
462 * many days of a week must reside in the first week of a year</li>
463 * </ul>
465 public void _getMinimumNumberOfDaysForFirstWeek() {
466 boolean res = true;
468 requiredMethod("setMinimumNumberOfDaysForFirstWeek()");
469 short aShort = oObj.getMinimumNumberOfDaysForFirstWeek();
470 res &= (aShort == mdfw);
471 tRes.tested("getMinimumNumberOfDaysForFirstWeek()", res);
475 * Test calls the method, then result is checked. <p>
476 * Has <b> OK </b> status if the method returns 12.
478 public void _getNumberOfMonthsInYear() {
479 boolean res = true;
480 short aShort = oObj.getNumberOfMonthsInYear();
482 res &= (aShort == (short) 12);
483 tRes.tested("getNumberOfMonthsInYear()", res);
487 * Test calls the method, then result is checked. <p>
488 * Has <b> OK </b> status if the method returns 7.
490 public void _getNumberOfDaysInWeek() {
491 boolean res = true;
492 short aShort = oObj.getNumberOfDaysInWeek();
494 res &= (aShort == (short) 7);
495 tRes.tested("getNumberOfDaysInWeek()", res);
499 * Test calls the method, then result is checked. <p>
500 * Has <b> OK </b> status if length of array, returned by the method is 12.
502 public void _getMonths() {
503 boolean res = true;
504 CalendarItem[] months = oObj.getMonths();
506 res &= (months.length == 12);
507 tRes.tested("getMonths()", res);
511 * Test calls the method, then result is checked. <p>
512 * Has <b> OK </b> status if length of array, returned by the method is 7.
514 public void _getDays() {
515 boolean res = true;
516 CalendarItem[] Days = oObj.getDays();
518 res &= (Days.length == 7);
519 tRes.tested("getDays()", res);
523 * After loading calendar, test calls the method, then result is checked.<p>
524 * Has <b> OK </b> status if length of string, returned by the method is 3.
526 public void _getDisplayName() {
527 boolean res = true;
529 oObj.loadCalendar(calendars[0][0],installed_locales[0]);
530 String DisplayName = oObj.getDisplayName(CalendarDisplayIndex.MONTH,
531 newValue, (short) 0);
532 res &= (DisplayName.length() == 3);
533 tRes.tested("getDisplayName()", res);
538 * The test sets obviously wrong value, then calls a method. After that the
539 * test sets correct value, and again calls a method. <p>
540 * Has <b> OK </b> status if the method returns true when valid month is
541 * set, and if the method returns false when set month is not valid.
543 public void _isValid() {
544 boolean res = true;
546 oObj.loadDefaultCalendar(installed_locales[0]);
547 oObj.setValue(CalendarFieldIndex.MONTH, (short) 37);
548 res &= !oObj.isValid();
549 oObj.setValue(CalendarFieldIndex.MONTH, (short) 10);
550 res &= oObj.isValid();
552 tRes.tested("isValid()", res);
556 * Method returns locale for a given language and country.
557 * @param localeIndex index of needed locale.
559 /* public Locale getLocale(int localeIndex) {
560 return new Locale(languages[localeIndex], countries[localeIndex], "");