jdk/test/java/awt/TextArea/TextAreaCaretVisibilityTest/bug7129742.java
author serb
Fri, 17 Apr 2015 16:54:13 +0300
changeset 30469 bac0a7ff7e1e
parent 27763 1fba965925e8
child 31660 1a4e2e5c15e1
permissions -rw-r--r--
8074028: Remove API references to java.awt.peer Reviewed-by: alanb, ant, yan, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12523
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
     1
/*
30469
bac0a7ff7e1e 8074028: Remove API references to java.awt.peer
serb
parents: 27763
diff changeset
     2
 * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
12523
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
     4
 *
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
     7
 * published by the Free Software Foundation.
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
     8
 *
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    13
 * accompanied this code).
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    14
 *
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    18
 *
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    21
 * questions.
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    22
 */
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    23
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    24
/*
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    25
 * Portions Copyright (c) 2012 IBM Corporation
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    26
 */
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    27
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    28
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    29
/* @test
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    30
 * @bug 7129742
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    31
 * @summary Focus in non-editable TextArea is not shown on Linux.
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    32
 * @author Sean Chou
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    33
 */
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    34
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    35
import java.awt.FlowLayout;
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    36
import java.awt.TextArea;
27763
1fba965925e8 8063102: Change open awt regression tests to avoid sun.awt.SunToolkit.realSync, part 1
yan
parents: 14856
diff changeset
    37
import java.awt.Robot;
12523
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    38
import java.lang.reflect.Field;
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    39
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    40
import javax.swing.JFrame;
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    41
import javax.swing.JTextArea;
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    42
import javax.swing.SwingUtilities;
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    43
import javax.swing.text.DefaultCaret;
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    44
30469
bac0a7ff7e1e 8074028: Remove API references to java.awt.peer
serb
parents: 27763
diff changeset
    45
import sun.awt.AWTAccessor;
bac0a7ff7e1e 8074028: Remove API references to java.awt.peer
serb
parents: 27763
diff changeset
    46
import sun.awt.AWTAccessor.ComponentAccessor;
bac0a7ff7e1e 8074028: Remove API references to java.awt.peer
serb
parents: 27763
diff changeset
    47
12523
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    48
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    49
public class bug7129742 {
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    50
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    51
    public static DefaultCaret caret = null;
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    52
    public static JFrame frame = null;
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    53
    public static boolean fastreturn = false;
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    54
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    55
    public static void main(String[] args) throws Exception {
27763
1fba965925e8 8063102: Change open awt regression tests to avoid sun.awt.SunToolkit.realSync, part 1
yan
parents: 14856
diff changeset
    56
        Robot robot = new Robot();
12523
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    57
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    58
        SwingUtilities.invokeAndWait(new Runnable() {
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    59
            @Override
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    60
            public void run() {
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    61
                frame = new JFrame("Test");
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    62
                TextArea textArea = new TextArea("Non-editable textArea");
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    63
                textArea.setEditable(false);
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    64
                frame.setLayout(new FlowLayout());
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    65
                frame.add(textArea);
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    66
                frame.pack();
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    67
                frame.setVisible(true);
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    68
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    69
                try {
30469
bac0a7ff7e1e 8074028: Remove API references to java.awt.peer
serb
parents: 27763
diff changeset
    70
                    ComponentAccessor acc = AWTAccessor.getComponentAccessor();
bac0a7ff7e1e 8074028: Remove API references to java.awt.peer
serb
parents: 27763
diff changeset
    71
                    Class XTextAreaPeerClzz = acc.getPeer(textArea).getClass();
12523
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    72
                    System.out.println(XTextAreaPeerClzz.getName());
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    73
                    if (!XTextAreaPeerClzz.getName().equals("sun.awt.X11.XTextAreaPeer")) {
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    74
                        fastreturn = true;
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    75
                        return;
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    76
                    }
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    77
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    78
                    Field jtextField = XTextAreaPeerClzz.getDeclaredField("jtext");
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    79
                    jtextField.setAccessible(true);
30469
bac0a7ff7e1e 8074028: Remove API references to java.awt.peer
serb
parents: 27763
diff changeset
    80
                    JTextArea jtext = (JTextArea)jtextField.get(acc.getPeer(textArea));
12523
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    81
                    caret = (DefaultCaret) jtext.getCaret();
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    82
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    83
                    textArea.requestFocusInWindow();
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    84
                } catch (NoSuchFieldException | SecurityException
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    85
                         | IllegalArgumentException | IllegalAccessException e) {
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    86
                    /* These exceptions mean the implementation of XTextAreaPeer is
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    87
                     * changed, this testcase is not valid any more, fix it or remove.
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    88
                     */
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    89
                    frame.dispose();
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    90
                    throw new RuntimeException("This testcase is not valid any more!");
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    91
                }
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    92
            }
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    93
        });
27763
1fba965925e8 8063102: Change open awt regression tests to avoid sun.awt.SunToolkit.realSync, part 1
yan
parents: 14856
diff changeset
    94
        robot.waitForIdle();
12523
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    95
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    96
        SwingUtilities.invokeAndWait(new Runnable() {
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    97
            @Override
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    98
            public void run() {
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
    99
                try{
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
   100
                    if (fastreturn) {
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
   101
                        return;
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
   102
                    }
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
   103
                    boolean passed = caret.isActive();
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
   104
                    System.out.println("is caret visible : " + passed);
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
   105
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
   106
                    if (!passed) {
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
   107
                        throw new RuntimeException("The test for bug 71297422 failed");
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
   108
                    }
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
   109
                } finally {
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
   110
                    frame.dispose();
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
   111
                }
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
   112
            }
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
   113
        });
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
   114
    }
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
   115
a3c0565476da 7129742: Unable to view focus in Non-Editable TextArea
zhouyx
parents:
diff changeset
   116
}