|
| 1 | +/* |
| 2 | + * Copyright (c) 2002, 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 4700276 |
| 27 | + * @summary Peers process KeyEvents before KeyEventPostProcessors |
| 28 | + * @requires (os.family == "windows") |
| 29 | + * @library /java/awt/regtesthelpers |
| 30 | + * @build PassFailJFrame |
| 31 | + * @run main/manual ConsumedKeyEventTest |
| 32 | +*/ |
| 33 | + |
| 34 | +import java.awt.Canvas; |
| 35 | +import java.awt.Component; |
| 36 | +import java.awt.Color; |
| 37 | +import java.awt.Dimension; |
| 38 | +import java.awt.FlowLayout; |
| 39 | +import java.awt.Frame; |
| 40 | +import java.awt.Graphics; |
| 41 | +import java.awt.KeyboardFocusManager; |
| 42 | +import java.awt.KeyEventPostProcessor; |
| 43 | +import java.awt.event.KeyEvent; |
| 44 | +import java.awt.event.FocusAdapter; |
| 45 | +import java.awt.event.FocusEvent; |
| 46 | +import java.awt.event.FocusListener; |
| 47 | +import java.awt.event.MouseAdapter; |
| 48 | +import java.awt.event.MouseEvent; |
| 49 | + |
| 50 | +public class ConsumedKeyEventTest implements KeyEventPostProcessor { |
| 51 | + |
| 52 | + private static final String INSTRUCTIONS = """ |
| 53 | + This is a Windows-only test. |
| 54 | + When the test starts, you will see a Frame with two components in it, |
| 55 | + components look like colored rectangles, one of them is lightweight, one is heavyweight. |
| 56 | + Do the following: |
| 57 | + 1. Click the mouse on the left component. |
| 58 | + If it isn't yellow after the click (that means it doesn't have focus), the test fails. |
| 59 | + 2. Press and release ALT key. |
| 60 | + In the output window, the text should appear stating that those key events were consumed. |
| 61 | + If no output appears, the test fails. |
| 62 | + 3. Press space bar. If system menu drops down, the test fails. |
| 63 | + 4. Click the right rectangle. |
| 64 | + It should become red after the click. If it doesn't, it means that it didn't get the focus, and the test fails. |
| 65 | + 5. Repeat steps 2. and 3. |
| 66 | + 6. If the test didn't fail on any of the previous steps, the test passes."""; |
| 67 | + |
| 68 | + |
| 69 | + public static void main(String[] args) throws Exception { |
| 70 | + PassFailJFrame.builder() |
| 71 | + .title("ConsumedKeyEventTest Instructions") |
| 72 | + .instructions(INSTRUCTIONS) |
| 73 | + .rows((int) INSTRUCTIONS.lines().count() + 5) |
| 74 | + .columns(35) |
| 75 | + .testUI(ConsumedKeyEventTest::createTestUI) |
| 76 | + .logArea() |
| 77 | + .build() |
| 78 | + .awaitAndCheck(); |
| 79 | + } |
| 80 | + |
| 81 | + private static Frame createTestUI() { |
| 82 | + KeyboardFocusManager.getCurrentKeyboardFocusManager(). |
| 83 | + addKeyEventPostProcessor((e) -> { |
| 84 | + System.out.println("postProcessor(" + e + ")"); |
| 85 | + // consumes all ALT-events |
| 86 | + if (e.getKeyCode() == KeyEvent.VK_ALT) { |
| 87 | + println("consumed " + e); |
| 88 | + e.consume(); |
| 89 | + return true; |
| 90 | + } |
| 91 | + return false; |
| 92 | + }); |
| 93 | + FocusRequestor requestor = new FocusRequestor(); |
| 94 | + Frame frame = new Frame("Main Frame"); |
| 95 | + frame.setLayout(new FlowLayout()); |
| 96 | + |
| 97 | + Canvas canvas = new CustomCanvas(); |
| 98 | + canvas.addMouseListener(requestor); |
| 99 | + frame.add(canvas); |
| 100 | + canvas.requestFocus(); |
| 101 | + |
| 102 | + Component lwComp = new LWComponent(); |
| 103 | + lwComp.addMouseListener(requestor); |
| 104 | + frame.add(lwComp); |
| 105 | + |
| 106 | + frame.pack(); |
| 107 | + |
| 108 | + return frame; |
| 109 | + } |
| 110 | + |
| 111 | + public boolean postProcessKeyEvent(KeyEvent e) { |
| 112 | + System.out.println("postProcessor(" + e + ")"); |
| 113 | + // consumes all ALT-events |
| 114 | + if (e.getKeyCode() == KeyEvent.VK_ALT) { |
| 115 | + println("consumed " + e); |
| 116 | + e.consume(); |
| 117 | + return true; |
| 118 | + } |
| 119 | + return false; |
| 120 | + } |
| 121 | + |
| 122 | + static void println(String messageIn) { |
| 123 | + PassFailJFrame.log(messageIn); |
| 124 | + } |
| 125 | +}// class ConsumedKeyEventTest |
| 126 | + |
| 127 | +class CustomCanvas extends Canvas { |
| 128 | + CustomCanvas() { |
| 129 | + super(); |
| 130 | + setName("HWComponent"); |
| 131 | + setSize(100, 100); |
| 132 | + addFocusListener(new FocusAdapter() { |
| 133 | + public void focusGained(FocusEvent fe) { |
| 134 | + repaint(); |
| 135 | + } |
| 136 | + |
| 137 | + public void focusLost(FocusEvent fe) { |
| 138 | + repaint(); |
| 139 | + } |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + public void paint(Graphics g) { |
| 144 | + if (isFocusOwner()) { |
| 145 | + g.setColor(Color.YELLOW); |
| 146 | + } else { |
| 147 | + g.setColor(Color.GREEN); |
| 148 | + } |
| 149 | + g.fillRect(0, 0, 100, 100); |
| 150 | + } |
| 151 | + |
| 152 | +} |
| 153 | + |
| 154 | +class LWComponent extends Component { |
| 155 | + LWComponent() { |
| 156 | + super(); |
| 157 | + setName("LWComponent"); |
| 158 | + addFocusListener(new FocusAdapter() { |
| 159 | + public void focusGained(FocusEvent fe) { |
| 160 | + repaint(); |
| 161 | + } |
| 162 | + |
| 163 | + public void focusLost(FocusEvent fe) { |
| 164 | + repaint(); |
| 165 | + } |
| 166 | + }); |
| 167 | + } |
| 168 | + |
| 169 | + public Dimension getPreferredSize() { |
| 170 | + return new Dimension(100, 100); |
| 171 | + } |
| 172 | + |
| 173 | + public void paint(Graphics g) { |
| 174 | + if (isFocusOwner()) { |
| 175 | + g.setColor(Color.RED); |
| 176 | + } else { |
| 177 | + g.setColor(Color.BLACK); |
| 178 | + } |
| 179 | + g.fillRect(0, 0, 100, 100); |
| 180 | + } |
| 181 | + |
| 182 | +} |
| 183 | + |
| 184 | +class FocusRequestor extends MouseAdapter { |
| 185 | + static int counter = 0; |
| 186 | + public void mouseClicked(MouseEvent me) { |
| 187 | + System.out.println("mouseClicked on " + me.getComponent().getName()); |
| 188 | + } |
| 189 | + public void mousePressed(MouseEvent me) { |
| 190 | + System.out.println("mousePressed on " + me.getComponent().getName()); |
| 191 | + me.getComponent().requestFocus(); |
| 192 | + } |
| 193 | + public void mouseReleased(MouseEvent me) { |
| 194 | + System.out.println("mouseReleased on " + me.getComponent().getName()); |
| 195 | + } |
| 196 | +} |
| 197 | + |
0 commit comments