1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.jelly.swing;
17  
18  import java.awt.Color;
19  import java.awt.Component;
20  import java.awt.Container;
21  import java.awt.Dimension;
22  import java.awt.GridBagConstraints;
23  import java.awt.GridBagLayout;
24  import java.awt.Insets;
25  import java.awt.Point;
26  import java.io.UnsupportedEncodingException;
27  
28  import javax.swing.ButtonGroup;
29  import javax.swing.DebugGraphics;
30  import javax.swing.JButton;
31  import javax.swing.JFrame;
32  import javax.swing.JPanel;
33  
34  import junit.framework.TestSuite;
35  
36  import org.apache.commons.jelly.JellyContext;
37  import org.apache.commons.jelly.Script;
38  import org.apache.commons.jelly.XMLOutput;
39  import org.apache.commons.jelly.test.BaseJellyTest;
40  
41  /*** Tests many swing tags for basic functionality.
42   * @author Hans Gilde
43   *
44   */
45  public class TestSwingTags extends BaseJellyTest {
46  
47      /***
48       * @param name
49       */
50      public TestSwingTags(String name) {
51          super(name);
52      }
53  
54      public static TestSuite suite() throws Exception {
55          return new TestSuite(TestSwingTags.class);
56      }
57  
58      /*** Tests some basic Swing tag functions like creating components
59       * , adding them to the parent container and setting bean values.
60       * @throws Exception
61       */
62      public void testBasicComponentFunctions() throws Exception {
63          if (!isAWTAvailable()) return;
64          runSwingScript("test.simple");
65          JellyContext context = getJellyContext();
66          JFrame frame = (JFrame) context.getVariable("frame");
67          assertEquals(new Dimension(100,100), frame.getSize());
68          assertEquals(new Point(200,200), frame.getLocation());
69          JPanel panel = (JPanel) componentByName(frame.getContentPane(), "panel");
70          JButton button = (JButton) componentByName(panel, "button");
71          assertNotNull(button);
72          assertEquals(new Color(0x11,0x22,0x33), button.getBackground());
73          assertEquals(new Color(0x44,0x55,0x66), button.getForeground());
74          assertEquals(DebugGraphics.FLASH_OPTION|DebugGraphics.LOG_OPTION, panel.getDebugGraphicsOptions());
75          assertEquals(DebugGraphics.BUFFERED_OPTION, button.getDebugGraphicsOptions());
76      }
77  
78      /*** Tests the GridbagLayout tags, making sure that the constraints are
79       * set properly including inheritance and basedOn.
80       * @throws Exception
81       */
82      public void testGridBagBasic() throws Exception {
83          if (!isAWTAvailable()) return;
84          runSwingScript("test.gbc");
85          JellyContext context = getJellyContext();
86          JFrame frame = (JFrame) context.getVariable("frame");
87          JButton button = (JButton) componentByName(frame.getContentPane(), "button");
88          JButton button2 = (JButton) componentByName(frame.getContentPane(), "button2");
89          GridBagLayout layout = (GridBagLayout) frame.getContentPane().getLayout();
90          GridBagConstraints constraints = layout.getConstraints(button);
91  
92          // this is failing
93          assertEquals(GridBagConstraints.NORTH,constraints.anchor);
94          assertEquals(GridBagConstraints.VERTICAL, constraints.fill);
95          assertEquals(3, constraints.gridheight);
96          assertEquals(2, constraints.gridwidth);
97          assertEquals(4, constraints.gridx);
98          assertEquals(5, constraints.gridy);
99          assertEquals(7, constraints.ipadx);
100         assertEquals(8, constraints.ipady);
101         assertEquals(0.3, constraints.weightx, 0);
102         assertEquals(new Insets(1,2,3,4), constraints.insets);
103         assertEquals(0.6, constraints.weighty, 0);
104 
105         GridBagConstraints constraints2 = layout.getConstraints(button2);
106         assertEquals(1, constraints2.gridx);
107         assertEquals(2, constraints2.gridy);
108         assertEquals(2, constraints2.ipadx);
109         assertEquals(9, constraints2.ipady);
110         assertEquals(new Insets(3,4,5,6), constraints2.insets);
111     }
112 
113     public void testGridBag14() throws Exception {
114         if (!isAWTAvailable()) return;
115         if (System.getProperty("java.version").startsWith("1.4")) {
116             runSwingScript("test.gbc14");
117             JellyContext context = getJellyContext();
118             JFrame frame = (JFrame) context.getVariable("frame");
119             JButton button = (JButton) componentByName(frame.getContentPane(), "button");
120             GridBagLayout layout = (GridBagLayout) frame.getContentPane().getLayout();
121             GridBagConstraints constraints = layout.getConstraints(button);
122             //note that 21 is the JDK 1.4 value of GridBagConstraint.LINE_START
123             assertEquals(21,constraints.anchor);
124         }
125     }
126 
127     public void testGridBagFail(){
128         if (!isAWTAvailable()) return;
129         try {
130             runSwingScript("test.gbcBad");
131         } catch (Exception e) {
132             //success
133             return;
134         }
135         fail("Should have thrown an exception for a bad GBC anchor");
136     }
137 
138     public void testButtonGroup() throws Exception {
139         if (!isAWTAvailable()) return;
140         runSwingScript("test.buttonGroup");
141         JellyContext context = getJellyContext();
142         ButtonGroup bg = (ButtonGroup) context.getVariable("bg");
143         assertEquals(3, bg.getButtonCount());
144         assertNotNull(bg.getSelection());
145     }
146 
147     public void testInvalidBeanProperty() throws Exception {
148         if (!isAWTAvailable()) return;
149         try {
150             runSwingScript("test.invalidProperty");
151         } catch (Exception e) {
152             //success
153             return;
154         }
155         fail("Should have thrown an exception due to an invalid bean property.");
156     }
157 
158     protected void runSwingScript(String testName) throws Exception {
159         setUpScript("swingTags.jelly");
160         Script script = getJelly().compileScript();
161         getJellyContext().getVariables().clear();
162         getJellyContext().setVariable(testName,Boolean.TRUE);
163         script.run(getJellyContext(),getXMLOutput());
164     }
165 
166     /*** Searches a container for a component with a given name. Searches only
167      * the immediate container, not child containers.
168      * @param container the Container to search in
169      * @param name the name to look for
170      * @return the first component with the given name
171      * @throws Exception if the name isn't found
172      */
173     protected static Component componentByName(Container container, String name) throws Exception{
174         Component[] components = container.getComponents();
175 
176         for (int i=0;i<components.length;i++) {
177             Component component = components[i];
178             if (component.getName().equals(name)) {
179                 return component;
180             }
181         }
182 
183         throw new Exception("Component " + name + " not found in container " + container);
184     }
185 
186     /***
187      * @return true if we are running with AWT present
188      */
189     private boolean isAWTAvailable() {
190         return !Boolean.getBoolean("java.awt.headless");
191     }
192     /* (non-Javadoc)
193      * @see org.apache.commons.jelly.core.BaseJellyTest#getXMLOutput()
194      */
195     protected XMLOutput getXMLOutput() {
196         try {
197             return XMLOutput.createXMLOutput(System.out);
198         } catch (UnsupportedEncodingException e) {
199             return null;
200         }
201     }
202 }