test/java/awt/TextArea/TextAreaCaretVisibilityTest/bug7129742.java

Print this page


   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  */


  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.Robot;
  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 
  46 public class bug7129742 {
  47 
  48     public static DefaultCaret caret = null;
  49     public static JFrame frame = null;
  50     public static boolean fastreturn = false;
  51 
  52     public static void main(String[] args) throws Exception {
  53         Robot robot = new Robot();
  54 
  55         SwingUtilities.invokeAndWait(new Runnable() {
  56             @Override
  57             public void run() {
  58                 frame = new JFrame("Test");
  59                 TextArea textArea = new TextArea("Non-editable textArea");
  60                 textArea.setEditable(false);
  61                 frame.setLayout(new FlowLayout());
  62                 frame.add(textArea);
  63                 frame.pack();
  64                 frame.setVisible(true);
  65 
  66                 try {
  67                     Class XTextAreaPeerClzz  = textArea.getPeer().getClass();

  68                     System.out.println(XTextAreaPeerClzz.getName());
  69                     if (!XTextAreaPeerClzz.getName().equals("sun.awt.X11.XTextAreaPeer")) {
  70                         fastreturn = true;
  71                         return;
  72                     }
  73 
  74                     Field jtextField = XTextAreaPeerClzz.getDeclaredField("jtext");
  75                     jtextField.setAccessible(true);
  76                     JTextArea jtext = (JTextArea)jtextField.get(textArea.getPeer());
  77                     caret = (DefaultCaret) jtext.getCaret();
  78 
  79                     textArea.requestFocusInWindow();
  80                 } catch (NoSuchFieldException | SecurityException
  81                          | IllegalArgumentException | IllegalAccessException e) {
  82                     /* These exceptions mean the implementation of XTextAreaPeer is
  83                      * changed, this testcase is not valid any more, fix it or remove.
  84                      */
  85                     frame.dispose();
  86                     throw new RuntimeException("This testcase is not valid any more!");
  87                 }
  88             }
  89         });
  90         robot.waitForIdle();
  91 
  92         SwingUtilities.invokeAndWait(new Runnable() {
  93             @Override
  94             public void run() {
  95                 try{
  96                     if (fastreturn) {
   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  */


  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.Robot;
  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.AWTAccessor;
  46 import sun.awt.AWTAccessor.ComponentAccessor;
  47 
  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         Robot robot = new Robot();
  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                     ComponentAccessor acc = AWTAccessor.getComponentAccessor();
  71                     Class XTextAreaPeerClzz = acc.getPeer(textArea).getClass();
  72                     System.out.println(XTextAreaPeerClzz.getName());
  73                     if (!XTextAreaPeerClzz.getName().equals("sun.awt.X11.XTextAreaPeer")) {
  74                         fastreturn = true;
  75                         return;
  76                     }
  77 
  78                     Field jtextField = XTextAreaPeerClzz.getDeclaredField("jtext");
  79                     jtextField.setAccessible(true);
  80                     JTextArea jtext = (JTextArea)jtextField.get(acc.getPeer(textArea));
  81                     caret = (DefaultCaret) jtext.getCaret();
  82 
  83                     textArea.requestFocusInWindow();
  84                 } catch (NoSuchFieldException | SecurityException
  85                          | IllegalArgumentException | IllegalAccessException e) {
  86                     /* These exceptions mean the implementation of XTextAreaPeer is
  87                      * changed, this testcase is not valid any more, fix it or remove.
  88                      */
  89                     frame.dispose();
  90                     throw new RuntimeException("This testcase is not valid any more!");
  91                 }
  92             }
  93         });
  94         robot.waitForIdle();
  95 
  96         SwingUtilities.invokeAndWait(new Runnable() {
  97             @Override
  98             public void run() {
  99                 try{
 100                     if (fastreturn) {