|
1 /* |
|
2 * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 import java.awt.*; |
|
25 import java.awt.event.InputEvent; |
|
26 |
|
27 /* |
|
28 * @test |
|
29 * @summary Have different components having different preferred sizes |
|
30 * added to a grid layout having various values of row/columns. |
|
31 * Check if the compnents are correctly laid out. |
|
32 * The strategy followed is to calculate the component location |
|
33 * depending on the preferred sizes and gaps and click the cornors |
|
34 * of the components to check if events are triggered |
|
35 * @library ../../../../lib/testlibrary/ |
|
36 * @build ExtendedRobot |
|
37 * @run main ComponentPreferredSize |
|
38 * @run main ComponentPreferredSize -hg 20 -vg 20 |
|
39 */ |
|
40 |
|
41 public class ComponentPreferredSize { |
|
42 |
|
43 private int width = 200; |
|
44 private int height = 200; |
|
45 private final int hGap, vGap; |
|
46 private final int rows = 3; |
|
47 private final int columns = 2; |
|
48 private final int componentCount = 6; |
|
49 |
|
50 private Button[] buttons; |
|
51 private Frame frame; |
|
52 |
|
53 private ExtendedRobot robot; |
|
54 private GridLayout layout; |
|
55 |
|
56 private volatile boolean actionPerformed = false; |
|
57 |
|
58 public ComponentPreferredSize(int hGap, int vGap) throws Exception { |
|
59 this.hGap = hGap; |
|
60 this.vGap = vGap; |
|
61 robot = new ExtendedRobot(); |
|
62 EventQueue.invokeAndWait( () -> { |
|
63 frame = new Frame("Test frame"); |
|
64 frame.setSize(width, height); |
|
65 layout = new GridLayout(rows, columns, hGap, vGap); |
|
66 frame.setLayout(layout); |
|
67 |
|
68 buttons = new Button[componentCount]; |
|
69 for (int i = 0; i < componentCount; i++) { |
|
70 buttons[i] = new Button("Button" + i); |
|
71 buttons[i].setPreferredSize(new Dimension((int) Math.random() * 100, |
|
72 (int) Math.random() * 100)); |
|
73 frame.add(buttons[i]); |
|
74 buttons[i].addActionListener((event) -> {actionPerformed = true;}); |
|
75 } |
|
76 |
|
77 frame.setVisible(true); |
|
78 }); |
|
79 } |
|
80 |
|
81 public static void main(String[] args) throws Exception { |
|
82 int hGap = 0; |
|
83 int vGap = 0; |
|
84 for (int i = 0; i < args.length; i++) { |
|
85 switch (args[i]) { |
|
86 case "-hg": |
|
87 hGap = Integer.parseInt(args[++i]); |
|
88 break; |
|
89 case "-vg": |
|
90 vGap = Integer.parseInt(args[++i]); |
|
91 break; |
|
92 } |
|
93 } |
|
94 new ComponentPreferredSize(hGap, vGap).doTest(); |
|
95 } |
|
96 |
|
97 private void resizeFrame() throws Exception { |
|
98 EventQueue.invokeAndWait(() -> { |
|
99 Insets insets = frame.getInsets(); |
|
100 double dH = (height-insets.top-insets.bottom - vGap*(rows-1)) % rows; |
|
101 double dW = (width-insets.left-insets.right - hGap*(columns-1)) % columns; |
|
102 height -= dH; |
|
103 width -= dW; |
|
104 frame.setSize(width, height); |
|
105 frame.revalidate(); |
|
106 }); |
|
107 robot.waitForIdle(); |
|
108 } |
|
109 |
|
110 public void testBoundaries(int topLeftX, int topLeftY, int bottomRightX, int bottomRightY) throws Exception { |
|
111 |
|
112 actionPerformed = false; |
|
113 robot.mouseMove(topLeftX, topLeftY); |
|
114 robot.delay(500); |
|
115 robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
|
116 robot.delay(500); |
|
117 robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
|
118 robot.waitForIdle(3000); |
|
119 |
|
120 if(!actionPerformed) |
|
121 throw new RuntimeException("Clicking on the left top of button did not trigger action event"); |
|
122 |
|
123 actionPerformed = false; |
|
124 robot.mouseMove(bottomRightX, bottomRightY); |
|
125 robot.delay(500); |
|
126 robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
|
127 robot.delay(500); |
|
128 robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
|
129 robot.waitForIdle(3000); |
|
130 |
|
131 if(!actionPerformed) |
|
132 throw new RuntimeException("Clicking on the bottom right of button did not trigger action event"); |
|
133 } |
|
134 |
|
135 private void doTest() throws Exception { |
|
136 robot.waitForIdle(); |
|
137 resizeFrame(); |
|
138 |
|
139 int availableWidth = width - frame.getInsets().left - |
|
140 frame.getInsets().right; |
|
141 int componentWidth = (availableWidth + hGap) / columns - hGap; |
|
142 int availableHeight = height - frame.getInsets().top - |
|
143 frame.getInsets().bottom; |
|
144 int componentHeight = (availableHeight + vGap) / rows - vGap; |
|
145 |
|
146 for (int i = 0; i < buttons.length; i++) { |
|
147 if (buttons[i].getSize().width != componentWidth || |
|
148 buttons[i].getSize().height != componentHeight) { |
|
149 throw new RuntimeException( |
|
150 "FAIL: Button " + i + " not of proper size" + |
|
151 "Expected: " + componentWidth + "*" + componentHeight + |
|
152 "Actual: " + buttons[i].getSize().width + "*" + buttons[i].getSize().height); |
|
153 } |
|
154 } |
|
155 |
|
156 // Components are visible. They should trigger events. |
|
157 // Now you can check for the actual size shown. |
|
158 int currentRow = 1; |
|
159 int currentColumn = 0; |
|
160 for (int i = 0; i < buttons.length; i++) { |
|
161 currentColumn++; |
|
162 if (currentColumn > columns) { |
|
163 currentColumn = 1; |
|
164 currentRow++; |
|
165 } |
|
166 |
|
167 int topPosX = frame.getLocationOnScreen().x + |
|
168 frame.getInsets().left + |
|
169 (currentColumn - 1) * (componentWidth + hGap); |
|
170 int topPosY = frame.getLocationOnScreen().y + |
|
171 frame.getInsets().top + |
|
172 (currentRow - 1) * (componentHeight + vGap); |
|
173 |
|
174 int bottomPosX = topPosX + componentWidth - 1; |
|
175 int bottomPosY = topPosY + componentHeight - 1; |
|
176 testBoundaries(topPosX, topPosY, bottomPosX, bottomPosY); |
|
177 } |
|
178 |
|
179 frame.dispose(); |
|
180 } |
|
181 } |