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