001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Vadim L. Bogdanov
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.Dimension;
023: import java.awt.FlowLayout;
024: import java.awt.Insets;
025: import java.awt.event.ActionEvent;
026: import javax.swing.plaf.ComponentUI;
027: import javax.swing.plaf.basic.BasicToolBarUI;
028:
029: public class JToolBarTest extends SwingTestCase {
030: private static final int INVALID_ORIENTATION = 4;
031:
032: private JToolBar toolBar;
033:
034: @Override
035: protected void setUp() throws Exception {
036: super .setUp();
037: toolBar = new JToolBar();
038: }
039:
040: @Override
041: protected void tearDown() throws Exception {
042: super .tearDown();
043: }
044:
045: public JToolBarTest(final String name) {
046: super (name);
047: }
048:
049: public void testGetAccessibleContext() {
050: assertTrue(toolBar.getAccessibleContext() instanceof JToolBar.AccessibleJToolBar);
051: }
052:
053: public void testAddImpl() {
054: JButton b = new JButton("b");
055: b.setEnabled(true);
056: toolBar.addImpl(b, null, 0);
057: assertTrue(toolBar.isAncestorOf(b));
058: assertTrue(b.isEnabled());
059: }
060:
061: public void testSetLayout() {
062: assertNotNull(toolBar.getLayout());
063: FlowLayout layout = new FlowLayout();
064: toolBar.setLayout(layout);
065: assertSame(layout, toolBar.getLayout());
066: toolBar.setLayout(null);
067: assertNull(toolBar.getLayout());
068: }
069:
070: public void testGetUIClassID() {
071: assertEquals("ToolBarUI", toolBar.getUIClassID());
072: }
073:
074: public void testPaintBorder() {
075: // Note: painting code, cannot test
076: }
077:
078: public void testUpdateUI() {
079: toolBar.updateUI();
080: ComponentUI ui1 = toolBar.getUI();
081: ComponentUI ui2 = UIManager.getUI(toolBar);
082: // at least names of classes must be the same
083: assertEquals(ui2.getClass().getName(), ui1.getClass().getName());
084: }
085:
086: public void testJToolBar() {
087: toolBar = new JToolBar();
088: toolBar.setName(null);
089: assertEquals(SwingConstants.HORIZONTAL, toolBar
090: .getOrientation());
091: assertNull(toolBar.getName());
092: assertNotNull(toolBar.getUI());
093: assertNotNull(toolBar.getLayout());
094: }
095:
096: public void testJToolBarint() {
097: toolBar = new JToolBar(SwingConstants.VERTICAL);
098: assertEquals(SwingConstants.VERTICAL, toolBar.getOrientation());
099: assertNull(toolBar.getName());
100: assertNotNull(toolBar.getUI());
101: assertNotNull(toolBar.getLayout());
102: toolBar = new JToolBar(SwingConstants.HORIZONTAL);
103: assertEquals(SwingConstants.HORIZONTAL, toolBar
104: .getOrientation());
105: testExceptionalCase(new IllegalArgumentCase() {
106: @Override
107: public void exceptionalAction() throws Exception {
108: toolBar = new JToolBar(INVALID_ORIENTATION);
109: }
110: });
111: }
112:
113: public void testJToolBarString() {
114: toolBar = new JToolBar("tb1");
115: assertEquals("tb1", toolBar.getName());
116: assertEquals(SwingConstants.HORIZONTAL, toolBar
117: .getOrientation());
118: assertNotNull(toolBar.getUI());
119: assertNotNull(toolBar.getLayout());
120: }
121:
122: public void testJToolBarStringint() {
123: toolBar = new JToolBar("tb1", SwingConstants.HORIZONTAL);
124: assertEquals("tb1", toolBar.getName());
125: assertNotNull(toolBar.getUI());
126: assertNotNull(toolBar.getLayout());
127: testExceptionalCase(new IllegalArgumentCase() {
128: @Override
129: public void exceptionalAction() throws Exception {
130: toolBar = new JToolBar("tb2", INVALID_ORIENTATION);
131: }
132: });
133: }
134:
135: public void testSetGetUI() {
136: BasicToolBarUI ui = new BasicToolBarUI();
137: toolBar.setUI(ui);
138: assertSame(ui, toolBar.getUI());
139: }
140:
141: public void testGetComponentIndex() {
142: JComponent c1 = new JButton("1");
143: toolBar.add(c1);
144: JComponent c2 = new JLabel("2");
145: toolBar.add(c2);
146: toolBar.addSeparator();
147: assertEquals(0, toolBar.getComponentIndex(c1));
148: assertEquals(1, toolBar.getComponentIndex(c2));
149: assertEquals(2, toolBar.getComponentIndex(toolBar
150: .getComponentAtIndex(2)));
151: assertEquals(-1, toolBar.getComponentIndex(new JLabel()));
152: }
153:
154: public void testGetComponentAtIndex() {
155: JComponent c1 = new JButton("1");
156: toolBar.add(c1);
157: JComponent c2 = new JLabel("2");
158: toolBar.add(c2);
159: toolBar.addSeparator();
160: assertSame(c1, toolBar.getComponentAtIndex(0));
161: assertSame(c2, toolBar.getComponentAtIndex(1));
162: assertTrue(toolBar.getComponentAtIndex(2) instanceof JToolBar.Separator);
163: assertNull(toolBar.getComponentAtIndex(toolBar
164: .getComponentCount()));
165: }
166:
167: public void testSetGetMargin() {
168: final Insets defaultMagin = new Insets(0, 0, 0, 0);
169: assertEquals(defaultMagin, toolBar.getMargin());
170: Insets insets = new Insets(1, 2, 3, 4);
171: PropertyChangeController controller = new PropertyChangeController();
172: toolBar.addPropertyChangeListener("margin", controller);
173: toolBar.setMargin(insets);
174: assertTrue(controller.isChanged());
175: assertEquals(insets, toolBar.getMargin());
176: assertSame(insets, toolBar.getMargin());
177: toolBar.setMargin(null);
178: assertEquals(defaultMagin, toolBar.getMargin());
179: }
180:
181: public void testSetIsBorderPainted() {
182: assertTrue(toolBar.isBorderPainted());
183: PropertyChangeController controller = new PropertyChangeController();
184: toolBar.addPropertyChangeListener("borderPainted", controller);
185: toolBar.setBorderPainted(false);
186: assertTrue(controller.isChanged());
187: assertFalse(toolBar.isBorderPainted());
188: }
189:
190: public void testSetIsFloatable() {
191: assertTrue(toolBar.isFloatable());
192: PropertyChangeController controller = new PropertyChangeController();
193: toolBar.addPropertyChangeListener("floatable", controller);
194: toolBar.setFloatable(false);
195: assertTrue(controller.isChanged());
196: assertFalse(toolBar.isFloatable());
197: }
198:
199: public void testSetGetOrientation() {
200: toolBar.addSeparator();
201: PropertyChangeController controller = new PropertyChangeController();
202: toolBar.addPropertyChangeListener("orientation", controller);
203: assertEquals(SwingConstants.VERTICAL,
204: getToolBarSeparatorAtIndex(0).getOrientation());
205: toolBar.setOrientation(SwingConstants.VERTICAL);
206: assertTrue(controller.isChanged());
207: assertEquals(SwingConstants.HORIZONTAL,
208: getToolBarSeparatorAtIndex(0).getOrientation());
209: testExceptionalCase(new IllegalArgumentCase() {
210: @Override
211: public void exceptionalAction() throws Exception {
212: toolBar.setOrientation(INVALID_ORIENTATION);
213: }
214: });
215: }
216:
217: public void testSetIsRollover() {
218: assertFalse(toolBar.isRollover());
219: PropertyChangeController controller = new PropertyChangeController();
220: toolBar.addPropertyChangeListener("JToolBar.isRollover",
221: controller);
222: toolBar.setRollover(true);
223: assertTrue(controller.isChanged());
224: assertTrue(toolBar.isRollover());
225: }
226:
227: public void testAddSeparator() {
228: toolBar.addSeparator();
229: assertEquals(1, toolBar.getComponentCount());
230: assertEquals(UIManager.get("ToolBar.separatorSize"),
231: getToolBarSeparatorAtIndex(0).getSeparatorSize());
232: assertEquals(SwingConstants.VERTICAL,
233: getToolBarSeparatorAtIndex(0).getOrientation());
234: toolBar.setOrientation(SwingConstants.VERTICAL);
235: toolBar.addSeparator();
236: assertEquals(2, toolBar.getComponentCount());
237: assertEquals(SwingConstants.HORIZONTAL,
238: getToolBarSeparatorAtIndex(1).getOrientation());
239: }
240:
241: public void testAddSeparatorDimension() {
242: final Dimension size = new Dimension(5, 10);
243: toolBar.addSeparator(size);
244: assertEquals(1, toolBar.getComponentCount());
245: assertEquals(size, getToolBarSeparatorAtIndex(0)
246: .getSeparatorSize());
247: toolBar.addSeparator(null);
248: assertEquals(UIManager.get("ToolBar.separatorSize"),
249: getToolBarSeparatorAtIndex(1).getSeparatorSize());
250: }
251:
252: public void testAddAction() {
253: AbstractAction action = new AbstractAction() {
254: private static final long serialVersionUID = 1L;
255: {
256: putValue(Action.NAME, "action");
257: }
258:
259: public void actionPerformed(final ActionEvent e) {
260: }
261: };
262: JButton b = toolBar.add(action);
263: assertTrue(toolBar.isAncestorOf(b));
264: assertEquals(action.getValue(Action.NAME), b.getText());
265: assertSame(action, b.getAction());
266: action.setEnabled(false);
267: assertFalse(b.isEnabled());
268: }
269:
270: public void testCreateActionComponent() {
271: AbstractAction action = new AbstractAction() {
272: private static final long serialVersionUID = 1L;
273: {
274: putValue(Action.NAME, "action");
275: }
276:
277: public void actionPerformed(final ActionEvent e) {
278: }
279: };
280: JButton b = toolBar.createActionComponent(action);
281: assertEquals(action.getValue(Action.NAME), b.getText());
282: assertNull(b.getAction());
283: b = toolBar.createActionComponent(null);
284: assertEquals("", b.getText());
285: }
286:
287: public void testCreateActionChangeListener() {
288: JButton b = new JButton("b");
289: assertNull(toolBar.createActionChangeListener(b));
290: }
291:
292: private JToolBar.Separator getToolBarSeparatorAtIndex(final int i) {
293: return (JToolBar.Separator) toolBar.getComponentAtIndex(i);
294: }
295: }
|