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