1 /*
   2  * Copyright (c) 2016, 2017, 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.BorderLayout;
  25 import java.awt.Color;
  26 import java.awt.Point;
  27 import java.awt.Rectangle;
  28 import java.awt.Robot;
  29 import java.awt.event.InputEvent;
  30 import java.awt.image.BufferedImage;
  31 import javax.swing.JDesktopPane;
  32 import javax.swing.JFrame;
  33 import javax.swing.JInternalFrame;
  34 import javax.swing.SwingUtilities;
  35 
  36 /**
  37  * @test
  38  * @key headful
  39  * @bug 8160248 8160332
  40  * @summary Dragged internal frame leaves artifacts for floating point ui scale
  41  * @run main/othervm -Dsun.java2d.uiScale=1.2 JInternalFrameDraggingTest
  42  * @run main/othervm -Dsun.java2d.uiScale=1.5 JInternalFrameDraggingTest
  43  * @run main/othervm -Dsun.java2d.uiScale=1 JInternalFrameDraggingTest
  44  * @run main/othervm -Dsun.java2d.uiScale=2.5 JInternalFrameDraggingTest
  45  */
  46 
  47 public class JInternalFrameDraggingTest {
  48 
  49     private static JFrame frame;
  50     private static JDesktopPane desktopPane;
  51     private static JInternalFrame internalFrame;
  52     private static int FRAME_SIZE = 500;
  53     private static Color BACKGROUND_COLOR = Color.ORANGE;
  54 
  55     public static void main(String[] args) throws Exception {
  56 
  57         Robot robot = new Robot();
  58         robot.setAutoDelay(20);
  59         SwingUtilities.invokeAndWait(JInternalFrameDraggingTest::createAndShowGUI);
  60         robot.waitForIdle();
  61 
  62         final int translate = FRAME_SIZE / 4;
  63         moveFrame(robot, translate, translate / 2, translate / 2);
  64         robot.waitForIdle();
  65 
  66         Point p = getDesktopPaneLocation();
  67         int size = translate / 2;
  68         Rectangle rect = new Rectangle(p.x, p.y, size, size);
  69         BufferedImage img = robot.createScreenCapture(rect);
  70 
  71         int testRGB = BACKGROUND_COLOR.getRGB();
  72         for (int i = 0; i < size; i++) {
  73             int rgbCW = img.getRGB(i, size / 2);
  74             int rgbCH = img.getRGB(size / 2, i);
  75             if (rgbCW != testRGB || rgbCH != testRGB) {
  76                 throw new RuntimeException("Background color is wrong!");
  77             }
  78         }
  79     }
  80 
  81     private static void createAndShowGUI() {
  82 
  83         frame = new JFrame();
  84         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  85         frame.setLayout(new BorderLayout());
  86 
  87         desktopPane = new JDesktopPane();
  88         desktopPane.setBackground(BACKGROUND_COLOR);
  89 
  90         frame.add(desktopPane, BorderLayout.CENTER);
  91         frame.setSize(FRAME_SIZE, FRAME_SIZE);
  92         frame.setVisible(true);
  93 
  94         internalFrame = new JInternalFrame("Test");
  95         internalFrame.setSize(FRAME_SIZE / 2, FRAME_SIZE / 2);
  96         desktopPane.add(internalFrame);
  97         internalFrame.setVisible(true);
  98         internalFrame.setResizable(true);
  99 
 100         frame.setVisible(true);
 101     }
 102 
 103     private static void moveFrame(Robot robot, int w, int h, int N) throws Exception {
 104         Point p = getInternalFrameLocation();
 105         int xs = p.x + 100;
 106         int ys = p.y + 15;
 107         robot.mouseMove(xs, ys);
 108         try {
 109             robot.mousePress(InputEvent.BUTTON1_MASK);
 110 
 111             int dx = w / N;
 112             int dy = h / N;
 113 
 114             int y = ys;
 115             for (int x = xs; x < xs + w; x += dx, y += dy) {
 116                 robot.mouseMove(x, y);
 117             }
 118         } finally {
 119             robot.mouseRelease(InputEvent.BUTTON1_MASK);
 120         }
 121     }
 122 
 123     private static Point getInternalFrameLocation() throws Exception {
 124         final Point[] points = new Point[1];
 125         SwingUtilities.invokeAndWait(() -> {
 126             points[0] = internalFrame.getLocationOnScreen();
 127         });
 128         return points[0];
 129     }
 130 
 131     private static Point getDesktopPaneLocation() throws Exception {
 132         final Point[] points = new Point[1];
 133         SwingUtilities.invokeAndWait(() -> {
 134             points[0] = desktopPane.getLocationOnScreen();
 135         });
 136         return points[0];
 137     }
 138 }