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