1 /* 2 * Copyright (c) 2011, 2013, 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 @summary <rdar://problem/4307013> When hiding popup, focus is gone 26 * JEditorPane 27 * @summary com.apple.junit.java.awt.Event; 28 * @library ../../../../java/awt/regtesthelpers 29 * @build RobotUtilities 30 * @build VisibilityValidator 31 * @run main R4307013_PopupVsFocus 32 */ 33 import junit.framework.*; 34 35 import javax.swing.*; 36 import java.awt.*; 37 import java.awt.event.*; 38 import sun.awt.SunToolkit; 39 40 import test.java.awt.regtesthelpers.RobotUtilities; 41 import test.java.awt.regtesthelpers.VisibilityValidator; 42 43 public class R4307013_PopupVsFocus extends TestCase { 44 45 public static Test suite() { 46 return new TestSuite(R4307013_PopupVsFocus.class); 47 } 48 49 public static void main(String[] args) throws RuntimeException { 50 String name = System.getProperty("os.name"); 51 if (name.equals("Mac OS X")) { 52 // This test makes a Mac OS X assumption about the trigger for popup menus 53 TestResult tr = junit.textui.TestRunner.run(suite()); 54 if ((tr.errorCount() != 0) || (tr.failureCount() != 0)) { 55 throw new RuntimeException("### Unexpected JUnit errors or failures."); 56 } 57 } 58 } 59 private javax.swing.JEditorPane jEditorPane1; 60 private javax.swing.JScrollPane jScrollPane1; 61 volatile MyPanel panel; 62 volatile JFrame jframe; 63 64 public void testPopupVsFocus() throws Exception { 65 SwingUtilities.invokeAndWait(new Runnable() { 66 @Override 67 public void run() { 68 jframe = new JFrame(); 69 70 jScrollPane1 = new javax.swing.JScrollPane(); 71 jEditorPane1 = new javax.swing.JEditorPane(); 72 73 jEditorPane1.setText("TESTING"); 74 jScrollPane1.setViewportView(jEditorPane1); 75 76 jframe.getContentPane().add(jScrollPane1); 77 jframe.pack(); 78 jframe.setSize(300, 300); 79 80 jEditorPane1.getActionMap().put("XXXPopup", new PopupAction()); 81 jEditorPane1.getInputMap().put(KeyStroke.getKeyStroke("control shift X"), "XXXPopup"); 82 } 83 }); 84 85 VisibilityValidator.setVisibleAndConfirm(jframe); 86 87 jEditorPane1.requestFocusInWindow(); 88 ((SunToolkit) Toolkit.getDefaultToolkit()).realSync(); 89 90 // activate the popup 91 RobotUtilities.pressKey(KeyEvent.VK_CONTROL); 92 RobotUtilities.pressKey(KeyEvent.VK_SHIFT); 93 RobotUtilities.typeKey(KeyEvent.VK_X); 94 RobotUtilities.releaseKey(KeyEvent.VK_SHIFT); 95 RobotUtilities.releaseKey(KeyEvent.VK_CONTROL); 96 ((SunToolkit) Toolkit.getDefaultToolkit()).realSync(); 97 98 // click on the popup, dismissing it 99 RobotUtilities.click(panel); 100 ((SunToolkit) Toolkit.getDefaultToolkit()).realSync(); 101 102 // see if we got back focus 103 RobotUtilities.typeKey(KeyEvent.VK_H); 104 RobotUtilities.typeKey(KeyEvent.VK_I); 105 106 String hiString = jEditorPane1.getText(); 107 assertTrue("EditorPane didn't get focus back after popup: " + hiString, (hiString.indexOf("hi") != -1)); 108 109 SwingUtilities.invokeAndWait(new Runnable() { 110 @Override 111 public void run() { 112 if (jframe != null) { 113 jframe.setVisible(false); 114 jframe.dispose(); 115 jframe = null; 116 } 117 } 118 }); 119 } 120 121 private class PopupAction extends AbstractAction { 122 123 public void actionPerformed(java.awt.event.ActionEvent e) { 124 MyPanel pnl = new MyPanel(); 125 panel = pnl; 126 Point point = new Point(100, 100); 127 SwingUtilities.convertPointToScreen(new Point(10, 10), jEditorPane1); 128 final Popup popup = PopupFactory.getSharedInstance().getPopup(jframe, pnl, point.x, point.y); 129 pnl.list.addMouseListener(new MouseListener() { 130 public void mouseClicked(MouseEvent me) { 131 popup.hide(); 132 me.consume(); 133 panel = null; 134 } 135 136 public void mouseEntered(MouseEvent me) { 137 } 138 139 public void mouseExited(MouseEvent me) { 140 } 141 142 public void mousePressed(MouseEvent me) { 143 me.consume(); 144 } 145 146 public void mouseReleased(MouseEvent me) { 147 me.consume(); 148 } 149 }); 150 popup.show(); 151 } 152 } 153 154 private class MyPanel extends JPanel { 155 156 public JList list = new JList(new Object[]{ 157 "itemx1", 158 "itemx2", 159 "item11", 160 "item22", 161 "item01", 162 "item02", 163 "item1", 164 "item2", 165 "item3" 166 }); 167 168 public MyPanel() { 169 setLayout(new BorderLayout()); 170 add(list, BorderLayout.CENTER); 171 } 172 } 173 }