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