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 Alexander T. Simbirtsev
019: * @version $Revision$
020: */package javax.swing;
021:
022: import java.awt.Insets;
023: import java.awt.event.KeyEvent;
024: import javax.accessibility.AccessibleRole;
025: import javax.swing.plaf.MenuBarUI;
026: import javax.swing.plaf.basic.BasicMenuBarUI;
027: import javax.swing.plaf.metal.MetalMenuBarUI;
028:
029: public class JMenuBarTest extends SwingTestCase {
030: protected JMenuBar menuBar;
031:
032: @Override
033: protected void setUp() throws Exception {
034: super .setUp();
035: menuBar = new JMenuBar();
036: }
037:
038: @Override
039: protected void tearDown() throws Exception {
040: menuBar = null;
041: super .tearDown();
042: }
043:
044: /*
045: * Test method for 'javax.swing.JMenuBar.JMenuBar()'
046: */
047: public void testJMenuBar() {
048: assertTrue(menuBar.getSelectionModel() instanceof DefaultSingleSelectionModel);
049: assertEquals(0, menuBar.getComponentCount());
050: assertTrue(menuBar.isFocusable());
051: assertEquals(menuBar, menuBar.getComponent());
052: assertTrue(menuBar.isBorderPainted());
053: assertTrue(menuBar.isOpaque());
054: assertEquals(0, menuBar.getSubElements().length);
055: }
056:
057: /*
058: * Test method for 'javax.swing.JMenuBar.getAccessibleContext()'
059: */
060: public void testGetAccessibleContext() {
061: boolean assertedValue = (menuBar.getAccessibleContext() != null && menuBar
062: .getAccessibleContext().getClass().getName().equals(
063: "javax.swing.JMenuBar$AccessibleJMenuBar"));
064: assertTrue("AccessibleContext created properly ", assertedValue);
065: assertEquals("AccessibleRole", AccessibleRole.MENU_BAR, menuBar
066: .getAccessibleContext().getAccessibleRole());
067: }
068:
069: /*
070: * Test method for 'javax.swing.JMenuBar.getComponentIndex(Component)'
071: */
072: public void testGetComponentIndex() {
073: JMenuItem item1 = new JMenuItem();
074: JMenuItem item2 = new JMenuItem();
075: JMenuItem item3 = new JMenuItem();
076: menuBar.add(item1);
077: menuBar.add(item2);
078: assertEquals(0, menuBar.getComponentIndex(item1));
079: assertEquals(1, menuBar.getComponentIndex(item2));
080: assertEquals(-1, menuBar.getComponentIndex(item3));
081: }
082:
083: /*
084: * Test method for 'javax.swing.JMenuBar.processKeyBinding(KeyStroke, KeyEvent, int, boolean)'
085: */
086: public void testProcessKeyBinding() {
087: JMenuBar jm = new JMenuBar() {
088: public boolean processKeyBinding(KeyStroke ks, KeyEvent e,
089: int condition, boolean pressed) {
090: return super .processKeyBinding(ks, e, condition,
091: pressed);
092: }
093: };
094: KeyStroke ks = KeyStroke.getKeyStroke('x');
095:
096: try { // Regression test for HARMONY-2622
097: jm.processKeyBinding(ks, null, -1, true);
098: fail("IllegalArgumentException should have been thrown");
099: } catch (IllegalArgumentException e) {
100: // Expected
101: } catch (NullPointerException e) {
102: fail("NullPointerException is thrown instead of IllegalArgumentException");
103: }
104:
105: // TODO implement other checkings
106: }
107:
108: /*
109: * Test method for 'javax.swing.JMenuBar.getUIClassID()'
110: */
111: public void testGetUIClassID() {
112: assertEquals("MenuBarUI", menuBar.getUIClassID());
113: }
114:
115: /*
116: * Test method for 'javax.swing.JMenuBar.add(JMenu)'
117: */
118: public void testAddJMenu() {
119: JMenu item1 = new JMenu();
120: JMenu item2 = new JMenu();
121: menuBar.add(item1);
122: assertEquals(1, menuBar.getComponentCount());
123: assertSame(item1, menuBar.getComponent(0));
124: menuBar.add(item2);
125: assertEquals(2, menuBar.getComponentCount());
126: assertSame(item1, menuBar.getComponent(0));
127: assertSame(item2, menuBar.getComponent(1));
128: }
129:
130: /*
131: * Test method for 'javax.swing.JMenuBar.getComponent()'
132: */
133: public void testGetComponent() {
134: assertSame(menuBar, menuBar.getComponent());
135: }
136:
137: /*
138: * Test method for 'javax.swing.JMenuBar.getMenu(int)'
139: */
140: public void testGetMenu() {
141: JMenu menu1 = new JMenu();
142: JMenu menu2 = new JMenu();
143: menuBar.add(menu1);
144: assertEquals(menu1, menuBar.getMenu(0));
145: menuBar.add(menu2);
146: assertEquals(menu1, menuBar.getMenu(0));
147: assertEquals(menu2, menuBar.getMenu(1));
148: menuBar.add(new JButton());
149: assertEquals(menu1, menuBar.getMenu(0));
150: assertEquals(menu2, menuBar.getMenu(1));
151: assertNull(menuBar.getMenu(2));
152:
153: try {
154: JMenuBar jm = new JMenuBar();
155: jm.getMenu(0);
156: fail("ArrayIndexOutOfBoundsException expected");
157: } catch (ArrayIndexOutOfBoundsException e) {
158: // PASSED
159: }
160: }
161:
162: /*
163: * Test method for 'javax.swing.JMenuBar.getMenuCount()'
164: */
165: public void testGetMenuCount() {
166: JMenu item1 = new JMenu();
167: JMenuItem item2 = new JMenuItem();
168: assertEquals(0, menuBar.getMenuCount());
169: menuBar.add(item1);
170: assertEquals(1, menuBar.getMenuCount());
171: menuBar.add(item2);
172: assertEquals(2, menuBar.getMenuCount());
173: menuBar.add(new JSeparator());
174: assertEquals(3, menuBar.getMenuCount());
175: menuBar.add(new JButton(), 0);
176: assertEquals(4, menuBar.getMenuCount());
177: }
178:
179: /*
180: * Test method for 'javax.swing.JMenuBar.getSubElements()'
181: */
182: public void testGetSubElements() {
183: JMenu item1 = new JMenu();
184: JMenuItem item2 = new JMenuItem();
185: JMenu item3 = new JMenu();
186: JMenu item4 = new JMenu();
187: assertEquals(0, menuBar.getSubElements().length);
188: menuBar.add(item1);
189: assertEquals(1, menuBar.getSubElements().length);
190: assertSame(item1, menuBar.getSubElements()[0]);
191: menuBar.add(item2);
192: menuBar.add(new JButton());
193: assertEquals(2, menuBar.getSubElements().length);
194: assertSame(item1, menuBar.getSubElements()[0]);
195: assertSame(item2, menuBar.getSubElements()[1]);
196: menuBar.add(item3);
197: assertEquals(3, menuBar.getSubElements().length);
198: assertSame(item1, menuBar.getSubElements()[0]);
199: assertSame(item2, menuBar.getSubElements()[1]);
200: assertSame(item3, menuBar.getSubElements()[2]);
201: menuBar.add(new JButton());
202: assertEquals(3, menuBar.getSubElements().length);
203: assertSame(item1, menuBar.getSubElements()[0]);
204: assertSame(item2, menuBar.getSubElements()[1]);
205: assertSame(item3, menuBar.getSubElements()[2]);
206: item3.add(item4);
207: assertEquals(3, menuBar.getSubElements().length);
208: }
209:
210: /*
211: * Test method for 'javax.swing.JMenuBar.isSelected()'
212: */
213: public void testIsSelected() {
214: SingleSelectionModel selectionModel1 = new DefaultSingleSelectionModel();
215: JMenu item1 = new JMenu();
216: JMenuItem item2 = new JMenuItem();
217: if (isHarmony()) {
218: menuBar.setSelectionModel(null);
219: assertFalse(menuBar.isSelected());
220: }
221: menuBar.setSelectionModel(selectionModel1);
222: menuBar.add(item1);
223: menuBar.add(item2);
224: assertFalse(menuBar.isSelected());
225: menuBar.setSelected(item1);
226: assertTrue(menuBar.isSelected());
227: menuBar.setSelected(item2);
228: assertTrue(menuBar.isSelected());
229: menuBar.setSelected(new JButton());
230: assertFalse(menuBar.isSelected());
231: }
232:
233: /*
234: * Test method for 'javax.swing.JPopupMenu.isBorderPainted()'
235: */
236: public void testSetIsBorderPainted() {
237: PropertyChangeController listener = new PropertyChangeController();
238: menuBar.addPropertyChangeListener(listener);
239: assertTrue(menuBar.isBorderPainted());
240: menuBar.setBorderPainted(false);
241: assertFalse(menuBar.isBorderPainted());
242: listener.checkLastPropertyFired(menuBar, "borderPainted",
243: Boolean.TRUE, Boolean.FALSE);
244: listener.reset();
245: menuBar.setBorderPainted(true);
246: listener.checkLastPropertyFired(menuBar, "borderPainted",
247: Boolean.FALSE, Boolean.TRUE);
248: assertTrue(menuBar.isBorderPainted());
249: listener.reset();
250: menuBar.setBorderPainted(true);
251: assertFalse(listener.isChanged("borderPainted"));
252: }
253:
254: /*
255: * Test method for 'javax.swing.JMenuBar.setHelpMenu(JMenu)'
256: */
257: public void testSetGetHelpMenu() {
258: try {
259: menuBar.setHelpMenu(new JMenu());
260: fail("no exception has been thrown");
261: } catch (Error e) {
262: }
263: try {
264: menuBar.getHelpMenu();
265: fail("no exception has been thrown");
266: } catch (Error e) {
267: }
268: }
269:
270: /*
271: * Test method for 'javax.swing.JMenuBar.setMargin(Insets)'
272: */
273: public void testSetGetMargin() {
274: Insets defaultMargin = new Insets(2, 14, 2, 14);
275: menuBar.setMargin(defaultMargin);
276: Insets margin1 = new Insets(1, 1, 1, 1);
277: Insets margin2 = new Insets(2, 2, 2, 2);
278: PropertyChangeController listener1 = new PropertyChangeController();
279: menuBar.addPropertyChangeListener(listener1);
280: menuBar.setMargin(margin1);
281: assertSame(margin1, menuBar.getMargin());
282: listener1.checkLastPropertyFired(menuBar, "margin",
283: defaultMargin, margin1);
284: listener1.reset();
285: menuBar.setMargin(margin2);
286: assertSame(margin2, menuBar.getMargin());
287: listener1.checkLastPropertyFired(menuBar, "margin", margin1,
288: margin2);
289: listener1.reset();
290: menuBar.setMargin(margin2);
291: assertFalse(listener1.isChanged("margin"));
292: listener1.reset();
293: }
294:
295: /*
296: * Test method for 'javax.swing.JMenuBar.setSelected(Component)'
297: */
298: public void testSetSelected() {
299: SingleSelectionModel selectionModel1 = new DefaultSingleSelectionModel();
300: JMenu item1 = new JMenu();
301: JMenuItem item2 = new JMenuItem();
302: menuBar.setSelectionModel(null);
303: menuBar.add(item1);
304: menuBar.add(item2);
305: menuBar.setSelectionModel(selectionModel1);
306: menuBar.setSelected(item1);
307: assertEquals("selection", 0, selectionModel1.getSelectedIndex());
308: menuBar.setSelected(item2);
309: assertEquals("selection", 1, selectionModel1.getSelectedIndex());
310: menuBar.setSelected(new JButton());
311: assertEquals("selection", -1, selectionModel1
312: .getSelectedIndex());
313: }
314:
315: /*
316: * Test method for 'javax.swing.JMenuBar.setSelectionModel(SingleSelectionModel)'
317: */
318: public void testSetGetSelectionModel() {
319: SingleSelectionModel selectionModel1 = new DefaultSingleSelectionModel();
320: SingleSelectionModel selectionModel2 = new DefaultSingleSelectionModel();
321: PropertyChangeController listener = new PropertyChangeController();
322: menuBar.setSelectionModel(null);
323: menuBar.addPropertyChangeListener(listener);
324: menuBar.setSelectionModel(selectionModel1);
325: assertEquals("selectionModel", selectionModel1, menuBar
326: .getSelectionModel());
327: listener.checkLastPropertyFired(menuBar, "selectionModel",
328: null, selectionModel1);
329: listener.reset();
330: menuBar.setSelectionModel(selectionModel2);
331: assertEquals("selectionModel", selectionModel2, menuBar
332: .getSelectionModel());
333: listener.checkLastPropertyFired(menuBar, "selectionModel",
334: selectionModel1, selectionModel2);
335: listener.reset();
336: }
337:
338: /*
339: * Test method for 'javax.swing.JMenuBar.getUI()'
340: */
341: public void testGetUI() {
342: assertNotNull(menuBar.getUI());
343: }
344:
345: /*
346: * Test method for 'javax.swing.JMenuBar.setUI(MenuBarUI)'
347: */
348: public void testSetUIMenuBarUI() {
349: MenuBarUI ui1 = new BasicMenuBarUI();
350: MenuBarUI ui2 = new MetalMenuBarUI();
351: JMenuBar menuBar = new JMenuBar();
352: menuBar.setUI(ui1);
353: assertSame(ui1, menuBar.getUI());
354: menuBar.setUI(ui2);
355: assertSame(ui2, menuBar.getUI());
356: }
357: }
|