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 Anton Avtamonov
019: * @version $Revision$
020: */package javax.swing.plaf.basic;
021:
022: import java.awt.Insets;
023: import java.awt.Point;
024: import java.awt.event.MouseEvent;
025: import javax.swing.JComboBox;
026: import javax.swing.JFrame;
027: import javax.swing.JList;
028: import javax.swing.JScrollPane;
029: import javax.swing.ListSelectionModel;
030: import javax.swing.SwingTestCase;
031: import javax.swing.border.LineBorder;
032: import javax.swing.event.PopupMenuEvent;
033: import javax.swing.event.PopupMenuListener;
034:
035: public class BasicComboPopupTest extends SwingTestCase {
036: private BasicComboPopup popup;
037:
038: private JComboBox comboBox;
039:
040: public BasicComboPopupTest(final String name) {
041: super (name);
042: }
043:
044: @Override
045: protected void setUp() throws Exception {
046: comboBox = new JComboBox(new Object[] { "1", "2", "3" });
047: popup = new BasicComboPopup(comboBox);
048: ((BasicComboBoxUI) comboBox.getUI()).popup = popup;
049: }
050:
051: @Override
052: protected void tearDown() throws Exception {
053: comboBox = null;
054: popup = null;
055: }
056:
057: public void testBasicComboPopup() throws Exception {
058: assertNotNull(popup.comboBox);
059: assertNotNull(popup.list);
060: assertNotNull(popup.scroller);
061: assertEquals(3, popup.list.getModel().getSize());
062: popup = new BasicComboPopup(new JComboBox());
063: assertNotNull(popup.list);
064: assertEquals(0, popup.list.getModel().getSize());
065: assertTrue(popup.comboBox.getModel() == popup.list.getModel());
066: assertEquals(popup.getComponent(), popup);
067: assertEquals(1, popup.getComponentCount());
068: assertEquals(popup.scroller, popup.getComponent(0));
069: assertEquals(ListSelectionModel.SINGLE_SELECTION, popup.list
070: .getSelectionMode());
071: assertTrue(popup.getBorder().getClass() == LineBorder.class);
072: assertEquals(new Insets(1, 1, 1, 1), popup.getInsets());
073: assertFalse(popup.isAutoScrolling);
074: }
075:
076: public void testShowHide() throws Exception {
077: createVisibleComboBox();
078: popup.show();
079: assertTrue(popup.isShowing());
080: popup.show();
081: assertTrue(popup.isShowing());
082: popup.hide();
083: assertFalse(popup.isShowing());
084: popup.hide();
085: assertFalse(popup.isShowing());
086: popup.show();
087: assertTrue(popup.isShowing());
088: }
089:
090: public void testGetList() throws Exception {
091: assertNotNull(popup.getList());
092: assertEquals(popup.list, popup.getList());
093: JList newList = new JList();
094: popup.list = newList;
095: assertEquals(newList, popup.getList());
096: }
097:
098: public void testGetMouseListener() throws Exception {
099: assertNull(popup.mouseListener);
100: assertEquals(popup.getMouseListener(), popup.mouseListener);
101: if (isHarmony()) {
102: assertTrue(popup.getMouseListener().getClass() == BasicComboPopup.InvocationMouseHandler.class);
103: }
104: }
105:
106: public void testGetMouseMotionListener() throws Exception {
107: assertNull(popup.mouseMotionListener);
108: assertEquals(popup.getMouseMotionListener(),
109: popup.mouseMotionListener);
110: if (isHarmony()) {
111: assertTrue(popup.getMouseMotionListener().getClass() == BasicComboPopup.InvocationMouseMotionHandler.class);
112: }
113: }
114:
115: public void testGetKeyListener() throws Exception {
116: assertNull(popup.keyListener);
117: assertNull(popup.getKeyListener());
118: }
119:
120: public void testUninstallingUI() throws Exception {
121: comboBox = new JComboBox();
122: int mouseListenerCount = comboBox.getMouseListeners().length;
123: int mouseMotionListenerCount = comboBox
124: .getMouseMotionListeners().length;
125: int itemListenerCount = comboBox.getItemListeners().length;
126: int propertyChangeListenerCount = comboBox
127: .getPropertyChangeListeners().length;
128: int keyListenerCount = comboBox.getKeyListeners().length;
129: popup = new BasicComboPopup(comboBox);
130: assertEquals(mouseListenerCount,
131: comboBox.getMouseListeners().length);
132: assertEquals(mouseMotionListenerCount, comboBox
133: .getMouseMotionListeners().length);
134: assertEquals(keyListenerCount,
135: comboBox.getKeyListeners().length);
136: assertEquals(itemListenerCount + 1,
137: comboBox.getItemListeners().length);
138: assertEquals(propertyChangeListenerCount + 1, comboBox
139: .getPropertyChangeListeners().length);
140: popup.uninstallingUI();
141: assertEquals(mouseListenerCount,
142: comboBox.getMouseListeners().length);
143: assertEquals(mouseMotionListenerCount, comboBox
144: .getMouseMotionListeners().length);
145: assertEquals(keyListenerCount,
146: comboBox.getKeyListeners().length);
147: assertEquals(itemListenerCount,
148: comboBox.getItemListeners().length);
149: assertEquals(propertyChangeListenerCount, comboBox
150: .getPropertyChangeListeners().length);
151: }
152:
153: public void testFirePopupMenuWillBecomeVisibleInvisibleCanceled()
154: throws Exception {
155: PopupMenuController comboController = new PopupMenuController();
156: PopupMenuController popupController = new PopupMenuController();
157: popup.addPopupMenuListener(popupController);
158: popup.comboBox.addPopupMenuListener(comboController);
159: popup.firePopupMenuCanceled();
160: assertNotNull(popupController.getEvent());
161: assertNotNull(comboController.getEvent());
162: assertEquals(PopupMenuController.MENU_CANCELED, popupController
163: .getEventType());
164: assertEquals(PopupMenuController.MENU_CANCELED, comboController
165: .getEventType());
166: popupController.reset();
167: comboController.reset();
168: popup.firePopupMenuWillBecomeInvisible();
169: assertNotNull(popupController.getEvent());
170: assertNotNull(comboController.getEvent());
171: assertEquals(PopupMenuController.MENU_INVISIBLE,
172: popupController.getEventType());
173: assertEquals(PopupMenuController.MENU_INVISIBLE,
174: comboController.getEventType());
175: popupController.reset();
176: comboController.reset();
177: popup.firePopupMenuWillBecomeVisible();
178: assertNotNull(popupController.getEvent());
179: assertNotNull(comboController.getEvent());
180: assertEquals(PopupMenuController.MENU_VISIBLE, popupController
181: .getEventType());
182: assertEquals(PopupMenuController.MENU_VISIBLE, comboController
183: .getEventType());
184: }
185:
186: public void testCreateMouseListener() throws Exception {
187: if (isHarmony()) {
188: assertTrue(popup.createMouseListener().getClass() == BasicComboPopup.InvocationMouseHandler.class);
189: assertFalse(popup.createMouseListener() == popup
190: .createMouseListener());
191: }
192: }
193:
194: public void testCreateMouseMotionListener() throws Exception {
195: if (isHarmony()) {
196: assertTrue(popup.createMouseMotionListener().getClass() == BasicComboPopup.InvocationMouseMotionHandler.class);
197: assertFalse(popup.createMouseMotionListener() == popup
198: .createMouseMotionListener());
199: }
200: }
201:
202: public void testCreateKeyListener() throws Exception {
203: assertNull(popup.createKeyListener());
204: }
205:
206: public void testCreateListSelectionListener() throws Exception {
207: assertNull(popup.createListSelectionListener());
208: assertNull(popup.listSelectionListener);
209: }
210:
211: public void testCreateListDataListener() throws Exception {
212: assertNull(popup.createListDataListener());
213: assertNull(popup.listDataListener);
214: }
215:
216: public void testCreateListMouseListener() throws Exception {
217: if (isHarmony()) {
218: assertTrue(popup.listMouseListener.getClass() == BasicComboPopup.ListMouseHandler.class);
219: assertTrue(popup.createListMouseListener().getClass() == BasicComboPopup.ListMouseHandler.class);
220: assertFalse(popup.createListMouseListener() == popup
221: .createListMouseListener());
222: }
223: }
224:
225: public void testCreateListMouseMotionListener() throws Exception {
226: if (isHarmony()) {
227: assertTrue(popup.listMouseMotionListener.getClass() == BasicComboPopup.ListMouseMotionHandler.class);
228: assertTrue(popup.createListMouseMotionListener().getClass() == BasicComboPopup.ListMouseMotionHandler.class);
229: assertFalse(popup.createListMouseMotionListener() == popup
230: .createListMouseMotionListener());
231: }
232: }
233:
234: public void testCreatePropertyChangeListener() throws Exception {
235: if (isHarmony()) {
236: assertTrue(popup.propertyChangeListener.getClass() == BasicComboPopup.PropertyChangeHandler.class);
237: assertTrue(popup.createPropertyChangeListener().getClass() == BasicComboPopup.PropertyChangeHandler.class);
238: assertFalse(popup.createPropertyChangeListener() == popup
239: .createPropertyChangeListener());
240: }
241: }
242:
243: public void testCreateItemListener() throws Exception {
244: if (isHarmony()) {
245: assertTrue(popup.itemListener.getClass() == BasicComboPopup.ItemHandler.class);
246: assertTrue(popup.createItemListener().getClass() == BasicComboPopup.ItemHandler.class);
247: assertFalse(popup.createItemListener() == popup
248: .createItemListener());
249: }
250: }
251:
252: public void testCreateList() throws Exception {
253: assertNotSame(popup.createList(), popup.createList());
254: assertNotSame(popup.createList(), popup.list);
255: }
256:
257: public void testConfigureList() throws Exception {
258: popup.list = new JList();
259: int mouseListenerCount = popup.list.getMouseListeners().length;
260: int mouseMotionListenerCount = popup.list
261: .getMouseMotionListeners().length;
262: popup.configureList();
263: assertEquals(mouseListenerCount + 1, popup.list
264: .getMouseListeners().length);
265: assertEquals(mouseMotionListenerCount + 1, popup.list
266: .getMouseMotionListeners().length);
267: assertEquals(ListSelectionModel.SINGLE_SELECTION, popup.list
268: .getSelectionMode());
269: }
270:
271: public void testCreateScroller() throws Exception {
272: assertNotNull(popup.scroller);
273: assertNotSame(popup.createScroller(), popup.createScroller());
274: assertNotSame(popup.createScroller(), popup.scroller);
275: }
276:
277: public void testConfigureScroller() throws Exception {
278: popup.scroller = new JScrollPane();
279: popup.configureScroller();
280: }
281:
282: public void testInstallUninstallComboBoxModelListeners()
283: throws Exception {
284: popup.installComboBoxModelListeners(null);
285: popup.uninstallComboBoxModelListeners(null);
286: }
287:
288: public void testInstallUninstallComboBoxListeners()
289: throws Exception {
290: int mouseListenerCount = comboBox.getMouseListeners().length;
291: int mouseMotionListenerCount = comboBox
292: .getMouseMotionListeners().length;
293: int itemListenerCount = comboBox.getItemListeners().length;
294: int propertyChangeListenerCount = comboBox
295: .getPropertyChangeListeners().length;
296: int keyListenerCount = comboBox.getKeyListeners().length;
297: popup.installComboBoxListeners();
298: assertEquals(mouseListenerCount,
299: comboBox.getMouseListeners().length);
300: assertEquals(mouseMotionListenerCount, comboBox
301: .getMouseMotionListeners().length);
302: assertEquals(keyListenerCount,
303: comboBox.getKeyListeners().length);
304: assertEquals(itemListenerCount + 1,
305: comboBox.getItemListeners().length);
306: assertEquals(propertyChangeListenerCount + 1, comboBox
307: .getPropertyChangeListeners().length);
308: }
309:
310: public void testInstallUninstallKeyboardActions() throws Exception {
311: int count = popup.comboBox.getActionMap().allKeys().length;
312: popup.uninstallKeyboardActions();
313: assertEquals(count,
314: popup.comboBox.getActionMap().allKeys().length);
315: popup.installKeyboardActions();
316: assertEquals(count,
317: popup.comboBox.getActionMap().allKeys().length);
318: }
319:
320: public void testInstallListListeners() throws Exception {
321: int mouseListenerCount = popup.list.getMouseListeners().length;
322: int mouseMotionListenerCount = popup.list
323: .getMouseMotionListeners().length;
324: int selectionListenerCount = popup.list
325: .getListSelectionListeners().length;
326: popup.installListListeners();
327: assertEquals(mouseListenerCount + 1, popup.list
328: .getMouseListeners().length);
329: assertEquals(mouseMotionListenerCount + 1, popup.list
330: .getMouseMotionListeners().length);
331: assertEquals(selectionListenerCount, popup.list
332: .getListSelectionListeners().length);
333: }
334:
335: public void testIsFocusTraversable() throws Exception {
336: assertFalse(popup.isFocusTraversable());
337: }
338:
339: public void testStartStopAutoscrolloing() throws Exception {
340: assertNull(popup.autoscrollTimer);
341: assertFalse(popup.isAutoScrolling);
342: popup.startAutoScrolling(BasicComboPopup.SCROLL_UP);
343: assertNotNull(popup.autoscrollTimer);
344: assertTrue(popup.isAutoScrolling);
345: assertEquals(BasicComboPopup.SCROLL_UP, popup.scrollDirection);
346: popup.startAutoScrolling(BasicComboPopup.SCROLL_DOWN);
347: assertNotNull(popup.autoscrollTimer);
348: assertTrue(popup.isAutoScrolling);
349: assertEquals(BasicComboPopup.SCROLL_DOWN, popup.scrollDirection);
350: popup.stopAutoScrolling();
351: assertFalse(popup.isAutoScrolling);
352: }
353:
354: public void testAutoScrollUpDown() throws Exception {
355: if (isHarmony()) {
356: createVisibleComboBox();
357: popup.show();
358: popup.list.setSelectedIndex(2);
359: popup.autoScrollUp();
360: assertEquals(0, popup.list.getSelectedIndex());
361: popup.autoScrollUp();
362: assertEquals(0, popup.list.getSelectedIndex());
363: popup.autoScrollDown();
364: assertEquals(2, popup.list.getSelectedIndex());
365: popup.autoScrollDown();
366: assertEquals(2, popup.list.getSelectedIndex());
367: popup.autoScrollUp();
368: assertEquals(0, popup.list.getSelectedIndex());
369: }
370: }
371:
372: public void testGetAccessibleContext() throws Exception {
373: assertNotNull(popup.getAccessibleContext());
374: // Is not clear how it should be
375: // assertEquals(popup.comboBox, popup.getAccessibleContext().getAccessibleParent());
376: }
377:
378: public void testTogglePopup() throws Exception {
379: createVisibleComboBox();
380: assertFalse(popup.isShowing());
381: popup.togglePopup();
382: assertTrue(popup.isShowing());
383: popup.togglePopup();
384: assertFalse(popup.isShowing());
385: }
386:
387: public void testConvertMouseEvent() throws Exception {
388: MouseEvent original = createMouseEvent(0, 0);
389: assertNotSame(original, popup.convertMouseEvent(original));
390: comboBox.setLocation(0, 0);
391: assertEquals(new Point(10, 20), popup.convertMouseEvent(
392: createMouseEvent(10, 20)).getPoint());
393: assertEquals(new Point(-10, -20), popup.convertMouseEvent(
394: createMouseEvent(-10, -20)).getPoint());
395: comboBox.setLocation(100, 200);
396: assertEquals(new Point(110, 220), popup.convertMouseEvent(
397: createMouseEvent(10, 20)).getPoint());
398: assertEquals(new Point(90, 180), popup.convertMouseEvent(
399: createMouseEvent(-10, -20)).getPoint());
400: }
401:
402: public void testGetPopupHeightForRowCount() throws Exception {
403: popup = new BasicComboPopup(new JComboBox());
404: assertEquals(100, popup.getPopupHeightForRowCount(0));
405: assertEquals(100, popup.getPopupHeightForRowCount(1));
406: assertEquals(100, popup.getPopupHeightForRowCount(100));
407: popup = new BasicComboPopup(new JComboBox(new Object[] { "1" }));
408: popup.list.setFont(comboBox.getFont().deriveFont(40f));
409: int oneElemHeight = popup.getPopupHeightForRowCount(1);
410: assertTrue(oneElemHeight > 0 && oneElemHeight != 100);
411: popup = new BasicComboPopup(new JComboBox(new Object[] { "1",
412: "2", "3" }));
413: popup.list.setFont(comboBox.getFont().deriveFont(40f));
414: assertEquals(oneElemHeight, popup.getPopupHeightForRowCount(1));
415: assertEquals(2 * oneElemHeight, popup
416: .getPopupHeightForRowCount(2));
417: assertEquals(3 * oneElemHeight, popup
418: .getPopupHeightForRowCount(3));
419: assertEquals(3 * oneElemHeight, popup
420: .getPopupHeightForRowCount(100));
421: assertEquals(100, popup.getPopupHeightForRowCount(0));
422: }
423:
424: private MouseEvent createMouseEvent(final int x, final int y) {
425: return new MouseEvent(comboBox, MouseEvent.MOUSE_CLICKED, 0, 0,
426: x, y, 0, false);
427: }
428:
429: @SuppressWarnings("deprecation")
430: private void createVisibleComboBox() {
431: JFrame frame = new JFrame();
432: frame.getContentPane().add(comboBox);
433: frame.pack();
434: frame.show();
435: }
436:
437: private class PopupMenuController implements PopupMenuListener {
438: public static final int MENU_CANCELED = 1;
439:
440: public static final int MENU_VISIBLE = 2;
441:
442: public static final int MENU_INVISIBLE = 3;
443:
444: private PopupMenuEvent event;
445:
446: private int eventType;
447:
448: public void popupMenuCanceled(final PopupMenuEvent e) {
449: event = e;
450: eventType = MENU_CANCELED;
451: }
452:
453: public void popupMenuWillBecomeInvisible(final PopupMenuEvent e) {
454: event = e;
455: eventType = MENU_INVISIBLE;
456: }
457:
458: public void popupMenuWillBecomeVisible(final PopupMenuEvent e) {
459: event = e;
460: eventType = MENU_VISIBLE;
461: }
462:
463: public PopupMenuEvent getEvent() {
464: return event;
465: }
466:
467: public int getEventType() {
468: return eventType;
469: }
470:
471: public void reset() {
472: event = null;
473: eventType = 0;
474: }
475: }
476: }
|