001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.qa.form.visualDevelopment;
043:
044: import java.awt.Component;
045: import java.awt.event.KeyEvent;
046: import javax.swing.JPanel;
047: import org.netbeans.jellytools.modules.form.ComponentInspectorOperator;
048: import org.netbeans.jellytools.modules.form.ComponentPaletteOperator;
049: import org.netbeans.jellytools.modules.form.FormDesignerOperator;
050: import org.netbeans.jellytools.properties.Property;
051: import org.netbeans.jemmy.operators.JTextFieldOperator;
052: import org.netbeans.junit.NbTestSuite;
053: import java.util.*;
054: import javax.swing.JButton;
055: import javax.swing.JCheckBox;
056: import javax.swing.JLabel;
057: import javax.swing.JRadioButton;
058: import javax.swing.JTextArea;
059: import javax.swing.JToggleButton;
060: import org.netbeans.jellytools.actions.*;
061: import org.netbeans.jellytools.*;
062: import org.netbeans.jellytools.nodes.Node;
063: import org.netbeans.jemmy.operators.JListOperator;
064: import org.netbeans.jemmy.operators.Operator;
065: import org.netbeans.qa.form.BorderCustomEditorOperator;
066: import org.netbeans.qa.form.ExtJellyTestCase;
067:
068: /**
069: * Tests from NetBeans 5.5.1 Form Test Specification
070: * from Visual Development Test Specification
071: * @see <a href="http://qa.netbeans.org/modules/form/promo-f/testspecs/visualDevelopment.html">Test specification</a>
072: *
073: * @author Jiri Vagner
074: */
075: public class FormGenerateCodeTest extends ExtJellyTestCase {
076:
077: /** Constructor required by JUnit */
078: public FormGenerateCodeTest(String testName) {
079: super (testName);
080: }
081:
082: /* Method allowing to execute test directly from IDE. */
083: public static void main(java.lang.String[] args) {
084: junit.textui.TestRunner.run(suite());
085: }
086:
087: /** Creates suite from particular test cases. */
088: public static NbTestSuite suite() {
089: NbTestSuite suite = new NbTestSuite();
090:
091: suite.addTest(new FormGenerateCodeTest(
092: "testAWTAndSwingComponentsTogether"));
093: suite.addTest(new FormGenerateCodeTest(
094: "testAddComponentsIntoContainersSwing"));
095: suite.addTest(new FormGenerateCodeTest(
096: "testAddComponentsIntoContainersAwt"));
097: suite.addTest(new FormGenerateCodeTest("testInPlaceEditing"));
098: suite.addTest(new FormGenerateCodeTest(
099: "testSimpleComponentInsertingIntoForm"));
100: suite.addTest(new FormGenerateCodeTest("testLayouts"));
101: suite.addTest(new FormGenerateCodeTest("testBorderSettings"));
102:
103: return suite;
104: }
105:
106: public void testSimpleComponentInsertingIntoForm() {
107: p("testSimpleComponentInsertingIntoForm - start"); // NOI18N
108: String frameName = createJFrameFile();
109: FormDesignerOperator designer = new FormDesignerOperator(
110: frameName);
111: ComponentPaletteOperator palette = new ComponentPaletteOperator();
112: ComponentInspectorOperator inspector = new ComponentInspectorOperator();
113:
114: palette.expandSwingContainers();
115: palette.selectComponent("Panel"); // NOI18N
116: designer.clickOnComponent(designer.fakePane().getSource());
117:
118: inspector.selectComponent("[JFrame]|jPanel1 [JPanel]"); // NOI18N
119: new Property(inspector.properties(), "background")
120: .setValue("[0,255,0]"); // NOI18N
121:
122: Component firstPanel = designer.findComponent(JPanel.class);
123: palette.expandSwingControls();
124: palette.selectComponent("Label"); // NOI18N
125: designer.clickOnComponent(firstPanel);
126:
127: findInCode(
128: "jPanel1.setBackground(new java.awt.Color(0, 255, 0));",
129: designer); // NOI18N
130: findInCode("jLabel1 = new javax.swing.JLabel();", designer); // NOI18N
131:
132: removeFile(frameName);
133: }
134:
135: public void testLayouts() {
136: p("testLayouts - start"); // NOI18N
137: String nodeName = "JFrame"; // NOI18N
138: String name = createJFrameFile();
139:
140: FormDesignerOperator designer = new FormDesignerOperator(name);
141: ComponentPaletteOperator palette = new ComponentPaletteOperator();
142: ComponentInspectorOperator inspector = new ComponentInspectorOperator();
143: Node frameNode = new Node(inspector.treeComponents(), nodeName);
144:
145: HashMap<String, String> task = new HashMap<String, String>();
146: task
147: .put("Set Layout|AbsoluteLayout",
148: "setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());"); // NOI18N
149: task
150: .put(
151: "Set Layout|BoxLayout",
152: "setLayout(new javax.swing.BoxLayout(getContentPane(), javax.swing.BoxLayout.LINE_AXIS));"); // NOI18N
153: task.put("Set Layout|CardLayout",
154: "setLayout(new java.awt.CardLayout());"); // NOI18N
155: task.put("Set Layout|FlowLayout",
156: "setLayout(new java.awt.FlowLayout());"); // NOI18N
157: task.put("Set Layout|GridBagLayout",
158: "setLayout(new java.awt.GridBagLayout());"); // NOI18N
159: task.put("Set Layout|GridLayout",
160: "setLayout(new java.awt.GridLayout());"); // NOI18N
161: task.put("Set Layout|Null Layout", "setLayout(null);"); // NOI18N
162: task.put("Set Layout|Free Design",
163: ".GroupLayout(getContentPane());"); // NOI18N
164: task.put("Set Layout|BorderLayout", "setLayout("); //setLayout("); // NOI18N
165:
166: for (Map.Entry<String, String> entry : task.entrySet()) {
167: String layoutCmd = entry.getKey();
168: runPopupOverNode(layoutCmd, frameNode);
169:
170: if (layoutCmd != "Set Layout|BorderLayout") // NOI18N
171: findInCode(entry.getValue(), designer);
172: else
173: missInCode(entry.getValue(), designer);
174: }
175: removeFile(name);
176: }
177:
178: public void testAWTAndSwingComponentsTogether() {
179: p("testAWTAndSwingComponentsTogether - start"); // NOI18N
180: String nodeName = "JFrame"; // NOI18N
181: String name = createJFrameFile();
182:
183: ArrayList<String> cmds = new ArrayList<String>();
184: cmds.add("Add From Palette|Swing Controls|Label"); // NOI18N
185: cmds.add("Add From Palette|AWT|Label"); // NOI18N
186:
187: FormDesignerOperator designer = new FormDesignerOperator(name);
188: ComponentInspectorOperator inspector = new ComponentInspectorOperator();
189: Node node = new Node(inspector.treeComponents(), nodeName);
190:
191: runPopupOverNode(cmds, node);
192:
193: findInCode("jLabel1 = new javax.swing.JLabel();", designer); // NOI18N
194: findInCode("label1 = new java.awt.Label()", designer); // NOI18N
195:
196: removeFile(name);
197: }
198:
199: public void testAddComponentsIntoContainersSwing() {
200: p("testAddComponentsIntoContainersSwing - start"); // NOI18N
201: String frameName = createJFrameFile();
202: FormDesignerOperator designer = new FormDesignerOperator(
203: frameName);
204: ComponentInspectorOperator inspector = new ComponentInspectorOperator();
205: Node frameNode = new Node(inspector.treeComponents(), "JFrame"); // NOI18N
206:
207: ArrayList<String> containers = new ArrayList<String>();
208: containers.add("Add From Palette|Swing Containers|Panel"); // NOI18N
209: containers.add("Add From Palette|Swing Containers|Tabbed Pane"); // NOI18N
210: containers.add("Add From Palette|Swing Containers|Scroll Pane"); // NOI18N
211: containers.add("Add From Palette|Swing Containers|Split Pane"); // NOI18N
212: containers.add("Add From Palette|Swing Containers|Tool Bar"); // NOI18N
213: containers
214: .add("Add From Palette|Swing Containers|Internal Frame"); // NOI18N
215: containers
216: .add("Add From Palette|Swing Containers|Desktop Pane"); // NOI18N
217: runPopupOverNode(containers, frameNode);
218:
219: ArrayList<String> lines = new ArrayList<String>();
220: lines.add("jPanel1 = new javax.swing.JPanel();"); // NOI18N
221: lines.add("jTabbedPane1 = new javax.swing.JTabbedPane();"); // NOI18N
222: lines.add("jScrollPane1 = new javax.swing.JScrollPane();"); // NOI18N
223: lines.add("jSplitPane1 = new javax.swing.JSplitPane();"); // NOI18N
224: lines.add("jToolBar1 = new javax.swing.JToolBar();"); // NOI18N
225: lines
226: .add("jInternalFrame1 = new javax.swing.JInternalFrame();"); // NOI18N
227: lines.add("jDesktopPane1 = new javax.swing.JDesktopPane();"); // NOI18N
228: findInCode(lines, designer);
229:
230: Node node = new Node(inspector.treeComponents(),
231: "[JFrame]|jPanel1 [JPanel]"); // NOI18N
232: runPopupOverNode("Add From Palette|Swing Controls|Text Area",
233: node); // NOI18N
234: findInCode("jScrollPane2.setViewportView(jTextArea1);",
235: designer); // NOI18N
236:
237: node = new Node(inspector.treeComponents(),
238: "[JFrame]|jTabbedPane1 [JTabbedPane]"); // NOI18N
239: runPopupOverNode("Add From Palette|Swing Containers|Panel",
240: node); // NOI18N
241: findInCode("jTabbedPane1.addTab(\"tab1\", jPanel2);", designer); // NOI18N
242:
243: node = new Node(inspector.treeComponents(),
244: "[JFrame]|jScrollPane1 [JScrollPane]"); // NOI18N
245: runPopupOverNode("Add From Palette|Swing Controls|Table", node); // NOI18N
246: findInCode("jScrollPane1.setViewportView(jTable1);", designer); // NOI18N
247:
248: node = new Node(inspector.treeComponents(),
249: "[JFrame]|jSplitPane1 [JSplitPane]"); // NOI18N
250: runPopupOverNode("Add From Palette|Swing Controls|Button", node); // NOI18N
251: runPopupOverNode("Add From Palette|Swing Containers|Panel",
252: node); // NOI18N
253: findInCode("jSplitPane1.setRightComponent(jPanel3);", designer); // NOI18N
254: findInCode("jSplitPane1.setLeftComponent(jButton1);", designer); // NOI18N
255:
256: node = new Node(inspector.treeComponents(),
257: "[JFrame]|jToolBar1 [JToolBar]"); // NOI18N
258: runPopupOverNode(
259: "Add From Palette|Swing Controls|Toggle Button", node); // NOI18N
260: findInCode("jToolBar1.add(jToggleButton1);", designer); // NOI18N
261:
262: node = new Node(inspector.treeComponents(),
263: "[JFrame]|jInternalFrame1 [JInternalFrame]"); // NOI18N
264: runPopupOverNode("Add From Palette|Swing Controls|Tree", node); // NOI18N
265: findInCode("jScrollPane3.setViewportView(jTree1);", designer); // NOI18N
266:
267: node = new Node(inspector.treeComponents(),
268: "[JFrame]|jDesktopPane1 [JDesktopPane]"); // NOI18N
269: runPopupOverNode(
270: "Add From Palette|Swing Containers|Internal Frame",
271: node); // NOI18N
272: findInCode(
273: "jDesktopPane1.add(jInternalFrame2, javax.swing.JLayeredPane.DEFAULT_LAYER);",
274: designer); // NOI18N
275:
276: removeFile(frameName);
277: }
278:
279: public void testAddComponentsIntoContainersAwt() {
280: p("testAddComponentsIntoContainersAwt - start"); // NOI18N
281: String frameName = createFrameFile();
282: FormDesignerOperator designer = new FormDesignerOperator(
283: frameName);
284: ComponentInspectorOperator inspector = new ComponentInspectorOperator();
285: Node frameNode = new Node(inspector.treeComponents(), "Frame"); // NOI18N
286:
287: ArrayList<String> containers = new ArrayList<String>();
288: containers.add("Add From Palette|AWT|Panel"); // NOI18N
289: containers.add("Add From Palette|AWT|Scroll Pane"); // NOI18N
290: runPopupOverNode(containers, frameNode);
291:
292: findInCode("scrollPane1 = new java.awt.ScrollPane();", designer); // NOI18N
293: findInCode("panel1 = new java.awt.Panel();", designer); // NOI18N
294:
295: Node node = new Node(inspector.treeComponents(),
296: "[Frame]|scrollPane1 [ScrollPane]"); // NOI18N
297: runPopupOverNode("Add From Palette|AWT|Text Area", node); // NOI18N
298: findInCode("scrollPane1.add(textArea1);", designer); // NOI18N
299:
300: node = new Node(inspector.treeComponents(),
301: "[Frame]|panel1 [Panel]"); // NOI18N
302: runPopupOverNode("Add From Palette|AWT|Canvas", node); // NOI18N
303: findInCode("panel1.add(canvas1);", designer); // NOI18N
304:
305: removeFile(frameName);
306: }
307:
308: public void testInPlaceEditing() {
309: p("testInPlaceEditing - start"); // NOI18N
310: String testText = "xyz"; // NOI18N
311: String dialogName = createJDialogFile();
312:
313: HashMap<String, Class> components = new HashMap<String, Class>();
314: // TODO: strange, findComponent is not able to find JToggleButton.class
315: //components.put("Toggle Button", JToggleButton.class);
316: components.put("Label", JLabel.class); // NOI18N
317: components.put("Button", JButton.class); // NOI18N
318: components.put("Check Box", JCheckBox.class); // NOI18N
319: components.put("Radio Button", JRadioButton.class); // NOI18N
320:
321: FormDesignerOperator designer = new FormDesignerOperator(
322: dialogName);
323: ComponentPaletteOperator palette = new ComponentPaletteOperator();
324: ComponentInspectorOperator inspector = new ComponentInspectorOperator();
325:
326: palette.expandSwingControls();
327:
328: for (Map.Entry<String, Class> entry : components.entrySet()) {
329: palette.selectComponent(entry.getKey());
330: designer.clickOnComponent(designer.fakePane().getSource());
331:
332: Class componentClass = entry.getValue();
333: java.awt.Component component = designer
334: .findComponent(componentClass);
335:
336: designer.clickOnComponent(component);
337: waitNoEvent(300);
338: designer.handleLayer().pushKey(KeyEvent.VK_SPACE);
339: waitNoEvent(300);
340: new JTextFieldOperator(designer).typeText(testText);
341: waitNoEvent(300);
342: designer.handleLayer().pushKey(KeyEvent.VK_ENTER);
343: waitNoEvent(300);
344: designer.handleLayer().pushKey(KeyEvent.VK_ESCAPE);
345: }
346:
347: String baseName = "[JDialog]"; // NOI18N
348: Node dialogNode = new Node(inspector.treeComponents(), baseName);
349: String[] names = dialogNode.getChildren();
350:
351: for (String name : names) {
352: inspector.selectComponent(baseName + "|" + name);
353: Property prop = new Property(inspector.properties(), "text"); // NOI18N
354: assertEquals("Text property of component " + name
355: + " was not set correctly.", prop.getValue(),
356: testText); // NOI18N
357: }
358: removeFile(dialogName);
359: }
360:
361: public void testBorderSettings() {
362: p("testBorderSettings - start"); // NOI18N
363: String dialogName = createJFrameFile();
364:
365: HashMap<String, Class> components = new HashMap<String, Class>();
366: components.put("Button", JButton.class); // NOI18N
367: components.put("Label", JLabel.class); // NOI18N
368: components.put("Radio Button", JRadioButton.class); // NOI18N
369: components.put("Toggle Button", JToggleButton.class); // NOI18N
370: components.put("Text Area", JTextArea.class); // NOI18N
371:
372: FormDesignerOperator designer = new FormDesignerOperator(
373: dialogName);
374: ComponentPaletteOperator palette = new ComponentPaletteOperator();
375: ComponentInspectorOperator inspector = new ComponentInspectorOperator();
376:
377: palette.expandSwingControls();
378:
379: for (Map.Entry<String, Class> entry : components.entrySet()) {
380: String componentName = entry.getKey();
381: palette.selectComponent(componentName);
382: designer.clickOnComponent(designer.fakePane().getSource());
383: }
384:
385: String baseName = "[JFrame]"; // NOI18N
386: Node dialogNode = new Node(inspector.treeComponents(), baseName);
387: String[] names = dialogNode.getChildren();
388:
389: int counter = 0;
390: for (String name : names) {
391: String path = baseName + "|" + name;
392: inspector.selectComponent(path);
393:
394: Property prop = new Property(inspector.properties(),
395: "border"); // NOI18N
396: prop.openEditor();
397:
398: BorderCustomEditorOperator editor = new BorderCustomEditorOperator(
399: name);
400: JListOperator lstOp = editor.lstAvailableBorders();
401: lstOp.clickOnItem(++counter, 1);
402: editor.ok();
403: }
404:
405: ArrayList<String> lines = new ArrayList<String>();
406: lines
407: .add("jToggleButton1.setBorder(javax.swing.BorderFactory.");
408: lines.add("jLabel1.setBorder(javax.swing.BorderFactory.");
409: lines.add("jButton1.setBorder(javax.swing.BorderFactory.");
410: findInCode(lines, designer);
411:
412: removeFile(dialogName);
413: }
414: }
|