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.Color;
023: import java.awt.Component;
024: import java.awt.Dimension;
025: import java.awt.Font;
026: import java.awt.Point;
027: import java.awt.Rectangle;
028: import java.util.Arrays;
029: import javax.swing.BorderFactory;
030: import javax.swing.DefaultListCellRenderer;
031: import javax.swing.JComponent;
032: import javax.swing.JFileChooser;
033: import javax.swing.JList;
034: import javax.swing.SwingTestCase;
035: import javax.swing.SwingUtilities;
036: import javax.swing.UIManager;
037: import javax.swing.border.Border;
038: import javax.swing.plaf.BorderUIResource;
039: import javax.swing.plaf.ColorUIResource;
040: import javax.swing.plaf.FontUIResource;
041:
042: public class BasicListUITest extends SwingTestCase {
043: private JList list;
044:
045: private BasicListUI ui;
046:
047: private Object defaultBackground;
048:
049: private Object defaultForeground;
050:
051: private Object defaultFont;
052:
053: private Object defaultRenderer;
054:
055: private Object defaultBorder;
056:
057: public BasicListUITest(final String name) {
058: super (name);
059: }
060:
061: @Override
062: protected void setUp() throws Exception {
063: super .setUp();
064: ui = new BasicListUI();
065: list = new JList();
066: defaultBackground = UIManager.get("List.background");
067: defaultForeground = UIManager.get("List.foreground");
068: defaultFont = UIManager.get("List.font");
069: defaultRenderer = UIManager.get("List.cellRenderer");
070: defaultBorder = UIManager.get("List.border");
071: }
072:
073: @Override
074: protected void tearDown() throws Exception {
075: ui = null;
076: list = null;
077: UIManager.put("List.background", defaultBackground);
078: UIManager.put("List.foreground", defaultForeground);
079: UIManager.put("List.font", defaultFont);
080: UIManager.put("List.cellRenderer", defaultRenderer);
081: UIManager.put("List.border", defaultBorder);
082: super .tearDown();
083: }
084:
085: public void testBasicListUI() throws Exception {
086: assertEquals(-1, ui.cellHeight);
087: assertEquals(-1, ui.cellWidth);
088: assertNull(ui.cellHeights);
089: assertNull(ui.rendererPane);
090: assertNull(ui.list);
091: assertEquals(1, ui.updateLayoutStateNeeded);
092: }
093:
094: public void testBasicListUI_FocusListener() throws Exception {
095: assertNull(ui.focusListener);
096: int listenersCount = list.getFocusListeners().length;
097: ui.installUI(list);
098: if (isHarmony()) {
099: assertTrue(ui.focusListener instanceof BasicListUI.FocusHandler);
100: }
101: assertTrue(Arrays.asList(list.getFocusListeners()).contains(
102: ui.focusListener));
103: assertEquals(listenersCount + 1,
104: list.getFocusListeners().length);
105: ui.uninstallUI(list);
106: assertNull(ui.focusListener);
107: assertEquals(listenersCount, list.getFocusListeners().length);
108: }
109:
110: public void testBasicListUI_ListDataHandler() throws Exception {
111: }
112:
113: public void testConvertRowToY() throws Exception {
114: ui.installUI(list);
115: assertEquals(-1, ui.convertRowToY(0));
116: assertEquals(-1, ui.convertRowToY(-1));
117: list.setListData(new Object[] { "a", "b" });
118: ui.installUI(list);
119: list.setFixedCellHeight(20);
120: ui.maybeUpdateLayoutState();
121: assertEquals(-1, ui.convertRowToY(-1));
122: assertEquals(0, ui.convertRowToY(0));
123: assertEquals(20, ui.convertRowToY(1));
124: }
125:
126: public void testConvertYToRow() throws Exception {
127: ui.installUI(list);
128: assertEquals(-1, ui.convertYToRow(0));
129: list.setListData(new Object[] { "a", "b" });
130: list.setFixedCellHeight(10);
131: ui.maybeUpdateLayoutState();
132: assertEquals(0, ui.convertYToRow(0));
133: assertEquals(0, ui.convertYToRow(5));
134: assertEquals(1, ui.convertYToRow(11));
135: assertEquals(1, ui.convertYToRow(19));
136: }
137:
138: public void testCreateFocusListener() throws Exception {
139: if (isHarmony()) {
140: assertTrue(ui.createFocusListener() instanceof BasicListUI.FocusHandler);
141: }
142: }
143:
144: public void testCreateListDataListener() throws Exception {
145: if (isHarmony()) {
146: assertTrue(ui.createListDataListener() instanceof BasicListUI.ListDataHandler);
147: }
148: }
149:
150: public void testCreateListSelectionListener() throws Exception {
151: if (isHarmony()) {
152: assertTrue(ui.createListSelectionListener() instanceof BasicListUI.ListSelectionHandler);
153: }
154: }
155:
156: public void testCreateMouseInputListener() throws Exception {
157: if (isHarmony()) {
158: assertTrue(ui.createMouseInputListener() instanceof BasicListUI.MouseInputHandler);
159: }
160: }
161:
162: public void testCreatePropertyChangeListener() throws Exception {
163: if (isHarmony()) {
164: assertTrue(ui.createPropertyChangeListener() instanceof BasicListUI.PropertyChangeHandler);
165: }
166: }
167:
168: public void testCreateUI() throws Exception {
169: BasicListUI newUI1 = (BasicListUI) BasicListUI.createUI(list);
170: BasicListUI newUI2 = (BasicListUI) BasicListUI.createUI(list);
171: assertNotSame(newUI1, newUI2);
172: }
173:
174: public void testGetCellBounds() throws Exception {
175: ui.installUI(list);
176: assertNull(ui.getCellBounds(list, 0, 0));
177: assertNull(ui.getCellBounds(list, -1, -1));
178: list.setListData(new Object[] { "a", "b" });
179: list.setFixedCellHeight(10);
180: list.setFixedCellWidth(20);
181: list.setSize(100, 100);
182: assertEquals(new Rectangle(0, 0, 100, 10), ui.getCellBounds(
183: list, 0, 0));
184: assertEquals(new Rectangle(0, 10, 100, 10), ui.getCellBounds(
185: list, 1, 1));
186: assertEquals(new Rectangle(0, 0, 100, 20), ui.getCellBounds(
187: list, 0, 1));
188: list.setLayoutOrientation(JList.VERTICAL_WRAP);
189: assertEquals(new Rectangle(0, 0, 20, 10), ui.getCellBounds(
190: list, 0, 0));
191: assertEquals(new Rectangle(0, 10, 20, 10), ui.getCellBounds(
192: list, 1, 1));
193: assertEquals(new Rectangle(0, 0, 20, 20), ui.getCellBounds(
194: list, 1, 0));
195: list.setBorder(BorderFactory.createEmptyBorder(10, 5, 20, 7));
196: assertEquals(new Rectangle(5, 10, 20, 10), ui.getCellBounds(
197: list, 0, 0));
198: list.setLayoutOrientation(JList.VERTICAL);
199: assertEquals(new Rectangle(5, 10, 100 - 5 - 7, 20), ui
200: .getCellBounds(list, 0, 1));
201: }
202:
203: public void testGetCellBounds_Null() throws Exception {
204: testExceptionalCase(new NullPointerCase() {
205: @Override
206: public void exceptionalAction() throws Exception {
207: ui.getCellBounds(null, -1, 9);
208: }
209: });
210: testExceptionalCase(new NullPointerCase() {
211: @Override
212: public void exceptionalAction() throws Exception {
213: ui.getCellBounds(null, 1, 9);
214: }
215: });
216: }
217:
218: public void testGetMaximumSize() throws Exception {
219: ui.installUI(list);
220: assertEquals(ui.getPreferredSize(list), ui.getMaximumSize(list));
221: }
222:
223: public void testGetMinimumSize() throws Exception {
224: ui.installUI(list);
225: assertEquals(ui.getPreferredSize(list), ui.getMinimumSize(list));
226: }
227:
228: public void testGetPreferredSize() throws Exception {
229: ui.installUI(list);
230: list.setSize(100, 100);
231: assertEquals(new Dimension(0, 0), ui.getPreferredSize(list));
232: list.setListData(new Object[] { "a", "bbb" });
233: Component renderer = new DefaultListCellRenderer()
234: .getListCellRendererComponent(list, "bbb", 1, false,
235: false);
236: assertEquals(new Dimension(renderer.getPreferredSize().width,
237: 2 * renderer.getPreferredSize().height), ui
238: .getPreferredSize(list));
239: list.setFixedCellHeight(20);
240: list.setFixedCellWidth(30);
241: assertEquals(new Dimension(30, 40), ui.getPreferredSize(list));
242: list.setVisibleRowCount(1);
243: assertEquals(new Dimension(30, 40), ui.getPreferredSize(list));
244: list.setLayoutOrientation(JList.VERTICAL_WRAP);
245: assertEquals(new Dimension(60, 20), ui.getPreferredSize(list));
246: }
247:
248: public void testGetRowHeight() throws Exception {
249: assertEquals(-1, ui.getRowHeight(-1));
250: ui.installUI(list);
251: assertEquals(-1, ui.getRowHeight(0));
252: list.setListData(new Object[] { "a", "bbb" });
253: ui.maybeUpdateLayoutState();
254: Component renderer = new DefaultListCellRenderer()
255: .getListCellRendererComponent(list, "bbb", 1, false,
256: false);
257: assertEquals(renderer.getPreferredSize().height, ui
258: .getRowHeight(0));
259: assertEquals(renderer.getPreferredSize().height, ui
260: .getRowHeight(1));
261: assertEquals(-1, ui.getRowHeight(2));
262: list.setFixedCellHeight(30);
263: ui.maybeUpdateLayoutState();
264: assertEquals(30, ui.getRowHeight(0));
265: }
266:
267: public void testIndexToLocation() throws Exception {
268: ui.installUI(list);
269: assertNull(ui.indexToLocation(list, -1));
270: assertNull(ui.indexToLocation(list, 0));
271: list.setListData(new Object[] { "a", "bbb" });
272: Component renderer = new DefaultListCellRenderer()
273: .getListCellRendererComponent(list, "bbb", 1, false,
274: false);
275: assertEquals(new Point(0, 0), ui.indexToLocation(list, 0));
276: assertEquals(new Point(0, renderer.getPreferredSize().height),
277: ui.indexToLocation(list, 1));
278: list.setVisibleRowCount(1);
279: list.setLayoutOrientation(JList.VERTICAL_WRAP);
280: assertEquals(new Point(0, 0), ui.indexToLocation(list, 0));
281: assertEquals(new Point(renderer.getPreferredSize().width, 0),
282: ui.indexToLocation(list, 1));
283: assertNull(ui.indexToLocation(list, 2));
284: }
285:
286: public void testInstallDefaults() throws Exception {
287: UIManager.getDefaults().put("List.background",
288: new ColorUIResource(Color.red));
289: UIManager.getDefaults().put("List.foreground",
290: new ColorUIResource(Color.yellow));
291: Font font = new FontUIResource(list.getFont().deriveFont(100f));
292: UIManager.getDefaults().put("List.font", font);
293: DefaultListCellRenderer renderer = new DefaultListCellRenderer.UIResource();
294: UIManager.getDefaults().put("List.cellRenderer", renderer);
295: Border border = new BorderUIResource(BorderFactory
296: .createEmptyBorder(0, 0, 0, 0));
297: UIManager.getDefaults().put("List.border", border);
298: list.setUI(ui);
299: ui.installDefaults();
300: assertEquals(Color.red, list.getBackground());
301: assertEquals(Color.yellow, list.getForeground());
302: assertEquals(font, list.getFont());
303: assertEquals(renderer, list.getCellRenderer());
304: assertEquals(border, list.getBorder());
305: }
306:
307: public void testUninstallDefaults() throws Exception {
308: UIManager.getDefaults().put("List.background",
309: new ColorUIResource(Color.red));
310: UIManager.getDefaults().put("List.foreground",
311: new ColorUIResource(Color.yellow));
312: Font font = new FontUIResource(list.getFont().deriveFont(100f));
313: UIManager.getDefaults().put("List.font", font);
314: DefaultListCellRenderer renderer = new DefaultListCellRenderer.UIResource();
315: UIManager.getDefaults().put("List.cellRenderer", renderer);
316: Border border = new BorderUIResource(BorderFactory
317: .createEmptyBorder(0, 0, 0, 0));
318: UIManager.getDefaults().put("List.border", border);
319: list.setUI(ui);
320: ui.installDefaults();
321: ui.uninstallDefaults();
322: assertNull(list.getBackground());
323: assertNull(list.getForeground());
324: assertNull(list.getFont());
325: assertNull(list.getCellRenderer());
326: assertNull(list.getBorder());
327: UIManager.getDefaults().put("List.background", Color.red);
328: list.setUI(ui);
329: ui.uninstallDefaults();
330: if (isHarmony()) {
331: assertNull(list.getBackground());
332: }
333: assertNull(list.getForeground());
334: assertNull(list.getFont());
335: assertNull(list.getCellRenderer());
336: assertNull(list.getBorder());
337: }
338:
339: public void testInstallKeyboardActions() throws Exception {
340: list.setUI(ui);
341: assertNotNull(SwingUtilities.getUIInputMap(list,
342: JComponent.WHEN_FOCUSED));
343: }
344:
345: public void testUninstallKeyboardActions() throws Exception {
346: list.setUI(ui);
347: ui.uninstallKeyboardActions();
348: assertNull(SwingUtilities.getUIInputMap(list,
349: JComponent.WHEN_FOCUSED));
350: }
351:
352: public void testInstallListeners() throws Exception {
353: list.setUI(ui);
354: assertNotNull(ui.focusListener);
355: assertTrue(list.getFocusListeners().length > 0);
356: assertNotNull(ui.listDataListener);
357: assertNotNull(ui.mouseInputListener);
358: assertTrue(list.getMouseListeners().length > 0);
359: assertTrue(list.getMouseMotionListeners().length > 0);
360: assertNotNull(ui.propertyChangeListener);
361: assertTrue(list.getPropertyChangeListeners().length > 0);
362: }
363:
364: public void testUninstallListeners() throws Exception {
365: list.setUI(ui);
366: int focusListenersCount = list.getFocusListeners().length;
367: int mouseListenersCount = list.getMouseListeners().length;
368: int mouseMotionListenersCount = list.getMouseMotionListeners().length;
369: int propertyChangeListenersCount = list
370: .getPropertyChangeListeners().length;
371: ui.uninstallListeners();
372: assertNull(ui.focusListener);
373: assertTrue(focusListenersCount > list.getFocusListeners().length);
374: assertNull(ui.listDataListener);
375: assertNull(ui.mouseInputListener);
376: assertTrue(mouseListenersCount > list.getMouseListeners().length);
377: assertTrue(mouseMotionListenersCount > list
378: .getMouseMotionListeners().length);
379: assertNull(ui.propertyChangeListener);
380: assertTrue(propertyChangeListenersCount > list
381: .getPropertyChangeListeners().length);
382: }
383:
384: public void testInstallUI() throws Exception {
385: ui.installUI(list);
386: assertNotNull(list.getBackground());
387: assertNotNull(SwingUtilities.getUIInputMap(list,
388: JComponent.WHEN_FOCUSED));
389: assertNotNull(ui.rendererPane);
390: assertFalse(ui.rendererPane.isVisible());
391: assertEquals(2, list.getComponentCount());
392: }
393:
394: public void testUninstallUI() throws Exception {
395: list.setUI(ui);
396: ui.uninstallUI(list);
397: if (isHarmony()) {
398: assertNull(list.getBackground());
399: }
400: assertNull(SwingUtilities.getUIInputMap(list,
401: JComponent.WHEN_FOCUSED));
402: assertEquals(0, list.getComponentCount());
403: }
404:
405: public void testLocationToIndex() throws Exception {
406: ui.installUI(list);
407: assertEquals(-1, ui.locationToIndex(list, new Point(3, 3)));
408: list.setListData(new Object[] { "aa", "bb" });
409: assertEquals(0, ui.locationToIndex(list, new Point(3, 3)));
410: assertEquals(1, ui.locationToIndex(list, new Point(3, 25)));
411: assertEquals(0, ui.locationToIndex(list, new Point(70, 3)));
412: assertEquals(1, ui.locationToIndex(list, new Point(70, 25)));
413: list.setVisibleRowCount(1);
414: list.setLayoutOrientation(JList.VERTICAL_WRAP);
415: assertEquals(0, ui.locationToIndex(list, new Point(3, 3)));
416: assertEquals(0, ui.locationToIndex(list, new Point(3, 25)));
417: assertEquals(1, ui.locationToIndex(list, new Point(50, 3)));
418: assertEquals(1, ui.locationToIndex(list, new Point(50, 25)));
419:
420: try {
421: BasicListUI localBasicListUI = new BasicListUI();
422: javax.swing.JList localJList = new javax.swing.JList();
423: localBasicListUI.locationToIndex(localJList, null);
424: fail("NPE should be thrown");
425: } catch (NullPointerException npe) {
426: // PASSED
427: }
428: }
429:
430: public void testMaybeUpdateLayoutState() throws Exception {
431: ui.installUI(list);
432: assertTrue(ui.updateLayoutStateNeeded != 0);
433: ui.maybeUpdateLayoutState();
434: assertEquals(0, ui.updateLayoutStateNeeded);
435: }
436:
437: //TODO
438: public void testPaint() throws Exception {
439: }
440:
441: //TODO
442: public void testPaintCell() throws Exception {
443: }
444:
445: public void testUpdateLayoutState() throws Exception {
446: ui.installUI(list);
447: assertEquals(-1, ui.cellHeight);
448: assertEquals(-1, ui.cellWidth);
449: assertNull(ui.cellHeights);
450: list.setSize(100, 100);
451: ui.maybeUpdateLayoutState();
452: assertEquals(-1, ui.cellHeight);
453: assertEquals(-1, ui.cellWidth);
454: assertNotNull(ui.cellHeights);
455: assertEquals(0, ui.cellHeights.length);
456: list.setListData(new Object[] { "aa" });
457: ui.maybeUpdateLayoutState();
458: assertEquals(-1, ui.cellHeight);
459: assertTrue(ui.cellWidth > 0);
460: assertNotNull(ui.cellHeights);
461: assertEquals(1, ui.cellHeights.length);
462: assertTrue(ui.cellHeights[0] > 0);
463: list.setLayoutOrientation(JList.VERTICAL_WRAP);
464: ui.maybeUpdateLayoutState();
465: assertTrue(ui.cellHeight > 0);
466: assertTrue(ui.cellWidth > 0);
467: assertNull(ui.cellHeights);
468: }
469:
470: public void testSelectNextPreviousIndex() throws Exception {
471: ui.installUI(list);
472: list.setListData(new Object[] { "1", "2", "3" });
473: assertTrue(list.isSelectionEmpty());
474: list.setSelectedIndex(0);
475: ui.selectNextIndex();
476: assertFalse(list.isSelectedIndex(0));
477: assertTrue(list.isSelectedIndex(1));
478: assertFalse(list.isSelectedIndex(2));
479: ui.selectNextIndex();
480: assertFalse(list.isSelectedIndex(0));
481: assertFalse(list.isSelectedIndex(1));
482: assertTrue(list.isSelectedIndex(2));
483: ui.selectNextIndex();
484: assertFalse(list.isSelectedIndex(0));
485: assertFalse(list.isSelectedIndex(1));
486: assertTrue(list.isSelectedIndex(2));
487: ui.selectPreviousIndex();
488: assertFalse(list.isSelectedIndex(0));
489: assertTrue(list.isSelectedIndex(1));
490: assertFalse(list.isSelectedIndex(2));
491: ui.selectPreviousIndex();
492: assertTrue(list.isSelectedIndex(0));
493: assertFalse(list.isSelectedIndex(1));
494: assertFalse(list.isSelectedIndex(2));
495: ui.selectPreviousIndex();
496: assertTrue(list.isSelectedIndex(0));
497: assertFalse(list.isSelectedIndex(1));
498: assertFalse(list.isSelectedIndex(2));
499: list.clearSelection();
500: ui.selectNextIndex();
501: assertTrue(list.isSelectedIndex(0));
502: assertFalse(list.isSelectedIndex(1));
503: assertFalse(list.isSelectedIndex(2));
504: list.clearSelection();
505: ui.selectPreviousIndex();
506: assertTrue(list.isSelectionEmpty());
507: list.setSelectionInterval(0, 1);
508: ui.selectNextIndex();
509: assertFalse(list.isSelectedIndex(0));
510: assertTrue(list.isSelectedIndex(1));
511: assertFalse(list.isSelectedIndex(2));
512: list.setSelectionInterval(1, 0);
513: ui.selectNextIndex();
514: assertFalse(list.isSelectedIndex(0));
515: assertTrue(list.isSelectedIndex(1));
516: assertFalse(list.isSelectedIndex(2));
517: list.setSelectedIndex(1);
518: list.setSelectionInterval(0, 0);
519: ui.selectNextIndex();
520: assertFalse(list.isSelectedIndex(0));
521: assertTrue(list.isSelectedIndex(1));
522: assertFalse(list.isSelectedIndex(2));
523: list.setSelectionInterval(0, 1);
524: ui.selectPreviousIndex();
525: assertTrue(list.isSelectedIndex(0));
526: assertTrue(list.isSelectedIndex(1));
527: assertFalse(list.isSelectedIndex(2));
528: list.setSelectionInterval(2, 1);
529: ui.selectPreviousIndex();
530: assertTrue(list.isSelectedIndex(0));
531: assertFalse(list.isSelectedIndex(1));
532: assertFalse(list.isSelectedIndex(2));
533: }
534:
535: /**
536: * Regression test for HARMONY-2653
537: * */
538: public void testRGetPreferredSize() {
539: try {
540: BasicListUI bl = new BasicListUI();
541: bl.getPreferredSize(new JFileChooser());
542: fail("No NPE thrown");
543: } catch (NullPointerException e) {
544: //expected
545: }
546: }
547:
548: }
|