1 /* 2 * Copyright (c) 2012, 2015, 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 * Portions Copyright (c) 2012 IBM Corporation 26 */ 27 28 29 /* @test 30 * @bug 7129742 31 * @summary Focus in non-editable TextArea is not shown on Linux. 32 * @author Sean Chou 33 */ 34 35 import java.awt.FlowLayout; 36 import java.awt.TextArea; 37 import java.awt.Robot; 38 import java.lang.reflect.Field; 39 40 import javax.swing.JFrame; 41 import javax.swing.JTextArea; 42 import javax.swing.SwingUtilities; 43 import javax.swing.text.DefaultCaret; 44 45 import sun.awt.AWTAccessor; 46 import sun.awt.AWTAccessor.ComponentAccessor; 47 48 49 public class bug7129742 { 50 51 public static DefaultCaret caret = null; 52 public static JFrame frame = null; 53 public static boolean fastreturn = false; 54 55 public static void main(String[] args) throws Exception { 56 Robot robot = new Robot(); 57 58 SwingUtilities.invokeAndWait(new Runnable() { 59 @Override 60 public void run() { 61 frame = new JFrame("Test"); 62 TextArea textArea = new TextArea("Non-editable textArea"); 63 textArea.setEditable(false); 64 frame.setLayout(new FlowLayout()); 65 frame.add(textArea); 66 frame.pack(); 67 frame.setVisible(true); 68 69 try { 70 ComponentAccessor acc = AWTAccessor.getComponentAccessor(); 71 Class XTextAreaPeerClzz = acc.getPeer(textArea).getClass(); 72 System.out.println(XTextAreaPeerClzz.getName()); 73 if (!XTextAreaPeerClzz.getName().equals("sun.awt.X11.XTextAreaPeer")) { 74 fastreturn = true; 75 return; 76 } 77 78 Field jtextField = XTextAreaPeerClzz.getDeclaredField("jtext"); 79 jtextField.setAccessible(true); 80 JTextArea jtext = (JTextArea)jtextField.get(acc.getPeer(textArea)); 81 caret = (DefaultCaret) jtext.getCaret(); 82 83 textArea.requestFocusInWindow(); 84 } catch (NoSuchFieldException | SecurityException 85 | IllegalArgumentException | IllegalAccessException e) { 86 /* These exceptions mean the implementation of XTextAreaPeer is 87 * changed, this testcase is not valid any more, fix it or remove. 88 */ 89 frame.dispose(); 90 throw new RuntimeException("This testcase is not valid any more!"); 91 } 92 } 93 }); 94 robot.waitForIdle(); 95 96 SwingUtilities.invokeAndWait(new Runnable() { 97 @Override 98 public void run() { 99 try{ 100 if (fastreturn) { 101 return; 102 } 103 boolean passed = caret.isActive(); 104 System.out.println("is caret visible : " + passed); 105 106 if (!passed) { 107 throw new RuntimeException("The test for bug 71297422 failed"); 108 } 109 } finally { 110 frame.dispose(); 111 } 112 } 113 }); 114 } 115 116 }