1 /*
   2  * Copyright (c) 2015, 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 sun.awt.image.MultiResolutionToolkitImage;
  25 import sun.java2d.SunGraphics2D;
  26 
  27 import javax.swing.*;
  28 import java.awt.*;
  29 import java.awt.image.BufferedImage;
  30 
  31 import static java.awt.event.InputEvent.BUTTON1_DOWN_MASK;
  32 
  33 /**
  34  * @test
  35  * @bug 8076106
  36  * @author Hendrik Schreiber
  37  * @summary [macosx] Drag image of TransferHandler does not honor
  38  * MultiResolutionImage
  39  * @modules java.desktop/sun.awt.image
  40  *          java.desktop/sun.java2d
  41  * @run main MultiResolutionDragImageTest TEST_DRAG
  42  */
  43 public class MultiResolutionDragImageTest {
  44 
  45     private static final Color COLOR_1X = Color.BLUE;
  46     private static final Color COLOR_2X = Color.RED;
  47     private static JFrame frame;
  48     private static JTextField field;
  49     private static Point p;
  50 
  51     public static void main(String[] args) throws Exception {
  52 
  53         final String test = args[0];
  54 
  55         switch (test) {
  56             case "TEST_DRAG":
  57                 testDrag();
  58                 break;
  59             default:
  60                 throw new RuntimeException("Unknown test: " + test);
  61         }
  62     }
  63 
  64     private static void testDrag() throws Exception {
  65 
  66 
  67         SwingUtilities.invokeAndWait(() -> {
  68 
  69             frame = new JFrame();
  70             field = new JTextField("Drag Me");
  71             setupFrame(frame, field);
  72             frame.setVisible(true);
  73         });
  74 
  75         final Robot robot = new Robot();
  76         robot.setAutoDelay(500);
  77         robot.setAutoWaitForIdle(true);
  78         robot.waitForIdle();
  79 
  80         // get mouse into position
  81         SwingUtilities.invokeAndWait(() -> {
  82 
  83             p = new Point(field.getWidth() / 2, field.getHeight() / 2);
  84             SwingUtilities.convertPointToScreen(p, field);
  85         });
  86 
  87         robot.mouseMove(p.x, p.y);
  88         // simulate dragging
  89         robot.mousePress(BUTTON1_DOWN_MASK);
  90         p.translate(10, 10);
  91         robot.mouseMove(p.x, p.y);
  92 
  93         p.translate(5, 5);
  94         final Color color = robot.getPixelColor(p.x, p.y);
  95         robot.mouseRelease(BUTTON1_DOWN_MASK);
  96 
  97         SwingUtilities.invokeAndWait(frame::dispose);
  98 
  99         final float scaleFactor = getScaleFactor();
 100         final Color testColor = (1 < scaleFactor) ? COLOR_2X : COLOR_1X;
 101 
 102         if (!similar(testColor, color)) {
 103             throw new RuntimeException(
 104                     "TEST FAILED: Image with wrong resolution is used for drag image!");
 105         }
 106         System.out.println("TEST PASSED!");
 107     }
 108 
 109     private static void setupFrame(final JFrame frame, final JTextField field) {
 110 
 111         frame.setBounds(0, 0, 50, 50);
 112         frame.setLayout(new BorderLayout());
 113         field.setDragEnabled(true);
 114         final TransferHandler transferHandler = field.getTransferHandler();
 115         transferHandler.setDragImage(createMultiResolutionImage());
 116         frame.getContentPane().add(field, BorderLayout.CENTER);
 117     }
 118 
 119     private static boolean similar(Color c1, Color c2){
 120         return similar(c1.getRed(), c2.getRed())
 121                 && similar(c1.getGreen(), c2.getGreen())
 122                 && similar(c1.getBlue(), c2.getBlue());
 123     }
 124 
 125     private static boolean similar(int n, int m){
 126         return Math.abs(n - m) <= 50;
 127     }
 128 
 129     private static float getScaleFactor() {
 130 
 131         final Dialog dialog = new Dialog((Window) null);
 132         dialog.setSize(100, 100);
 133         dialog.setModal(true);
 134         final float[] scaleFactors = new float[1];
 135         Panel panel = new Panel() {
 136 
 137             @Override
 138             public void paint(Graphics g) {
 139                 float scaleFactor = 1;
 140                 if (g instanceof SunGraphics2D) {
 141                     scaleFactor = ((SunGraphics2D) g).surfaceData.getDefaultScale();
 142                 }
 143                 scaleFactors[0] = scaleFactor;
 144                 dialog.setVisible(false);
 145             }
 146         };
 147 
 148         dialog.add(panel);
 149         dialog.setVisible(true);
 150         dialog.dispose();
 151 
 152         return scaleFactors[0];
 153     }
 154 
 155     private static Image createMultiResolutionImage() {
 156 
 157         return new MultiResolutionToolkitImage(
 158                 createImage(50, COLOR_1X),
 159                 createImage(100, COLOR_2X)
 160         );
 161 
 162     }
 163 
 164     private static Image createImage(final int length, final Color color) {
 165 
 166         final BufferedImage image = new BufferedImage(length, length,
 167                 BufferedImage.TYPE_INT_ARGB_PRE);
 168         final Graphics graphics = image.getGraphics();
 169         graphics.setColor(color);
 170         graphics.fillRect(0, 0, length, length);
 171         graphics.dispose();
 172         return image;
 173     }
 174 }