Path: blob/master/test/jdk/java/awt/GridLayout/ChangeGridSize/ChangeGridSize.java
41153 views
/*1* Copyright (c) 2006, 2016, 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* @key headful29* @summary Have different components having different preferred sizes30* added to a grid layout. Change the rows and columns of the31* grid layout and check the components are re-laid out.32* The strategy followed is to calculate the component location33* depending on the preferred sizes and gaps and click the cornors34* of the components to check if events are triggered35* @library ../../../../lib/testlibrary/36* @run main ChangeGridSize37* @run main ChangeGridSize -hg 20 -vg 2038*/3940public class ChangeGridSize {4142private int width = 300;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 Robot 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 Robot();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.delay(3000);125126if (!actionPerformed) {127frame.dispose();128throw new RuntimeException("Clicking on the left top of button did not trigger action event");129}130131actionPerformed = false;132robot.mouseMove(bottomRightX, bottomRightY);133robot.delay(500);134robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);135robot.delay(500);136robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);137robot.delay(3000);138139if (!actionPerformed) {140frame.dispose();141throw new RuntimeException("Clicking on the bottom right of button did not trigger action event");142}143}144145private void doTest() throws Exception {146robot.waitForIdle();147changeGridSize();148resizeFrame();149150int availableWidth = width - frame.getInsets().left -151frame.getInsets().right;152int componentWidth = (availableWidth + hGap) / columns - hGap;153int availableHeight = height - frame.getInsets().top -154frame.getInsets().bottom;155int componentHeight = (availableHeight + vGap) / rows - vGap;156157for (int i = 0; i < buttons.length; i++) {158if (buttons[i].getSize().width != componentWidth ||159buttons[i].getSize().height != componentHeight) {160frame.dispose();161throw new RuntimeException(162"FAIL: Button " + i + " not of proper size" +163"Expected: " + componentWidth + "*" + componentHeight +164"Actual: " + buttons[i].getSize().width + "*" + buttons[i].getSize().height);165}166}167168// Components are visible. They should trigger events.169// Now you can check for the actual size shown.170int currentRow = 1;171int currentColumn = 0;172for (int i = 0; i < buttons.length; i++) {173currentColumn++;174if (currentColumn > columns) {175currentColumn = 1;176currentRow++;177}178179int topPosX = frame.getLocationOnScreen().x +180frame.getInsets().left +181(currentColumn - 1) * (componentWidth + hGap);182int topPosY = frame.getLocationOnScreen().y +183frame.getInsets().top +184(currentRow - 1) * (componentHeight + vGap);185186int bottomPosX = topPosX + componentWidth - 1;187int bottomPosY = topPosY + componentHeight - 1;188testBoundaries(topPosX, topPosY, bottomPosX, bottomPosY);189}190191frame.dispose();192}193}194195196