Path: blob/master/test/jdk/java/awt/Component/SetComponentsBounds/SetComponentsBounds.java
41154 views
/*1* Copyright (c) 2020, 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.Button;24import java.awt.Canvas;25import java.awt.Checkbox;26import java.awt.Choice;27import java.awt.Component;28import java.awt.Frame;29import java.awt.GraphicsConfiguration;30import java.awt.GraphicsDevice;31import java.awt.GraphicsEnvironment;32import java.awt.Label;33import java.awt.List;34import java.awt.Rectangle;35import java.awt.Robot;36import java.awt.ScrollPane;37import java.awt.Scrollbar;38import java.awt.TextArea;39import java.awt.TextField;40import java.awt.Window;4142/**43* @test44* @key headful45* @bug 821199946* @run main/othervm SetComponentsBounds47* @run main/othervm -Dsun.java2d.uiScale=1 SetComponentsBounds48* @run main/othervm -Dsun.java2d.uiScale=2.25 SetComponentsBounds49*/50public final class SetComponentsBounds {5152private static final int X = 111;53private static final int Y = 222;54private static final int WIDTH = 321;55private static final int HEIGHT = 123;5657public static void main(String[] args) throws Exception {58var ge = GraphicsEnvironment.getLocalGraphicsEnvironment();59for (GraphicsDevice gd : ge.getScreenDevices()) {60test(gd.getDefaultConfiguration(), true);61test(gd.getDefaultConfiguration(), false);62}63}6465private static void test(GraphicsConfiguration gc, boolean visible) throws Exception {66Rectangle screen = gc.getBounds();67Window frame = new Frame();68try {69frame.setLayout(null); // trigger use the minimum size of70// the peer71frame.setBounds(screen.x + 100, screen.y + 100, 500, 500);72frame.add(new Button());73frame.add(new Canvas());74frame.add(new Checkbox());75frame.add(new Choice());76frame.add(new Label());77frame.add(new List());78frame.add(new Scrollbar());79frame.add(new ScrollPane());80frame.add(new TextArea());81frame.add(new TextField());82for (Component comp : frame.getComponents()) {83comp.setBounds(X, Y, WIDTH, HEIGHT);84}85if (visible) {86frame.setVisible(true);87} else {88frame.pack();89}90Robot robot = new Robot();91robot.waitForIdle();92checkGC(gc, frame);93for (Component comp : frame.getComponents()) {94Rectangle bounds = comp.getBounds();95if (bounds.x != X || bounds.y != Y || bounds.width != WIDTH) {96System.err.println("Screen bounds:" + screen);97System.err.println("Component:" + comp);98throw new RuntimeException("Wrong bounds:" + bounds);99}100if (bounds.height > HEIGHT) {101// different check for HEIGHT, it depends on the font102throw new RuntimeException("Wrong height:" + bounds.height);103}104checkGC(gc, comp);105}106} finally {107frame.dispose();108}109}110111private static void checkGC(GraphicsConfiguration gc, Component comp) {112GraphicsConfiguration compGC = comp.getGraphicsConfiguration();113if (compGC != gc) {114System.err.println("Expected GC:" + gc);115System.err.println("Actual GC:" + compGC);116throw new RuntimeException();117}118}119}120121122