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 * @key randomness 40 */ 41 42 public class ComponentPreferredSize { 43 44 private int width = 200; 45 private int height = 200; 46 private final int hGap, vGap; 47 private final int rows = 3; 48 private final int columns = 2; 49 private final int componentCount = 6; 50 51 private Button[] buttons; 52 private Frame frame; 53 54 private ExtendedRobot robot; 55 private GridLayout layout; 56 57 private volatile boolean actionPerformed = false; 58 59 public ComponentPreferredSize(int hGap, int vGap) throws Exception { 60 this.hGap = hGap; 61 this.vGap = vGap; 62 robot = new ExtendedRobot(); 63 EventQueue.invokeAndWait( () -> { 64 frame = new Frame("Test frame"); 65 frame.setSize(width, height); 66 layout = new GridLayout(rows, columns, hGap, vGap); 67 frame.setLayout(layout); 68 69 buttons = new Button[componentCount]; 70 for (int i = 0; i < componentCount; i++) { 71 buttons[i] = new Button("Button" + i); 72 buttons[i].setPreferredSize(new Dimension((int) Math.random() * 100, 73 (int) Math.random() * 100)); 74 frame.add(buttons[i]); 75 buttons[i].addActionListener((event) -> {actionPerformed = true;}); 76 } 77 78 frame.setVisible(true); 79 }); 80 } 81 82 public static void main(String[] args) throws Exception { 83 int hGap = 0; 84 int vGap = 0; 85 for (int i = 0; i < args.length; i++) { 86 switch (args[i]) { 87 case "-hg": 88 hGap = Integer.parseInt(args[++i]); 89 break; 90 case "-vg": 91 vGap = Integer.parseInt(args[++i]); 92 break; 93 } 94 } 95 new ComponentPreferredSize(hGap, vGap).doTest(); 96 } 97 98 private void resizeFrame() throws Exception { 99 EventQueue.invokeAndWait(() -> { 100 Insets insets = frame.getInsets(); 101 double dH = (height-insets.top-insets.bottom - vGap*(rows-1)) % rows; 102 double dW = (width-insets.left-insets.right - hGap*(columns-1)) % columns; 103 height -= dH; 104 width -= dW; 105 frame.setSize(width, height); 106 frame.revalidate(); 107 }); 108 robot.waitForIdle(); 109 } 110 111 public void testBoundaries(int topLeftX, int topLeftY, int bottomRightX, int bottomRightY) throws Exception { 112 113 actionPerformed = false; 114 robot.mouseMove(topLeftX, topLeftY); 115 robot.delay(500); 116 robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); 117 robot.delay(500); 118 robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); 119 robot.waitForIdle(3000); 120 121 if(!actionPerformed) 122 throw new RuntimeException("Clicking on the left top of button did not trigger action event"); 123 124 actionPerformed = false; 125 robot.mouseMove(bottomRightX, bottomRightY); 126 robot.delay(500); 127 robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); 128 robot.delay(500); 129 robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); 130 robot.waitForIdle(3000); 131 132 if(!actionPerformed) 133 throw new RuntimeException("Clicking on the bottom right of button did not trigger action event"); 134 } 135 136 private void doTest() throws Exception { 137 robot.waitForIdle(); 138 resizeFrame(); 139 140 int availableWidth = width - frame.getInsets().left - 141 frame.getInsets().right; 142 int componentWidth = (availableWidth + hGap) / columns - hGap; 143 int availableHeight = height - frame.getInsets().top - 144 frame.getInsets().bottom; 145 int componentHeight = (availableHeight + vGap) / rows - vGap; 146 147 for (int i = 0; i < buttons.length; i++) { 148 if (buttons[i].getSize().width != componentWidth || 149 buttons[i].getSize().height != componentHeight) { 150 throw new RuntimeException( 151 "FAIL: Button " + i + " not of proper size" + 152 "Expected: " + componentWidth + "*" + componentHeight + 153 "Actual: " + buttons[i].getSize().width + "*" + buttons[i].getSize().height); 154 } 155 } 156 157 // Components are visible. They should trigger events. 158 // Now you can check for the actual size shown. 159 int currentRow = 1; 160 int currentColumn = 0; 161 for (int i = 0; i < buttons.length; i++) { 162 currentColumn++; 163 if (currentColumn > columns) { 164 currentColumn = 1; 165 currentRow++; 166 } 167 168 int topPosX = frame.getLocationOnScreen().x + 169 frame.getInsets().left + 170 (currentColumn - 1) * (componentWidth + hGap); 171 int topPosY = frame.getLocationOnScreen().y + 172 frame.getInsets().top + 173 (currentRow - 1) * (componentHeight + vGap); 174 175 int bottomPosX = topPosX + componentWidth - 1; 176 int bottomPosY = topPosY + componentHeight - 1; 177 testBoundaries(topPosX, topPosY, bottomPosX, bottomPosY); 178 } 179 180 frame.dispose(); 181 } 182 }