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