KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gui > propertyeditors > PropertyEditorsTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package gui.propertyeditors;
21
22 import java.io.PrintStream JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24
25 import org.netbeans.jellytools.JellyTestCase;
26 import org.netbeans.jellytools.NbDialogOperator;
27
28 import org.netbeans.jellytools.properties.Property;
29 import org.netbeans.jellytools.properties.PropertySheetOperator;
30
31 import org.netbeans.jemmy.JemmyProperties;
32 import org.netbeans.jemmy.TestOut;
33 import org.netbeans.jemmy.operators.JTableOperator;
34 import org.netbeans.jemmy.operators.Operator;
35
36
37 /**
38  * JellyTestCase test case with implemented Property Editors Test support stuff
39  *
40  * @author mmirilovic@netbeans.org
41  */

42 public abstract class PropertyEditorsTest extends JellyTestCase {
43     
44     protected static PrintStream JavaDoc err;
45     protected static PrintStream JavaDoc log;
46     
47     public String JavaDoc propertyInitialValue;
48     public String JavaDoc propertyValue;
49     
50     protected static JTableOperator tableOperator;
51
52     protected static NbDialogOperator propertyCustomizer;
53     
54     protected static NbDialogOperator propertiesWindow = null;
55     
56     private static final String JavaDoc CAPTION = "\n===========================";
57     
58     /** Creates a new instance of PropertyEditorsTest */
59     public PropertyEditorsTest(String JavaDoc testName) {
60         super(testName);
61     }
62     
63     
64     public void setUp() {
65         //err = System.out;
66
err = getLog();
67         log = getRef();
68         
69         try {
70             JemmyProperties.getProperties().setOutput(new TestOut(null, new PrintWriter JavaDoc(err, true), new PrintWriter JavaDoc(err, true), null));
71             initializeWorkplace();
72         }catch(Exception JavaDoc exc) {
73             failTest(exc, "SetUp failed. It seems like initializeWorkplace cause exception:"+exc.getMessage());
74         }
75     }
76     
77     /** Open Property Customizer for <b>propertyName</b>, set value by customizer and press Ok button, verify value with <b>expectance</b>.
78      * @param propertyName name of property to be customized
79      * @param expectance true- new value must be the same as expected value, false-value needn't be the same as expected
80      */

81     public void setByCustomizerOk(String JavaDoc propertyName, boolean expectance){
82         try {
83             err.println(CAPTION + " Trying to set value by customizer-ok {name="+propertyName+" / value="+propertyValue+"} .");
84             propertyInitialValue = getValue(propertyName);
85             
86             openAndGetPropertyCustomizer(propertyName);
87             setCustomizerValue();
88             
89             if(propertyCustomizer.isShowing())
90                 propertyCustomizer.ok();
91             
92             err.println(CAPTION + " Trying to set value by customizer-ok {name="+propertyName+" / value="+propertyValue+"} - finished.");
93             verifyPropertyValue(expectance);
94             
95         }catch(Exception JavaDoc exc) {
96             failTest(exc, "EXCEPTION: setByCustomizer("+propertyName+", "+expectance+") failed and cause exception:"+exc.getMessage());
97         }
98     }
99     
100     /** Open Property Customizer for <b>propertyName</b>, set value by customizer and press Cancel button, verify value with <b>expectance</b>.
101      * @param propertyName name of property to be customized
102      * @param expectance true- new value must be the same as expected value, false-value needn't be the same as expected
103      */

104     public void setByCustomizerCancel(String JavaDoc propertyName, boolean expectance) {
105         try {
106             err.println(CAPTION + " Trying to set value by customizer-cancel {name="+propertyName+" / value="+propertyValue+"} .");
107             propertyInitialValue = getValue(propertyName);
108             openAndGetPropertyCustomizer(propertyName);
109             setCustomizerValue();
110             
111             if(propertyCustomizer.isShowing())
112                 propertyCustomizer.cancel();
113             
114             err.println(CAPTION + " Trying to set value by customizer-cancel {name="+propertyName+" / value="+propertyValue+"} - finished.");
115             verifyPropertyValue(expectance);
116             
117         }catch(Exception JavaDoc exc) {
118             failTest(exc, "EXCEPTION: setByCustomizerCancel("+propertyName+", "+expectance+") failed and cause exception:"+exc.getMessage());
119         }
120     }
121     
122     /** Set value <b>propertyValue</b> of property <b>propertyName</b> by in-place, verify value with <b>expectance</b>.
123      * @param propertyName name of property to be changed
124      * @param propertyValue new value of property
125      * @param expectance true- new value must be the same as expected value, false-value needn't be the same as expected
126      */

127     public void setByInPlace(String JavaDoc propertyName, String JavaDoc propertyValue, boolean expectance) {
128         try {
129             err.println(CAPTION + " Trying to set value by in-place {name="+propertyName+" / value="+propertyValue+"} .");
130             propertyInitialValue = getValue(propertyName);
131             
132 // ((TextFieldProperty) findProperty(propertyName, "TextFieldProperty")).setValue(propertyValue);
133
new PropertySheetOperator(propertiesWindow).tblSheet().changeCellObject(findProperty(propertyName, propertiesWindow).getRow(),1, propertyValue);
134             
135             err.println(CAPTION + " Trying to set value by in-place {name="+propertyName+" / value="+propertyValue+"} - finished.");
136             verifyPropertyValue(expectance);
137             
138         }catch(Exception JavaDoc exc) {
139             failTest(exc, "EXCEPTION: setByInPlace("+propertyName+", "+propertyValue+", "+expectance+") failed and cause exception:"+exc.getMessage());
140         }
141     }
142     
143     /** Set value <b>propertyValue</b> of property <b>propertyName</b> by combobox, verify value with <b>expectance</b>.
144      * @param propertyName name of property to be changed
145      * @param propertyValue new value of property
146      * @param expectance true- new value must be the same as expected value, false-value needn't be the same as expected
147      */

148     public void setByCombo(String JavaDoc propertyName, String JavaDoc propertyValue, boolean expectance) {
149         try {
150             err.println(CAPTION + " Trying to set value by combo box {name="+propertyName+" / value="+propertyValue+"} .");
151             propertyInitialValue = getValue(propertyName);
152             
153             findProperty(propertyName, propertiesWindow).setValue(propertyValue);
154             
155             err.println(CAPTION + " Trying to set value by combo box {name="+propertyName+" / value="+propertyValue+"} - finished.");
156             verifyPropertyValue(expectance);
157             
158         }catch(Exception JavaDoc exc) {
159             failTest(exc, "EXCEPTION: setByCombo("+propertyName+", "+propertyValue+", "+expectance+") failed and cause exception:"+exc.getMessage());
160         }
161     }
162     
163     /** Set indexed value <b>propertyValueIndex</b> of property <b>propertyName</b> by combobox, verify value with <b>expectance</b>.
164      * @param propertyName name of property to be changed
165      * @param propertyValueIndex index of new value in combobox
166      * @param expectance true- new value must be the same as expected value, false-value needn't be the same as expected
167      */

168     public void setByCombo(String JavaDoc propertyName, int propertyValueIndex, boolean expectance) {
169         try {
170             err.println(CAPTION + " Trying to set value by combo box {name="+propertyName+" / value="+propertyValueIndex+"} .");
171             propertyInitialValue = getValue(propertyName);
172             
173             findProperty(propertyName, propertiesWindow).setValue(propertyValueIndex);
174             
175             err.println(CAPTION + " Trying to set value by combo box {name="+propertyName+" / value="+propertyValueIndex+"} - finished.");
176             verifyPropertyValue(expectance);
177             
178         }catch(Exception JavaDoc exc) {
179             failTest(exc, "EXCEPTION: setByCombo("+propertyName+", "+propertyValueIndex+", "+expectance+") failed and cause exception:"+exc.getMessage());
180         }
181     }
182     
183     /** Verify customizer layout for property <b>propertyName</b>.
184      * @param propertyName name of property to be changed
185      */

186     public void verifyCustomizer(String JavaDoc propertyName){
187         try {
188             err.println(CAPTION + " Trying to verify customizer {name="+propertyName+"} .");
189             openAndGetPropertyCustomizer(propertyName);
190             verifyCustomizerLayout();
191             
192             if(propertyCustomizer.isShowing())
193                 propertyCustomizer.cancel();
194             
195             err.println(CAPTION + " Trying to verify customizer {name="+propertyName+"} - finished.");
196             
197         }catch(Exception JavaDoc exc) {
198             failTest(exc, "EXCEPTION: Verification of Property Customizer Layout for property("+propertyName+") failed and cause exception:"+exc.getMessage());
199         }
200     }
201     
202     /** Open property customizer for property <b>propertyName</b>.
203      * @param propertyName name of property to be changed
204      * @return Property Customizer
205      */

206     public static NbDialogOperator openAndGetPropertyCustomizer(String JavaDoc propertyName) {
207         // hack for troubles with request focus on already focused property
208
new PropertySheetOperator(propertiesWindow).tblSheet().selectCell(0,0);
209         
210         findProperty(propertyName, propertiesWindow).openEditor();
211         propertyCustomizer = findPropertyCustomizer(propertyName);
212         return propertyCustomizer;
213     }
214     
215     /** Return Property Customizer.
216      * @return Property Customizer.
217      */

218     public NbDialogOperator getPropertyCustomizer() {
219         return propertyCustomizer;
220     }
221     
222     /** Return Informational dialog
223      * @return Informational dialog
224      */

225     public NbDialogOperator getInformationDialog() {
226         String JavaDoc title = org.netbeans.jellytools.Bundle.getString("org.openide.Bundle", "NTF_InformationTitle");
227         
228         err.println(CAPTION + " Waiting dialog {"+title+"} .");
229         NbDialogOperator dialog = new NbDialogOperator(title);
230         err.println(CAPTION + " Waiting dialog {"+title+"} - finished.");
231         return dialog;
232     }
233     
234     /** Get value of property <b>propertyName</b>
235      * @param propertyName name of property asked for value
236      * @return value of property
237      */

238     public String JavaDoc getValue(String JavaDoc propertyName) {
239         String JavaDoc returnValue = findProperty(propertyName, propertiesWindow).getValue();
240         err.println("GET VALUE = [" + returnValue + "].");
241         return returnValue;
242     }
243     
244     /** Find Property Cusotmizer by name of property <b>propertyName</b>
245      * @param propertyName name of property
246      * @return founded Property Customizer
247      */

248     private static NbDialogOperator findPropertyCustomizer(String JavaDoc propertyName){
249         return new NbDialogOperator(propertyName);
250     }
251     
252     /** Verify exceptation value.
253      * @param propertyName name of property
254      * @param expectation true - expected value must be the same as new value, false - expected value should not be the same
255      * @param propertyValueExpectation expected value
256      * @param propertyValue new value
257      * @param waitDialog true - after changing value Informational dialog about impissibility to set invalid value arise
258      */

259     public void verifyExpectationValue(String JavaDoc propertyName, boolean expectation, String JavaDoc propertyValueExpectation, String JavaDoc propertyValue, boolean waitDialog){
260         
261         // Dialog isn't used for informing user about Invalid new value: Class,
262
if(waitDialog) {
263             getInformationDialog().ok();
264             err.println(CAPTION + " Dialog closed by [Ok].");
265             
266             if(propertyCustomizer!=null && propertyCustomizer.isShowing()){
267                 err.println(CAPTION + " Property Customizer is still showing.");
268                 propertyCustomizer.cancel();
269                 err.println(CAPTION + " Property Customizer closed by [Cancel].");
270             }
271             
272         }
273         
274         String JavaDoc newValue = getValue(propertyName);
275         String JavaDoc log = "Actual value is {"+newValue+"} and initial is{"+propertyInitialValue+"} - set value is {"+propertyValue+"} / expectation value is {"+propertyValueExpectation+"}";
276         
277         err.println(CAPTION + " Trying to verify value ["+log+"].");
278         
279         if(expectation){
280             if(newValue.equals(propertyValueExpectation) ) {
281                 log(log + " --> PASS");
282             }else {
283                 fail(log + " --> FAIL");
284             }
285         }else {
286             if(newValue.equals(propertyInitialValue)){
287                 log(log + " --> PASS");
288             }else{
289                 fail(log + " --> FAIL");
290             }
291             
292         }
293     }
294     
295     
296     /** Reinitialize Workplace. */
297     public static NbDialogOperator reInitializeWorkplace() {
298         propertiesWindow = null;
299         return openPropertySheet();
300     }
301     
302     /** Initialize Workplace. */
303     public static NbDialogOperator initializeWorkplace() {
304         return openPropertySheet();
305     }
306     
307     /** Open property sheet (bean customizer). */
308     private static NbDialogOperator openPropertySheet() {
309         String JavaDoc waitDialogTimeout = "DialogWaiter.WaitDialogTimeout";
310         long findTimeout = JemmyProperties.getCurrentTimeout(waitDialogTimeout);
311         JemmyProperties.setCurrentTimeout(waitDialogTimeout, 3000);
312         
313         try{
314             propertiesWindow = new NbDialogOperator(org.netbeans.jellytools.Bundle.getString("org.netbeans.core.Bundle", "CTL_FMT_LocalProperties", new Object JavaDoc[]{new Integer JavaDoc(1),"TestNode"}));
315         }catch(org.netbeans.jemmy.TimeoutExpiredException exception){
316             new PropertiesTest();
317             propertiesWindow = new NbDialogOperator(org.netbeans.jellytools.Bundle.getString("org.netbeans.core.Bundle", "CTL_FMT_LocalProperties", new Object JavaDoc[]{new Integer JavaDoc(1),"TestNode"}));
318         }
319         
320         JemmyProperties.setCurrentTimeout(waitDialogTimeout, findTimeout);
321         
322         return propertiesWindow;
323     }
324     
325     
326     /** Find Property in Property Sheet and return them.
327      * This is first hack for new Jelly2, because it isn't possible to set String Comparator only for one operator.
328      * @param propertyName name of property
329      * @param type TextFieldProperty - textfield property, ComboBoxProperty - combobox property
330      * @return property by <b>propertyName</b> and <b>type</b>.
331      */

332     protected static Property findProperty(String JavaDoc propertyName, NbDialogOperator propertiesWindow) {
333         PropertySheetOperator propertySheet = new PropertySheetOperator(propertiesWindow);
334         Property property = new Property(propertySheet, propertyName);
335         
336         // property.openEditor(); - doesn't work - custom editor is opened without Users Event
337
// hack for invoking Custom Editor by pushing shortcut CTRL+SPACE
338
tableOperator = propertySheet.tblSheet();
339         // Need to request focus before selection because invokeCustomEditor action works
340
// only when table is focused
341
tableOperator.makeComponentVisible();
342         tableOperator.requestFocus();
343         tableOperator.waitHasFocus();
344         // need to select property first
345
((javax.swing.JTable JavaDoc)tableOperator.getSource()).changeSelection(property.getRow(), 0, false, false);
346 // return new Property(new PropertySheetOperator(propertiesWindow), propertyName);
347
return property;
348     }
349     
350     public void tearDown() {
351         closeAllModal();
352     }
353     
354     /** Print full stack trace to log files, get message and log to test results if test fails.
355      * @param exc Exception logged to description
356      * @param message written to test results
357      */

358     protected static void failTest(Exception JavaDoc exc, String JavaDoc message) {
359         err.println("################################");
360         exc.printStackTrace(err);
361         err.println("################################");
362         fail(message);
363     }
364     
365     /** Make IDE screenshot of whole IDE
366      * @param testCase it is needed for locate destination directory of saving screenshot file
367      */

368     public static void makeIDEScreenshot(JellyTestCase testCase) {
369         try{
370             testCase.getWorkDir();
371             org.netbeans.jemmy.util.PNGEncoder.captureScreen(testCase.getWorkDirPath()+System.getProperty("file.separator")+"IDEscreenshot.png");
372         }catch(Exception JavaDoc ioexc){
373             testCase.log("Impossible make IDE screenshot!!! \n" + ioexc.toString());
374         }
375     }
376     
377     public abstract void setCustomizerValue();
378     
379     public abstract void verifyCustomizerLayout();
380     
381     public abstract void verifyPropertyValue(boolean expectation);
382     
383 }
384
Popular Tags