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' Licensed to the Apache Software Foundation (ASF) under one 9' or more contributor license agreements. See the NOTICE file 10' distributed with this work for additional information 11' regarding copyright ownership. The ASF licenses this file 12' to you under the Apache License, Version 2.0 (the 13' "License"); you may not use this file except in compliance 14' with the License. You may obtain a copy of the License at 15' 16' http://www.apache.org/licenses/LICENSE-2.0 17' 18' Unless required by applicable law or agreed to in writing, 19' software distributed under the License is distributed on an 20' "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 21' KIND, either express or implied. See the License for the 22' specific language governing permissions and limitations 23' under the License. 24' 25'************************************************************************* 26 27 28 29 30 31' Be sure that all variables are dimensioned: 32option explicit 33 34 35 36Sub RunTest() 37 38'************************************************************************* 39' INTERFACE: 40' com.sun.star.i18n.XCalendar 41'************************************************************************* 42On Error Goto ErrHndl 43 Dim bOK As Boolean 44 Dim oLocaleData As Object 45 Dim oLocale As Object 46 Dim allLocales As Variant 47 Dim allCalendars As Variant 48 Dim uniqueID As String 49 Dim oCalendar As Object 50 Dim dDateTime As Double 51 Dim aValue As Variant 52 Dim oldValue As Variant 53 Dim newValue As Variant 54 Dim i As Integer 55 56 oLocaleData = createUnoService("com.sun.star.i18n.LocaleData") 57 allLocales = oLocaleData.getAllInstalledLocaleNames() 58 Out.Log("Found " + ubound(allLocales) + " Loacales registered in the system") 59 Randomize 60 oLocale = allLocales(rnd * ubound(allLocales)) 61 Out.Log("Choose for testing : " + oLocale.Country) 62 63 Test.StartMethod("getAllCalendars()") 64 bOK = true 65 allCalendars = oObj.getAllCalendars(oLocale) 66 Out.Log("There are " + ubound(allCalendars) + 1 + " calendars for this locale.") 67 bOK = bOK AND NOT isNULL(allCalendars) 68 Test.MethodTested("getAllCalendars()", bOK) 69 70 Test.StartMethod("loadDefaultCalendar()") 71 bOK = true 72 oObj.loadDefaultCalendar(oLocale) 73 bOK = bOK AND oObj.getLoadedCalendar().Default 74 Test.MethodTested("loadDefaultCalendar()", bOK) 75 76 Test.StartMethod("getUniqueID()") 77 bOK = true 78 uniqueID = oObj.getUniqueID() 79 Out.Log("UniqueID for loaded calendar is " + uniqueID) 80 bOK = bOK AND (uniqueID = allCalendars(0)) 81 Test.MethodTested("getUniqueID()", bOK) 82 83 Test.StartMethod("loadCalendar()") 84 bOK = true 85 oObj.loadCalendar(allCalendars(0), oLocale) 86 bOK = bOK AND NOT isNULL(oObj.getLoadedCalendar()) 87 Test.MethodTested("loadCalendar()", bOK) 88 89 Test.StartMethod("getLoadedCalendar()") 90 bOK = true 91 oCalendar = oObj.getLoadedCalendar() 92 Out.Log("Loaded calendar's name is '" + oCalendar.Name + "'") 93 bOK = bOK AND NOT isNULL(oCalendar) 94 Test.MethodTested("getLoadedCalendar()", bOK) 95 96 Test.StartMethod("setDateTime()") 97 Test.StartMethod("getDateTime()") 98 bOK = true 99 oObj.setDateTime(12345.25) 100 dDateTime = oObj.getDateTime() 101 bOK = bOK AND dDateTime = 12345.25 102 Out.Log("Expected: 12345,25; Actual: " + dDateTime) 103 Test.MethodTested("setDateTime()", bOK) 104 Test.MethodTested("getDateTime()", bOK) 105 106 Test.StartMethod("getFirstDayOfWeek()") 107 Test.StartMethod("setFirstDayOfWeek()") 108 bOK = true 109 oObj.setFirstDayOfWeek(com.sun.star.i18n.Weekdays.SATURDAY) 110 Out.Log("Expected: " + com.sun.star.i18n.Weekdays.SATURDAY + ", Actual: " + oObj.getFirstDayOfWeek()) 111 bOK = bOK AND oObj.getFirstDayOfWeek() = com.sun.star.i18n.Weekdays.SATURDAY 112 oObj.setFirstDayOfWeek(com.sun.star.i18n.Weekdays.SUNDAY) 113 Out.Log("Expected: " + com.sun.star.i18n.Weekdays.SUNDAY + ", Actual: " + oObj.getFirstDayOfWeek()) 114 bOK = bOK AND oObj.getFirstDayOfWeek() = com.sun.star.i18n.Weekdays.SUNDAY 115 116 Test.MethodTested("getFirstDayOfWeek()", bOK) 117 Test.MethodTested("setFirstDayOfWeek()", bOK) 118 119 Test.StartMethod("setValue()") 120 Test.StartMethod("getValue()") 121 bOK = true 122 bOK = bOK AND TestValue(1, com.sun.star.i18n.Months.JANUARY, 2001, com.sun.star.i18n.WeekDays.MONDAY, 1, 1) 123 '1.01.2001 - Monday, 1-st week of month, 1-st week of year 124 bOK = bOK AND TestValue(12, com.sun.star.i18n.Months.JULY, 2001, com.sun.star.i18n.WeekDays.THURSDAY, 2, 28) 125 '12.07.2001 - Thursday, 2-nd week of month, 28-st week of year 126 127 Test.MethodTested("setValue()", bOK) 128 Test.MethodTested("getValue()", bOK) 129 130 Test.StartMethod("addValue()") 131 bOK = true 132 133 bOK = bOK AND TestAddValue("DST_OFFSET", com.sun.star.i18n.CalendarFieldIndex.DST_OFFSET, 1, 0) 134 bOK = bOK AND TestAddValue("DAY_OF_MONTH", com.sun.star.i18n.CalendarFieldIndex.DAY_OF_MONTH, 1, 0) 135 bOK = bOK AND TestAddValue("MONTH", com.sun.star.i18n.CalendarFieldIndex.MONTH, 3, 12) 136 bOK = bOK AND TestAddValue("YEAR", com.sun.star.i18n.CalendarFieldIndex.YEAR, -1, 0) 137 bOK = bOK AND TestAddValue("HOUR", com.sun.star.i18n.CalendarFieldIndex.HOUR, 7, 24) 138 bOK = bOK AND TestAddValue("MINUTE", com.sun.star.i18n.CalendarFieldIndex.MINUTE, 31, 60) 139 bOK = bOK AND TestAddValue("SECOND", com.sun.star.i18n.CalendarFieldIndex.SECOND, 13, 60) 140 bOK = bOK AND TestAddValue("MILLISECOND", com.sun.star.i18n.CalendarFieldIndex.MILLISECOND, 67, 1000) 141 bOK = bOK AND TestAddValue("ERA", com.sun.star.i18n.CalendarFieldIndex.ERA, -1, 0) 142 143 Test.MethodTested("addValue()", bOK) 144 145 Test.StartMethod("getNumberOfMonthsInYear()") 146 bOK = true 147 aValue = oObj.getNumberOfMonthsInYear() 148 Out.Log("There are " + aValue + " months in year") 149 bOK = bOK AND aValue = 12 150 Test.MethodTested("getNumberOfMonthsInYear()", bOK) 151 152 Test.StartMethod("getNumberOfDaysInWeek()") 153 bOK = true 154 aValue = oObj.getNumberOfDaysInWeek() 155 Out.Log("There are " + aValue + " days in week") 156 bOK = bOK AND aValue = 7 157 Test.MethodTested("getNumberOfDaysInWeek()", bOK) 158 159 Test.StartMethod("getMinimumNumberOfDaysForFirstWeek()") 160 bOK = true 161 aValue = oObj.getMinimumNumberOfDaysForFirstWeek() 162 Out.Log("MinimumNumberOfDaysForFirstWeek = " + aValue) 163 bOK = bOK AND aValue > 0 AND aValue < 8 164 Test.MethodTested("getMinimumNumberOfDaysForFirstWeek()", bOK) 165 166 Test.StartMethod("setMinimumNumberOfDaysForFirstWeek()") 167 bOK = true 168 oObj.setMinimumNumberOfDaysForFirstWeek(211) 169 aValue = oObj.getMinimumNumberOfDaysForFirstWeek() 170 Out.Log("MinimumNumberOfDaysForFirstWeek = " + aValue) 171 bOK = bOK AND aValue > 0 AND aValue < 8 172 Test.MethodTested("setMinimumNumberOfDaysForFirstWeek()", bOK) 173 174 Test.StartMethod("getMonths()") 175 bOK = true 176 Dim allMonths As Variant 177 Dim cAllMonths As String 178 allMonths = oObj.getMonths() 179 Out.Log("Returned " + ubound(allMonths) + 1 + " months.") 180 bOK = bOK AND ubound(allMonths) + 1= oObj.getNumberOfMonthsInYear() 181 if (bOK) then 182 cAllMonths = "" 183 for i = 0 to ubound(allMonths) 184 cAllMonths = cAllMonths + allMonths(i).FullName + ", " 185 next i 186 Out.Log("Months: " + cAllMonths) 187 end if 188 189 Test.MethodTested("getMonths()", bOK) 190 191 Test.StartMethod("getDays()") 192 bOK = true 193 Dim allDays As Variant 194 Dim cAllDays As String 195 allDays = oObj.getDays() 196 Out.Log("Returned " + ubound(allMonths) + 1 + " days.") 197 bOK = bOK AND ubound(allDays) + 1= oObj.getNumberOfDaysInWeek() 198 if (bOK) then 199 cAllDays = "" 200 for i = 0 to ubound(allDays) 201 cAllDays = cAllDays + allDays(i).FullName + ", " 202 next i 203 Out.Log("Days: " + cAllDays) 204 end if 205 Test.MethodTested("getDays()", bOK) 206 207 Test.StartMethod("getDisplayName()") 208 bOK = true 209 Dim cDisplayName As String 210 cDisplayName = oObj.getDisplayName(com.sun.star.i18n.CalendarDisplayIndex.MONTH, com.sun.star.i18n.Months.JANUARY, 1) 211 Out.Log("Full name for JANUARY is: " + cDisplayName) 212 bOK = bOK AND cDisplayName <> "" 213 Test.MethodTested("getDisplayName()", bOK) 214 215 Test.StartMethod("isValid()") 216 bOK = true 217 218 oObj.setValue(com.sun.star.i18n.CalendarFieldIndex.DAY_OF_MONTH, 30) 219 oObj.setValue(com.sun.star.i18n.CalendarFieldIndex.MONTH, com.sun.star.i18n.Months.FEBURARY) 220 oObj.setValue(com.sun.star.i18n.CalendarFieldIndex.YEAR, 2001) 221 Out.Log("isValid() = " + oObj.isValid()) 222 bOK = bOK AND NOT oObj.isValid() 223 224 oObj.setValue(com.sun.star.i18n.CalendarFieldIndex.DAY_OF_MONTH, 27) 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 oObj.isValid() 229 230 Test.MethodTested("isValid()", bOK) 231 232Exit Sub 233ErrHndl: 234 Test.Exception() 235 bOK = false 236 resume next 237End Sub 238 239Function TestValue(_Day As Integer, _Month As Integer, _Year As Integer, _ 240 _Day_of_week As Integer, _Week_of_month As Integer, _Week_of_year As Integer) As Boolean 241 242 Dim bOK As Boolean 243 Dim aDay As Integer, aMonth As Integer, aYear As Integer 244 Dim aDay_of_week As Integer, aWeek_of_Month As Integer, aWeek_of_year As Integer 245 246 bOK = true 247 248 Out.Log("Set date to " + _Day + "." + _Month + "." + _Year + ";") 249 250 oObj.setValue(com.sun.star.i18n.CalendarFieldIndex.DAY_OF_MONTH, _Day) 251 oObj.setValue(com.sun.star.i18n.CalendarFieldIndex.MONTH, _Month) 252 oObj.setValue(com.sun.star.i18n.CalendarFieldIndex.YEAR, _Year) 253 254 aDay = oObj.getValue(com.sun.star.i18n.CalendarFieldIndex.DAY_OF_MONTH) 255 aMonth = oObj.getValue(com.sun.star.i18n.CalendarFieldIndex.MONTH) 256 aYear = oObj.getValue(com.sun.star.i18n.CalendarFieldIndex.YEAR) 257 258 Out.Log("Now date is " + aDay + "." + aMonth + "." + aYear + ";") 259 260 bOK = (aDay = _Day) AND (aMonth = _Month) AND (aYear = _Year) 261 262 if (bOK) then 263 aDay_of_week = oObj.getValue(com.sun.star.i18n.CalendarFieldIndex.DAY_OF_WEEK) 264 aWeek_of_month = oObj.getValue(com.sun.star.i18n.CalendarFieldIndex.WEEK_OF_MONTH) 265 aWeek_of_year = oObj.getValue(com.sun.star.i18n.CalendarFieldIndex.WEEK_OF_YEAR) 266 Out.Log("aDay_of_week: Expected: " + _Day_of_week + ", Actual: " + aDay_of_week) 267 Out.Log("aWeek_of_month: Expected: " + _Week_of_month + ", Actual: " + aWeek_of_month) 268 Out.Log("aWeek_of_year: Expected: " + _Week_of_year + ", Actual: " + aWeek_of_year) 269 bOK = (aDay_of_week = _Day_of_week) AND (aWeek_of_month = _Week_of_month) AND (aWeek_of_year = _Week_of_year) 270 end if 271 272 TestValue() = bOK 273 274Exit Function 275ErrHndl: 276 Test.Exception() 277 TestValue() = false 278End Function 279 280Function TestAddValue(valname As String, Idx As Integer, incValue As Integer, valModule As Integer) As Boolean 281 Dim aValue As Integer 282 Dim newValue As Integer 283 Dim bOK As Boolean 284 285 aValue = oObj.getValue(idx) 286 oObj.addValue(idx, incValue) 287 newValue = oObj.getValue(idx) 288 if (valModule = 0) then 289 bOK = (newValue = (aValue + incValue)) 290 Out.Log(valname + ": Expected: " + (aValue + incValue) + ", Actual: " + newValue) 291 else 292 bOK = (newValue = (aValue + incValue) mod valModule) 293 Out.Log(valname + ": Expected: " + (aValue + incValue) mod valModule + ", Actual: " + newValue) 294 end if 295 TestAddValue() = bOK 296Exit Function 297ErrHndl: 298 Test.Exception() 299 TestAddValue() = false 300End Function 301</script:module> 302