1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.util;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 |
|
---|
6 | import java.awt.Dimension;
|
---|
7 | import java.awt.FlowLayout;
|
---|
8 |
|
---|
9 | import javax.swing.BorderFactory;
|
---|
10 | import javax.swing.JPanel;
|
---|
11 |
|
---|
12 | import org.junit.Before;
|
---|
13 | import org.junit.Rule;
|
---|
14 | import org.junit.Test;
|
---|
15 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
16 |
|
---|
17 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Test {@link MultiLineFlowLayout}
|
---|
21 | * @author Michael Zangl
|
---|
22 | */
|
---|
23 | public class MultiLineFlowLayoutTest {
|
---|
24 | /**
|
---|
25 | * No special rules.
|
---|
26 | */
|
---|
27 | @Rule
|
---|
28 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
29 | public JOSMTestRules test = new JOSMTestRules();
|
---|
30 |
|
---|
31 | private static final int TEST_WIDHT = 500;
|
---|
32 | private JPanel container;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Prepare test container.
|
---|
36 | */
|
---|
37 | @Before
|
---|
38 | public void setUp() {
|
---|
39 | JPanel parent = new JPanel();
|
---|
40 | parent.setBounds(0, 0, TEST_WIDHT, 500);
|
---|
41 |
|
---|
42 | container = new JPanel(new MultiLineFlowLayout(FlowLayout.CENTER, 0, 0));
|
---|
43 | parent.add(container);
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Test that one line is layed out correctly
|
---|
48 | */
|
---|
49 | @Test
|
---|
50 | public void testOneLine() {
|
---|
51 | fillOneLine();
|
---|
52 |
|
---|
53 | container.invalidate();
|
---|
54 | Dimension preffered = container.getPreferredSize();
|
---|
55 | assertEquals(TEST_WIDHT, preffered.width);
|
---|
56 | assertEquals(100, preffered.height);
|
---|
57 |
|
---|
58 | Dimension minimum = container.getMinimumSize();
|
---|
59 | assertEquals(TEST_WIDHT, minimum.width);
|
---|
60 | assertEquals(50, minimum.height);
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Test that insets are respected
|
---|
65 | */
|
---|
66 | @Test
|
---|
67 | public void testInsets() {
|
---|
68 | fillOneLine();
|
---|
69 |
|
---|
70 | container.setBorder(BorderFactory.createEmptyBorder(3, 0, 7, 0));
|
---|
71 | container.invalidate();
|
---|
72 | Dimension preffered = container.getPreferredSize();
|
---|
73 | assertEquals(TEST_WIDHT, preffered.width);
|
---|
74 | assertEquals(110, preffered.height);
|
---|
75 |
|
---|
76 | // This should force wrapping
|
---|
77 | container.setBorder(BorderFactory.createEmptyBorder(0, 20, 0, 40));
|
---|
78 | container.invalidate();
|
---|
79 | preffered = container.getPreferredSize();
|
---|
80 | assertEquals(TEST_WIDHT, preffered.width);
|
---|
81 | assertEquals(200, preffered.height);
|
---|
82 | }
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Test that gaps are respected
|
---|
86 | */
|
---|
87 | @Test
|
---|
88 | public void testGaps() {
|
---|
89 | fillOneLine();
|
---|
90 |
|
---|
91 | container.setLayout(new MultiLineFlowLayout(FlowLayout.LEADING, 20, 10));
|
---|
92 | container.invalidate();
|
---|
93 | Dimension preffered = container.getPreferredSize();
|
---|
94 | assertEquals(TEST_WIDHT, preffered.width);
|
---|
95 | assertEquals(230, preffered.height);
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Test that it behaves the same as FlowLayout for one line.
|
---|
100 | */
|
---|
101 | @Test
|
---|
102 | public void testSameAsFlowLayout() {
|
---|
103 | fillOneLine();
|
---|
104 | JPanel childx = new JPanel();
|
---|
105 | childx.setPreferredSize(new Dimension(300, 100));
|
---|
106 | childx.setMinimumSize(new Dimension(200, 50));
|
---|
107 | childx.setVisible(false);
|
---|
108 | container.add(childx);
|
---|
109 | container.setBorder(BorderFactory.createEmptyBorder(3, 4, 5, 6));
|
---|
110 |
|
---|
111 | container.setLayout(new MultiLineFlowLayout(FlowLayout.LEADING, 2, 1));
|
---|
112 | container.invalidate();
|
---|
113 | Dimension is = container.getPreferredSize();
|
---|
114 |
|
---|
115 | container.setLayout(new FlowLayout(FlowLayout.LEADING, 2, 1));
|
---|
116 | container.invalidate();
|
---|
117 | Dimension should = container.getPreferredSize();
|
---|
118 |
|
---|
119 | assertEquals(should.height, is.height);
|
---|
120 | }
|
---|
121 |
|
---|
122 | private void fillOneLine() {
|
---|
123 | JPanel child1 = new JPanel();
|
---|
124 | child1.setPreferredSize(new Dimension(300, 100));
|
---|
125 | child1.setMinimumSize(new Dimension(200, 50));
|
---|
126 | container.add(child1);
|
---|
127 | JPanel child2 = new JPanel();
|
---|
128 | child2.setPreferredSize(new Dimension(100, 100));
|
---|
129 | child1.setMinimumSize(new Dimension(100, 50));
|
---|
130 | container.add(child2);
|
---|
131 | JPanel child3 = new JPanel();
|
---|
132 | child3.setPreferredSize(new Dimension(50, 100));
|
---|
133 | child1.setMinimumSize(new Dimension(50, 50));
|
---|
134 | container.add(child3);
|
---|
135 | }
|
---|
136 | }
|
---|