1 /* 2 * Copyright (c) 2016, 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 /* @test 25 * @bug 8041909 26 * @summary Test to check JComboBox does not lose its ability to invoke 27 * registerd ActionListener in case of exception in ActionListener 28 * @run main ActionListenerExceptionTest 29 */ 30 31 import java.awt.AWTEvent; 32 import java.awt.AWTException; 33 import java.awt.Dimension; 34 import java.awt.EventQueue; 35 import java.awt.Point; 36 import java.awt.Robot; 37 import java.awt.Toolkit; 38 import java.awt.event.ActionEvent; 39 import java.awt.event.ActionListener; 40 import java.awt.event.InputEvent; 41 import java.util.logging.Level; 42 import java.util.logging.Logger; 43 import javax.swing.JComboBox; 44 import javax.swing.JComponent; 45 import javax.swing.JFrame; 46 import javax.swing.JPopupMenu; 47 import javax.swing.JScrollPane; 48 import javax.swing.SwingUtilities; 49 50 public class ActionListenerExceptionTest { 51 52 static final int TOTAL_MENU_ITEMS = 3; 53 private volatile int count = 0; 54 private JFrame frame; 55 private JComboBox combo; 56 57 private int menuItemHeight = 0; 58 private int yPos = 0; 59 private Point cbPos = null; 60 private Dimension cbSize = null; 61 62 63 public static void main(String[] args) throws Exception { 64 65 // See EvenQueueProxy class description below. 66 EventQueue queue = Toolkit.getDefaultToolkit().getSystemEventQueue(); 67 queue.push(new EventQueueProxy()); 68 69 ActionListenerExceptionTest testObject = new ActionListenerExceptionTest(); 70 71 testObject.createGUI(); 72 73 testObject.test(); 74 75 testObject.disposeGUI(); 76 77 if (testObject.getCount() != TOTAL_MENU_ITEMS) { 78 throw new RuntimeException("ActionListener is not invoked expected number of times"); 79 } 80 } 81 82 private void createGUI() throws Exception { 83 SwingUtilities.invokeAndWait(new Runnable() { 84 public void run() { 85 frame = new JFrame(); 86 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 87 88 combo = new JComboBox(new String[]{"One", "Two", "Three"}); 89 combo.addActionListener(new ActionListener() { 90 91 @Override 92 public void actionPerformed(ActionEvent e) { 93 count++; 94 throw new RuntimeException(); 95 } 96 }); 97 combo.setSize(200, 20); 98 frame.add(combo); 99 frame.pack(); 100 frame.setLocationRelativeTo(null); 101 frame.setVisible(true); 102 103 } 104 }); 105 } 106 107 private void disposeGUI() throws Exception { 108 SwingUtilities.invokeAndWait(new Runnable() { 109 public void run() { 110 frame.dispose(); 111 } 112 }); 113 } 114 115 private void test() throws Exception { 116 Robot testRobot = new Robot(); 117 testRobot.delay(200); // delay to make test frame visible on screen 118 119 SwingUtilities.invokeAndWait(new Runnable() { 120 public void run() { 121 cbPos = combo.getLocationOnScreen(); 122 cbSize = combo.getSize(); 123 } 124 }); 125 126 Point center = new Point((cbPos.x + cbSize.width / 2), (cbPos.y + cbSize.height - 5)); 127 testRobot.mouseMove(center.x, center.y); 128 testRobot.delay(100); 129 130 testRobot.mousePress(InputEvent.BUTTON1_DOWN_MASK); 131 testRobot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); 132 testRobot.delay(500); // delay to make popup visible 133 134 SwingUtilities.invokeAndWait(new Runnable() { 135 public void run() { 136 Object comp = combo.getUI().getAccessibleChild(combo, 0); 137 int i = 0; 138 JComponent scrollPane; 139 do { 140 scrollPane = (JComponent) ((JPopupMenu) comp).getComponent(i++); 141 } while (!(scrollPane instanceof JScrollPane)); 142 143 menuItemHeight = scrollPane.getSize().height / TOTAL_MENU_ITEMS; 144 yPos = scrollPane.getLocationOnScreen().y + menuItemHeight / 2; 145 } 146 }); 147 148 for (int i = 0; i < TOTAL_MENU_ITEMS; i++) { 149 150 testRobot.mouseMove(center.x, yPos); 151 152 testRobot.mousePress(InputEvent.BUTTON1_DOWN_MASK); 153 testRobot.delay(100); 154 testRobot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); 155 testRobot.delay(200); 156 157 yPos += menuItemHeight; 158 } 159 160 } 161 162 private int getCount() { 163 return count; 164 } 165 } 166 167 // Proxy class to invoke dispatchEvent and catch exceptions 168 // 169 // This is needed in order to test Exceptions from ActionListener 170 // Without this, jtreg reports test failure at first exception from ActionListener and 171 // we cannot test whether ActionListener is invoked again 172 class EventQueueProxy extends EventQueue { 173 174 protected void dispatchEvent(AWTEvent evt) { 175 try { 176 super.dispatchEvent(evt); 177 } catch (Exception e) { 178 System.out.println("Intentionally consumed Exception from ActionListener"); 179 e.printStackTrace(); 180 } 181 } 182 } 183