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 /* @test
25 @bug 8001633
26 @summary Wrong alt processing during switching between windows
27 @author mikhail.cherkasov@oracle.com
28 @run main WrongAltProcessing
29 */
30
31 import sun.awt.SunToolkit;
32
33 import javax.swing.*;
34 import java.awt.*;
35 import java.awt.event.*;
36
37
38 public class WrongAltProcessing {
39
40 private static Robot robot;
41 private static JFrame firstFrame;
42 private static JFrame secondFrame;
43 private static JTextField mainFrameTf1;
44 private static JTextField mainFrameTf2;
45 private static JTextField secondFrameTf;
59 private static void verify() {
60 Component c = DefaultKeyboardFocusManager
61 .getCurrentKeyboardFocusManager().getFocusOwner();
62 if (!(c == mainFrameTf2)) {
63 throw new RuntimeException("Wrong focus owner.");
64 }
65 }
66
67 public static void sync() {
68 SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
69 toolkit.realSync();
70 }
71
72 public static void initRobot() throws AWTException {
73 robot = new Robot();
74 robot.setAutoDelay(100);
75 }
76
77 private static void clickWindowsTitle(JFrame frame) {
78 Point point = frame.getLocationOnScreen();
79 robot.mouseMove(point.x + (frame.getWidth() / 2), point.y + 5);
80 robot.mousePress(InputEvent.BUTTON1_MASK);
81 robot.mouseRelease(InputEvent.BUTTON1_MASK);
82 }
83
84 public static void runScript() {
85 robot.delay(1000);
86 printABCD();
87 pressTab();
88 clickWindowsTitle(secondFrame);
89 robot.delay(500);
90 robot.keyPress(KeyEvent.VK_ALT);
91 robot.keyRelease(KeyEvent.VK_ALT);
92 clickWindowsTitle(firstFrame);
93 sync();
94 }
95
96 private static void pressTab() {
97 robot.keyPress(KeyEvent.VK_TAB);
98 robot.keyRelease(KeyEvent.VK_TAB);
99 }
100
101 private static void printABCD() {
102 robot.keyPress(KeyEvent.VK_A);
103 robot.keyRelease(KeyEvent.VK_A);
104 robot.keyPress(KeyEvent.VK_B);
105 robot.keyRelease(KeyEvent.VK_B);
106 robot.keyPress(KeyEvent.VK_C);
107 robot.keyRelease(KeyEvent.VK_C);
108 robot.keyPress(KeyEvent.VK_D);
109 robot.keyRelease(KeyEvent.VK_D);
110 }
111
112 public static void createWindows() {
113 firstFrame = new JFrame("Frame");
114 firstFrame.setLayout(new FlowLayout());
115
116 JMenuBar bar = new JMenuBar();
117 JMenu menu = new JMenu("File");
118 JMenuItem item = new JMenuItem("Save");
119
120 mainFrameTf1 = new JTextField(10);
121 mainFrameTf2 = new JTextField(10);
122
123 mainFrameTf1.addKeyListener(new KeyAdapter() {
124 public void keyPressed(KeyEvent EVT) {
125 if (EVT.getKeyChar() >= 'a' && EVT.getKeyChar() <= 'z') {
126 try {
127 // imitate some long processing
128 Thread.sleep(2000);
129 } catch (InterruptedException e) {
130 throw new RuntimeException(e);
131 }
132 }
133 }
134 });
135
136 menu.add(item);
137 bar.add(menu);
138 firstFrame.setJMenuBar(bar);
139
140
141 firstFrame.add(mainFrameTf1);
142 firstFrame.add(mainFrameTf2);
143
144 firstFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
145
146 firstFrame.pack();
147
148 secondFrame = new JFrame("Frame 2");
149 secondFrame.setLocation(0, 150);
150 secondFrameTf = new JTextField(20);
151 secondFrame.add(secondFrameTf);
152 secondFrame.pack();
153 SwingUtilities.invokeLater(new Runnable() {
154 @Override
155 public void run() {
156 secondFrame.setVisible(true);
157 }
158 });
159 SwingUtilities.invokeLater(new Runnable() {
160 @Override
161 public void run() {
162 firstFrame.setVisible(true);
163 }
164 });
165
166 mainFrameTf1.requestFocus();
167 sync();
168 }
|
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 /* @test
25 @bug 8001633 8028271
26 @summary Wrong alt processing during switching between windows
27 @author mikhail.cherkasov@oracle.com
28 @run main WrongAltProcessing
29 */
30
31 import sun.awt.SunToolkit;
32
33 import javax.swing.*;
34 import java.awt.*;
35 import java.awt.event.*;
36
37
38 public class WrongAltProcessing {
39
40 private static Robot robot;
41 private static JFrame firstFrame;
42 private static JFrame secondFrame;
43 private static JTextField mainFrameTf1;
44 private static JTextField mainFrameTf2;
45 private static JTextField secondFrameTf;
59 private static void verify() {
60 Component c = DefaultKeyboardFocusManager
61 .getCurrentKeyboardFocusManager().getFocusOwner();
62 if (!(c == mainFrameTf2)) {
63 throw new RuntimeException("Wrong focus owner.");
64 }
65 }
66
67 public static void sync() {
68 SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
69 toolkit.realSync();
70 }
71
72 public static void initRobot() throws AWTException {
73 robot = new Robot();
74 robot.setAutoDelay(100);
75 }
76
77 private static void clickWindowsTitle(JFrame frame) {
78 Point point = frame.getLocationOnScreen();
79 robot.mouseMove(point.x + (frame.getWidth() / 2), point.y + 10);
80 robot.mousePress(InputEvent.BUTTON1_MASK);
81 robot.mouseRelease(InputEvent.BUTTON1_MASK);
82 }
83
84 public static void runScript() {
85 robot.delay(1000);
86 printABCD();
87 pressTab();
88 clickWindowsTitle(secondFrame);
89 robot.delay(500);
90 robot.keyPress(KeyEvent.VK_ALT);
91 robot.keyRelease(KeyEvent.VK_ALT);
92 clickWindowsTitle(firstFrame);
93 sync();
94 }
95
96 private static void pressTab() {
97 robot.keyPress(KeyEvent.VK_TAB);
98 robot.keyRelease(KeyEvent.VK_TAB);
99 }
100
101 private static void printABCD() {
102 robot.keyPress(KeyEvent.VK_A);
103 robot.keyRelease(KeyEvent.VK_A);
104 robot.keyPress(KeyEvent.VK_B);
105 robot.keyRelease(KeyEvent.VK_B);
106 robot.keyPress(KeyEvent.VK_C);
107 robot.keyRelease(KeyEvent.VK_C);
108 robot.keyPress(KeyEvent.VK_D);
109 robot.keyRelease(KeyEvent.VK_D);
110 }
111
112 public static void createWindows() {
113 firstFrame = new JFrame("Frame");
114 firstFrame.setLayout(new FlowLayout());
115 firstFrame.setPreferredSize(new Dimension(600,100));
116
117 JMenuBar bar = new JMenuBar();
118 JMenu menu = new JMenu("File");
119 JMenuItem item = new JMenuItem("Save");
120
121 mainFrameTf1 = new JTextField(10);
122 mainFrameTf2 = new JTextField(10);
123
124 mainFrameTf1.addKeyListener(new KeyAdapter() {
125 public void keyPressed(KeyEvent EVT) {
126 if (EVT.getKeyChar() >= 'a' && EVT.getKeyChar() <= 'z') {
127 try {
128 // imitate some long processing
129 Thread.sleep(2000);
130 } catch (InterruptedException e) {
131 throw new RuntimeException(e);
132 }
133 }
134 }
135 });
136
137 menu.add(item);
138 bar.add(menu);
139 firstFrame.setJMenuBar(bar);
140
141
142 firstFrame.add(mainFrameTf1);
143 firstFrame.add(mainFrameTf2);
144
145 firstFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
146
147 firstFrame.pack();
148
149 secondFrame = new JFrame("Frame 2");
150 secondFrame.setPreferredSize(new Dimension(600,100));
151 secondFrame.setLocation(0, 150);
152 secondFrameTf = new JTextField(20);
153 secondFrame.add(secondFrameTf);
154 secondFrame.pack();
155 SwingUtilities.invokeLater(new Runnable() {
156 @Override
157 public void run() {
158 secondFrame.setVisible(true);
159 }
160 });
161 SwingUtilities.invokeLater(new Runnable() {
162 @Override
163 public void run() {
164 firstFrame.setVisible(true);
165 }
166 });
167
168 mainFrameTf1.requestFocus();
169 sync();
170 }
|