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