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.awt;
29 
30 import lib.MultiMethodTest;
31 import util.ValueComparer;
32 
33 import com.sun.star.awt.Selection;
34 import com.sun.star.awt.TextEvent;
35 import com.sun.star.awt.XTextComponent;
36 import com.sun.star.awt.XTextListener;
37 import com.sun.star.lang.EventObject;
38 
39 /**
40 * Testing <code>com.sun.star.awt.XTextComponent</code>
41 * interface methods:
42 * <ul>
43 *  <li><code> addTextListener() </code></li>
44 *  <li><code> removeTextListener() </code></li>
45 *  <li><code> setText() </code></li>
46 *  <li><code> getText() </code></li>
47 *  <li><code> insertText() </code></li>
48 *  <li><code> getSelectedText() </code></li>
49 *  <li><code> setSelection() </code></li>
50 *  <li><code> getSelection() </code></li>
51 *  <li><code> setEditable() </code></li>
52 *  <li><code> isEditable() </code></li>
53 *  <li><code> setMaxTextLen() </code></li>
54 *  <li><code> getMaxTextLen() </code></li>
55 * </ul><p>
56 * This test needs the following object relations :
57 * <ul>
58 *  <li> <code>'XTextComponent.onlyNumbers'</code> (of type <code>Object</code>):
59 *  needed for checking if component can contain only numeric values </li>
60 * </ul><p>
61 * Test is <b> NOT </b> multithread compilant. <p>
62 * @see com.sun.star.awt.XTextComponent
63 */
64 public class _XTextComponent extends MultiMethodTest {
65     public XTextComponent oObj = null;
66     public boolean textChanged = false;
67     // indicates that component can contain only numeric values
68     private boolean num = false ;
69 
70     /**
71     * Listener implementation which just set flag when listener
72     * method is called.
73     */
74     protected class MyChangeListener implements XTextListener {
75         public void disposing ( EventObject oEvent ) {}
76         public void textChanged(TextEvent ev) {
77             textChanged = true;
78         }
79     }
80 
81     XTextListener listener = new MyChangeListener();
82 
83     /**
84     * Retrieves object relation, then sets flag 'num' to 'true'
85     * if relation is not null.
86     */
87     public void before() {
88         if (tEnv.getObjRelation("XTextComponent.onlyNumbers") != null)
89             num = true;
90     }
91 
92     /**
93     * After test calls the method, a new text is set to the object. Then
94     * we check if listener was called, and set a new text value
95     * to the object.<p>
96     * Has <b> OK </b> status if listener was called.
97     */
98     public void _addTextListener(){
99         oObj.addTextListener(listener);
100         oObj.setText("Listen");
101         try {
102             Thread.sleep(500);
103         } catch(java.lang.InterruptedException e) {
104             e.printStackTrace(log);
105         }
106         if (!textChanged) {
107             log.println("Listener wasn't called after changing Text");
108         }
109 
110         tRes.tested("addTextListener()",textChanged);
111     }
112 
113     /**
114     * After setting flag 'textChanged' to false, test calls the method.
115     * Then a new text value is set to the object. <p>
116     * Has <b> OK </b> status if listener was not called. <p>
117     * The following method tests are to be completed successfully before :
118     * <ul>
119     *  <li><code> addTextListener() </code>: adds listener to the object.</li>
120     * </ul>
121     */
122     public void _removeTextListener() {
123         requiredMethod("addTextListener()");
124         textChanged = false;
125         oObj.removeTextListener(listener);
126         oObj.setText("Do not listen");
127         tRes.tested("removeTextListener()",!textChanged);
128     }
129 
130     /**
131     * At first we're setting some string variable 'newText' depending of a kind
132     * of object we are working with. Then test calls the method. <p>
133     * Has <b> OK </b> status if set value is equal to a value obtained after.
134     */
135     public void _setText() {
136         String newText = num ? "823" : "setText" ;
137         if (tEnv.getTestCase().getObjectName().equals("OTimeControl")) {
138             newText = "8:15";
139         }
140         log.println("Setting text to : '" + newText + "'") ;
141         oObj.setText(newText);
142         log.println("Getting text : '" + oObj.getText() + "'") ;
143         tRes.tested("setText()",oObj.getText().equals(newText));
144     }
145 
146     /**
147     * At first we're setting some string variable 'newText' depending of a kind
148     * of object we are working with. Then we set text to the object and call
149     * the method. <p>
150     * Has <b> OK </b> status if set value is equal to a value obtained using
151     * getText() method.
152     */
153     public void _getText() {
154         String newText = num ? "823" : "setText" ;
155         if (tEnv.getTestCase().getObjectName().equals("OTimeControl")) {
156             newText = "8:15";
157         }
158         oObj.setText(newText);
159         tRes.tested("getText()",oObj.getText().equals(newText));
160     }
161 
162     /**
163     * At first we're setting string variables 'text' and 'itext' depending
164     * of a kind of object we are working with. Next, value from 'text' variable
165     * is set to an object using setText(), then the method insertText() is called.
166     * <p>
167     * Has <b> OK </b> status if text is inserted to the object.
168     */
169     public void _insertText() {
170         String text = num ? "753" :  "iText" ;
171         String itext = num ? "6" :  "insert" ;
172         log.println("Setting text to : '" + text + "'") ;
173         oObj.setText(text);
174         log.println("Iserting text to (0,1) : '" + itext + "'") ;
175         oObj.insertText(new Selection(0,1), itext);
176         log.println("getText() returns: " + oObj.getText());
177         tRes.tested("insertText()", oObj.getText().equals
178             (num ? "653" : "insertText"));
179     }
180 
181     /**
182     * After text is set to the object, test calls the method.<p>
183     * Has <b> OK </b> status if selected text is equal to first three symbols
184     * of text added before.
185     */
186     public void _getSelectedText() {
187         String text = num ? "753" :  "txt" ;
188         oObj.setText(text);
189         oObj.setSelection(new Selection(0,3));
190         boolean result = oObj.getSelectedText().equals(text);
191 
192         if (! result) {
193             System.out.println("Getting '"+oObj.getSelectedText()+"'");
194             System.out.println("Expected '"+text+"'");
195         }
196 
197         tRes.tested("getSelectedText()",result);
198     }
199 
200     /**
201     * After setting new text to an object, and defining selection variable,
202     * test calls the method. <p>
203     * Has <b> OK </b> status if selection set before is equal to a selection we
204     * got using getSelection().
205     */
206     public void _setSelection() {
207         oObj.setText("setSelection");
208         Selection sel = new Selection(0,3);
209         oObj.setSelection(sel);
210         tRes.tested("setSelection()", ValueComparer.equalValue
211             (oObj.getSelection(), sel));
212     }
213 
214     /**
215     * After setting new text to an object, and defining selection variable,
216     * test calls the method. <p>
217     * Has <b> OK </b> status if selection set before is equal to a selection we
218     * got using getSelection().
219     */
220     public void _getSelection() {
221         oObj.setText("getSelection");
222         Selection sel = new Selection(2,3);
223         oObj.setSelection(sel);
224         tRes.tested("getSelection()", ValueComparer.equalValue
225             (oObj.getSelection(), sel));
226     }
227 
228     /**
229     * Test calls the method. <p>
230     * Has <b> OK </b> status if method has changed a property 'Editable'.
231     */
232     public void _setEditable(){
233         oObj.setEditable(true);
234         tRes.tested("setEditable()", oObj.isEditable());
235     }
236 
237     /**
238     * First we set 'Editable' variable to false. Then test calls the method.<p>
239     * Has <b> OK </b> status if method returns value we set before.
240     */
241     public void _isEditable(){
242         oObj.setEditable(false);
243         tRes.tested("isEditable()", ! oObj.isEditable());
244     }
245 
246     /**
247     * Test calls the method. Then new text value is set to the object. <p>
248     * Has <b> OK </b> status if text, returned by getText() is a string of
249     * length we set before.
250     */
251     public void _setMaxTextLen() {
252         oObj.setMaxTextLen((short)10);
253         //oObj.setText("0123456789ABCDE");
254         //String get = oObj.getText();
255         //tRes.tested("setMaxTextLen()",get.length() == 10);
256         tRes.tested("setMaxTextLen()",oObj.getMaxTextLen()==10);
257     }
258 
259     /**
260     * At first we set MaxTextLen, then test calls the method. <p>
261     * Has <b> OK </b> status if method returns a value we set before.
262     */
263     public void _getMaxTextLen() {
264         oObj.setMaxTextLen((short)15);
265         log.println("getMaxTextLen() returns: "+oObj.getMaxTextLen());
266         tRes.tested("getMaxTextLen()",oObj.getMaxTextLen()==15);
267     }
268 
269 }
270 
271