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.event.KeyEvent;
024: import java.awt.event.MouseEvent;
025: import java.util.EventListener;
026: import javax.swing.event.ChangeEvent;
027: import javax.swing.event.ChangeListener;
028:
029: public class MenuSelectionManagerTest extends SwingTestCase {
030: // class ConcreteMenuDragMouseListener implements MenuDragMouseListener {
031: // public Object event;
032: // public void menuDragMouseDragged(MenuDragMouseEvent e) {
033: // event = "dragged";
034: // }
035: // public void menuDragMouseEntered(MenuDragMouseEvent e) {
036: // event = "entered";
037: // }
038: // public void menuDragMouseExited(MenuDragMouseEvent e) {
039: // event = "exited";
040: // }
041: // public void menuDragMouseReleased(MenuDragMouseEvent e) {
042: // event = "released";
043: // }
044: // };
045: // JMenuItem itemitem = new JMenuItem() {
046: // public void processKeyEvent(KeyEvent event, MenuElement[] path, MenuSelectionManager manager) {
047: // System.out.println("itemitem: processKeyEvent");
048: // super.processKeyEvent(event, path, manager);
049: // }
050: //
051: // public int getMnemonic() {
052: // System.out.println("itemitem: getMnemonic");
053: // return super.getMnemonic();
054: // }
055: // };
056: class ConcreteMenuElement extends JComponent implements MenuElement {
057: private static final long serialVersionUID = 1L;
058:
059: private Component component;
060:
061: private String name;
062:
063: public boolean selected = false;
064:
065: public ConcreteMenuElement(String name, Component component) {
066: this .component = component;
067: this .name = name;
068: }
069:
070: public ConcreteMenuElement(String name) {
071: this .component = this ;
072: this .name = name;
073: }
074:
075: public void processMouseEvent(MouseEvent event,
076: MenuElement[] path, MenuSelectionManager manager) {
077: // System.out.println("processMouseEvent (" + name + ")");
078: }
079:
080: public void processKeyEvent(KeyEvent event, MenuElement[] path,
081: MenuSelectionManager manager) {
082: // System.out.println("processKeyEvent (" + name + ")");
083: }
084:
085: public void menuSelectionChanged(boolean isIncluded) {
086: // System.out.println("menuSelectionChanged (" + name + ")" + ": " + isIncluded);
087: selected = isIncluded;
088: }
089:
090: public MenuElement[] getSubElements() {
091: // System.out.println("getSubElements (" + name + ")");
092: return new MenuElement[] {
093: new ConcreteMenuElement(name + "1", component),
094: new ConcreteMenuElement(name + "2", component) };
095: }
096:
097: public Component getComponent() {
098: // System.out.println("getComponent (" + name + ")");
099: return component;
100: }
101: }
102:
103: class ConcreteChangeListener implements ChangeListener {
104: public ChangeEvent eventHappened;
105:
106: private final boolean debugOut;
107:
108: public ConcreteChangeListener() {
109: debugOut = false;
110: }
111:
112: public ConcreteChangeListener(final boolean debugOut) {
113: this .debugOut = debugOut;
114: }
115:
116: public void stateChanged(final ChangeEvent event) {
117: eventHappened = event;
118: if (debugOut) {
119: System.out.println("stateChanged");
120: System.out.println("Class " + event.getClass());
121: System.out.println("Source " + event.getSource());
122: System.out.println();
123: }
124: }
125: };
126:
127: protected MenuSelectionManager manager;
128:
129: @Override
130: protected void setUp() throws Exception {
131: super .setUp();
132: manager = new MenuSelectionManager();
133: }
134:
135: @Override
136: protected void tearDown() throws Exception {
137: manager = null;
138: super .tearDown();
139: }
140:
141: /*
142: * Test method for 'javax.swing.MenuSelectionManager.MenuSelectionManager()'
143: */
144: public void testMenuSelectionManager() {
145: assertNotNull(manager.listenerList);
146: assertEquals(0, manager.listenerList.getListenerCount());
147: assertNull(manager.changeEvent);
148: assertEquals(0, manager.getSelectedPath().length);
149: }
150:
151: /*
152: * Test method for 'javax.swing.MenuSelectionManager.addChangeListener(ChangeListener)'
153: */
154: public void testAddGetRemoveChangeListener() {
155: ChangeListener listener1 = new ConcreteChangeListener();
156: ChangeListener listener2 = new ConcreteChangeListener();
157: ChangeListener listener3 = new ConcreteChangeListener();
158: EventListener[] listenersArray = null;
159: listenersArray = manager.getChangeListeners();
160: int initialValue = listenersArray.length;
161: manager.addChangeListener(listener1);
162: manager.addChangeListener(listener2);
163: manager.addChangeListener(listener2);
164: listenersArray = manager.getChangeListeners();
165: assertEquals(initialValue + 3, listenersArray.length);
166: manager.removeChangeListener(listener1);
167: manager.addChangeListener(listener3);
168: manager.addChangeListener(listener3);
169: listenersArray = manager.getChangeListeners();
170: assertEquals(initialValue + 4, listenersArray.length);
171: manager.removeChangeListener(listener3);
172: manager.removeChangeListener(listener3);
173: listenersArray = manager.getChangeListeners();
174: assertEquals(initialValue + 2, listenersArray.length);
175: manager.removeChangeListener(listener2);
176: manager.removeChangeListener(listener2);
177: listenersArray = manager.getChangeListeners();
178: assertEquals(initialValue, listenersArray.length);
179: }
180:
181: /*
182: * Test method for 'javax.swing.MenuSelectionManager.componentForPoint(Component, Point)'
183: */
184: public void testComponentForPoint() {
185: // TODO implement
186: // final JMenuBar menuBar = new JMenuBar();
187: // final JMenu menu1 = new JMenu();
188: // final JMenu menu2 = new JMenu();
189: // final JMenu menu3 = new JMenu();
190: // JFrame frame = new JFrame();
191: // frame.getContentPane().add(menuBar);
192: // menuBar.add(menu1);
193: // menu1.add(menu2);
194: // menu2.add(menu3);
195: // menu1.setPreferredSize(new Dimension(20, 20));
196: // menu2.setPreferredSize(new Dimension(100, 100));
197: // menu3.setPreferredSize(new Dimension(100, 100));
198: // MenuElement[] path1 = new MenuElement[] {menuBar, menu1, menu2};
199: // MenuElement[] path2 = new MenuElement[] {menu3, menu1, menu2};
200: // MenuElement[] path4 = new MenuElement[] {menu1};
201: // MenuElement[] path5 = new MenuElement[] {menu2};
202: // MenuElement[] path6 = new MenuElement[] {menu3};
203: // frame.pack();
204: // frame.show();
205: // manager.setSelectedPath(path1);
206: // for (int i = 0; i < 100; i++) {
207: // if (manager.componentForPoint(frame, new Point(i, i)) != null) {
208: // System.out.println(manager.componentForPoint(frame, new Point(i, i)));
209: // }
210: // }
211: // menu1.setPopupMenuVisible(true);
212: // manager.setSelectedPath(path1);
213: // for (int i = 0; i < 100; i++) {
214: // if (manager.componentForPoint(frame, new Point(i, i)) != null) {
215: // System.out.println(manager.componentForPoint(frame, new Point(i, i)));
216: // }
217: // }
218: // frame.dispose();
219: }
220:
221: /*
222: * Test method for 'javax.swing.MenuSelectionManager.defaultManager()'
223: */
224: public void testDefaultManager() {
225: assertNotNull(MenuSelectionManager.defaultManager());
226: assertSame(MenuSelectionManager.defaultManager(),
227: MenuSelectionManager.defaultManager());
228: }
229:
230: /*
231: * Test method for 'javax.swing.MenuSelectionManager.fireStateChanged()'
232: */
233: public void testFireStateChanged() {
234: ConcreteChangeListener listener1 = new ConcreteChangeListener();
235: ConcreteChangeListener listener2 = new ConcreteChangeListener();
236: manager.addChangeListener(listener1);
237: manager.addChangeListener(listener2);
238: manager.fireStateChanged();
239: assertNotNull("event fired properly ", listener1.eventHappened);
240: assertNotNull("event fired properly ", listener2.eventHappened);
241: assertSame("one event fired ", listener1.eventHappened,
242: listener2.eventHappened);
243: ChangeEvent event1 = listener1.eventHappened;
244: listener1.eventHappened = null;
245: listener2.eventHappened = null;
246: manager.fireStateChanged();
247: assertNotNull("event fired properly ", listener1.eventHappened);
248: assertNotNull("event fired properly ", listener2.eventHappened);
249: assertSame("one event fired ", listener1.eventHappened,
250: listener2.eventHappened);
251: assertSame("one event fired ", event1, listener1.eventHappened);
252: }
253:
254: /*
255: * Test method for 'javax.swing.MenuSelectionManager.isComponentPartOfCurrentMenu(Component)'
256: */
257: public void testIsComponentPartOfCurrentMenu() {
258: final JMenu menu1 = new JMenu();
259: final JMenu menu2 = new JMenu();
260: final JMenu menu3 = new JMenu();
261: final JMenu menu4 = new JMenu();
262: final JMenuItem menu5 = new JMenuItem();
263: final JMenuBar menu6 = new JMenuBar();
264: final JMenuItem menu7 = new JMenuItem();
265: menu6.add(menu4);
266: menu4.add(menu5);
267: menu6.add(menu7);
268: menu1.add(menu2);
269: menu3.add(menu1);
270: MenuElement[] path1 = new MenuElement[] { menu1, menu2,
271: new JMenuItem() };
272: MenuElement[] path2 = new MenuElement[] { menu3, menu1,
273: new JCheckBoxMenuItem() };
274: MenuElement[] path3 = new MenuElement[] { menu4, menu1,
275: new JCheckBoxMenuItem() };
276: MenuElement[] path4 = new MenuElement[] { menu3 };
277: MenuElement[] path5 = new MenuElement[] { menu5 };
278: MenuElement[] path6 = new MenuElement[] { menu6 };
279: assertFalse(manager.isComponentPartOfCurrentMenu(menu1));
280: assertFalse(manager.isComponentPartOfCurrentMenu(menu2));
281: assertFalse(manager.isComponentPartOfCurrentMenu(menu3));
282: assertFalse(manager.isComponentPartOfCurrentMenu(menu4));
283: manager.setSelectedPath(path1);
284: assertTrue(manager.isComponentPartOfCurrentMenu(menu1));
285: assertTrue(manager.isComponentPartOfCurrentMenu(menu2));
286: assertFalse(manager.isComponentPartOfCurrentMenu(menu3));
287: assertFalse(manager.isComponentPartOfCurrentMenu(menu4));
288: manager.setSelectedPath(path2);
289: assertTrue(manager.isComponentPartOfCurrentMenu(menu1));
290: assertTrue(manager.isComponentPartOfCurrentMenu(menu2));
291: assertTrue(manager.isComponentPartOfCurrentMenu(menu3));
292: assertFalse(manager.isComponentPartOfCurrentMenu(menu4));
293: manager.setSelectedPath(path3);
294: assertFalse(manager.isComponentPartOfCurrentMenu(menu1));
295: assertFalse(manager.isComponentPartOfCurrentMenu(menu2));
296: assertFalse(manager.isComponentPartOfCurrentMenu(menu3));
297: assertTrue(manager.isComponentPartOfCurrentMenu(menu4));
298: manager.setSelectedPath(path4);
299: assertTrue(manager.isComponentPartOfCurrentMenu(menu1));
300: assertTrue(manager.isComponentPartOfCurrentMenu(menu2));
301: assertTrue(manager.isComponentPartOfCurrentMenu(menu3));
302: assertFalse(manager.isComponentPartOfCurrentMenu(menu4));
303: manager.setSelectedPath(path5);
304: assertTrue(manager.isComponentPartOfCurrentMenu(menu5));
305: manager.setSelectedPath(path6);
306: assertTrue(manager.isComponentPartOfCurrentMenu(menu6));
307: assertTrue(manager.isComponentPartOfCurrentMenu(menu4));
308: assertTrue(manager.isComponentPartOfCurrentMenu(menu5));
309: assertTrue(manager.isComponentPartOfCurrentMenu(menu7));
310: }
311:
312: /*
313: * Test method for 'javax.swing.MenuSelectionManager.processKeyEvent(KeyEvent)'
314: */
315: public void testProcessKeyEvent() {
316: // TODO implement
317: // ConcreteChangeListener listener1 = new ConcreteChangeListener();
318: // Component c = new ConcreteMenuElement("container", new JMenu());
319: // ConcreteMenuElement element1 = new ConcreteMenuElement("elem1", c);
320: // ConcreteMenuElement element2 = new ConcreteMenuElement("elem2", c);
321: // MenuElement[] path1 = new MenuElement[] {element1};
322: // MenuElement[] path2 = new MenuElement[] {element1, element2};
323: // manager.addChangeListener(listener1);
324: //
325: // itemitem.setMnemonic(KeyEvent.VK_A);
326: // itemitem.addActionListener(new ActionListener() {
327: // public void actionPerformed(ActionEvent e) {
328: // System.out.println("Item");
329: // }
330: // });
331: // KeyEvent event = new KeyEvent(itemitem, KeyEvent.KEY_PRESSED, EventQueue.getMostRecentEventTime(), 0, KeyEvent.VK_A);
332: // manager.processKeyEvent(event);
333: // manager.setSelectedPath(new MenuElement[] {element1, element2});
334: // manager.processKeyEvent(event);
335: }
336:
337: /*
338: * Test method for 'javax.swing.MenuSelectionManager.processMouseEvent(MouseEvent)'
339: */
340: public void testProcessMouseEvent() {
341: // JMenuItem menuItem = new JMenuItem();
342: // MouseEvent event1 = new MouseEvent(menuItem, MouseEvent.MOUSE_DRAGGED,
343: // 0, 0, 10, 10, 1, true);
344: // MouseEvent event2 = new MouseEvent(menuItem, MouseEvent.MOUSE_EXITED,
345: // 0, 0, 10, 10, 1, true);
346: // MouseEvent event3 = new MouseEvent(new JButton(), MouseEvent.MOUSE_ENTERED,
347: // 0, 0, 10, 10, 1, true);
348: // MouseEvent event4 = new MouseEvent(menuItem, MouseEvent.MOUSE_RELEASED,
349: // 0, MouseEvent.BUTTON1, 10, 10, 1, false);
350: // ConcreteMenuDragMouseListener listener1 = new ConcreteMenuDragMouseListener();
351: // menuItem.addMenuDragMouseListener(listener1);
352: //
353: // manager.setSelectedPath(new MenuElement[] {menuItem});
354: // manager.processMouseEvent(event1);
355: // assertEquals("exited", listener1.event);
356: //
357: // manager.processMouseEvent(event1);
358: // assertEquals("dragged", listener1.event);
359: //
360: // manager.processMouseEvent(event3);
361: // assertEquals("entered", listener1.event);
362: // listener1.event = null;
363: //
364: // manager.processMouseEvent(event4);
365: // assertNull(listener1.event);
366: //
367: // manager.processMouseEvent(event1);
368: // assertEquals("dragged", listener1.event);
369: // listener1.event = null;
370: //
371: // manager.processMouseEvent(event4);
372: // assertEquals("released", listener1.event);
373:
374: try {
375: MenuSelectionManager m = new MenuSelectionManager();
376: m.processMouseEvent(null);
377: fail("NPE should be thrown");
378: } catch (NullPointerException npe) {
379: // PASSED
380: }
381: }
382:
383: /*
384: * Test method for 'javax.swing.MenuSelectionManager.setSelectedPath(MenuElement[])'
385: */
386: public void testSetGetClearSelectedPath() {
387: ConcreteChangeListener listener1 = new ConcreteChangeListener();
388: MenuElement[] path1 = new MenuElement[] { new JMenu(),
389: new JMenu(), new JMenuItem() };
390: MenuElement[] path2 = new MenuElement[] { new JMenu(),
391: new JMenu(), new JCheckBoxMenuItem() };
392: manager.addChangeListener(listener1);
393: manager.setSelectedPath(path1);
394: assertNotSame(path1, manager.getSelectedPath());
395: assertEquals(path1.length, manager.getSelectedPath().length);
396: assertSame(path1[0], manager.getSelectedPath()[0]);
397: assertSame(path1[1], manager.getSelectedPath()[1]);
398: assertSame(path1[2], manager.getSelectedPath()[2]);
399: assertNotNull(listener1.eventHappened);
400: assertSame(manager, listener1.eventHappened.getSource());
401: listener1.eventHappened = null;
402: assertNotSame(manager.getSelectedPath(), manager
403: .getSelectedPath());
404: manager.clearSelectedPath();
405: assertEquals(0, manager.getSelectedPath().length);
406: assertNotNull(listener1.eventHappened);
407: assertSame(manager, listener1.eventHappened.getSource());
408: manager.setSelectedPath(path2);
409: assertNotSame(path2, manager.getSelectedPath());
410: assertEquals(path2.length, manager.getSelectedPath().length);
411: assertSame(path2[0], manager.getSelectedPath()[0]);
412: assertSame(path2[1], manager.getSelectedPath()[1]);
413: assertSame(path2[2], manager.getSelectedPath()[2]);
414: assertNotNull(listener1.eventHappened);
415: assertSame(manager, listener1.eventHappened.getSource());
416: listener1.eventHappened = null;
417: manager.setSelectedPath(path2);
418: assertEquals(path2.length, manager.getSelectedPath().length);
419: if (!isHarmony()) {
420: assertNotNull(listener1.eventHappened);
421: } else {
422: assertNull(listener1.eventHappened);
423: }
424: listener1.eventHappened = null;
425: manager.setSelectedPath(null);
426: assertEquals(0, manager.getSelectedPath().length);
427: assertNotNull(listener1.eventHappened);
428: assertSame(manager, listener1.eventHappened.getSource());
429: }
430:
431: public void testSetSelectedPath() {
432: ConcreteChangeListener listener1 = new ConcreteChangeListener();
433: ConcreteMenuElement element1 = new ConcreteMenuElement("elem1",
434: null);
435: ConcreteMenuElement element2 = new ConcreteMenuElement("elem2",
436: null);
437: MenuElement[] path1 = new MenuElement[] { element1 };
438: MenuElement[] path2 = new MenuElement[] { element1, element2 };
439: manager.addChangeListener(listener1);
440: manager.setSelectedPath(path1);
441: assertTrue(element1.selected);
442: assertFalse(element2.selected);
443: manager.setSelectedPath(path2);
444: assertTrue(element1.selected);
445: assertTrue(element2.selected);
446: manager.setSelectedPath(path1);
447: assertTrue(element1.selected);
448: assertFalse(element2.selected);
449: manager.clearSelectedPath();
450: assertFalse(element1.selected);
451: assertFalse(element2.selected);
452: manager.setSelectedPath(path2);
453: assertTrue(element1.selected);
454: assertTrue(element2.selected);
455: manager.clearSelectedPath();
456: assertFalse(element1.selected);
457: assertFalse(element2.selected);
458: }
459: }
|