merge the formfield patch from ooo-build
[ooovba.git] / qadevOOo / tests / basic / ifc / i18n / XBreakIterator / i18n_XBreakIterator.xba
blob0613d1e96296bf6ecccfa41c596ccca1f5d55781
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_XBreakIterator" script:language="StarBasic">
6 '*************************************************************************
8 ' DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9 '
10 ' Copyright 2008 by Sun Microsystems, Inc.
12 ' OpenOffice.org - a multi-platform office productivity suite
14 ' $RCSfile: i18n_XBreakIterator.xba,v $
16 ' $Revision: 1.3 $
18 ' This file is part of OpenOffice.org.
20 ' OpenOffice.org is free software: you can redistribute it and/or modify
21 ' it under the terms of the GNU Lesser General Public License version 3
22 ' only, as published by the Free Software Foundation.
24 ' OpenOffice.org is distributed in the hope that it will be useful,
25 ' but WITHOUT ANY WARRANTY; without even the implied warranty of
26 ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 ' GNU Lesser General Public License version 3 for more details
28 ' (a copy is included in the LICENSE file that accompanied this code).
30 ' You should have received a copy of the GNU Lesser General Public License
31 ' version 3 along with OpenOffice.org. If not, see
32 ' <http://www.openoffice.org/license.html>
33 ' for a copy of the LGPLv3 License.
35 '*************************************************************************
36 '*************************************************************************
40 ' Be sure that all variables are dimensioned:
41 option explicit
43 '*************************************************************************
44 ' This Interface/Service test depends on the following GLOBAL variables,
45 ' which must be specified in the object creation:
47 ' - Global oLocale As Object
48 ' - Global UnicodeString As String
49 ' - Global ScriptType(2, 1) As Integer
51 '*************************************************************************
57 const arrSize = 300
60 Sub RunTest()
62 '*************************************************************************
63 ' INTERFACE:
64 ' com.sun.star.i18n.XBreakIterator
65 '*************************************************************************
66 On Error Goto ErrHndl
67 Dim bOK As Boolean
68 Dim i As Integer
69 Dim nCharacterIteratorMode As Integer
70 Dim lDone As Long
71 Dim lRes As Long
72 Dim wordType As Integer
73 Dim Boundary As Variant
74 Dim BoundsToTest(arrSize) As new com.sun.star.i18n.Boundary
75 Dim Types(arrSize) As Integer
76 Dim nBoundsToTest As Integer
77 Dim iPos As Integer
78 Dim iType As Integer
79 Dim bFound As Boolean
80 Dim j As Integer
81 Dim NextCharacters(2, 3) As Integer
82 Dim PreviousCharacters(2, 3) As Integer
84 Test.StartMethod("nextCharacters()")
85 bOK = true
86 nCharacterIteratorMode = com.sun.star.i18n.CharacterIteratorMode.SKIPCHARACTER
88 'Start from position : Travel ... chars : Actual position after : How many chars traveled
89 NextCharacters(0, 0) = 1 : NextCharacters(0, 1) = 5000 : NextCharacters(0, 2) = len(UnicodeString) : NextCharacters(0, 3) = len(UnicodeString) - 1
90 NextCharacters(1, 0) = 5 : NextCharacters(1, 1) = -5000 : NextCharacters(1, 2) = 0 : NextCharacters(1, 3) = -5
91 NextCharacters(2, 0) = 10 : NextCharacters(2, 1) = 6 : NextCharacters(2, 2) = 16 : NextCharacters(2, 3) = 6
93 for i = 0 to 2
94 lRes = oObj.nextCharacters(UnicodeString, NextCharacters(i, 0), _
95 oLocale, nCharacterIteratorMode, NextCharacters(i, 1), lDone)
96 Out.Log("Expected result is: lRes = " + NextCharacters(i, 2) + _
97 "; lDone = " + NextCharacters(i, 3))
98 Out.Log("Actual result is: lRes = " + lRes + "; lDone = " + lDone)
100 bOK = bOK AND lRes = NextCharacters(i, 2)
101 bOK = bOK AND lDone = NextCharacters(i, 3)
102 next i
104 Test.MethodTested("nextCharacters()", bOK)
106 Test.StartMethod("previousCharacters()")
107 bOK = true
108 nCharacterIteratorMode = com.sun.star.i18n.CharacterIteratorMode.SKIPCHARACTER
110 'Start from position : Travel ... chars : Actual position after : How many chars traveled
111 PreviousCharacters(0, 0) = 10 : PreviousCharacters(0, 1) = -5000 : PreviousCharacters(0, 2) = len(UnicodeString) : PreviousCharacters(0, 3) = len(UnicodeString) - 10
112 PreviousCharacters(1, 0) = 5 : PreviousCharacters(1, 1) = 5000 : PreviousCharacters(1, 2) = 0 : PreviousCharacters(1, 3) = 5
113 PreviousCharacters(2, 0) = 10 : PreviousCharacters(2, 1) = 6 : PreviousCharacters(2, 2) = 4 : PreviousCharacters(2, 3) = 6
115 for i = 0 to 2
116 lRes = oObj.previousCharacters(UnicodeString, PreviousCharacters(i, 0), _
117 oLocale, nCharacterIteratorMode, PreviousCharacters(i, 1), lDone)
118 Out.Log("Expected result is: lRes = " + PreviousCharacters(i, 2) + _
119 "; lDone = " + PreviousCharacters(i, 3))
120 Out.Log("Actual result is: lRes = " + lRes + "; lDone = " + lDone)
122 bOK = bOK AND lRes = PreviousCharacters(i, 2)
123 bOK = bOK AND lDone = PreviousCharacters(i, 3)
124 next i
126 Test.MethodTested("previousCharacters()", bOK)
129 Test.StartMethod("nextWord()")
130 Test.StartMethod("previousWord()")
131 bOK = true
132 wordType = com.sun.star.i18n.WordType.ANYWORD_IGNOREWHITESPACES
133 Dim wordCount1 As Integer
134 Dim wordCount2 As Integer
135 wordCount1 = 0
136 wordCount2 = 0
137 i = 0
138 nBoundsToTest = 0
140 while i &lt; len(UnicodeString) - 1
141 Boundary = oObj.nextWord(UnicodeString, i, oLocale, wordType)
142 i = Boundary.endPos - 1
143 wordCount1 = wordCount1 + 1
144 if ((nBoundsToTest &lt; arrSize) AND (Boundary.endPos - Boundary.startPos &gt; 3)) then
145 BoundsToTest(nBoundsToTest).startPos = Boundary.startPos
146 BoundsToTest(nBoundsToTest).endPos = Boundary.endPos ' Here we find out some words and
147 nBoundsToTest = nBoundsToTest + 1 ' get their bounds to test in future
148 end if
149 Out.Log("Word " + wordCount1 + "(" + Boundary.startPos + "," + Boundary.endPos + "): '" + _
150 mid(UnicodeString, Boundary.startPos + 1, Boundary.endPos - Boundary.startPos) + "'")
151 wend
152 Out.Log("In text there are " + wordCount1 + " words, if count from left to right")
154 while i &gt; 0
155 Boundary = oObj.previousWord(UnicodeString, i, oLocale, wordType)
156 i = Boundary.startPos
157 wordCount2 = wordCount2 + 1
158 Out.Log("Word " + wordCount2 + "(" + Boundary.startPos + "," + Boundary.endPos + "): '" + _
159 mid(UnicodeString, Boundary.startPos + 1, Boundary.endPos - Boundary.startPos) + "'")
160 wend
161 Out.Log("In text there are " + wordCount2 + " words, if count from right to left")
163 bOK = bOK AND wordCount1 = wordCount2
164 Test.MethodTested("previousWord()", bOK)
165 Test.MethodTested("nextWord()", bOK)
167 Test.StartMethod("getWordBoundary()")
168 bOK = true
169 if (nBoundsToTest = 0) then
170 Out.Log("Couldn't find words longer than 3 letters.")
171 bOK = false
172 end if
174 for i = 0 to nBoundsToTest - 1
175 iPos = (BoundsToTest(i).endPos - BoundsToTest(i).startPos) / 2 + BoundsToTest(i).startPos ' calculate middle of the word
176 Boundary = oObj.getWordBoundary(UnicodeString, iPos, oLocale, wordType, true)
177 Out.Log("Expected result is: startPos = " + BoundsToTest(i).startPos + _
178 "; endPos = " + BoundsToTest(i).endPos)
179 Out.Log("Actual result is: startPos = " + Boundary.startPos + "; endPos = " + Boundary.endPos + _
180 " Word is: '" + mid(UnicodeString, Boundary.startPos + 1, Boundary.endPos - Boundary.startPos) + "'")
182 bOK = bOK AND BoundsToTest(i).startPos = Boundary.startPos
183 bOK = bOK AND BoundsToTest(i).endPos = Boundary.endPos
184 next i
186 Test.MethodTested("getWordBoundary()", bOK)
188 Test.StartMethod("getWordType()")
189 bOK = true
190 for i = 0 to nBoundsToTest - 1
191 iPos = (BoundsToTest(i).endPos - BoundsToTest(i).startPos) / 2 + BoundsToTest(i).startPos ' calculate middle of the word
192 wordType = oObj.getWordType(UnicodeString, iPos, oLocale)
193 bOK = bOK AND wordType &gt;= 0 AND wordType &lt;= 2
194 next i
195 Test.MethodTested("getWordType()", bOK)
197 Test.StartMethod("isBeginWord()")
198 bOK = true
199 if (nBoundsToTest = 0) then
200 Out.Log("Couldn't find words longer than 3 letters.")
201 bOK = false
202 end if
204 for i = 0 to nBoundsToTest - 1
205 bOK = bOK AND oObj.isBeginWord(UnicodeString, BoundsToTest(i).startPos, oLocale, com.sun.star.i18n.WordType.ANY_WORD)
206 bOK = bOK AND NOT oObj.isBeginWord(UnicodeString, BoundsToTest(i).startPos + 1, oLocale, com.sun.star.i18n.WordType.ANY_WORD)
207 next i
208 Test.MethodTested("isBeginWord()", bOK)
210 Test.StartMethod("isEndWord()")
211 bOK = true
212 if (nBoundsToTest = 0) then
213 Out.Log("Couldn't find words longer than 3 letters.")
214 bOK = false
215 end if
217 for i = 0 to nBoundsToTest - 1
218 bOK = bOK AND oObj.isEndWord(UnicodeString, BoundsToTest(i).endPos, oLocale, com.sun.star.i18n.WordType.ANY_WORD)
219 bOK = bOK AND NOT oObj.isEndWord(UnicodeString, BoundsToTest(i).endPos - 1, oLocale, com.sun.star.i18n.WordType.ANY_WORD)
220 next i
221 Test.MethodTested("isEndWord()", bOK)
223 Test.StartMethod("beginOfSentence()")
224 Test.StartMethod("endOfSentence()")
225 Dim sStart As Integer
226 Dim sEnd As Integer
228 bOK = true
229 iPos = 0
230 while (iPos &lt;= len(UnicodeString))' AND bOK)
231 sStart = oObj.beginOfSentence(UnicodeString, iPos, oLocale)
232 sEnd = oObj.endOfSentence(UnicodeString, iPos, oLocale)
233 if (iPos &lt; sStart OR iPos &gt; sEnd) then
234 bOK = false
235 Out.Log("From position " + iPos + " sentence range is [" + sStart + ", " + sEnd + "]")
236 Out.Log("'" + mid(UnicodeString, sStart + 1, sEnd - sStart) + "'")
237 end if
238 iPos = iPos + 1
239 wend
241 if (bOK) then
242 bOK = oObj.beginOfSentence(UnicodeString, -10, oLocale) = -1
243 bOK = oObj.endOfSentence(UnicodeString, -10, oLocale) = -1
244 bOK = oObj.beginOfSentence(UnicodeString, len(UnicodeString) + 1, oLocale) = -1
245 bOK = oObj.endOfSentence(UnicodeString, len(UnicodeString) + 1, oLocale) = -1
246 if (NOT bOK) then Out.Log("When invalid position, returned range is NOT [-1, -1]")
247 end if
249 Test.MethodTested("beginOfSentence()", bOK)
250 Test.MethodTested("endOfSentence()", bOK)
252 Test.StartMethod("getLineBreak()")
253 bOK = true
254 Dim LineBreakResults As Variant
255 Dim _LineBreakHyphenationOptions As new com.sun.star.i18n.LineBreakHyphenationOptions
256 Dim _LineBreakUserOptions As new com.sun.star.i18n.LineBreakUserOptions
257 Dim breakPos As Long, pos As Long
258 _LineBreakUserOptions.applyForbiddenRules = false
259 _LineBreakUserOptions.allowHyphenateEnglish = false
261 breakPos = 0 : pos = 0
263 while breakPos = 0 AND pos &lt; len(UnicodeString)
264 LineBreakResults = oObj.getLineBreak(UnicodeString, pos, oLocale, 0, _LineBreakHyphenationOptions, _LineBreakUserOptions)
265 breakPos = LineBreakResults.BreakIndex
266 pos = pos + 1
267 wend
269 ' finally the position of break must be found in the middle and
270 ' it must be before the break position specified
271 bOK = (breakPos &lt;= pos AND breakPos > 0)
273 if (NOT bOK) then
274 Out.Log("The last position was: " + pos + ", and the break position was: " + breakPos)
275 end if
277 Test.MethodTested("getLineBreak()", bOK)
280 Test.StartMethod("beginOfScript()")
281 Test.StartMethod("endOfScript()")
282 bOK = true
283 Out.Log("Searching for scripts...")
284 iPos = 0
285 nBoundsToTest = 0
287 while iPos &lt; len(UnicodeString) AND iPos &gt; -1 AND nBoundsToTest &lt; arrSize
288 iType = oObj.getScriptType(UnicodeString, iPos)
289 BoundsToTest(nBoundsToTest).startPos = oObj.beginOfScript(UnicodeString, iPos, iType)
290 BoundsToTest(nBoundsToTest).endPos = oObj.endOfScript(UnicodeString, iPos, iType)
291 Types(nBoundsToTest) = iType
292 iPos = BoundsToTest(nBoundsToTest).endPos
293 Out.Log("Type: " + iType + "; Bounds: [" + BoundsToTest(nBoundsToTest).startPos + "," + BoundsToTest(nBoundsToTest).endPos + "]")
294 nBoundsToTest = nBoundsToTest + 1
295 wend
296 bOK = bOK AND nBoundsToTest &gt; 0
298 Test.MethodTested("beginOfScript()", bOK)
299 Test.MethodTested("endOfScript()", bOK)
302 Test.StartMethod("nextScript()")
303 bOK = true
304 i = 1
305 iPos = -1
306 while i &lt; nBoundsToTest AND bOK
307 iPos = oObj.nextScript(UnicodeString, iPos, Types(i))
308 Out.Log("Expected index for Type " + Types(i) + " for nextScript is " + BoundsToTest(i).startPos)
309 bOK = bOK AND iPos = BoundsToTest(i).startPos
310 Out.Log("NextScript returned " + iPos + ";")
311 i = i + 1
312 wend
313 Test.MethodTested("nextScript()", bOK)
315 Test.StartMethod("previousScript()")
316 bOK = true
317 i = nBoundsToTest - 2
318 iPos = len(UnicodeString) + 1
319 while i &gt;= 0 AND bOK
320 iPos = oObj.previousScript(UnicodeString, iPos, Types(i))
321 Out.Log("Expected index for Type " + Types(i) + " for previousScript is " + BoundsToTest(i).startPos)
322 bOK = bOK AND iPos = BoundsToTest(i).startPos
323 Out.Log("PreviousScript returned " + iPos + ";")
324 i = i - 1
325 wend
326 Test.MethodTested("previousScript()", bOK)
328 Test.StartMethod("getScriptType()")
329 bOK = true
330 for i = 0 to 2
331 iPos = oObj.getScriptType(UnicodeString, ScriptType(i, 0))
332 Out.Log("Expected result is: ScriptType = " + ScriptType(i, 1))
333 Out.Log("Actual result is: ScriptType = " + iPos)
334 bOK = bOK AND iPos = ScriptType(i, 1)
335 next i
336 Test.MethodTested("getScriptType()", bOK)
338 Test.StartMethod("beginOfCharBlock()")
339 Test.StartMethod("endOfCharBlock()")
340 bOK = true
341 Out.Log("getting CharBlocks bounds...")
342 iPos = 0
343 nBoundsToTest = 0
345 while iPos &lt; len(UnicodeString) AND iPos &gt; -1 AND nBoundsToTest &lt; arrSize
346 iType = getCharBlockType(iPos)
347 BoundsToTest(nBoundsToTest).startPos = oObj.beginOfCharBlock(UnicodeString, iPos, oLocale, iType)
348 BoundsToTest(nBoundsToTest).endPos = oObj.endOfCharBlock(UnicodeString, iPos, oLocale, iType)
349 iPos = BoundsToTest(nBoundsToTest).endPos
350 Out.Log("" + nBoundsToTest + "). Bounds: [" + BoundsToTest(nBoundsToTest).startPos + "," + BoundsToTest(nBoundsToTest).endPos + "]; Type = " + iType)
351 Types(nBoundsToTest) = iType
352 nBoundsToTest = nBoundsToTest + 1
353 wend
355 for i = 0 to nBoundsToTest - 2
356 bOK = bOK AND BoundsToTest(i).endPos = BoundsToTest(i + 1).startPos
357 next
358 Out.Log("Testing for no intersections : " + bOK)
359 bOK = bOK AND BoundsToTest(0).startPos = 0
360 bOK = bOK AND BoundsToTest(nBoundsToTest - 1).endPos = len(UnicodeString)
361 Out.Log("Regions should starts with 0 and ends with " + len(UnicodeString))
363 Test.MethodTested("beginOfCharBlock()", bOK)
364 Test.MethodTested("endOfCharBlock()", bOK)
366 Test.StartMethod("nextCharBlock()")
367 bOK = true
368 for i = 0 to nBoundsToTest - 1
369 iPos = oObj.nextCharBlock(UnicodeString, BoundsToTest(i).startPos - 1, oLocale, Types(i))
370 if (iPos &lt;&gt; BoundsToTest(i).startPos) then
371 bOK = false
372 Out.Log("nextCharBlock(UnicodeString, " + BoundsToTest(i).startPos - 1 + ", oLocale, " + Types(i) + ") should return " + BoundsToTest(i).startPos)
373 Out.Log("... and actual value is " + iPos)
374 end if
375 next i
376 Test.MethodTested("nextCharBlock()", bOK)
378 Test.StartMethod("previousCharBlock()")
379 bOK = true
380 for i = 0 to nBoundsToTest - 1
381 iPos = oObj.previousCharBlock(UnicodeString, BoundsToTest(i).endPos + 1, oLocale, Types(i))
382 if (iPos &lt;&gt; BoundsToTest(i).startPos) then
383 bOK = false
384 Out.Log("previousCharBlock(UnicodeString, " + BoundsToTest(i).startPos - 1 +", oLocale, " + Types(i) + ") should return " + BoundsToTest(i).startPos)
385 Out.Log("... and actual value is " + iPos)
386 end if
387 next i
388 Test.MethodTested("previousCharBlock()", bOK)
390 Exit Sub
391 ErrHndl:
392 Test.Exception()
393 bOK = false
394 resume next
395 End Sub
396 Function getCharBlockType(pos As Integer) As Integer
397 Dim i As Integer
398 Dim cType As Integer
400 cType = 0
401 i = 1
402 while (i &lt; 31)
403 if (oObj.beginOfCharBlock(UnicodeString, pos, oLocale, i) &lt;&gt; -1) then
404 cType = i
405 i = 100
406 end if
407 i = i + 1
408 wend
410 getCharBlockType = cType
411 End Function
412 </script:module>