|
| 1 | +/* |
| 2 | + * Copyright (c) 2000, 2024, 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 6251983 6722236 |
| 27 | + * @summary MouseDragged events not triggered for Choice when dragging it with left mouse button |
| 28 | + * @key headful |
| 29 | + * @run main ChoiceDragEventsInside |
| 30 | + */ |
| 31 | + |
| 32 | +import java.awt.Choice; |
| 33 | +import java.awt.Color; |
| 34 | +import java.awt.Dimension; |
| 35 | +import java.awt.EventQueue; |
| 36 | +import java.awt.FlowLayout; |
| 37 | +import java.awt.Frame; |
| 38 | +import java.awt.Point; |
| 39 | +import java.awt.Robot; |
| 40 | +import java.awt.event.InputEvent; |
| 41 | +import java.awt.event.KeyEvent; |
| 42 | +import java.awt.event.MouseEvent; |
| 43 | +import java.awt.event.MouseMotionAdapter; |
| 44 | +import java.lang.reflect.InvocationTargetException; |
| 45 | + |
| 46 | +public class ChoiceDragEventsInside extends Frame { |
| 47 | + Robot robot; |
| 48 | + Choice choice1; |
| 49 | + Point pt; |
| 50 | + Dimension size; |
| 51 | + volatile boolean mouseDragged = false; |
| 52 | + volatile boolean mouseDraggedOutside = false; |
| 53 | + |
| 54 | + public void setupUI() { |
| 55 | + setTitle("Choce Drag Events Inside"); |
| 56 | + choice1 = new Choice(); |
| 57 | + for (int i = 1; i < 50; i++) { |
| 58 | + choice1.add("item-0" + i); |
| 59 | + } |
| 60 | + choice1.setForeground(Color.red); |
| 61 | + choice1.setBackground(Color.red); |
| 62 | + choice1.addMouseMotionListener(new MouseMotionAdapter() { |
| 63 | + public void mouseMoved(MouseEvent me) { |
| 64 | + System.out.println(me); |
| 65 | + } |
| 66 | + |
| 67 | + public void mouseDragged(MouseEvent me) { |
| 68 | + System.out.println(me); |
| 69 | + mouseDragged = true; |
| 70 | + if (me.getY() < 0) { |
| 71 | + mouseDraggedOutside = true; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + ); |
| 76 | + add(choice1); |
| 77 | + setLayout(new FlowLayout()); |
| 78 | + setSize(200, 200); |
| 79 | + setLocationRelativeTo(null); |
| 80 | + setVisible(true); |
| 81 | + validate(); |
| 82 | + } |
| 83 | + |
| 84 | + public void start() { |
| 85 | + try { |
| 86 | + robot = new Robot(); |
| 87 | + robot.setAutoWaitForIdle(true); |
| 88 | + robot.setAutoDelay(50); |
| 89 | + robot.delay(100); |
| 90 | + EventQueue.invokeAndWait(() -> { |
| 91 | + pt = choice1.getLocationOnScreen(); |
| 92 | + size = choice1.getSize(); |
| 93 | + }); |
| 94 | + testDragInsideChoice(InputEvent.BUTTON1_MASK); |
| 95 | + testDragInsideChoiceList(InputEvent.BUTTON1_MASK); |
| 96 | + testDragOutsideChoice(InputEvent.BUTTON1_MASK); |
| 97 | + } catch (Throwable e) { |
| 98 | + throw new RuntimeException("Test failed. Exception thrown: " + e); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + public void testDragInsideChoice(int button) { |
| 103 | + robot.mouseMove(pt.x + size.width / 2, pt.y + size.height / 2); |
| 104 | + robot.delay(100); |
| 105 | + robot.mousePress(button); |
| 106 | + robot.mouseRelease(button); |
| 107 | + robot.delay(200); |
| 108 | + robot.mousePress(button); |
| 109 | + robot.mouseRelease(button); |
| 110 | + robot.delay(200); |
| 111 | + |
| 112 | + //close opened choice |
| 113 | + robot.keyPress(KeyEvent.VK_ESCAPE); |
| 114 | + robot.keyRelease(KeyEvent.VK_ESCAPE); |
| 115 | + robot.delay(200); |
| 116 | + |
| 117 | + robot.mouseMove(pt.x + size.width / 4, pt.y + size.height / 2); |
| 118 | + robot.mousePress(button); |
| 119 | + |
| 120 | + dragMouse(pt.x + size.width / 4, pt.y + size.height / 2, |
| 121 | + pt.x + size.width * 3 / 4, pt.y + size.height / 2); |
| 122 | + robot.mouseRelease(button); |
| 123 | + robot.delay(200); |
| 124 | + if (!mouseDragged) { |
| 125 | + throw new RuntimeException("Test failed. Choice should generate MouseDragged events inside Choice itself"); |
| 126 | + } else { |
| 127 | + System.out.println("Stage 1 passed. Choice generates MouseDragged events inside Choice itself"); |
| 128 | + } |
| 129 | + mouseDragged = false; |
| 130 | + //close opened choice |
| 131 | + robot.keyPress(KeyEvent.VK_ESCAPE); |
| 132 | + robot.keyRelease(KeyEvent.VK_ESCAPE); |
| 133 | + robot.delay(200); |
| 134 | + } |
| 135 | + |
| 136 | + public void testDragInsideChoiceList(int button) { |
| 137 | + robot.mouseMove(pt.x + size.width / 2, pt.y + size.height / 2); |
| 138 | + robot.delay(100); |
| 139 | + robot.mousePress(button); |
| 140 | + robot.mouseRelease(button); |
| 141 | + robot.delay(200); |
| 142 | + |
| 143 | + robot.mouseMove(pt.x + size.width / 2, pt.y + 5 * size.height); |
| 144 | + robot.delay(200); |
| 145 | + robot.mousePress(button); |
| 146 | + |
| 147 | + dragMouse(pt.x + size.width / 2, pt.y + 5 * size.height, |
| 148 | + pt.x + size.width / 2, pt.y + 8 * size.height); |
| 149 | + robot.mouseRelease(button); |
| 150 | + robot.delay(200); |
| 151 | + if (mouseDragged) { |
| 152 | + throw new RuntimeException("Test failed. Choice shouldn't generate MouseDragged events inside Choice's list"); |
| 153 | + } else { |
| 154 | + System.out.println("Stage 2 passed. Choice doesn't generate MouseDragged events inside Choice's list"); |
| 155 | + } |
| 156 | + robot.keyPress(KeyEvent.VK_ESCAPE); |
| 157 | + robot.keyRelease(KeyEvent.VK_ESCAPE); |
| 158 | + robot.delay(200); |
| 159 | + mouseDragged = false; |
| 160 | + } |
| 161 | + |
| 162 | + public void testDragOutsideChoice(int button) { |
| 163 | + pt = choice1.getLocationOnScreen(); |
| 164 | + robot.mouseMove(pt.x + size.width / 2, pt.y + size.height / 2); |
| 165 | + robot.delay(100); |
| 166 | + |
| 167 | + robot.mousePress(button); |
| 168 | + //drag mouse outside of Choice |
| 169 | + dragMouse(pt.x + size.width / 2, pt.y + size.height / 2, |
| 170 | + pt.x + size.width / 2, pt.y - 3 * size.height); |
| 171 | + robot.mouseRelease(button); |
| 172 | + robot.delay(200); |
| 173 | + if (!mouseDragged || !mouseDraggedOutside) { |
| 174 | + throw new RuntimeException("Test failed. Choice should generate MouseDragged events outside Choice"); |
| 175 | + } else { |
| 176 | + System.out.println("Stage 3 passed. Choice generates MouseDragged events outside Choice"); |
| 177 | + } |
| 178 | + robot.keyPress(KeyEvent.VK_ESCAPE); |
| 179 | + robot.keyRelease(KeyEvent.VK_ESCAPE); |
| 180 | + robot.delay(200); |
| 181 | + mouseDragged = false; |
| 182 | + } |
| 183 | + |
| 184 | + public void dragMouse(int x0, int y0, int x1, int y1) { |
| 185 | + int curX = x0; |
| 186 | + int curY = y0; |
| 187 | + int dx = x0 < x1 ? 1 : -1; |
| 188 | + int dy = y0 < y1 ? 1 : -1; |
| 189 | + |
| 190 | + while (curX != x1) { |
| 191 | + curX += dx; |
| 192 | + robot.mouseMove(curX, curY); |
| 193 | + } |
| 194 | + while (curY != y1) { |
| 195 | + curY += dy; |
| 196 | + robot.mouseMove(curX, curY); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + public static void main(final String[] args) throws InterruptedException, |
| 201 | + InvocationTargetException { |
| 202 | + ChoiceDragEventsInside app = new ChoiceDragEventsInside(); |
| 203 | + try { |
| 204 | + EventQueue.invokeAndWait(app::setupUI); |
| 205 | + app.start(); |
| 206 | + } finally { |
| 207 | + EventQueue.invokeAndWait(app::dispose); |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments