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 javax.swing.*; 25 import java.awt.*; 26 import java.awt.event.InputEvent; 27 import static jdk.testlibrary.Asserts.assertTrue; 28 29 /* 30 * @test 31 * @summary Have different components having different preferred sizes 32 * added to a grid layout having various values of row/columns. 33 * Check if the compnents are correctly laid out. 34 * The strategy followed is to calculate the component location 35 * depending on the preferred sizes and gaps and click the cornors 36 * of the components to check if events are triggered 37 * @library ../../../../lib/testlibrary/ 38 * @build ExtendedRobot 39 * @run main ComponentPreferredSize 40 * @run main ComponentPreferredSize -hg 20 -vg 20 41 */ 42 43 public class ComponentPreferredSize { 44 45 private int hGap, vGap; 46 private int width = 200; 47 private int height = 200; 48 private int rows = 3; 49 private int columns = 2; 50 private int componentCount = 6; 51 52 private JButton[] buttons; 53 private JFrame frame; 54 55 private ExtendedRobot robot; 56 private GridLayout layout; 57 58 private boolean actionPerformed = false; 59 60 public ComponentPreferredSize(int hGap, int vGap) throws Exception { 61 this.hGap = hGap; 62 this.vGap = vGap; 63 robot = new ExtendedRobot(); 64 SwingUtilities.invokeAndWait( () -> { 65 frame = new JFrame("Test frame"); 66 frame.setSize(width, height); 67 layout = new GridLayout(rows, columns, hGap, vGap); 68 frame.getContentPane().setLayout(layout); 69 70 buttons = new JButton[componentCount]; 71 for (int i = 0; i < componentCount; i++) { 72 buttons[i] = new JButton("Button" + i); 73 buttons[i].setPreferredSize(new Dimension((int) Math.random() * 100, 74 (int) Math.random() * 100)); 75 frame.getContentPane().add(buttons[i]); 76 buttons[i].addActionListener((event) -> {actionPerformed = true;}); 77 } 78 79 frame.setVisible(true); 80 }); 81 } 82 83 public static void main(String[] args) throws Exception { 84 int hGap = 0; 85 int vGap = 0; 86 for (int i = 0; i < args.length; i++) { 87 switch (args[i]) { 88 case "-hg": 89 hGap = Integer.parseInt(args[++i]); 90 break; 91 case "-vg": 92 vGap = Integer.parseInt(args[++i]); 93 break; 94 } 95 } 96 new ComponentPreferredSize(hGap, vGap).doTest(); 97 } 98 99 private void resizeFrame() throws Exception { 100 SwingUtilities.invokeAndWait( () -> { 101 double dH = (frame.getContentPane().getSize().getHeight() - vGap*(rows-1)) % rows; 102 height -= dH; 103 frame.setSize(width, height); 104 frame.getContentPane().revalidate(); 105 }); 106 robot.waitForIdle(); 107 } 108 109 public void testBoundaries(int topLeftX, int topLeftY, int bottomRightX, int bottomRightY) throws Exception { 110 111 actionPerformed = false; 112 robot.mouseMove(topLeftX, topLeftY); 113 robot.delay(500); 114 robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); 115 robot.delay(500); 116 robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); 117 robot.delay(3000); 118 robot.waitForIdle(); 119 120 assertTrue(actionPerformed, "Clicking on the left top of button did not trigger action event"); 121 122 actionPerformed = false; 123 robot.mouseMove(bottomRightX, bottomRightY); 124 robot.delay(500); 125 robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); 126 robot.delay(500); 127 robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); 128 robot.delay(3000); 129 robot.waitForIdle(); 130 131 assertTrue(actionPerformed, "Clicking on the bottom right of button did not trigger action event"); 132 } 133 134 private void doTest() throws Exception { 135 robot.waitForIdle(); 136 resizeFrame(); 137 138 int availableWidth = width - frame.getInsets().left - 139 frame.getInsets().right; 140 int componentWidth = (availableWidth + hGap) / columns - hGap; 141 int availableHeight = height - frame.getInsets().top - 142 frame.getInsets().bottom; 143 int componentHeight = (availableHeight + vGap) / rows - vGap; 144 145 for (int i = 0; i < buttons.length; i++) { 146 if (buttons[i].getSize().width != componentWidth || 147 buttons[i].getSize().height != componentHeight) { 148 throw new RuntimeException( 149 "FAIL: Button " + i + " not of proper size" + 150 "Expected: " + componentWidth + "*" + componentHeight + 151 "Actual: " + buttons[i].getSize().width + "*" + buttons[i].getSize().height); 152 } 153 } 154 155 // Components are visible. They should trigger events. 156 // Now you can check for the actual size shown. 157 int currentRow = 1; 158 int currentColumn = 0; 159 for (int i = 0; i < buttons.length; i++) { 160 currentColumn++; 161 if (currentColumn > columns) { 162 currentColumn = 1; 163 currentRow++; 164 } 165 166 int topPosX = frame.getLocationOnScreen().x + 167 frame.getInsets().left + 168 (currentColumn - 1) * (componentWidth + hGap); 169 int topPosY = frame.getLocationOnScreen().y + 170 frame.getInsets().top + 171 (currentRow - 1) * (componentHeight + vGap); 172 173 int bottomPosX = topPosX + componentWidth - 1; 174 int bottomPosY = topPosY + componentHeight - 1; 175 testBoundaries(topPosX, topPosY, bottomPosX, bottomPosY); 176 } 177 } 178 }