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 /*
  30  * @test
  31  * @key headful
  32  * @bug 7129742
  33  * @summary Focus in non-editable TextArea is not shown on Linux.
  34  * @author Sean Chou
  35  */
  36 
  37 import java.awt.FlowLayout;
  38 import java.awt.TextArea;
  39 import java.awt.Toolkit;
  40 import java.lang.reflect.Field;
  41 
  42 import javax.swing.JFrame;
  43 import javax.swing.JTextArea;
  44 import javax.swing.SwingUtilities;
  45 import javax.swing.text.DefaultCaret;
  46 
  47 import sun.awt.SunToolkit;
  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         SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
  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                     Class XTextAreaPeerClzz  = textArea.getPeer().getClass();
  71                     System.out.println(XTextAreaPeerClzz.getName());
  72                     if (!XTextAreaPeerClzz.getName().equals("sun.awt.X11.XTextAreaPeer")) {
  73                         fastreturn = true;
  74                         return;
  75                     }
  76 
  77                     Field jtextField = XTextAreaPeerClzz.getDeclaredField("jtext");
  78                     jtextField.setAccessible(true);
  79                     JTextArea jtext = (JTextArea)jtextField.get(textArea.getPeer());
  80                     caret = (DefaultCaret) jtext.getCaret();
  81 
  82                     textArea.requestFocusInWindow();
  83                 } catch (NoSuchFieldException | SecurityException
  84                          | IllegalArgumentException | IllegalAccessException e) {
  85                     /* These exceptions mean the implementation of XTextAreaPeer is
  86                      * changed, this testcase is not valid any more, fix it or remove.
  87                      */
  88                     frame.dispose();
  89                     throw new RuntimeException("This testcase is not valid any more!");
  90                 }
  91             }
  92         });
  93         toolkit.realSync();
  94 
  95         SwingUtilities.invokeAndWait(new Runnable() {
  96             @Override
  97             public void run() {
  98                 try{
  99                     if (fastreturn) {
 100                         return;
 101                     }
 102                     boolean passed = caret.isActive();
 103                     System.out.println("is caret visible : " + passed);
 104 
 105                     if (!passed) {
 106                         throw new RuntimeException("The test for bug 71297422 failed");
 107                     }
 108                 } finally {
 109                     frame.dispose();
 110                 }
 111             }
 112         });
 113     }
 114 
 115 }