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.*; 26 27 /* 28 * @test 29 * @key headful 30 * @summary Have different components having different preferred sizes 31 * added to a grid layout. Change the rows and columns of the 32 * grid layout and check the components are re-laid out. 33 * The strategy followed is to calculate the component location 34 * depending on the preferred sizes and gaps and click the cornors 35 * of the components to check if events are triggered 36 * @library ../../../../lib/testlibrary/ 37 * @build ExtendedRobot 38 * @run main ChangeGridSize 39 * @run main ChangeGridSize -hg 20 -vg 20 40 */ 41 42 public class ChangeGridSize { 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 ChangeGridSize(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 + 3, columns - 1, 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 frame.add(buttons[i]); 73 buttons[i].addActionListener( (event) -> { actionPerformed = true; }); 74 } 75 frame.setVisible(true); 76 }); 77 } 78 79 public static void main(String[] args) throws Exception { 80 int hGap = 0; 81 int vGap = 0; 82 for (int i = 0; i < args.length; i++) { 83 switch (args[i]) { 84 case "-hg": 85 hGap = Integer.parseInt(args[++i]); 86 break; 87 case "-vg": 88 vGap = Integer.parseInt(args[++i]); 89 break; 90 } 91 } 92 new ChangeGridSize(hGap, vGap).doTest(); 93 } 94 95 private void resizeFrame() throws Exception { 96 EventQueue.invokeAndWait(() -> { 97 Insets insets = frame.getInsets(); 98 double dH = (height-insets.top-insets.bottom - vGap*(rows-1)) % rows; 99 double dW = (width-insets.left-insets.right - hGap*(columns-1)) % columns; 100 height -= dH; 101 width -= dW; 102 frame.setSize(width, height); 103 frame.revalidate(); 104 }); 105 robot.waitForIdle(); 106 } 107 108 private void changeGridSize() throws Exception { 109 EventQueue.invokeAndWait(() -> { 110 layout.setRows(rows); 111 layout.setColumns(columns); 112 113 frame.revalidate(); 114 }); 115 robot.waitForIdle(); 116 } 117 118 public void testBoundaries(int topLeftX, int topLeftY, int bottomRightX, int bottomRightY) throws Exception { 119 120 actionPerformed = false; 121 robot.mouseMove(topLeftX, topLeftY); 122 robot.delay(500); 123 robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); 124 robot.delay(500); 125 robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); 126 robot.waitForIdle(3000); 127 128 if(!actionPerformed) 129 throw new RuntimeException("Clicking on the left top of button did not trigger action event"); 130 131 actionPerformed = false; 132 robot.mouseMove(bottomRightX, bottomRightY); 133 robot.delay(500); 134 robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); 135 robot.delay(500); 136 robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); 137 robot.waitForIdle(3000); 138 139 if(!actionPerformed) 140 throw new RuntimeException("Clicking on the bottom right of button did not trigger action event"); 141 } 142 143 private void doTest() throws Exception { 144 robot.waitForIdle(); 145 changeGridSize(); 146 resizeFrame(); 147 148 int availableWidth = width - frame.getInsets().left - 149 frame.getInsets().right; 150 int componentWidth = (availableWidth + hGap) / columns - hGap; 151 int availableHeight = height - frame.getInsets().top - 152 frame.getInsets().bottom; 153 int componentHeight = (availableHeight + vGap) / rows - vGap; 154 155 for (int i = 0; i < buttons.length; i++) { 156 if (buttons[i].getSize().width != componentWidth || 157 buttons[i].getSize().height != componentHeight) { 158 throw new RuntimeException( 159 "FAIL: Button " + i + " not of proper size" + 160 "Expected: " + componentWidth + "*" + componentHeight + 161 "Actual: " + buttons[i].getSize().width + "*" + buttons[i].getSize().height); 162 } 163 } 164 165 // Components are visible. They should trigger events. 166 // Now you can check for the actual size shown. 167 int currentRow = 1; 168 int currentColumn = 0; 169 for (int i = 0; i < buttons.length; i++) { 170 currentColumn++; 171 if (currentColumn > columns) { 172 currentColumn = 1; 173 currentRow++; 174 } 175 176 int topPosX = frame.getLocationOnScreen().x + 177 frame.getInsets().left + 178 (currentColumn - 1) * (componentWidth + hGap); 179 int topPosY = frame.getLocationOnScreen().y + 180 frame.getInsets().top + 181 (currentRow - 1) * (componentHeight + vGap); 182 183 int bottomPosX = topPosX + componentWidth - 1; 184 int bottomPosY = topPosY + componentHeight - 1; 185 testBoundaries(topPosX, topPosY, bottomPosX, bottomPosY); 186 } 187 188 frame.dispose(); 189 } 190 }