1 /*
   2  * Copyright (c) 2006, 2014, 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.*;
  26 
  27 import static jdk.testlibrary.Asserts.assertTrue;
  28 
  29 /*
  30  * @test
  31  * @summary Have different components having different preferred sizes
  32  *          added to a grid layout. Change the rows and columns of the
  33  *          grid layout and check the components are re-laid out.
  34  *          The strategy followed is to calculate the component location
  35  *          depending on the preferred sizes and gaps and click the cornors
  36  *          of the components to check if events are triggered
  37  * @library ../../../../lib/testlibrary/
  38  * @build ExtendedRobot jdk.testlibrary.Asserts
  39  * @run main ChangeGridSize
  40  * @run main ChangeGridSize -hg 20 -vg 20
  41  */
  42 
  43 public class ChangeGridSize {
  44 
  45     private int hGap, vGap;
  46     private int width = 200;
  47     private int height = 200;
  48     private int rows = 3;
  49     private int columns = 2;
  50     private int componentCount = 6;
  51 
  52     private Button[] buttons;
  53     private Frame frame;
  54 
  55     private ExtendedRobot robot;
  56     private GridLayout layout;
  57 
  58     private boolean actionPerformed = false;
  59 
  60     public ChangeGridSize(int hGap, int vGap) throws Exception  {
  61         this.hGap = hGap;
  62         this.vGap = vGap;
  63         robot = new ExtendedRobot();
  64         EventQueue.invokeAndWait( () -> {
  65             frame = new Frame("Test frame");
  66             frame.setSize(width, height);
  67             layout = new GridLayout(rows + 3, columns - 1, hGap, vGap);
  68             frame.setLayout(layout);
  69 
  70             buttons = new Button[componentCount];
  71             for (int i = 0; i < componentCount; i++) {
  72                 buttons[i] = new Button("Button" + i);
  73                 frame.add(buttons[i]);
  74                 buttons[i].addActionListener( (event) -> { actionPerformed = true; });
  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 ChangeGridSize(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     private void changeGridSize() throws Exception {
 110         EventQueue.invokeAndWait(() -> {
 111             layout.setRows(rows);
 112             layout.setColumns(columns);
 113 
 114             frame.revalidate();
 115         });
 116         robot.waitForIdle();
 117     }
 118 
 119     public void testBoundaries(int topLeftX, int topLeftY, int bottomRightX, int bottomRightY) throws Exception {
 120 
 121         actionPerformed = false;
 122         robot.mouseMove(topLeftX, topLeftY);
 123         robot.delay(500);
 124         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 125         robot.delay(500);
 126         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 127         robot.delay(3000);
 128         robot.waitForIdle();
 129 
 130         assertTrue(actionPerformed, "Clicking on the left top of button triggered action event");
 131 
 132         actionPerformed = false;
 133         robot.mouseMove(bottomRightX, bottomRightY);
 134         robot.delay(500);
 135         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 136         robot.delay(500);
 137         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 138         robot.delay(3000);
 139         robot.waitForIdle();
 140 
 141         assertTrue(actionPerformed, "Clicking on the bottom right of button triggered action event");
 142     }
 143 
 144     private void doTest() throws Exception {
 145         robot.waitForIdle();
 146         changeGridSize();
 147         resizeFrame();
 148 
 149         int availableWidth = width - frame.getInsets().left -
 150                 frame.getInsets().right;
 151         int componentWidth = (availableWidth + hGap) / columns - hGap;
 152         int availableHeight = height - frame.getInsets().top -
 153                 frame.getInsets().bottom;
 154         int componentHeight = (availableHeight + vGap) / rows - vGap;
 155 
 156         for (int i = 0; i < buttons.length; i++) {
 157             if (buttons[i].getSize().width != componentWidth ||
 158                     buttons[i].getSize().height != componentHeight) {
 159                 throw new RuntimeException(
 160                         "FAIL: Button " + i + " not of proper size" +
 161                         "Expected: " + componentWidth + "*" + componentHeight +
 162                         "Actual: " + buttons[i].getSize().width + "*" + buttons[i].getSize().height);
 163             }
 164         }
 165 
 166         // Components are visible. They should trigger events.
 167         // Now you can check for the actual size shown.
 168         int currentRow = 1;
 169         int currentColumn = 0;
 170         for (int i = 0; i < buttons.length; i++) {
 171             currentColumn++;
 172             if (currentColumn > columns) {
 173                 currentColumn = 1;
 174                 currentRow++;
 175             }
 176 
 177             int topPosX = frame.getLocationOnScreen().x +
 178                     frame.getInsets().left +
 179                     (currentColumn - 1) * (componentWidth + hGap);
 180             int topPosY = frame.getLocationOnScreen().y +
 181                     frame.getInsets().top +
 182                     (currentRow - 1) * (componentHeight + vGap);
 183 
 184             int bottomPosX = topPosX + componentWidth - 1;
 185             int bottomPosY = topPosY + componentHeight - 1;
 186             testBoundaries(topPosX, topPosY, bottomPosX, bottomPosY);
 187         }
 188     }
 189 }