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