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.Component;
023: import java.awt.Container;
024: import java.awt.Dimension;
025: import java.awt.Insets;
026: import java.awt.Point;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.MouseEvent;
029: import java.awt.image.BufferedImage;
030: import java.beans.PropertyChangeListener;
031: import java.util.EventListener;
032: import javax.accessibility.AccessibleRole;
033: import javax.swing.event.MenuEvent;
034: import javax.swing.event.PopupMenuEvent;
035: import javax.swing.event.PopupMenuListener;
036: import javax.swing.plaf.PopupMenuUI;
037: import javax.swing.plaf.basic.BasicPopupMenuUI;
038:
039: public class JPopupMenuTest extends SwingTestCase {
040: public static class MyAction extends AbstractAction {
041: private static final long serialVersionUID = 1L;
042:
043: final String command = "dnammoc";
044:
045: final KeyStroke accelerator = KeyStroke.getKeyStroke('a');
046:
047: public boolean performed = false;
048:
049: public MyAction(final String text, final Icon icon) {
050: super (text, icon);
051: putValue(Action.ACTION_COMMAND_KEY, command);
052: putValue(Action.ACCELERATOR_KEY, accelerator);
053: }
054:
055: public void actionPerformed(final ActionEvent e) {
056: performed = true;
057: }
058: };
059:
060: class ConcretePopupMenuListener implements PopupMenuListener {
061: public Object event;
062:
063: public Object src;
064:
065: public void menuSelected(MenuEvent e) {
066: event = "selected";
067: src = e.getSource();
068: }
069:
070: public void popupMenuCanceled(PopupMenuEvent e) {
071: event = "canceled";
072: src = e.getSource();
073: }
074:
075: public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
076: event = "invisible";
077: src = e.getSource();
078: }
079:
080: public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
081: event = "visible";
082: src = e.getSource();
083: }
084:
085: public void reset() {
086: event = null;
087: src = null;
088: }
089: };
090:
091: protected JPopupMenu popup;
092:
093: @Override
094: protected void setUp() throws Exception {
095: super .setUp();
096: popup = new JPopupMenu();
097: }
098:
099: @Override
100: protected void tearDown() throws Exception {
101: popup = null;
102: super .tearDown();
103: }
104:
105: /*
106: * Test method for 'javax.swing.JPopupMenu.getAccessibleContext()'
107: */
108: public void testGetAccessibleContext() {
109: boolean assertedValue = (popup.getAccessibleContext() != null && popup
110: .getAccessibleContext().getClass().getName().equals(
111: "javax.swing.JPopupMenu$AccessibleJPopupMenu"));
112: assertTrue("AccessibleContext created properly ", assertedValue);
113: assertEquals("AccessibleRole", AccessibleRole.POPUP_MENU, popup
114: .getAccessibleContext().getAccessibleRole());
115: }
116:
117: /*
118: * Test method for 'javax.swing.JPopupMenu.setVisible(boolean)'
119: */
120: public void testSetIsVisible() {
121: PropertyChangeController listener1 = new PropertyChangeController();
122: popup.addPropertyChangeListener(listener1);
123: ConcretePopupMenuListener listener2 = new ConcretePopupMenuListener();
124: popup.addPopupMenuListener(listener2);
125: assertNull(popup.getParent());
126: assertFalse(popup.isVisible());
127: assertNull(SwingUtilities.getWindowAncestor(popup));
128: popup.setVisible(true);
129: assertNotNull(SwingUtilities.getWindowAncestor(popup));
130: Container ancestor = popup.getParent();
131: assertEquals("event fired properly ", "visible",
132: listener2.event);
133: assertEquals("source ", popup, listener2.src);
134: listener2.reset();
135: listener1.checkPropertyFired(popup, "visible", Boolean.FALSE,
136: Boolean.TRUE);
137: listener1.checkPropertyFired(popup, "ancestor", null, ancestor);
138: assertNotNull(ancestor);
139: assertTrue(popup.isVisible());
140: listener1.reset();
141: popup.setVisible(false);
142: assertNull(SwingUtilities.getWindowAncestor(popup));
143: assertEquals("event fired properly ", "invisible",
144: listener2.event);
145: assertEquals("source ", popup, listener2.src);
146: listener2.reset();
147: listener1.checkPropertyFired(popup, "visible", Boolean.TRUE,
148: Boolean.FALSE);
149: listener1.checkPropertyFired(popup, "ancestor", ancestor, null);
150: assertNull(popup.getParent());
151: assertFalse(popup.isVisible());
152: listener1.reset();
153: }
154:
155: /*
156: * Test method for 'javax.swing.JPopupMenu.setLocation(int, int)'
157: */
158: @SuppressWarnings("deprecation")
159: public void testSetLocationIntInt() {
160: JFrame frame = new JFrame();
161: JMenuBar menuBar = new JMenuBar();
162: JMenu menu1 = new JMenu();
163: JMenu menu2 = new JMenu();
164: frame.getContentPane().add(menuBar);
165: menuBar.add(menu1);
166: menuBar.add(menu2);
167: popup = menu1.getPopupMenu();
168: popup.setLocation(100, 200);
169: assertEquals(new Point(0, 0), popup.getLocation());
170: popup.setVisible(true);
171: assertEquals(new Point(100, 200), popup.getLocationOnScreen());
172: popup.setLocation(10, 20);
173: assertEquals(new Point(0, 0), popup.getLocation());
174: assertEquals(new Point(10, 20), popup.getLocationOnScreen());
175: popup.setInvoker(menu2);
176: popup.setLocation(100, 200);
177: assertEquals(new Point(0, 0), popup.getLocation());
178: assertEquals(new Point(100, 200), popup.getLocationOnScreen());
179: frame.pack();
180: frame.show();
181: popup.setVisible(true);
182: popup.setLocation(10, 20);
183: assertEquals(new Point(0, 0), popup.getLocation());
184: assertEquals(new Point(10, 20), popup.getLocationOnScreen());
185: popup.setInvoker(menu2);
186: popup.setLocation(100, 200);
187: assertEquals(new Point(0, 0), popup.getLocation());
188: assertEquals(new Point(100, 200), popup.getLocationOnScreen());
189: frame.dispose();
190: }
191:
192: /*
193: * Test method for 'javax.swing.JPopupMenu.show(Component, int, int)'
194: */
195: @SuppressWarnings("deprecation")
196: public void testShowComponentIntInt() {
197: JFrame frame = new JFrame();
198: JMenuBar menuBar = new JMenuBar();
199: JMenu menu1 = new JMenu();
200: JMenu menu2 = new JMenu();
201: frame.getContentPane().add(menuBar);
202: menuBar.add(menu1);
203: menuBar.add(menu2);
204: popup = menu1.getPopupMenu();
205: popup.setInvoker(new JButton());
206: assertNotNull(popup.getInvoker());
207: popup.show(null, 111, 222);
208: assertNull(popup.getInvoker());
209: assertEquals(new Point(111, 222), popup.getLocationOnScreen());
210: assertTrue(popup.isVisible());
211: assertTrue(popup.isShowing());
212: frame.pack();
213: frame.show();
214: popup.show(menu2, 333, 111);
215: assertSame(menu2, popup.getInvoker());
216: assertFalse(popup.getLocationOnScreen().equals(
217: new Point(333, 111)));
218: assertTrue(popup.isVisible());
219: assertTrue(popup.isShowing());
220: frame.dispose();
221: }
222:
223: /*
224: * Test method for 'javax.swing.JPopupMenu.getComponentIndex(Component)'
225: */
226: public void testGetComponentIndex() {
227: JMenuItem item1 = new JMenuItem();
228: JMenuItem item2 = new JMenuItem();
229: JMenuItem item3 = new JMenuItem();
230: popup.add(item1);
231: popup.add(item2);
232: assertEquals(0, popup.getComponentIndex(item1));
233: assertEquals(1, popup.getComponentIndex(item2));
234: assertEquals(-1, popup.getComponentIndex(item3));
235: }
236:
237: /*
238: * Test method for 'javax.swing.JPopupMenu.remove(int)'
239: */
240: public void testRemoveInt() {
241: JMenuItem item1 = new JMenuItem();
242: JMenuItem item2 = new JMenuItem();
243: popup.add(item1);
244: popup.add(item2);
245: assertEquals(2, popup.getComponentCount());
246: assertSame(item1, popup.getComponent(0));
247: assertSame(item2, popup.getComponent(1));
248: try {
249: popup.remove(5);
250: fail("no exception has been thrown");
251: } catch (IllegalArgumentException e) {
252: }
253: try {
254: popup.remove(-5);
255: fail("no exception has been thrown");
256: } catch (IllegalArgumentException e) {
257: }
258: popup.remove(0);
259: assertEquals(1, popup.getComponentCount());
260: assertSame(item2, popup.getComponent(0));
261: }
262:
263: /*
264: * Test method for 'javax.swing.JPopupMenu.getUIClassID()'
265: */
266: public void testGetUIClassID() {
267: assertEquals("PopupMenuUI", popup.getUIClassID());
268: }
269:
270: /*
271: * Test method for 'javax.swing.JPopupMenu.JPopupMenu()'
272: */
273: public void testJPopupMenu() {
274: assertNull("text ", popup.getLabel());
275: assertTrue(popup.getSelectionModel() instanceof DefaultSingleSelectionModel);
276: assertEquals(0, popup.getComponentCount());
277: assertTrue(popup.isFocusable());
278: assertEquals(popup, popup.getComponent());
279: assertNull(popup.getInvoker());
280: assertEquals(0, popup.getSubElements().length);
281: }
282:
283: /*
284: * Test method for 'javax.swing.JPopupMenu.JPopupMenu(String)'
285: */
286: public void testJPopupMenuString() {
287: String label = "label";
288: popup = new JPopupMenu(label);
289: assertEquals("text ", label, popup.getLabel());
290: assertTrue(popup.getSelectionModel() instanceof DefaultSingleSelectionModel);
291: assertEquals(0, popup.getComponentCount());
292: assertTrue(popup.isFocusable());
293: assertEquals(popup, popup.getComponent());
294: assertNull(popup.getInvoker());
295: assertEquals(0, popup.getSubElements().length);
296: popup = new JPopupMenu(null);
297: assertNull("text ", popup.getLabel());
298: assertTrue(popup.getSelectionModel() instanceof DefaultSingleSelectionModel);
299: assertEquals(0, popup.getComponentCount());
300: assertTrue(popup.isFocusable());
301: assertEquals(popup, popup.getComponent());
302: assertNull(popup.getInvoker());
303: assertEquals(0, popup.getSubElements().length);
304: }
305:
306: /*
307: * Test method for 'javax.swing.JPopupMenu.JPopupMenu()'
308: */
309: public void testJPopupMenuSeparator() {
310: JSeparator separator = new JPopupMenu.Separator();
311: assertEquals("PopupMenuSeparatorUI", separator.getUIClassID());
312:
313: // Regression test for HARMONY-2631
314: assertFalse(separator.isFocusable());
315: }
316:
317: /*
318: * Test method for 'javax.swing.JPopupMenu.add(Action)'
319: */
320: public void testAddAction() {
321: Icon icon = new ImageIcon(new BufferedImage(20, 20,
322: BufferedImage.TYPE_BYTE_GRAY));
323: String text = "texttext";
324: MyAction action = new MyAction(text, icon);
325: JMenuItem menuItem = popup.add(action);
326: assertEquals(1, popup.getComponentCount());
327: assertTrue(popup.getComponent(0) instanceof JMenuItem);
328: menuItem = ((JMenuItem) popup.getComponent(0));
329: assertEquals("icon ", icon, menuItem.getIcon());
330: assertEquals("text ", text, menuItem.getText());
331: assertSame(action, menuItem.getAction());
332: }
333:
334: /*
335: * Test method for 'javax.swing.JPopupMenu.add(String)'
336: */
337: public void testAddString() {
338: String label1 = "label1";
339: String label2 = "label1";
340: popup.add((String) null);
341: assertEquals(1, popup.getComponentCount());
342: assertTrue(popup.getComponent(0) instanceof JMenuItem);
343: assertEquals("", ((JMenuItem) popup.getComponent(0)).getText());
344: popup.add(label1);
345: assertEquals(2, popup.getComponentCount());
346: assertTrue(popup.getComponent(1) instanceof JMenuItem);
347: assertEquals(label1, ((JMenuItem) popup.getComponent(1))
348: .getText());
349: popup.add(label2);
350: assertEquals(3, popup.getComponentCount());
351: assertTrue(popup.getComponent(2) instanceof JMenuItem);
352: assertEquals(label2, ((JMenuItem) popup.getComponent(2))
353: .getText());
354: }
355:
356: /*
357: * Test method for 'javax.swing.JPopupMenu.add(JMenuItem)'
358: */
359: public void testAddJMenuItem() {
360: JMenuItem item1 = new JMenuItem();
361: JMenuItem item2 = new JMenuItem();
362: popup.add(item1);
363: assertEquals(1, popup.getComponentCount());
364: assertSame(item1, popup.getComponent(0));
365: popup.add(item2);
366: assertEquals(2, popup.getComponentCount());
367: assertSame(item1, popup.getComponent(0));
368: assertSame(item2, popup.getComponent(1));
369: }
370:
371: /*
372: * Test method for 'javax.swing.JPopupMenu.addPopupMenuListener(PopupMenuListener)'
373: */
374: public void testAddGetRemovePopupMenuListener() {
375: PopupMenuListener listener1 = new ConcretePopupMenuListener();
376: PopupMenuListener listener2 = new ConcretePopupMenuListener();
377: PopupMenuListener listener3 = new ConcretePopupMenuListener();
378: EventListener[] listenersArray = null;
379: listenersArray = popup.getPopupMenuListeners();
380: int initialValue = listenersArray.length;
381: popup.addPopupMenuListener(listener1);
382: popup.addPopupMenuListener(listener2);
383: popup.addPopupMenuListener(listener2);
384: listenersArray = popup.getPopupMenuListeners();
385: assertEquals(initialValue + 3, listenersArray.length);
386: popup.removePopupMenuListener(listener1);
387: popup.addPopupMenuListener(listener3);
388: popup.addPopupMenuListener(listener3);
389: listenersArray = popup.getPopupMenuListeners();
390: assertEquals(initialValue + 4, listenersArray.length);
391: popup.removePopupMenuListener(listener3);
392: popup.removePopupMenuListener(listener3);
393: listenersArray = popup.getPopupMenuListeners();
394: assertEquals(initialValue + 2, listenersArray.length);
395: popup.removePopupMenuListener(listener2);
396: popup.removePopupMenuListener(listener2);
397: listenersArray = popup.getPopupMenuListeners();
398: assertEquals(initialValue, listenersArray.length);
399: }
400:
401: /*
402: * Test method for 'javax.swing.JPopupMenu.addSeparator()'
403: */
404: public void testAddSeparator() {
405: JMenuItem item1 = new JMenuItem();
406: JMenuItem item2 = new JMenuItem();
407: popup.add(item1);
408: assertEquals(1, popup.getComponentCount());
409: assertSame(item1, popup.getComponent(0));
410: popup.addSeparator();
411: assertEquals(2, popup.getComponentCount());
412: assertTrue(popup.getComponent(1) instanceof JPopupMenu.Separator);
413: popup.add(item2);
414: assertEquals(3, popup.getComponentCount());
415: assertSame(item1, popup.getComponent(0));
416: assertSame(item2, popup.getComponent(2));
417: popup.addSeparator();
418: assertEquals(4, popup.getComponentCount());
419: assertTrue(popup.getComponent(3) instanceof JPopupMenu.Separator);
420: assertNotSame(popup.getComponent(1), popup.getComponent(3));
421: }
422:
423: /*
424: * Test method for 'javax.swing.JPopupMenu.createActionChangeListener(JMenuItem)'
425: */
426: public void testCreateActionChangeListener() {
427: AbstractAction action1 = new AbstractAction() {
428: private static final long serialVersionUID = 1L;
429:
430: public void actionPerformed(ActionEvent e) {
431: }
432: };
433: JMenuItem item1 = new JMenuItem(action1);
434: JMenuItem item2 = new JMenuItem();
435: PropertyChangeListener listener1 = popup
436: .createActionChangeListener(item2);
437: PropertyChangeListener listener2 = popup
438: .createActionChangeListener(item1);
439: assertNotNull(listener1);
440: assertNotNull(listener2);
441: assertNotSame(listener1, listener2);
442: String text = "textxetxet";
443: action1.putValue(Action.NAME, text);
444: assertEquals(text, item1.getText());
445: }
446:
447: /*
448: * Test method for 'javax.swing.JPopupMenu.createActionComponent(Action)'
449: */
450: public void testCreateActionComponent() {
451: Icon icon = new ImageIcon(new BufferedImage(20, 20,
452: BufferedImage.TYPE_BYTE_GRAY));
453: String text = "texttext";
454: MyAction action = new MyAction(text, icon);
455: JMenuItem menuItem = popup.createActionComponent(action);
456: assertEquals("icon ", icon, menuItem.getIcon());
457: assertEquals("text ", text, menuItem.getText());
458: assertNull("action", menuItem.getAction());
459: assertEquals("command ", text, menuItem.getActionCommand());
460: assertFalse("selected ", menuItem.isSelected());
461: assertTrue("enabled ", menuItem.isEnabled());
462: }
463:
464: /*
465: * Test method for 'javax.swing.JPopupMenu.firePopupMenuCanceled()'
466: */
467: public void testFirePopupMenuCanceled() {
468: ConcretePopupMenuListener listener1 = new ConcretePopupMenuListener();
469: ConcretePopupMenuListener listener2 = new ConcretePopupMenuListener();
470: popup.addPopupMenuListener(listener1);
471: popup.addPopupMenuListener(listener2);
472: popup.firePopupMenuCanceled();
473: assertEquals("event fired properly ", "canceled",
474: listener1.event);
475: assertEquals("event fired properly ", "canceled",
476: listener2.event);
477: assertEquals("source ", popup, listener1.src);
478: assertEquals("source ", popup, listener2.src);
479: }
480:
481: /*
482: * Test method for 'javax.swing.JPopupMenu.firePopupMenuWillBecomeInvisible()'
483: */
484: public void testFirePopupMenuWillBecomeInvisible() {
485: ConcretePopupMenuListener listener1 = new ConcretePopupMenuListener();
486: ConcretePopupMenuListener listener2 = new ConcretePopupMenuListener();
487: popup.addPopupMenuListener(listener1);
488: popup.addPopupMenuListener(listener2);
489: popup.firePopupMenuWillBecomeInvisible();
490: assertEquals("event fired properly ", "invisible",
491: listener1.event);
492: assertEquals("event fired properly ", "invisible",
493: listener2.event);
494: assertEquals("source ", popup, listener1.src);
495: assertEquals("source ", popup, listener2.src);
496: }
497:
498: /*
499: * Test method for 'javax.swing.JPopupMenu.firePopupMenuWillBecomeVisible()'
500: */
501: public void testFirePopupMenuWillBecomeVisible() {
502: ConcretePopupMenuListener listener1 = new ConcretePopupMenuListener();
503: ConcretePopupMenuListener listener2 = new ConcretePopupMenuListener();
504: popup.addPopupMenuListener(listener1);
505: popup.addPopupMenuListener(listener2);
506: popup.firePopupMenuWillBecomeVisible();
507: assertEquals("event fired properly ", "visible",
508: listener1.event);
509: assertEquals("event fired properly ", "visible",
510: listener2.event);
511: assertEquals("source ", popup, listener1.src);
512: assertEquals("source ", popup, listener2.src);
513: }
514:
515: /*
516: * Test method for 'javax.swing.JPopupMenu.getComponent()'
517: */
518: public void testGetComponent() {
519: assertSame(popup, popup.getComponent());
520: }
521:
522: /*
523: * Test method for 'javax.swing.JPopupMenu.getComponent(int)'
524: */
525: @SuppressWarnings("deprecation")
526: public void testGetComponentAtIndex() {
527: JMenuItem item1 = new JMenuItem();
528: JMenuItem item2 = new JMenuItem();
529: popup.add(item1);
530: assertEquals(1, popup.getComponentCount());
531: assertSame(item1, popup.getComponentAtIndex(0));
532: popup.add(item2);
533: assertEquals(2, popup.getComponentCount());
534: assertSame(item1, popup.getComponentAtIndex(0));
535: assertSame(item2, popup.getComponentAtIndex(1));
536: }
537:
538: /*
539: * Test method for 'javax.swing.JPopupMenu.getMargin()'
540: */
541: public void testGetMargin() {
542: assertEquals(new Insets(0, 0, 0, 0), popup.getMargin());
543: }
544:
545: /*
546: * Test method for 'javax.swing.JPopupMenu.getSubElements()'
547: */
548: public void testGetSubElements() {
549: JMenuItem item1 = new JMenuItem();
550: JMenuItem item2 = new JMenuItem();
551: JMenuItem item3 = new JMenuItem();
552: assertEquals(0, popup.getSubElements().length);
553: popup.add(item1);
554: assertEquals(1, popup.getSubElements().length);
555: assertSame(item1, popup.getSubElements()[0]);
556: popup.add(item2);
557: popup.addSeparator();
558: assertEquals(2, popup.getSubElements().length);
559: assertSame(item1, popup.getSubElements()[0]);
560: assertSame(item2, popup.getSubElements()[1]);
561: popup.add(item3);
562: assertEquals(3, popup.getSubElements().length);
563: assertSame(item1, popup.getSubElements()[0]);
564: assertSame(item2, popup.getSubElements()[1]);
565: assertSame(item3, popup.getSubElements()[2]);
566: popup.addSeparator();
567: assertEquals(3, popup.getSubElements().length);
568: assertSame(item1, popup.getSubElements()[0]);
569: assertSame(item2, popup.getSubElements()[1]);
570: assertSame(item3, popup.getSubElements()[2]);
571: item1.setEnabled(false);
572: assertEquals(3, popup.getSubElements().length);
573: }
574:
575: /*
576: * Test method for 'javax.swing.JPopupMenu.getUI()'
577: */
578: public void testSetGetUI() {
579: assertNotNull("ui is returned ", popup.getUI());
580: PopupMenuUI ui = new BasicPopupMenuUI();
581: popup.setUI(ui);
582: assertSame("ui is returned ", ui, popup.getUI());
583: }
584:
585: /*
586: * Test method for 'javax.swing.JPopupMenu.insert(Component, int)'
587: */
588: public void testInsertComponentInt() {
589: JMenuItem item1 = new JMenuItem();
590: JMenuItem item2 = new JMenuItem();
591: Component item3 = new JPanel();
592: popup.insert(item1, 0);
593: assertEquals(1, popup.getComponentCount());
594: assertSame(item1, popup.getComponent(0));
595: popup.insert(item2, 0);
596: assertEquals(2, popup.getComponentCount());
597: assertSame(item2, popup.getComponent(0));
598: assertSame(item1, popup.getComponent(1));
599: popup.insert(item3, 1);
600: assertEquals(3, popup.getComponentCount());
601: assertSame(item2, popup.getComponent(0));
602: assertSame(item3, popup.getComponent(1));
603: assertSame(item1, popup.getComponent(2));
604: popup.insert(item3, 16);
605: assertEquals(3, popup.getComponentCount());
606: try {
607: popup.insert(item3, -6);
608: fail("no exception has been thrown");
609: } catch (IllegalArgumentException e) {
610: }
611: }
612:
613: /*
614: * Test method for 'javax.swing.JPopupMenu.insert(Action, int)'
615: */
616: public void testInsertActionInt() {
617: Icon icon = new ImageIcon(new BufferedImage(20, 20,
618: BufferedImage.TYPE_BYTE_GRAY));
619: String text = "texttext";
620: MyAction action1 = new MyAction(text, icon);
621: MyAction action2 = new MyAction(text, icon);
622: popup.insert(action1, 0);
623: assertEquals(1, popup.getComponentCount());
624: assertTrue(popup.getComponent(0) instanceof JMenuItem);
625: JMenuItem menuItem = ((JMenuItem) popup.getComponent(0));
626: assertEquals("icon ", icon, menuItem.getIcon());
627: assertEquals("text ", text, menuItem.getText());
628: popup.insert(action2, 1);
629: assertEquals(2, popup.getComponentCount());
630: assertTrue(popup.getComponent(0) instanceof JMenuItem);
631: menuItem = ((JMenuItem) popup.getComponent(1));
632: assertEquals("icon ", icon, menuItem.getIcon());
633: assertEquals("text ", text, menuItem.getText());
634: popup.insert(action1, 16);
635: assertEquals(3, popup.getComponentCount());
636: assertTrue(popup.getComponent(2) instanceof JMenuItem);
637: menuItem = ((JMenuItem) popup.getComponent(2));
638: assertEquals("icon ", icon, menuItem.getIcon());
639: assertEquals("text ", text, menuItem.getText());
640: try {
641: popup.insert(action1, -3);
642: fail("no exception has been thrown");
643: } catch (IllegalArgumentException e) {
644: }
645: }
646:
647: /*
648: * Test method for 'javax.swing.JPopupMenu.isBorderPainted()'
649: */
650: public void testSetIsBorderPainted() {
651: assertTrue(popup.isBorderPainted());
652: popup.setBorderPainted(false);
653: assertFalse(popup.isBorderPainted());
654: popup.setBorderPainted(true);
655: assertTrue(popup.isBorderPainted());
656: }
657:
658: /*
659: * Test method for 'javax.swing.JPopupMenu.isPopupTrigger(MouseEvent)'
660: */
661: public void testIsPopupTrigger() {
662: class MyPopupMenuUI extends PopupMenuUI {
663: public boolean value = false;
664:
665: @Override
666: public boolean isPopupTrigger(final MouseEvent event) {
667: return value;
668: }
669: }
670: ;
671: MyPopupMenuUI fakeUI = new MyPopupMenuUI();
672: popup.setUI(fakeUI);
673: assertSame(fakeUI, popup.getUI());
674: fakeUI.value = true;
675: assertEquals(fakeUI.value, popup.isPopupTrigger(null));
676: fakeUI.value = false;
677: assertEquals(fakeUI.value, popup.isPopupTrigger(null));
678: }
679:
680: /*
681: * Test method for 'javax.swing.JPopupMenu.menuSelectionChanged(boolean)'
682: */
683: @SuppressWarnings("deprecation")
684: public void testMenuSelectionChanged() {
685: JFrame frame = new JFrame();
686: JMenuBar menuBar = new JMenuBar();
687: JMenu menu = new JMenu();
688: frame.getContentPane().add(menuBar);
689: menuBar.add(menu);
690: popup = menu.getPopupMenu();
691: popup.menuSelectionChanged(true);
692: assertFalse(popup.isShowing());
693: assertFalse(popup.isVisible());
694: frame.pack();
695: frame.show();
696: popup.menuSelectionChanged(true);
697: assertTrue(popup.isShowing());
698: assertTrue(popup.isVisible());
699: popup.menuSelectionChanged(false);
700: assertFalse(popup.isShowing());
701: assertFalse(popup.isVisible());
702: frame.dispose();
703: }
704:
705: /*
706: * Test method for 'javax.swing.JPopupMenu.pack()'
707: */
708: public void testPack() {
709: JButton button1 = new JButton();
710: JButton button2 = new JButton();
711: popup.add(button1);
712: popup.add(button2);
713: popup.pack();
714: Dimension size = popup.getMinimumSize();
715: assertEquals(size, popup.getMinimumSize());
716: assertEquals(size, popup.getPreferredSize());
717: assertEquals(size, popup.getMaximumSize());
718: }
719:
720: /*
721: * Test method for 'javax.swing.JPopupMenu.processKeyEvent(KeyEvent, MenuElement[], MenuSelectionManager)'
722: * as method has an empty body test is empty either
723: */
724: public void testProcessKeyEventKeyEventMenuElementArrayMenuSelectionManager() {
725: }
726:
727: /*
728: * Test method for 'javax.swing.JPopupMenu.setDefaultLightWeightPopupEnabled(boolean)'
729: */
730: public void testSetGetDefaultLightWeightPopupEnabled() {
731: assertTrue(JPopupMenu.getDefaultLightWeightPopupEnabled());
732: JPopupMenu.setDefaultLightWeightPopupEnabled(false);
733: assertFalse(JPopupMenu.getDefaultLightWeightPopupEnabled());
734: JPopupMenu.setDefaultLightWeightPopupEnabled(true);
735: assertTrue(JPopupMenu.getDefaultLightWeightPopupEnabled());
736: }
737:
738: /*
739: * Test method for 'javax.swing.JPopupMenu.getInvoker()'
740: */
741: public void testGetInvoker() {
742: assertNull(popup.getInvoker());
743: JMenu menu = new JMenu();
744: assertSame(menu, menu.getPopupMenu().getInvoker());
745: }
746:
747: /*
748: * Test method for 'javax.swing.JPopupMenu.setInvoker(Component)'
749: */
750: public void testSetInvoker() {
751: Component invoker1 = new JButton();
752: Component invoker2 = new JLabel();
753: popup.setInvoker(invoker1);
754: assertEquals("invoker", invoker1, popup.getInvoker());
755: popup.setInvoker(invoker2);
756: assertEquals("invoker", invoker2, popup.getInvoker());
757: popup.setInvoker(invoker2);
758: assertEquals("invoker", invoker2, popup.getInvoker());
759: }
760:
761: /*
762: * Test method for 'javax.swing.JPopupMenu.getLabel()'
763: */
764: public void testGetLabel() {
765: assertNull(popup.getLabel());
766: }
767:
768: /*
769: * Test method for 'javax.swing.JPopupMenu.setLabel(String)'
770: */
771: public void testSetLabel() {
772: PropertyChangeController listener = new PropertyChangeController();
773: String label1 = "dog is dog";
774: String label2 = "0xdeadbeef";
775: popup.setLabel(null);
776: popup.addPropertyChangeListener(listener);
777: popup.setLabel(label1);
778: listener.checkPropertyFired(popup, "label", null, label1);
779: assertEquals("label", label1, popup.getLabel());
780: listener.reset();
781: popup.setLabel(label2);
782: listener.checkPropertyFired(popup, "label", label1, label2);
783: assertEquals("label", label2, popup.getLabel());
784: listener.reset();
785: popup.setLabel(label2);
786: assertFalse("event's not been fired ", listener.isChanged());
787: }
788:
789: /*
790: * Test method for 'javax.swing.JPopupMenu.setLightWeightPopupEnabled(boolean)'
791: */
792: public void testSetIsLightWeightPopupEnabled() {
793: assertTrue(JPopupMenu.getDefaultLightWeightPopupEnabled());
794: JPopupMenu menu = new JPopupMenu();
795: assertTrue(menu.isLightWeightPopupEnabled());
796: menu.setLightWeightPopupEnabled(false);
797: assertFalse(menu.isLightWeightPopupEnabled());
798: JPopupMenu.setDefaultLightWeightPopupEnabled(false);
799: menu = new JPopupMenu();
800: assertFalse(menu.isLightWeightPopupEnabled());
801: menu.setLightWeightPopupEnabled(true);
802: assertTrue(menu.isLightWeightPopupEnabled());
803: JPopupMenu.setDefaultLightWeightPopupEnabled(true);
804: }
805:
806: /*
807: * Test method for 'javax.swing.JPopupMenu.setPopupSize(int, int)'
808: */
809: public void testSetPopupSizeIntInt() {
810: int height = 100;
811: int width = 200;
812: popup.setPopupSize(height, width);
813: assertEquals(new Dimension(height, width), popup
814: .getPreferredSize());
815: }
816:
817: /*
818: * Test method for 'javax.swing.JPopupMenu.setPopupSize(Dimension)'
819: */
820: public void testSetPopupSizeDimension() {
821: int height = 100;
822: int width = 200;
823: Dimension oldPrefs = popup.getPreferredSize();
824: popup.setPopupSize(new Dimension(height, width));
825: assertEquals(new Dimension(height, width), popup
826: .getPreferredSize());
827: popup.setPopupSize(null);
828: assertEquals(oldPrefs, popup.getPreferredSize());
829: }
830:
831: /*
832: * Test method for 'javax.swing.JPopupMenu.setSelected(Component)'
833: */
834: public void testSetSelected() {
835: SingleSelectionModel selectionModel1 = new DefaultSingleSelectionModel();
836: JMenuItem item1 = new JMenuItem();
837: JMenuItem item2 = new JMenuItem();
838: popup.setSelectionModel(null);
839: popup.add(item1);
840: popup.add(item2);
841: popup.setSelectionModel(selectionModel1);
842: popup.setSelected(item1);
843: assertEquals("selection", 0, selectionModel1.getSelectedIndex());
844: popup.setSelected(item2);
845: assertEquals("selection", 1, selectionModel1.getSelectedIndex());
846: popup.setSelected(new JButton());
847: assertEquals("selection", -1, selectionModel1
848: .getSelectedIndex());
849: }
850:
851: /*
852: * Test method for 'javax.swing.JPopupMenu.setSelectionModel(SingleSelectionModel)'
853: */
854: public void testSetGetSelectionModel() {
855: SingleSelectionModel selectionModel1 = new DefaultSingleSelectionModel();
856: SingleSelectionModel selectionModel2 = new DefaultSingleSelectionModel();
857: popup.setSelectionModel(null);
858: popup.setSelectionModel(selectionModel1);
859: assertEquals("selectionModel", selectionModel1, popup
860: .getSelectionModel());
861: popup.setSelectionModel(selectionModel2);
862: assertEquals("selectionModel", selectionModel2, popup
863: .getSelectionModel());
864: }
865: }
|