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