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 
  26 import javax.swing.*;
  27 import java.awt.*;
  28 import java.awt.image.BufferedImage;
  29 
  30 import static java.awt.event.InputEvent.BUTTON1_DOWN_MASK;
  31 
  32 /**
  33  * @test
  34  * @bug 8076106
  35  * @author Hendrik Schreiber
  36  * @summary [macosx] Drag image of TransferHandler does not honor
  37  * MultiResolutionImage
  38  * @modules java.desktop/sun.awt.image
  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     static float getScaleFactor() {
 128         return (float) GraphicsEnvironment.
 129                 getLocalGraphicsEnvironment().
 130                 getDefaultScreenDevice().getDefaultConfiguration().
 131                 getDefaultTransform().getScaleX();
 132     }
 133 
 134     private static Image createMultiResolutionImage() {
 135 
 136         return new MultiResolutionToolkitImage(
 137                 createImage(50, COLOR_1X),
 138                 createImage(100, COLOR_2X)
 139         );
 140 
 141     }
 142 
 143     private static Image createImage(final int length, final Color color) {
 144 
 145         final BufferedImage image = new BufferedImage(length, length,
 146                 BufferedImage.TYPE_INT_ARGB_PRE);
 147         final Graphics graphics = image.getGraphics();
 148         graphics.setColor(color);
 149         graphics.fillRect(0, 0, length, length);
 150         graphics.dispose();
 151         return image;
 152     }
 153 }