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 .
21 import lib
.MultiMethodTest
;
23 import com
.sun
.star
.i18n
.CalendarDisplayIndex
;
24 import com
.sun
.star
.i18n
.CalendarFieldIndex
;
25 import com
.sun
.star
.i18n
.CalendarItem
;
26 import com
.sun
.star
.i18n
.XCalendar
;
27 import com
.sun
.star
.i18n
.XLocaleData
;
28 import com
.sun
.star
.lang
.Locale
;
29 import com
.sun
.star
.lang
.XMultiServiceFactory
;
30 import com
.sun
.star
.uno
.UnoRuntime
;
33 * Testing <code>com.sun.star.i18n.XCalendar</code>
36 * <li><code> loadDefaultCalendar()</code></li>
37 * <li><code> loadCalendar()</code></li>
38 * <li><code> getLoadedCalendar()</code></li>
39 * <li><code> getAllCalendars()</code></li>
40 * <li><code> getUniqueID()</code></li>
41 * <li><code> setDateTime()</code></li>
42 * <li><code> getDateTime()</code></li>
43 * <li><code> setValue()</code></li>
44 * <li><code> getValue()</code></li>
45 * <li><code> isValid()</code></li>
46 * <li><code> addValue()</code></li>
47 * <li><code> getFirstDayOfWeek()</code></li>
48 * <li><code> setFirstDayOfWeek()</code></li>
49 * <li><code> setMinimumNumberOfDaysForFirstWeek()</code></li>
50 * <li><code> getMinimumNumberOfDaysForFirstWeek()</code></li>
51 * <li><code> getNumberOfMonthsInYear()</code></li>
52 * <li><code> getNumberOfDaysInWeek()</code></li>
53 * <li><code> getMonths()</code></li>
54 * <li><code> getDays()</code></li>
55 * <li><code> getDisplayName()</code></li>
57 * Test is <b> NOT </b> multithread compilant. <p>
58 * @see com.sun.star.i18n.XCalendar
60 public class _XCalendar
extends MultiMethodTest
{
61 private boolean debug
= false;
62 public XCalendar oObj
= null;
63 public String
[][] calendars
;
65 public double newDTime
= 1000.75;
66 public short newValue
= 2;
67 public short firstDay
= 2;
68 public short mdfw
= 3;
69 double aOriginalDTime
= 0;
70 Locale
[] installed_locales
;
72 public void before() {
73 XLocaleData locData
= null;
75 locData
= UnoRuntime
.queryInterface(
77 ((XMultiServiceFactory
)tParam
.getMSF()).createInstance(
78 "com.sun.star.i18n.LocaleData"));
79 } catch (com
.sun
.star
.uno
.Exception e
) {
82 installed_locales
= locData
.getAllInstalledLocaleNames();
83 calendars
= new String
[installed_locales
.length
][];
84 count
= new int[installed_locales
.length
];
85 oObj
.loadDefaultCalendar(installed_locales
[0]);
86 aOriginalDTime
= oObj
.getDateTime();
88 debug
= tParam
.getBool("DebugIsActive");
92 * Restore the changed time during the test to the original value of the
93 * machine: has to be correct for the following interface tests.
96 oObj
.loadDefaultCalendar(installed_locales
[0]);
97 oObj
.setDateTime(aOriginalDTime
);
101 * Loads default calendar for different locales. <p>
102 * Has <b> OK </b> status if method loads calendar, that is
103 * default for a given locale.
105 public void _loadDefaultCalendar() {
108 for (int i
=0; i
<installed_locales
.length
; i
++) {
109 String lang
= "Language: "+installed_locales
[i
].Language
+
110 ", Country: "+ installed_locales
[i
].Country
+
111 ", Variant: "+ installed_locales
[i
].Country
;
112 oObj
.loadDefaultCalendar(installed_locales
[i
]);
113 if (oObj
.getLoadedCalendar().Default
) {
114 //log.println(lang + " ... OK");
116 log
.println(lang
+ " ... FAILED");
118 res
&= oObj
.getLoadedCalendar().Default
;
121 tRes
.tested("loadDefaultCalendar()", res
);
125 * Tries to obtain calendars for a number of locales. <p>
126 * Has <b> OK </b> status if the method returns more than zero calendars for
129 public void _getAllCalendars() {
132 for (int i
=0; i
<installed_locales
.length
; i
++) {
133 String lang
= "Language: "+installed_locales
[i
].Language
+
134 ", Country: "+ installed_locales
[i
].Country
+
135 ", Variant: "+ installed_locales
[i
].Country
;
136 calendars
[i
] = oObj
.getAllCalendars(installed_locales
[i
]);
137 count
[i
] = calendars
[i
].length
-1;
138 if (calendars
[i
].length
> 0) {
139 //log.println(lang + " ... OK");
141 log
.println(lang
+ " ... FAILED");
143 res
&= (calendars
[i
].length
> 0);
145 tRes
.tested("getAllCalendars()", res
);
149 * Loads calendars for a number of locales. <p>
150 * Has <b> OK </b> status if loaded calendar names are equal to gotten
151 * calendar names after loading.<p>
152 * The following method tests are to be completed successfully before :
154 * <li> <code> getAllCalendars() </code> : gets all calendars for a given
158 public void _loadCalendar() {
160 requiredMethod("getAllCalendars()");
162 for (int i
=0; i
<installed_locales
.length
; i
++) {
163 String lang
= "Language: "+installed_locales
[i
].Language
+
164 ", Country: "+ installed_locales
[i
].Country
+
165 ", Variant: "+ installed_locales
[i
].Country
;
166 oObj
.loadCalendar(calendars
[i
][0], installed_locales
[i
]);
167 if (calendars
[i
][0].equals(oObj
.getLoadedCalendar().Name
)) {
168 //log.println(lang + " ... OK");
170 log
.println(lang
+ " ... FAILED");
172 res
&= calendars
[i
][0].equals(oObj
.getLoadedCalendar().Name
);
175 tRes
.tested("loadCalendar()", res
);
179 * Test calls the method, then result is checked. <p>
180 * Has <b> OK </b> status if loaded calendar names are equal to gotten
181 * calendar names after loading.<p>
182 * The following method tests are to be completed successfully before :
184 * <li> <code> loadCalendar() </code> : loads calendar using a given name
188 public void _getLoadedCalendar() {
191 requiredMethod("loadCalendar()");
192 for (int i
=0; i
<installed_locales
.length
; i
++) {
193 String lang
= "Language: "+installed_locales
[i
].Language
+
194 ", Country: "+ installed_locales
[i
].Country
+
195 ", Variant: "+ installed_locales
[i
].Country
;
196 oObj
.loadCalendar(calendars
[i
][0], installed_locales
[i
]);
197 if (calendars
[i
][0].equals(oObj
.getLoadedCalendar().Name
)) {
198 //log.println(lang + " ... OK");
200 log
.println(lang
+ " ... FAILED");
202 res
&= calendars
[i
][0].equals(oObj
.getLoadedCalendar().Name
);
204 tRes
.tested("getLoadedCalendar()", res
);
208 * Test calls the method, then result is checked. <p>
209 * Has <b> OK </b> status if the method returns value that's equal to a
211 * The following method tests are to be completed successfully before :
213 * <li> <code> loadCalendar() </code> : loads calendar using a given name
217 public void _getUniqueID() {
219 for (int i
=0; i
<installed_locales
.length
; i
++) {
220 String lang
= "Language: "+installed_locales
[i
].Language
+
221 ", Country: "+ installed_locales
[i
].Country
+
222 ", Variant: "+ installed_locales
[i
].Country
;
223 oObj
.loadCalendar(calendars
[i
][0], installed_locales
[i
]);
224 String uID
= oObj
.getUniqueID();
225 if (uID
.equals(calendars
[i
][0])) {
226 //log.println(lang + " ... OK");
228 log
.println(lang
+ " ... FAILED");
230 res
&= uID
.equals(calendars
[i
][0]);
233 tRes
.tested("getUniqueID()",res
);
237 * Test calls the method, then result is checked. <p>
238 * Has <b> OK </b> status if the method returns value, that's equal to
239 * value set before. <p>
242 public void _setDateTime() {
245 for (int i
=0; i
<installed_locales
.length
; i
++) {
246 String lang
= "Language: "+installed_locales
[i
].Language
+
247 ", Country: "+ installed_locales
[i
].Country
+
248 ", Variant: "+ installed_locales
[i
].Country
;
249 oObj
.setDateTime(newDTime
);
250 double aDTime
= oObj
.getDateTime();
251 if (aDTime
== newDTime
) {
252 //log.println(lang + " ... OK");
254 log
.println(lang
+ " ... FAILED");
256 res
&= (aDTime
== newDTime
);
259 tRes
.tested("setDateTime()", res
);
263 * Test calls the method, then result is checked. <p>
264 * Has <b> OK </b> status if the method returns value, that's equal to
265 * value set before. <p>
268 public void _getDateTime() {
271 for (int i
=0; i
<installed_locales
.length
; i
++) {
272 String lang
= "Language: "+installed_locales
[i
].Language
+
273 ", Country: "+ installed_locales
[i
].Country
+
274 ", Variant: "+ installed_locales
[i
].Country
;
275 oObj
.setDateTime(newDTime
);
276 double aDTime
= oObj
.getDateTime();
277 if (aDTime
== newDTime
) {
278 //log.println(lang + " ... OK");
280 log
.println(lang
+ " ... FAILED");
282 res
&= (aDTime
== newDTime
);
284 tRes
.tested("getDateTime()", res
);
288 * Test calls the method, then result is checked. <p>
289 * Has <b> OK </b> status if the method returns value, that's equal to
290 * value set before. <p>
293 public void _setValue() {
295 for (int i
=0; i
<installed_locales
.length
; i
++) {
297 String lang
= "Language: "+installed_locales
[i
].Language
+
298 ", Country: "+ installed_locales
[i
].Country
+
299 ", Variant: "+ installed_locales
[i
].Variant
+
300 ", Name: "+calendars
[i
][count
[i
]];
301 String
[] names
= new String
[]{"DAY_OF_MONTH",
302 "HOUR","MINUTE","SECOND","MILLISECOND",
304 oObj
.loadCalendar(calendars
[i
][count
[i
]],installed_locales
[i
]);
305 short[] fields
= new short[]{CalendarFieldIndex
.DAY_OF_MONTH
,
306 CalendarFieldIndex
.HOUR
,
307 CalendarFieldIndex
.MINUTE
,
308 CalendarFieldIndex
.SECOND
,
309 CalendarFieldIndex
.MILLISECOND
,
310 CalendarFieldIndex
.YEAR
,
311 CalendarFieldIndex
.MONTH
313 for (int k
=0; k
<fields
.length
;k
++) {
315 oObj
.setDateTime(0.0);
317 // save the current values for debug purposes
318 short[] oldValues
= new short[fields
.length
];
319 for (int n
=0; n
< oldValues
.length
; n
++){
320 oldValues
[n
] = oObj
.getValue(fields
[n
]);
323 short set
= oObj
.getValue(fields
[k
]);
324 if (fields
[k
] == CalendarFieldIndex
.MONTH
) set
= newValue
;
325 oObj
.setValue(fields
[k
],set
);
326 short get
= oObj
.getValue(fields
[k
]);
329 log
.println("ERROR occurred: tried to set " + names
[k
] + " to value " + set
);
330 log
.println("list of values BEFORE set " + names
[k
] + " to value " + set
+ ":");
331 for (int n
=0; n
< oldValues
.length
; n
++){
332 log
.println(names
[n
] + ":" + oldValues
[n
]);
334 log
.println("list of values AFTER set " + names
[k
] + " to value " + set
+ ":");
335 for (int n
=0; n
< fields
.length
;n
++){
336 log
.println(names
[n
] + ":" + oObj
.getValue(fields
[n
]));
339 error
+= "failed for "+names
[k
]+" expected "+
340 set
+" gained "+get
+" ; \n";
343 if (error
.equals("")) {
344 log
.println(lang
+ " ... OK");
346 log
.println("*** "+lang
+ " ... FAILED ***");
349 res
&= (error
.equals(""));
352 tRes
.tested("setValue()", res
);
356 * Test calls the method, then result is checked. <p>
357 * Has <b> OK </b> status if the method returns value, that's equal to
358 * value set before. <p>
361 public void _getValue() {
364 requiredMethod("setValue()");
365 short aValue
= oObj
.getValue(CalendarFieldIndex
.MONTH
);
366 res
&= (aValue
== newValue
);
368 log
.println("the returned value is not the expected value:");
369 log
.println("expexted: " + newValue
+ " returned value: " + aValue
);
371 tRes
.tested("getValue()", res
);
375 * Test calls the method, then result is checked. <p>
376 * Has <b> OK </b> status if value, added by the method is greater than
377 * previously defined "newValue".
379 * The following method tests are to be completed successfully before :
381 * <li> <code> getValue() </code> : gets the value of a field </li>
384 public void _addValue() {
387 requiredMethod("getValue()");
388 oObj
.addValue(CalendarFieldIndex
.MONTH
, 1);
389 short aValue
= oObj
.getValue(CalendarFieldIndex
.MONTH
);
390 res
&= (aValue
> newValue
);
392 log
.println("the returned value is not the expected value:");
393 log
.println("expexted: " + newValue
+ " returned value: " + aValue
);
395 tRes
.tested("addValue()", res
);
399 * Test calls the method. <p>
400 * Has <b> OK </b> status if the method successfully returns
401 * and no exceptions were thrown.
403 public void _setFirstDayOfWeek() {
406 oObj
.setFirstDayOfWeek(firstDay
);
408 tRes
.tested("setFirstDayOfWeek()", res
);
412 * Test calls the method, then result is checked. <p>
413 * Has <b> OK </b> status if the method returns value that is equal to
414 * value set before. <p>
415 * The following method tests are to be completed successfully before :
417 * <li> <code> setFirstDayOfWeek() </code> : set the first day of a
421 public void _getFirstDayOfWeek() {
424 requiredMethod("setFirstDayOfWeek()");
425 short aFirstDayOfWeek
= oObj
.getFirstDayOfWeek();
426 res
&= (aFirstDayOfWeek
== firstDay
);
427 tRes
.tested("getFirstDayOfWeek()", res
);
431 * Test calls the method. <p>
432 * Has <b> OK </b> status if the method successfully returns
433 * and no exceptions were thrown.
435 public void _setMinimumNumberOfDaysForFirstWeek() {
438 oObj
.setMinimumNumberOfDaysForFirstWeek(mdfw
);
440 tRes
.tested("setMinimumNumberOfDaysForFirstWeek()", res
);
444 * Test calls the method, then result is checked. <p>
445 * Has <b> OK </b> status if the method returns value that is equal to
446 * value set before. <p>
447 * The following method tests are to be completed successfully before :
449 * <li> <code> setMinimumNumberOfDaysForFirstWeek() </code> : sets how
450 * many days of a week must reside in the first week of a year</li>
453 public void _getMinimumNumberOfDaysForFirstWeek() {
456 requiredMethod("setMinimumNumberOfDaysForFirstWeek()");
457 short aShort
= oObj
.getMinimumNumberOfDaysForFirstWeek();
458 res
&= (aShort
== mdfw
);
459 tRes
.tested("getMinimumNumberOfDaysForFirstWeek()", res
);
463 * Test calls the method, then result is checked. <p>
464 * Has <b> OK </b> status if the method returns 12.
466 public void _getNumberOfMonthsInYear() {
468 short aShort
= oObj
.getNumberOfMonthsInYear();
470 res
&= (aShort
== (short) 12);
471 tRes
.tested("getNumberOfMonthsInYear()", res
);
475 * Test calls the method, then result is checked. <p>
476 * Has <b> OK </b> status if the method returns 7.
478 public void _getNumberOfDaysInWeek() {
480 short aShort
= oObj
.getNumberOfDaysInWeek();
482 res
&= (aShort
== (short) 7);
483 tRes
.tested("getNumberOfDaysInWeek()", res
);
487 * Test calls the method, then result is checked. <p>
488 * Has <b> OK </b> status if length of array, returned by the method is 12.
490 public void _getMonths() {
492 CalendarItem
[] months
= oObj
.getMonths();
494 res
&= (months
.length
== 12);
495 tRes
.tested("getMonths()", 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 7.
502 public void _getDays() {
504 CalendarItem
[] Days
= oObj
.getDays();
506 res
&= (Days
.length
== 7);
507 tRes
.tested("getDays()", res
);
511 * After loading calendar, test calls the method, then result is checked.<p>
512 * Has <b> OK </b> status if length of string, returned by the method is 3.
514 public void _getDisplayName() {
517 oObj
.loadCalendar(calendars
[0][0],installed_locales
[0]);
518 String DisplayName
= oObj
.getDisplayName(CalendarDisplayIndex
.MONTH
,
519 newValue
, (short) 0);
520 res
&= (DisplayName
.length() == 3);
521 tRes
.tested("getDisplayName()", res
);
526 * The test sets obviously wrong value, then calls a method. After that the
527 * test sets correct value, and again calls a method. <p>
528 * Has <b> OK </b> status if the method returns true when valid month is
529 * set, and if the method returns false when set month is not valid.
531 public void _isValid() {
534 oObj
.loadDefaultCalendar(installed_locales
[0]);
535 oObj
.setValue(CalendarFieldIndex
.MONTH
, (short) 37);
536 res
&= !oObj
.isValid();
537 oObj
.setValue(CalendarFieldIndex
.MONTH
, (short) 10);
538 res
&= oObj
.isValid();
540 tRes
.tested("isValid()", res
);
544 * Method returns locale for a given language and country.
545 * @param localeIndex index of needed locale.
547 /* public Locale getLocale(int localeIndex) {
548 return new Locale(languages[localeIndex], countries[localeIndex], "");