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