Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/GridLayout/ComponentPreferredSize/ComponentPreferredSize.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.InputEvent;2526/*27* @test28* @summary Have different components having different preferred sizes29* added to a grid layout having various values of row/columns.30* Check if the compnents are correctly 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 ComponentPreferredSize37* @run main ComponentPreferredSize -hg 20 -vg 2038*/3940public class ComponentPreferredSize {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 ComponentPreferredSize(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, columns, hGap, vGap);65frame.setLayout(layout);6667buttons = new Button[componentCount];68for (int i = 0; i < componentCount; i++) {69buttons[i] = new Button("Button" + i);70buttons[i].setPreferredSize(new Dimension((int) Math.random() * 100,71(int) Math.random() * 100));72frame.add(buttons[i]);73buttons[i].addActionListener((event) -> {actionPerformed = true;});74}7576frame.setVisible(true);77});78}7980public static void main(String[] args) throws Exception {81int hGap = 0;82int vGap = 0;83for (int i = 0; i < args.length; i++) {84switch (args[i]) {85case "-hg":86hGap = Integer.parseInt(args[++i]);87break;88case "-vg":89vGap = Integer.parseInt(args[++i]);90break;91}92}93new ComponentPreferredSize(hGap, vGap).doTest();94}9596private void resizeFrame() throws Exception {97EventQueue.invokeAndWait(() -> {98Insets insets = frame.getInsets();99double dH = (height-insets.top-insets.bottom - vGap*(rows-1)) % rows;100double dW = (width-insets.left-insets.right - hGap*(columns-1)) % columns;101height -= dH;102width -= dW;103frame.setSize(width, height);104frame.revalidate();105});106robot.waitForIdle();107}108109public void testBoundaries(int topLeftX, int topLeftY, int bottomRightX, int bottomRightY) throws Exception {110111actionPerformed = false;112robot.mouseMove(topLeftX, topLeftY);113robot.delay(500);114robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);115robot.delay(500);116robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);117robot.waitForIdle(3000);118119if(!actionPerformed)120throw new RuntimeException("Clicking on the left top of button did not trigger action event");121122actionPerformed = false;123robot.mouseMove(bottomRightX, bottomRightY);124robot.delay(500);125robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);126robot.delay(500);127robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);128robot.waitForIdle(3000);129130if(!actionPerformed)131throw new RuntimeException("Clicking on the bottom right of button did not trigger action event");132}133134private void doTest() throws Exception {135robot.waitForIdle();136resizeFrame();137138int availableWidth = width - frame.getInsets().left -139frame.getInsets().right;140int componentWidth = (availableWidth + hGap) / columns - hGap;141int availableHeight = height - frame.getInsets().top -142frame.getInsets().bottom;143int componentHeight = (availableHeight + vGap) / rows - vGap;144145for (int i = 0; i < buttons.length; i++) {146if (buttons[i].getSize().width != componentWidth ||147buttons[i].getSize().height != componentHeight) {148throw new RuntimeException(149"FAIL: Button " + i + " not of proper size" +150"Expected: " + componentWidth + "*" + componentHeight +151"Actual: " + buttons[i].getSize().width + "*" + buttons[i].getSize().height);152}153}154155// Components are visible. They should trigger events.156// Now you can check for the actual size shown.157int currentRow = 1;158int currentColumn = 0;159for (int i = 0; i < buttons.length; i++) {160currentColumn++;161if (currentColumn > columns) {162currentColumn = 1;163currentRow++;164}165166int topPosX = frame.getLocationOnScreen().x +167frame.getInsets().left +168(currentColumn - 1) * (componentWidth + hGap);169int topPosY = frame.getLocationOnScreen().y +170frame.getInsets().top +171(currentRow - 1) * (componentHeight + vGap);172173int bottomPosX = topPosX + componentWidth - 1;174int bottomPosY = topPosY + componentHeight - 1;175testBoundaries(topPosX, topPosY, bottomPosX, bottomPosY);176}177178frame.dispose();179}180}181182183