--- /dev/null 2014-04-08 13:33:32.287183578 +0400 +++ new/test/java/awt/GridLayout/ChangeGridSize/ChangeGridSize.java 2014-04-10 14:45:44.053184220 +0400 @@ -0,0 +1,187 @@ +/* + * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import javax.swing.*; +import java.awt.*; +import java.awt.event.InputEvent; +import static jdk.testlibrary.Asserts.assertTrue; + +/* + * @test + * @summary Have different components having different preferred sizes + * added to a grid layout. Change the rows and columns of the + * grid layout and check the components are re-laid out. + * The strategy followed is to calculate the component location + * depending on the preferred sizes and gaps and click the cornors + * of the components to check if events are triggered + * @library ../../../../lib/testlibrary/ + * @build ExtendedRobot jdk.testlibrary.Asserts + * @run main ChangeGridSize + * @run main ChangeGridSize -hg 20 -vg 20 + */ + +public class ChangeGridSize { + + private int hGap, vGap; + private int width = 200; + private int height = 200; + private int rows = 3; + private int columns = 2; + private int componentCount = 6; + + private JButton[] buttons; + private JFrame frame; + + private ExtendedRobot robot; + private GridLayout layout; + + private boolean actionPerformed = false; + + public ChangeGridSize(int hGap, int vGap) throws Exception { + this.hGap = hGap; + this.vGap = vGap; + robot = new ExtendedRobot(); + SwingUtilities.invokeAndWait( () -> { + frame = new JFrame("Test frame"); + frame.setSize(width, height); + layout = new GridLayout(rows + 3, columns - 1, hGap, vGap); + frame.getContentPane().setLayout(layout); + + buttons = new JButton[componentCount]; + for (int i = 0; i < componentCount; i++) { + buttons[i] = new JButton("Button" + i); + frame.getContentPane().add(buttons[i]); + buttons[i].addActionListener( (event) -> { actionPerformed = true; }); + } + + frame.setVisible(true); + }); + } + + public static void main(String[] args) throws Exception { + int hGap = 0; + int vGap = 0; + for (int i = 0; i < args.length; i++) { + switch (args[i]) { + case "-hg": + hGap = Integer.parseInt(args[++i]); + break; + case "-vg": + vGap = Integer.parseInt(args[++i]); + break; + } + } + new ChangeGridSize(hGap, vGap).doTest(); + } + + private void resizeFrame() throws Exception { + SwingUtilities.invokeAndWait( () -> { + double dH = (frame.getContentPane().getSize().getHeight() - vGap*(rows-1)) % rows; + height -= dH; + frame.setSize(width, height); + frame.getContentPane().revalidate(); + }); + robot.waitForIdle(); + } + + private void changeGridSize() throws Exception { + SwingUtilities.invokeAndWait(() -> { + layout.setRows(rows); + layout.setColumns(columns); + + frame.getContentPane().revalidate(); + }); + robot.waitForIdle(); + } + + public void testBoundaries(int topLeftX, int topLeftY, int bottomRightX, int bottomRightY) throws Exception { + + actionPerformed = false; + robot.mouseMove(topLeftX, topLeftY); + robot.delay(500); + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); + robot.delay(500); + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); + robot.delay(3000); + robot.waitForIdle(); + + assertTrue(actionPerformed, "Clicking on the left top of button did not trigger action event"); + + actionPerformed = false; + robot.mouseMove(bottomRightX, bottomRightY); + robot.delay(500); + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); + robot.delay(500); + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); + robot.delay(3000); + robot.waitForIdle(); + + assertTrue(actionPerformed, "Clicking on the bottom right of button did not trigger action event"); + } + + private void doTest() throws Exception { + robot.waitForIdle(); + changeGridSize(); + resizeFrame(); + + int availableWidth = width - frame.getInsets().left - + frame.getInsets().right; + int componentWidth = (availableWidth + hGap) / columns - hGap; + int availableHeight = height - frame.getInsets().top - + frame.getInsets().bottom; + int componentHeight = (availableHeight + vGap) / rows - vGap; + + for (int i = 0; i < buttons.length; i++) { + if (buttons[i].getSize().width != componentWidth || + buttons[i].getSize().height != componentHeight) { + throw new RuntimeException( + "FAIL: Button " + i + " not of proper size" + + "Expected: " + componentWidth + "*" + componentHeight + + "Actual: " + buttons[i].getSize().width + "*" + buttons[i].getSize().height); + } + } + + // Components are visible. They should trigger events. + // Now you can check for the actual size shown. + int currentRow = 1; + int currentColumn = 0; + for (int i = 0; i < buttons.length; i++) { + currentColumn++; + if (currentColumn > columns) { + currentColumn = 1; + currentRow++; + } + + int topPosX = frame.getLocationOnScreen().x + + frame.getInsets().left + + (currentColumn - 1) * (componentWidth + hGap); + int topPosY = frame.getLocationOnScreen().y + + frame.getInsets().top + + (currentRow - 1) * (componentHeight + vGap); + + int bottomPosX = topPosX + componentWidth - 1; + int bottomPosY = topPosY + componentHeight - 1; + testBoundaries(topPosX, topPosY, bottomPosX, bottomPosY); + } + } +}