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
.uno
.UnoRuntime
;
32 * Testing <code>com.sun.star.i18n.XCalendar</code>
35 * <li><code> loadDefaultCalendar()</code></li>
36 * <li><code> loadCalendar()</code></li>
37 * <li><code> getLoadedCalendar()</code></li>
38 * <li><code> getAllCalendars()</code></li>
39 * <li><code> getUniqueID()</code></li>
40 * <li><code> setDateTime()</code></li>
41 * <li><code> getDateTime()</code></li>
42 * <li><code> setValue()</code></li>
43 * <li><code> getValue()</code></li>
44 * <li><code> isValid()</code></li>
45 * <li><code> addValue()</code></li>
46 * <li><code> getFirstDayOfWeek()</code></li>
47 * <li><code> setFirstDayOfWeek()</code></li>
48 * <li><code> setMinimumNumberOfDaysForFirstWeek()</code></li>
49 * <li><code> getMinimumNumberOfDaysForFirstWeek()</code></li>
50 * <li><code> getNumberOfMonthsInYear()</code></li>
51 * <li><code> getNumberOfDaysInWeek()</code></li>
52 * <li><code> getMonths()</code></li>
53 * <li><code> getDays()</code></li>
54 * <li><code> getDisplayName()</code></li>
56 * Test is <b> NOT </b> multithread compliant. <p>
57 * @see com.sun.star.i18n.XCalendar
59 public class _XCalendar
extends MultiMethodTest
{
60 private boolean debug
= false;
61 public XCalendar oObj
= null;
62 public String
[][] calendars
;
64 public double newDTime
= 1000.75;
65 public short newValue
= 2;
66 public short firstDay
= 2;
67 public short mdfw
= 3;
68 double aOriginalDTime
= 0;
69 Locale
[] installed_locales
;
72 public void before() {
73 XLocaleData locData
= null;
75 locData
= UnoRuntime
.queryInterface(
77 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.
97 oObj
.loadDefaultCalendar(installed_locales
[0]);
98 oObj
.setDateTime(aOriginalDTime
);
102 * Loads default calendar for different locales. <p>
103 * Has <b> OK </b> status if method loads calendar, that is
104 * default for a given locale.
106 public void _loadDefaultCalendar() {
109 for (int i
=0; i
<installed_locales
.length
; i
++) {
110 String lang
= "Language: "+installed_locales
[i
].Language
+
111 ", Country: "+ installed_locales
[i
].Country
+
112 ", Variant: "+ installed_locales
[i
].Country
;
113 oObj
.loadDefaultCalendar(installed_locales
[i
]);
114 if (oObj
.getLoadedCalendar().Default
) {
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) {
140 log
.println(lang
+ " ... FAILED");
142 res
&= (calendars
[i
].length
> 0);
144 tRes
.tested("getAllCalendars()", res
);
148 * Loads calendars for a number of locales. <p>
149 * Has <b> OK </b> status if loaded calendar names are equal to gotten
150 * calendar names after loading.<p>
151 * The following method tests are to be completed successfully before :
153 * <li> <code> getAllCalendars() </code> : gets all calendars for a given
157 public void _loadCalendar() {
159 requiredMethod("getAllCalendars()");
161 for (int i
=0; i
<installed_locales
.length
; i
++) {
162 String lang
= "Language: "+installed_locales
[i
].Language
+
163 ", Country: "+ installed_locales
[i
].Country
+
164 ", Variant: "+ installed_locales
[i
].Country
;
165 oObj
.loadCalendar(calendars
[i
][0], installed_locales
[i
]);
166 if (calendars
[i
][0].equals(oObj
.getLoadedCalendar().Name
)) {
168 log
.println(lang
+ " ... FAILED");
170 res
&= calendars
[i
][0].equals(oObj
.getLoadedCalendar().Name
);
173 tRes
.tested("loadCalendar()", res
);
177 * Test calls the method, then result is checked. <p>
178 * Has <b> OK </b> status if loaded calendar names are equal to gotten
179 * calendar names after loading.<p>
180 * The following method tests are to be completed successfully before :
182 * <li> <code> loadCalendar() </code> : loads calendar using a given name
186 public void _getLoadedCalendar() {
189 requiredMethod("loadCalendar()");
190 for (int i
=0; i
<installed_locales
.length
; i
++) {
191 String lang
= "Language: "+installed_locales
[i
].Language
+
192 ", Country: "+ installed_locales
[i
].Country
+
193 ", Variant: "+ installed_locales
[i
].Country
;
194 oObj
.loadCalendar(calendars
[i
][0], installed_locales
[i
]);
195 if (calendars
[i
][0].equals(oObj
.getLoadedCalendar().Name
)) {
197 log
.println(lang
+ " ... FAILED");
199 res
&= calendars
[i
][0].equals(oObj
.getLoadedCalendar().Name
);
201 tRes
.tested("getLoadedCalendar()", res
);
205 * Test calls the method, then result is checked. <p>
206 * Has <b> OK </b> status if the method returns value that's equal to a
208 * The following method tests are to be completed successfully before :
210 * <li> <code> loadCalendar() </code> : loads calendar using a given name
214 public void _getUniqueID() {
216 for (int i
=0; i
<installed_locales
.length
; i
++) {
217 String lang
= "Language: "+installed_locales
[i
].Language
+
218 ", Country: "+ installed_locales
[i
].Country
+
219 ", Variant: "+ installed_locales
[i
].Country
;
220 oObj
.loadCalendar(calendars
[i
][0], installed_locales
[i
]);
221 String uID
= oObj
.getUniqueID();
222 if (uID
.equals(calendars
[i
][0])) {
224 log
.println(lang
+ " ... FAILED");
226 res
&= uID
.equals(calendars
[i
][0]);
229 tRes
.tested("getUniqueID()",res
);
233 * Test calls the method, then result is checked. <p>
234 * Has <b> OK </b> status if the method returns value, that's equal to
235 * value set before. <p>
238 public void _setDateTime() {
241 for (int i
=0; i
<installed_locales
.length
; i
++) {
242 String lang
= "Language: "+installed_locales
[i
].Language
+
243 ", Country: "+ installed_locales
[i
].Country
+
244 ", Variant: "+ installed_locales
[i
].Country
;
245 oObj
.setDateTime(newDTime
);
246 double aDTime
= oObj
.getDateTime();
247 if (aDTime
== newDTime
) {
249 log
.println(lang
+ " ... FAILED");
251 res
&= (aDTime
== newDTime
);
254 tRes
.tested("setDateTime()", res
);
258 * Test calls the method, then result is checked. <p>
259 * Has <b> OK </b> status if the method returns value, that's equal to
260 * value set before. <p>
263 public void _getDateTime() {
266 for (int i
=0; i
<installed_locales
.length
; i
++) {
267 String lang
= "Language: "+installed_locales
[i
].Language
+
268 ", Country: "+ installed_locales
[i
].Country
+
269 ", Variant: "+ installed_locales
[i
].Country
;
270 oObj
.setDateTime(newDTime
);
271 double aDTime
= oObj
.getDateTime();
272 if (aDTime
== newDTime
) {
274 log
.println(lang
+ " ... FAILED");
276 res
&= (aDTime
== newDTime
);
278 tRes
.tested("getDateTime()", res
);
282 * Test calls the method, then result is checked. <p>
283 * Has <b> OK </b> status if the method returns value, that's equal to
284 * value set before. <p>
287 public void _setValue() {
289 for (int i
=0; i
<installed_locales
.length
; i
++) {
291 String lang
= "Language: "+installed_locales
[i
].Language
+
292 ", Country: "+ installed_locales
[i
].Country
+
293 ", Variant: "+ installed_locales
[i
].Variant
+
294 ", Name: "+calendars
[i
][count
[i
]];
295 String
[] names
= new String
[]{"DAY_OF_MONTH",
296 "HOUR","MINUTE","SECOND","MILLISECOND",
298 oObj
.loadCalendar(calendars
[i
][count
[i
]],installed_locales
[i
]);
299 short[] fields
= new short[]{CalendarFieldIndex
.DAY_OF_MONTH
,
300 CalendarFieldIndex
.HOUR
,
301 CalendarFieldIndex
.MINUTE
,
302 CalendarFieldIndex
.SECOND
,
303 CalendarFieldIndex
.MILLISECOND
,
304 CalendarFieldIndex
.YEAR
,
305 CalendarFieldIndex
.MONTH
307 for (int k
=0; k
<fields
.length
;k
++) {
309 oObj
.setDateTime(0.0);
311 // save the current values for debug purposes
312 short[] oldValues
= new short[fields
.length
];
313 for (int n
=0; n
< oldValues
.length
; n
++){
314 oldValues
[n
] = oObj
.getValue(fields
[n
]);
317 short set
= oObj
.getValue(fields
[k
]);
318 if (fields
[k
] == CalendarFieldIndex
.MONTH
) set
= newValue
;
319 oObj
.setValue(fields
[k
],set
);
320 short get
= oObj
.getValue(fields
[k
]);
323 log
.println("ERROR occurred: tried to set " + names
[k
] + " to value " + set
);
324 log
.println("list of values BEFORE set " + names
[k
] + " to value " + set
+ ":");
325 for (int n
=0; n
< oldValues
.length
; n
++){
326 log
.println(names
[n
] + ":" + oldValues
[n
]);
328 log
.println("list of values AFTER set " + names
[k
] + " to value " + set
+ ":");
329 for (int n
=0; n
< fields
.length
;n
++){
330 log
.println(names
[n
] + ":" + oObj
.getValue(fields
[n
]));
333 error
+= "failed for "+names
[k
]+" expected "+
334 set
+" gained "+get
+" ; \n";
337 if (error
.equals("")) {
338 log
.println(lang
+ " ... OK");
340 log
.println("*** "+lang
+ " ... FAILED ***");
343 res
&= (error
.equals(""));
346 tRes
.tested("setValue()", res
);
350 * Test calls the method, then result is checked. <p>
351 * Has <b> OK </b> status if the method returns value, that's equal to
352 * value set before. <p>
355 public void _getValue() {
358 requiredMethod("setValue()");
359 short aValue
= oObj
.getValue(CalendarFieldIndex
.MONTH
);
360 res
&= (aValue
== newValue
);
362 log
.println("the returned value is not the expected value:");
363 log
.println("expexted: " + newValue
+ " returned value: " + aValue
);
365 tRes
.tested("getValue()", res
);
369 * Test calls the method, then result is checked. <p>
370 * Has <b> OK </b> status if value, added by the method is greater than
371 * previously defined "newValue".
373 * The following method tests are to be completed successfully before :
375 * <li> <code> getValue() </code> : gets the value of a field </li>
378 public void _addValue() {
381 requiredMethod("getValue()");
382 oObj
.addValue(CalendarFieldIndex
.MONTH
, 1);
383 short aValue
= oObj
.getValue(CalendarFieldIndex
.MONTH
);
384 res
&= (aValue
> newValue
);
386 log
.println("the returned value is not the expected value:");
387 log
.println("expexted: " + newValue
+ " returned value: " + aValue
);
389 tRes
.tested("addValue()", res
);
393 * Test calls the method. <p>
394 * Has <b> OK </b> status if the method successfully returns
395 * and no exceptions were thrown.
397 public void _setFirstDayOfWeek() {
400 oObj
.setFirstDayOfWeek(firstDay
);
402 tRes
.tested("setFirstDayOfWeek()", res
);
406 * Test calls the method, then result is checked. <p>
407 * Has <b> OK </b> status if the method returns value that is equal to
408 * value set before. <p>
409 * The following method tests are to be completed successfully before :
411 * <li> <code> setFirstDayOfWeek() </code> : set the first day of a
415 public void _getFirstDayOfWeek() {
418 requiredMethod("setFirstDayOfWeek()");
419 short aFirstDayOfWeek
= oObj
.getFirstDayOfWeek();
420 res
&= (aFirstDayOfWeek
== firstDay
);
421 tRes
.tested("getFirstDayOfWeek()", res
);
425 * Test calls the method. <p>
426 * Has <b> OK </b> status if the method successfully returns
427 * and no exceptions were thrown.
429 public void _setMinimumNumberOfDaysForFirstWeek() {
432 oObj
.setMinimumNumberOfDaysForFirstWeek(mdfw
);
434 tRes
.tested("setMinimumNumberOfDaysForFirstWeek()", res
);
438 * Test calls the method, then result is checked. <p>
439 * Has <b> OK </b> status if the method returns value that is equal to
440 * value set before. <p>
441 * The following method tests are to be completed successfully before :
443 * <li> <code> setMinimumNumberOfDaysForFirstWeek() </code> : sets how
444 * many days of a week must reside in the first week of a year</li>
447 public void _getMinimumNumberOfDaysForFirstWeek() {
450 requiredMethod("setMinimumNumberOfDaysForFirstWeek()");
451 short aShort
= oObj
.getMinimumNumberOfDaysForFirstWeek();
452 res
&= (aShort
== mdfw
);
453 tRes
.tested("getMinimumNumberOfDaysForFirstWeek()", res
);
457 * Test calls the method, then result is checked. <p>
458 * Has <b> OK </b> status if the method returns 12.
460 public void _getNumberOfMonthsInYear() {
462 short aShort
= oObj
.getNumberOfMonthsInYear();
464 res
&= (aShort
== (short) 12);
465 tRes
.tested("getNumberOfMonthsInYear()", res
);
469 * Test calls the method, then result is checked. <p>
470 * Has <b> OK </b> status if the method returns 7.
472 public void _getNumberOfDaysInWeek() {
474 short aShort
= oObj
.getNumberOfDaysInWeek();
476 res
&= (aShort
== (short) 7);
477 tRes
.tested("getNumberOfDaysInWeek()", res
);
481 * Test calls the method, then result is checked. <p>
482 * Has <b> OK </b> status if length of array, returned by the method is 12.
484 public void _getMonths() {
486 CalendarItem
[] months
= oObj
.getMonths();
488 res
&= (months
.length
== 12);
489 tRes
.tested("getMonths()", res
);
493 * Test calls the method, then result is checked. <p>
494 * Has <b> OK </b> status if length of array, returned by the method is 7.
496 public void _getDays() {
498 CalendarItem
[] Days
= oObj
.getDays();
500 res
&= (Days
.length
== 7);
501 tRes
.tested("getDays()", res
);
505 * After loading calendar, test calls the method, then result is checked.<p>
506 * Has <b> OK </b> status if length of string, returned by the method is 3.
508 public void _getDisplayName() {
511 oObj
.loadCalendar(calendars
[0][0],installed_locales
[0]);
512 String DisplayName
= oObj
.getDisplayName(CalendarDisplayIndex
.MONTH
,
513 newValue
, (short) 0);
514 res
&= (DisplayName
.length() == 3);
515 tRes
.tested("getDisplayName()", res
);
520 * The test sets obviously wrong value, then calls a method. After that the
521 * test sets correct value, and again calls a method. <p>
522 * Has <b> OK </b> status if the method returns true when valid month is
523 * set, and if the method returns false when set month is not valid.
525 public void _isValid() {
528 oObj
.loadDefaultCalendar(installed_locales
[0]);
529 oObj
.setValue(CalendarFieldIndex
.MONTH
, (short) 37);
530 res
&= !oObj
.isValid();
531 oObj
.setValue(CalendarFieldIndex
.MONTH
, (short) 10);
532 res
&= oObj
.isValid();
534 tRes
.tested("isValid()", res
);