1<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
3<script:module xmlns:script="http://openoffice.org/2000/script" script:name="i18n_XCalendar" script:language="StarBasic">
4
5
6'*************************************************************************
7'
8' DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9'
10' Copyright 2000, 2010 Oracle and/or its affiliates.
11'
12' OpenOffice.org - a multi-platform office productivity suite
13'
14' This file is part of OpenOffice.org.
15'
16' OpenOffice.org is free software: you can redistribute it and/or modify
17' it under the terms of the GNU Lesser General Public License version 3
18' only, as published by the Free Software Foundation.
19'
20' OpenOffice.org is distributed in the hope that it will be useful,
21' but WITHOUT ANY WARRANTY; without even the implied warranty of
22' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23' GNU Lesser General Public License version 3 for more details
24' (a copy is included in the LICENSE file that accompanied this code).
25'
26' You should have received a copy of the GNU Lesser General Public License
27' version 3 along with OpenOffice.org.  If not, see
28' <http://www.openoffice.org/license.html>
29' for a copy of the LGPLv3 License.
30'
31'*************************************************************************
32*****
33'*************************************************************************
34
35
36
37' Be sure that all variables are dimensioned:
38option explicit
39
40
41
42Sub RunTest()
43
44'*************************************************************************
45' INTERFACE:
46' com.sun.star.i18n.XCalendar
47'*************************************************************************
48On Error Goto ErrHndl
49    Dim bOK As Boolean
50    Dim oLocaleData As Object
51    Dim oLocale As Object
52    Dim allLocales As Variant
53    Dim allCalendars As Variant
54    Dim uniqueID As String
55    Dim oCalendar As Object
56    Dim dDateTime As Double
57    Dim aValue As Variant
58    Dim oldValue As Variant
59    Dim newValue As Variant
60    Dim i As Integer
61
62    oLocaleData = createUnoService("com.sun.star.i18n.LocaleData")
63    allLocales = oLocaleData.getAllInstalledLocaleNames()
64    Out.Log("Found " + ubound(allLocales) + " Loacales registered in the system")
65    Randomize
66    oLocale = allLocales(rnd * ubound(allLocales))
67    Out.Log("Choose for testing : " + oLocale.Country)
68
69    Test.StartMethod("getAllCalendars()")
70    bOK = true
71    allCalendars = oObj.getAllCalendars(oLocale)
72    Out.Log("There are " + ubound(allCalendars) + 1 + " calendars for this locale.")
73    bOK = bOK AND NOT isNULL(allCalendars)
74    Test.MethodTested("getAllCalendars()", bOK)
75
76    Test.StartMethod("loadDefaultCalendar()")
77    bOK = true
78    oObj.loadDefaultCalendar(oLocale)
79    bOK = bOK AND oObj.getLoadedCalendar().Default
80    Test.MethodTested("loadDefaultCalendar()", bOK)
81
82    Test.StartMethod("getUniqueID()")
83    bOK = true
84    uniqueID = oObj.getUniqueID()
85    Out.Log("UniqueID for loaded calendar is " + uniqueID)
86    bOK = bOK AND (uniqueID = allCalendars(0))
87    Test.MethodTested("getUniqueID()", bOK)
88
89    Test.StartMethod("loadCalendar()")
90    bOK = true
91    oObj.loadCalendar(allCalendars(0), oLocale)
92    bOK = bOK AND NOT isNULL(oObj.getLoadedCalendar())
93    Test.MethodTested("loadCalendar()", bOK)
94
95    Test.StartMethod("getLoadedCalendar()")
96    bOK = true
97    oCalendar = oObj.getLoadedCalendar()
98    Out.Log("Loaded calendar's name is '" + oCalendar.Name + "'")
99    bOK = bOK AND NOT isNULL(oCalendar)
100    Test.MethodTested("getLoadedCalendar()", bOK)
101
102    Test.StartMethod("setDateTime()")
103    Test.StartMethod("getDateTime()")
104    bOK = true
105    oObj.setDateTime(12345.25)
106    dDateTime = oObj.getDateTime()
107    bOK = bOK AND dDateTime = 12345.25
108    Out.Log("Expected: 12345,25; Actual: " + dDateTime)
109    Test.MethodTested("setDateTime()", bOK)
110    Test.MethodTested("getDateTime()", bOK)
111
112    Test.StartMethod("getFirstDayOfWeek()")
113    Test.StartMethod("setFirstDayOfWeek()")
114    bOK = true
115    oObj.setFirstDayOfWeek(com.sun.star.i18n.Weekdays.SATURDAY)
116    Out.Log("Expected: " + com.sun.star.i18n.Weekdays.SATURDAY + ", Actual: " + oObj.getFirstDayOfWeek())
117    bOK = bOK AND oObj.getFirstDayOfWeek() = com.sun.star.i18n.Weekdays.SATURDAY
118    oObj.setFirstDayOfWeek(com.sun.star.i18n.Weekdays.SUNDAY)
119    Out.Log("Expected: " + com.sun.star.i18n.Weekdays.SUNDAY + ", Actual: " + oObj.getFirstDayOfWeek())
120    bOK = bOK AND oObj.getFirstDayOfWeek() = com.sun.star.i18n.Weekdays.SUNDAY
121
122    Test.MethodTested("getFirstDayOfWeek()", bOK)
123    Test.MethodTested("setFirstDayOfWeek()", bOK)
124
125    Test.StartMethod("setValue()")
126    Test.StartMethod("getValue()")
127    bOK = true
128    bOK = bOK AND TestValue(1, com.sun.star.i18n.Months.JANUARY, 2001, com.sun.star.i18n.WeekDays.MONDAY, 1, 1)
129    '1.01.2001 - Monday, 1-st week of month, 1-st week of year
130    bOK = bOK AND TestValue(12, com.sun.star.i18n.Months.JULY, 2001, com.sun.star.i18n.WeekDays.THURSDAY, 2, 28)
131    '12.07.2001 - Thursday, 2-nd week of month, 28-st week of year
132
133    Test.MethodTested("setValue()", bOK)
134    Test.MethodTested("getValue()", bOK)
135
136    Test.StartMethod("addValue()")
137    bOK = true
138
139    bOK = bOK AND TestAddValue("DST_OFFSET", com.sun.star.i18n.CalendarFieldIndex.DST_OFFSET, 1, 0)
140    bOK = bOK AND TestAddValue("DAY_OF_MONTH", com.sun.star.i18n.CalendarFieldIndex.DAY_OF_MONTH, 1, 0)
141    bOK = bOK AND TestAddValue("MONTH", com.sun.star.i18n.CalendarFieldIndex.MONTH, 3, 12)
142    bOK = bOK AND TestAddValue("YEAR", com.sun.star.i18n.CalendarFieldIndex.YEAR, -1, 0)
143    bOK = bOK AND TestAddValue("HOUR", com.sun.star.i18n.CalendarFieldIndex.HOUR, 7, 24)
144    bOK = bOK AND TestAddValue("MINUTE", com.sun.star.i18n.CalendarFieldIndex.MINUTE, 31, 60)
145    bOK = bOK AND TestAddValue("SECOND", com.sun.star.i18n.CalendarFieldIndex.SECOND, 13, 60)
146    bOK = bOK AND TestAddValue("MILLISECOND", com.sun.star.i18n.CalendarFieldIndex.MILLISECOND, 67, 1000)
147    bOK = bOK AND TestAddValue("ERA", com.sun.star.i18n.CalendarFieldIndex.ERA, -1, 0)
148
149    Test.MethodTested("addValue()", bOK)
150
151    Test.StartMethod("getNumberOfMonthsInYear()")
152    bOK = true
153    aValue = oObj.getNumberOfMonthsInYear()
154    Out.Log("There are " + aValue + " months in year")
155    bOK = bOK AND aValue = 12
156    Test.MethodTested("getNumberOfMonthsInYear()", bOK)
157
158    Test.StartMethod("getNumberOfDaysInWeek()")
159    bOK = true
160    aValue = oObj.getNumberOfDaysInWeek()
161    Out.Log("There are " + aValue + " days in week")
162    bOK = bOK AND aValue = 7
163    Test.MethodTested("getNumberOfDaysInWeek()", bOK)
164
165    Test.StartMethod("getMinimumNumberOfDaysForFirstWeek()")
166    bOK = true
167    aValue = oObj.getMinimumNumberOfDaysForFirstWeek()
168    Out.Log("MinimumNumberOfDaysForFirstWeek = " + aValue)
169    bOK = bOK AND aValue &gt; 0 AND aValue &lt; 8
170    Test.MethodTested("getMinimumNumberOfDaysForFirstWeek()", bOK)
171
172    Test.StartMethod("setMinimumNumberOfDaysForFirstWeek()")
173    bOK = true
174    oObj.setMinimumNumberOfDaysForFirstWeek(211)
175    aValue = oObj.getMinimumNumberOfDaysForFirstWeek()
176    Out.Log("MinimumNumberOfDaysForFirstWeek = " + aValue)
177    bOK = bOK AND aValue &gt; 0 AND aValue &lt; 8
178    Test.MethodTested("setMinimumNumberOfDaysForFirstWeek()", bOK)
179
180    Test.StartMethod("getMonths()")
181    bOK = true
182    Dim allMonths As Variant
183    Dim cAllMonths As String
184    allMonths = oObj.getMonths()
185    Out.Log("Returned " + ubound(allMonths) + 1 + " months.")
186    bOK = bOK AND ubound(allMonths) + 1= oObj.getNumberOfMonthsInYear()
187    if (bOK) then
188        cAllMonths = ""
189        for i = 0 to ubound(allMonths)
190            cAllMonths = cAllMonths + allMonths(i).FullName + ", "
191        next i
192        Out.Log("Months: " + cAllMonths)
193    end if
194
195    Test.MethodTested("getMonths()", bOK)
196
197    Test.StartMethod("getDays()")
198    bOK = true
199    Dim allDays As Variant
200    Dim cAllDays As String
201    allDays = oObj.getDays()
202    Out.Log("Returned " + ubound(allMonths) + 1 + " days.")
203    bOK = bOK AND ubound(allDays) + 1= oObj.getNumberOfDaysInWeek()
204    if (bOK) then
205        cAllDays = ""
206        for i = 0 to ubound(allDays)
207            cAllDays = cAllDays + allDays(i).FullName + ", "
208        next i
209        Out.Log("Days: " + cAllDays)
210    end if
211    Test.MethodTested("getDays()", bOK)
212
213    Test.StartMethod("getDisplayName()")
214    bOK = true
215    Dim cDisplayName As String
216    cDisplayName = oObj.getDisplayName(com.sun.star.i18n.CalendarDisplayIndex.MONTH, com.sun.star.i18n.Months.JANUARY, 1)
217    Out.Log("Full name for JANUARY is: " + cDisplayName)
218    bOK = bOK AND cDisplayName &lt;&gt; ""
219    Test.MethodTested("getDisplayName()", bOK)
220
221    Test.StartMethod("isValid()")
222    bOK = true
223
224    oObj.setValue(com.sun.star.i18n.CalendarFieldIndex.DAY_OF_MONTH, 30)
225    oObj.setValue(com.sun.star.i18n.CalendarFieldIndex.MONTH, com.sun.star.i18n.Months.FEBURARY)
226    oObj.setValue(com.sun.star.i18n.CalendarFieldIndex.YEAR, 2001)
227    Out.Log("isValid() = " + oObj.isValid())
228    bOK = bOK AND NOT oObj.isValid()
229
230    oObj.setValue(com.sun.star.i18n.CalendarFieldIndex.DAY_OF_MONTH, 27)
231    oObj.setValue(com.sun.star.i18n.CalendarFieldIndex.MONTH, com.sun.star.i18n.Months.FEBURARY)
232    oObj.setValue(com.sun.star.i18n.CalendarFieldIndex.YEAR, 2001)
233    Out.Log("isValid() = " + oObj.isValid())
234    bOK = bOK AND oObj.isValid()
235
236    Test.MethodTested("isValid()", bOK)
237
238Exit Sub
239ErrHndl:
240    Test.Exception()
241    bOK = false
242    resume next
243End Sub
244
245Function TestValue(_Day As Integer, _Month As Integer, _Year As Integer, _
246                   _Day_of_week As Integer, _Week_of_month As Integer, _Week_of_year As Integer) As Boolean
247
248    Dim bOK As Boolean
249    Dim aDay As Integer, aMonth As Integer, aYear As Integer
250    Dim aDay_of_week As Integer, aWeek_of_Month As Integer, aWeek_of_year As Integer
251
252    bOK = true
253
254    Out.Log("Set date to " + _Day + "." + _Month + "." + _Year + ";")
255
256    oObj.setValue(com.sun.star.i18n.CalendarFieldIndex.DAY_OF_MONTH, _Day)
257    oObj.setValue(com.sun.star.i18n.CalendarFieldIndex.MONTH, _Month)
258    oObj.setValue(com.sun.star.i18n.CalendarFieldIndex.YEAR, _Year)
259
260    aDay = oObj.getValue(com.sun.star.i18n.CalendarFieldIndex.DAY_OF_MONTH)
261    aMonth = oObj.getValue(com.sun.star.i18n.CalendarFieldIndex.MONTH)
262    aYear = oObj.getValue(com.sun.star.i18n.CalendarFieldIndex.YEAR)
263
264    Out.Log("Now date is " + aDay + "." + aMonth + "." + aYear + ";")
265
266    bOK = (aDay = _Day) AND (aMonth = _Month) AND (aYear = _Year)
267
268    if (bOK) then
269        aDay_of_week = oObj.getValue(com.sun.star.i18n.CalendarFieldIndex.DAY_OF_WEEK)
270        aWeek_of_month = oObj.getValue(com.sun.star.i18n.CalendarFieldIndex.WEEK_OF_MONTH)
271        aWeek_of_year = oObj.getValue(com.sun.star.i18n.CalendarFieldIndex.WEEK_OF_YEAR)
272        Out.Log("aDay_of_week: Expected: " + _Day_of_week + ", Actual: " + aDay_of_week)
273        Out.Log("aWeek_of_month: Expected: " + _Week_of_month + ", Actual: " + aWeek_of_month)
274        Out.Log("aWeek_of_year: Expected: " + _Week_of_year + ", Actual: " + aWeek_of_year)
275        bOK = (aDay_of_week = _Day_of_week) AND (aWeek_of_month = _Week_of_month) AND (aWeek_of_year = _Week_of_year)
276    end if
277
278    TestValue() = bOK
279
280Exit Function
281ErrHndl:
282    Test.Exception()
283    TestValue() = false
284End Function
285
286Function TestAddValue(valname As String, Idx As Integer, incValue As Integer, valModule As Integer) As Boolean
287    Dim aValue As Integer
288    Dim newValue As Integer
289    Dim bOK As Boolean
290
291    aValue = oObj.getValue(idx)
292    oObj.addValue(idx, incValue)
293    newValue = oObj.getValue(idx)
294    if (valModule = 0) then
295        bOK = (newValue = (aValue + incValue))
296        Out.Log(valname + ": Expected: " + (aValue + incValue) + ", Actual: " + newValue)
297    else
298        bOK = (newValue = (aValue + incValue) mod valModule)
299        Out.Log(valname + ": Expected: " + (aValue + incValue) mod valModule + ", Actual: " + newValue)
300    end if
301    TestAddValue() = bOK
302Exit Function
303ErrHndl:
304    Test.Exception()
305    TestAddValue() = false
306End Function
307</script:module>
308