1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  *   Licensed to the Apache Software Foundation (ASF) under one or more
11  *   contributor license agreements. See the NOTICE file distributed
12  *   with this work for additional information regarding copyright
13  *   ownership. The ASF licenses this file to you under the Apache
14  *   License, Version 2.0 (the "License"); you may not use this file
15  *   except in compliance with the License. You may obtain a copy of
16  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 
19 package ifc.accessibility;
20 
21 import lib.MultiMethodTest;
22 import util.ValueComparer;
23 
24 import com.sun.star.accessibility.XAccessibleEditableText;
25 import com.sun.star.beans.PropertyValue;
26 
27 /**
28  * Testing <code>com.sun.star.accessibility.XAccessibleEditableText</code>
29  * interface methods :
30  * <ul>
31  *  <li><code> cutText()</code></li>
32  *  <li><code> pasteText()</code></li>
33  *  <li><code> deleteText()</code></li>
34  *  <li><code> insertText()</code></li>
35  *  <li><code> replaceText()</code></li>
36  *  <li><code> setAttributes()</code></li>
37  *  <li><code> setText()</code></li>
38  * </ul> <p>
39  *
40  * This test needs the following object relations :
41  * <ul>
42  *  <li> <code>'XAccessibleEditableText.hasAttr'</code>
43  *  (of type <code>Boolean</code>):
44  *   Indicates whether or not the text has changeable attributes.
45  *   E.g. text within writer document have attributes which can
46  *   be changed, while the text within edit field has fixed
47  *   attributes. <p>
48  *   If the relation is <code>false</code> then the component
49  *   has fixed text attributes. </li>
50  * </ul> <p>
51  *
52  * @see com.sun.star.accessibility.XAccessibleEditableText
53  */
54 public class _XAccessibleEditableText extends MultiMethodTest {
55 
56     public XAccessibleEditableText oObj = null;
57 
58 
59     String pasteText = null;
60 
61     String initialText = "";
62 
63     /**
64      * Indicates whether or not the text has changeable attributes.
65      * E.g. text within writer document have attributes which can
66      * be changed, while the text within edit field has fixed
67      * attributes.
68      */
69      private boolean changeableAttr = true;
70 
71      /**
72       * Retrieves object relation. Stores initial component text
73       * for restoring it in <code>after</code>.
74       */
75      @Override
before()76     protected void before() {
77         Boolean b = (Boolean)
78             tEnv.getObjRelation("XAccessibleEditableText.hasAttr");
79         if (b != null) {
80             changeableAttr = b.booleanValue();
81         }
82 
83         initialText = oObj.getText();
84      }
85 
86     /**
87      * Calls the method with the wrong indexes and with the correct indexes.
88      * Stores cutted text in the variable <code>pasteText</code>.
89      * Has OK status if exceptions were thrown for the wrong indexes,
90      * if exception wasn't thrown for the correct indexes.
91      */
_cutText()92     public void _cutText() {
93         boolean res = true;
94         boolean locRes = true;
95         String curText = null;
96 
97         String oldText = oObj.getText();
98         log.println("Text: '" + oldText + "'");
99         int length = oObj.getCharacterCount();
100         log.println("Character count: " + length);
101 
102         try {
103             log.print("cutText(-1," + (length-1) + "): ");
104             locRes = oObj.cutText(-1, length - 1);
105             log.println(locRes);
106             log.println("exception was expected => FAILED");
107             res &= false;
108         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
109             log.println("expected exception => OK");
110             curText = oObj.getText();
111             log.println("Current text: '" + curText + "'");
112             res &= curText.equals(oldText);
113         }
114 
115         try {
116             log.print("cutText(0," + (length+1) + "): ");
117             locRes = oObj.cutText(0, length + 1);
118             log.println(locRes);
119             log.println("exception was expected => FAILED");
120             res &= false;
121         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
122             log.println("expected exception => OK");
123             curText = oObj.getText();
124             log.println("Current text: '" + curText + "'");
125             res &= curText.equals(oldText);
126         }
127 
128         try {
129             pasteText = oldText;
130             log.print("cutText(0," + length + "): ");
131             locRes = oObj.cutText(0, length);
132             log.println(locRes);
133             curText = oObj.getText();
134             log.println("Current text: '" + curText + "'");
135             res &= curText.length() == 0 && locRes;
136         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
137             log.println("unexpected exception");
138             e.printStackTrace(log);
139             res &= false;
140         }
141 
142         tRes.tested("cutText()", res);
143     }
144 
145     /**
146      * Calls the method with the wrong indexes and with the correct indexes.
147      * Has OK status if exceptions were thrown for the wrong indexes,
148      * if exception wasn't thrown for the correct indexes and if cutted text was
149      * pasted.
150      * The following method tests are to be executed before:
151      * <ul>
152      *  <li> <code>cutText()</code> </li>
153      * </ul>
154      */
_pasteText()155     public void _pasteText() {
156         requiredMethod("cutText()");
157         boolean res = true;
158         boolean locRes = true;
159         String curText = null;
160 
161         String text = oObj.getText();
162         log.println("Text: '" + text + "'");
163         int length = oObj.getCharacterCount();
164         log.println("Character count: " + length);
165 
166         try {
167             log.print("pasteText(-1): ");
168             locRes = oObj.pasteText(-1);
169             log.println(locRes);
170             log.println("exception was expected => FAILED");
171             res &= false;
172         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
173             log.println("expected exception => OK");
174             curText = oObj.getText();
175             log.println("Current text: '" + curText + "'");
176             res &= curText.equals(text);
177         }
178 
179         try {
180             log.print("pasteText(" + (length+1) + "): ");
181             locRes = oObj.pasteText(length + 1);
182             log.println(locRes);
183             log.println("exception was expected => FAILED");
184             res &= false;
185         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
186             log.println("expected exception => OK");
187             curText = oObj.getText();
188             log.println("Current text: '" + curText + "'");
189             res &= curText.equals(text);
190         }
191 
192         try {
193             log.print("pasteText(" + (length) + "): ");
194             locRes = oObj.pasteText(length);
195             log.println(locRes);
196             curText = oObj.getText();
197             log.println("Current text: '" + curText + "'");
198             res &= curText.equals(text + pasteText) && locRes;
199             log.println("Expected text: '" + text + pasteText + "'");
200         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
201             log.println("unexpected exception");
202             e.printStackTrace(log);
203             res &= false;
204         }
205 
206         tRes.tested("pasteText()", res);
207     }
208 
209     /**
210      * Calls the method with the wrong indexes and with the correct indexes,
211      * checks text after method call.
212      * Has OK status if exceptions were thrown for the wrong indexes,
213      * if exception wasn't thrown for the correct indexes and if deleted string
214      * was really deleted from the text.
215      * The following method tests are to be executed before:
216      * <ul>
217      *  <li> <code>insertText()</code> </li>
218      * </ul>
219      */
_deleteText()220     public void _deleteText() {
221         executeMethod("insertText()");
222         boolean res = true;
223         boolean locRes = true;
224         String curText = null;
225 
226         String text = oObj.getText();
227         log.println("Text: '" + text + "'");
228         int length = oObj.getCharacterCount();
229         log.println("Character count: " + length);
230 
231         try {
232             log.print("deleteText(-1," + length + "): ");
233             locRes = oObj.deleteText(-1, length);
234             log.println(locRes);
235             log.println("exception was expected => FAILED");
236             res &= false;
237         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
238             log.println("expected exception => OK");
239             curText = oObj.getText();
240             log.println("Current text: '" + curText + "'");
241             res &= curText.equals(text);
242         }
243 
244         try {
245             log.print("deleteText(0," + (length+1) + "): ");
246             locRes = oObj.deleteText(0, length + 1);
247             log.println(locRes);
248             log.println("exception was expected => FAILED");
249             res &= false;
250         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
251             log.println("expected exception => OK");
252             curText = oObj.getText();
253             log.println("Current text: '" + curText + "'");
254             res &= curText.equals(text);
255         }
256 
257         try {
258             if (length >= 1) {
259                 log.print("deleteText(" + (length-1) + "," + (length) + "): ");
260                 locRes = oObj.deleteText(length - 1, length);
261                 log.println(locRes);
262                 String expStr = text.substring(0, length - 1);
263                 curText = oObj.getText();
264                 log.println("Current text: '" + curText + "'");
265                 res &= curText.equals(expStr);
266                 log.println("Expected text: '" + expStr + "'");
267             }
268         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
269             log.println("unexpected exception");
270             e.printStackTrace(log);
271             res &= false;
272         }
273 
274         tRes.tested("deleteText()", res);
275     }
276 
277     /**
278      * Calls the method with the wrong indexes and with the correct indexes,
279      * checks text after method call.
280      * Has OK status if exceptions were thrown for the wrong indexes,
281      * if exception wasn't thrown for the correct indexes and if inserted string
282      * was really inserted into the text.
283      * The following method tests are to be executed before:
284      * <ul>
285      *  <li> <code>pasteText()</code> </li>
286      * </ul>
287      */
_insertText()288     public void _insertText() {
289         executeMethod("pasteText()");
290         boolean res = true;
291         boolean locRes = true;
292         String curText = null;
293 
294         String text = oObj.getText();
295         log.println("Text: '" + text + "'");
296         int length = oObj.getCharacterCount();
297         log.println("Character count: " + length);
298 
299         final String insStr = "Inserted string";
300 
301         try {
302             log.print("insertText(insStr, -1): ");
303             locRes = oObj.insertText(insStr, -1);
304             log.println(locRes);
305             log.println("exception was expected=> FAILED");
306             res &= false;
307         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
308             log.println("expected exception => OK");
309             curText = oObj.getText();
310             log.println("Current text: '" + curText + "'");
311             res &= curText.equals(text);
312         }
313 
314         try {
315             log.print("insertText(insStr," + (length+1) + "): ");
316             locRes = oObj.insertText(insStr, length+1);
317             log.println(locRes);
318             log.println("exception was expected => FAILED");
319             res &= false;
320         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
321             log.println("expected exception => OK");
322             curText = oObj.getText();
323             log.println("Current text: '" + curText + "'");
324             res &= curText.equals(text);
325         }
326 
327         try {
328             log.print("insertText(insStr," + length + "): ");
329             locRes = oObj.insertText(insStr, length);
330             log.println(locRes);
331             curText = oObj.getText();
332             res &= curText.equals(text + insStr);
333             log.println("Current text: '" + curText + "'");
334             log.println("Expected text: '" + text + insStr + "'");
335         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
336             log.println("unexpected exception => FAILED");
337             e.printStackTrace(log);
338             res &= false;
339         }
340 
341         tRes.tested("insertText()", res);
342     }
343 
344     /**
345      * Calls the method with the wrong indexes and with the correct indexes,
346      * checks text after method call.
347      * Has OK status if exceptions were thrown for the wrong indexes,
348      * if exception wasn't thrown for the correct indexes and if part of text
349      * was really replaced by the specified replacement string.
350      * The following method tests are to be executed before:
351      * <ul>
352      *  <li> <code>deleteText()</code> </li>
353      * </ul>
354      */
_replaceText()355     public void _replaceText() {
356         executeMethod("deleteText()");
357         boolean res = true;
358         boolean locRes = true;
359         String curText = null;
360 
361         final String sReplacement = "String for replace";
362         String oldText = oObj.getText();
363         int startIndx = oldText.length();
364         oObj.setText(oldText + " part of string for replace");
365 
366         String text = oObj.getText();
367         log.println("Text: '" + text + "'");
368         int length = oObj.getCharacterCount();
369         log.println("Character count: " + length);
370 
371         try {
372             log.print("replaceText(-1," + length + "): ");
373             locRes = oObj.replaceText(-1, length, sReplacement);
374             log.println(locRes);
375             log.println("exception was expected => FAILED");
376             res &= false;
377         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
378             log.println("expected exception => OK");
379             curText = oObj.getText();
380             log.println("Current text: '" + curText + "'");
381             res &= curText.equals(text);
382         }
383 
384         try {
385             log.print("replaceText(0," + (length+1) + "): ");
386             locRes = oObj.replaceText(0, length + 1, sReplacement);
387             log.println(locRes);
388             log.println("exception was expected => FAILED");
389             res &= false;
390         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
391             log.println("expected exception => OK");
392             curText = oObj.getText();
393             log.println("Current text: '" + curText + "'");
394             res &= curText.equals(text);
395         }
396 
397         try {
398             log.print("replaceText(" + startIndx + "," + length + "): ");
399             locRes = oObj.replaceText(startIndx, length, sReplacement);
400             log.println(locRes);
401             curText = oObj.getText();
402             log.println("Current text: '" + curText + "'");
403             log.println("Expected text: '" + oldText + sReplacement + "'");
404             res &= curText.equals(oldText + sReplacement);
405         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
406             log.println("unexpected exception");
407             e.printStackTrace(log);
408             res &= false;
409         }
410 
411         tRes.tested("replaceText()", res);
412     }
413 
414     /**
415      * Calls the method with the wrong indexes and with the correct indexes,
416      * checks attributes after method call.
417      * Has OK status if exceptions were thrown for the wrong indexes,
418      * if exception wasn't thrown for the correct indexes and if attributes
419      * of text was changed.
420      * The following method tests are to be executed before:
421      * <ul>
422      *  <li> <code>replaceText()</code> </li>
423      * </ul>
424      */
_setAttributes()425     public void _setAttributes() {
426         executeMethod("replaceText()");
427         boolean res = true;
428         boolean locRes = true;
429 
430         String text = oObj.getText();
431         log.println("Text: '" + text + "'");
432         int length = oObj.getCharacterCount();
433         log.println("Length: " + length);
434 
435         PropertyValue[] attrs = null;
436 
437         try {
438             attrs = oObj.getCharacterAttributes(0, new String[]{""});
439             log.print("setAttributes(-1," + (length - 1) + "):");
440             locRes = oObj.setAttributes(-1, length - 1, attrs);
441             log.println(locRes);
442             log.println("exception was expected => FAILED");
443             res &= false;
444         } catch(com.sun.star.lang.IndexOutOfBoundsException e) {
445             log.println("expected exception => OK");
446             res &= true;
447         } catch(com.sun.star.beans.UnknownPropertyException e) {
448             log.println("unexpected exception => FAILED");
449             e.printStackTrace(log);
450             res &= false;
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 = Integer.valueOf(-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         } catch(com.sun.star.beans.UnknownPropertyException 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      */
_setText()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     @Override
after()540     protected void after() {
541         oObj.setText(initialText);
542     }
543 }
544