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