Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/GridLayout/ChangeGridSize/ChangeGridSize.java
38828 views
/*1* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.*;24import java.awt.event.*;2526/*27* @test28* @summary Have different components having different preferred sizes29* added to a grid layout. Change the rows and columns of the30* grid layout and check the components are re-laid out.31* The strategy followed is to calculate the component location32* depending on the preferred sizes and gaps and click the cornors33* of the components to check if events are triggered34* @library ../../../../lib/testlibrary/35* @build ExtendedRobot36* @run main ChangeGridSize37* @run main ChangeGridSize -hg 20 -vg 2038*/3940public class ChangeGridSize {4142private int width = 200;43private int height = 200;44private final int hGap, vGap;45private final int rows = 3;46private final int columns = 2;47private final int componentCount = 6;4849private Button[] buttons;50private Frame frame;5152private ExtendedRobot robot;53private GridLayout layout;5455private volatile boolean actionPerformed = false;5657public ChangeGridSize(int hGap, int vGap) throws Exception {58this.hGap = hGap;59this.vGap = vGap;60robot = new ExtendedRobot();61EventQueue.invokeAndWait( () -> {62frame = new Frame("Test frame");63frame.setSize(width, height);64layout = new GridLayout(rows + 3, columns - 1, hGap, vGap);65frame.setLayout(layout);6667buttons = new Button[componentCount];68for (int i = 0; i < componentCount; i++) {69buttons[i] = new Button("Button" + i);70frame.add(buttons[i]);71buttons[i].addActionListener( (event) -> { actionPerformed = true; });72}73frame.setVisible(true);74});75}7677public static void main(String[] args) throws Exception {78int hGap = 0;79int vGap = 0;80for (int i = 0; i < args.length; i++) {81switch (args[i]) {82case "-hg":83hGap = Integer.parseInt(args[++i]);84break;85case "-vg":86vGap = Integer.parseInt(args[++i]);87break;88}89}90new ChangeGridSize(hGap, vGap).doTest();91}9293private void resizeFrame() throws Exception {94EventQueue.invokeAndWait(() -> {95Insets insets = frame.getInsets();96double dH = (height-insets.top-insets.bottom - vGap*(rows-1)) % rows;97double dW = (width-insets.left-insets.right - hGap*(columns-1)) % columns;98height -= dH;99width -= dW;100frame.setSize(width, height);101frame.revalidate();102});103robot.waitForIdle();104}105106private void changeGridSize() throws Exception {107EventQueue.invokeAndWait(() -> {108layout.setRows(rows);109layout.setColumns(columns);110111frame.revalidate();112});113robot.waitForIdle();114}115116public void testBoundaries(int topLeftX, int topLeftY, int bottomRightX, int bottomRightY) throws Exception {117118actionPerformed = false;119robot.mouseMove(topLeftX, topLeftY);120robot.delay(500);121robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);122robot.delay(500);123robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);124robot.waitForIdle(3000);125126if(!actionPerformed)127throw new RuntimeException("Clicking on the left top of button did not trigger action event");128129actionPerformed = false;130robot.mouseMove(bottomRightX, bottomRightY);131robot.delay(500);132robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);133robot.delay(500);134robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);135robot.waitForIdle(3000);136137if(!actionPerformed)138throw new RuntimeException("Clicking on the bottom right of button did not trigger action event");139}140141private void doTest() throws Exception {142robot.waitForIdle();143changeGridSize();144resizeFrame();145146int availableWidth = width - frame.getInsets().left -147frame.getInsets().right;148int componentWidth = (availableWidth + hGap) / columns - hGap;149int availableHeight = height - frame.getInsets().top -150frame.getInsets().bottom;151int componentHeight = (availableHeight + vGap) / rows - vGap;152153for (int i = 0; i < buttons.length; i++) {154if (buttons[i].getSize().width != componentWidth ||155buttons[i].getSize().height != componentHeight) {156throw new RuntimeException(157"FAIL: Button " + i + " not of proper size" +158"Expected: " + componentWidth + "*" + componentHeight +159"Actual: " + buttons[i].getSize().width + "*" + buttons[i].getSize().height);160}161}162163// Components are visible. They should trigger events.164// Now you can check for the actual size shown.165int currentRow = 1;166int currentColumn = 0;167for (int i = 0; i < buttons.length; i++) {168currentColumn++;169if (currentColumn > columns) {170currentColumn = 1;171currentRow++;172}173174int topPosX = frame.getLocationOnScreen().x +175frame.getInsets().left +176(currentColumn - 1) * (componentWidth + hGap);177int topPosY = frame.getLocationOnScreen().y +178frame.getInsets().top +179(currentRow - 1) * (componentHeight + vGap);180181int bottomPosX = topPosX + componentWidth - 1;182int bottomPosY = topPosY + componentHeight - 1;183testBoundaries(topPosX, topPosY, bottomPosX, bottomPosY);184}185186frame.dispose();187}188}189190191