|
| 1 | +/* |
| 2 | + * Copyright (c) 2005, 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 | +import java.awt.Choice; |
| 25 | +import java.awt.EventQueue; |
| 26 | +import java.awt.FlowLayout; |
| 27 | +import java.awt.Frame; |
| 28 | +import java.awt.Point; |
| 29 | +import java.awt.Robot; |
| 30 | +import java.awt.Toolkit; |
| 31 | +import java.awt.event.InputEvent; |
| 32 | +import java.awt.event.KeyEvent; |
| 33 | +import java.awt.event.MouseWheelEvent; |
| 34 | +import java.awt.event.MouseWheelListener; |
| 35 | + |
| 36 | +/* |
| 37 | + * @test |
| 38 | + * @bug 6253211 |
| 39 | + * @summary PIT: MouseWheel events not triggered for Choice drop down in XAWT |
| 40 | + * @requires (os.family == "linux") |
| 41 | + * @key headful |
| 42 | + * @run main WheelEventsConsumed |
| 43 | + */ |
| 44 | + |
| 45 | +public class WheelEventsConsumed extends Frame implements MouseWheelListener |
| 46 | +{ |
| 47 | + Robot robot; |
| 48 | + Choice choice1 = new Choice(); |
| 49 | + Point pt; |
| 50 | + final static int delay = 100; |
| 51 | + boolean mouseWheeled = false; |
| 52 | + final static int OUTSIDE_CHOICE = 1; |
| 53 | + final static int INSIDE_LIST_OF_CHOICE = 2; |
| 54 | + final static int INSIDE_CHOICE_COMPONENT = 3; |
| 55 | + static String toolkit; |
| 56 | + |
| 57 | + private static volatile WheelEventsConsumed frame = null; |
| 58 | + |
| 59 | + public static void main(String[] args) throws Exception { |
| 60 | + toolkit = Toolkit.getDefaultToolkit().getClass().getName(); |
| 61 | + try { |
| 62 | + EventQueue.invokeAndWait(() -> { |
| 63 | + frame = new WheelEventsConsumed(); |
| 64 | + frame.initAndShow(); |
| 65 | + }); |
| 66 | + frame.test(); |
| 67 | + } finally { |
| 68 | + EventQueue.invokeAndWait(() -> { |
| 69 | + if (frame != null) { |
| 70 | + frame.dispose(); |
| 71 | + } |
| 72 | + }); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public void mouseWheelMoved(MouseWheelEvent me) { |
| 77 | + mouseWheeled = true; |
| 78 | + System.out.println(me); |
| 79 | + } |
| 80 | + |
| 81 | + public void initAndShow() { |
| 82 | + setTitle("WheelEventsConsumed test"); |
| 83 | + for (int i = 1; i < 10; i++) { |
| 84 | + choice1.add("item-0" + i); |
| 85 | + } |
| 86 | + |
| 87 | + choice1.addMouseWheelListener(this); |
| 88 | + add(choice1); |
| 89 | + setLayout(new FlowLayout()); |
| 90 | + setSize(200, 200); |
| 91 | + setLocationRelativeTo(null); |
| 92 | + setVisible(true); |
| 93 | + validate(); |
| 94 | + } |
| 95 | + |
| 96 | + public void test() { |
| 97 | + try { |
| 98 | + robot = new Robot(); |
| 99 | + robot.setAutoWaitForIdle(true); |
| 100 | + robot.setAutoDelay(50); |
| 101 | + robot.waitForIdle(); |
| 102 | + robot.delay(delay * 5); |
| 103 | + testMouseWheel(1, OUTSIDE_CHOICE); |
| 104 | + robot.delay(delay); |
| 105 | + testMouseWheel(-1, INSIDE_LIST_OF_CHOICE); |
| 106 | + robot.delay(delay); |
| 107 | + testMouseWheel(1, INSIDE_CHOICE_COMPONENT); |
| 108 | + robot.delay(delay); |
| 109 | + } catch (Throwable e) { |
| 110 | + throw new RuntimeException("Test failed. Exception thrown: " + e); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + public void testMouseWheel(int amt, int mousePosition) { |
| 115 | + pt = choice1.getLocationOnScreen(); |
| 116 | + robot.mouseMove(pt.x + choice1.getWidth() / 2, pt.y + choice1.getHeight() / 2); |
| 117 | + |
| 118 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 119 | + robot.delay(50); |
| 120 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 121 | + robot.delay(50); |
| 122 | + |
| 123 | + switch (mousePosition) { |
| 124 | + case OUTSIDE_CHOICE: |
| 125 | + robot.mouseMove(pt.x + choice1.getWidth() * 3 / 2, pt.y + choice1.getHeight() / 2); |
| 126 | + break; |
| 127 | + case INSIDE_LIST_OF_CHOICE: |
| 128 | + robot.mouseMove(pt.x + choice1.getWidth() / 2, pt.y + choice1.getHeight() * 4); |
| 129 | + break; |
| 130 | + case INSIDE_CHOICE_COMPONENT: |
| 131 | + robot.mouseMove(pt.x + choice1.getWidth() / 2, pt.y + choice1.getHeight() / 2); |
| 132 | + break; |
| 133 | + } |
| 134 | + |
| 135 | + robot.delay(delay); |
| 136 | + for (int i = 0; i < 10; i++) { |
| 137 | + robot.mouseWheel(amt); |
| 138 | + robot.delay(delay); |
| 139 | + } |
| 140 | + |
| 141 | + if (!mouseWheeled) { |
| 142 | + if (toolkit.equals("sun.awt.windows.WToolkit") && mousePosition == OUTSIDE_CHOICE) { |
| 143 | + System.out.println("Passed. Separate case on Win32. Choice generated MouseWheel events" + mousePosition); |
| 144 | + } else { |
| 145 | + throw new RuntimeException("Test failed. Choice should generate MOUSE_WHEEL events." + mousePosition); |
| 146 | + } |
| 147 | + } else { |
| 148 | + System.out.println("Passed. Choice generated MouseWheel events" + mousePosition); |
| 149 | + } |
| 150 | + robot.keyPress(KeyEvent.VK_ESCAPE); |
| 151 | + robot.delay(10); |
| 152 | + robot.keyRelease(KeyEvent.VK_ESCAPE); |
| 153 | + robot.delay(200); |
| 154 | + mouseWheeled = false; |
| 155 | + } |
| 156 | +} |
0 commit comments