1 /*
   2  * Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.awt.*;
  25 import java.awt.event.InputEvent;
  26 
  27 /*
  28  * @test
  29  * @summary Have different components having different preferred sizes
  30  *          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 location
  33  *          depending on the preferred sizes and gaps and click the cornors
  34  *          of the components to check if events are triggered
  35  * @library ../../../../lib/testlibrary/
  36  * @run main ComponentPreferredSize
  37  * @run main ComponentPreferredSize -hg 20 -vg 20
  38  */
  39 
  40 public class ComponentPreferredSize {
  41 
  42     private int width = 300;
  43     private int height = 200;
  44     private final int hGap, vGap;
  45     private final int rows = 3;
  46     private final int columns = 2;
  47     private final int componentCount = 6;
  48 
  49     private Button[] buttons;
  50     private Frame frame;
  51 
  52     private Robot robot;
  53     private GridLayout layout;
  54 
  55     private volatile boolean actionPerformed = false;
  56 
  57     public ComponentPreferredSize(int hGap, int vGap) throws Exception {
  58         this.hGap = hGap;
  59         this.vGap = vGap;
  60         robot = new Robot();
  61         EventQueue.invokeAndWait( () -> {
  62             frame = new Frame("Test frame");
  63             frame.setSize(width, height);
  64             layout = new GridLayout(rows, columns, hGap, vGap);
  65             frame.setLayout(layout);
  66 
  67             buttons = new Button[componentCount];
  68             for (int i = 0; i < componentCount; i++) {
  69                 buttons[i] = new Button("Button" + i);
  70                 buttons[i].setPreferredSize(new Dimension((int) Math.random() * 100,
  71                         (int) Math.random() * 100));
  72                 frame.add(buttons[i]);
  73                 buttons[i].addActionListener((event) -> {actionPerformed = true;});
  74             }
  75 
  76             frame.setVisible(true);
  77         });
  78     }
  79 
  80     public static void main(String[] args) throws Exception {
  81         int hGap = 0;
  82         int vGap = 0;
  83         for (int i = 0; i < args.length; i++) {
  84             switch (args[i]) {
  85                 case "-hg":
  86                     hGap = Integer.parseInt(args[++i]);
  87                     break;
  88                 case "-vg":
  89                     vGap = Integer.parseInt(args[++i]);
  90                     break;
  91             }
  92         }
  93         new ComponentPreferredSize(hGap, vGap).doTest();
  94     }
  95 
  96     private void resizeFrame() throws Exception {
  97         EventQueue.invokeAndWait(() -> {
  98             Insets insets = frame.getInsets();
  99             double dH = (height-insets.top-insets.bottom - vGap*(rows-1)) % rows;
 100             double dW = (width-insets.left-insets.right - hGap*(columns-1)) % columns;
 101             height -= dH;
 102             width -= dW;
 103             frame.setSize(width, height);
 104             frame.revalidate();
 105         });
 106         robot.waitForIdle();
 107     }
 108 
 109     public void testBoundaries(int topLeftX, int topLeftY, int bottomRightX, int bottomRightY) throws Exception {
 110 
 111         actionPerformed = false;
 112         robot.mouseMove(topLeftX, topLeftY);
 113         robot.delay(500);
 114         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 115         robot.delay(500);
 116         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 117         robot.delay(3000);
 118 
 119         if (!actionPerformed) {
 120             frame.dispose();
 121             throw new RuntimeException("Clicking on the left top of button did not trigger action event");
 122         }
 123 
 124         actionPerformed = false;
 125         robot.mouseMove(bottomRightX, bottomRightY);
 126         robot.delay(500);
 127         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 128         robot.delay(500);
 129         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 130         robot.delay(3000);
 131 
 132         if (!actionPerformed) {
 133             frame.dispose();
 134             throw new RuntimeException("Clicking on the bottom right of button did not trigger action event");
 135         }
 136     }
 137 
 138     private void doTest() throws Exception {
 139         robot.waitForIdle();
 140         resizeFrame();
 141 
 142         int availableWidth = width - frame.getInsets().left -
 143                 frame.getInsets().right;
 144         int componentWidth = (availableWidth + hGap) / columns - hGap;
 145         int availableHeight = height - frame.getInsets().top -
 146                 frame.getInsets().bottom;
 147         int componentHeight = (availableHeight + vGap) / rows - vGap;
 148 
 149         for (int i = 0; i < buttons.length; i++) {
 150             if (buttons[i].getSize().width != componentWidth ||
 151                     buttons[i].getSize().height != componentHeight) {
 152                 frame.dispose();
 153                 throw 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         }
 159 
 160         // Components are visible. They should trigger events.
 161         // Now you can check for the actual size shown.
 162         int currentRow = 1;
 163         int currentColumn = 0;
 164         for (int i = 0; i < buttons.length; i++) {
 165             currentColumn++;
 166             if (currentColumn > columns) {
 167                 currentColumn = 1;
 168                 currentRow++;
 169             }
 170 
 171             int topPosX = frame.getLocationOnScreen().x +
 172                     frame.getInsets().left +
 173                     (currentColumn - 1) * (componentWidth + hGap);
 174             int topPosY = frame.getLocationOnScreen().y +
 175                     frame.getInsets().top +
 176                     (currentRow - 1) * (componentHeight + vGap);
 177 
 178             int bottomPosX = topPosX + componentWidth - 1;
 179             int bottomPosY = topPosY + componentHeight - 1;
 180             testBoundaries(topPosX, topPosY, bottomPosX, bottomPosY);
 181         }
 182 
 183         frame.dispose();
 184     }
 185 }