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