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