|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2013, 2021, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
@@ -54,88 +54,99 @@ public class KeyEventForBadFocusOwnerTest {
|
54 | 54 | volatile static boolean unexpectedItemSelected = false;
|
55 | 55 |
|
56 | 56 | static Robot robot;
|
| 57 | + static JFrame frame; |
57 | 58 |
|
58 | 59 | public static void main(String[] args) throws Exception {
|
59 |
| - SwingUtilities.invokeAndWait(new Runnable() { |
60 |
| - public void run() { |
61 |
| - JFrame frame = new JFrame("TEST"); |
62 |
| - JMenuBar mb = new JMenuBar(); |
63 |
| - JMenu one = new JMenu(ITEM_ONE_TEXT); |
64 |
| - JMenu two = new JMenu(ITEM_TWO_TEXT); |
65 |
| - |
66 |
| - mb.add(one); |
67 |
| - mb.add(two); |
68 |
| - |
69 |
| - ActionListener al = new ActionListener() { |
70 |
| - public void actionPerformed(ActionEvent ae) { |
71 |
| - String itemText = ((JMenuItem)ae.getSource()).getText(); |
72 |
| - System.out.println("--> " + itemText); |
73 |
| - unexpectedItemSelected = true; |
74 |
| - } |
75 |
| - }; |
76 |
| - one.setMnemonic(KeyEvent.VK_O); |
77 |
| - JMenuItem item = new JMenuItem("one 1"); |
78 |
| - item.setMnemonic(KeyEvent.VK_O); |
79 |
| - item.addActionListener(al); |
80 |
| - one.add(item); |
81 |
| - one.add("two"); |
82 |
| - one.add("three"); |
83 |
| - |
84 |
| - two.setMnemonic(KeyEvent.VK_T); |
85 |
| - item = new JMenuItem("two 2"); |
86 |
| - item.setMnemonic(KeyEvent.VK_T); |
87 |
| - item.addActionListener(al); |
88 |
| - two.add(item); |
89 |
| - two.add("three"); |
90 |
| - two.add("four"); |
91 |
| - |
92 |
| - PopupMenuListener popupMenuListener = new PopupMenuListener() { |
93 |
| - public void popupMenuWillBecomeVisible(PopupMenuEvent e) { |
94 |
| - System.out.print(e); |
95 |
| - System.out.print(e.getSource()); |
96 |
| - String itemText = ((JPopupMenu)e.getSource()).getName(); |
97 |
| - System.out.println("Menu " + itemText + "is opened."); |
98 |
| - switch(itemText) { |
99 |
| - case ITEM_ONE_TEXT: |
100 |
| - itemOneSelected = true; |
101 |
| - break; |
102 |
| - case ITEM_TWO_TEXT: |
103 |
| - itemTwoSelected = true; |
104 |
| - break; |
| 60 | + try { |
| 61 | + SwingUtilities.invokeAndWait(new Runnable() { |
| 62 | + public void run() { |
| 63 | + frame = new JFrame("TEST"); |
| 64 | + JMenuBar mb = new JMenuBar(); |
| 65 | + JMenu one = new JMenu(ITEM_ONE_TEXT); |
| 66 | + JMenu two = new JMenu(ITEM_TWO_TEXT); |
| 67 | + |
| 68 | + mb.add(one); |
| 69 | + mb.add(two); |
| 70 | + |
| 71 | + ActionListener al = new ActionListener() { |
| 72 | + public void actionPerformed(ActionEvent ae) { |
| 73 | + String itemText = ((JMenuItem)ae.getSource()).getText(); |
| 74 | + System.out.println("--> " + itemText); |
| 75 | + unexpectedItemSelected = true; |
| 76 | + } |
| 77 | + }; |
| 78 | + one.setMnemonic(KeyEvent.VK_O); |
| 79 | + JMenuItem item = new JMenuItem("one 1"); |
| 80 | + item.setMnemonic(KeyEvent.VK_O); |
| 81 | + item.addActionListener(al); |
| 82 | + one.add(item); |
| 83 | + one.add("two"); |
| 84 | + one.add("three"); |
| 85 | + |
| 86 | + two.setMnemonic(KeyEvent.VK_T); |
| 87 | + item = new JMenuItem("two 2"); |
| 88 | + item.setMnemonic(KeyEvent.VK_T); |
| 89 | + item.addActionListener(al); |
| 90 | + two.add(item); |
| 91 | + two.add("three"); |
| 92 | + two.add("four"); |
| 93 | + |
| 94 | + PopupMenuListener popupMenuListener = new PopupMenuListener() { |
| 95 | + public void popupMenuWillBecomeVisible(PopupMenuEvent e) { |
| 96 | + System.out.print(e); |
| 97 | + System.out.print(e.getSource()); |
| 98 | + String itemText = ((JPopupMenu)e.getSource()).getName(); |
| 99 | + System.out.println("Menu " + itemText + "is opened."); |
| 100 | + switch(itemText) { |
| 101 | + case ITEM_ONE_TEXT: |
| 102 | + itemOneSelected = true; |
| 103 | + break; |
| 104 | + case ITEM_TWO_TEXT: |
| 105 | + itemTwoSelected = true; |
| 106 | + break; |
| 107 | + } |
105 | 108 | }
|
106 |
| - } |
107 |
| - |
108 |
| - public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {} |
109 |
| - public void popupMenuCanceled(PopupMenuEvent e) {} |
110 |
| - }; |
111 |
| - one.getPopupMenu().setName(ITEM_ONE_TEXT); |
112 |
| - two.getPopupMenu().setName(ITEM_TWO_TEXT); |
113 |
| - one.getPopupMenu().addPopupMenuListener(popupMenuListener); |
114 |
| - two.getPopupMenu().addPopupMenuListener(popupMenuListener); |
115 |
| - frame.setJMenuBar(mb); |
116 |
| - frame.setSize(100,100); |
117 |
| - frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
118 |
| - frame.pack(); |
119 |
| - frame.setVisible(true); |
120 |
| - } |
121 |
| - }); |
122 |
| - |
123 |
| - |
124 |
| - robot = new Robot(); |
125 |
| - robot.setAutoDelay(100); |
126 |
| - robot.waitForIdle(); |
127 |
| - |
128 |
| - Util.hitMnemonics(robot, KeyEvent.VK_O); |
129 |
| - Util.hitMnemonics(robot, KeyEvent.VK_T); |
130 |
| - |
131 |
| - robot.waitForIdle(); |
132 |
| - Thread.sleep(1000); // workaround for MacOS |
133 | 109 |
|
134 |
| - if (unexpectedItemSelected) { |
135 |
| - throw new Exception("Test failed. KeyEvent dispatched to old focus owner. "); |
136 |
| - } |
137 |
| - if (!itemOneSelected || !itemTwoSelected) { |
138 |
| - throw new Exception("Not all expected events were received"); |
| 110 | + public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {} |
| 111 | + public void popupMenuCanceled(PopupMenuEvent e) {} |
| 112 | + }; |
| 113 | + one.getPopupMenu().setName(ITEM_ONE_TEXT); |
| 114 | + two.getPopupMenu().setName(ITEM_TWO_TEXT); |
| 115 | + one.getPopupMenu().addPopupMenuListener(popupMenuListener); |
| 116 | + two.getPopupMenu().addPopupMenuListener(popupMenuListener); |
| 117 | + frame.setJMenuBar(mb); |
| 118 | + frame.setSize(100,100); |
| 119 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 120 | + frame.setLocationRelativeTo(null); |
| 121 | + frame.pack(); |
| 122 | + frame.setVisible(true); |
| 123 | + } |
| 124 | + }); |
| 125 | + |
| 126 | + |
| 127 | + robot = new Robot(); |
| 128 | + robot.setAutoDelay(100); |
| 129 | + robot.waitForIdle(); |
| 130 | + robot.delay(1000); |
| 131 | + |
| 132 | + Util.hitMnemonics(robot, KeyEvent.VK_O); |
| 133 | + Util.hitMnemonics(robot, KeyEvent.VK_T); |
| 134 | + |
| 135 | + robot.waitForIdle(); |
| 136 | + Thread.sleep(1000); // workaround for MacOS |
| 137 | + |
| 138 | + if (unexpectedItemSelected) { |
| 139 | + throw new Exception("Test failed. KeyEvent dispatched to old focus owner. "); |
| 140 | + } |
| 141 | + if (!itemOneSelected || !itemTwoSelected) { |
| 142 | + throw new Exception("Not all expected events were received"); |
| 143 | + } |
| 144 | + } finally { |
| 145 | + SwingUtilities.invokeAndWait(() -> { |
| 146 | + if (frame != null) { |
| 147 | + frame.dispose(); |
| 148 | + } |
| 149 | + }); |
139 | 150 | }
|
140 | 151 | }
|
141 | 152 | }
|
0 commit comments