Path: blob/master/test/jdk/java/awt/GridLayout/ComponentPreferredSize/ComponentPreferredSize.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.InputEvent;2526/*27* @test28* @key headful29* @summary Have different components having different preferred sizes30* added to a grid layout having various values of row/columns.31* Check if the compnents are correctly 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 ComponentPreferredSize37* @run main ComponentPreferredSize -hg 20 -vg 2038*/3940public class ComponentPreferredSize {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 ComponentPreferredSize(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, 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.delay(3000);118119if (!actionPerformed) {120frame.dispose();121throw new RuntimeException("Clicking on the left top of button did not trigger action event");122}123124actionPerformed = false;125robot.mouseMove(bottomRightX, bottomRightY);126robot.delay(500);127robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);128robot.delay(500);129robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);130robot.delay(3000);131132if (!actionPerformed) {133frame.dispose();134throw new RuntimeException("Clicking on the bottom right of button did not trigger action event");135}136}137138private void doTest() throws Exception {139robot.waitForIdle();140resizeFrame();141142int availableWidth = width - frame.getInsets().left -143frame.getInsets().right;144int componentWidth = (availableWidth + hGap) / columns - hGap;145int availableHeight = height - frame.getInsets().top -146frame.getInsets().bottom;147int componentHeight = (availableHeight + vGap) / rows - vGap;148149for (int i = 0; i < buttons.length; i++) {150if (buttons[i].getSize().width != componentWidth ||151buttons[i].getSize().height != componentHeight) {152frame.dispose();153throw new RuntimeException(154"FAIL: Button " + i + " not of proper size" +155"Expected: " + componentWidth + "*" + componentHeight +156"Actual: " + buttons[i].getSize().width + "*" + buttons[i].getSize().height);157}158}159160// Components are visible. They should trigger events.161// Now you can check for the actual size shown.162int currentRow = 1;163int currentColumn = 0;164for (int i = 0; i < buttons.length; i++) {165currentColumn++;166if (currentColumn > columns) {167currentColumn = 1;168currentRow++;169}170171int topPosX = frame.getLocationOnScreen().x +172frame.getInsets().left +173(currentColumn - 1) * (componentWidth + hGap);174int topPosY = frame.getLocationOnScreen().y +175frame.getInsets().top +176(currentRow - 1) * (componentHeight + vGap);177178int bottomPosX = topPosX + componentWidth - 1;179int bottomPosY = topPosY + componentHeight - 1;180testBoundaries(topPosX, topPosY, bottomPosX, bottomPosY);181}182183frame.dispose();184}185}186187188