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.accessibility;
25 
26 import lib.MultiMethodTest;
27 import util.ValueComparer;
28 
29 import com.sun.star.accessibility.XAccessibleEditableText;
30 import com.sun.star.beans.PropertyValue;
31 
32 /**
33  * Testing <code>com.sun.star.accessibility.XAccessibleEditableText</code>
34  * interface methods :
35  * <ul>
36  *  <li><code> cutText()</code></li>
37  *  <li><code> pasteText()</code></li>
38  *  <li><code> deleteText()</code></li>
39  *  <li><code> insertText()</code></li>
40  *  <li><code> replaceText()</code></li>
41  *  <li><code> setAttributes()</code></li>
42  *  <li><code> setText()</code></li>
43  * </ul> <p>
44  *
45  * This test needs the following object relations :
46  * <ul>
47  *  <li> <code>'XAccessibleEditableText.hasAttr'</code>
48  *  (of type <code>Boolean</code>):
49  *   Indicates whether or not the text has changeable attributes.
50  *   E.g. text within writer document have attributes which can
51  *   be changed, while the text within edit field has fixed
52  *   attributes. <p>
53  *   If the relation is <code>false</code> then the component
54  *   has fixed text attributes. </li>
55  * </ul> <p>
56  *
57  * @see com.sun.star.accessibility.XAccessibleEditableText
58  */
59 public class _XAccessibleEditableText extends MultiMethodTest {
60 
61     public XAccessibleEditableText oObj = null;
62 
63 
64     String pasteText = null;
65 
66     String initialText = "";
67 
68     /**
69      * Indicates whether or not the text has changeable attributes.
70      * E.g. text within writer document have attributes which can
71      * be changed, while the text within edit field has fixed
72      * attributes.
73      */
74      private boolean changeableAttr = true;
75 
76      /**
77       * Retrieves object relation. Stores initial component text
78       * for restoding it in <code>after</code>.
79       */
before()80      protected void before() {
81         Boolean b = (Boolean)
82             tEnv.getObjRelation("XAccessibleEditableText.hasAttr");
83         if (b != null) {
84             changeableAttr = b.booleanValue();
85         }
86 
87         initialText = oObj.getText();
88      }
89 
90     /**
91      * Calls the method with the wrong indexes and with the correct indexes.
92      * Stores cutted text in the variable <code>pasteText</code>.
93      * Has OK status if exceptions were thrown for the wrong indexes,
94      * if exception wasn't thrown for the correct indexes.
95      */
_cutText()96     public void _cutText() {
97         boolean res = true;
98         boolean locRes = true;
99         String curText = null;
100 
101         String oldText = oObj.getText();
102         log.println("Text: '" + oldText + "'");
103         int length = oObj.getCharacterCount();
104         log.println("Character count: " + length);
105 
106         try {
107             log.print("cutText(-1," + (length-1) + "): ");
108             locRes = oObj.cutText(-1, length - 1);
109             log.println(locRes);
110             log.println("exception was expected => FAILED");
111             res &= false;
112         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
113             log.println("expected exception => OK");
114             curText = oObj.getText();
115             log.println("Current text: '" + curText + "'");
116             res &= curText.equals(oldText);
117         }
118 
119         try {
120             log.print("cutText(0," + (length+1) + "): ");
121             locRes = oObj.cutText(0, length + 1);
122             log.println(locRes);
123             log.println("exception was expected => FAILED");
124             res &= false;
125         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
126             log.println("expected exception => OK");
127             curText = oObj.getText();
128             log.println("Current text: '" + curText + "'");
129             res &= curText.equals(oldText);
130         }
131 
132         try {
133             pasteText = oldText;
134             log.print("cutText(0," + length + "): ");
135             locRes = oObj.cutText(0, length);
136             log.println(locRes);
137             curText = oObj.getText();
138             log.println("Current text: '" + curText + "'");
139             res &= curText.length() == 0 && locRes;
140         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
141             log.println("unexpected exception");
142             e.printStackTrace(log);
143             res &= false;
144         }
145 
146         tRes.tested("cutText()", res);
147     }
148 
149     /**
150      * Calls the method with the wrong indexes and with the correct indexes.
151      * Has OK status if exceptions were thrown for the wrong indexes,
152      * if exception wasn't thrown for the correct indexes and if cutted text was
153      * pasted.
154      * The following method tests are to be executed before:
155      * <ul>
156      *  <li> <code>cutText()</code> </li>
157      * </ul>
158      */
_pasteText()159     public void _pasteText() {
160         requiredMethod("cutText()");
161         boolean res = true;
162         boolean locRes = true;
163         String curText = null;
164 
165         String text = oObj.getText();
166         log.println("Text: '" + text + "'");
167         int length = oObj.getCharacterCount();
168         log.println("Character count: " + length);
169 
170         try {
171             log.print("pasteText(-1): ");
172             locRes = oObj.pasteText(-1);
173             log.println(locRes);
174             log.println("exception was expected => FAILED");
175             res &= false;
176         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
177             log.println("expected exception => OK");
178             curText = oObj.getText();
179             log.println("Current text: '" + curText + "'");
180             res &= curText.equals(text);
181         }
182 
183         try {
184             log.print("pasteText(" + (length+1) + "): ");
185             locRes = oObj.pasteText(length + 1);
186             log.println(locRes);
187             log.println("exception was expected => FAILED");
188             res &= false;
189         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
190             log.println("expected exception => OK");
191             curText = oObj.getText();
192             log.println("Current text: '" + curText + "'");
193             res &= curText.equals(text);
194         }
195 
196         try {
197             log.print("pasteText(" + (length) + "): ");
198             locRes = oObj.pasteText(length);
199             log.println(locRes);
200             curText = oObj.getText();
201             log.println("Current text: '" + curText + "'");
202             res &= curText.equals(text + pasteText) && locRes;
203             log.println("Expected text: '" + text + pasteText + "'");
204         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
205             log.println("unexpected exception");
206             e.printStackTrace(log);
207             res &= false;
208         }
209 
210         tRes.tested("pasteText()", res);
211     }
212 
213     /**
214      * Calls the method with the wrong indexes and with the correct indexes,
215      * checks text after method call.
216      * Has OK status if exceptions were thrown for the wrong indexes,
217      * if exception wasn't thrown for the correct indexes and if deleted string
218      * was really deleted from the text.
219      * The following method tests are to be executed before:
220      * <ul>
221      *  <li> <code>insertText()</code> </li>
222      * </ul>
223      */
_deleteText()224     public void _deleteText() {
225         executeMethod("insertText()");
226         boolean res = true;
227         boolean locRes = true;
228         String curText = null;
229 
230         String text = oObj.getText();
231         log.println("Text: '" + text + "'");
232         int length = oObj.getCharacterCount();
233         log.println("Character count: " + length);
234 
235         try {
236             log.print("deleteText(-1," + length + "): ");
237             locRes = oObj.deleteText(-1, length);
238             log.println(locRes);
239             log.println("exception was expected => FAILED");
240             res &= false;
241         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
242             log.println("expected exception => OK");
243             curText = oObj.getText();
244             log.println("Current text: '" + curText + "'");
245             res &= curText.equals(text);
246         }
247 
248         try {
249             log.print("deleteText(0," + (length+1) + "): ");
250             locRes = oObj.deleteText(0, length + 1);
251             log.println(locRes);
252             log.println("exception was expected => FAILED");
253             res &= false;
254         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
255             log.println("expected exception => OK");
256             curText = oObj.getText();
257             log.println("Current text: '" + curText + "'");
258             res &= curText.equals(text);
259         }
260 
261         try {
262             if (length >= 1) {
263                 log.print("deleteText(" + (length-1) + "," + (length) + "): ");
264                 locRes = oObj.deleteText(length - 1, length);
265                 log.println(locRes);
266                 String expStr = expStr = text.substring(0, length - 1);
267                 curText = oObj.getText();
268                 log.println("Current text: '" + curText + "'");
269                 res &= curText.equals(expStr);
270                 log.println("Expected text: '" + expStr + "'");
271             }
272         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
273             log.println("unexpected exception");
274             e.printStackTrace(log);
275             res &= false;
276         }
277 
278         tRes.tested("deleteText()", res);
279     }
280 
281     /**
282      * Calls the method with the wrong indexes and with the correct indexes,
283      * checks text after method call.
284      * Has OK status if exceptions were thrown for the wrong indexes,
285      * if exception wasn't thrown for the correct indexes and if inserted string
286      * was really inserted into the text.
287      * The following method tests are to be executed before:
288      * <ul>
289      *  <li> <code>pasteText()</code> </li>
290      * </ul>
291      */
_insertText()292     public void _insertText() {
293         executeMethod("pasteText()");
294         boolean res = true;
295         boolean locRes = true;
296         String curText = null;
297 
298         String text = oObj.getText();
299         log.println("Text: '" + text + "'");
300         int length = oObj.getCharacterCount();
301         log.println("Character count: " + length);
302 
303         final String insStr = "Inserted string";
304 
305         try {
306             log.print("insertText(insStr, -1): ");
307             locRes = oObj.insertText(insStr, -1);
308             log.println(locRes);
309             log.println("exception was expected=> FAILED");
310             res &= false;
311         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
312             log.println("expected exception => OK");
313             curText = oObj.getText();
314             log.println("Current text: '" + curText + "'");
315             res &= curText.equals(text);
316         }
317 
318         try {
319             log.print("insertText(insStr," + (length+1) + "): ");
320             locRes = oObj.insertText(insStr, length+1);
321             log.println(locRes);
322             log.println("exception was expected => FAILED");
323             res &= false;
324         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
325             log.println("expected exception => OK");
326             curText = oObj.getText();
327             log.println("Current text: '" + curText + "'");
328             res &= curText.equals(text);
329         }
330 
331         try {
332             log.print("insertText(insStr," + length + "): ");
333             locRes = oObj.insertText(insStr, length);
334             log.println(locRes);
335             curText = oObj.getText();
336             res &= curText.equals(text + insStr);
337             log.println("Current text: '" + curText + "'");
338             log.println("Expected text: '" + text + insStr + "'");
339         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
340             log.println("unexpected exception => FAILED");
341             e.printStackTrace(log);
342             res &= false;
343         }
344 
345         tRes.tested("insertText()", res);
346     }
347 
348     /**
349      * Calls the method with the wrong indexes and with the correct indexes,
350      * checks text after method call.
351      * Has OK status if exceptions were thrown for the wrong indexes,
352      * if exception wasn't thrown for the correct indexes and if part of text
353      * was really replaced by the specified replacement string.
354      * The following method tests are to be executed before:
355      * <ul>
356      *  <li> <code>deleteText()</code> </li>
357      * </ul>
358      */
_replaceText()359     public void _replaceText() {
360         executeMethod("deleteText()");
361         boolean res = true;
362         boolean locRes = true;
363         String curText = null;
364 
365         final String sReplacement = "String for replace";
366         String oldText = oObj.getText();
367         int startIndx = oldText.length();
368         oObj.setText(oldText + " part of string for replace");
369 
370         String text = oObj.getText();
371         log.println("Text: '" + text + "'");
372         int length = oObj.getCharacterCount();
373         log.println("Character count: " + length);
374 
375         try {
376             log.print("replaceText(-1," + length + "): ");
377             locRes = oObj.replaceText(-1, length, sReplacement);
378             log.println(locRes);
379             log.println("exception was expected => FAILED");
380             res &= false;
381         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
382             log.println("expected exception => OK");
383             curText = oObj.getText();
384             log.println("Current text: '" + curText + "'");
385             res &= curText.equals(text);
386         }
387 
388         try {
389             log.print("replaceText(0," + (length+1) + "): ");
390             locRes = oObj.replaceText(0, length + 1, sReplacement);
391             log.println(locRes);
392             log.println("exception was expected => FAILED");
393             res &= false;
394         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
395             log.println("expected exception => OK");
396             curText = oObj.getText();
397             log.println("Current text: '" + curText + "'");
398             res &= curText.equals(text);
399         }
400 
401         try {
402             log.print("replaceText(" + startIndx + "," + length + "): ");
403             locRes = oObj.replaceText(startIndx, length, sReplacement);
404             log.println(locRes);
405             curText = oObj.getText();
406             log.println("Current text: '" + curText + "'");
407             log.println("Expected text: '" + oldText + sReplacement + "'");
408             res &= curText.equals(oldText + sReplacement);
409         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
410             log.println("unexpected exception");
411             e.printStackTrace(log);
412             res &= false;
413         }
414 
415         tRes.tested("replaceText()", res);
416     }
417 
418     /**
419      * Calls the method with the wrong indexes and with the correct indexes,
420      * checks attributes after method call.
421      * Has OK status if exceptions were thrown for the wrong indexes,
422      * if exception wasn't thrown for the correct indexes and if attributes
423      * of text was changed.
424      * The following method tests are to be executed before:
425      * <ul>
426      *  <li> <code>replaceText()</code> </li>
427      * </ul>
428      */
_setAttributes()429     public void _setAttributes() {
430         executeMethod("replaceText()");
431         boolean res = true;
432         boolean locRes = true;
433 
434         String text = oObj.getText();
435         log.println("Text: '" + text + "'");
436         int length = oObj.getCharacterCount();
437         log.println("Length: " + length);
438 
439         PropertyValue[] attrs = null;
440 
441         try {
442             attrs = oObj.getCharacterAttributes(0, new String[]{""});
443             log.print("setAttributes(-1," + (length - 1) + "):");
444             locRes = oObj.setAttributes(-1, length - 1, attrs);
445             log.println(locRes);
446             log.println("exception was expected => FAILED");
447             res &= false;
448         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
449             log.println("expected exception => OK");
450             res &= true;
451         }
452 
453         try {
454             log.print("setAttributes(0," + (length+1) + "):");
455             locRes = oObj.setAttributes(0, length + 1, attrs);
456             log.println(locRes);
457             log.println("exception was expected => FAILED");
458             res &= false;
459         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
460             log.println("expected exception => OK");
461             res &= true;
462         }
463 
464         //change old attributes set
465         for(int i = 0; i < attrs.length; i++) {
466             if (attrs[i].Name.equals("CharColor")) {
467                 attrs[i].Value = new Integer(-2);
468             }
469         }
470 
471         try {
472             log.print("setAttributes(0," + length + "):");
473             locRes = oObj.setAttributes(0, length, attrs);
474             log.println(locRes);
475             res &= (changeableAttr && locRes)
476                 || (!changeableAttr && !locRes);
477             if (changeableAttr) {
478                 log.print("checking that new attributes was set...");
479                 PropertyValue[] newAttrs = oObj.getCharacterAttributes(0, new String[]{""});
480                 locRes = ValueComparer.equalValue(attrs, newAttrs);
481                 log.println(locRes);
482                 res &= locRes;
483             } else {
484                 log.println("Text attributes can't be changed.");
485             }
486         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
487             log.println("unexpected exception => FAILED");
488             e.printStackTrace(log);
489             res &= false;
490         }
491 
492         tRes.tested("setAttributes()", res);
493     }
494 
495     /**
496      * Calls the method with different parameters and checks text.
497      */
_setText()498     public void _setText() {
499         executeMethod("setAttributes()");
500         boolean res = true;
501         boolean locRes = true;
502 
503         String oldText = oObj.getText();
504         log.println("Current text: '" + oldText + "'");
505 
506         String newText = "New text";
507         log.print("setText('" + newText + "'): ");
508         locRes = oObj.setText(newText);
509         log.println(locRes);
510         String newCurText = oObj.getText();
511         log.println("getText(): '" + newCurText + "'");
512         res &= locRes && newCurText.equals(newText);
513 
514         newText = "";
515         log.print("setText('" + newText + "'): ");
516         locRes = oObj.setText(newText);
517         log.println(locRes);
518         newCurText = oObj.getText();
519         log.println("getText(): '" + newCurText + "'");
520         res &= locRes && newCurText.equals(newText);
521 
522         log.print("setText('" + oldText + "'): ");
523         locRes = oObj.setText(oldText);
524         log.println(locRes);
525         newCurText = oObj.getText();
526         log.println("getText(): '" + newCurText + "'");
527         res &= locRes && newCurText.equals(oldText);
528 
529         tRes.tested("setText()", res);
530     }
531 
532     /**
533      * Restores initial component text.
534      */
after()535     protected void after() {
536         oObj.setText(initialText);
537     }
538 }