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