|
| 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 4524015 |
| 27 | + * @summary Tests that when user switches between windows using Alt-tab then the appropriate events are generated |
| 28 | + * @library /java/awt/regtesthelpers |
| 29 | + * @build PassFailJFrame |
| 30 | + * @run main/manual AltTabEventsTest |
| 31 | + */ |
| 32 | + |
| 33 | +import java.awt.Button; |
| 34 | +import java.awt.Choice; |
| 35 | +import java.awt.Component; |
| 36 | +import java.awt.FlowLayout; |
| 37 | +import java.awt.Frame; |
| 38 | +import java.awt.Menu; |
| 39 | +import java.awt.MenuBar; |
| 40 | +import java.awt.MenuItem; |
| 41 | +import java.awt.PopupMenu; |
| 42 | +import java.awt.event.ActionListener; |
| 43 | +import java.awt.event.ActionEvent; |
| 44 | +import java.awt.event.WindowAdapter; |
| 45 | +import java.awt.event.WindowEvent; |
| 46 | + |
| 47 | +public class AltTabEventsTest { |
| 48 | + |
| 49 | + private static final String INSTRUCTIONS = """ |
| 50 | + This test verifies that when user switches between windows using Alt-tab |
| 51 | + key combination then appropriate window events are generated. Also, when |
| 52 | + user interacts with Menu bar, Popup menu, Choice then no excessive window |
| 53 | + event is generated. |
| 54 | +
|
| 55 | + After test started you will see Frame('Test for 4524015')-F1 with some |
| 56 | + components and Frame('Another frame')-F2 with no components. |
| 57 | + 1. Make F1 active by clicking on it. |
| 58 | + 2. Press Alt-tab. |
| 59 | + In the messqge dialog area you should see that |
| 60 | + WINDOW_DEACTIVATED, WINDOW_LOST_FOCUS event were generated. |
| 61 | + If you switched to F2 then also WINDOW_ACTIVATED, WINDOW_GAINED_FOCUS |
| 62 | + were generated. |
| 63 | + If no events were generated the test FAILED. |
| 64 | + Repeat the 2) with different circumstances. |
| 65 | +
|
| 66 | + 3. Make F1 active by clicking on it. |
| 67 | + 4. Click on Menu bar/Button 'popup'/Choice and select some item from |
| 68 | + the list shown. If any of the window events appeared in the output then |
| 69 | + the test FAILED. |
| 70 | +
|
| 71 | + else the test PASSED."""; |
| 72 | + |
| 73 | + public static void main(String[] args) throws Exception { |
| 74 | + PassFailJFrame.builder() |
| 75 | + .title("AltTabEventsTest Instructions") |
| 76 | + .instructions(INSTRUCTIONS) |
| 77 | + .rows((int) INSTRUCTIONS.lines().count() + 5) |
| 78 | + .columns(35) |
| 79 | + .testUI(Test::new) |
| 80 | + .logArea() |
| 81 | + .build() |
| 82 | + .awaitAndCheck(); |
| 83 | + } |
| 84 | + |
| 85 | +} |
| 86 | + |
| 87 | + |
| 88 | +class Test extends Frame { |
| 89 | + PopupMenu pop; |
| 90 | + Frame f; |
| 91 | + |
| 92 | + void println(String messageIn) { |
| 93 | + PassFailJFrame.log(messageIn); |
| 94 | + } |
| 95 | + |
| 96 | + public Test() { |
| 97 | + super("Test for 4524015"); |
| 98 | + WindowAdapter wa = new WindowAdapter() { |
| 99 | + public void windowActivated(WindowEvent e) { |
| 100 | + println(e.toString()); |
| 101 | + } |
| 102 | + public void windowDeactivated(WindowEvent e) { |
| 103 | + println(e.toString()); |
| 104 | + } |
| 105 | + public void windowGainedFocus(WindowEvent e) { |
| 106 | + println(e.toString()); |
| 107 | + } |
| 108 | + public void windowLostFocus(WindowEvent e) { |
| 109 | + println(e.toString()); |
| 110 | + } |
| 111 | + }; |
| 112 | + addWindowListener(wa); |
| 113 | + addWindowFocusListener(wa); |
| 114 | + |
| 115 | + f = new Frame("Another frame"); |
| 116 | + f.addWindowListener(wa); |
| 117 | + f.addWindowFocusListener(wa); |
| 118 | + f.setBounds(800, 300, 300, 100); |
| 119 | + f.setVisible(true); |
| 120 | + |
| 121 | + setLayout(new FlowLayout()); |
| 122 | + Button b = new Button("popup"); |
| 123 | + add(b); |
| 124 | + b.addActionListener(new ActionListener() { |
| 125 | + public void actionPerformed(ActionEvent e) { |
| 126 | + pop.show((Component)e.getSource(), 10, 10); |
| 127 | + } |
| 128 | + }); |
| 129 | + Choice cho = new Choice(); |
| 130 | + add(cho); |
| 131 | + cho.addItem("1"); |
| 132 | + cho.addItem("2"); |
| 133 | + cho.addItem("3"); |
| 134 | + |
| 135 | + MenuBar bar = new MenuBar(); |
| 136 | + Menu menu = new Menu("menu"); |
| 137 | + MenuItem item = new MenuItem("first"); |
| 138 | + menu.add(item); |
| 139 | + item = new MenuItem("second"); |
| 140 | + menu.add(item); |
| 141 | + bar.add(menu); |
| 142 | + setMenuBar(bar); |
| 143 | + |
| 144 | + pop = new PopupMenu(); |
| 145 | + pop.add("1"); |
| 146 | + pop.add("@"); |
| 147 | + add(pop); |
| 148 | + setSize(300, 100); |
| 149 | + } |
| 150 | +} |
| 151 | + |
0 commit comments