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 * @key headful 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 * @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) <= 50; 126 } 127 128 static float getScaleFactor() { 129 return (float) GraphicsEnvironment. 130 getLocalGraphicsEnvironment(). 131 getDefaultScreenDevice().getDefaultConfiguration(). 132 getDefaultTransform().getScaleX(); 133 } 134 135 private static Image createMultiResolutionImage() { 136 137 return new MultiResolutionToolkitImage( 138 createImage(50, COLOR_1X), 139 createImage(100, COLOR_2X) 140 ); 141 142 } 143 144 private static Image createImage(final int length, final Color color) { 145 146 final BufferedImage image = new BufferedImage(length, length, 147 BufferedImage.TYPE_INT_ARGB_PRE); 148 final Graphics graphics = image.getGraphics(); 149 graphics.setColor(color); 150 graphics.fillRect(0, 0, length, length); 151 graphics.dispose(); 152 return image; 153 } 154 }