1 /* 2 * Copyright (c) 2013, 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 /** 25 * @test 26 * @bug 8027913 27 * @library ../../regtesthelpers 28 * @build Util 29 * @compile MissingDragExitEventTest.java 30 * @run main/othervm MissingDragExitEventTest 31 * @author Sergey Bylokhov 32 */ 33 34 import java.awt.Color; 35 import java.awt.Point; 36 import java.awt.Robot; 37 import java.awt.Toolkit; 38 import java.awt.dnd.DnDConstants; 39 import java.awt.dnd.DropTarget; 40 import java.awt.dnd.DropTargetAdapter; 41 import java.awt.dnd.DropTargetDragEvent; 42 import java.awt.dnd.DropTargetDropEvent; 43 import java.awt.dnd.DropTargetEvent; 44 import java.awt.event.InputEvent; 45 import java.awt.event.MouseAdapter; 46 import java.awt.event.MouseEvent; 47 48 import javax.swing.JFrame; 49 import javax.swing.JTextArea; 50 import javax.swing.SwingUtilities; 51 52 import sun.awt.SunToolkit; 53 import test.java.awt.regtesthelpers.Util; 54 55 public class MissingDragExitEventTest { 56 57 private static volatile JFrame frame; 58 private static boolean FAILED; 59 private static boolean MOUSE_ENTERED_DT; 60 private static boolean MOUSE_ENTERED; 61 private static boolean MOUSE_EXIT_TD; 62 private static boolean MOUSE_EXIT; 63 private static int SIZE = 300; 64 65 private static void initAndShowUI() { 66 frame = new JFrame("Test frame"); 67 68 frame.setSize(SIZE, SIZE); 69 frame.setLocationRelativeTo(null); 70 final JTextArea jta = new JTextArea(); 71 jta.setBackground(Color.RED); 72 frame.add(jta); 73 jta.setText("1234567890"); 74 jta.setFont(jta.getFont().deriveFont(150f)); 75 jta.setDragEnabled(true); 76 jta.selectAll(); 77 jta.setDropTarget(new DropTarget(jta, DnDConstants.ACTION_COPY, 78 new TestdropTargetListener())); 79 jta.addMouseListener(new TestMouseAdapter()); 80 frame.setVisible(true); 81 } 82 83 public static void main(final String[] args) throws Exception { 84 try { 85 final Robot r = new Robot(); 86 r.setAutoDelay(50); 87 r.mouseMove(100, 100); 88 Util.waitForIdle(r); 89 90 SwingUtilities.invokeAndWait(new Runnable() { 91 @Override 92 public void run() { 93 initAndShowUI(); 94 } 95 }); 96 97 final Point inside = new Point(frame.getLocationOnScreen()); 98 inside.translate(20, SIZE / 2); 99 final Point outer = new Point(inside); 100 outer.translate(-40, 0); 101 r.mouseMove(inside.x, inside.y); 102 r.mousePress(InputEvent.BUTTON1_MASK); 103 try { 104 for (int i = 0; i < 3; ++i) { 105 Util.mouseMove(r, inside, outer); 106 Util.mouseMove(r, outer, inside); 107 } 108 } finally { 109 r.mouseRelease(InputEvent.BUTTON1_MASK); 110 } 111 sleep(); 112 113 if (FAILED || !MOUSE_ENTERED || !MOUSE_ENTERED_DT || !MOUSE_EXIT 114 || !MOUSE_EXIT_TD) { 115 throw new RuntimeException("Failed"); 116 } 117 } finally { 118 if (frame != null) { 119 frame.dispose(); 120 } 121 } 122 } 123 124 private static void sleep() { 125 try { 126 Thread.sleep(10000); 127 } catch (InterruptedException ignored) { 128 } 129 ((SunToolkit) Toolkit.getDefaultToolkit()).realSync(); 130 } 131 132 static class TestdropTargetListener extends DropTargetAdapter { 133 134 private volatile boolean inside; 135 136 @Override 137 public void dragEnter(final DropTargetDragEvent dtde) { 138 if (inside) { 139 FAILED = true; 140 Thread.dumpStack(); 141 } 142 inside = true; 143 MOUSE_ENTERED_DT = true; 144 try { 145 Thread.sleep(10000); // we should have time to leave a component 146 } catch (InterruptedException ignored) { 147 } 148 } 149 150 @Override 151 public void dragOver(final DropTargetDragEvent dtde) { 152 if (!inside) { 153 FAILED = true; 154 Thread.dumpStack(); 155 } 156 } 157 158 @Override 159 public void dragExit(final DropTargetEvent dte) { 160 if (!inside) { 161 FAILED = true; 162 Thread.dumpStack(); 163 } 164 inside = false; 165 MOUSE_EXIT_TD = true; 166 } 167 168 @Override 169 public void drop(final DropTargetDropEvent dtde) { 170 if (!inside) { 171 FAILED = true; 172 Thread.dumpStack(); 173 } 174 inside = false; 175 } 176 } 177 178 static class TestMouseAdapter extends MouseAdapter { 179 180 private volatile boolean inside; 181 182 @Override 183 public void mouseEntered(final MouseEvent e) { 184 if (inside) { 185 FAILED = true; 186 Thread.dumpStack(); 187 } 188 inside = true; 189 MOUSE_ENTERED = true; 190 } 191 192 @Override 193 public void mouseExited(final MouseEvent e) { 194 if (!inside) { 195 FAILED = true; 196 Thread.dumpStack(); 197 } 198 inside = false; 199 MOUSE_EXIT = true; 200 } 201 } 202 }