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