1/*
2 * Copyright (c) 2015, 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
24import java.awt.BorderLayout;
25import java.awt.Color;
26import java.awt.Graphics;
27import java.awt.Rectangle;
28import java.awt.Robot;
29import java.awt.event.InputEvent;
30import javax.swing.JDesktopPane;
31import javax.swing.JFrame;
32import javax.swing.JInternalFrame;
33import javax.swing.JPanel;
34import javax.swing.SwingUtilities;
35import static sun.awt.OSInfo.*;
36
37/**
38 * @test
39 * @key headful
40 * @bug 8069348 8159902
41 * @summary SunGraphics2D.copyArea() does not properly work for scaled graphics
42 * @author Alexandr Scherbatiy
43 * @modules java.desktop/sun.awt
44 * @run main/othervm -Dsun.java2d.uiScale=2 bug8069348
45 * @run main/othervm -Dsun.java2d.opengl=true -Dsun.java2d.uiScale=2 bug8069348
46 * @run main/othervm -Dsun.java2d.d3d=true -Dsun.java2d.uiScale=2 bug8069348
47 */
48public class bug8069348 {
49
50    private static final int WIN_WIDTH = 500;
51    private static final int WIN_HEIGHT = 500;
52
53    private static final Color DESKTOPPANE_COLOR = Color.YELLOW;
54    private static final Color FRAME_COLOR = Color.ORANGE;
55
56    private static JFrame frame;
57    private static JInternalFrame internalFrame;
58
59    public static void main(String[] args) throws Exception {
60
61        if (!isSupported()) {
62            return;
63        }
64
65        try {
66
67            SwingUtilities.invokeAndWait(bug8069348::createAndShowGUI);
68
69            Robot robot = new Robot();
70            robot.setAutoDelay(50);
71            robot.waitForIdle();
72
73            Rectangle screenBounds = getInternalFrameScreenBounds();
74
75            int x = screenBounds.x + screenBounds.width / 2;
76            int y = screenBounds.y + 10;
77            int dx = screenBounds.width / 2;
78            int dy = screenBounds.height / 2;
79
80            robot.mouseMove(x, y);
81            robot.waitForIdle();
82
83            robot.mousePress(InputEvent.BUTTON1_MASK);
84            robot.mouseMove(x + dx, y + dy);
85            robot.mouseRelease(InputEvent.BUTTON1_MASK);
86            robot.waitForIdle();
87
88            int cx = screenBounds.x + screenBounds.width + dx / 2;
89            int cy = screenBounds.y + screenBounds.height + dy / 2;
90
91            robot.mouseMove(cx, cy);
92            if (!FRAME_COLOR.equals(robot.getPixelColor(cx, cy))) {
93                throw new RuntimeException("Internal frame is not correctly dragged!");
94            }
95        } finally {
96            if (frame != null) {
97                frame.dispose();
98            }
99        }
100    }
101
102    private static boolean isSupported() {
103        String d3d = System.getProperty("sun.java2d.d3d");
104        return !Boolean.getBoolean(d3d) || getOSType() == OSType.WINDOWS;
105    }
106
107    private static Rectangle getInternalFrameScreenBounds() throws Exception {
108        Rectangle[] points = new Rectangle[1];
109        SwingUtilities.invokeAndWait(() -> {
110            points[0] = new Rectangle(internalFrame.getLocationOnScreen(),
111                    internalFrame.getSize());
112        });
113        return points[0];
114    }
115
116    private static void createAndShowGUI() {
117
118        frame = new JFrame();
119        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
120
121        JDesktopPane desktopPane = new JDesktopPane();
122        desktopPane.setBackground(DESKTOPPANE_COLOR);
123
124        internalFrame = new JInternalFrame("Test") {
125
126            @Override
127            public void paint(Graphics g) {
128                super.paint(g);
129                g.setColor(FRAME_COLOR);
130                g.fillRect(0, 0, getWidth(), getHeight());
131            }
132        };
133        internalFrame.setSize(WIN_WIDTH / 3, WIN_HEIGHT / 3);
134        internalFrame.setVisible(true);
135        desktopPane.add(internalFrame);
136
137        JPanel panel = new JPanel();
138        panel.setLayout(new BorderLayout());
139        panel.add(desktopPane, BorderLayout.CENTER);
140        frame.add(panel);
141        frame.setSize(WIN_WIDTH, WIN_HEIGHT);
142        frame.setVisible(true);
143        frame.requestFocus();
144    }
145}
146