Path: blob/master/test/jdk/javax/swing/JInternalFrame/8160248/JInternalFrameDraggingTest.java
41153 views
/*1* Copyright (c) 2016, 2018, 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.BorderLayout;24import java.awt.Color;25import java.awt.Point;26import java.awt.Rectangle;27import java.awt.Robot;28import java.awt.event.InputEvent;29import java.awt.image.BufferedImage;30import javax.swing.JDesktopPane;31import javax.swing.JFrame;32import javax.swing.JInternalFrame;33import javax.swing.SwingUtilities;3435/**36* @test37* @key headful38* @bug 8160248 8160332 818651339* @summary Dragged internal frame leaves artifacts for floating point ui scale40* @run main/othervm -Dsun.java2d.uiScale=1.2 JInternalFrameDraggingTest41* @run main/othervm -Dsun.java2d.uiScale=1.5 JInternalFrameDraggingTest42* @run main/othervm -Dsun.java2d.uiScale=1 JInternalFrameDraggingTest43* @run main/othervm -Dsun.java2d.uiScale=2.5 JInternalFrameDraggingTest44*/4546public class JInternalFrameDraggingTest {4748private static JFrame frame;49private static JDesktopPane desktopPane;50private static JInternalFrame internalFrame;51private static int FRAME_SIZE = 500;52private static Color BACKGROUND_COLOR = Color.ORANGE;5354public static void main(String[] args) throws Exception {55try {56Robot robot = new Robot();57robot.setAutoDelay(20);58SwingUtilities.invokeAndWait(JInternalFrameDraggingTest::createAndShowGUI);59robot.waitForIdle();6061final int translate = FRAME_SIZE / 4;62moveFrame(robot, translate, translate / 2, translate / 2);63robot.waitForIdle();6465Point p = getDesktopPaneLocation();66int size = translate / 2;67Rectangle rect = new Rectangle(p.x, p.y, size, size);68BufferedImage img = robot.createScreenCapture(rect);6970int testRGB = BACKGROUND_COLOR.getRGB();71for (int i = 1; i < size; i++) {72int rgbCW = img.getRGB(i, size / 2);73int rgbCH = img.getRGB(size / 2, i);74if (rgbCW != testRGB || rgbCH != testRGB) {75System.out.println("i " + i + " rgbCW " +76Integer.toHexString(rgbCW) +77" testRGB " + Integer.toHexString(testRGB) +78" rgbCH " + Integer.toHexString(rgbCH));79throw new RuntimeException("Background color is wrong!");80}81}82} finally {83if (frame != null) SwingUtilities.invokeAndWait(() -> frame.dispose());84}85}8687private static void createAndShowGUI() {8889frame = new JFrame();90frame.setUndecorated(true);91frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);92frame.setLayout(new BorderLayout());9394desktopPane = new JDesktopPane();95desktopPane.setBackground(BACKGROUND_COLOR);9697frame.add(desktopPane, BorderLayout.CENTER);98frame.setSize(FRAME_SIZE, FRAME_SIZE);99frame.setVisible(true);100101internalFrame = new JInternalFrame("Test");102internalFrame.setSize(FRAME_SIZE / 2, FRAME_SIZE / 2);103desktopPane.add(internalFrame);104internalFrame.setVisible(true);105internalFrame.setResizable(true);106107frame.setVisible(true);108}109110private static void moveFrame(Robot robot, int w, int h, int N) throws Exception {111Point p = getInternalFrameLocation();112int xs = p.x + 100;113int ys = p.y + 15;114robot.mouseMove(xs, ys);115try {116robot.mousePress(InputEvent.BUTTON1_MASK);117118int dx = w / N;119int dy = h / N;120121int y = ys;122for (int x = xs; x < xs + w; x += dx, y += dy) {123robot.mouseMove(x, y);124}125} finally {126robot.mouseRelease(InputEvent.BUTTON1_MASK);127}128}129130private static Point getInternalFrameLocation() throws Exception {131final Point[] points = new Point[1];132SwingUtilities.invokeAndWait(() -> {133points[0] = internalFrame.getLocationOnScreen();134});135return points[0];136}137138private static Point getDesktopPaneLocation() throws Exception {139final Point[] points = new Point[1];140SwingUtilities.invokeAndWait(() -> {141points[0] = desktopPane.getLocationOnScreen();142});143return points[0];144}145}146147148