< prev index next >

test/java/awt/GridLayout/ChangeGridSize/ChangeGridSize.java

Print this page


   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 /*
  28  * @test
  29  * @summary Have different components having different preferred sizes
  30  *          added to a grid layout. Change the rows and columns of the
  31  *          grid layout and check the components are re-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  * @build ExtendedRobot
  37  * @run main ChangeGridSize
  38  * @run main ChangeGridSize -hg 20 -vg 20
  39  */
  40 
  41 public class ChangeGridSize {
  42 
  43     private int width = 200;
  44     private int height = 200;
  45     private final int hGap, vGap;
  46     private final int rows = 3;
  47     private final int columns = 2;
  48     private final int componentCount = 6;
  49 
  50     private Button[] buttons;
  51     private Frame frame;
  52 
  53     private ExtendedRobot robot;
  54     private GridLayout layout;
  55 
  56     private volatile boolean actionPerformed = false;
  57 
  58     public ChangeGridSize(int hGap, int vGap) throws Exception  {
  59         this.hGap = hGap;
  60         this.vGap = vGap;
  61         robot = new ExtendedRobot();
  62         EventQueue.invokeAndWait( () -> {
  63             frame = new Frame("Test frame");
  64             frame.setSize(width, height);
  65             layout = new GridLayout(rows + 3, columns - 1, hGap, vGap);
  66             frame.setLayout(layout);
  67 
  68             buttons = new Button[componentCount];
  69             for (int i = 0; i < componentCount; i++) {
  70                 buttons[i] = new Button("Button" + i);
  71                 frame.add(buttons[i]);
  72                 buttons[i].addActionListener( (event) -> { actionPerformed = true; });
  73             }
  74             frame.setVisible(true);
  75         });
  76     }
  77 
  78     public static void main(String[] args) throws Exception {
  79         int hGap = 0;
  80         int vGap = 0;
  81         for (int i = 0; i < args.length; i++) {


 105     }
 106 
 107     private void changeGridSize() throws Exception {
 108         EventQueue.invokeAndWait(() -> {
 109             layout.setRows(rows);
 110             layout.setColumns(columns);
 111 
 112             frame.revalidate();
 113         });
 114         robot.waitForIdle();
 115     }
 116 
 117     public void testBoundaries(int topLeftX, int topLeftY, int bottomRightX, int bottomRightY) throws Exception {
 118 
 119         actionPerformed = false;
 120         robot.mouseMove(topLeftX, topLeftY);
 121         robot.delay(500);
 122         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 123         robot.delay(500);
 124         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 125         robot.waitForIdle(3000);
 126 
 127         if(!actionPerformed)

 128             throw new RuntimeException("Clicking on the left top of button did not trigger action event");

 129 
 130         actionPerformed = false;
 131         robot.mouseMove(bottomRightX, bottomRightY);
 132         robot.delay(500);
 133         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 134         robot.delay(500);
 135         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 136         robot.waitForIdle(3000);
 137 
 138         if(!actionPerformed)

 139             throw new RuntimeException("Clicking on the bottom right of button did not trigger action event");
 140     }

 141 
 142     private void doTest() throws Exception {
 143         robot.waitForIdle();
 144         changeGridSize();
 145         resizeFrame();
 146 
 147         int availableWidth = width - frame.getInsets().left -
 148                 frame.getInsets().right;
 149         int componentWidth = (availableWidth + hGap) / columns - hGap;
 150         int availableHeight = height - frame.getInsets().top -
 151                 frame.getInsets().bottom;
 152         int componentHeight = (availableHeight + vGap) / rows - vGap;
 153 
 154         for (int i = 0; i < buttons.length; i++) {
 155             if (buttons[i].getSize().width != componentWidth ||
 156                     buttons[i].getSize().height != componentHeight) {

 157                 throw new RuntimeException(
 158                         "FAIL: Button " + i + " not of proper size" +
 159                         "Expected: " + componentWidth + "*" + componentHeight +
 160                         "Actual: " + buttons[i].getSize().width + "*" + buttons[i].getSize().height);
 161             }
 162         }
 163 
 164         // Components are visible. They should trigger events.
 165         // Now you can check for the actual size shown.
 166         int currentRow = 1;
 167         int currentColumn = 0;
 168         for (int i = 0; i < buttons.length; i++) {
 169             currentColumn++;
 170             if (currentColumn > columns) {
 171                 currentColumn = 1;
 172                 currentRow++;
 173             }
 174 
 175             int topPosX = frame.getLocationOnScreen().x +
 176                     frame.getInsets().left +
   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.*;
  26 
  27 /*
  28  * @test
  29  * @summary Have different components having different preferred sizes
  30  *          added to a grid layout. Change the rows and columns of the
  31  *          grid layout and check the components are re-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 ChangeGridSize
  37  * @run main ChangeGridSize -hg 20 -vg 20
  38  */
  39 
  40 public class ChangeGridSize {
  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 ChangeGridSize(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 + 3, columns - 1, 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                 frame.add(buttons[i]);
  71                 buttons[i].addActionListener( (event) -> { actionPerformed = true; });
  72             }
  73             frame.setVisible(true);
  74         });
  75     }
  76 
  77     public static void main(String[] args) throws Exception {
  78         int hGap = 0;
  79         int vGap = 0;
  80         for (int i = 0; i < args.length; i++) {


 104     }
 105 
 106     private void changeGridSize() throws Exception {
 107         EventQueue.invokeAndWait(() -> {
 108             layout.setRows(rows);
 109             layout.setColumns(columns);
 110 
 111             frame.revalidate();
 112         });
 113         robot.waitForIdle();
 114     }
 115 
 116     public void testBoundaries(int topLeftX, int topLeftY, int bottomRightX, int bottomRightY) throws Exception {
 117 
 118         actionPerformed = false;
 119         robot.mouseMove(topLeftX, topLeftY);
 120         robot.delay(500);
 121         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 122         robot.delay(500);
 123         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 124         robot.delay(3000);
 125 
 126         if (!actionPerformed) {
 127             frame.dispose();
 128             throw new RuntimeException("Clicking on the left top of button did not trigger action event");
 129         }
 130 
 131         actionPerformed = false;
 132         robot.mouseMove(bottomRightX, bottomRightY);
 133         robot.delay(500);
 134         robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
 135         robot.delay(500);
 136         robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
 137         robot.delay(3000);
 138 
 139         if (!actionPerformed) {
 140             frame.dispose();
 141             throw new RuntimeException("Clicking on the bottom right of button did not trigger action event");
 142         }
 143     }
 144 
 145     private void doTest() throws Exception {
 146         robot.waitForIdle();
 147         changeGridSize();
 148         resizeFrame();
 149 
 150         int availableWidth = width - frame.getInsets().left -
 151                 frame.getInsets().right;
 152         int componentWidth = (availableWidth + hGap) / columns - hGap;
 153         int availableHeight = height - frame.getInsets().top -
 154                 frame.getInsets().bottom;
 155         int componentHeight = (availableHeight + vGap) / rows - vGap;
 156 
 157         for (int i = 0; i < buttons.length; i++) {
 158             if (buttons[i].getSize().width != componentWidth ||
 159                     buttons[i].getSize().height != componentHeight) {
 160                 frame.dispose();
 161                 throw 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         }
 167 
 168         // Components are visible. They should trigger events.
 169         // Now you can check for the actual size shown.
 170         int currentRow = 1;
 171         int currentColumn = 0;
 172         for (int i = 0; i < buttons.length; i++) {
 173             currentColumn++;
 174             if (currentColumn > columns) {
 175                 currentColumn = 1;
 176                 currentRow++;
 177             }
 178 
 179             int topPosX = frame.getLocationOnScreen().x +
 180                     frame.getInsets().left +
< prev index next >