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 package ifc.accessibility;
24 
25 import lib.MultiMethodTest;
26 import lib.Status;
27 import lib.StatusException;
28 
29 import com.sun.star.accessibility.AccessibleTextType;
30 import com.sun.star.accessibility.TextSegment;
31 import com.sun.star.accessibility.XAccessibleComponent;
32 import com.sun.star.accessibility.XAccessibleText;
33 import com.sun.star.awt.Point;
34 import com.sun.star.awt.Rectangle;
35 import com.sun.star.beans.PropertyValue;
36 import com.sun.star.lang.XMultiServiceFactory;
37 import com.sun.star.uno.UnoRuntime;
38 
39 
40 /**
41  * Testing <code>com.sun.star.accessibility.XAccessibleText</code>
42  * interface methods :
43  * <ul>
44  *  <li><code> getCaretPosition()</code></li>
45  *  <li><code> setCaretPosition()</code></li>
46  *  <li><code> getCharacter()</code></li>
47  *  <li><code> getCharacterAttributes()</code></li>
48  *  <li><code> getCharacterBounds()</code></li>
49  *  <li><code> getCharacterCount()</code></li>
50  *  <li><code> getIndexAtPoint()</code></li>
51  *  <li><code> getSelectedText()</code></li>
52  *  <li><code> getSelectionStart()</code></li>
53  *  <li><code> getSelectionEnd()</code></li>
54  *  <li><code> setSelection()</code></li>
55  *  <li><code> getText()</code></li>
56  *  <li><code> getTextRange()</code></li>
57  *  <li><code> getTextAtIndex()</code></li>
58  *  <li><code> getTextBeforeIndex()</code></li>
59  *  <li><code> getTextBehindIndex()</code></li>
60  *  <li><code> copyText()</code></li>
61  * </ul> <p>
62  * This test needs the following object relations :
63  * <ul>
64  *  <li> <code>'XAccessibleText.Text'</code> (of type <code>String</code>)
65  *   <b> optional </b> :
66  *   the string presentation of component's text. If the relation
67  *   is not specified, then text from method <code>getText()</code>
68  *   is used.
69  *  </li>
70  *  </ul> <p>
71  * @see com.sun.star.accessibility.XAccessibleText
72  */
73 public class _XAccessibleText extends MultiMethodTest {
74 
75     public XAccessibleText oObj = null;
76     protected com.sun.star.awt.Rectangle bounds = null;
77     String text = null;
78     String editOnly = null;
79     Object LimitedBounds = null;
80     Rectangle chBounds = null;
81     int chCount = 0;
82 
83 
84     /**
85      * Retrieves a string representation of the component's text.
86      * The length of retrieved string must be greater than zero.
87      */
before()88     protected void before() {
89         Object xat = tEnv.getObjRelation("XAccessibleText");
90 
91         XAccessibleComponent component = null;
92 
93         if (xat != null) {
94             oObj = (XAccessibleText) UnoRuntime.queryInterface(
95                            XAccessibleText.class, xat);
96             component = (XAccessibleComponent) UnoRuntime.queryInterface(
97                                 XAccessibleComponent.class, xat);
98         }
99 
100         text = (String) tEnv.getObjRelation("XAccessibleText.Text");
101 
102         if (text == null) {
103             text = oObj.getText();
104         }
105 
106         if (text.length() == 0) {
107             throw new StatusException(Status.failed(
108                                               "The length of text must be greater than zero"));
109         }
110 
111         editOnly = (String) tEnv.getObjRelation("EditOnly");
112         LimitedBounds = tEnv.getObjRelation("LimitedBounds");
113 
114         if (component == null) {
115             component = (XAccessibleComponent) UnoRuntime.queryInterface(
116                                 XAccessibleComponent.class,
117                                 tEnv.getTestObject());
118         }
119 
120         bounds = component.getBounds();
121 
122         log.println("Text is '" + text + "'");
123         System.out.println("############################");
124     }
125 
126     /**
127      * Calls the method and checks returned value.
128      * Has OK status if returned value is equal to <code>chCount - 1</code>.
129      * The following method tests are to be executed before:
130      * <ul>
131      *  <li> <code>setCaretPosition()</code> </li>
132      * </ul>
133      */
_getCaretPosition()134     public void _getCaretPosition() {
135         requiredMethod("getCharacterCount()");
136 
137         if (editOnly != null) {
138             log.println(editOnly);
139             throw new StatusException(Status.skipped(true));
140         }
141 
142         boolean res = true;
143         boolean sc = true;
144 
145         try {
146             oObj.setCaretPosition(chCount - 1);
147         } catch (com.sun.star.lang.IndexOutOfBoundsException ie) {
148         }
149 
150         int carPos = oObj.getCaretPosition();
151         log.println("getCaretPosition: " + carPos);
152 
153         if (sc) {
154             res = carPos == (chCount - 1);
155         } else {
156             log.println(
157                     "Object is read only and Caret position couldn't be set");
158             res = carPos == -1;
159         }
160 
161         tRes.tested("getCaretPosition()", res);
162     }
163 
164     /**
165      * Calls the method with the wrong index and with the correct index
166      * <code>chCount - 1</code>.
167      * Has OK status if exception was thrown for wrong index and
168      * if exception wasn't thrown for the correct index.
169      * The following method tests are to be executed before:
170      * <ul>
171      *  <li> <code>getCharacterCount()</code> </li>
172      * </ul>
173      */
_setCaretPosition()174     public void _setCaretPosition() {
175         requiredMethod("getCharacterCount()");
176 
177         boolean res = true;
178 
179         try {
180             log.print("setCaretPosition(-1):");
181             oObj.setCaretPosition(-1);
182             res &= false;
183             log.println("exception was expected ... FAILED");
184         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
185             log.println("expected exception");
186             res &= true;
187         }
188 
189         try {
190             log.print("setCaretPosition(chCount+1):");
191             oObj.setCaretPosition(chCount + 1);
192             res &= false;
193             log.println("exception was expected  ... FAILED");
194         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
195             log.println("expected exception");
196             res &= true;
197         }
198 
199         try {
200             log.println("setCaretPosition(chCount - 1)");
201             oObj.setCaretPosition(chCount - 1);
202             res &= true;
203         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
204             log.println("unexpected exception  ... FAILED");
205             e.printStackTrace(log);
206             res &= false;
207         }
208 
209         tRes.tested("setCaretPosition()", res);
210     }
211 
212     /**
213      * Calls the method with the wrong index and with the correct indexes.
214      * Checks every character in the text.
215      * Has OK status if exception was thrown for wrong index,
216      * if exception wasn't thrown for the correct index and
217      * if every character is equal to corresponding character in the text.
218      * The following method tests are to be executed before:
219      * <ul>
220      *  <li> <code>getCharacterCount()</code> </li>
221      * </ul>
222      */
_getCharacter()223     public void _getCharacter() {
224         requiredMethod("getCharacterCount()");
225 
226         boolean res = true;
227 
228         try {
229             log.println("getCharacter(-1)");
230             oObj.getCharacter(-1);
231             log.println("Exception was expected");
232             res = false;
233         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
234             log.println("Expected exception");
235             res = true;
236         }
237 
238         try {
239             log.println("getCharacter(chCount)");
240             oObj.getCharacter(chCount);
241             log.println("Exception was expected");
242             res &= false;
243         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
244             log.println("Expected exception");
245             res &= true;
246         }
247 
248         try {
249             log.println("Checking of every character in the text...");
250 
251             boolean isEqCh = true;
252 
253             for (int i = 0; i < chCount; i++) {
254                 char ch = oObj.getCharacter(i);
255                 isEqCh = ch == text.charAt(i);
256                 res &= isEqCh;
257 
258                 if (!isEqCh) {
259                     log.println("At the position " + i +
260                                 "was expected character: " + text.charAt(i));
261                     log.println("but was returned: " + ch);
262 
263                     break;
264                 }
265             }
266         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
267             log.println("Unexpected exception");
268             e.printStackTrace(log);
269             res &= false;
270         }
271 
272         tRes.tested("getCharacter()", res);
273     }
274 
275     /**
276      * Calls the method with the wrong indexes and with the correct index,
277      * checks a returned value.
278      * Has OK status if exception was thrown for the wrong indexes,
279      * if exception wasn't thrown for the correct index and
280      * if returned value isn't <code>null</code>.
281      * The following method tests are to be executed before:
282      * <ul>
283      *  <li> <code>getCharacterCount()</code> </li>
284      * </ul>
285      */
_getCharacterAttributes()286     public void _getCharacterAttributes() {
287         requiredMethod("getCharacterCount()");
288 
289         boolean res = true;
290         String[] attr = new String[] { "" };
291 
292         try {
293             log.println("getCharacterAttributes(-1)");
294             oObj.getCharacterAttributes(-1, attr);
295             log.println("Exception was expected");
296             res &= false;
297         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
298             log.println("Expected exception");
299             res &= true;
300         }
301 
302         try {
303             log.println("getCharacterAttributes(chCount)");
304             oObj.getCharacterAttributes(chCount, attr);
305             log.println("Exception was expected");
306             res &= false;
307         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
308             log.println("Expected exception");
309             res &= true;
310         }
311 
312         try {
313             log.println("getCharacterAttributes(chCount-1)");
314 
315             PropertyValue[] props = oObj.getCharacterAttributes(chCount - 1,
316                                                                 attr);
317             res &= (props != null);
318         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
319             log.println("Unexpected exception");
320             e.printStackTrace(log);
321             res &= false;
322         }
323 
324         tRes.tested("getCharacterAttributes()", res);
325     }
326 
327     /**
328      * Calls the method with the wrong indexes and with the correct index.
329      * checks and stores a returned value.
330      * Has OK status if exception was thrown for the wrong indexes,
331      * if exception wasn't thrown for the correct index and
332      * if returned value isn't <code>null</code>.
333      * The following method tests are to be executed before:
334      * <ul>
335      *  <li> <code>getCharacterCount()</code> </li>
336      * </ul>
337      */
_getCharacterBounds()338     public void _getCharacterBounds() {
339         requiredMethod("getCharacterCount()");
340 
341         boolean res = true;
342 
343         int lastIndex = chCount;
344 
345         if (LimitedBounds != null) {
346             if (LimitedBounds instanceof Integer) {
347                 lastIndex = ((Integer) LimitedBounds).intValue();
348             } else {
349                 lastIndex = chCount - 1;
350             }
351 
352             log.println(LimitedBounds);
353         }
354 
355         try {
356             log.println("getCharacterBounds(-1)");
357             oObj.getCharacterBounds(-1);
358             log.println("Exception was expected");
359             res &= false;
360         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
361             log.println("Expected exception");
362             res &= true;
363         }
364 
365         try {
366             log.println("getCharacterBounds(" + (lastIndex + 1) + ")");
367             oObj.getCharacterBounds(lastIndex + 1);
368             log.println("Exception was expected");
369             res &= false;
370         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
371             log.println("Expected exception");
372             res &= true;
373         }
374 
375         try {
376             for (int i = 0; i < lastIndex; i++) {
377                 log.println("getCharacterBounds(" + i + ")");
378                 chBounds = oObj.getCharacterBounds(i);
379 
380                 boolean localres = true;
381                 localres = chBounds.X >= 0;
382                 localres &= (chBounds.Y >= 0);
383                 localres &= ((chBounds.X + chBounds.Width) <= bounds.Width);
384                 localres &= ((chBounds.X + chBounds.Width) > 0);
385                 localres &= ((chBounds.Y + chBounds.Height) <= bounds.Height);
386                 localres &= ((chBounds.Y + chBounds.Height) > 0);
387 
388                 if (!localres) {
389                     log.println("Text at this place: "+oObj.getCharacter(i));
390                     log.println("Character bounds outside component");
391                     log.println("Character rect: " + chBounds.X + ", " +
392                                 chBounds.Y + ", " + chBounds.Width + ", " +
393                                 chBounds.Height);
394                     log.println("Component rect: " + bounds.X + ", " +
395                                 bounds.Y + ", " + bounds.Width + ", " +
396                                 bounds.Height);
397                     res &= localres;
398                 }
399             }
400         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
401             log.println("Unexpected exception");
402             e.printStackTrace(log);
403             res &= false;
404         }
405 
406         tRes.tested("getCharacterBounds()", res);
407     }
408 
409     /**
410      * Calls the method and stores a returned value to the variable
411      * <code>chCount</code>.
412      * Has OK status if a returned value is equal to the text length.
413      */
_getCharacterCount()414     public void _getCharacterCount() {
415         chCount = oObj.getCharacterCount();
416         log.println("Character count:" + chCount);
417 
418         boolean res = chCount == text.length();
419         tRes.tested("getCharacterCount()", res);
420     }
421 
422     /**
423      * Calls the method for an invalid point and for the point of rectangle
424      * returned by the method <code>getCharacterBounds()</code>.
425      * Has OK status if returned value is equal to <code>-1</code> for an
426      * invalid point and if returned value is equal to <code>chCount-1</code>
427      * for a valid point.
428      * The following method tests are to be executed before:
429      * <ul>
430      *  <li> <code>getCharacterBounds()</code> </li>
431      * </ul>
432      */
_getIndexAtPoint()433     public void _getIndexAtPoint() {
434         //requiredMethod("getCharacterBounds()");
435         boolean res = true;
436         log.print("getIndexAtPoint(-1, -1):");
437 
438         Point pt = new Point(-1, -1);
439         int index = oObj.getIndexAtPoint(pt);
440         log.println(index);
441         res &= (index == -1);
442 
443         int lastIndex = chCount;
444 
445         if (LimitedBounds != null) {
446             if (LimitedBounds instanceof Integer) {
447                 lastIndex = ((Integer) LimitedBounds).intValue();
448             } else {
449                 lastIndex = chCount - 1;
450             }
451 
452             log.println(LimitedBounds);
453         }
454 
455         for (int i = 0; i < lastIndex; i++) {
456             Rectangle aRect = null;
457             String text = "empty";
458 
459             try {
460                 aRect = oObj.getCharacterBounds(i);
461                 text = oObj.getTextAtIndex(i, (short) 1).SegmentText;
462             } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
463             } catch (com.sun.star.lang.IllegalArgumentException e) {
464             }
465 
466             int x = aRect.X + (aRect.Width / 2);
467             int y = aRect.Y + (aRect.Height / 2);
468             Point aPoint = new Point(x, y);
469             int nIndex = oObj.getIndexAtPoint(aPoint);
470 
471             x = aRect.X;
472             y = aRect.Y + (aRect.Height / 2);
473             aPoint = new Point(x, y);
474             int left = oObj.getIndexAtPoint(aPoint);
475 
476 
477 
478             int[] previous = (int[]) tEnv.getObjRelation("PreviousUsed");
479 
480             if (previous != null) {
481                 for (int k = 0; k < previous.length; k++) {
482                     if (i == previous[k]) {
483                         nIndex++;
484                     }
485                 }
486             }
487 
488             if (nIndex != i) {
489                 // for some letters the center of the rectangle isn't recognised
490                 // in this case we are happy if the left border of the rectangle
491                 // returns the correct value.
492                 if (left !=i) {
493                     log.println("## Method didn't work for Point (" + x + "," + y +
494                             ")");
495                     log.println("Expected Index " + i);
496                     log.println("Gained Index: " + nIndex);
497                     log.println("Left Border: "+left);
498                     log.println("CharacterAtIndex: " + text);
499                     res &= false;
500                 }
501             }
502         }
503 
504         tRes.tested("getIndexAtPoint()", res);
505     }
506 
507     /**
508      * Checks a returned values after different calls of the method
509      * <code>setSelection()</code>.
510      * The following method tests are to be executed before:
511      * <ul>
512      *  <li> <code>setSelection()</code> </li>
513      * </ul>
514      */
_getSelectedText()515     public void _getSelectedText() {
516         if (editOnly != null) {
517             log.println(editOnly);
518             throw new StatusException(Status.skipped(true));
519         }
520 
521         requiredMethod("setSelection()");
522 
523         boolean res = true;
524 
525         try {
526             log.println("setSelection(0, 0)");
527             oObj.setSelection(0, 0);
528             log.print("getSelectedText():");
529 
530             String txt = oObj.getSelectedText();
531             log.println("'" + txt + "'");
532             res &= (txt.length() == 0);
533 
534             log.println("setSelection(0, chCount)");
535             oObj.setSelection(0, chCount);
536             log.print("getSelectedText():");
537             txt = oObj.getSelectedText();
538             log.println("'" + txt + "'");
539             res &= txt.equals(text);
540 
541             if (chCount > 2) {
542                 log.println("setSelection(1, chCount-1)");
543                 oObj.setSelection(1, chCount - 1);
544                 log.print("getSelectedText():");
545                 txt = oObj.getSelectedText();
546                 log.println("'" + txt + "'");
547                 res &= txt.equals(text.substring(1, chCount - 1));
548             }
549         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
550             log.println("Unexpected exception");
551             e.printStackTrace(log);
552             res &= false;
553         }
554 
555         tRes.tested("getSelectedText()", res);
556     }
557 
558     /**
559      * Checks a returned values after different calls of the method
560      * <code>setSelection()</code>.
561      * The following method tests are to be executed before:
562      * <ul>
563      *  <li> <code>setSelection()</code> </li>
564      * </ul>
565      */
_getSelectionStart()566     public void _getSelectionStart() {
567         if (editOnly != null) {
568             log.println(editOnly);
569             throw new StatusException(Status.skipped(true));
570         }
571 
572         requiredMethod("setSelection()");
573 
574         boolean res = true;
575 
576         try {
577             log.println("setSelection(0, chCount)");
578             oObj.setSelection(0, chCount);
579 
580             int start = oObj.getSelectionStart();
581             log.println("getSelectionStart():" + start);
582             res &= (start == 0);
583 
584             if (chCount > 2) {
585                 log.println("setSelection(1, chCount-1)");
586                 oObj.setSelection(1, chCount - 1);
587                 start = oObj.getSelectionStart();
588                 log.println("getSelectionStart():" + start);
589                 res &= (start == 1);
590             }
591         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
592             log.println("Unexpected exception");
593             e.printStackTrace(log);
594             res &= false;
595         }
596 
597         tRes.tested("getSelectionStart()", res);
598     }
599 
600     /**
601      * Checks a returned values after different calls of the method
602      * <code>setSelection()</code>.
603      * The following method tests are to be executed before:
604      * <ul>
605      *  <li> <code>setSelection()</code> </li>
606      * </ul>
607      */
_getSelectionEnd()608     public void _getSelectionEnd() {
609         if (editOnly != null) {
610             log.println(editOnly);
611             throw new StatusException(Status.skipped(true));
612         }
613 
614         requiredMethod("setSelection()");
615 
616         boolean res = true;
617 
618         try {
619             log.println("setSelection(0, chCount)");
620             oObj.setSelection(0, chCount);
621 
622             int end = oObj.getSelectionEnd();
623             log.println("getSelectionEnd():" + end);
624             res &= (end == chCount);
625 
626             if (chCount > 2) {
627                 log.println("setSelection(1, chCount-1)");
628                 oObj.setSelection(1, chCount - 1);
629                 end = oObj.getSelectionEnd();
630                 log.println("getSelectionEnd():" + end);
631                 res &= (end == (chCount - 1));
632             }
633         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
634             log.println("Unexpected exception");
635             e.printStackTrace(log);
636             res &= false;
637         }
638 
639         tRes.tested("getSelectionEnd()", res);
640     }
641 
642     /**
643      * Calls the method with invalid parameters an with valid parameters.
644      * Has OK status if exception was thrown for invalid parameters,
645      * if exception wasn't thrown for valid parameters.
646      * The following method tests are to be executed before:
647      * <ul>
648      *  <li> <code>getCharacterCount()</code> </li>
649      * </ul>
650      */
_setSelection()651     public void _setSelection() {
652         requiredMethod("getCharacterCount()");
653 
654         boolean res = true;
655         boolean locRes = true;
656 
657         if (editOnly != null) {
658             log.println(editOnly);
659             throw new StatusException(Status.skipped(true));
660         }
661 
662         try {
663             log.print("setSelection(-1, chCount-1):");
664             locRes = oObj.setSelection(-1, chCount - 1);
665             log.println(locRes + " excepion was expected");
666             res &= !locRes;
667         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
668             log.println("Expected exception");
669             res &= true;
670         }
671 
672         try {
673             log.print("setSelection(0, chCount+1):");
674             locRes = oObj.setSelection(0, chCount + 1);
675             log.println(locRes + " excepion was expected");
676             res &= !locRes;
677         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
678             log.println("Expected exception");
679             res &= true;
680         }
681 
682         try {
683             if (chCount > 2) {
684                 log.print("setSelection(1, chCount-1):");
685                 locRes = oObj.setSelection(1, chCount - 1);
686                 log.println(locRes);
687                 res &= locRes;
688 
689                 log.print("setSelection(chCount-1, 1):");
690                 locRes = oObj.setSelection(chCount - 1, 1);
691                 log.println(locRes);
692                 res &= locRes;
693             }
694 
695             log.print("setSelection(0, chCount-1):");
696             locRes = oObj.setSelection(0, chCount - 1);
697             log.println(locRes);
698             res &= locRes;
699 
700             log.print("setSelection(chCount-1, 0):");
701             locRes = oObj.setSelection(chCount - 1, 0);
702             log.println(locRes);
703             res &= locRes;
704 
705             log.print("setSelection(0, 0):");
706             locRes = oObj.setSelection(0, 0);
707             log.println(locRes);
708             res &= locRes;
709         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
710             log.println("Unexpected exception");
711             e.printStackTrace(log);
712             res &= false;
713         }
714 
715         tRes.tested("setSelection()", res);
716     }
717 
718     /**
719      * Calls the method and checks returned value.
720      * Has OK status if returned string is equal to string
721      * received from relation.
722      */
_getText()723     public void _getText() {
724         String txt = oObj.getText();
725         log.println("getText: " + txt);
726 
727         boolean res = txt.equals(text);
728         tRes.tested("getText()", res);
729     }
730 
731     /**
732      * Calls the method with invalid parameters an with valid parameters,
733      * checks returned values.
734      * Has OK status if exception was thrown for invalid parameters,
735      * if exception wasn't thrown for valid parameters and if returned values
736      * are equal to corresponding substrings of the text received by relation.
737      * The following method tests are to be executed before:
738      * <ul>
739      *  <li> <code>getCharacterCount()</code> </li>
740      * </ul>
741      */
_getTextRange()742     public void _getTextRange() {
743         requiredMethod("getCharacterCount()");
744 
745         boolean res = true;
746         boolean locRes = true;
747 
748 		String txtRange = "";
749 
750         try {
751             if (chCount > 3) {
752                 log.print("getTextRange(1, chCount - 2): ");
753 
754                 txtRange = oObj.getTextRange(1, chCount - 2);
755                 log.println(txtRange);
756                 locRes = txtRange.equals(text.substring(1, chCount - 2));
757                 res &= locRes;
758 
759                 if (!locRes) {
760                     log.println("Was expected: " +
761                                 text.substring(1, chCount - 2));
762                 }
763             }
764 
765             log.print("getTextRange(0, chCount-1): ");
766 
767             txtRange = oObj.getTextRange(0, chCount - 1);
768             log.println(txtRange);
769             locRes = txtRange.equals(text.substring(0, chCount - 1));
770             res &= locRes;
771 
772             if (!locRes) {
773                 log.println("Was expected: " +
774                             text.substring(0, chCount - 1));
775             }
776 
777             log.print("getTextRange(chCount, 0): ");
778             txtRange = oObj.getTextRange(chCount, 0);
779             log.println(txtRange);
780             res &= txtRange.equals(text);
781 
782             log.print("getTextRange(0, 0): ");
783             txtRange = oObj.getTextRange(0, 0);
784             log.println(txtRange);
785             locRes = txtRange.equals("");
786             res &= locRes;
787 
788             if (!locRes) {
789                 log.println("Empty string was expected");
790             }
791         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
792             log.println("Unexpected exception");
793             e.printStackTrace(log);
794             res &= false;
795         }
796 
797         try {
798             log.print("getTextRange(-1, chCount - 1): ");
799 
800             txtRange = oObj.getTextRange(-1, chCount - 1);
801             log.println("Exception was expected");
802             res &= false;
803         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
804             log.println("Expected exception");
805             res &= true;
806         }
807 
808         try {
809             log.print("getTextRange(0, chCount + 1): ");
810 
811             txtRange = oObj.getTextRange(0, chCount + 1);
812             log.println("Exception was expected");
813             res &= false;
814         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
815             log.println("Expected exception");
816             res &= true;
817         }
818 
819         try {
820             log.print("getTextRange(chCount+1, -1): ");
821 
822             txtRange = oObj.getTextRange(chCount + 1, -1);
823             log.println("Exception was expected");
824             res &= false;
825         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
826             log.println("Expected exception");
827             res &= true;
828         }
829 
830         tRes.tested("getTextRange()", res);
831     }
832 
833     /**
834      * Calls the method with invalid parameters an with valid parameters,
835      * checks returned values.
836      * Has OK status if exception was thrown for invalid parameters,
837      * if exception wasn't thrown for valid parameters and if returned values
838      * are equal to corresponding substrings of the text received by relation.
839      * The following method tests are to be executed before:
840      * <ul>
841      *  <li> <code>getCharacterCount()</code> </li>
842      * </ul>
843      */
_getTextAtIndex()844     public void _getTextAtIndex() {
845         requiredMethod("getCharacterCount()");
846 		TextSegment txt = null;
847         boolean res = true;
848 
849         try {
850             log.print("getTextAtIndex(-1, AccessibleTextType.PARAGRAPH):");
851 
852             txt = oObj.getTextAtIndex(-1,
853                                                   AccessibleTextType.PARAGRAPH);
854             log.println("Exception was expected");
855             res &= false;
856         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
857             log.println("Expected exception");
858             res &= true;
859         } catch (com.sun.star.lang.IllegalArgumentException e) {
860             log.println("UnExpected exception");
861             res &= false;
862         }
863 
864         try {
865             log.print("getTextAtIndex(chCount+1," +
866                       " AccessibleTextType.PARAGRAPH):");
867 
868             txt = oObj.getTextAtIndex(chCount + 1,
869                                                   AccessibleTextType.PARAGRAPH);
870             log.println("Exception was expected");
871             res &= false;
872         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
873             log.println("Expected exception");
874             res &= true;
875         } catch (com.sun.star.lang.IllegalArgumentException e) {
876             log.println("UnExpected exception");
877             res &= false;
878         }
879 
880         try {
881             log.print("getTextAtIndex(chCount," +
882                       " AccessibleTextType.WORD):");
883 
884             txt = oObj.getTextAtIndex(chCount, AccessibleTextType.WORD);
885             log.println("'" + txt.SegmentText + "'");
886             res &= compareLength(0,txt.SegmentText);
887             if (!tEnv.getTestCase().getObjectName().equals("SmGraphicAccessible")) {
888                 log.print("getTextAtIndex(1," +
889                           " AccessibleTextType.PARAGRAPH):");
890                 txt = oObj.getTextAtIndex(1, AccessibleTextType.PARAGRAPH);
891                 log.println("'" + txt.SegmentText + "'");
892                 res &= compareStrings(text,txt.SegmentText);
893             }
894         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
895             log.println("Unexpected exception");
896             e.printStackTrace(log);
897             res &= false;
898         } catch (com.sun.star.lang.IllegalArgumentException e) {
899             log.println("Unexpected exception");
900             e.printStackTrace(log);
901             res &= false;
902         }
903 
904         tRes.tested("getTextAtIndex()", res);
905     }
906 
907     /**
908      * Calls the method with invalid parameters an with valid parameters,
909      * checks returned values.
910      * Has OK status if exception was thrown for invalid parameters,
911      * if exception wasn't thrown for valid parameters and if returned values
912      * are equal to corresponding substrings of the text received by relation.
913      * The following method tests are to be executed before:
914      * <ul>
915      *  <li> <code>getCharacterCount()</code> </li>
916      * </ul>
917      */
_getTextBeforeIndex()918     public void _getTextBeforeIndex() {
919         requiredMethod("getCharacterCount()");
920 		TextSegment txt = null;
921         boolean res = true;
922 
923         try {
924             log.print("getTextBeforeIndex(-1, AccessibleTextType.PARAGRAPH):");
925 
926             txt = oObj.getTextBeforeIndex(-1,
927                                                       AccessibleTextType.PARAGRAPH);
928             log.println("Exception was expected");
929             res &= false;
930         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
931             log.println("Expected exception");
932             res &= true;
933         } catch (com.sun.star.lang.IllegalArgumentException e) {
934             log.println("UnExpected exception");
935             res &= false;
936         }
937 
938         try {
939             log.print("getTextBeforeIndex(chCount+1, " +
940                       "AccessibleTextType.PARAGRAPH):");
941 
942             txt = oObj.getTextBeforeIndex(chCount + 1,
943                                                       AccessibleTextType.PARAGRAPH);
944             log.println("Exception was expected");
945             res &= false;
946         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
947             log.println("Expected exception");
948             res &= true;
949         } catch (com.sun.star.lang.IllegalArgumentException e) {
950             log.println("UnExpected exception");
951             res &= true;
952         }
953 
954         try {
955             if (!tEnv.getTestCase().getObjectName().equals("SmGraphicAccessible")) {
956                 log.print("getTextBeforeIndex(chCount," +
957                           " AccessibleTextType.WORD):");
958 
959                 txt = oObj.getTextBeforeIndex(chCount,
960                                                      AccessibleTextType.WORD);
961                 log.println("'" + txt.SegmentText + "'");
962                 res &= compareLength(chCount, txt.SegmentText);
963             }
964 
965             log.print("getTextBeforeIndex(1," +
966                       " AccessibleTextType.PARAGRAPH):");
967             txt = oObj.getTextBeforeIndex(1, AccessibleTextType.PARAGRAPH);
968             log.println("'" + txt.SegmentText + "'");
969             res &= compareLength(0, txt.SegmentText);
970 
971             log.print("getTextBeforeIndex(chCount-1," +
972                       " AccessibleTextType.CHARACTER):");
973             txt = oObj.getTextBeforeIndex(chCount - 1,
974                                           AccessibleTextType.CHARACTER);
975             log.println("'" + txt.SegmentText + "'");
976             res &= compareStrings(text.substring(chCount - 2, chCount - 1),
977                                   txt.SegmentText);
978 
979             if (chCount > 2) {
980                 log.print("getTextBeforeIndex(2," +
981                           " AccessibleTextType.CHARACTER):");
982                 txt = oObj.getTextBeforeIndex(2, AccessibleTextType.CHARACTER);
983                 log.println("'" + txt.SegmentText + "'");
984                 res &= compareStrings(text.substring(1, 2), txt.SegmentText);
985             }
986         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
987             log.println("Unexpected exception");
988             e.printStackTrace(log);
989             res &= false;
990         } catch (com.sun.star.lang.IllegalArgumentException e) {
991             log.println("Unexpected exception");
992             e.printStackTrace(log);
993             res &= false;
994         }
995 
996         tRes.tested("getTextBeforeIndex()", res);
997     }
998 
999     /**
1000      * Calls the method with invalid parameters an with valid parameters,
1001      * checks returned values.
1002      * Has OK status if exception was thrown for invalid parameters,
1003      * if exception wasn't thrown for valid parameters and if returned values
1004      * are equal to corresponding substrings of the text received by relation.
1005      * The following method tests are to be executed before:
1006      * <ul>
1007      *  <li> <code>getCharacterCount()</code> </li>
1008      * </ul>
1009      */
_getTextBehindIndex()1010     public void _getTextBehindIndex() {
1011         requiredMethod("getCharacterCount()");
1012 		TextSegment txt = null;
1013         boolean res = true;
1014 
1015         try {
1016             log.print("getTextBehindIndex(-1, AccessibleTextType.PARAGRAPH):");
1017 
1018             txt = oObj.getTextBehindIndex(-1,
1019                                                       AccessibleTextType.PARAGRAPH);
1020             log.println("Exception was expected");
1021             res &= false;
1022         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
1023             log.println("Expected exception");
1024             res &= true;
1025         } catch (com.sun.star.lang.IllegalArgumentException e) {
1026             log.println("UnExpected exception");
1027             res &= true;
1028         }
1029 
1030         try {
1031             log.print("getTextBehindIndex(chCount+1, " +
1032                       "AccessibleTextType.PARAGRAPH):");
1033 
1034             txt = oObj.getTextBehindIndex(chCount + 1,
1035                                                       AccessibleTextType.PARAGRAPH);
1036             log.println("Exception was expected");
1037             res &= false;
1038         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
1039             log.println("Expected exception");
1040             res &= true;
1041         } catch (com.sun.star.lang.IllegalArgumentException e) {
1042             log.println("UnExpected exception");
1043             res &= true;
1044         }
1045 
1046         try {
1047             log.print("getTextBehindIndex(chCount," +
1048                       " AccessibleTextType.PARAGRAPH):");
1049 
1050             txt = oObj.getTextBehindIndex(chCount,
1051                                                  AccessibleTextType.PARAGRAPH);
1052             log.println("'" + txt.SegmentText + "'");
1053             res &= (txt.SegmentText.length() == 0);
1054 
1055             log.print("getTextBehindIndex(chCount-1," +
1056                       " AccessibleTextType.PARAGRAPH):");
1057             txt = oObj.getTextBehindIndex(chCount - 1,
1058                                           AccessibleTextType.PARAGRAPH);
1059             log.println("'" + txt.SegmentText + "'");
1060             res &= (txt.SegmentText.length() == 0);
1061 
1062             log.print("getTextBehindIndex(1," +
1063                       " AccessibleTextType.CHARACTER):");
1064             txt = oObj.getTextBehindIndex(1, AccessibleTextType.CHARACTER);
1065             log.println("'" + txt.SegmentText + "'");
1066             res &= txt.SegmentText.equals(text.substring(2, 3));
1067 
1068             if (chCount > 2) {
1069                 log.print("getTextBehindIndex(chCount-2," +
1070                           " AccessibleTextType.CHARACTER):");
1071                 txt = oObj.getTextBehindIndex(chCount - 2,
1072                                               AccessibleTextType.CHARACTER);
1073                 log.println("'" + txt.SegmentText + "'");
1074                 res &= txt.SegmentText.equals(text.substring(chCount - 1, chCount));
1075             }
1076         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
1077             log.println("Unexpected exception");
1078             e.printStackTrace(log);
1079             res &= false;
1080         } catch (com.sun.star.lang.IllegalArgumentException e) {
1081             log.println("Unexpected exception");
1082             e.printStackTrace(log);
1083             res &= false;
1084         }
1085 
1086         tRes.tested("getTextBehindIndex()", res);
1087     }
1088 
1089     /**
1090      * Calls the method with invalid parameters an with valid parameter,
1091      * checks returned values.
1092      * Has OK status if exception was thrown for invalid parameters,
1093      * if exception wasn't thrown for valid parameter and if returned value for
1094      * valid parameter is equal to <code>true</code>.
1095      */
_copyText()1096     public void _copyText() {
1097         boolean res = true;
1098         boolean locRes = true;
1099 
1100         if (editOnly != null) {
1101             log.println(editOnly);
1102             throw new StatusException(Status.skipped(true));
1103         }
1104 
1105         try {
1106             log.print("copyText(-1,chCount):");
1107             oObj.copyText(-1, chCount);
1108             log.println("Exception was expected");
1109             res &= false;
1110         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
1111             log.println("Expected exception");
1112             res &= true;
1113         }
1114 
1115         try {
1116             log.print("copyText(0,chCount+1):");
1117             oObj.copyText(0, chCount + 1);
1118             log.println("Exception was expected");
1119             res &= false;
1120         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
1121             log.println("Expected exception");
1122             res &= true;
1123         }
1124 
1125         try {
1126             log.print("copyText(0,chCount):");
1127             locRes = oObj.copyText(0, chCount);
1128             log.println(locRes);
1129             res &= locRes;
1130 
1131             String cbText = null;
1132 
1133             try {
1134                 cbText = util.SysUtils.getSysClipboardText((XMultiServiceFactory)tParam.getMSF());
1135             } catch (com.sun.star.uno.Exception e) {
1136                 log.println("Couldn't access system clipboard :");
1137                 e.printStackTrace(log);
1138             }
1139 
1140             log.println("Clipboard: '" + cbText + "'");
1141             res &= text.equals(cbText);
1142 
1143             if (chCount > 2) {
1144                 log.print("copyText(1,chCount-1):");
1145                 locRes = oObj.copyText(1, chCount - 1);
1146                 log.println(locRes);
1147                 res &= locRes;
1148 
1149                 try {
1150                     cbText = util.SysUtils.getSysClipboardText((XMultiServiceFactory)tParam.getMSF());
1151                 } catch (com.sun.star.uno.Exception e) {
1152                     log.println("Couldn't access system clipboard :");
1153                     e.printStackTrace(log);
1154                 }
1155 
1156                 log.println("Clipboard: '" + cbText + "'");
1157                 res &= text.substring(1, chCount - 1).equals(cbText);
1158             }
1159         } catch (com.sun.star.lang.IndexOutOfBoundsException e) {
1160             log.println("Unexpected exception");
1161             e.printStackTrace(log);
1162             res &= false;
1163         }
1164 
1165         tRes.tested("copyText()", res);
1166     }
1167 
compareStrings(String expected, String getting)1168     public boolean compareStrings(String expected, String getting) {
1169         boolean res = expected.equals(getting);
1170 
1171         if (!res) {
1172             log.println("## The result isn't the expected:");
1173             log.println("\tGetting: " + getting);
1174             log.println("\tExpected: " + expected);
1175         }
1176 
1177         return res;
1178     }
1179 
compareLength(int expected, String getting)1180     public boolean compareLength(int expected, String getting) {
1181         boolean res = (expected == getting.length());
1182 
1183         if (!res) {
1184             log.println("## The result isn't the expected:");
1185             log.println("\tGetting: " + getting.length());
1186             log.println("\tExpected: " + expected);
1187         }
1188 
1189         return res;
1190     }
1191 }