Source Code Cross Referenced for JListTest.java in  » Apache-Harmony-Java-SE » javax-package » javax » swing » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » javax package » javax.swing 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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;
021:
022:        import java.awt.Color;
023:        import java.awt.Component;
024:        import java.awt.Dimension;
025:        import java.awt.EventQueue;
026:        import java.awt.Font;
027:        import java.awt.FontMetrics;
028:        import java.awt.Point;
029:        import java.awt.Rectangle;
030:        import java.awt.event.MouseEvent;
031:        import java.beans.PropertyChangeEvent;
032:        import java.beans.PropertyChangeListener;
033:        import java.util.ArrayList;
034:        import java.util.List;
035:        import java.util.Vector;
036:        import javax.swing.event.ListDataListener;
037:        import javax.swing.event.ListSelectionEvent;
038:        import javax.swing.event.ListSelectionListener;
039:        import javax.swing.plaf.ListUI;
040:        import javax.swing.text.Position;
041:
042:        public class JListTest extends SwingTestCase {
043:            private JList list;
044:
045:            private TestPropertyChangeListener changeListener;
046:
047:            private JFrame frame;
048:
049:            public JListTest(final String name) {
050:                super (name);
051:            }
052:
053:            @Override
054:            protected void setUp() throws Exception {
055:                super .setUp();
056:                list = new JList(new Object[] { "a", "b", "c" });
057:                list.setCellRenderer(new DefaultListCellRenderer() {
058:                    private static final long serialVersionUID = 1L;
059:
060:                    @Override
061:                    public FontMetrics getFontMetrics(Font f) {
062:                        return JListTest.this .getFontMetrics(f, 10, 10);
063:                    }
064:                });
065:                changeListener = new TestPropertyChangeListener();
066:                list.addPropertyChangeListener(changeListener);
067:                frame = new JFrame();
068:            }
069:
070:            @Override
071:            protected void tearDown() throws Exception {
072:                list = null;
073:                changeListener = null;
074:                frame = null;
075:                super .tearDown();
076:            }
077:
078:            public void testJList() throws Exception {
079:                list = new JList();
080:                assertNotNull(list.getModel());
081:                assertTrue(list.getSelectionModel() instanceof  DefaultListSelectionModel);
082:                assertNotNull(list.getUI());
083:                assertTrue(list.isOpaque());
084:                ListModel testModel = new ListModel() {
085:                    public void addListDataListener(final ListDataListener l) {
086:                    }
087:
088:                    public Object getElementAt(final int index) {
089:                        return null;
090:                    }
091:
092:                    public int getSize() {
093:                        return 0;
094:                    }
095:
096:                    public void removeListDataListener(final ListDataListener l) {
097:                    }
098:                };
099:                list = new JList(testModel);
100:                assertEquals(testModel, list.getModel());
101:                assertTrue(list.getSelectionModel() instanceof  DefaultListSelectionModel);
102:                assertNotNull(list.getUI());
103:                list = new JList(new Object[] { "a", "b" });
104:                assertNotNull(list.getModel());
105:                assertTrue(list.getSelectionModel() instanceof  DefaultListSelectionModel);
106:                assertNotNull(list.getUI());
107:                list = new JList(new Vector<Object>());
108:                assertNotNull(list.getModel());
109:                assertTrue(list.getSelectionModel() instanceof  DefaultListSelectionModel);
110:                assertNotNull(list.getUI());
111:            }
112:
113:            public void testAddGetRemoveListSelectionListener()
114:                    throws Exception {
115:                assertEquals(0, list.getListSelectionListeners().length);
116:                TestListSelectionListener l = new TestListSelectionListener();
117:                list.addListSelectionListener(l);
118:                list.addListSelectionListener(new TestListSelectionListener());
119:                list.addListSelectionListener(new TestListSelectionListener());
120:                assertEquals(3, list.getListSelectionListeners().length);
121:                list.removeListSelectionListener(l);
122:                assertEquals(2, list.getListSelectionListeners().length);
123:            }
124:
125:            public void testAddSelectionInterval() throws Exception {
126:                assertTrue(list.isSelectionEmpty());
127:                assertTrue(list.getSelectionModel().isSelectionEmpty());
128:                list.addSelectionInterval(1, 2);
129:                assertFalse(list.isSelectionEmpty());
130:                assertFalse(list.getSelectionModel().isSelectionEmpty());
131:                try {
132:                    // Regression for HARMONY-1965
133:                    list.addSelectionInterval(1, -2);
134:                    fail("IndexOutOfBoundsException expected.");
135:                } catch (IndexOutOfBoundsException e) {
136:                }
137:                try {
138:                    // Regression for HARMONY-1965
139:                    list.addSelectionInterval(-2, 2);
140:                    fail("IndexOutOfBoundsException expected.");
141:                } catch (IndexOutOfBoundsException e) {
142:                }
143:            }
144:
145:            public void testClearSelection() throws Exception {
146:                assertTrue(list.isSelectionEmpty());
147:                assertTrue(list.getSelectionModel().isSelectionEmpty());
148:                list.setSelectedIndex(1);
149:                assertFalse(list.isSelectionEmpty());
150:                assertFalse(list.getSelectionModel().isSelectionEmpty());
151:                list.clearSelection();
152:                assertTrue(list.isSelectionEmpty());
153:                assertTrue(list.getSelectionModel().isSelectionEmpty());
154:            }
155:
156:            public void testCreateSelectionModel() throws Exception {
157:                assertTrue(new TestList().createSelectionModel() instanceof  DefaultListSelectionModel);
158:            }
159:
160:            public void testEnsureIndexIsVisible() throws Exception {
161:                JScrollPane scroller = insertListToFrame();
162:                assertNotNull(scroller);
163:                Rectangle bounds = list.getCellBounds(1, 1);
164:                assertFalse(list.getVisibleRect().contains(bounds));
165:                list.ensureIndexIsVisible(1);
166:                assertTrue(list.getVisibleRect().contains(bounds));
167:                list = new JList();
168:                assertEquals(0, list.getModel().getSize());
169:                list.ensureIndexIsVisible(0);
170:                list.ensureIndexIsVisible(10);
171:                list.ensureIndexIsVisible(-10);
172:                list.setListData(new String[] { "1", "2" });
173:                list.ensureIndexIsVisible(0);
174:                list.ensureIndexIsVisible(10);
175:                list.ensureIndexIsVisible(-10);
176:            }
177:
178:            public void testFireSelectionValueChanged() throws Exception {
179:                TestListSelectionListener l1 = new TestListSelectionListener();
180:                TestListSelectionListener l2 = new TestListSelectionListener();
181:                TestListSelectionListener l3 = new TestListSelectionListener();
182:                TestList list = new TestList();
183:                list.addListSelectionListener(l1);
184:                list.addListSelectionListener(l2);
185:                list.getSelectionModel().addListSelectionListener(l3);
186:                list.fireSelectionValueChanged(2, 5, true);
187:                assertNotNull(l1.getEvent());
188:                assertNotNull(l2.getEvent());
189:                assertNull(l3.getEvent());
190:                assertEquals(2, l1.getEvent().getFirstIndex());
191:                assertEquals(5, l1.getEvent().getLastIndex());
192:                assertTrue(l1.getEvent().getValueIsAdjusting());
193:                if (isHarmony()) {
194:                    l1.reset();
195:                    l2.reset();
196:                    l3.reset();
197:                    list.setSelectedIndex(1);
198:                    assertNotNull(l1.getEvent());
199:                    assertNotNull(l2.getEvent());
200:                    assertNotNull(l3.getEvent());
201:                    assertEquals(list, l1.getEvent().getSource());
202:                    assertEquals(1, l1.getEvent().getFirstIndex());
203:                    assertEquals(1, l1.getEvent().getLastIndex());
204:                    assertFalse(l1.getEvent().getValueIsAdjusting());
205:                    assertEquals(list.getSelectionModel(), l3.getEvent()
206:                            .getSource());
207:                    assertEquals(1, l3.getEvent().getFirstIndex());
208:                    assertEquals(1, l3.getEvent().getLastIndex());
209:                    assertFalse(l3.getEvent().getValueIsAdjusting());
210:                }
211:            }
212:
213:            public void testGetAccssibleContext() throws Exception {
214:                assertTrue(list.getAccessibleContext() instanceof  JList.AccessibleJList);
215:            }
216:
217:            public void testGetAnchorSelectionIndex() throws Exception {
218:                list.addSelectionInterval(2, 1);
219:                assertEquals(2, list.getAnchorSelectionIndex());
220:                assertEquals(2, list.getSelectionModel()
221:                        .getAnchorSelectionIndex());
222:            }
223:
224:            public void testGetCellBounds() throws Exception {
225:                list.setUI(new ListUI() {
226:                    @Override
227:                    public Point indexToLocation(final JList arg0,
228:                            final int arg1) {
229:                        return null;
230:                    }
231:
232:                    @Override
233:                    public int locationToIndex(final JList arg0,
234:                            final Point arg1) {
235:                        return 0;
236:                    }
237:
238:                    @Override
239:                    public Rectangle getCellBounds(final JList arg0,
240:                            final int arg1, final int arg2) {
241:                        return new Rectangle(10, 20, 30, 40);
242:                    }
243:                });
244:                assertEquals(new Rectangle(10, 20, 30, 40), list.getCellBounds(
245:                        0, 0));
246:            }
247:
248:            public void testGetSetCellRenderer() throws Exception {
249:                assertTrue(list.getCellRenderer() instanceof  DefaultListCellRenderer);
250:                ListCellRenderer testRenderer = new ListCellRenderer() {
251:                    public Component getListCellRendererComponent(
252:                            final JList list, final Object value,
253:                            final int index, final boolean isSelected,
254:                            final boolean cellHasFocus) {
255:                        return null;
256:                    }
257:                };
258:                list.setCellRenderer(testRenderer);
259:                assertEquals(testRenderer, list.getCellRenderer());
260:            }
261:
262:            public void testGetSetDragEnabled() throws Exception {
263:                assertFalse(list.getDragEnabled());
264:                list.setDragEnabled(true);
265:                assertTrue(list.getDragEnabled());
266:            }
267:
268:            public void testGetFirstVisibleIndex() throws Exception {
269:                JScrollPane scroller = insertListToFrame();
270:                Rectangle bounds = list.getCellBounds(1, 1);
271:                assertNotNull(bounds);
272:                assertEquals(0, list.getFirstVisibleIndex());
273:                scroller.getVerticalScrollBar().setValue(
274:                        scroller.getVerticalScrollBar().getMaximum());
275:                assertEquals(1, list.getFirstVisibleIndex());
276:            }
277:
278:            public void testGetSetFixedCellHeight() throws Exception {
279:                assertEquals(-1, list.getFixedCellHeight());
280:                list.setFixedCellHeight(10);
281:                assertEquals(10, list.getFixedCellHeight());
282:                assertTrue(changeListener.isChanged("fixedCellHeight"));
283:            }
284:
285:            public void testGetSetFixedCellWidth() throws Exception {
286:                assertEquals(-1, list.getFixedCellWidth());
287:                list.setFixedCellWidth(10);
288:                assertEquals(10, list.getFixedCellWidth());
289:                assertTrue(changeListener.isChanged("fixedCellWidth"));
290:            }
291:
292:            public void testGetLastVisibleIndex() throws Exception {
293:                JScrollPane scroller = insertListToFrame();
294:                Rectangle bounds = list.getCellBounds(1, 1);
295:                assertNotNull(bounds);
296:                assertEquals(1, list.getLastVisibleIndex());
297:                scroller.getVerticalScrollBar().setValue(
298:                        scroller.getVerticalScrollBar().getMaximum());
299:                assertEquals(2, list.getLastVisibleIndex());
300:            }
301:
302:            public void testGetSetLayoutOrientation() throws Exception {
303:                assertEquals(JList.VERTICAL, list.getLayoutOrientation());
304:                list.setLayoutOrientation(JList.VERTICAL_WRAP);
305:                assertEquals(JList.VERTICAL_WRAP, list.getLayoutOrientation());
306:                assertTrue(changeListener.isChanged("layoutOrientation"));
307:                list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
308:                assertEquals(JList.HORIZONTAL_WRAP, list.getLayoutOrientation());
309:                testExceptionalCase(new IllegalArgumentCase() {
310:                    @Override
311:                    public void exceptionalAction() {
312:                        list.setLayoutOrientation(10);
313:                    }
314:                });
315:            }
316:
317:            public void testGetLeadSelectionIndex() throws Exception {
318:                list.addSelectionInterval(2, 1);
319:                assertEquals(1, list.getLeadSelectionIndex());
320:                assertEquals(1, list.getSelectionModel()
321:                        .getLeadSelectionIndex());
322:            }
323:
324:            public void testGetMaxSelectionIndex() throws Exception {
325:                list.addSelectionInterval(2, 1);
326:                assertEquals(2, list.getMaxSelectionIndex());
327:                assertEquals(2, list.getSelectionModel().getMaxSelectionIndex());
328:            }
329:
330:            public void testGetMinSelectionIndex() throws Exception {
331:                list.addSelectionInterval(2, 1);
332:                assertEquals(1, list.getMinSelectionIndex());
333:                assertEquals(1, list.getSelectionModel().getMinSelectionIndex());
334:            }
335:
336:            public void testGetSetModel() throws Exception {
337:                assertNotNull(list.getModel());
338:                ListModel m = new DefaultListModel();
339:                list.setModel(m);
340:                assertEquals(m, list.getModel());
341:                assertTrue(changeListener.isChanged("model"));
342:            }
343:
344:            public void testGetNextMatch() throws Exception {
345:                list = new JList(new Object[] { "a1", "b1", "c1", "a2", "B2",
346:                        "c2" });
347:                assertEquals(0, list
348:                        .getNextMatch("a", 0, Position.Bias.Forward));
349:                assertEquals(3, list
350:                        .getNextMatch("a", 1, Position.Bias.Forward));
351:                assertEquals(0, list
352:                        .getNextMatch("a", 4, Position.Bias.Forward));
353:                assertEquals(1, list.getNextMatch("B", 1,
354:                        Position.Bias.Backward));
355:                assertEquals(1, list.getNextMatch("b", 3,
356:                        Position.Bias.Backward));
357:                assertEquals(4, list.getNextMatch("b", 5,
358:                        Position.Bias.Backward));
359:                assertEquals(5, list.getNextMatch("c", 1,
360:                        Position.Bias.Backward));
361:                assertEquals(-1, list.getNextMatch("d", 1,
362:                        Position.Bias.Backward));
363:                testExceptionalCase(new IllegalArgumentCase() {
364:                    @Override
365:                    public void exceptionalAction() {
366:                        list.getNextMatch("a", -1, Position.Bias.Forward);
367:                    }
368:                });
369:                testExceptionalCase(new IllegalArgumentCase() {
370:                    @Override
371:                    public void exceptionalAction() {
372:                        list.getNextMatch("a", 10, Position.Bias.Forward);
373:                    }
374:                });
375:                testExceptionalCase(new IllegalArgumentCase() {
376:                    @Override
377:                    public void exceptionalAction() {
378:                        list.getNextMatch(null, 1, Position.Bias.Forward);
379:                    }
380:                });
381:            }
382:
383:            public void testGetPreferredScrollableViewportSize()
384:                    throws Exception {
385:                list.setFixedCellHeight(10);
386:                list.setFixedCellWidth(100);
387:                list.setVisibleRowCount(5);
388:                assertEquals(new Dimension(100, 50), list
389:                        .getPreferredScrollableViewportSize());
390:                list.setFixedCellWidth(-1);
391:                list.setListData(new Object[] { "a", "bbb", "cc" });
392:                int expectedWidth = list.getCellRenderer()
393:                        .getListCellRendererComponent(list, "bbb", 1, false,
394:                                false).getPreferredSize().width;
395:                assertEquals(new Dimension(expectedWidth, 50), list
396:                        .getPreferredScrollableViewportSize());
397:                list.setFixedCellHeight(-1);
398:                int expectedHeight = list.getCellRenderer()
399:                        .getListCellRendererComponent(list, "bbb", 1, false,
400:                                false).getPreferredSize().height * 5;
401:                assertEquals(new Dimension(expectedWidth, expectedHeight), list
402:                        .getPreferredScrollableViewportSize());
403:                list.setListData(new Object[] {});
404:                assertEquals(new Dimension(256, 16 * 5), list
405:                        .getPreferredScrollableViewportSize());
406:            }
407:
408:            public void testGetSetPrototypeCellValue() throws Exception {
409:                assertNull(list.getPrototypeCellValue());
410:                list.setFixedCellHeight(1);
411:                list.setFixedCellWidth(1);
412:                changeListener.reset();
413:                list.setPrototypeCellValue("abcdef");
414:                assertEquals("abcdef", list.getPrototypeCellValue());
415:                assertTrue(changeListener.isChanged("prototypeCellValue"));
416:                assertEquals(1, changeListener.getNumberOfChanges());
417:                assertTrue(list.getFixedCellHeight() > 10);
418:                assertTrue(list.getFixedCellWidth() > 10);
419:            }
420:
421:            //TODO
422:            public void testGetScrollableBlockIncrement() throws Exception {
423:                list.setListData(new Object[] { "a", "b", "c", "d", "e", "f",
424:                        "g", "h" });
425:                JScrollPane scroller = insertListToFrame(50);
426:                int lastVisibleIndex = list.getLastVisibleIndex();
427:                assertEquals(3, lastVisibleIndex);
428:                int rowHeight = list.getCellBounds(lastVisibleIndex,
429:                        lastVisibleIndex).height;
430:                assertEquals(rowHeight * lastVisibleIndex, list
431:                        .getScrollableBlockIncrement(list.getVisibleRect(),
432:                                SwingConstants.VERTICAL, 1));
433:                scroller.getVerticalScrollBar().setValue(
434:                        scroller.getVerticalScrollBar().getValue() + rowHeight
435:                                * 2);
436:                assertEquals(2, list.getFirstVisibleIndex());
437:                //        scroller.getVerticalScrollBar().setValue(columnWidth * 3);
438:                //        assertEquals(3, list.getFirstVisibleIndex());
439:                //        assertEquals(columnWidth * 2, list.getScrollableBlockIncrement(list.getVisibleRect(), SwingConstants.VERTICAL, -1));
440:                //        scroller.getVerticalScrollBar().setValue(scroller.getVerticalScrollBar().getValue() - columnWidth * 2);
441:                //        assertEquals(1, list.getFirstVisibleIndex());
442:                testExceptionalCase(new IllegalArgumentCase() {
443:                    @Override
444:                    public void exceptionalAction() {
445:                        list.getScrollableBlockIncrement(null,
446:                                SwingConstants.VERTICAL, 1);
447:                    }
448:                });
449:                testExceptionalCase(new IllegalArgumentCase() {
450:                    @Override
451:                    public void exceptionalAction() {
452:                        list.getScrollableBlockIncrement(list.getVisibleRect(),
453:                                10, 1);
454:                    }
455:                });
456:            }
457:
458:            public void testGetScrollableTracksViewportHeight()
459:                    throws Exception {
460:                JScrollPane scroller = insertListToFrame();
461:                assertNotNull(scroller);
462:                list.setPreferredSize(new Dimension(1000, 10));
463:                assertTrue(list.getScrollableTracksViewportHeight());
464:                list.setPreferredSize(new Dimension(1000, 1000));
465:                assertFalse(list.getScrollableTracksViewportHeight());
466:                list.setLayoutOrientation(JList.VERTICAL_WRAP);
467:                assertFalse(list.getScrollableTracksViewportHeight());
468:                list.setVisibleRowCount(0);
469:                assertTrue(list.getScrollableTracksViewportHeight());
470:                list.setVisibleRowCount(-1);
471:                assertTrue(list.getScrollableTracksViewportHeight());
472:            }
473:
474:            public void testGetScrollableTracksViewportWidth() throws Exception {
475:                JScrollPane scroller = insertListToFrame();
476:                assertNotNull(scroller);
477:                list.setPreferredSize(new Dimension(10, 1000));
478:                assertTrue(list.getScrollableTracksViewportWidth());
479:                list.setPreferredSize(new Dimension(1000, 1000));
480:                assertFalse(list.getScrollableTracksViewportWidth());
481:                list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
482:                assertFalse(list.getScrollableTracksViewportWidth());
483:                list.setVisibleRowCount(0);
484:                assertTrue(list.getScrollableTracksViewportWidth());
485:                list.setVisibleRowCount(-1);
486:                assertTrue(list.getScrollableTracksViewportWidth());
487:            }
488:
489:            public void testGetScrollableUnitIncrement() throws Exception {
490:                JScrollPane scroller = insertListToFrame();
491:                assertEquals(0, list.getFirstVisibleIndex());
492:                int rowHeight = list.getCellBounds(0, 0).height;
493:                assertEquals(rowHeight, list.getScrollableUnitIncrement(list
494:                        .getVisibleRect(), SwingConstants.VERTICAL, 1));
495:                assertEquals(0, list.getScrollableUnitIncrement(list
496:                        .getVisibleRect(), SwingConstants.VERTICAL, -1));
497:                scroller.getVerticalScrollBar().setValue(5);
498:                assertEquals(rowHeight - 5, list.getScrollableUnitIncrement(
499:                        list.getVisibleRect(), SwingConstants.VERTICAL, 1));
500:                assertEquals(5, list.getScrollableUnitIncrement(list
501:                        .getVisibleRect(), SwingConstants.VERTICAL, -1));
502:                scroller.getVerticalScrollBar().setValue(rowHeight);
503:                assertEquals(rowHeight, list.getScrollableUnitIncrement(list
504:                        .getVisibleRect(), SwingConstants.VERTICAL, 1));
505:                assertEquals(rowHeight, list.getScrollableUnitIncrement(list
506:                        .getVisibleRect(), SwingConstants.VERTICAL, -1));
507:                assertEquals(list.getFont().getSize(), list
508:                        .getScrollableUnitIncrement(list.getVisibleRect(),
509:                                SwingConstants.HORIZONTAL, 1));
510:                assertEquals(list.getFont().getSize(), list
511:                        .getScrollableUnitIncrement(list.getVisibleRect(),
512:                                SwingConstants.HORIZONTAL, -1));
513:                list.setFont(list.getFont().deriveFont(100f));
514:                assertEquals(100, list.getScrollableUnitIncrement(list
515:                        .getVisibleRect(), SwingConstants.HORIZONTAL, 1));
516:                assertEquals(100, list.getScrollableUnitIncrement(list
517:                        .getVisibleRect(), SwingConstants.HORIZONTAL, -1));
518:                frame.setFont(null);
519:                frame.getContentPane().setFont(null);
520:                scroller.setFont(null);
521:                scroller.getViewport().setFont(null);
522:                list.setFont(null);
523:                assertNull(list.getFont());
524:                assertEquals(1, list.getScrollableUnitIncrement(list
525:                        .getVisibleRect(), SwingConstants.HORIZONTAL, 1));
526:                assertEquals(1, list.getScrollableUnitIncrement(list
527:                        .getVisibleRect(), SwingConstants.HORIZONTAL, -1));
528:                testExceptionalCase(new IllegalArgumentCase() {
529:                    @Override
530:                    public void exceptionalAction() {
531:                        list.getScrollableUnitIncrement(null,
532:                                SwingConstants.VERTICAL, 1);
533:                    }
534:                });
535:                testExceptionalCase(new IllegalArgumentCase() {
536:                    @Override
537:                    public void exceptionalAction() {
538:                        list.getScrollableUnitIncrement(list.getVisibleRect(),
539:                                10, 1);
540:                    }
541:                });
542:            }
543:
544:            public void testGetSetSelectedIndex() throws Exception {
545:                assertEquals(-1, list.getSelectedIndex());
546:                list.setSelectedIndex(2);
547:                assertEquals(2, list.getSelectedIndex());
548:                assertTrue(list.getSelectionModel().isSelectedIndex(2));
549:                list.clearSelection();
550:                assertEquals(-1, list.getSelectedIndex());
551:            }
552:
553:            public void testGetSetSelectedIndices() throws Exception {
554:                assertEquals(0, list.getSelectedIndices().length);
555:                list.setSelectedIndices(new int[] { 0, 2 });
556:                assertEquals(2, list.getSelectedIndices().length);
557:                assertEquals(0, list.getSelectedIndices()[0]);
558:                assertEquals(2, list.getSelectedIndices()[1]);
559:                assertTrue(list.getSelectionModel().isSelectedIndex(0));
560:                assertFalse(list.getSelectionModel().isSelectedIndex(1));
561:                assertTrue(list.getSelectionModel().isSelectedIndex(2));
562:            }
563:
564:            public void testGetSetSelectedValue() throws Exception {
565:                assertNull(list.getSelectedValue());
566:                list.setSelectedIndex(1);
567:                assertEquals("b", list.getSelectedValue());
568:                list.setSelectedValue("c", true);
569:                assertEquals("c", list.getSelectedValue());
570:                assertEquals(2, list.getSelectedIndex());
571:                assertEquals(1, list.getSelectedIndices().length);
572:                assertEquals(2, list.getSelectedIndices()[0]);
573:                list.setSelectedValue("d", true);
574:                assertEquals("c", list.getSelectedValue());
575:            }
576:
577:            public void testGetSelectedValues() throws Exception {
578:                assertEquals(0, list.getSelectedValues().length);
579:                list.setSelectedIndex(1);
580:                assertEquals(1, list.getSelectedValues().length);
581:                list.addSelectionInterval(1, 2);
582:                assertEquals(2, list.getSelectedValues().length);
583:                assertEquals("b", list.getSelectedValues()[0]);
584:                assertEquals("c", list.getSelectedValues()[1]);
585:                list.setSelectedValue("a", true);
586:                assertEquals(1, list.getSelectedValues().length);
587:                assertEquals("a", list.getSelectedValues()[0]);
588:            }
589:
590:            public void testGetSetSelectionBackground() throws Exception {
591:                assertNotNull(list.getSelectionBackground());
592:                list.setSelectionBackground(Color.red);
593:                assertEquals(Color.red, list.getSelectionBackground());
594:                assertTrue(changeListener.isChanged("selectionBackground"));
595:            }
596:
597:            public void testGetSetSelectionForeground() throws Exception {
598:                assertNotNull(list.getSelectionForeground());
599:                list.setSelectionForeground(Color.red);
600:                assertEquals(Color.red, list.getSelectionForeground());
601:                assertTrue(changeListener.isChanged("selectionForeground"));
602:            }
603:
604:            public void testGetSetSelectionMode() throws Exception {
605:                assertEquals(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION,
606:                        list.getSelectionMode());
607:                list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
608:                assertEquals(ListSelectionModel.SINGLE_SELECTION, list
609:                        .getSelectionMode());
610:                assertEquals(ListSelectionModel.SINGLE_SELECTION, list
611:                        .getSelectionModel().getSelectionMode());
612:            }
613:
614:            public void testGetSetSelectionModel() throws Exception {
615:                assertTrue(list.getSelectionModel() instanceof  DefaultListSelectionModel);
616:                ListSelectionModel model = new DefaultListSelectionModel();
617:                list.setSelectionModel(model);
618:                assertEquals(model, list.getSelectionModel());
619:                assertTrue(changeListener.isChanged("selectionModel"));
620:            }
621:
622:            public void testGetToolTipText() throws Exception {
623:                insertListToFrame();
624:                assertNull(list.getToolTipText());
625:                list.setToolTipText("list tooltip");
626:                assertEquals("list tooltip", list.getToolTipText());
627:                assertEquals("list tooltip", list
628:                        .getToolTipText(new MouseEvent(list,
629:                                MouseEvent.MOUSE_ENTERED, EventQueue
630:                                        .getMostRecentEventTime(), 0, 5, 5, 0,
631:                                false)));
632:                list.setCellRenderer(new DefaultListCellRenderer() {
633:                    private static final long serialVersionUID = 1L;
634:
635:                    @Override
636:                    public Component getListCellRendererComponent(
637:                            final JList list, final Object value,
638:                            final int index, final boolean isSelected,
639:                            final boolean cellHasFocus) {
640:                        JLabel result = (JLabel) super 
641:                                .getListCellRendererComponent(list, value,
642:                                        index, isSelected, cellHasFocus);
643:                        result.setToolTipText("item tooltip");
644:                        return result;
645:                    }
646:                });
647:                assertEquals("item tooltip", list
648:                        .getToolTipText(new MouseEvent(list,
649:                                MouseEvent.MOUSE_ENTERED, EventQueue
650:                                        .getMostRecentEventTime(), 0, 5, 5, 0,
651:                                false)));
652:            }
653:
654:            public void testGetSetUpdateUI() throws Exception {
655:                ListUI ui = new ListUI() {
656:                    @Override
657:                    public Point indexToLocation(final JList arg0,
658:                            final int arg1) {
659:                        return null;
660:                    }
661:
662:                    @Override
663:                    public int locationToIndex(final JList arg0,
664:                            final Point arg1) {
665:                        return 0;
666:                    }
667:
668:                    @Override
669:                    public Rectangle getCellBounds(final JList arg0,
670:                            final int arg1, final int arg2) {
671:                        return null;
672:                    }
673:                };
674:                list.setUI(ui);
675:                assertEquals(ui, list.getUI());
676:                list.updateUI();
677:                assertNotSame(ui, list.getUI());
678:            }
679:
680:            public void testGetUICalssID() throws Exception {
681:                assertEquals("ListUI", list.getUIClassID());
682:            }
683:
684:            public void testGetSetValueIsAdjusting() throws Exception {
685:                assertFalse(list.getValueIsAdjusting());
686:                list.setValueIsAdjusting(true);
687:                assertTrue(list.getValueIsAdjusting());
688:                assertTrue(list.getSelectionModel().getValueIsAdjusting());
689:            }
690:
691:            public void testGetSetVisibleRowCount() throws Exception {
692:                assertEquals(8, list.getVisibleRowCount());
693:                list.setVisibleRowCount(10);
694:                assertEquals(10, list.getVisibleRowCount());
695:                assertTrue(changeListener.isChanged("visibleRowCount"));
696:                changeListener.reset();
697:                list.setVisibleRowCount(-2);
698:                assertEquals(0, list.getVisibleRowCount());
699:                assertTrue(changeListener.isChanged("visibleRowCount"));
700:            }
701:
702:            public void testIndexToLocation() throws Exception {
703:                ListUI ui = new ListUI() {
704:                    @Override
705:                    public Point indexToLocation(final JList arg0,
706:                            final int arg1) {
707:                        return new Point(10, 20);
708:                    }
709:
710:                    @Override
711:                    public int locationToIndex(final JList arg0,
712:                            final Point arg1) {
713:                        return 0;
714:                    }
715:
716:                    @Override
717:                    public Rectangle getCellBounds(final JList arg0,
718:                            final int arg1, final int arg2) {
719:                        return null;
720:                    }
721:                };
722:                list.setUI(ui);
723:                assertEquals(new Point(10, 20), list.indexToLocation(0));
724:            }
725:
726:            public void testIsSelectedIndex() throws Exception {
727:                assertFalse(list.isSelectedIndex(0));
728:                list.setSelectedIndex(0);
729:                assertTrue(list.isSelectedIndex(0));
730:                assertTrue(list.getSelectionModel().isSelectedIndex(0));
731:            }
732:
733:            public void testIsSelectionEmpty() throws Exception {
734:                assertTrue(list.isSelectionEmpty());
735:                list.setSelectedIndex(1);
736:                assertFalse(list.isSelectionEmpty());
737:                assertFalse(list.getSelectionModel().isSelectionEmpty());
738:            }
739:
740:            public void testLocationToIndex() throws Exception {
741:                ListUI ui = new ListUI() {
742:                    @Override
743:                    public Point indexToLocation(final JList arg0,
744:                            final int arg1) {
745:                        return null;
746:                    }
747:
748:                    @Override
749:                    public int locationToIndex(final JList arg0,
750:                            final Point arg1) {
751:                        return 30;
752:                    }
753:
754:                    @Override
755:                    public Rectangle getCellBounds(final JList arg0,
756:                            final int arg1, final int arg2) {
757:                        return null;
758:                    }
759:                };
760:                list.setUI(ui);
761:                assertEquals(30, list.locationToIndex(null));
762:            }
763:
764:            public void testRemoveSelectionInterval() throws Exception {
765:                assertTrue(list.isSelectionEmpty());
766:                assertTrue(list.getSelectionModel().isSelectionEmpty());
767:                list.addSelectionInterval(0, 2);
768:                assertFalse(list.isSelectionEmpty());
769:                assertFalse(list.getSelectionModel().isSelectionEmpty());
770:                list.removeSelectionInterval(0, 1);
771:                assertFalse(list.isSelectedIndex(0));
772:                assertFalse(list.isSelectedIndex(1));
773:                assertFalse(list.getSelectionModel().isSelectedIndex(0));
774:                assertFalse(list.getSelectionModel().isSelectedIndex(1));
775:            }
776:
777:            public void testSetListData() throws Exception {
778:                ListModel model = list.getModel();
779:                assertEquals(3, model.getSize());
780:                list.setListData(new Object[] { "1", "2" });
781:                assertTrue(changeListener.isChanged("model"));
782:                assertNotSame(model, list.getModel());
783:                assertEquals(2, list.getModel().getSize());
784:                model = list.getModel();
785:                changeListener.reset();
786:                Vector<String> data = new Vector<String>();
787:                data.add("x");
788:                data.add("y");
789:                data.add("z");
790:                list.setListData(data);
791:                assertTrue(changeListener.isChanged("model"));
792:                assertNotSame(model, list.getModel());
793:                assertEquals(3, list.getModel().getSize());
794:            }
795:
796:            private JScrollPane insertListToFrame() {
797:                return insertListToFrame(25);
798:            }
799:
800:            private JScrollPane insertListToFrame(final int preferredHeight) {
801:                frame.setLocation(100, 100);
802:                JScrollPane result = new JScrollPane(list) {
803:                    private static final long serialVersionUID = 1L;
804:
805:                    @Override
806:                    public Dimension getPreferredSize() {
807:                        return new Dimension(100, preferredHeight);
808:                    }
809:                };
810:                frame.getContentPane().add(result);
811:                frame.pack();
812:                return result;
813:            }
814:
815:            private class TestListSelectionListener implements 
816:                    ListSelectionListener {
817:                private ListSelectionEvent event;
818:
819:                public void valueChanged(final ListSelectionEvent event) {
820:                    this .event = event;
821:                }
822:
823:                public ListSelectionEvent getEvent() {
824:                    return event;
825:                }
826:
827:                public void reset() {
828:                    event = null;
829:                }
830:            }
831:
832:            private class TestPropertyChangeListener implements 
833:                    PropertyChangeListener {
834:                private List<String> changedNames = new ArrayList<String>();
835:
836:                public void propertyChange(final PropertyChangeEvent event) {
837:                    changedNames.add(event.getPropertyName());
838:                }
839:
840:                public void reset() {
841:                    changedNames.clear();
842:                }
843:
844:                public boolean isChanged(final String name) {
845:                    return changedNames.contains(name);
846:                }
847:
848:                public boolean isChanged() {
849:                    return !changedNames.isEmpty();
850:                }
851:
852:                public int getNumberOfChanges() {
853:                    return changedNames.size();
854:                }
855:
856:                public List<String> getAllChangedNames() {
857:                    return changedNames;
858:                }
859:
860:                @Override
861:                public String toString() {
862:                    return "Changed: " + changedNames;
863:                }
864:            }
865:
866:            private class TestList extends JList {
867:                private static final long serialVersionUID = 1L;
868:
869:                @Override
870:                public ListSelectionModel createSelectionModel() {
871:                    return super .createSelectionModel();
872:                }
873:
874:                @Override
875:                public void fireSelectionValueChanged(final int firstIndex,
876:                        final int lastIndex, final boolean isAdjusting) {
877:                    super.fireSelectionValueChanged(firstIndex, lastIndex,
878:                            isAdjusting);
879:                }
880:            }
881:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.